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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
#include "sway/config.h"
#include "sway/input/switch.h"
#include "log.h"
struct sway_switch *sway_switch_create(struct sway_seat *seat,
struct sway_seat_device *device) {
struct sway_switch *switch_device =
calloc(1, sizeof(struct sway_switch));
if (!sway_assert(switch_device, "could not allocate switch")) {
return NULL;
}
device->switch_device = switch_device;
switch_device->wlr = wlr_switch_from_input_device(device->input_device->wlr_device);
switch_device->seat_device = device;
switch_device->state = WLR_SWITCH_STATE_OFF;
wl_list_init(&switch_device->switch_toggle.link);
sway_log(SWAY_DEBUG, "Allocated switch for device");
return switch_device;
}
static bool sway_switch_trigger_test(enum sway_switch_trigger trigger,
enum wlr_switch_state state) {
switch (trigger) {
case SWAY_SWITCH_TRIGGER_ON:
return state == WLR_SWITCH_STATE_ON;
case SWAY_SWITCH_TRIGGER_OFF:
return state == WLR_SWITCH_STATE_OFF;
case SWAY_SWITCH_TRIGGER_TOGGLE:
return true;
}
abort(); // unreachable
}
static void execute_binding(struct sway_switch *sway_switch) {
struct sway_seat *seat = sway_switch->seat_device->sway_seat;
bool locked = server.session_lock.locked;
list_t *bindings = config->current_mode->switch_bindings;
struct sway_switch_binding *matched_binding = NULL;
for (int i = 0; i < bindings->length; ++i) {
struct sway_switch_binding *binding = bindings->items[i];
if (binding->type != sway_switch->type) {
continue;
}
if (!sway_switch_trigger_test(binding->trigger, sway_switch->state)) {
continue;
}
if (config->reloading && (binding->trigger == SWAY_SWITCH_TRIGGER_TOGGLE
|| (binding->flags & BINDING_RELOAD) == 0)) {
continue;
}
bool binding_locked = binding->flags & BINDING_LOCKED;
if (!binding_locked && locked) {
continue;
}
matched_binding = binding;
if (binding_locked == locked) {
break;
}
}
if (matched_binding) {
struct sway_binding *dummy_binding =
calloc(1, sizeof(struct sway_binding));
dummy_binding->type = BINDING_SWITCH;
dummy_binding->flags = matched_binding->flags;
dummy_binding->command = matched_binding->command;
seat_execute_command(seat, dummy_binding);
free(dummy_binding);
}
}
static void handle_switch_toggle(struct wl_listener *listener, void *data) {
struct sway_switch *sway_switch =
wl_container_of(listener, sway_switch, switch_toggle);
struct wlr_switch_toggle_event *event = data;
struct sway_seat *seat = sway_switch->seat_device->sway_seat;
seat_idle_notify_activity(seat, IDLE_SOURCE_SWITCH);
struct wlr_input_device *wlr_device =
sway_switch->seat_device->input_device->wlr_device;
char *device_identifier = input_device_get_identifier(wlr_device);
sway_log(SWAY_DEBUG, "%s: type %d state %d", device_identifier,
event->switch_type, event->switch_state);
free(device_identifier);
sway_switch->type = event->switch_type;
sway_switch->state = event->switch_state;
execute_binding(sway_switch);
}
void sway_switch_configure(struct sway_switch *sway_switch) {
wl_list_remove(&sway_switch->switch_toggle.link);
wl_signal_add(&sway_switch->wlr->events.toggle,
&sway_switch->switch_toggle);
sway_switch->switch_toggle.notify = handle_switch_toggle;
sway_log(SWAY_DEBUG, "Configured switch for device");
}
void sway_switch_destroy(struct sway_switch *sway_switch) {
if (!sway_switch) {
return;
}
wl_list_remove(&sway_switch->switch_toggle.link);
free(sway_switch);
}
void sway_switch_retrigger_bindings_for_all(void) {
struct sway_seat *seat;
wl_list_for_each(seat, &server.input->seats, link) {
struct sway_seat_device *seat_device;
wl_list_for_each(seat_device, &seat->devices, link) {
struct sway_input_device *input_device = seat_device->input_device;
if (input_device->wlr_device->type != WLR_INPUT_DEVICE_SWITCH) {
continue;
}
execute_binding(seat_device->switch_device);
};
}
}
|