aboutsummaryrefslogtreecommitdiffstats
path: root/source/platform/platform_linux_wayland.cpp
blob: bb013a576c838a8582469a776eea2b5eb6b22bc9 (plain) (blame)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//- input helpers

internal void wayland_button_update(PlatformButton *button, b32 down)
{
	if(button->ended_down != down) {
		button->ended_down = down;
		++button->half_transition_count;
	}
}

internal u32 wayland_current_mods(WaylandState *state)
{
	u32 mods = 0;
	if(!state->xkb_state) {
		return 0;
	}
	if(xkb_state_mod_name_is_active(state->xkb_state, XKB_MOD_NAME_SHIFT, XKB_STATE_MODS_EFFECTIVE) > 0) mods |= PLATFORM_MOD_SHIFT;
	if(xkb_state_mod_name_is_active(state->xkb_state, XKB_MOD_NAME_CTRL, XKB_STATE_MODS_EFFECTIVE) > 0)  mods |= PLATFORM_MOD_CTRL;
	if(xkb_state_mod_name_is_active(state->xkb_state, XKB_MOD_NAME_ALT, XKB_STATE_MODS_EFFECTIVE) > 0)   mods |= PLATFORM_MOD_ALT;
	return mods;
}

// NOTE: a key press (or synthesized repeat): one key event plus whatever text it types
internal void wayland_emit_key(WaylandState *state, u32 evdev_key, b32 repeat)
{
	if(!state->xkb_state) {
		return;
	}
	PlatformInput *input   = &state->input;
	u32            keycode = evdev_key + 8; // xkb keycodes are evdev + 8, historical x11 offset
	u32            mods    = wayland_current_mods(state);

	if(input->key_count < PLATFORM_MAX_KEY_EVENTS) {
		PlatformKeyEvent *event = &input->keys[input->key_count++];
		event->keysym           = xkb_state_key_get_one_sym(state->xkb_state, keycode);
		event->mods             = mods;
		event->repeat           = repeat;
	}

	if(!(mods & (PLATFORM_MOD_CTRL | PLATFORM_MOD_ALT))) {
		char text[16];
		int  length = xkb_state_key_get_utf8(state->xkb_state, keycode, text, sizeof(text));
		if(length > 0 && (u8)text[0] >= 0x20 && text[0] != 0x7f && input->text_size + (u32)length <= sizeof(input->text)) {
			MemCpy(input->text + input->text_size, text, (u64)length);
			input->text_size += (u32)length;
		}
	}
}

//- wl_keyboard

internal void wayland_keyboard_keymap(void *data, struct wl_keyboard *keyboard, u32 format, s32 fd, u32 size)
{
	WaylandState *state = (WaylandState *)data;
	state->input.changed = true;
	if(format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
		close(fd);
		return;
	}

	char *text = (char *)mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
	close(fd);
	if(text == MAP_FAILED) {
		return;
	}

	struct xkb_keymap *keymap = xkb_keymap_new_from_string(state->xkb_context, text, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
	munmap(text, size);
	if(!keymap) {
		Log("failed to compile the xkb keymap");
		return;
	}

	if(state->xkb_state)  xkb_state_unref(state->xkb_state);
	if(state->xkb_keymap) xkb_keymap_unref(state->xkb_keymap);
	state->xkb_keymap = keymap;
	state->xkb_state  = xkb_state_new(keymap);
}

internal void wayland_keyboard_enter(void *data, struct wl_keyboard *keyboard, u32 serial, struct wl_surface *surface, struct wl_array *keys)
{
	WaylandState *state  = (WaylandState *)data;
	state->input.changed = true;
	state->input.focused = true;
}

// NOTE: after a leave we get no releases for keys still held, so stop repeating ourselves
internal void wayland_keyboard_leave(void *data, struct wl_keyboard *keyboard, u32 serial, struct wl_surface *surface)
{
	WaylandState *state  = (WaylandState *)data;
	state->input.changed = true;
	state->input.focused = false;
	state->repeat_key    = 0;
}

internal void wayland_keyboard_key(void *data, struct wl_keyboard *keyboard, u32 serial, u32 time, u32 key, u32 key_state)
{
	WaylandState *state   = (WaylandState *)data;
	state->input.changed = true;
	b32           pressed = (key_state == WL_KEYBOARD_KEY_STATE_PRESSED);

	if(pressed) {
		wayland_emit_key(state, key, false);
		if(state->repeat_rate > 0 && state->xkb_keymap && xkb_keymap_key_repeats(state->xkb_keymap, key + 8)) {
			state->repeat_key     = key;
			state->repeat_next_ns = time_now_ns() + (u64)state->repeat_delay_ms * 1000000ull;
		}
	} else if(state->repeat_key == key) {
		state->repeat_key = 0;
	}
}

internal void wayland_keyboard_modifiers(void *data, struct wl_keyboard *keyboard, u32 serial, u32 depressed, u32 latched, u32 locked, u32 group)
{
	WaylandState *state = (WaylandState *)data;
	state->input.changed = true;
	if(state->xkb_state) {
		xkb_state_update_mask(state->xkb_state, depressed, latched, locked, 0, 0, group);
	}
	state->input.mods = wayland_current_mods(state);
}

internal void wayland_keyboard_repeat_info(void *data, struct wl_keyboard *keyboard, s32 rate, s32 delay)
{
	WaylandState *state    = (WaylandState *)data;
	state->input.changed = true;
	state->repeat_rate     = rate;
	state->repeat_delay_ms = delay;
}

global_variable const struct wl_keyboard_listener wayland_keyboard_listener = {
	wayland_keyboard_keymap,
	wayland_keyboard_enter,
	wayland_keyboard_leave,
	wayland_keyboard_key,
	wayland_keyboard_modifiers,
	wayland_keyboard_repeat_info,
};

//- wl_pointer

internal void wayland_pointer_enter(void *data, struct wl_pointer *pointer, u32 serial, struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
{
	WaylandState *state          = (WaylandState *)data;
	state->input.changed = true;
	state->input.mouse_in_window = true;
	state->input.mouse_x         = (f32)wl_fixed_to_double(x);
	state->input.mouse_y         = (f32)wl_fixed_to_double(y);
	state->pointer_serial = serial;
	state->cursor         = UINT32_MAX; // force the next set_cursor to apply
	if(state->cursor_shape_device) {
		wp_cursor_shape_device_v1_set_shape(state->cursor_shape_device, serial, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT);
		state->cursor = PLATFORM_CURSOR_DEFAULT;
	}
}

internal void wayland_pointer_leave(void *data, struct wl_pointer *pointer, u32 serial, struct wl_surface *surface)
{
	WaylandState *state          = (WaylandState *)data;
	state->input.changed = true;
	state->input.mouse_in_window = false;
	for(u32 index = 0; index < ArrayCount(state->input.mouse); ++index) {
		wayland_button_update(&state->input.mouse[index], false);
	}
}

internal void wayland_pointer_motion(void *data, struct wl_pointer *pointer, u32 time, wl_fixed_t x, wl_fixed_t y)
{
	WaylandState *state  = (WaylandState *)data;
	state->input.changed = true;
	state->input.mouse_x = (f32)wl_fixed_to_double(x);
	state->input.mouse_y = (f32)wl_fixed_to_double(y);
}

internal void wayland_pointer_button(void *data, struct wl_pointer *pointer, u32 serial, u32 time, u32 button, u32 button_state)
{
	WaylandState *state       = (WaylandState *)data;
	state->input.changed = true;
	state->last_button_serial = serial;
	// BTN_LEFT, BTN_RIGHT, BTN_MIDDLE are consecutive
	if(button >= BTN_LEFT && button <= BTN_MIDDLE) {
		wayland_button_update(&state->input.mouse[button - BTN_LEFT], button_state == WL_POINTER_BUTTON_STATE_PRESSED);
	}
}

internal void wayland_pointer_axis(void *data, struct wl_pointer *pointer, u32 time, u32 axis, wl_fixed_t value)
{
	WaylandState *state = (WaylandState *)data;
	state->input.changed = true;
	if(axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
		// NOTE: a wheel notch is 10 units on wlroots/libinput, touchpads send smaller deltas
		state->input.scroll_y += (f32)wl_fixed_to_double(value) / 10.0f;
	}
}

internal void wayland_pointer_frame(void *data, struct wl_pointer *pointer) {}
internal void wayland_pointer_axis_source(void *data, struct wl_pointer *pointer, u32 source) {}
internal void wayland_pointer_axis_stop(void *data, struct wl_pointer *pointer, u32 time, u32 axis) {}
internal void wayland_pointer_axis_discrete(void *data, struct wl_pointer *pointer, u32 axis, s32 discrete) {}

global_variable const struct wl_pointer_listener wayland_pointer_listener = {
	wayland_pointer_enter,
	wayland_pointer_leave,
	wayland_pointer_motion,
	wayland_pointer_button,
	wayland_pointer_axis,
	wayland_pointer_frame,
	wayland_pointer_axis_source,
	wayland_pointer_axis_stop,
	wayland_pointer_axis_discrete,
};

//- wl_seat

internal void wayland_seat_capabilities(void *data, struct wl_seat *seat, u32 capabilities)
{
	WaylandState *state = (WaylandState *)data;

	if((capabilities & WL_SEAT_CAPABILITY_KEYBOARD) && !state->keyboard) {
		state->keyboard = wl_seat_get_keyboard(seat);
		wl_keyboard_add_listener(state->keyboard, &wayland_keyboard_listener, state);
	}

	if((capabilities & WL_SEAT_CAPABILITY_POINTER) && !state->pointer) {
		state->pointer = wl_seat_get_pointer(seat);
		wl_pointer_add_listener(state->pointer, &wayland_pointer_listener, state);
		if(state->cursor_shape_manager) {
			state->cursor_shape_device = wp_cursor_shape_manager_v1_get_pointer(state->cursor_shape_manager, state->pointer);
		}
	}
}

internal void wayland_seat_name(void *data, struct wl_seat *seat, const char *name) {}

global_variable const struct wl_seat_listener wayland_seat_listener = {
	wayland_seat_capabilities,
	wayland_seat_name,
};

//- wl_surface
// NOTE: compositor v6 tells us the scale it wants directly, no wl_output bookkeeping

internal void wayland_surface_enter(void *data, struct wl_surface *surface, struct wl_output *output) {}
internal void wayland_surface_leave(void *data, struct wl_surface *surface, struct wl_output *output) {}

internal void wayland_surface_preferred_buffer_scale(void *data, struct wl_surface *surface, s32 factor)
{
	WaylandState *state = (WaylandState *)data;
	state->input.changed = true;
	if(factor > 0 && (u32)factor != state->scale) {
		state->scale   = (u32)factor;
		state->resized = true;
		wl_surface_set_buffer_scale(state->surface, factor);
	}
}

internal void wayland_surface_preferred_buffer_transform(void *data, struct wl_surface *surface, u32 transform) {}

global_variable const struct wl_surface_listener wayland_surface_listener = {
	wayland_surface_enter,
	wayland_surface_leave,
	wayland_surface_preferred_buffer_scale,
	wayland_surface_preferred_buffer_transform,
};

//- xdg

internal void wayland_xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg_wm_base, u32 serial)
{
	xdg_wm_base_pong(xdg_wm_base, serial);
}

global_variable const struct xdg_wm_base_listener wayland_xdg_wm_base_listener = {
	wayland_xdg_wm_base_ping,
};

internal void wayland_xdg_surface_configure(void *data, struct xdg_surface *xdg_surface, u32 serial)
{
	WaylandState *state = (WaylandState *)data;
	xdg_surface_ack_configure(xdg_surface, serial);
	state->configured = true;
}

global_variable const struct xdg_surface_listener wayland_xdg_surface_listener = {
	wayland_xdg_surface_configure,
};

internal void wayland_xdg_toplevel_configure(void *data, struct xdg_toplevel *toplevel, s32 width, s32 height, struct wl_array *states)
{
	WaylandState *state = (WaylandState *)data;
	state->input.changed = true;
	state->maximized    = false;
	state->suspended    = false;
	for(u32 *it = (u32 *)states->data; (u8 *)it < (u8 *)states->data + states->size; ++it) {
		if(*it == XDG_TOPLEVEL_STATE_MAXIMIZED) state->maximized = true;
		if(*it == XDG_TOPLEVEL_STATE_SUSPENDED) state->suspended = true;
	}
	if(width > 0 && height > 0 && ((u32)width != state->width || (u32)height != state->height)) {
		state->width   = (u32)width;
		state->height  = (u32)height;
		state->resized = true;
	}
}

internal void wayland_xdg_toplevel_close(void *data, struct xdg_toplevel *toplevel)
{
	WaylandState *state = (WaylandState *)data;
	state->should_close = true;
}

internal void wayland_xdg_toplevel_configure_bounds(void *data, struct xdg_toplevel *toplevel, s32 width, s32 height) {}
internal void wayland_xdg_toplevel_wm_capabilities(void *data, struct xdg_toplevel *toplevel, struct wl_array *capabilities) {}

global_variable const struct xdg_toplevel_listener wayland_xdg_toplevel_listener = {
	wayland_xdg_toplevel_configure,
	wayland_xdg_toplevel_close,
	wayland_xdg_toplevel_configure_bounds,
	wayland_xdg_toplevel_wm_capabilities,
};

//- layer shell

internal void wayland_layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *layer_surface, u32 serial, u32 width, u32 height)
{
	WaylandState *state = (WaylandState *)data;
	state->input.changed = true;
	zwlr_layer_surface_v1_ack_configure(layer_surface, serial);
	if(width > 0 && height > 0 && (width != state->width || height != state->height)) {
		state->width   = width;
		state->height  = height;
		state->resized = true;
	}
	state->configured = true;
}

internal void wayland_layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *layer_surface)
{
	WaylandState *state = (WaylandState *)data;
	state->should_close = true;
}

global_variable const struct zwlr_layer_surface_v1_listener wayland_layer_surface_listener = {
	wayland_layer_surface_configure,
	wayland_layer_surface_closed,
};

//- registry

internal void wayland_registry_global(void *data, struct wl_registry *registry, u32 name, const char *interface, u32 version)
{
	WaylandState *state = (WaylandState *)data;

	if(CstringIsMatch(interface, wl_compositor_interface.name)) {
		state->compositor_version = Min(version, 6u);
		state->compositor         = (struct wl_compositor *)wl_registry_bind(registry, name, &wl_compositor_interface, state->compositor_version);
	} else if(CstringIsMatch(interface, xdg_wm_base_interface.name)) {
		// NOTE: v6 for the `suspended` toplevel state, our toplevel listener covers every event up to v6
		state->xdg_wm_base = (struct xdg_wm_base *)wl_registry_bind(registry, name, &xdg_wm_base_interface, Min(version, 6u));
		xdg_wm_base_add_listener(state->xdg_wm_base, &wayland_xdg_wm_base_listener, state);
	} else if(CstringIsMatch(interface, zwlr_layer_shell_v1_interface.name)) {
		state->layer_shell_version = Min(version, 4u);
		state->layer_shell         = (struct zwlr_layer_shell_v1 *)wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, state->layer_shell_version);
	} else if(CstringIsMatch(interface, wl_seat_interface.name) && !state->seat) {
		state->seat = (struct wl_seat *)wl_registry_bind(registry, name, &wl_seat_interface, Min(version, 5u));
		wl_seat_add_listener(state->seat, &wayland_seat_listener, state);
	} else if(CstringIsMatch(interface, zxdg_decoration_manager_v1_interface.name)) {
		state->decoration_manager = (struct zxdg_decoration_manager_v1 *)wl_registry_bind(registry, name, &zxdg_decoration_manager_v1_interface, 1);
	} else if(CstringIsMatch(interface, wp_cursor_shape_manager_v1_interface.name)) {
		state->cursor_shape_manager = (struct wp_cursor_shape_manager_v1 *)wl_registry_bind(registry, name, &wp_cursor_shape_manager_v1_interface, 1);
	}
}

internal void wayland_registry_global_remove(void *data, struct wl_registry *registry, u32 name) {}

global_variable const struct wl_registry_listener wayland_registry_listener = {
	wayland_registry_global,
	wayland_registry_global_remove,
};

//- public api

internal void wayland_update_opaque_region(WaylandState *state);

internal PlatformWindow *wayland_window_open(Arena *arena, WindowParams params)
{
	PlatformWindow *window = PushStructZero(arena, PlatformWindow);
	WaylandState   *state  = &window->wayland;

	state->width       = params.width ? params.width : 960;
	state->height      = params.height ? params.height : 600;
	state->scale       = 1;
	state->repeat_rate = 25;
	state->repeat_delay_ms = 600;
	state->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);

	state->display = wl_display_connect(NULL);
	if(!state->display) {
		Log("failed to connect to the wayland display");
		return NULL;
	}

	// NOTE: first roundtrip binds the globals, the second delivers the seat capabilities
	state->registry = wl_display_get_registry(state->display);
	wl_registry_add_listener(state->registry, &wayland_registry_listener, state);
	wl_display_roundtrip(state->display);
	wl_display_roundtrip(state->display);

	if(!state->compositor) {
		Log("compositor has no wl_compositor");
		return NULL;
	}

	state->surface = wl_compositor_create_surface(state->compositor);
	if(state->compositor_version >= 6) {
		wl_surface_add_listener(state->surface, &wayland_surface_listener, state);
	}

	state->is_layer = params.layer && state->layer_shell;
	if(params.layer && !state->layer_shell) {
		Log("compositor has no wlr layer shell, falling back to a normal window");
	}

	if(state->is_layer) {
		state->layer_surface = zwlr_layer_shell_v1_get_layer_surface(state->layer_shell, state->surface, NULL,
		                                                             ZWLR_LAYER_SHELL_V1_LAYER_TOP, params.name);
		zwlr_layer_surface_v1_add_listener(state->layer_surface, &wayland_layer_surface_listener, state);
		zwlr_layer_surface_v1_set_size(state->layer_surface, state->width, state->height);
		zwlr_layer_surface_v1_set_anchor(state->layer_surface, params.anchor); // PlatformAnchor matches the protocol bits
		zwlr_layer_surface_v1_set_margin(state->layer_surface, params.margin, params.margin, params.margin, params.margin);
		zwlr_layer_surface_v1_set_exclusive_zone(state->layer_surface, 0);
		// NOTE: on_demand (v4) = focus when clicked, like a normal window. v3 only has exclusive.
		zwlr_layer_surface_v1_set_keyboard_interactivity(state->layer_surface, state->layer_shell_version >= 4 ? 2u : 1u);
	} else {
		if(!state->xdg_wm_base) {
			Log("compositor has no xdg_wm_base");
			return NULL;
		}
		state->xdg_surface = xdg_wm_base_get_xdg_surface(state->xdg_wm_base, state->surface);
		xdg_surface_add_listener(state->xdg_surface, &wayland_xdg_surface_listener, state);
		state->xdg_toplevel = xdg_surface_get_toplevel(state->xdg_surface);
		xdg_toplevel_add_listener(state->xdg_toplevel, &wayland_xdg_toplevel_listener, state);
		xdg_toplevel_set_title(state->xdg_toplevel, params.title ? params.title : params.name);
		xdg_toplevel_set_app_id(state->xdg_toplevel, params.name);
		if(params.min_width && params.min_height) {
			xdg_toplevel_set_min_size(state->xdg_toplevel, (s32)params.min_width, (s32)params.min_height);
		}
		// NOTE: we draw no title bar. asking for server side decorations tells tiling compositors
		// (niri, sway) this is a regular window they should frame, focus-ring and round themselves.
		if(state->decoration_manager) {
			state->decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(state->decoration_manager, state->xdg_toplevel);
			zxdg_toplevel_decoration_v1_set_mode(state->decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
		}
	}

	// NOTE: commit without a buffer and wait for the first configure, attaching before is a protocol error
	wl_surface_commit(state->surface);
	while(!state->configured && !state->should_close) {
		if(wl_display_dispatch(state->display) == -1) {
			Log("wayland connection lost before the first configure");
			return NULL;
		}
	}
	state->resized = false;
	wayland_update_opaque_region(state);
	return window;
}

internal b32 wayland_window_pump(PlatformWindow *window, PlatformInput *input, s64 timeout_ns, int *extra_fds, u32 extra_count, u32 *ready_mask)
{
	if(ready_mask) *ready_mask = 0;
	WaylandState *state = &window->wayland;

	// NOTE: a held key needs us awake for its next repeat
	if(state->repeat_key && state->repeat_rate > 0) {
		u64 now       = time_now_ns();
		s64 repeat_ns = (state->repeat_next_ns > now) ? (s64)(state->repeat_next_ns - now) : 0;
		timeout_ns    = (timeout_ns < 0) ? repeat_ns : Min(timeout_ns, repeat_ns);
	}

	while(wl_display_prepare_read(state->display) != 0) {
		if(wl_display_dispatch_pending(state->display) == -1) {
			return false;
		}
	}
	wl_display_flush(state->display);

	struct pollfd fds[8] = {{wl_display_get_fd(state->display), POLLIN, 0}};
	u32           which[8] = {};  // fds slot -> extra_fds index, for ready_mask
	u32           count  = 1;
	for(u32 index = 0; index < extra_count && count < ArrayCount(fds); ++index) {
		if(extra_fds[index] >= 0) {
			which[count] = index;
			fds[count++] = {extra_fds[index], POLLIN, 0};
		}
	}
	// NOTE: ppoll for nanosecond timeouts, a 240hz frame is 4.17ms and poll() only does whole ms
	struct timespec  timeout     = {};
	struct timespec *timeout_ptr = NULL;
	if(timeout_ns >= 0) {
		timeout.tv_sec  = timeout_ns / 1000000000ll;
		timeout.tv_nsec = timeout_ns % 1000000000ll;
		timeout_ptr     = &timeout;
	}
	int ready = ppoll(fds, count, timeout_ptr, NULL);
	if(ready > 0 && ready_mask) {
		for(u32 slot = 1; slot < count; ++slot) {
			if(fds[slot].revents) *ready_mask |= 1u << which[slot];
		}
	}

	if(ready > 0 && (fds[0].revents & POLLIN)) {
		if(wl_display_read_events(state->display) == -1) {
			return false;
		}
	} else {
		wl_display_cancel_read(state->display);
	}

	if(wl_display_dispatch_pending(state->display) == -1) {
		Log("wayland connection lost");
		return false;
	}

	//- synthesize key repeats
	if(state->repeat_key && state->repeat_rate > 0) {
		u64 now      = time_now_ns();
		u64 interval = 1000000000ull / (u64)state->repeat_rate;
		while(now >= state->repeat_next_ns) {
			wayland_emit_key(state, state->repeat_key, true);
			state->input.changed = true;
			state->repeat_next_ns += interval;
		}
	}

	*input = state->input;

	// NOTE: transitions, key events, text and scroll are per frame, held state carries over
	for(u32 index = 0; index < ArrayCount(state->input.mouse); ++index) {
		state->input.mouse[index].half_transition_count = 0;
	}
	state->input.key_count = 0;
	state->input.text_size = 0;
	state->input.scroll_y  = 0;
	state->input.changed   = false;

	return !state->should_close;
}

internal void wayland_window_size(PlatformWindow *window, u32 *width, u32 *height, u32 *scale)
{
	*width  = window->wayland.width;
	*height = window->wayland.height;
	*scale  = window->wayland.scale;
}

// NOTE: a normal window is fully opaque, telling the compositor lets it skip blending whatever
// is behind us. layer surfaces keep their rounded, transparent corners.
internal void wayland_update_opaque_region(WaylandState *state)
{
	if(state->is_layer) {
		return;
	}
	struct wl_region *region = wl_compositor_create_region(state->compositor);
	wl_region_add(region, 0, 0, (s32)state->width, (s32)state->height);
	wl_surface_set_opaque_region(state->surface, region);
	wl_region_destroy(region);
}

internal b32 wayland_window_take_resized(PlatformWindow *window)
{
	b32 resized             = window->wayland.resized;
	window->wayland.resized = false;
	if(resized) {
		wayland_update_opaque_region(&window->wayland);
	}
	return resized;
}

//- window management

internal void wayland_window_begin_move(PlatformWindow *window)
{
	// NOTE: hands the drag to the compositor, it moves the window (or in niri, the tile) until release
	WaylandState *state = &window->wayland;
	if(state->xdg_toplevel && state->seat) {
		xdg_toplevel_move(state->xdg_toplevel, state->seat, state->last_button_serial);
	}
}

internal void wayland_window_toggle_maximized(PlatformWindow *window)
{
	WaylandState *state = &window->wayland;
	if(!state->xdg_toplevel) return;
	if(state->maximized) xdg_toplevel_unset_maximized(state->xdg_toplevel);
	else xdg_toplevel_set_maximized(state->xdg_toplevel);
}

//- cursor

internal void wayland_window_set_cursor(PlatformWindow *window, PlatformCursor cursor)
{
	WaylandState *state = &window->wayland;
	if(!state->cursor_shape_device || !state->input.mouse_in_window || state->cursor == (u32)cursor) {
		return;
	}
	u32 shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT;
	switch(cursor) {
		case PLATFORM_CURSOR_DEFAULT:  shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT; break;
		case PLATFORM_CURSOR_POINTER:  shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER; break;
		case PLATFORM_CURSOR_GRABBING: shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRABBING; break;
		case PLATFORM_CURSOR_RESIZE_X: shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COL_RESIZE; break;
		case PLATFORM_CURSOR_RESIZE_Y: shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ROW_RESIZE; break;
	}
	wp_cursor_shape_device_v1_set_shape(state->cursor_shape_device, state->pointer_serial, shape);
	state->cursor = (u32)cursor;
}

//- frame pacing

internal void wayland_frame_done(void *data, struct wl_callback *callback, u32 time)
{
	WaylandState *state = (WaylandState *)data;
	wl_callback_destroy(callback);
	if(state->frame_callback == callback) state->frame_callback = NULL;
	state->last_frame_done_ns = time_now_ns();
}

global_variable const struct wl_callback_listener wayland_frame_listener = {
	wayland_frame_done,
};

internal void wayland_window_request_frame(PlatformWindow *window)
{
	// NOTE: frame requests are double buffered surface state. the vulkan driver's commit inside
	// vkQueuePresentKHR picks it up, so this has to be called right before presenting.
	WaylandState *state = &window->wayland;
	if(state->frame_callback) {
		wl_callback_destroy(state->frame_callback);
	}
	state->frame_callback     = wl_surface_frame(state->surface);
	state->frame_requested_ns = time_now_ns();
	wl_callback_add_listener(state->frame_callback, &wayland_frame_listener, state);
}

internal void wayland_window_cancel_frame(PlatformWindow *window)
{
	WaylandState *state = &window->wayland;
	if(state->frame_callback) {
		wl_callback_destroy(state->frame_callback);
		state->frame_callback = NULL;
	}
}

internal b32 wayland_window_frame_ready(PlatformWindow *window)
{
	return window->wayland.frame_callback == NULL;
}

internal void wayland_window_close(PlatformWindow *window)
{
	WaylandState *state = &window->wayland;
	wayland_window_cancel_frame(window);
	if(state->decoration)    zxdg_toplevel_decoration_v1_destroy(state->decoration);
	if(state->layer_surface) zwlr_layer_surface_v1_destroy(state->layer_surface);
	if(state->xdg_toplevel)  xdg_toplevel_destroy(state->xdg_toplevel);
	if(state->xdg_surface)   xdg_surface_destroy(state->xdg_surface);
	if(state->surface)       wl_surface_destroy(state->surface);
	if(state->xkb_state)     xkb_state_unref(state->xkb_state);
	if(state->xkb_keymap)    xkb_keymap_unref(state->xkb_keymap);
	if(state->xkb_context)   xkb_context_unref(state->xkb_context);
	wl_display_disconnect(state->display);
}