1 #include "log.h"
2 #include "sway/commands.h"
3 #include "sway/input/seat.h"
4 #include "sway/input/input-manager.h"
5 #include "util.h"
6 
handle_action(struct seat_config * sc,struct sway_seat * seat,const char * action)7 static struct cmd_results *handle_action(struct seat_config *sc,
8 		struct sway_seat *seat, const char *action) {
9 	struct sway_keyboard_shortcuts_inhibitor *sway_inhibitor = NULL;
10 	if (strcmp(action, "disable") == 0) {
11 		sc->shortcuts_inhibit = SHORTCUTS_INHIBIT_DISABLE;
12 
13 		wl_list_for_each(sway_inhibitor,
14 				&seat->keyboard_shortcuts_inhibitors, link) {
15 			wlr_keyboard_shortcuts_inhibitor_v1_deactivate(
16 					sway_inhibitor->inhibitor);
17 		}
18 
19 		sway_log(SWAY_DEBUG, "Deactivated all keyboard shortcuts inhibitors");
20 	} else {
21 		sway_inhibitor = keyboard_shortcuts_inhibitor_get_for_focused_surface(seat);
22 		if (!sway_inhibitor) {
23 			return cmd_results_new(CMD_FAILURE,
24 					"No inhibitor found for focused surface");
25 		}
26 
27 		struct wlr_keyboard_shortcuts_inhibitor_v1 *inhibitor =
28 			sway_inhibitor->inhibitor;
29 		bool inhibit;
30 		if (strcmp(action, "activate") == 0) {
31 			inhibit = true;
32 		} else if (strcmp(action, "deactivate") == 0) {
33 			inhibit = false;
34 		} else if (strcmp(action, "toggle") == 0) {
35 			inhibit = !inhibitor->active;
36 		} else {
37 			return cmd_results_new(CMD_INVALID, "Expected enable|"
38 					"disable|activate|deactivate|toggle");
39 		}
40 
41 		if (inhibit) {
42 			wlr_keyboard_shortcuts_inhibitor_v1_activate(inhibitor);
43 		} else {
44 			wlr_keyboard_shortcuts_inhibitor_v1_deactivate(inhibitor);
45 		}
46 
47 		sway_log(SWAY_DEBUG, "%sctivated keyboard shortcuts inhibitor",
48 				inhibit ? "A" : "Dea");
49 	}
50 
51 	return cmd_results_new(CMD_SUCCESS, NULL);
52 }
53 
54 // shortcuts_inhibitor [enable|disable|activate|deactivate|toggle]
seat_cmd_shortcuts_inhibitor(int argc,char ** argv)55 struct cmd_results *seat_cmd_shortcuts_inhibitor(int argc, char **argv) {
56 	struct cmd_results *error =
57 		checkarg(argc, "shortcuts_inhibitor", EXPECTED_EQUAL_TO, 1);
58 	if (error) {
59 		return error;
60 	}
61 
62 	struct seat_config *sc = config->handler_context.seat_config;
63 	if (!sc) {
64 		return cmd_results_new(CMD_FAILURE, "No seat defined");
65 	}
66 
67 	if (strcmp(argv[0], "enable") == 0) {
68 		sc->shortcuts_inhibit = SHORTCUTS_INHIBIT_ENABLE;
69 	// at runtime disable is an action that also deactivates all active
70 	// inhibitors handled in handle_action()
71 	} else if (strcmp(argv[0], "disable") == 0 && !config->active) {
72 		sc->shortcuts_inhibit = SHORTCUTS_INHIBIT_DISABLE;
73 	} else if (!config->active) {
74 		return cmd_results_new(CMD_INVALID, "only enable and disable "
75 				"can be used in the config");
76 	} else {
77 		if (strcmp(sc->name, "*") != 0) {
78 			struct sway_seat *seat = input_manager_get_seat(sc->name, false);
79 			if (!seat) {
80 				return cmd_results_new(CMD_FAILURE,
81 						"Seat %s does not exist", sc->name);
82 			}
83 			error = handle_action(sc, seat, argv[0]);
84 		} else {
85 			struct sway_seat *seat = NULL;
86 			wl_list_for_each(seat, &server.input->seats, link) {
87 				error = handle_action(sc, seat, argv[0]);
88 				if (error && error->status != CMD_SUCCESS) {
89 					break;
90 				}
91 			}
92 		}
93 	}
94 
95 	return error ? error : cmd_results_new(CMD_SUCCESS, NULL);
96 }
97