aboutsummaryrefslogtreecommitdiff
path: root/sway/commands/seat/pointer_constraint.c
blob: 3890ebde0f9fee9f74615ad4a08d654b3e2c83bb (plain)
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
#include <string.h>
#include <wlr/types/wlr_pointer_constraints_v1.h>
#include "sway/commands.h"
#include "sway/config.h"
#include "sway/input/cursor.h"
#include "sway/input/seat.h"

enum operation {
	OP_ENABLE,
	OP_DISABLE,
	OP_ESCAPE,
};

// pointer_constraint [enable|disable|escape]
struct cmd_results *seat_cmd_pointer_constraint(int argc, char **argv) {
	struct cmd_results *error = NULL;
	if ((error = checkarg(argc, "pointer_constraint", EXPECTED_EQUAL_TO, 1))) {
		return error;
	}
	if (!config->handler_context.seat_config) {
		return cmd_results_new(CMD_FAILURE, "No seat defined");
	}

	enum operation op;
	if (strcmp(argv[0], "enable") == 0) {
		op = OP_ENABLE;
	} else if (strcmp(argv[0], "disable") == 0) {
		op = OP_DISABLE;
	} else if (strcmp(argv[0], "escape") == 0) {
		op = OP_ESCAPE;
	} else {
		return cmd_results_new(CMD_FAILURE, "Expected enable|disable|escape");
	}

	if (op == OP_ESCAPE && config->reading) {
		return cmd_results_new(CMD_FAILURE, "Can only escape at runtime.");
	}

	struct seat_config *seat_config = config->handler_context.seat_config;
	switch (op) {
	case OP_ENABLE:
		seat_config->allow_constrain = CONSTRAIN_ENABLE;
		break;
	case OP_DISABLE:
		seat_config->allow_constrain = CONSTRAIN_DISABLE;
		/* fallthrough */
	case OP_ESCAPE:;
		bool wildcard = !strcmp(seat_config->name, "*");
		struct sway_seat *seat = NULL;
		wl_list_for_each(seat, &server.input->seats, link) {
			if (wildcard || !strcmp(seat->wlr_seat->name, seat_config->name)) {
				sway_cursor_constrain(seat->cursor, NULL);
			}
		}
		break;
	}
	return cmd_results_new(CMD_SUCCESS, NULL);
}