summaryrefslogtreecommitdiffstats
path: root/swaylock/seat.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaylock/seat.c')
-rw-r--r--swaylock/seat.c190
1 files changed, 190 insertions, 0 deletions
diff --git a/swaylock/seat.c b/swaylock/seat.c
new file mode 100644
index 00000000..21db7c4f
--- /dev/null
+++ b/swaylock/seat.c
@@ -0,0 +1,190 @@
1#include <assert.h>
2#include <stdlib.h>
3#include <sys/mman.h>
4#include <unistd.h>
5#include <wlr/util/log.h>
6#include <xkbcommon/xkbcommon.h>
7#include "swaylock/swaylock.h"
8#include "swaylock/seat.h"
9
10const char *XKB_MASK_NAMES[MASK_LAST] = {
11 XKB_MOD_NAME_SHIFT,
12 XKB_MOD_NAME_CAPS,
13 XKB_MOD_NAME_CTRL,
14 XKB_MOD_NAME_ALT,
15 "Mod2",
16 "Mod3",
17 XKB_MOD_NAME_LOGO,
18 "Mod5",
19};
20
21const enum mod_bit XKB_MODS[MASK_LAST] = {
22 MOD_SHIFT,
23 MOD_CAPS,
24 MOD_CTRL,
25 MOD_ALT,
26 MOD_MOD2,
27 MOD_MOD3,
28 MOD_LOGO,
29 MOD_MOD5
30};
31
32static void keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard,
33 uint32_t format, int32_t fd, uint32_t size) {
34 struct swaylock_state *state = data;
35 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
36 close(fd);
37 wlr_log(L_ERROR, "Unknown keymap format %d, aborting", format);
38 exit(1);
39 }
40 char *map_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
41 if (map_shm == MAP_FAILED) {
42 close(fd);
43 wlr_log(L_ERROR, "Unable to initialize keymap shm, aborting");
44 exit(1);
45 }
46 struct xkb_keymap *keymap = xkb_keymap_new_from_string(
47 state->xkb.context, map_shm, XKB_KEYMAP_FORMAT_TEXT_V1, 0);
48 munmap(map_shm, size);
49 close(fd);
50 assert(keymap);
51 struct xkb_state *xkb_state = xkb_state_new(keymap);
52 assert(xkb_state);
53 xkb_keymap_unref(state->xkb.keymap);
54 xkb_state_unref(state->xkb.state);
55 state->xkb.keymap = keymap;
56 state->xkb.state = xkb_state;
57}
58
59static void keyboard_enter(void *data, struct wl_keyboard *wl_keyboard,
60 uint32_t serial, struct wl_surface *surface, struct wl_array *keys) {
61 // Who cares
62}
63
64static void keyboard_leave(void *data, struct wl_keyboard *wl_keyboard,
65 uint32_t serial, struct wl_surface *surface) {
66 // Who cares
67}
68
69static void keyboard_key(void *data, struct wl_keyboard *wl_keyboard,
70 uint32_t serial, uint32_t time, uint32_t key, uint32_t _key_state) {
71 struct swaylock_state *state = data;
72 enum wl_keyboard_key_state key_state = _key_state;
73 xkb_keysym_t sym = xkb_state_key_get_one_sym(state->xkb.state, key + 8);
74 uint32_t keycode = key_state == WL_KEYBOARD_KEY_STATE_PRESSED ?
75 key + 8 : 0;
76 uint32_t codepoint = xkb_state_key_get_utf32(state->xkb.state, keycode);
77 if (key_state == WL_KEYBOARD_KEY_STATE_PRESSED) {
78 swaylock_handle_key(state, sym, codepoint);
79 }
80}
81
82static void keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard,
83 uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched,
84 uint32_t mods_locked, uint32_t group) {
85 struct swaylock_state *state = data;
86 xkb_state_update_mask(state->xkb.state,
87 mods_depressed, mods_latched, mods_locked, 0, 0, group);
88 xkb_mod_mask_t mask = xkb_state_serialize_mods(state->xkb.state,
89 XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED);
90 state->xkb.modifiers = 0;
91 for (uint32_t i = 0; i < MASK_LAST; ++i) {
92 if (mask & state->xkb.masks[i]) {
93 state->xkb.modifiers |= XKB_MODS[i];
94 }
95 }
96}
97
98static void keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
99 int32_t rate, int32_t delay) {
100 // TODO
101}
102
103static const struct wl_keyboard_listener keyboard_listener = {
104 .keymap = keyboard_keymap,
105 .enter = keyboard_enter,
106 .leave = keyboard_leave,
107 .key = keyboard_key,
108 .modifiers = keyboard_modifiers,
109 .repeat_info = keyboard_repeat_info,
110};
111
112static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
113 uint32_t serial, struct wl_surface *surface,
114 wl_fixed_t surface_x, wl_fixed_t surface_y) {
115 wl_pointer_set_cursor(wl_pointer, serial, NULL, 0, 0);
116}
117
118static void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer,
119 uint32_t serial, struct wl_surface *surface) {
120 // Who cares
121}
122
123static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
124 uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
125 // Who cares
126}
127
128static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
129 uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
130 // Who cares
131}
132
133static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
134 uint32_t time, uint32_t axis, wl_fixed_t value) {
135 // Who cares
136}
137
138static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) {
139 // Who cares
140}
141
142static void wl_pointer_axis_source(void *data, struct wl_pointer *wl_pointer,
143 uint32_t axis_source) {
144 // Who cares
145}
146
147static void wl_pointer_axis_stop(void *data, struct wl_pointer *wl_pointer,
148 uint32_t time, uint32_t axis) {
149 // Who cares
150}
151
152static void wl_pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer,
153 uint32_t axis, int32_t discrete) {
154 // Who cares
155}
156
157static const struct wl_pointer_listener pointer_listener = {
158 .enter = wl_pointer_enter,
159 .leave = wl_pointer_leave,
160 .motion = wl_pointer_motion,
161 .button = wl_pointer_button,
162 .axis = wl_pointer_axis,
163 .frame = wl_pointer_frame,
164 .axis_source = wl_pointer_axis_source,
165 .axis_stop = wl_pointer_axis_stop,
166 .axis_discrete = wl_pointer_axis_discrete,
167};
168
169static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
170 enum wl_seat_capability caps) {
171 struct swaylock_state *state = data;
172 if ((caps & WL_SEAT_CAPABILITY_POINTER)) {
173 struct wl_pointer *pointer = wl_seat_get_pointer(wl_seat);
174 wl_pointer_add_listener(pointer, &pointer_listener, NULL);
175 }
176 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
177 struct wl_keyboard *keyboard = wl_seat_get_keyboard(wl_seat);
178 wl_keyboard_add_listener(keyboard, &keyboard_listener, state);
179 }
180}
181
182static void seat_handle_name(void *data, struct wl_seat *wl_seat,
183 const char *name) {
184 // Who cares
185}
186
187const struct wl_seat_listener seat_listener = {
188 .capabilities = seat_handle_capabilities,
189 .name = seat_handle_name,
190};