aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/seat/shortcuts_inhibitor.c
blob: 7c7f99cf020d0385623ea1397ef63f22f50e2ff9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "log.h"
#include "sway/commands.h"
#include "sway/input/seat.h"
#include "sway/input/input-manager.h"
#include "util.h"

static struct cmd_results *handle_action(struct seat_config *sc,
		struct sway_seat *seat, const char *action) {
	struct sway_keyboard_shortcuts_inhibitor *sway_inhibitor = NULL;
	if (strcmp(action, "disable") == 0) {
		sc->shortcuts_inhibit = SHORTCUTS_INHIBIT_DISABLE;

		wl_list_for_each(sway_inhibitor,
				&seat->keyboard_shortcuts_inhibitors, link) {
			wlr_keyboard_shortcuts_inhibitor_v1_deactivate(
					sway_inhibitor->inhibitor);
		}

		sway_log(SWAY_DEBUG, "Deactivated all keyboard shortcuts inhibitors");
	} else {
		sway_inhibitor = keyboard_shortcuts_inhibitor_get_for_focused_surface(seat);
		if (!sway_inhibitor) {
			return cmd_results_new(CMD_FAILURE,
					"No inhibitor found for focused surface");
		}

		struct wlr_keyboard_shortcuts_inhibitor_v1 *inhibitor =
			sway_inhibitor->inhibitor;
		bool inhibit;
		if (strcmp(action, "activate") == 0) {
			inhibit = true;
		} else if (strcmp(action, "deactivate") == 0) {
			inhibit = false;
		} else if (strcmp(action, "toggle") == 0) {
			inhibit = !inhibitor->active;
		} else {
			return cmd_results_new(CMD_INVALID, "Expected enable|"
					"disable|activate|deactivate|toggle");
		}

		if (inhibit) {
			wlr_keyboard_shortcuts_inhibitor_v1_activate(inhibitor);
		} else {
			wlr_keyboard_shortcuts_inhibitor_v1_deactivate(inhibitor);
		}

		sway_log(SWAY_DEBUG, "%sctivated keyboard shortcuts inhibitor",
				inhibit ? "A" : "Dea");
	}

	return cmd_results_new(CMD_SUCCESS, NULL);
}

// shortcuts_inhibitor [enable|disable|activate|deactivate|toggle]
struct cmd_results *seat_cmd_shortcuts_inhibitor(int argc, char **argv) {
	struct cmd_results *error =
		checkarg(argc, "shortcuts_inhibitor", EXPECTED_EQUAL_TO, 1);
	if (error) {
		return error;
	}

	struct seat_config *sc = config->handler_context.seat_config;
	if (!sc) {
		return cmd_results_new(CMD_FAILURE, "No seat defined");
	}

	if (strcmp(argv[0], "enable") == 0) {
		sc->shortcuts_inhibit = SHORTCUTS_INHIBIT_ENABLE;
	// at runtime disable is an action that also deactivates all active
	// inhibitors handled in handle_action()
	} else if (strcmp(argv[0], "disable") == 0 && !config->active) {
		sc->shortcuts_inhibit = SHORTCUTS_INHIBIT_DISABLE;
	} else if (!config->active) {
		return cmd_results_new(CMD_INVALID, "only enable and disable "
				"can be used in the config");
	} else {
		if (strcmp(sc->name, "*") != 0) {
			struct sway_seat *seat = input_manager_get_seat(sc->name, false);
			if (!seat) {
				return cmd_results_new(CMD_FAILURE,
						"Seat %s does not exist", sc->name);
			}
			error = handle_action(sc, seat, argv[0]);
		} else {
			struct sway_seat *seat = NULL;
			wl_list_for_each(seat, &server.input->seats, link) {
				error = handle_action(sc, seat, argv[0]);
				if (error && error->status != CMD_SUCCESS) {
					break;
				}
			}
		}
	}

	return error ? error : cmd_results_new(CMD_SUCCESS, NULL);
}