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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
#include "wayland.h"
#include <unistd.h>
#include <sys/mman.h>
#include <sys/timerfd.h>
const char *BM_XKB_MASK_NAMES[MASK_LAST] = {
XKB_MOD_NAME_SHIFT,
XKB_MOD_NAME_CAPS,
XKB_MOD_NAME_CTRL,
XKB_MOD_NAME_ALT,
"Mod2",
"Mod3",
XKB_MOD_NAME_LOGO,
"Mod5",
};
const enum mod_bit BM_XKB_MODS[MASK_LAST] = {
MOD_SHIFT,
MOD_CAPS,
MOD_CTRL,
MOD_ALT,
MOD_MOD2,
MOD_MOD3,
MOD_LOGO,
MOD_MOD5
};
static void
shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
{
(void)wl_shm;
struct wayland *wayland = data;
wayland->formats |= (1 << format);
}
struct wl_shm_listener shm_listener = {
.format = shm_format
};
static void
keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size)
{
(void)keyboard;
struct input *input = data;
if (!data) {
close(fd);
return;
}
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
close(fd);
return;
}
char *map_str;
if ((map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
close(fd);
return;
}
struct xkb_keymap *keymap = xkb_keymap_new_from_string(input->xkb.context, map_str, XKB_KEYMAP_FORMAT_TEXT_V1, 0);
munmap(map_str, size);
close(fd);
if (!keymap) {
fprintf(stderr, "failed to compile keymap\n");
return;
}
struct xkb_state *state;
if (!(state = xkb_state_new(keymap))) {
fprintf(stderr, "failed to create XKB state\n");
xkb_keymap_unref(keymap);
return;
}
xkb_keymap_unref(input->xkb.keymap);
xkb_state_unref(input->xkb.state);
input->xkb.keymap = keymap;
input->xkb.state = state;
for (uint32_t i = 0; i < MASK_LAST; ++i)
input->xkb.masks[i] = 1 << xkb_keymap_mod_get_index(input->xkb.keymap, BM_XKB_MASK_NAMES[i]);
}
static void
keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys)
{
(void)data, (void)keyboard, (void)serial, (void)surface, (void)keys;
}
static void
keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface)
{
(void)keyboard, (void)serial, (void)surface;
struct input *input = data;
struct itimerspec its;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = 0;
timerfd_settime(*input->repeat_fd, 0, &its, NULL);
}
static void
press(struct input *input, xkb_keysym_t sym, uint32_t key, enum wl_keyboard_key_state state)
{
input->sym = (state == WL_KEYBOARD_KEY_STATE_PRESSED ? sym : XKB_KEY_NoSymbol);
input->code = (state == WL_KEYBOARD_KEY_STATE_PRESSED ? key + 8 : 0);
if (input->notify.key)
input->notify.key(state, sym, key);
}
static void
keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state_w)
{
(void)keyboard, (void)serial, (void)time;
struct input *input = data;
enum wl_keyboard_key_state state = state_w;
if (!input->xkb.state)
return;
xkb_keysym_t sym = xkb_state_key_get_one_sym(input->xkb.state, key + 8);
press(input, sym, key, state);
if (state == WL_KEYBOARD_KEY_STATE_PRESSED && xkb_keymap_key_repeats(input->xkb.keymap, input->code)) {
struct itimerspec its;
input->repeat_sym = sym;
input->repeat_key = key;
its.it_interval.tv_sec = input->repeat_rate_sec;
its.it_interval.tv_nsec = input->repeat_rate_nsec;
its.it_value.tv_sec = input->repeat_delay_sec;
its.it_value.tv_nsec = input->repeat_delay_nsec;
timerfd_settime(*input->repeat_fd, 0, &its, NULL);
} else if (state == WL_KEYBOARD_KEY_STATE_RELEASED && key == input->repeat_key) {
struct itimerspec its;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = 0;
timerfd_settime(*input->repeat_fd, 0, &its, NULL);
}
}
static void
keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group)
{
(void)keyboard, (void)serial;
struct input *input = data;
if (!input->xkb.keymap)
return;
xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched, mods_locked, 0, 0, group);
xkb_mod_mask_t mask = xkb_state_serialize_mods(input->xkb.state, XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED);
input->modifiers = 0;
for (uint32_t i = 0; i < MASK_LAST; ++i) {
if (mask & input->xkb.masks[i])
input->modifiers |= BM_XKB_MODS[i];
}
}
static void
set_repeat_info(struct input *input, int32_t rate, int32_t delay)
{
assert(input);
input->repeat_rate_sec = input->repeat_rate_nsec = 0;
input->repeat_delay_sec = input->repeat_delay_nsec = 0;
/* a rate of zero disables any repeating, regardless of the delay's value */
if (rate == 0)
return;
if (rate == 1)
input->repeat_rate_sec = 1;
else
input->repeat_rate_nsec = 1000000000 / rate;
input->repeat_delay_sec = delay / 1000;
delay -= (input->repeat_delay_sec * 1000);
input->repeat_delay_nsec = delay * 1000 * 1000;
}
static void
keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard, int32_t rate, int32_t delay)
{
(void)keyboard;
set_repeat_info(data, rate, delay);
}
static const struct wl_keyboard_listener keyboard_listener = {
.keymap = keyboard_handle_keymap,
.enter = keyboard_handle_enter,
.leave = keyboard_handle_leave,
.key = keyboard_handle_key,
.modifiers = keyboard_handle_modifiers,
.repeat_info = keyboard_handle_repeat_info
};
static void
seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps)
{
struct input *input = data;
if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
input->keyboard = wl_seat_get_keyboard(seat);
wl_keyboard_add_listener(input->keyboard, &keyboard_listener, data);
} else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
wl_keyboard_destroy(input->keyboard);
input->keyboard = NULL;
}
}
static void
seat_handle_name(void *data, struct wl_seat *seat, const char *name)
{
(void)data, (void)seat, (void)name;
}
static const struct wl_seat_listener seat_listener = {
.capabilities = seat_handle_capabilities,
.name = seat_handle_name
};
static void
display_handle_geometry(void *data, struct wl_output *wl_output, int x, int y, int physical_width, int physical_height, int subpixel, const char *make, const char *model, int transform)
{
(void)data, (void)wl_output, (void)x, (void)y, (void)physical_width, (void)physical_height, (void)subpixel, (void)make, (void)model, (void)transform;
}
static void
display_handle_done(void *data, struct wl_output *wl_output)
{
(void)data, (void)wl_output;
}
static void
display_handle_scale(void *data, struct wl_output *wl_output, int32_t scale)
{
(void)data, (void)wl_output, (void)scale;
}
static void
display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags, int width, int height, int refresh)
{
(void)wl_output, (void)refresh, (void)height, (void)width;
struct output *output = data;
if (flags & WL_OUTPUT_MODE_CURRENT) {
output->height = height;
}
}
static const struct wl_output_listener output_listener = {
.geometry = display_handle_geometry,
.mode = display_handle_mode,
.done = display_handle_done,
.scale = display_handle_scale
};
static void
registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface, uint32_t version)
{
(void)version;
struct wayland *wayland = data;
if (strcmp(interface, "wl_compositor") == 0) {
wayland->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 1);
} else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
wayland->layer_shell = wl_registry_bind(registry, id, &zwlr_layer_shell_v1_interface, 1);
} else if (strcmp(interface, "wl_seat") == 0) {
wayland->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
wl_seat_add_listener(wayland->seat, &seat_listener, &wayland->input);
} else if (strcmp(interface, "wl_shm") == 0) {
wayland->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
wl_shm_add_listener(wayland->shm, &shm_listener, data);
} else if (strcmp(interface, "wl_output") == 0) {
struct wl_output *wl_output = wl_registry_bind(registry, id, &wl_output_interface, 2);
struct output *output = calloc(1, sizeof(struct output));
output->output = wl_output;
wl_list_insert(&wayland->outputs, &output->link);
wl_output_add_listener(wl_output, &output_listener, output);
}
}
static void
registry_handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
{
(void)data, (void)registry, (void)name;
}
static const struct wl_registry_listener registry_listener = {
registry_handle_global,
registry_handle_global_remove
};
void
bm_wl_repeat(struct wayland *wayland)
{
uint64_t exp;
if (read(wayland->fds.repeat, &exp, sizeof(exp)) != sizeof(exp))
return;
if (wayland->input.notify.key)
wayland->input.notify.key(WL_KEYBOARD_KEY_STATE_PRESSED, wayland->input.repeat_sym, wayland->input.repeat_key + 8);
press(&wayland->input, wayland->input.repeat_sym, wayland->input.repeat_key, WL_KEYBOARD_KEY_STATE_PRESSED);
}
void
bm_wl_registry_destroy(struct wayland *wayland)
{
assert(wayland);
if (wayland->shm)
wl_shm_destroy(wayland->shm);
if (wayland->layer_shell)
zwlr_layer_shell_v1_destroy(wayland->layer_shell);
if (wayland->compositor)
wl_compositor_destroy(wayland->compositor);
if (wayland->registry)
wl_registry_destroy(wayland->registry);
xkb_keymap_unref(wayland->input.xkb.keymap);
xkb_state_unref(wayland->input.xkb.state);
}
bool
bm_wl_registry_register(struct wayland *wayland)
{
assert(wayland);
if (!(wayland->registry = wl_display_get_registry(wayland->display)))
return false;
wl_list_init(&wayland->outputs);
wl_list_init(&wayland->windows);
wl_registry_add_listener(wayland->registry, ®istry_listener, wayland);
wl_display_roundtrip(wayland->display); // trip 1, registry globals
if (!wayland->compositor || !wayland->seat || !wayland->shm || !wayland->layer_shell)
return false;
wl_display_roundtrip(wayland->display); // trip 2, global listeners
if (!wayland->input.keyboard || !(wayland->formats & (1 << WL_SHM_FORMAT_ARGB8888)))
return false;
set_repeat_info(&wayland->input, 40, 400);
wayland->input.last_code = 0xDEADBEEF;
return true;
}
/* vim: set ts=8 sw=4 tw=0 :*/
|