1#include <assert.h>
2#include <stdlib.h>
3#include <sys/mman.h>
4#include <unistd.h>
5#include <xkbcommon/xkbcommon.h>
6#include "log.h"
7#include "swaylock.h"
8#include "seat.h"
9#include "loop.h"
10
11static void keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard,
12 uint32_t format, int32_t fd, uint32_t size) {
13 struct swaylock_seat *seat = data;
14 struct swaylock_state *state = seat->state;
15 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
16 close(fd);
17 swaylock_log(LOG_ERROR, "Unknown keymap format %d, aborting", format);
18 exit(1);
19 }
20 char *map_shm = mmap(NULL, size - 1, PROT_READ, MAP_PRIVATE, fd, 0);
21 if (map_shm == MAP_FAILED) {
22 close(fd);
23 swaylock_log(LOG_ERROR, "Unable to initialize keymap shm, aborting");
24 exit(1);
25 }
26 struct xkb_keymap *keymap = xkb_keymap_new_from_buffer(
27 state->xkb.context, map_shm, size - 1, XKB_KEYMAP_FORMAT_TEXT_V1,
28 XKB_KEYMAP_COMPILE_NO_FLAGS);
29 munmap(map_shm, size - 1);
30 close(fd);
31 assert(keymap);
32 struct xkb_state *xkb_state = xkb_state_new(keymap);
33 assert(xkb_state);
34 xkb_keymap_unref(state->xkb.keymap);
35 xkb_state_unref(state->xkb.state);
36 state->xkb.keymap = keymap;
37 state->xkb.state = xkb_state;
38}
39
40static void keyboard_enter(void *data, struct wl_keyboard *wl_keyboard,
41 uint32_t serial, struct wl_surface *surface, struct wl_array *keys) {
42 // Who cares
43}
44
45static void keyboard_leave(void *data, struct wl_keyboard *wl_keyboard,
46 uint32_t serial, struct wl_surface *surface) {
47 // Who cares
48}
49
50static void keyboard_repeat(void *data) {
51 struct swaylock_seat *seat = data;
52 struct swaylock_state *state = seat->state;
53 seat->repeat_timer = loop_add_timer(
54 state->eventloop, seat->repeat_period_ms, keyboard_repeat, seat);
55 swaylock_handle_key(state, seat->repeat_sym, seat->repeat_codepoint);
56}
57
58static void keyboard_key(void *data, struct wl_keyboard *wl_keyboard,
59 uint32_t serial, uint32_t time, uint32_t key, uint32_t _key_state) {
60 struct swaylock_seat *seat = data;
61 struct swaylock_state *state = seat->state;
62 enum wl_keyboard_key_state key_state = _key_state;
63 xkb_keysym_t sym = xkb_state_key_get_one_sym(state->xkb.state, key + 8);
64 uint32_t keycode = key_state == WL_KEYBOARD_KEY_STATE_PRESSED ?
65 key + 8 : 0;
66 uint32_t codepoint = xkb_state_key_get_utf32(state->xkb.state, keycode);
67 if (key_state == WL_KEYBOARD_KEY_STATE_PRESSED) {
68 swaylock_handle_key(state, sym, codepoint);
69 }
70
71 if (seat->repeat_timer) {
72 loop_remove_timer(seat->state->eventloop, seat->repeat_timer);
73 seat->repeat_timer = NULL;
74 }
75
76 if (key_state == WL_KEYBOARD_KEY_STATE_PRESSED && seat->repeat_period_ms > 0) {
77 seat->repeat_sym = sym;
78 seat->repeat_codepoint = codepoint;
79 seat->repeat_timer = loop_add_timer(
80 seat->state->eventloop, seat->repeat_delay_ms, keyboard_repeat, seat);
81 }
82}
83
84static void keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard,
85 uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched,
86 uint32_t mods_locked, uint32_t group) {
87 struct swaylock_seat *seat = data;
88 struct swaylock_state *state = seat->state;
89 if (state->xkb.state == NULL) {
90 return;
91 }
92
93 int layout_same = xkb_state_layout_index_is_active(state->xkb.state,
94 group, XKB_STATE_LAYOUT_EFFECTIVE);
95 if (!layout_same) {
96 damage_state(state);
97 }
98 xkb_state_update_mask(state->xkb.state,
99 mods_depressed, mods_latched, mods_locked, 0, 0, group);
100 int caps_lock = xkb_state_mod_name_is_active(state->xkb.state,
101 XKB_MOD_NAME_CAPS, XKB_STATE_MODS_LOCKED);
102 if (caps_lock != state->xkb.caps_lock) {
103 state->xkb.caps_lock = caps_lock;
104 damage_state(state);
105 }
106 state->xkb.control = xkb_state_mod_name_is_active(state->xkb.state,
107 XKB_MOD_NAME_CTRL,
108 XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED);
109}
110
111static void keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
112 int32_t rate, int32_t delay) {
113 struct swaylock_seat *seat = data;
114 if (rate <= 0) {
115 seat->repeat_period_ms = -1;
116 } else {
117 // Keys per second -> milliseconds between keys
118 seat->repeat_period_ms = 1000 / rate;
119 }
120 seat->repeat_delay_ms = delay;
121}
122
123static const struct wl_keyboard_listener keyboard_listener = {
124 .keymap = keyboard_keymap,
125 .enter = keyboard_enter,
126 .leave = keyboard_leave,
127 .key = keyboard_key,
128 .modifiers = keyboard_modifiers,
129 .repeat_info = keyboard_repeat_info,
130};
131
132static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
133 uint32_t serial, struct wl_surface *surface,
134 wl_fixed_t surface_x, wl_fixed_t surface_y) {
135 wl_pointer_set_cursor(wl_pointer, serial, NULL, 0, 0);
136}
137
138static void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer,
139 uint32_t serial, struct wl_surface *surface) {
140 // Who cares
141}
142
143static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
144 uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
145 swaylock_handle_mouse((struct swaylock_state *)data);
146}
147
148static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
149 uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
150 swaylock_handle_mouse((struct swaylock_state *)data);
151}
152
153static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
154 uint32_t time, uint32_t axis, wl_fixed_t value) {
155 swaylock_handle_mouse((struct swaylock_state *)data);
156}
157
158static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) {
159 // Who cares
160}
161
162static void wl_pointer_axis_source(void *data, struct wl_pointer *wl_pointer,
163 uint32_t axis_source) {
164 // Who cares
165}
166
167static void wl_pointer_axis_stop(void *data, struct wl_pointer *wl_pointer,
168 uint32_t time, uint32_t axis) {
169 // Who cares
170}
171
172static void wl_pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer,
173 uint32_t axis, int32_t discrete) {
174 // Who cares
175}
176
177static const struct wl_pointer_listener pointer_listener = {
178 .enter = wl_pointer_enter,
179 .leave = wl_pointer_leave,
180 .motion = wl_pointer_motion,
181 .button = wl_pointer_button,
182 .axis = wl_pointer_axis,
183 .frame = wl_pointer_frame,
184 .axis_source = wl_pointer_axis_source,
185 .axis_stop = wl_pointer_axis_stop,
186 .axis_discrete = wl_pointer_axis_discrete,
187};
188
189static void wl_touch_down(void *data, struct wl_touch *touch, uint32_t serial,
190 uint32_t time, struct wl_surface *surface, int32_t id, wl_fixed_t x, wl_fixed_t y) {
191 swaylock_handle_touch((struct swaylock_state *)data);
192}
193
194static void wl_touch_up(void *data, struct wl_touch *touch, uint32_t serial,
195 uint32_t time, int32_t id) {
196 // Who cares
197}
198
199static void wl_touch_motion(void *data, struct wl_touch *touch, uint32_t time,
200 int32_t id, wl_fixed_t x, wl_fixed_t y) {
201 swaylock_handle_touch((struct swaylock_state *)data);
202}
203
204static void wl_touch_frame(void *data, struct wl_touch *touch) {
205 // Who cares
206}
207
208static void wl_touch_cancel(void *data, struct wl_touch *touch) {
209 // Who cares
210}
211
212static const struct wl_touch_listener touch_listener = {
213 .down = wl_touch_down,
214 .up = wl_touch_up,
215 .motion = wl_touch_motion,
216 .frame = wl_touch_frame,
217 .cancel = wl_touch_cancel,
218};
219
220static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
221 enum wl_seat_capability caps) {
222 struct swaylock_seat *seat = data;
223 if (seat->pointer) {
224 wl_pointer_release(seat->pointer);
225 seat->pointer = NULL;
226 }
227 if (seat->keyboard) {
228 wl_keyboard_release(seat->keyboard);
229 seat->keyboard = NULL;
230 }
231 if ((caps & WL_SEAT_CAPABILITY_POINTER)) {
232 seat->pointer = wl_seat_get_pointer(wl_seat);
233 wl_pointer_add_listener(seat->pointer, &pointer_listener, seat->state);
234 }
235 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
236 seat->keyboard = wl_seat_get_keyboard(wl_seat);
237 wl_keyboard_add_listener(seat->keyboard, &keyboard_listener, seat);
238 }
239 if ((caps & WL_SEAT_CAPABILITY_TOUCH)) {
240 seat->touch = wl_seat_get_touch(wl_seat);
241 wl_touch_add_listener(seat->touch, &touch_listener, seat->state);
242 }
243}
244
245static void seat_handle_name(void *data, struct wl_seat *wl_seat,
246 const char *name) {
247 // Who cares
248}
249
250const struct wl_seat_listener seat_listener = {
251 .capabilities = seat_handle_capabilities,
252 .name = seat_handle_name,
253};