Debian Codegraph @

platform/linuxbsd/wayland/wayland_thread.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* wayland_thread.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "wayland_thread.h"
32
33 #include "core/config/engine.h"
34
35 #ifdef WAYLAND_ENABLED
36
37 #ifdef __FreeBSD__
38 #include <dev/evdev/input-event-codes.h>
39 #else
40 // Assume Linux.
41 #include <linux/input-event-codes.h>
42 #endif
43
44 // For the actual polling thread.
45 #include <poll.h>
46
47 // For shared memory buffer creation.
48 #include <fcntl.h>
49 #include <sys/mman.h>
50 #include <unistd.h>
51
52 // Fix the wl_array_for_each macro to work with C++. This is based on the
53 // original from `wayland-util.h` in the Wayland client library.
54 #undef wl_array_for_each
55 #define wl_array_for_each(pos, array) \
56 for (pos = (decltype(pos))(array)->data; (const char *)pos < ((const char *)(array)->data + (array)->size); (pos)++)
57
58 #define WAYLAND_THREAD_DEBUG_LOGS_ENABLED
59 #ifdef WAYLAND_THREAD_DEBUG_LOGS_ENABLED
60 #define DEBUG_LOG_WAYLAND_THREAD(...) print_verbose(__VA_ARGS__)
61 #else
62 #define DEBUG_LOG_WAYLAND_THREAD(...)
63 #endif
64
65 // Since we're never going to use this interface directly, it's not worth
66 // generating the whole deal.
67 #define FIFO_INTERFACE_NAME "wp_fifo_manager_v1"
68
69 // Read the content pointed by fd into a Vector<uint8_t>.
70 Vector<uint8_t> WaylandThread::_read_fd(int fd) {
71 // This is pretty much an arbitrary size.
72 uint32_t chunk_size = 2048;
73
74 LocalVector<uint8_t> data;
75 data.resize(chunk_size);
76
77 uint32_t bytes_read = 0;
78
79 while (true) {
80 ssize_t last_bytes_read = read(fd, data.ptr() + bytes_read, chunk_size);
81 if (last_bytes_read < 0) {
82 ERR_PRINT(vformat("Read error %d.", errno));
83
84 data.clear();
85 break;
86 }
87
88 if (last_bytes_read == 0) {
89 // We're done, we've reached the EOF.
90 DEBUG_LOG_WAYLAND_THREAD(vformat("Done reading %d bytes.", bytes_read));
91
92 close(fd);
93
94 data.resize(bytes_read);
95 break;
96 }
97
98 DEBUG_LOG_WAYLAND_THREAD(vformat("Read chunk of %d bytes.", last_bytes_read));
99
100 bytes_read += last_bytes_read;
101
102 // Increase the buffer size by one chunk in preparation of the next read.
103 data.resize(bytes_read + chunk_size);
104 }
105
106 return Vector<uint8_t>(data);
107 }
108
109 // Based on the wayland book's shared memory boilerplate (PD/CC0).
110 // See: https://wayland-book.com/surfaces/shared-memory.html
111 int WaylandThread::_allocate_shm_file(size_t size) {
112 int retries = 100;
113
114 do {
115 // Generate a random name.
116 char name[] = "/wl_shm-godot-XXXXXX";
117 for (long unsigned int i = sizeof(name) - 7; i < sizeof(name) - 1; i++) {
118 name[i] = Math::random('A', 'Z');
119 }
120
121 // Try to open a shared memory object with that name.
122 int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
123 if (fd >= 0) {
124 // Success, unlink its name as we just need the file descriptor.
125 shm_unlink(name);
126
127 // Resize the file to the requested length.
128 int ret;
129 do {
130 ret = ftruncate(fd, size);
131 } while (ret < 0 && errno == EINTR);
132
133 if (ret < 0) {
134 close(fd);
135 return -1;
136 }
137
138 return fd;
139 }
140
141 retries--;
142 } while (retries > 0 && errno == EEXIST);
143
144 return -1;
145 }
146
147 // Return the content of a wl_data_offer.
148 Vector<uint8_t> WaylandThread::_wl_data_offer_read(struct wl_display *p_display, const char *p_mime, struct wl_data_offer *p_offer) {
149 if (!p_offer) {
150 return Vector<uint8_t>();
151 }
152
153 int fds[2];
154 if (pipe(fds) == 0) {
155 wl_data_offer_receive(p_offer, p_mime, fds[1]);
156
157 // Let the compositor know about the pipe.
158 // NOTE: It's important to just flush and not roundtrip here as we would risk
159 // running some cleanup event, like for example `wl_data_device::leave`. We're
160 // going to wait for the message anyways as the read will probably block if
161 // the compositor doesn't read from the other end of the pipe.
162 wl_display_flush(p_display);
163
164 // Close the write end of the pipe, which we don't need and would otherwise
165 // just stall our next `read`s.
166 close(fds[1]);
167
168 return _read_fd(fds[0]);
169 }
170
171 return Vector<uint8_t>();
172 }
173
174 // Read the content of a wp_primary_selection_offer.
175 Vector<uint8_t> WaylandThread::_wp_primary_selection_offer_read(struct wl_display *p_display, const char *p_mime, struct zwp_primary_selection_offer_v1 *p_offer) {
176 if (!p_offer) {
177 return Vector<uint8_t>();
178 }
179
180 int fds[2];
181 if (pipe(fds) == 0) {
182 zwp_primary_selection_offer_v1_receive(p_offer, p_mime, fds[1]);
183
184 // NOTE: It's important to just flush and not roundtrip here as we would risk
185 // running some cleanup event, like for example `wl_data_device::leave`. We're
186 // going to wait for the message anyways as the read will probably block if
187 // the compositor doesn't read from the other end of the pipe.
188 wl_display_flush(p_display);
189
190 // Close the write end of the pipe, which we don't need and would otherwise
191 // just stall our next `read`s.
192 close(fds[1]);
193
194 return _read_fd(fds[0]);
195 }
196
197 return Vector<uint8_t>();
198 }
199
200 void WaylandThread::_wl_display_check_error(struct wl_display *wl_display) {
201 int werror = wl_display_get_error(wl_display);
202 if (werror) {
203 if (werror == EPROTO) {
204 struct wl_interface *wl_interface = nullptr;
205 uint32_t id = 0;
206
207 int error_code = wl_display_get_protocol_error(wl_display, (const struct wl_interface **)&wl_interface, &id);
208 CRASH_NOW_MSG(vformat("Wayland protocol error %d on interface %s@%d.", error_code, wl_interface ? wl_interface->name : "unknown", id));
209 } else {
210 CRASH_NOW_MSG(vformat("Wayland client error code %d.", werror));
211 }
212 }
213 }
214
215 Ref<InputEventKey> WaylandThread::_seat_state_get_key_event(SeatState *p_ss, xkb_keycode_t p_keycode, bool p_pressed) {
216 Ref<InputEventKey> event;
217
218 ERR_FAIL_NULL_V(p_ss, event);
219
220 Key shifted_key = KeyMappingXKB::get_keycode(xkb_state_key_get_one_sym(p_ss->xkb_state, p_keycode));
221
222 Key plain_key = Key::NONE;
223 // NOTE: xkbcommon's API really encourages to apply the modifier state but we
224 // only want a "plain" symbol so that we can convert it into a godot keycode.
225 const xkb_keysym_t *syms = nullptr;
226 int num_sys = xkb_keymap_key_get_syms_by_level(p_ss->xkb_keymap, p_keycode, p_ss->current_layout_index, 0, &syms);
227 if (num_sys > 0 && syms) {
228 plain_key = KeyMappingXKB::get_keycode(syms[0]);
229 }
230
231 Key physical_keycode = KeyMappingXKB::get_scancode(p_keycode);
232 KeyLocation key_location = KeyMappingXKB::get_location(p_keycode);
233 uint32_t unicode = xkb_state_key_get_utf32(p_ss->xkb_state, p_keycode);
234
235 Key keycode = Key::NONE;
236
237 if ((shifted_key & Key::SPECIAL) != Key::NONE || (plain_key & Key::SPECIAL) != Key::NONE) {
238 keycode = shifted_key;
239 }
240
241 if (keycode == Key::NONE) {
242 keycode = plain_key;
243 }
244
245 if (keycode == Key::NONE) {
246 keycode = physical_keycode;
247 }
248
249 if (keycode >= Key::A + 32 && keycode <= Key::Z + 32) {
250 keycode -= 'a' - 'A';
251 }
252
253 if (physical_keycode == Key::NONE && keycode == Key::NONE && unicode == 0) {
254 return event;
255 }
256
257 event.instantiate();
258
259 event->set_window_id(p_ss->focused_id);
260
261 // Set all pressed modifiers.
262 event->set_shift_pressed(p_ss->shift_pressed);
263 event->set_ctrl_pressed(p_ss->ctrl_pressed);
264 event->set_alt_pressed(p_ss->alt_pressed);
265 event->set_meta_pressed(p_ss->meta_pressed);
266
267 event->set_pressed(p_pressed);
268 event->set_keycode(keycode);
269 event->set_physical_keycode(physical_keycode);
270 event->set_location(key_location);
271
272 if (unicode != 0) {
273 event->set_key_label(fix_key_label(unicode, keycode));
274 } else {
275 event->set_key_label(keycode);
276 }
277
278 if (p_pressed) {
279 event->set_unicode(fix_unicode(unicode));
280 }
281
282 // Taken from DisplayServerX11.
283 if (event->get_keycode() == Key::BACKTAB) {
284 // Make it consistent across platforms.
285 event->set_keycode(Key::TAB);
286 event->set_physical_keycode(Key::TAB);
287 event->set_shift_pressed(true);
288 }
289
290 return event;
291 }
292
293 // NOTE: Due to the nature of the way keys are encoded, there's an ambiguity
294 // regarding "special" keys. In other words: there's no reliable way of
295 // switching between a special key and a character key if not marking a
296 // different Godot keycode, even if we're actually using the same XKB raw
297 // keycode. This means that, during this switch, the old key will get "stuck",
298 // as it will never receive a release event. This method returns the necessary
299 // event to fix this if needed.
300 Ref<InputEventKey> WaylandThread::_seat_state_get_unstuck_key_event(SeatState *p_ss, xkb_keycode_t p_keycode, bool p_pressed, Key p_key) {
301 Ref<InputEventKey> event;
302
303 if (p_pressed) {
304 Key *old_key = p_ss->pressed_keycodes.getptr(p_keycode);
305 if (old_key != nullptr && *old_key != p_key) {
306 print_verbose(vformat("%s and %s have same keycode. Generating release event for %s", keycode_get_string(*old_key), keycode_get_string(p_key), keycode_get_string(*old_key)));
307 event = _seat_state_get_key_event(p_ss, p_keycode, false);
308 if (event.is_valid()) {
309 event->set_keycode(*old_key);
310 }
311 }
312 p_ss->pressed_keycodes[p_keycode] = p_key;
313 } else {
314 p_ss->pressed_keycodes.erase(p_keycode);
315 }
316
317 return event;
318 }
319
320 void WaylandThread::_seat_state_handle_xkb_keycode(SeatState *p_ss, xkb_keycode_t p_xkb_keycode, bool p_pressed, bool p_echo) {
321 ERR_FAIL_NULL(p_ss);
322
323 WaylandThread *wayland_thread = p_ss->wayland_thread;
324 ERR_FAIL_NULL(wayland_thread);
325
326 Key last_key = Key::NONE;
327 xkb_compose_status compose_status = xkb_compose_state_get_status(p_ss->xkb_compose_state);
328
329 if (p_pressed) {
330 xkb_keysym_t keysym = xkb_state_key_get_one_sym(p_ss->xkb_state, p_xkb_keycode);
331 xkb_compose_feed_result compose_result = xkb_compose_state_feed(p_ss->xkb_compose_state, keysym);
332 compose_status = xkb_compose_state_get_status(p_ss->xkb_compose_state);
333
334 if (compose_result == XKB_COMPOSE_FEED_ACCEPTED && compose_status == XKB_COMPOSE_COMPOSED) {
335 // We need to generate multiple key events to report the composed result, One
336 // per character.
337 char str_xkb[256] = {};
338 int str_xkb_size = xkb_compose_state_get_utf8(p_ss->xkb_compose_state, str_xkb, 255);
339
340 String decoded_str = String::utf8(str_xkb, str_xkb_size);
341 for (int i = 0; i < decoded_str.length(); ++i) {
342 Ref<InputEventKey> k = _seat_state_get_key_event(p_ss, p_xkb_keycode, p_pressed);
343 if (k.is_null()) {
344 continue;
345 }
346
347 k->set_unicode(decoded_str[i]);
348 k->set_echo(p_echo);
349
350 Ref<InputEventMessage> msg;
351 msg.instantiate();
352 msg->event = k;
353 wayland_thread->push_message(msg);
354
355 last_key = k->get_keycode();
356 }
357 }
358 }
359
360 if (last_key == Key::NONE && compose_status == XKB_COMPOSE_NOTHING) {
361 // If we continued with other compose status (e.g. XKB_COMPOSE_COMPOSING) we
362 // would get the composing keys _and_ the result.
363 Ref<InputEventKey> k = _seat_state_get_key_event(p_ss, p_xkb_keycode, p_pressed);
364 if (k.is_valid()) {
365 k->set_echo(p_echo);
366
367 Ref<InputEventMessage> msg;
368 msg.instantiate();
369 msg->event = k;
370 wayland_thread->push_message(msg);
371
372 last_key = k->get_keycode();
373 }
374 }
375
376 if (last_key != Key::NONE) {
377 Ref<InputEventKey> uk = _seat_state_get_unstuck_key_event(p_ss, p_xkb_keycode, p_pressed, last_key);
378 if (uk.is_valid()) {
379 Ref<InputEventMessage> u_msg;
380 u_msg.instantiate();
381 u_msg->event = uk;
382 wayland_thread->push_message(u_msg);
383 }
384 }
385 }
386
387 void WaylandThread::_set_current_seat(struct wl_seat *p_seat) {
388 if (p_seat == wl_seat_current) {
389 return;
390 }
391
392 SeatState *old_state = wl_seat_get_seat_state(wl_seat_current);
393
394 if (old_state) {
395 seat_state_unlock_pointer(old_state);
396 }
397
398 SeatState *new_state = wl_seat_get_seat_state(p_seat);
399 seat_state_unlock_pointer(new_state);
400
401 wl_seat_current = p_seat;
402 pointer_set_constraint(pointer_constraint);
403 }
404
405 // Returns whether it loaded the theme or not.
406 bool WaylandThread::_load_cursor_theme(int p_cursor_size) {
407 if (wl_cursor_theme) {
408 wl_cursor_theme_destroy(wl_cursor_theme);
409 wl_cursor_theme = nullptr;
410 }
411
412 if (cursor_theme_name.is_empty()) {
413 cursor_theme_name = "default";
414 }
415
416 print_verbose(vformat("Loading cursor theme \"%s\" size %d.", cursor_theme_name, p_cursor_size));
417
418 wl_cursor_theme = wl_cursor_theme_load(cursor_theme_name.utf8().get_data(), p_cursor_size, registry.wl_shm);
419
420 ERR_FAIL_NULL_V_MSG(wl_cursor_theme, false, "Can't load any cursor theme.");
421
422 static const char *cursor_names[] = {
423 "left_ptr",
424 "xterm",
425 "hand2",
426 "cross",
427 "watch",
428 "left_ptr_watch",
429 "fleur",
430 "dnd-move",
431 "crossed_circle",
432 "v_double_arrow",
433 "h_double_arrow",
434 "size_bdiag",
435 "size_fdiag",
436 "move",
437 "row_resize",
438 "col_resize",
439 "question_arrow"
440 };
441
442 static const char *cursor_names_fallback[] = {
443 nullptr,
444 nullptr,
445 "pointer",
446 "cross",
447 "wait",
448 "progress",
449 "grabbing",
450 "hand1",
451 "forbidden",
452 "ns-resize",
453 "ew-resize",
454 "fd_double_arrow",
455 "bd_double_arrow",
456 "fleur",
457 "sb_v_double_arrow",
458 "sb_h_double_arrow",
459 "help"
460 };
461
462 for (int i = 0; i < DisplayServer::CURSOR_MAX; i++) {
463 struct wl_cursor *cursor = wl_cursor_theme_get_cursor(wl_cursor_theme, cursor_names[i]);
464
465 if (!cursor && cursor_names_fallback[i]) {
466 cursor = wl_cursor_theme_get_cursor(wl_cursor_theme, cursor_names_fallback[i]);
467 }
468
469 if (cursor && cursor->image_count > 0) {
470 wl_cursors[i] = cursor;
471 } else {
472 wl_cursors[i] = nullptr;
473 print_verbose("Failed loading cursor: " + String(cursor_names[i]));
474 }
475 }
476
477 return true;
478 }
479
480 void WaylandThread::_update_scale(int p_scale) {
481 if (p_scale <= cursor_scale) {
482 return;
483 }
484
485 print_verbose(vformat("Bumping cursor scale to %d", p_scale));
486
487 // There's some display that's bigger than the cache, let's update it.
488 cursor_scale = p_scale;
489
490 if (wl_cursor_theme == nullptr) {
491 // Ugh. Either we're still initializing (this must've been called from the
492 // first roundtrips) or we had some error while doing so. We'll trust that it
493 // will be updated for us if needed.
494 return;
495 }
496
497 int cursor_size = unscaled_cursor_size * p_scale;
498
499 if (_load_cursor_theme(cursor_size)) {
500 for (struct wl_seat *wl_seat : registry.wl_seats) {
501 SeatState *ss = wl_seat_get_seat_state(wl_seat);
502 ERR_FAIL_NULL(ss);
503
504 seat_state_update_cursor(ss);
505 }
506 }
507 }
508
509 void WaylandThread::_wl_registry_on_global(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface, uint32_t version) {
510 RegistryState *registry = (RegistryState *)data;
511 ERR_FAIL_NULL(registry);
512
513 if (strcmp(interface, wl_shm_interface.name) == 0) {
514 registry->wl_shm = (struct wl_shm *)wl_registry_bind(wl_registry, name, &wl_shm_interface, 1);
515 registry->wl_shm_name = name;
516 return;
517 }
518
519 // NOTE: Deprecated.
520 if (strcmp(interface, zxdg_exporter_v1_interface.name) == 0) {
521 registry->xdg_exporter_v1 = (struct zxdg_exporter_v1 *)wl_registry_bind(wl_registry, name, &zxdg_exporter_v1_interface, 1);
522 registry->xdg_exporter_v1_name = name;
523 return;
524 }
525
526 if (strcmp(interface, zxdg_exporter_v2_interface.name) == 0) {
527 registry->xdg_exporter_v2 = (struct zxdg_exporter_v2 *)wl_registry_bind(wl_registry, name, &zxdg_exporter_v2_interface, 1);
528 registry->xdg_exporter_v2_name = name;
529 return;
530 }
531
532 if (strcmp(interface, wl_compositor_interface.name) == 0) {
533 registry->wl_compositor = (struct wl_compositor *)wl_registry_bind(wl_registry, name, &wl_compositor_interface, CLAMP((int)version, 1, 6));
534 registry->wl_compositor_name = name;
535 return;
536 }
537
538 if (strcmp(interface, wl_data_device_manager_interface.name) == 0) {
539 registry->wl_data_device_manager = (struct wl_data_device_manager *)wl_registry_bind(wl_registry, name, &wl_data_device_manager_interface, CLAMP((int)version, 1, 3));
540 registry->wl_data_device_manager_name = name;
541
542 // This global creates some seat data. Let's do that for the ones already available.
543 for (struct wl_seat *wl_seat : registry->wl_seats) {
544 SeatState *ss = wl_seat_get_seat_state(wl_seat);
545 ERR_FAIL_NULL(ss);
546
547 if (ss->wl_data_device == nullptr) {
548 ss->wl_data_device = wl_data_device_manager_get_data_device(registry->wl_data_device_manager, wl_seat);
549 wl_data_device_add_listener(ss->wl_data_device, &wl_data_device_listener, ss);
550 }
551 }
552 return;
553 }
554
555 if (strcmp(interface, wl_output_interface.name) == 0) {
556 struct wl_output *wl_output = (struct wl_output *)wl_registry_bind(wl_registry, name, &wl_output_interface, CLAMP((int)version, 1, 4));
557 wl_proxy_tag_godot((struct wl_proxy *)wl_output);
558
559 registry->wl_outputs.push_back(wl_output);
560
561 ScreenState *ss = memnew(ScreenState);
562 ss->wl_output_name = name;
563 ss->wayland_thread = registry->wayland_thread;
564
565 wl_proxy_tag_godot((struct wl_proxy *)wl_output);
566 wl_output_add_listener(wl_output, &wl_output_listener, ss);
567 return;
568 }
569
570 if (strcmp(interface, wl_seat_interface.name) == 0) {
571 struct wl_seat *wl_seat = (struct wl_seat *)wl_registry_bind(wl_registry, name, &wl_seat_interface, CLAMP((int)version, 1, 9));
572 wl_proxy_tag_godot((struct wl_proxy *)wl_seat);
573
574 SeatState *ss = memnew(SeatState);
575 ss->wl_seat = wl_seat;
576 ss->wl_seat_name = name;
577
578 ss->registry = registry;
579 ss->wayland_thread = registry->wayland_thread;
580
581 // Some extra stuff depends on other globals. We'll initialize them if the
582 // globals are already there, otherwise we'll have to do that once and if they
583 // get announced.
584 //
585 // NOTE: Don't forget to also bind/destroy with the respective global.
586 if (!ss->wl_data_device && registry->wl_data_device_manager) {
587 // Clipboard & DnD.
588 ss->wl_data_device = wl_data_device_manager_get_data_device(registry->wl_data_device_manager, wl_seat);
589 wl_data_device_add_listener(ss->wl_data_device, &wl_data_device_listener, ss);
590 }
591
592 if (!ss->wp_primary_selection_device && registry->wp_primary_selection_device_manager) {
593 // Primary selection.
594 ss->wp_primary_selection_device = zwp_primary_selection_device_manager_v1_get_device(registry->wp_primary_selection_device_manager, wl_seat);
595 zwp_primary_selection_device_v1_add_listener(ss->wp_primary_selection_device, &wp_primary_selection_device_listener, ss);
596 }
597
598 if (!ss->wp_tablet_seat && registry->wp_tablet_manager) {
599 // Tablet.
600 ss->wp_tablet_seat = zwp_tablet_manager_v2_get_tablet_seat(registry->wp_tablet_manager, wl_seat);
601 zwp_tablet_seat_v2_add_listener(ss->wp_tablet_seat, &wp_tablet_seat_listener, ss);
602 }
603
604 if (!ss->wp_text_input && registry->wp_text_input_manager) {
605 // IME.
606 ss->wp_text_input = zwp_text_input_manager_v3_get_text_input(registry->wp_text_input_manager, wl_seat);
607 zwp_text_input_v3_add_listener(ss->wp_text_input, &wp_text_input_listener, ss);
608 }
609
610 registry->wl_seats.push_back(wl_seat);
611
612 wl_seat_add_listener(wl_seat, &wl_seat_listener, ss);
613
614 if (registry->wayland_thread->wl_seat_current == nullptr) {
615 registry->wayland_thread->_set_current_seat(wl_seat);
616 }
617
618 return;
619 }
620
621 if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
622 registry->xdg_wm_base = (struct xdg_wm_base *)wl_registry_bind(wl_registry, name, &xdg_wm_base_interface, CLAMP((int)version, 1, 6));
623 registry->xdg_wm_base_name = name;
624
625 xdg_wm_base_add_listener(registry->xdg_wm_base, &xdg_wm_base_listener, nullptr);
626 return;
627 }
628
629 if (strcmp(interface, wp_viewporter_interface.name) == 0) {
630 registry->wp_viewporter = (struct wp_viewporter *)wl_registry_bind(wl_registry, name, &wp_viewporter_interface, 1);
631 registry->wp_viewporter_name = name;
632 }
633
634 if (strcmp(interface, wp_cursor_shape_manager_v1_interface.name) == 0) {
635 registry->wp_cursor_shape_manager = (struct wp_cursor_shape_manager_v1 *)wl_registry_bind(wl_registry, name, &wp_cursor_shape_manager_v1_interface, 1);
636 registry->wp_cursor_shape_manager_name = name;
637 return;
638 }
639
640 if (strcmp(interface, wp_fractional_scale_manager_v1_interface.name) == 0) {
641 registry->wp_fractional_scale_manager = (struct wp_fractional_scale_manager_v1 *)wl_registry_bind(wl_registry, name, &wp_fractional_scale_manager_v1_interface, 1);
642 registry->wp_fractional_scale_manager_name = name;
643
644 // NOTE: We're not mapping the fractional scale object here because this is
645 // supposed to be a "startup global". If for some reason this isn't true (who
646 // knows), add a conditional branch for creating the add-on object.
647 }
648
649 if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
650 registry->xdg_decoration_manager = (struct zxdg_decoration_manager_v1 *)wl_registry_bind(wl_registry, name, &zxdg_decoration_manager_v1_interface, 1);
651 registry->xdg_decoration_manager_name = name;
652 return;
653 }
654
655 if (strcmp(interface, xdg_system_bell_v1_interface.name) == 0) {
656 registry->xdg_system_bell = (struct xdg_system_bell_v1 *)wl_registry_bind(wl_registry, name, &xdg_system_bell_v1_interface, 1);
657 registry->xdg_system_bell_name = name;
658 return;
659 }
660
661 if (strcmp(interface, xdg_toplevel_icon_manager_v1_interface.name) == 0) {
662 registry->xdg_toplevel_icon_manager = (struct xdg_toplevel_icon_manager_v1 *)wl_registry_bind(wl_registry, name, &xdg_toplevel_icon_manager_v1_interface, 1);
663 registry->xdg_toplevel_icon_manager_name = name;
664 return;
665 }
666
667 if (strcmp(interface, xdg_activation_v1_interface.name) == 0) {
668 registry->xdg_activation = (struct xdg_activation_v1 *)wl_registry_bind(wl_registry, name, &xdg_activation_v1_interface, 1);
669 registry->xdg_activation_name = name;
670 return;
671 }
672
673 if (strcmp(interface, zwp_primary_selection_device_manager_v1_interface.name) == 0) {
674 registry->wp_primary_selection_device_manager = (struct zwp_primary_selection_device_manager_v1 *)wl_registry_bind(wl_registry, name, &zwp_primary_selection_device_manager_v1_interface, 1);
675
676 // This global creates some seat data. Let's do that for the ones already available.
677 for (struct wl_seat *wl_seat : registry->wl_seats) {
678 SeatState *ss = wl_seat_get_seat_state(wl_seat);
679 ERR_FAIL_NULL(ss);
680
681 if (!ss->wp_primary_selection_device && registry->wp_primary_selection_device_manager) {
682 ss->wp_primary_selection_device = zwp_primary_selection_device_manager_v1_get_device(registry->wp_primary_selection_device_manager, wl_seat);
683 zwp_primary_selection_device_v1_add_listener(ss->wp_primary_selection_device, &wp_primary_selection_device_listener, ss);
684 }
685 }
686 }
687
688 if (strcmp(interface, zwp_relative_pointer_manager_v1_interface.name) == 0) {
689 registry->wp_relative_pointer_manager = (struct zwp_relative_pointer_manager_v1 *)wl_registry_bind(wl_registry, name, &zwp_relative_pointer_manager_v1_interface, 1);
690 registry->wp_relative_pointer_manager_name = name;
691 return;
692 }
693
694 if (strcmp(interface, zwp_pointer_constraints_v1_interface.name) == 0) {
695 registry->wp_pointer_constraints = (struct zwp_pointer_constraints_v1 *)wl_registry_bind(wl_registry, name, &zwp_pointer_constraints_v1_interface, 1);
696 registry->wp_pointer_constraints_name = name;
697 return;
698 }
699
700 if (strcmp(interface, zwp_pointer_gestures_v1_interface.name) == 0) {
701 registry->wp_pointer_gestures = (struct zwp_pointer_gestures_v1 *)wl_registry_bind(wl_registry, name, &zwp_pointer_gestures_v1_interface, 1);
702 registry->wp_pointer_gestures_name = name;
703 return;
704 }
705
706 if (strcmp(interface, zwp_idle_inhibit_manager_v1_interface.name) == 0) {
707 registry->wp_idle_inhibit_manager = (struct zwp_idle_inhibit_manager_v1 *)wl_registry_bind(wl_registry, name, &zwp_idle_inhibit_manager_v1_interface, 1);
708 registry->wp_idle_inhibit_manager_name = name;
709 return;
710 }
711
712 if (strcmp(interface, zwp_tablet_manager_v2_interface.name) == 0) {
713 registry->wp_tablet_manager = (struct zwp_tablet_manager_v2 *)wl_registry_bind(wl_registry, name, &zwp_tablet_manager_v2_interface, 1);
714 registry->wp_tablet_manager_name = name;
715
716 // This global creates some seat data. Let's do that for the ones already available.
717 for (struct wl_seat *wl_seat : registry->wl_seats) {
718 SeatState *ss = wl_seat_get_seat_state(wl_seat);
719 ERR_FAIL_NULL(ss);
720
721 ss->wp_tablet_seat = zwp_tablet_manager_v2_get_tablet_seat(registry->wp_tablet_manager, wl_seat);
722 zwp_tablet_seat_v2_add_listener(ss->wp_tablet_seat, &wp_tablet_seat_listener, ss);
723 }
724
725 return;
726 }
727
728 if (strcmp(interface, zwp_text_input_manager_v3_interface.name) == 0) {
729 registry->wp_text_input_manager = (struct zwp_text_input_manager_v3 *)wl_registry_bind(wl_registry, name, &zwp_text_input_manager_v3_interface, 1);
730 registry->wp_text_input_manager_name = name;
731
732 // This global creates some seat data. Let's do that for the ones already available.
733 for (struct wl_seat *wl_seat : registry->wl_seats) {
734 SeatState *ss = wl_seat_get_seat_state(wl_seat);
735 ERR_FAIL_NULL(ss);
736
737 ss->wp_text_input = zwp_text_input_manager_v3_get_text_input(registry->wp_text_input_manager, wl_seat);
738 zwp_text_input_v3_add_listener(ss->wp_text_input, &wp_text_input_listener, ss);
739 }
740
741 return;
742 }
743
744 if (strcmp(interface, FIFO_INTERFACE_NAME) == 0) {
745 registry->wp_fifo_manager_name = name;
746 }
747
748 if (strcmp(interface, godot_embedding_compositor_interface.name) == 0) {
749 registry->godot_embedding_compositor = (struct godot_embedding_compositor *)wl_registry_bind(wl_registry, name, &godot_embedding_compositor_interface, 1);
750 registry->godot_embedding_compositor_name = name;
751
752 godot_embedding_compositor_add_listener(registry->godot_embedding_compositor, &godot_embedding_compositor_listener, memnew(EmbeddingCompositorState));
753 }
754 }
755
756 void WaylandThread::_wl_registry_on_global_remove(void *data, struct wl_registry *wl_registry, uint32_t name) {
757 RegistryState *registry = (RegistryState *)data;
758 ERR_FAIL_NULL(registry);
759
760 if (name == registry->wl_shm_name) {
761 if (registry->wl_shm) {
762 wl_shm_destroy(registry->wl_shm);
763 registry->wl_shm = nullptr;
764 }
765
766 registry->wl_shm_name = 0;
767
768 return;
769 }
770
771 // NOTE: Deprecated.
772 if (name == registry->xdg_exporter_v1_name) {
773 if (registry->xdg_exporter_v1) {
774 zxdg_exporter_v1_destroy(registry->xdg_exporter_v1);
775 registry->xdg_exporter_v1 = nullptr;
776 }
777
778 registry->xdg_exporter_v1_name = 0;
779
780 return;
781 }
782
783 if (name == registry->xdg_exporter_v2_name) {
784 if (registry->xdg_exporter_v2) {
785 zxdg_exporter_v2_destroy(registry->xdg_exporter_v2);
786 registry->xdg_exporter_v2 = nullptr;
787 }
788
789 registry->xdg_exporter_v2_name = 0;
790
791 return;
792 }
793
794 if (name == registry->wl_compositor_name) {
795 if (registry->wl_compositor) {
796 wl_compositor_destroy(registry->wl_compositor);
797 registry->wl_compositor = nullptr;
798 }
799
800 registry->wl_compositor_name = 0;
801
802 return;
803 }
804
805 if (name == registry->wl_data_device_manager_name) {
806 if (registry->wl_data_device_manager) {
807 wl_data_device_manager_destroy(registry->wl_data_device_manager);
808 registry->wl_data_device_manager = nullptr;
809 }
810
811 registry->wl_data_device_manager_name = 0;
812
813 // This global is used to create some seat data. Let's clean it.
814 for (struct wl_seat *wl_seat : registry->wl_seats) {
815 SeatState *ss = wl_seat_get_seat_state(wl_seat);
816 ERR_FAIL_NULL(ss);
817
818 if (ss->wl_data_device) {
819 wl_data_device_destroy(ss->wl_data_device);
820 ss->wl_data_device = nullptr;
821 }
822
823 ss->wl_data_device = nullptr;
824 }
825
826 return;
827 }
828
829 if (name == registry->xdg_wm_base_name) {
830 if (registry->xdg_wm_base) {
831 xdg_wm_base_destroy(registry->xdg_wm_base);
832 registry->xdg_wm_base = nullptr;
833 }
834
835 registry->xdg_wm_base_name = 0;
836
837 return;
838 }
839
840 if (name == registry->wp_viewporter_name) {
841 for (KeyValue<DisplayServer::WindowID, WindowState> &pair : registry->wayland_thread->windows) {
842 WindowState &ws = pair.value;
843 if (registry->wp_viewporter) {
844 wp_viewporter_destroy(registry->wp_viewporter);
845 registry->wp_viewporter = nullptr;
846 }
847
848 if (ws.wp_viewport) {
849 wp_viewport_destroy(ws.wp_viewport);
850 ws.wp_viewport = nullptr;
851 }
852 }
853
854 registry->wp_viewporter_name = 0;
855
856 return;
857 }
858
859 if (name == registry->wp_cursor_shape_manager_name) {
860 if (registry->wp_cursor_shape_manager) {
861 wp_cursor_shape_manager_v1_destroy(registry->wp_cursor_shape_manager);
862 registry->wp_cursor_shape_manager = nullptr;
863 }
864
865 registry->wp_cursor_shape_manager_name = 0;
866
867 for (struct wl_seat *wl_seat : registry->wl_seats) {
868 SeatState *ss = wl_seat_get_seat_state(wl_seat);
869 ERR_FAIL_NULL(ss);
870
871 if (ss->wp_cursor_shape_device) {
872 wp_cursor_shape_device_v1_destroy(ss->wp_cursor_shape_device);
873 ss->wp_cursor_shape_device = nullptr;
874 }
875 }
876 }
877
878 if (name == registry->wp_fractional_scale_manager_name) {
879 for (KeyValue<DisplayServer::WindowID, WindowState> &pair : registry->wayland_thread->windows) {
880 WindowState &ws = pair.value;
881
882 if (registry->wp_fractional_scale_manager) {
883 wp_fractional_scale_manager_v1_destroy(registry->wp_fractional_scale_manager);
884 registry->wp_fractional_scale_manager = nullptr;
885 }
886
887 if (ws.wp_fractional_scale) {
888 wp_fractional_scale_v1_destroy(ws.wp_fractional_scale);
889 ws.wp_fractional_scale = nullptr;
890 }
891 }
892
893 registry->wp_fractional_scale_manager_name = 0;
894 }
895
896 if (name == registry->xdg_decoration_manager_name) {
897 if (registry->xdg_decoration_manager) {
898 zxdg_decoration_manager_v1_destroy(registry->xdg_decoration_manager);
899 registry->xdg_decoration_manager = nullptr;
900 }
901
902 registry->xdg_decoration_manager_name = 0;
903
904 return;
905 }
906
907 if (name == registry->xdg_system_bell_name) {
908 if (registry->xdg_system_bell) {
909 xdg_system_bell_v1_destroy(registry->xdg_system_bell);
910 registry->xdg_system_bell = nullptr;
911 }
912
913 registry->xdg_system_bell_name = 0;
914
915 return;
916 }
917
918 if (name == registry->xdg_toplevel_icon_manager_name) {
919 if (registry->xdg_toplevel_icon_manager) {
920 xdg_toplevel_icon_manager_v1_destroy(registry->xdg_toplevel_icon_manager);
921 registry->xdg_toplevel_icon_manager = nullptr;
922 }
923
924 if (registry->wayland_thread->xdg_icon) {
925 xdg_toplevel_icon_v1_destroy(registry->wayland_thread->xdg_icon);
926 }
927
928 if (registry->wayland_thread->icon_buffer) {
929 wl_buffer_destroy(registry->wayland_thread->icon_buffer);
930 }
931
932 registry->xdg_toplevel_icon_manager_name = 0;
933
934 return;
935 }
936
937 if (name == registry->xdg_activation_name) {
938 if (registry->xdg_activation) {
939 xdg_activation_v1_destroy(registry->xdg_activation);
940 registry->xdg_activation = nullptr;
941 }
942
943 registry->xdg_activation_name = 0;
944
945 return;
946 }
947
948 if (name == registry->wp_primary_selection_device_manager_name) {
949 if (registry->wp_primary_selection_device_manager) {
950 zwp_primary_selection_device_manager_v1_destroy(registry->wp_primary_selection_device_manager);
951 registry->wp_primary_selection_device_manager = nullptr;
952 }
953
954 registry->wp_primary_selection_device_manager_name = 0;
955
956 // This global is used to create some seat data. Let's clean it.
957 for (struct wl_seat *wl_seat : registry->wl_seats) {
958 SeatState *ss = wl_seat_get_seat_state(wl_seat);
959 ERR_FAIL_NULL(ss);
960
961 if (ss->wp_primary_selection_device) {
962 zwp_primary_selection_device_v1_destroy(ss->wp_primary_selection_device);
963 ss->wp_primary_selection_device = nullptr;
964 }
965
966 if (ss->wp_primary_selection_source) {
967 zwp_primary_selection_source_v1_destroy(ss->wp_primary_selection_source);
968 ss->wp_primary_selection_source = nullptr;
969 }
970
971 if (ss->wp_primary_selection_offer) {
972 memfree(wp_primary_selection_offer_get_offer_state(ss->wp_primary_selection_offer));
973 zwp_primary_selection_offer_v1_destroy(ss->wp_primary_selection_offer);
974 ss->wp_primary_selection_offer = nullptr;
975 }
976 }
977
978 return;
979 }
980
981 if (name == registry->wp_relative_pointer_manager_name) {
982 if (registry->wp_relative_pointer_manager) {
983 zwp_relative_pointer_manager_v1_destroy(registry->wp_relative_pointer_manager);
984 registry->wp_relative_pointer_manager = nullptr;
985 }
986
987 registry->wp_relative_pointer_manager_name = 0;
988
989 // This global is used to create some seat data. Let's clean it.
990 for (struct wl_seat *wl_seat : registry->wl_seats) {
991 SeatState *ss = wl_seat_get_seat_state(wl_seat);
992 ERR_FAIL_NULL(ss);
993
994 if (ss->wp_relative_pointer) {
995 zwp_relative_pointer_v1_destroy(ss->wp_relative_pointer);
996 ss->wp_relative_pointer = nullptr;
997 }
998 }
999
1000 return;
1001 }
1002
1003 if (name == registry->wp_pointer_constraints_name) {
1004 if (registry->wp_pointer_constraints) {
1005 zwp_pointer_constraints_v1_destroy(registry->wp_pointer_constraints);
1006 registry->wp_pointer_constraints = nullptr;
1007 }
1008
1009 registry->wp_pointer_constraints_name = 0;
1010
1011 // This global is used to create some seat data. Let's clean it.
1012 for (struct wl_seat *wl_seat : registry->wl_seats) {
1013 SeatState *ss = wl_seat_get_seat_state(wl_seat);
1014 ERR_FAIL_NULL(ss);
1015
1016 if (ss->wp_relative_pointer) {
1017 zwp_relative_pointer_v1_destroy(ss->wp_relative_pointer);
1018 ss->wp_relative_pointer = nullptr;
1019 }
1020
1021 if (ss->wp_locked_pointer) {
1022 zwp_locked_pointer_v1_destroy(ss->wp_locked_pointer);
1023 ss->wp_locked_pointer = nullptr;
1024 }
1025
1026 if (ss->wp_confined_pointer) {
1027 zwp_confined_pointer_v1_destroy(ss->wp_confined_pointer);
1028 ss->wp_confined_pointer = nullptr;
1029 }
1030 }
1031
1032 return;
1033 }
1034
1035 if (name == registry->wp_pointer_gestures_name) {
1036 if (registry->wp_pointer_gestures) {
1037 zwp_pointer_gestures_v1_destroy(registry->wp_pointer_gestures);
1038 }
1039
1040 registry->wp_pointer_gestures = nullptr;
1041 registry->wp_pointer_gestures_name = 0;
1042
1043 // This global is used to create some seat data. Let's clean it.
1044 for (struct wl_seat *wl_seat : registry->wl_seats) {
1045 SeatState *ss = wl_seat_get_seat_state(wl_seat);
1046 ERR_FAIL_NULL(ss);
1047
1048 if (ss->wp_pointer_gesture_pinch) {
1049 zwp_pointer_gesture_pinch_v1_destroy(ss->wp_pointer_gesture_pinch);
1050 ss->wp_pointer_gesture_pinch = nullptr;
1051 }
1052 }
1053
1054 return;
1055 }
1056
1057 if (name == registry->wp_idle_inhibit_manager_name) {
1058 if (registry->wp_idle_inhibit_manager) {
1059 zwp_idle_inhibit_manager_v1_destroy(registry->wp_idle_inhibit_manager);
1060 registry->wp_idle_inhibit_manager = nullptr;
1061 }
1062
1063 registry->wp_idle_inhibit_manager_name = 0;
1064
1065 return;
1066 }
1067
1068 if (name == registry->wp_tablet_manager_name) {
1069 if (registry->wp_tablet_manager) {
1070 zwp_tablet_manager_v2_destroy(registry->wp_tablet_manager);
1071 registry->wp_tablet_manager = nullptr;
1072 }
1073
1074 registry->wp_tablet_manager_name = 0;
1075
1076 // This global is used to create some seat data. Let's clean it.
1077 for (struct wl_seat *wl_seat : registry->wl_seats) {
1078 SeatState *ss = wl_seat_get_seat_state(wl_seat);
1079 ERR_FAIL_NULL(ss);
1080
1081 for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
1082 TabletToolState *state = wp_tablet_tool_get_state(tool);
1083 if (state) {
1084 memdelete(state);
1085 }
1086
1087 zwp_tablet_tool_v2_destroy(tool);
1088 }
1089
1090 ss->tablet_tools.clear();
1091 }
1092
1093 return;
1094 }
1095
1096 if (name == registry->wp_text_input_manager_name) {
1097 if (registry->wp_text_input_manager) {
1098 zwp_text_input_manager_v3_destroy(registry->wp_text_input_manager);
1099 registry->wp_text_input_manager = nullptr;
1100 }
1101
1102 registry->wp_text_input_manager_name = 0;
1103
1104 for (struct wl_seat *wl_seat : registry->wl_seats) {
1105 SeatState *ss = wl_seat_get_seat_state(wl_seat);
1106 ERR_FAIL_NULL(ss);
1107
1108 zwp_text_input_v3_destroy(ss->wp_text_input);
1109 ss->wp_text_input = nullptr;
1110 }
1111
1112 return;
1113 }
1114
1115 {
1116 // Iterate through all of the seats to find if any got removed.
1117 List<struct wl_seat *>::Element *E = registry->wl_seats.front();
1118 while (E) {
1119 struct wl_seat *wl_seat = E->get();
1120 List<struct wl_seat *>::Element *N = E->next();
1121
1122 SeatState *ss = wl_seat_get_seat_state(wl_seat);
1123 ERR_FAIL_NULL(ss);
1124
1125 if (ss->wl_seat_name == name) {
1126 if (wl_seat) {
1127 wl_seat_destroy(wl_seat);
1128 }
1129
1130 if (ss->wl_data_device) {
1131 wl_data_device_destroy(ss->wl_data_device);
1132 }
1133
1134 if (ss->wp_tablet_seat) {
1135 zwp_tablet_seat_v2_destroy(ss->wp_tablet_seat);
1136
1137 for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
1138 TabletToolState *state = wp_tablet_tool_get_state(tool);
1139 if (state) {
1140 memdelete(state);
1141 }
1142
1143 zwp_tablet_tool_v2_destroy(tool);
1144 }
1145 }
1146
1147 memdelete(ss);
1148
1149 registry->wl_seats.erase(E);
1150 return;
1151 }
1152
1153 E = N;
1154 }
1155 }
1156
1157 {
1158 // Iterate through all of the outputs to find if any got removed.
1159 // FIXME: This is a very bruteforce approach.
1160 List<struct wl_output *>::Element *it = registry->wl_outputs.front();
1161 while (it) {
1162 // Iterate through all of the screens to find if any got removed.
1163 struct wl_output *wl_output = it->get();
1164 ERR_FAIL_NULL(wl_output);
1165
1166 ScreenState *ss = wl_output_get_screen_state(wl_output);
1167
1168 if (ss->wl_output_name == name) {
1169 registry->wl_outputs.erase(it);
1170
1171 memdelete(ss);
1172 wl_output_destroy(wl_output);
1173
1174 return;
1175 }
1176
1177 it = it->next();
1178 }
1179 }
1180
1181 if (name == registry->wp_fifo_manager_name) {
1182 registry->wp_fifo_manager_name = 0;
1183 }
1184
1185 if (name == registry->godot_embedding_compositor_name) {
1186 registry->godot_embedding_compositor_name = 0;
1187
1188 EmbeddingCompositorState *es = godot_embedding_compositor_get_state(registry->godot_embedding_compositor);
1189 ERR_FAIL_NULL(es);
1190
1191 es->mapped_clients.clear();
1192
1193 for (struct godot_embedded_client *client : es->clients) {
1194 godot_embedded_client_destroy(client);
1195 }
1196 es->clients.clear();
1197
1198 memdelete(es);
1199
1200 godot_embedding_compositor_destroy(registry->godot_embedding_compositor);
1201 registry->godot_embedding_compositor = nullptr;
1202 }
1203 }
1204
1205 void WaylandThread::_wl_surface_on_enter(void *data, struct wl_surface *wl_surface, struct wl_output *wl_output) {
1206 if (!wl_output || !wl_proxy_is_godot((struct wl_proxy *)wl_output)) {
1207 // This won't have the right data bound to it. Not worth it and would probably
1208 // just break everything.
1209 return;
1210 }
1211
1212 WindowState *ws = (WindowState *)data;
1213 ERR_FAIL_NULL(ws);
1214
1215 DEBUG_LOG_WAYLAND_THREAD(vformat("Window entered output %x.", (size_t)wl_output));
1216
1217 ws->wl_outputs.insert(wl_output);
1218
1219 // Workaround for buffer scaling as there's no guaranteed way of knowing the
1220 // preferred scale.
1221 // TODO: Skip this branch for newer `wl_surface`s once we add support for
1222 // `wl_surface::preferred_buffer_scale`
1223 if (ws->preferred_fractional_scale == 0) {
1224 window_state_update_size(ws, ws->rect.size.width, ws->rect.size.height);
1225 }
1226 }
1227
1228 void WaylandThread::_frame_wl_callback_on_done(void *data, struct wl_callback *wl_callback, uint32_t callback_data) {
1229 wl_callback_destroy(wl_callback);
1230
1231 WindowState *ws = (WindowState *)data;
1232 ERR_FAIL_NULL(ws);
1233 ERR_FAIL_NULL(ws->wayland_thread);
1234 ERR_FAIL_NULL(ws->wl_surface);
1235
1236 ws->last_frame_time = OS::get_singleton()->get_ticks_usec();
1237 ws->wayland_thread->set_frame();
1238
1239 ws->frame_callback = wl_surface_frame(ws->wl_surface);
1240 wl_callback_add_listener(ws->frame_callback, &frame_wl_callback_listener, ws);
1241
1242 if (ws->wl_surface && ws->buffer_scale_changed) {
1243 // NOTE: We're only now setting the buffer scale as the idea is to get this
1244 // data committed together with the new frame, all by the rendering driver.
1245 // This is important because we might otherwise set an invalid combination of
1246 // buffer size and scale (e.g. odd size and 2x scale). We're pretty much
1247 // guaranteed to get a proper buffer in the next render loop as the rescaling
1248 // method also informs the engine of a "window rect change", triggering
1249 // rendering if needed.
1250 wl_surface_set_buffer_scale(ws->wl_surface, window_state_get_preferred_buffer_scale(ws));
1251 }
1252 }
1253
1254 void WaylandThread::_wl_surface_on_leave(void *data, struct wl_surface *wl_surface, struct wl_output *wl_output) {
1255 if (!wl_output || !wl_proxy_is_godot((struct wl_proxy *)wl_output)) {
1256 // This won't have the right data bound to it. Not worth it and would probably
1257 // just break everything.
1258 return;
1259 }
1260
1261 WindowState *ws = (WindowState *)data;
1262 ERR_FAIL_NULL(ws);
1263
1264 ws->wl_outputs.erase(wl_output);
1265
1266 DEBUG_LOG_WAYLAND_THREAD(vformat("Window left output %x.\n", (size_t)wl_output));
1267 }
1268
1269 // TODO: Add support to this event.
1270 void WaylandThread::_wl_surface_on_preferred_buffer_scale(void *data, struct wl_surface *wl_surface, int32_t factor) {
1271 }
1272
1273 // TODO: Add support to this event.
1274 void WaylandThread::_wl_surface_on_preferred_buffer_transform(void *data, struct wl_surface *wl_surface, uint32_t transform) {
1275 }
1276
1277 void WaylandThread::_wl_output_on_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y, int32_t physical_width, int32_t physical_height, int32_t subpixel, const char *make, const char *model, int32_t transform) {
1278 ScreenState *ss = (ScreenState *)data;
1279 ERR_FAIL_NULL(ss);
1280
1281 ss->pending_data.position.x = x;
1282
1283 ss->pending_data.position.x = x;
1284 ss->pending_data.position.y = y;
1285
1286 ss->pending_data.physical_size.width = physical_width;
1287 ss->pending_data.physical_size.height = physical_height;
1288
1289 ss->pending_data.make.clear();
1290 ss->pending_data.make.append_utf8(make);
1291 ss->pending_data.model.clear();
1292 ss->pending_data.model.append_utf8(model);
1293
1294 // `wl_output::done` is a version 2 addition. We'll directly update the data
1295 // for compatibility.
1296 if (wl_output_get_version(wl_output) == 1) {
1297 ss->data = ss->pending_data;
1298 }
1299 }
1300
1301 void WaylandThread::_wl_output_on_mode(void *data, struct wl_output *wl_output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
1302 ScreenState *ss = (ScreenState *)data;
1303 ERR_FAIL_NULL(ss);
1304
1305 if (!(flags & WL_OUTPUT_MODE_CURRENT)) {
1306 return;
1307 }
1308
1309 ss->pending_data.size.width = width;
1310 ss->pending_data.size.height = height;
1311
1312 ss->pending_data.refresh_rate = refresh ? refresh / 1000.0f : -1;
1313
1314 // `wl_output::done` is a version 2 addition. We'll directly update the data
1315 // for compatibility.
1316 if (wl_output_get_version(wl_output) == 1) {
1317 ss->data = ss->pending_data;
1318 }
1319 }
1320
1321 // NOTE: The following `wl_output` events are only for version 2 onwards, so we
1322 // can assume that they're "atomic" (i.e. rely on the `wl_output::done` event).
1323
1324 void WaylandThread::_wl_output_on_done(void *data, struct wl_output *wl_output) {
1325 ScreenState *ss = (ScreenState *)data;
1326 ERR_FAIL_NULL(ss);
1327
1328 ss->data = ss->pending_data;
1329
1330 ss->wayland_thread->_update_scale(ss->data.scale);
1331
1332 DEBUG_LOG_WAYLAND_THREAD(vformat("Output %x done.", (size_t)wl_output));
1333 }
1334
1335 void WaylandThread::_wl_output_on_scale(void *data, struct wl_output *wl_output, int32_t factor) {
1336 ScreenState *ss = (ScreenState *)data;
1337 ERR_FAIL_NULL(ss);
1338
1339 ss->pending_data.scale = factor;
1340
1341 DEBUG_LOG_WAYLAND_THREAD(vformat("Output %x scale %d", (size_t)wl_output, factor));
1342 }
1343
1344 void WaylandThread::_wl_output_on_name(void *data, struct wl_output *wl_output, const char *name) {
1345 }
1346
1347 void WaylandThread::_wl_output_on_description(void *data, struct wl_output *wl_output, const char *description) {
1348 }
1349
1350 void WaylandThread::_xdg_wm_base_on_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial) {
1351 xdg_wm_base_pong(xdg_wm_base, serial);
1352 }
1353
1354 void WaylandThread::_xdg_surface_on_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial) {
1355 xdg_surface_ack_configure(xdg_surface, serial);
1356
1357 WindowState *ws = (WindowState *)data;
1358 ERR_FAIL_NULL(ws);
1359
1360 ws->ready = true;
1361
1362 DEBUG_LOG_WAYLAND_THREAD(vformat("xdg surface on configure rect %s", ws->rect));
1363 }
1364
1365 void WaylandThread::_xdg_toplevel_on_configure(void *data, struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height, struct wl_array *states) {
1366 WindowState *ws = (WindowState *)data;
1367 ERR_FAIL_NULL(ws);
1368
1369 if (width == 0) {
1370 width = ws->rect.size.width;
1371 }
1372
1373 if (height == 0) {
1374 height = ws->rect.size.height;
1375 }
1376
1377 // Expect the window to be in a plain state. It will get properly set if the
1378 // compositor reports otherwise below.
1379 ws->mode = DisplayServer::WINDOW_MODE_WINDOWED;
1380 ws->maximized = false;
1381 ws->fullscreen = false;
1382 ws->resizing = false;
1383 ws->tiled_left = false;
1384 ws->tiled_right = false;
1385 ws->tiled_top = false;
1386 ws->tiled_bottom = false;
1387 ws->suspended = false;
1388
1389 uint32_t *state = nullptr;
1390 wl_array_for_each(state, states) {
1391 switch (*state) {
1392 case XDG_TOPLEVEL_STATE_MAXIMIZED: {
1393 ws->mode = DisplayServer::WINDOW_MODE_MAXIMIZED;
1394 ws->maximized = true;
1395 } break;
1396
1397 case XDG_TOPLEVEL_STATE_FULLSCREEN: {
1398 ws->mode = DisplayServer::WINDOW_MODE_FULLSCREEN;
1399 ws->fullscreen = true;
1400 } break;
1401
1402 case XDG_TOPLEVEL_STATE_RESIZING: {
1403 ws->resizing = true;
1404 } break;
1405
1406 case XDG_TOPLEVEL_STATE_TILED_LEFT: {
1407 ws->tiled_left = true;
1408 } break;
1409
1410 case XDG_TOPLEVEL_STATE_TILED_RIGHT: {
1411 ws->tiled_right = true;
1412 } break;
1413
1414 case XDG_TOPLEVEL_STATE_TILED_TOP: {
1415 ws->tiled_top = true;
1416 } break;
1417
1418 case XDG_TOPLEVEL_STATE_TILED_BOTTOM: {
1419 ws->tiled_bottom = true;
1420 } break;
1421
1422 case XDG_TOPLEVEL_STATE_SUSPENDED: {
1423 ws->suspended = true;
1424 } break;
1425
1426 default: {
1427 // We don't care about the other states (for now).
1428 } break;
1429 }
1430 }
1431
1432 window_state_update_size(ws, width, height);
1433
1434 DEBUG_LOG_WAYLAND_THREAD(vformat("XDG toplevel on configure width %d height %d.", width, height));
1435 }
1436
1437 void WaylandThread::_xdg_toplevel_on_close(void *data, struct xdg_toplevel *xdg_toplevel) {
1438 WindowState *ws = (WindowState *)data;
1439 ERR_FAIL_NULL(ws);
1440
1441 Ref<WindowEventMessage> msg;
1442 msg.instantiate();
1443 msg->id = ws->id;
1444 msg->event = DisplayServer::WINDOW_EVENT_CLOSE_REQUEST;
1445 ws->wayland_thread->push_message(msg);
1446 }
1447
1448 void WaylandThread::_xdg_toplevel_on_configure_bounds(void *data, struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height) {
1449 }
1450
1451 void WaylandThread::_xdg_toplevel_on_wm_capabilities(void *data, struct xdg_toplevel *xdg_toplevel, struct wl_array *capabilities) {
1452 WindowState *ws = (WindowState *)data;
1453 ERR_FAIL_NULL(ws);
1454
1455 ws->can_maximize = false;
1456 ws->can_fullscreen = false;
1457 ws->can_minimize = false;
1458
1459 uint32_t *capability = nullptr;
1460 wl_array_for_each(capability, capabilities) {
1461 switch (*capability) {
1462 case XDG_TOPLEVEL_WM_CAPABILITIES_MAXIMIZE: {
1463 ws->can_maximize = true;
1464 } break;
1465 case XDG_TOPLEVEL_WM_CAPABILITIES_FULLSCREEN: {
1466 ws->can_fullscreen = true;
1467 } break;
1468
1469 case XDG_TOPLEVEL_WM_CAPABILITIES_MINIMIZE: {
1470 ws->can_minimize = true;
1471 } break;
1472
1473 default: {
1474 } break;
1475 }
1476 }
1477 }
1478
1479 void WaylandThread::_xdg_popup_on_configure(void *data, struct xdg_popup *xdg_popup, int32_t x, int32_t y, int32_t width, int32_t height) {
1480 WindowState *ws = (WindowState *)data;
1481 ERR_FAIL_NULL(ws);
1482
1483 if (width == 0) {
1484 width = ws->rect.size.width;
1485 }
1486
1487 if (height == 0) {
1488 height = ws->rect.size.width;
1489 }
1490
1491 window_state_update_size(ws, width, height);
1492
1493 WindowState *parent = ws->wayland_thread->window_get_state(ws->parent_id);
1494 ERR_FAIL_NULL(parent);
1495
1496 Point2i pos = Point2i(x, y);
1497 #ifdef LIBDECOR_ENABLED
1498 if (parent->libdecor_frame) {
1499 int translated_x = x;
1500 int translated_y = y;
1501 libdecor_frame_translate_coordinate(parent->libdecor_frame, x, y, &translated_x, &translated_y);
1502
1503 pos.x = translated_x;
1504 pos.y = translated_y;
1505 }
1506 #endif
1507
1508 // Looks like the position returned here is relative to the parent. We have to
1509 // accumulate it or there's gonna be a lot of confusion godot-side.
1510 pos += parent->rect.position;
1511
1512 if (ws->rect.position != pos) {
1513 DEBUG_LOG_WAYLAND_THREAD(vformat("Repositioning popup %d from %s to %s", ws->id, ws->rect.position, pos));
1514
1515 double parent_scale = window_state_get_scale_factor(parent);
1516
1517 ws->rect.position = pos;
1518
1519 Ref<WindowRectMessage> rect_msg;
1520 rect_msg.instantiate();
1521 rect_msg->id = ws->id;
1522 rect_msg->rect.position = scale_vector2i(ws->rect.position, parent_scale);
1523 rect_msg->rect.size = scale_vector2i(ws->rect.size, parent_scale);
1524
1525 ws->wayland_thread->push_message(rect_msg);
1526 }
1527
1528 DEBUG_LOG_WAYLAND_THREAD(vformat("xdg popup on configure x%d y%d w%d h%d", x, y, width, height));
1529 }
1530
1531 void WaylandThread::_xdg_popup_on_popup_done(void *data, struct xdg_popup *xdg_popup) {
1532 WindowState *ws = (WindowState *)data;
1533 ERR_FAIL_NULL(ws);
1534
1535 Ref<WindowEventMessage> ev_msg;
1536 ev_msg.instantiate();
1537 ev_msg->id = ws->id;
1538 ev_msg->event = DisplayServer::WINDOW_EVENT_FORCE_CLOSE;
1539
1540 ws->wayland_thread->push_message(ev_msg);
1541 }
1542
1543 void WaylandThread::_xdg_popup_on_repositioned(void *data, struct xdg_popup *xdg_popup, uint32_t token) {
1544 DEBUG_LOG_WAYLAND_THREAD(vformat("stub xdg popup repositioned %x", token));
1545 }
1546
1547 // NOTE: Deprecated.
1548 void WaylandThread::_xdg_exported_v1_on_handle(void *data, zxdg_exported_v1 *exported, const char *handle) {
1549 WindowState *ws = (WindowState *)data;
1550 ERR_FAIL_NULL(ws);
1551
1552 ws->exported_handle = vformat("wayland:%s", String::utf8(handle));
1553 }
1554
1555 void WaylandThread::_xdg_exported_v2_on_handle(void *data, zxdg_exported_v2 *exported, const char *handle) {
1556 WindowState *ws = (WindowState *)data;
1557 ERR_FAIL_NULL(ws);
1558
1559 ws->exported_handle = vformat("wayland:%s", String::utf8(handle));
1560 }
1561
1562 void WaylandThread::_xdg_toplevel_decoration_on_configure(void *data, struct zxdg_toplevel_decoration_v1 *xdg_toplevel_decoration, uint32_t mode) {
1563 if (mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE) {
1564 #ifdef LIBDECOR_ENABLED
1565 WARN_PRINT_ONCE("Native client side decorations are not yet supported without libdecor!");
1566 #else
1567 WARN_PRINT_ONCE("Native client side decorations are not yet supported!");
1568 #endif // LIBDECOR_ENABLED
1569 }
1570 }
1571
1572 #ifdef LIBDECOR_ENABLED
1573 void WaylandThread::libdecor_on_error(struct libdecor *context, enum libdecor_error error, const char *message) {
1574 ERR_PRINT(vformat("libdecor error %d: %s", error, message));
1575 }
1576
1577 // NOTE: This is pretty much a reimplementation of _xdg_surface_on_configure
1578 // and _xdg_toplevel_on_configure. Libdecor really likes wrapping everything,
1579 // forcing us to do stuff like this.
1580 void WaylandThread::libdecor_frame_on_configure(struct libdecor_frame *frame, struct libdecor_configuration *configuration, void *user_data) {
1581 WindowState *ws = (WindowState *)user_data;
1582 ERR_FAIL_NULL(ws);
1583
1584 ws->ready = true;
1585
1586 int width = 0;
1587 int height = 0;
1588
1589 ws->pending_libdecor_configuration = configuration;
1590
1591 if (!libdecor_configuration_get_content_size(configuration, frame, &width, &height)) {
1592 // The configuration doesn't have a size. We'll use the one already set in the window.
1593 width = ws->rect.size.width;
1594 height = ws->rect.size.height;
1595 }
1596
1597 ERR_FAIL_COND_MSG(width == 0 || height == 0, "Window has invalid size.");
1598
1599 libdecor_window_state window_state = LIBDECOR_WINDOW_STATE_NONE;
1600
1601 // Expect the window to be in a plain state. It will get properly set if the
1602 // compositor reports otherwise below.
1603 ws->mode = DisplayServer::WINDOW_MODE_WINDOWED;
1604 ws->maximized = false;
1605 ws->fullscreen = false;
1606 ws->resizing = false;
1607 ws->tiled_left = false;
1608 ws->tiled_right = false;
1609 ws->tiled_top = false;
1610 ws->tiled_bottom = false;
1611 ws->suspended = false;
1612
1613 if (libdecor_configuration_get_window_state(configuration, &window_state)) {
1614 if (window_state & LIBDECOR_WINDOW_STATE_MAXIMIZED) {
1615 ws->mode = DisplayServer::WINDOW_MODE_MAXIMIZED;
1616 ws->maximized = true;
1617 }
1618
1619 if (window_state & LIBDECOR_WINDOW_STATE_FULLSCREEN) {
1620 ws->mode = DisplayServer::WINDOW_MODE_FULLSCREEN;
1621 ws->fullscreen = true;
1622 }
1623
1624 // libdecor doesn't have the resizing state for whatever reason.
1625
1626 if (window_state & LIBDECOR_WINDOW_STATE_TILED_LEFT) {
1627 ws->tiled_left = true;
1628 }
1629
1630 if (window_state & LIBDECOR_WINDOW_STATE_TILED_RIGHT) {
1631 ws->tiled_right = true;
1632 }
1633
1634 if (window_state & LIBDECOR_WINDOW_STATE_TILED_TOP) {
1635 ws->tiled_top = true;
1636 }
1637
1638 if (window_state & LIBDECOR_WINDOW_STATE_TILED_BOTTOM) {
1639 ws->tiled_bottom = true;
1640 }
1641
1642 if (window_state & LIBDECOR_WINDOW_STATE_SUSPENDED) {
1643 ws->suspended = true;
1644 }
1645 }
1646
1647 window_state_update_size(ws, width, height);
1648
1649 DEBUG_LOG_WAYLAND_THREAD(vformat("libdecor frame on configure rect %s", ws->rect));
1650 }
1651
1652 void WaylandThread::libdecor_frame_on_close(struct libdecor_frame *frame, void *user_data) {
1653 WindowState *ws = (WindowState *)user_data;
1654 ERR_FAIL_NULL(ws);
1655
1656 Ref<WindowEventMessage> winevent_msg;
1657 winevent_msg.instantiate();
1658 winevent_msg->id = ws->id;
1659 winevent_msg->event = DisplayServer::WINDOW_EVENT_CLOSE_REQUEST;
1660
1661 ws->wayland_thread->push_message(winevent_msg);
1662
1663 DEBUG_LOG_WAYLAND_THREAD("libdecor frame on close");
1664 }
1665
1666 void WaylandThread::libdecor_frame_on_commit(struct libdecor_frame *frame, void *user_data) {
1667 // We're skipping this as we don't really care about libdecor's commit for
1668 // atomicity reasons. See `_frame_wl_callback_on_done` for more info.
1669
1670 DEBUG_LOG_WAYLAND_THREAD("libdecor frame on commit");
1671 }
1672
1673 void WaylandThread::libdecor_frame_on_dismiss_popup(struct libdecor_frame *frame, const char *seat_name, void *user_data) {
1674 }
1675 #endif // LIBDECOR_ENABLED
1676
1677 void WaylandThread::_wl_seat_on_capabilities(void *data, struct wl_seat *wl_seat, uint32_t capabilities) {
1678 SeatState *ss = (SeatState *)data;
1679
1680 ERR_FAIL_NULL(ss);
1681
1682 // TODO: Handle touch.
1683
1684 // Pointer handling.
1685 if (capabilities & WL_SEAT_CAPABILITY_POINTER) {
1686 if (!ss->wl_pointer) {
1687 ss->cursor_surface = wl_compositor_create_surface(ss->registry->wl_compositor);
1688 wl_surface_commit(ss->cursor_surface);
1689
1690 ss->wl_pointer = wl_seat_get_pointer(wl_seat);
1691 wl_pointer_add_listener(ss->wl_pointer, &wl_pointer_listener, ss);
1692
1693 if (ss->registry->wp_cursor_shape_manager) {
1694 ss->wp_cursor_shape_device = wp_cursor_shape_manager_v1_get_pointer(ss->registry->wp_cursor_shape_manager, ss->wl_pointer);
1695 }
1696
1697 if (ss->registry->wp_relative_pointer_manager) {
1698 ss->wp_relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(ss->registry->wp_relative_pointer_manager, ss->wl_pointer);
1699 zwp_relative_pointer_v1_add_listener(ss->wp_relative_pointer, &wp_relative_pointer_listener, ss);
1700 }
1701
1702 if (ss->registry->wp_pointer_gestures) {
1703 ss->wp_pointer_gesture_pinch = zwp_pointer_gestures_v1_get_pinch_gesture(ss->registry->wp_pointer_gestures, ss->wl_pointer);
1704 zwp_pointer_gesture_pinch_v1_add_listener(ss->wp_pointer_gesture_pinch, &wp_pointer_gesture_pinch_listener, ss);
1705 }
1706
1707 // TODO: Constrain new pointers if the global mouse mode is constrained.
1708 }
1709 } else {
1710 if (ss->cursor_frame_callback) {
1711 // Just in case. I got bitten by weird race-like conditions already.
1712 wl_callback_set_user_data(ss->cursor_frame_callback, nullptr);
1713
1714 wl_callback_destroy(ss->cursor_frame_callback);
1715 ss->cursor_frame_callback = nullptr;
1716 }
1717
1718 if (ss->cursor_surface) {
1719 wl_surface_destroy(ss->cursor_surface);
1720 ss->cursor_surface = nullptr;
1721 }
1722
1723 if (ss->wl_pointer) {
1724 wl_pointer_destroy(ss->wl_pointer);
1725 ss->wl_pointer = nullptr;
1726 }
1727
1728 if (ss->wp_cursor_shape_device) {
1729 wp_cursor_shape_device_v1_destroy(ss->wp_cursor_shape_device);
1730 ss->wp_cursor_shape_device = nullptr;
1731 }
1732
1733 if (ss->wp_relative_pointer) {
1734 zwp_relative_pointer_v1_destroy(ss->wp_relative_pointer);
1735 ss->wp_relative_pointer = nullptr;
1736 }
1737
1738 if (ss->wp_confined_pointer) {
1739 zwp_confined_pointer_v1_destroy(ss->wp_confined_pointer);
1740 ss->wp_confined_pointer = nullptr;
1741 }
1742
1743 if (ss->wp_locked_pointer) {
1744 zwp_locked_pointer_v1_destroy(ss->wp_locked_pointer);
1745 ss->wp_locked_pointer = nullptr;
1746 }
1747 }
1748
1749 // Keyboard handling.
1750 if (capabilities & WL_SEAT_CAPABILITY_KEYBOARD) {
1751 if (!ss->wl_keyboard) {
1752 ss->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
1753 ERR_FAIL_NULL(ss->xkb_context);
1754
1755 ss->wl_keyboard = wl_seat_get_keyboard(wl_seat);
1756 wl_keyboard_add_listener(ss->wl_keyboard, &wl_keyboard_listener, ss);
1757 }
1758 } else {
1759 if (ss->xkb_context) {
1760 xkb_context_unref(ss->xkb_context);
1761 ss->xkb_context = nullptr;
1762 }
1763
1764 if (ss->xkb_compose_table) {
1765 xkb_compose_table_unref(ss->xkb_compose_table);
1766 ss->xkb_compose_table = nullptr;
1767 }
1768
1769 if (ss->xkb_compose_state) {
1770 xkb_compose_state_unref(ss->xkb_compose_state);
1771 ss->xkb_compose_state = nullptr;
1772 }
1773
1774 if (ss->xkb_keymap) {
1775 xkb_keymap_unref(ss->xkb_keymap);
1776 ss->xkb_keymap = nullptr;
1777 }
1778
1779 if (ss->xkb_state) {
1780 xkb_state_unref(ss->xkb_state);
1781 ss->xkb_state = nullptr;
1782 }
1783
1784 if (ss->wl_keyboard) {
1785 wl_keyboard_destroy(ss->wl_keyboard);
1786 ss->wl_keyboard = nullptr;
1787 }
1788 }
1789 }
1790
1791 void WaylandThread::_wl_seat_on_name(void *data, struct wl_seat *wl_seat, const char *name) {
1792 }
1793
1794 void WaylandThread::_cursor_frame_callback_on_done(void *data, struct wl_callback *wl_callback, uint32_t time_ms) {
1795 wl_callback_destroy(wl_callback);
1796
1797 SeatState *ss = (SeatState *)data;
1798 ERR_FAIL_NULL(ss);
1799
1800 ss->cursor_frame_callback = nullptr;
1801
1802 ss->cursor_time_ms = time_ms;
1803
1804 seat_state_update_cursor(ss);
1805 }
1806
1807 void WaylandThread::_wl_pointer_on_enter(void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y) {
1808 WindowState *ws = wl_surface_get_window_state(surface);
1809 if (!ws) {
1810 return;
1811 }
1812
1813 SeatState *ss = (SeatState *)data;
1814 ERR_FAIL_NULL(ss);
1815
1816 ERR_FAIL_NULL(ss->cursor_surface);
1817
1818 PointerData &pd = ss->pointer_data_buffer;
1819
1820 ss->pointer_enter_serial = serial;
1821 pd.pointed_id = ws->id;
1822 pd.last_pointed_id = ws->id;
1823 pd.position.x = wl_fixed_to_double(surface_x);
1824 pd.position.y = wl_fixed_to_double(surface_y);
1825
1826 seat_state_update_cursor(ss);
1827
1828 DEBUG_LOG_WAYLAND_THREAD(vformat("Pointer entered window %d.", ws->id));
1829
1830 if (wl_pointer_get_version(wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
1831 _wl_pointer_on_frame(data, wl_pointer);
1832 }
1833 }
1834
1835 void WaylandThread::_wl_pointer_on_leave(void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface) {
1836 // NOTE: `surface` will probably be null when the surface is destroyed.
1837 // See: https://gitlab.freedesktop.org/wayland/wayland/-/issues/366
1838 // See: https://gitlab.freedesktop.org/wayland/wayland/-/issues/465
1839
1840 SeatState *ss = (SeatState *)data;
1841 ERR_FAIL_NULL(ss);
1842
1843 PointerData &pd = ss->pointer_data_buffer;
1844
1845 if (pd.pointed_id == DisplayServer::INVALID_WINDOW_ID) {
1846 // We're probably on a decoration or some other third-party thing.
1847 return;
1848 }
1849
1850 DisplayServer::WindowID id = pd.pointed_id;
1851
1852 pd.pointed_id = DisplayServer::INVALID_WINDOW_ID;
1853 pd.pressed_button_mask.clear();
1854
1855 DEBUG_LOG_WAYLAND_THREAD(vformat("Pointer left window %d.", id));
1856
1857 if (wl_pointer_get_version(wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
1858 _wl_pointer_on_frame(data, wl_pointer);
1859 }
1860 }
1861
1862 void WaylandThread::_wl_pointer_on_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
1863 SeatState *ss = (SeatState *)data;
1864 ERR_FAIL_NULL(ss);
1865
1866 PointerData &pd = ss->pointer_data_buffer;
1867
1868 pd.position.x = wl_fixed_to_double(surface_x);
1869 pd.position.y = wl_fixed_to_double(surface_y);
1870
1871 pd.motion_time = time;
1872
1873 if (wl_pointer_get_version(wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
1874 _wl_pointer_on_frame(data, wl_pointer);
1875 }
1876 }
1877
1878 void WaylandThread::_wl_pointer_on_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
1879 SeatState *ss = (SeatState *)data;
1880 ERR_FAIL_NULL(ss);
1881
1882 PointerData &pd = ss->pointer_data_buffer;
1883
1884 MouseButton button_pressed = MouseButton::NONE;
1885
1886 switch (button) {
1887 case BTN_LEFT:
1888 button_pressed = MouseButton::LEFT;
1889 break;
1890
1891 case BTN_MIDDLE:
1892 button_pressed = MouseButton::MIDDLE;
1893 break;
1894
1895 case BTN_RIGHT:
1896 button_pressed = MouseButton::RIGHT;
1897 break;
1898
1899 case BTN_EXTRA:
1900 button_pressed = MouseButton::MB_XBUTTON1;
1901 break;
1902
1903 case BTN_SIDE:
1904 button_pressed = MouseButton::MB_XBUTTON2;
1905 break;
1906
1907 default: {
1908 }
1909 }
1910
1911 MouseButtonMask mask = mouse_button_to_mask(button_pressed);
1912
1913 if (state & WL_POINTER_BUTTON_STATE_PRESSED) {
1914 pd.pressed_button_mask.set_flag(mask);
1915 pd.last_button_pressed = button_pressed;
1916 pd.double_click_begun = true;
1917 } else {
1918 pd.pressed_button_mask.clear_flag(mask);
1919 }
1920
1921 pd.button_time = time;
1922 pd.button_serial = serial;
1923
1924 if (wl_pointer_get_version(wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
1925 _wl_pointer_on_frame(data, wl_pointer);
1926 }
1927 }
1928
1929 void WaylandThread::_wl_pointer_on_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {
1930 SeatState *ss = (SeatState *)data;
1931 ERR_FAIL_NULL(ss);
1932
1933 PointerData &pd = ss->pointer_data_buffer;
1934
1935 switch (axis) {
1936 case WL_POINTER_AXIS_VERTICAL_SCROLL: {
1937 pd.scroll_vector.y = wl_fixed_to_double(value);
1938 } break;
1939
1940 case WL_POINTER_AXIS_HORIZONTAL_SCROLL: {
1941 pd.scroll_vector.x = wl_fixed_to_double(value);
1942 } break;
1943 }
1944
1945 pd.button_time = time;
1946
1947 if (wl_pointer_get_version(wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
1948 _wl_pointer_on_frame(data, wl_pointer);
1949 }
1950 }
1951
1952 void WaylandThread::_wl_pointer_on_frame(void *data, struct wl_pointer *wl_pointer) {
1953 SeatState *ss = (SeatState *)data;
1954 ERR_FAIL_NULL(ss);
1955
1956 WaylandThread *wayland_thread = ss->wayland_thread;
1957 ERR_FAIL_NULL(wayland_thread);
1958
1959 PointerData &old_pd = ss->pointer_data;
1960 PointerData &pd = ss->pointer_data_buffer;
1961
1962 if (pd.pointed_id != old_pd.pointed_id) {
1963 if (old_pd.pointed_id != DisplayServer::INVALID_WINDOW_ID) {
1964 Ref<WindowEventMessage> msg;
1965 msg.instantiate();
1966 msg->id = old_pd.pointed_id;
1967 msg->event = DisplayServer::WINDOW_EVENT_MOUSE_EXIT;
1968
1969 wayland_thread->push_message(msg);
1970 }
1971
1972 if (pd.pointed_id != DisplayServer::INVALID_WINDOW_ID) {
1973 Ref<WindowEventMessage> msg;
1974 msg.instantiate();
1975 msg->id = pd.pointed_id;
1976 msg->event = DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
1977
1978 wayland_thread->push_message(msg);
1979 }
1980 }
1981
1982 WindowState *ws = nullptr;
1983
1984 // NOTE: At least on sway, with wl_pointer version 5 or greater,
1985 // wl_pointer::leave might be emitted with other events (like
1986 // wl_pointer::button) within the same wl_pointer::frame. Because of this, we
1987 // need to account for when the currently pointed window might be invalid
1988 // (third-party or even none) and fall back to the old one.
1989 if (pd.pointed_id != DisplayServer::INVALID_WINDOW_ID) {
1990 ws = ss->wayland_thread->window_get_state(pd.pointed_id);
1991 ERR_FAIL_NULL(ws);
1992 } else if (old_pd.pointed_id != DisplayServer::INVALID_WINDOW_ID) {
1993 ws = ss->wayland_thread->window_get_state(old_pd.pointed_id);
1994 ERR_FAIL_NULL(ws);
1995 }
1996
1997 if (ws == nullptr) {
1998 // We're probably on a decoration or some other third-party thing. Let's
1999 // "commit" the data and call it a day.
2000 old_pd = pd;
2001 return;
2002 }
2003
2004 double scale = window_state_get_scale_factor(ws);
2005
2006 wayland_thread->_set_current_seat(ss->wl_seat);
2007
2008 if (old_pd.motion_time != pd.motion_time || old_pd.relative_motion_time != pd.relative_motion_time) {
2009 Ref<InputEventMouseMotion> mm;
2010 mm.instantiate();
2011
2012 // Set all pressed modifiers.
2013 mm->set_shift_pressed(ss->shift_pressed);
2014 mm->set_ctrl_pressed(ss->ctrl_pressed);
2015 mm->set_alt_pressed(ss->alt_pressed);
2016 mm->set_meta_pressed(ss->meta_pressed);
2017
2018 mm->set_window_id(ws->id);
2019
2020 mm->set_button_mask(pd.pressed_button_mask);
2021
2022 mm->set_position(pd.position * scale);
2023 mm->set_global_position(pd.position * scale);
2024
2025 Vector2 pos_delta = (pd.position - old_pd.position) * scale;
2026
2027 if (old_pd.relative_motion_time != pd.relative_motion_time) {
2028 uint32_t time_delta = pd.relative_motion_time - old_pd.relative_motion_time;
2029
2030 mm->set_relative(pd.relative_motion * scale);
2031 mm->set_velocity((Vector2)pos_delta / time_delta);
2032 } else {
2033 // The spec includes the possibility of having motion events without an
2034 // associated relative motion event. If that's the case, fallback to a
2035 // simple delta of the position. The captured mouse won't report the
2036 // relative speed anymore though.
2037 uint32_t time_delta = pd.motion_time - old_pd.motion_time;
2038
2039 mm->set_relative(pos_delta);
2040 mm->set_velocity((Vector2)pos_delta / time_delta);
2041 }
2042 mm->set_relative_screen_position(mm->get_relative());
2043 mm->set_screen_velocity(mm->get_velocity());
2044
2045 Ref<InputEventMessage> msg;
2046 msg.instantiate();
2047
2048 msg->event = mm;
2049
2050 wayland_thread->push_message(msg);
2051 }
2052
2053 if (pd.discrete_scroll_vector_120 - old_pd.discrete_scroll_vector_120 != Vector2i()) {
2054 // This is a discrete scroll (eg. from a scroll wheel), so we'll just emit
2055 // scroll wheel buttons.
2056 if (pd.scroll_vector.y != 0) {
2057 MouseButton button = pd.scroll_vector.y > 0 ? MouseButton::WHEEL_DOWN : MouseButton::WHEEL_UP;
2058 pd.pressed_button_mask.set_flag(mouse_button_to_mask(button));
2059 }
2060
2061 if (pd.scroll_vector.x != 0) {
2062 MouseButton button = pd.scroll_vector.x > 0 ? MouseButton::WHEEL_RIGHT : MouseButton::WHEEL_LEFT;
2063 pd.pressed_button_mask.set_flag(mouse_button_to_mask(button));
2064 }
2065 } else {
2066 if (pd.scroll_vector - old_pd.scroll_vector != Vector2()) {
2067 // This is a continuous scroll, so we'll emit a pan gesture.
2068 Ref<InputEventPanGesture> pg;
2069 pg.instantiate();
2070
2071 // Set all pressed modifiers.
2072 pg->set_shift_pressed(ss->shift_pressed);
2073 pg->set_ctrl_pressed(ss->ctrl_pressed);
2074 pg->set_alt_pressed(ss->alt_pressed);
2075 pg->set_meta_pressed(ss->meta_pressed);
2076
2077 pg->set_position(pd.position * scale);
2078
2079 pg->set_window_id(ws->id);
2080
2081 pg->set_delta(pd.scroll_vector);
2082
2083 Ref<InputEventMessage> msg;
2084 msg.instantiate();
2085
2086 msg->event = pg;
2087
2088 wayland_thread->push_message(msg);
2089 }
2090 }
2091
2092 if (old_pd.pressed_button_mask != pd.pressed_button_mask) {
2093 BitField<MouseButtonMask> pressed_mask_delta = old_pd.pressed_button_mask.get_different(pd.pressed_button_mask);
2094
2095 const MouseButton buttons_to_test[] = {
2096 MouseButton::LEFT,
2097 MouseButton::MIDDLE,
2098 MouseButton::RIGHT,
2099 MouseButton::WHEEL_UP,
2100 MouseButton::WHEEL_DOWN,
2101 MouseButton::WHEEL_LEFT,
2102 MouseButton::WHEEL_RIGHT,
2103 MouseButton::MB_XBUTTON1,
2104 MouseButton::MB_XBUTTON2,
2105 };
2106
2107 for (MouseButton test_button : buttons_to_test) {
2108 MouseButtonMask test_button_mask = mouse_button_to_mask(test_button);
2109 if (pressed_mask_delta.has_flag(test_button_mask)) {
2110 Ref<InputEventMouseButton> mb;
2111 mb.instantiate();
2112
2113 // Set all pressed modifiers.
2114 mb->set_shift_pressed(ss->shift_pressed);
2115 mb->set_ctrl_pressed(ss->ctrl_pressed);
2116 mb->set_alt_pressed(ss->alt_pressed);
2117 mb->set_meta_pressed(ss->meta_pressed);
2118
2119 mb->set_window_id(ws->id);
2120 mb->set_position(pd.position * scale);
2121 mb->set_global_position(pd.position * scale);
2122
2123 if (test_button == MouseButton::WHEEL_UP || test_button == MouseButton::WHEEL_DOWN) {
2124 // If this is a discrete scroll, specify how many "clicks" it did for this
2125 // pointer frame.
2126 mb->set_factor(Math::abs(pd.discrete_scroll_vector_120.y / (float)120));
2127 }
2128
2129 if (test_button == MouseButton::WHEEL_RIGHT || test_button == MouseButton::WHEEL_LEFT) {
2130 // If this is a discrete scroll, specify how many "clicks" it did for this
2131 // pointer frame.
2132 mb->set_factor(std::abs(pd.discrete_scroll_vector_120.x / (float)120));
2133 }
2134
2135 mb->set_button_mask(pd.pressed_button_mask);
2136
2137 mb->set_button_index(test_button);
2138 mb->set_pressed(pd.pressed_button_mask.has_flag(test_button_mask));
2139
2140 // We have to set the last position pressed here as we can't take for
2141 // granted what the individual events might have seen due to them not having
2142 // a guaranteed order.
2143 if (mb->is_pressed()) {
2144 pd.last_pressed_position = pd.position;
2145 }
2146
2147 if (old_pd.double_click_begun && mb->is_pressed() && pd.last_button_pressed == old_pd.last_button_pressed && (pd.button_time - old_pd.button_time) < 400 && Vector2(old_pd.last_pressed_position * scale).distance_to(Vector2(pd.last_pressed_position * scale)) < 5) {
2148 pd.double_click_begun = false;
2149 mb->set_double_click(true);
2150 }
2151
2152 Ref<InputEventMessage> msg;
2153 msg.instantiate();
2154
2155 msg->event = mb;
2156
2157 wayland_thread->push_message(msg);
2158
2159 // Send an event resetting immediately the wheel key.
2160 // Wayland specification defines axis_stop events as optional and says to
2161 // treat all axis events as unterminated. As such, we have to manually do
2162 // it ourselves.
2163 if (test_button == MouseButton::WHEEL_UP || test_button == MouseButton::WHEEL_DOWN || test_button == MouseButton::WHEEL_LEFT || test_button == MouseButton::WHEEL_RIGHT) {
2164 // FIXME: This is ugly, I can't find a clean way to clone an InputEvent.
2165 // This works for now, despite being horrible.
2166 Ref<InputEventMouseButton> wh_up;
2167 wh_up.instantiate();
2168
2169 wh_up->set_window_id(ws->id);
2170 wh_up->set_position(pd.position * scale);
2171 wh_up->set_global_position(pd.position * scale);
2172
2173 // We have to unset the button to avoid it getting stuck.
2174 pd.pressed_button_mask.clear_flag(test_button_mask);
2175 wh_up->set_button_mask(pd.pressed_button_mask);
2176
2177 wh_up->set_button_index(test_button);
2178 wh_up->set_pressed(false);
2179
2180 Ref<InputEventMessage> msg_up;
2181 msg_up.instantiate();
2182 msg_up->event = wh_up;
2183 wayland_thread->push_message(msg_up);
2184 }
2185 }
2186 }
2187 }
2188
2189 // Reset the scroll vectors as we already handled them.
2190 pd.scroll_vector = Vector2();
2191 pd.discrete_scroll_vector_120 = Vector2i();
2192
2193 // Update the data all getters read. Wayland's specification requires us to do
2194 // this, since all pointer actions are sent in individual events.
2195 old_pd = pd;
2196 }
2197
2198 void WaylandThread::_wl_pointer_on_axis_source(void *data, struct wl_pointer *wl_pointer, uint32_t axis_source) {
2199 SeatState *ss = (SeatState *)data;
2200 ERR_FAIL_NULL(ss);
2201
2202 ss->pointer_data_buffer.scroll_type = axis_source;
2203 }
2204
2205 void WaylandThread::_wl_pointer_on_axis_stop(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis) {
2206 }
2207
2208 // NOTE: This event is deprecated since version 8 and superseded by
2209 // `wl_pointer::axis_value120`. This thus converts the data to its
2210 // fraction-of-120 format.
2211 void WaylandThread::_wl_pointer_on_axis_discrete(void *data, struct wl_pointer *wl_pointer, uint32_t axis, int32_t discrete) {
2212 SeatState *ss = (SeatState *)data;
2213 ERR_FAIL_NULL(ss);
2214
2215 PointerData &pd = ss->pointer_data_buffer;
2216
2217 // NOTE: We can allow ourselves to not accumulate this data (and thus just
2218 // assign it) as the spec guarantees only one event per axis type.
2219
2220 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
2221 pd.discrete_scroll_vector_120.y = discrete * 120;
2222 }
2223
2224 if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
2225 pd.discrete_scroll_vector_120.x = discrete * 120;
2226 }
2227 }
2228
2229 // Supersedes `wl_pointer::axis_discrete` Since version 8.
2230 void WaylandThread::_wl_pointer_on_axis_value120(void *data, struct wl_pointer *wl_pointer, uint32_t axis, int32_t value120) {
2231 SeatState *ss = (SeatState *)data;
2232 ERR_FAIL_NULL(ss);
2233
2234 PointerData &pd = ss->pointer_data_buffer;
2235
2236 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
2237 pd.discrete_scroll_vector_120.y += value120;
2238 }
2239
2240 if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
2241 pd.discrete_scroll_vector_120.x += value120;
2242 }
2243 }
2244
2245 // TODO: Add support to this event.
2246 void WaylandThread::_wl_pointer_on_axis_relative_direction(void *data, struct wl_pointer *wl_pointer, uint32_t axis, uint32_t direction) {
2247 }
2248
2249 void WaylandThread::_wl_keyboard_on_keymap(void *data, struct wl_keyboard *wl_keyboard, uint32_t format, int32_t fd, uint32_t size) {
2250 ERR_FAIL_COND_MSG(format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, "Unsupported keymap format announced from the Wayland compositor.");
2251
2252 SeatState *ss = (SeatState *)data;
2253 ERR_FAIL_NULL(ss);
2254
2255 if (ss->keymap_buffer) {
2256 // We have already a mapped buffer, so we unmap it. There's no need to reset
2257 // its pointer or size, as we're gonna set them below.
2258 munmap((void *)ss->keymap_buffer, ss->keymap_buffer_size);
2259 ss->keymap_buffer = nullptr;
2260 }
2261
2262 ss->keymap_buffer = (const char *)mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
2263 ss->keymap_buffer_size = size;
2264
2265 xkb_keymap_unref(ss->xkb_keymap);
2266 ss->xkb_keymap = xkb_keymap_new_from_string(ss->xkb_context, ss->keymap_buffer,
2267 XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
2268
2269 xkb_state_unref(ss->xkb_state);
2270 ss->xkb_state = xkb_state_new(ss->xkb_keymap);
2271
2272 xkb_compose_table_unref(ss->xkb_compose_table);
2273 const char *locale = getenv("LC_ALL");
2274 if (!locale || !*locale) {
2275 locale = getenv("LC_CTYPE");
2276 }
2277 if (!locale || !*locale) {
2278 locale = getenv("LANG");
2279 }
2280 if (!locale || !*locale) {
2281 locale = "C";
2282 }
2283 ss->xkb_compose_table = xkb_compose_table_new_from_locale(ss->xkb_context, locale, XKB_COMPOSE_COMPILE_NO_FLAGS);
2284
2285 xkb_compose_state_unref(ss->xkb_compose_state);
2286 ss->xkb_compose_state = xkb_compose_state_new(ss->xkb_compose_table, XKB_COMPOSE_STATE_NO_FLAGS);
2287
2288 xkb_state_update_mask(ss->xkb_state, ss->mods_depressed, ss->mods_latched, ss->mods_locked, 0, 0, ss->current_layout_index);
2289 }
2290
2291 void WaylandThread::_wl_keyboard_on_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys) {
2292 WindowState *ws = wl_surface_get_window_state(surface);
2293 if (!ws) {
2294 return;
2295 }
2296
2297 SeatState *ss = (SeatState *)data;
2298 ERR_FAIL_NULL(ss);
2299
2300 WaylandThread *wayland_thread = ss->wayland_thread;
2301 ERR_FAIL_NULL(wayland_thread);
2302
2303 ss->focused_id = ws->id;
2304
2305 wayland_thread->_set_current_seat(ss->wl_seat);
2306
2307 Ref<WindowEventMessage> msg;
2308 msg.instantiate();
2309 msg->id = ws->id;
2310 msg->event = DisplayServer::WINDOW_EVENT_FOCUS_IN;
2311 wayland_thread->push_message(msg);
2312
2313 DEBUG_LOG_WAYLAND_THREAD(vformat("Keyboard focused window %d.", ws->id));
2314 }
2315
2316 void WaylandThread::_wl_keyboard_on_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface) {
2317 // NOTE: `surface` will probably be null when the surface is destroyed.
2318 // See: https://gitlab.freedesktop.org/wayland/wayland/-/issues/366
2319 // See: https://gitlab.freedesktop.org/wayland/wayland/-/issues/465
2320
2321 if (surface && !wl_proxy_is_godot((struct wl_proxy *)surface)) {
2322 return;
2323 }
2324
2325 SeatState *ss = (SeatState *)data;
2326 ERR_FAIL_NULL(ss);
2327
2328 WaylandThread *wayland_thread = ss->wayland_thread;
2329 ERR_FAIL_NULL(wayland_thread);
2330
2331 ss->repeating_keycode = XKB_KEYCODE_INVALID;
2332
2333 if (ss->focused_id == DisplayServer::INVALID_WINDOW_ID) {
2334 // We're probably on a decoration or some other third-party thing.
2335 return;
2336 }
2337
2338 WindowState *ws = wayland_thread->window_get_state(ss->focused_id);
2339 ERR_FAIL_NULL(ws);
2340
2341 ss->focused_id = DisplayServer::INVALID_WINDOW_ID;
2342
2343 Ref<WindowEventMessage> msg;
2344 msg.instantiate();
2345 msg->id = ws->id;
2346 msg->event = DisplayServer::WINDOW_EVENT_FOCUS_OUT;
2347 wayland_thread->push_message(msg);
2348
2349 ss->shift_pressed = false;
2350 ss->ctrl_pressed = false;
2351 ss->alt_pressed = false;
2352 ss->meta_pressed = false;
2353
2354 if (ss->xkb_state != nullptr) {
2355 xkb_state_update_mask(ss->xkb_state, 0, 0, 0, 0, 0, 0);
2356 }
2357
2358 DEBUG_LOG_WAYLAND_THREAD(vformat("Keyboard unfocused window %d.", ws->id));
2359 }
2360
2361 void WaylandThread::_wl_keyboard_on_key(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) {
2362 SeatState *ss = (SeatState *)data;
2363 ERR_FAIL_NULL(ss);
2364
2365 if (ss->focused_id == DisplayServer::INVALID_WINDOW_ID) {
2366 return;
2367 }
2368
2369 // We have to add 8 to the scancode to get an XKB-compatible keycode.
2370 xkb_keycode_t xkb_keycode = key + 8;
2371
2372 bool pressed = state & WL_KEYBOARD_KEY_STATE_PRESSED;
2373
2374 if (pressed) {
2375 if (xkb_keymap_key_repeats(ss->xkb_keymap, xkb_keycode)) {
2376 ss->last_repeat_start_msec = OS::get_singleton()->get_ticks_msec();
2377 ss->repeating_keycode = xkb_keycode;
2378 }
2379
2380 ss->last_key_pressed_serial = serial;
2381 } else if (ss->repeating_keycode == xkb_keycode) {
2382 ss->repeating_keycode = XKB_KEYCODE_INVALID;
2383 }
2384
2385 _seat_state_handle_xkb_keycode(ss, xkb_keycode, pressed);
2386 }
2387
2388 void WaylandThread::_wl_keyboard_on_modifiers(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {
2389 SeatState *ss = (SeatState *)data;
2390 ERR_FAIL_NULL(ss);
2391
2392 ss->mods_depressed = mods_depressed;
2393 ss->mods_latched = mods_latched;
2394 ss->mods_locked = mods_locked;
2395 ss->current_layout_index = group;
2396
2397 if (ss->xkb_state == nullptr) {
2398 return;
2399 }
2400
2401 xkb_state_update_mask(ss->xkb_state, mods_depressed, mods_latched, mods_locked, 0, 0, group);
2402
2403 ss->shift_pressed = xkb_state_mod_name_is_active(ss->xkb_state, XKB_MOD_NAME_SHIFT, XKB_STATE_MODS_EFFECTIVE);
2404 ss->ctrl_pressed = xkb_state_mod_name_is_active(ss->xkb_state, XKB_MOD_NAME_CTRL, XKB_STATE_MODS_EFFECTIVE);
2405 ss->alt_pressed = xkb_state_mod_name_is_active(ss->xkb_state, XKB_MOD_NAME_ALT, XKB_STATE_MODS_EFFECTIVE);
2406 ss->meta_pressed = xkb_state_mod_name_is_active(ss->xkb_state, XKB_MOD_NAME_LOGO, XKB_STATE_MODS_EFFECTIVE);
2407 }
2408
2409 void WaylandThread::_wl_keyboard_on_repeat_info(void *data, struct wl_keyboard *wl_keyboard, int32_t rate, int32_t delay) {
2410 SeatState *ss = (SeatState *)data;
2411 ERR_FAIL_NULL(ss);
2412
2413 ss->repeat_key_delay_msec = rate ? 1000 / rate : 0;
2414 ss->repeat_start_delay_msec = delay;
2415 }
2416
2417 // NOTE: Don't forget to `memfree` the offer's state.
2418 void WaylandThread::_wl_data_device_on_data_offer(void *data, struct wl_data_device *wl_data_device, struct wl_data_offer *id) {
2419 wl_proxy_tag_godot((struct wl_proxy *)id);
2420 wl_data_offer_add_listener(id, &wl_data_offer_listener, memnew(OfferState));
2421 }
2422
2423 void WaylandThread::_wl_data_device_on_enter(void *data, struct wl_data_device *wl_data_device, uint32_t serial, struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y, struct wl_data_offer *id) {
2424 WindowState *ws = wl_surface_get_window_state(surface);
2425 if (!ws) {
2426 return;
2427 }
2428
2429 SeatState *ss = (SeatState *)data;
2430 ERR_FAIL_NULL(ss);
2431
2432 ss->dnd_id = ws->id;
2433
2434 ss->dnd_enter_serial = serial;
2435 ss->wl_data_offer_dnd = id;
2436
2437 // Godot only supports DnD file copying for now.
2438 wl_data_offer_accept(id, serial, "text/uri-list");
2439 wl_data_offer_set_actions(id, WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY, WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY);
2440 }
2441
2442 void WaylandThread::_wl_data_device_on_leave(void *data, struct wl_data_device *wl_data_device) {
2443 SeatState *ss = (SeatState *)data;
2444 ERR_FAIL_NULL(ss);
2445
2446 if (ss->wl_data_offer_dnd) {
2447 memdelete(wl_data_offer_get_offer_state(ss->wl_data_offer_dnd));
2448 wl_data_offer_destroy(ss->wl_data_offer_dnd);
2449 ss->wl_data_offer_dnd = nullptr;
2450 ss->dnd_id = DisplayServer::INVALID_WINDOW_ID;
2451 }
2452 }
2453
2454 void WaylandThread::_wl_data_device_on_motion(void *data, struct wl_data_device *wl_data_device, uint32_t time, wl_fixed_t x, wl_fixed_t y) {
2455 }
2456
2457 void WaylandThread::_wl_data_device_on_drop(void *data, struct wl_data_device *wl_data_device) {
2458 SeatState *ss = (SeatState *)data;
2459 ERR_FAIL_NULL(ss);
2460
2461 WaylandThread *wayland_thread = ss->wayland_thread;
2462 ERR_FAIL_NULL(wayland_thread);
2463
2464 OfferState *os = wl_data_offer_get_offer_state(ss->wl_data_offer_dnd);
2465 ERR_FAIL_NULL(os);
2466
2467 if (os) {
2468 Ref<DropFilesEventMessage> msg;
2469 msg.instantiate();
2470 msg->id = ss->dnd_id;
2471
2472 Vector<uint8_t> list_data = _wl_data_offer_read(wayland_thread->wl_display, "text/uri-list", ss->wl_data_offer_dnd);
2473
2474 msg->files = String::utf8((const char *)list_data.ptr(), list_data.size()).split("\r\n", false);
2475 for (int i = 0; i < msg->files.size(); i++) {
2476 msg->files.write[i] = msg->files[i].replace("file://", "").uri_file_decode();
2477 }
2478
2479 wayland_thread->push_message(msg);
2480
2481 wl_data_offer_finish(ss->wl_data_offer_dnd);
2482 }
2483
2484 memdelete(wl_data_offer_get_offer_state(ss->wl_data_offer_dnd));
2485 wl_data_offer_destroy(ss->wl_data_offer_dnd);
2486 ss->wl_data_offer_dnd = nullptr;
2487 ss->dnd_id = DisplayServer::INVALID_WINDOW_ID;
2488 }
2489
2490 void WaylandThread::_wl_data_device_on_selection(void *data, struct wl_data_device *wl_data_device, struct wl_data_offer *id) {
2491 SeatState *ss = (SeatState *)data;
2492 ERR_FAIL_NULL(ss);
2493
2494 if (ss->wl_data_offer_selection) {
2495 memdelete(wl_data_offer_get_offer_state(ss->wl_data_offer_selection));
2496 wl_data_offer_destroy(ss->wl_data_offer_selection);
2497 }
2498
2499 ss->wl_data_offer_selection = id;
2500 }
2501
2502 void WaylandThread::_wl_data_offer_on_offer(void *data, struct wl_data_offer *wl_data_offer, const char *mime_type) {
2503 OfferState *os = (OfferState *)data;
2504 ERR_FAIL_NULL(os);
2505
2506 if (os) {
2507 os->mime_types.insert(String::utf8(mime_type));
2508 }
2509 }
2510
2511 void WaylandThread::_wl_data_offer_on_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions) {
2512 }
2513
2514 void WaylandThread::_wl_data_offer_on_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action) {
2515 }
2516
2517 void WaylandThread::_wl_data_source_on_target(void *data, struct wl_data_source *wl_data_source, const char *mime_type) {
2518 }
2519
2520 void WaylandThread::_wl_data_source_on_send(void *data, struct wl_data_source *wl_data_source, const char *mime_type, int32_t fd) {
2521 SeatState *ss = (SeatState *)data;
2522 if (ss == nullptr) {
2523 ERR_PRINT("Seat state not set.");
2524 close(fd);
2525 return;
2526 }
2527
2528 if (wl_data_source == ss->wl_data_source_selection) {
2529 DEBUG_LOG_WAYLAND_THREAD("Clipboard: requested selection.");
2530 _clipboard_send(ss->selection_data, mime_type, fd);
2531 }
2532
2533 close(fd);
2534 }
2535
2536 void WaylandThread::_wl_data_source_on_cancelled(void *data, struct wl_data_source *wl_data_source) {
2537 SeatState *ss = (SeatState *)data;
2538 ERR_FAIL_NULL(ss);
2539
2540 wl_data_source_destroy(wl_data_source);
2541
2542 if (wl_data_source == ss->wl_data_source_selection) {
2543 ss->wl_data_source_selection = nullptr;
2544 ss->selection_data.clear();
2545
2546 DEBUG_LOG_WAYLAND_THREAD("Clipboard: selection set by another program.");
2547 return;
2548 }
2549 }
2550
2551 void WaylandThread::_wl_data_source_on_dnd_drop_performed(void *data, struct wl_data_source *wl_data_source) {
2552 }
2553
2554 void WaylandThread::_wl_data_source_on_dnd_finished(void *data, struct wl_data_source *wl_data_source) {
2555 }
2556
2557 void WaylandThread::_wl_data_source_on_action(void *data, struct wl_data_source *wl_data_source, uint32_t dnd_action) {
2558 }
2559
2560 void WaylandThread::_wp_fractional_scale_on_preferred_scale(void *data, struct wp_fractional_scale_v1 *wp_fractional_scale_v1, uint32_t scale) {
2561 WindowState *ws = (WindowState *)data;
2562 ERR_FAIL_NULL(ws);
2563
2564 ws->preferred_fractional_scale = (double)scale / 120;
2565
2566 window_state_update_size(ws, ws->rect.size.width, ws->rect.size.height);
2567 }
2568
2569 void WaylandThread::_wp_relative_pointer_on_relative_motion(void *data, struct zwp_relative_pointer_v1 *wp_relative_pointer, uint32_t uptime_hi, uint32_t uptime_lo, wl_fixed_t dx, wl_fixed_t dy, wl_fixed_t dx_unaccel, wl_fixed_t dy_unaccel) {
2570 SeatState *ss = (SeatState *)data;
2571 ERR_FAIL_NULL(ss);
2572
2573 PointerData &pd = ss->pointer_data_buffer;
2574
2575 pd.relative_motion.x = wl_fixed_to_double(dx);
2576 pd.relative_motion.y = wl_fixed_to_double(dy);
2577
2578 pd.relative_motion_time = uptime_lo;
2579 }
2580
2581 void WaylandThread::_wp_pointer_gesture_pinch_on_begin(void *data, struct zwp_pointer_gesture_pinch_v1 *wp_pointer_gesture_pinch_v1, uint32_t serial, uint32_t time, struct wl_surface *surface, uint32_t fingers) {
2582 SeatState *ss = (SeatState *)data;
2583 ERR_FAIL_NULL(ss);
2584
2585 if (fingers == 2) {
2586 ss->old_pinch_scale = wl_fixed_from_int(1);
2587 ss->active_gesture = Gesture::MAGNIFY;
2588 }
2589 }
2590
2591 void WaylandThread::_wp_pointer_gesture_pinch_on_update(void *data, struct zwp_pointer_gesture_pinch_v1 *wp_pointer_gesture_pinch_v1, uint32_t time, wl_fixed_t dx, wl_fixed_t dy, wl_fixed_t scale, wl_fixed_t rotation) {
2592 SeatState *ss = (SeatState *)data;
2593 ERR_FAIL_NULL(ss);
2594
2595 // NOTE: From what I can tell, this and all other pointer gestures are separate
2596 // from the "frame" mechanism of regular pointers. Thus, let's just assume we
2597 // can read from the "committed" state.
2598 const PointerData &pd = ss->pointer_data;
2599
2600 WaylandThread *wayland_thread = ss->wayland_thread;
2601 ERR_FAIL_NULL(wayland_thread);
2602
2603 WindowState *ws = wayland_thread->window_get_state(pd.pointed_id);
2604 ERR_FAIL_NULL(ws);
2605
2606 double win_scale = window_state_get_scale_factor(ws);
2607
2608 if (ss->active_gesture == Gesture::MAGNIFY) {
2609 Ref<InputEventMagnifyGesture> mg;
2610 mg.instantiate();
2611
2612 mg->set_window_id(pd.pointed_id);
2613
2614 if (ws) {
2615 mg->set_window_id(ws->id);
2616 }
2617
2618 // Set all pressed modifiers.
2619 mg->set_shift_pressed(ss->shift_pressed);
2620 mg->set_ctrl_pressed(ss->ctrl_pressed);
2621 mg->set_alt_pressed(ss->alt_pressed);
2622 mg->set_meta_pressed(ss->meta_pressed);
2623
2624 mg->set_position(pd.position * win_scale);
2625
2626 wl_fixed_t scale_delta = scale - ss->old_pinch_scale;
2627 mg->set_factor(1 + wl_fixed_to_double(scale_delta));
2628
2629 Ref<InputEventMessage> magnify_msg;
2630 magnify_msg.instantiate();
2631 magnify_msg->event = mg;
2632
2633 // Since Wayland allows only one gesture at a time and godot instead expects
2634 // both of them, we'll have to create two separate input events: one for
2635 // magnification and one for panning.
2636
2637 Ref<InputEventPanGesture> pg;
2638 pg.instantiate();
2639
2640 // Set all pressed modifiers.
2641 pg->set_shift_pressed(ss->shift_pressed);
2642 pg->set_ctrl_pressed(ss->ctrl_pressed);
2643 pg->set_alt_pressed(ss->alt_pressed);
2644 pg->set_meta_pressed(ss->meta_pressed);
2645
2646 pg->set_position(pd.position * win_scale);
2647 pg->set_delta(Vector2(wl_fixed_to_double(dx), wl_fixed_to_double(dy)));
2648
2649 Ref<InputEventMessage> pan_msg;
2650 pan_msg.instantiate();
2651 pan_msg->event = pg;
2652
2653 wayland_thread->push_message(magnify_msg);
2654 wayland_thread->push_message(pan_msg);
2655
2656 ss->old_pinch_scale = scale;
2657 }
2658 }
2659
2660 void WaylandThread::_wp_pointer_gesture_pinch_on_end(void *data, struct zwp_pointer_gesture_pinch_v1 *wp_pointer_gesture_pinch_v1, uint32_t serial, uint32_t time, int32_t cancelled) {
2661 SeatState *ss = (SeatState *)data;
2662 ERR_FAIL_NULL(ss);
2663
2664 ss->active_gesture = Gesture::NONE;
2665 }
2666
2667 // NOTE: Don't forget to `memfree` the offer's state.
2668 void WaylandThread::_wp_primary_selection_device_on_data_offer(void *data, struct zwp_primary_selection_device_v1 *wp_primary_selection_device_v1, struct zwp_primary_selection_offer_v1 *offer) {
2669 wl_proxy_tag_godot((struct wl_proxy *)offer);
2670 zwp_primary_selection_offer_v1_add_listener(offer, &wp_primary_selection_offer_listener, memnew(OfferState));
2671 }
2672
2673 void WaylandThread::_wp_primary_selection_device_on_selection(void *data, struct zwp_primary_selection_device_v1 *wp_primary_selection_device_v1, struct zwp_primary_selection_offer_v1 *id) {
2674 SeatState *ss = (SeatState *)data;
2675 ERR_FAIL_NULL(ss);
2676
2677 if (ss->wp_primary_selection_offer) {
2678 memfree(wp_primary_selection_offer_get_offer_state(ss->wp_primary_selection_offer));
2679 zwp_primary_selection_offer_v1_destroy(ss->wp_primary_selection_offer);
2680 }
2681
2682 ss->wp_primary_selection_offer = id;
2683 }
2684
2685 void WaylandThread::_wp_primary_selection_offer_on_offer(void *data, struct zwp_primary_selection_offer_v1 *wp_primary_selection_offer_v1, const char *mime_type) {
2686 OfferState *os = (OfferState *)data;
2687 ERR_FAIL_NULL(os);
2688
2689 if (os) {
2690 os->mime_types.insert(String::utf8(mime_type));
2691 }
2692 }
2693
2694 void WaylandThread::_wp_primary_selection_source_on_send(void *data, struct zwp_primary_selection_source_v1 *wp_primary_selection_source_v1, const char *mime_type, int32_t fd) {
2695 SeatState *ss = (SeatState *)data;
2696 if (ss == nullptr) {
2697 ERR_PRINT("Seat state not set.");
2698 close(fd);
2699 return;
2700 }
2701
2702 if (wp_primary_selection_source_v1 == ss->wp_primary_selection_source) {
2703 DEBUG_LOG_WAYLAND_THREAD("Clipboard: requested primary selection.");
2704 _clipboard_send(ss->primary_data, mime_type, fd);
2705 }
2706
2707 close(fd);
2708 }
2709
2710 void WaylandThread::_wp_primary_selection_source_on_cancelled(void *data, struct zwp_primary_selection_source_v1 *wp_primary_selection_source_v1) {
2711 SeatState *ss = (SeatState *)data;
2712 ERR_FAIL_NULL(ss);
2713
2714 if (wp_primary_selection_source_v1 == ss->wp_primary_selection_source) {
2715 zwp_primary_selection_source_v1_destroy(ss->wp_primary_selection_source);
2716 ss->wp_primary_selection_source = nullptr;
2717
2718 ss->primary_data.clear();
2719
2720 DEBUG_LOG_WAYLAND_THREAD("Clipboard: primary selection set by another program.");
2721 return;
2722 }
2723 }
2724
2725 void WaylandThread::_wp_tablet_seat_on_tablet_added(void *data, struct zwp_tablet_seat_v2 *wp_tablet_seat_v2, struct zwp_tablet_v2 *id) {
2726 }
2727
2728 void WaylandThread::_wp_tablet_seat_on_tool_added(void *data, struct zwp_tablet_seat_v2 *wp_tablet_seat_v2, struct zwp_tablet_tool_v2 *id) {
2729 SeatState *ss = (SeatState *)data;
2730 ERR_FAIL_NULL(ss);
2731
2732 TabletToolState *state = memnew(TabletToolState);
2733 state->wl_seat = ss->wl_seat;
2734
2735 wl_proxy_tag_godot((struct wl_proxy *)id);
2736 zwp_tablet_tool_v2_add_listener(id, &wp_tablet_tool_listener, state);
2737 ss->tablet_tools.push_back(id);
2738 }
2739
2740 void WaylandThread::_wp_tablet_seat_on_pad_added(void *data, struct zwp_tablet_seat_v2 *wp_tablet_seat_v2, struct zwp_tablet_pad_v2 *id) {
2741 }
2742
2743 void WaylandThread::_wp_tablet_tool_on_type(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t tool_type) {
2744 TabletToolState *state = wp_tablet_tool_get_state(wp_tablet_tool_v2);
2745
2746 if (state && tool_type == ZWP_TABLET_TOOL_V2_TYPE_ERASER) {
2747 state->is_eraser = true;
2748 }
2749 }
2750
2751 void WaylandThread::_wp_tablet_tool_on_hardware_serial(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t hardware_serial_hi, uint32_t hardware_serial_lo) {
2752 }
2753
2754 void WaylandThread::_wp_tablet_tool_on_hardware_id_wacom(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t hardware_id_hi, uint32_t hardware_id_lo) {
2755 }
2756
2757 void WaylandThread::_wp_tablet_tool_on_capability(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t capability) {
2758 }
2759
2760 void WaylandThread::_wp_tablet_tool_on_done(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2) {
2761 }
2762
2763 void WaylandThread::_wp_tablet_tool_on_removed(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2) {
2764 TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
2765 if (!ts) {
2766 return;
2767 }
2768
2769 SeatState *ss = wl_seat_get_seat_state(ts->wl_seat);
2770 if (!ss) {
2771 return;
2772 }
2773
2774 List<struct zwp_tablet_tool_v2 *>::Element *E = ss->tablet_tools.find(wp_tablet_tool_v2);
2775
2776 if (E && E->get()) {
2777 struct zwp_tablet_tool_v2 *tool = E->get();
2778 TabletToolState *state = wp_tablet_tool_get_state(tool);
2779 if (state) {
2780 memdelete(state);
2781 }
2782
2783 zwp_tablet_tool_v2_destroy(tool);
2784 ss->tablet_tools.erase(E);
2785 }
2786 }
2787
2788 void WaylandThread::_wp_tablet_tool_on_proximity_in(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t serial, struct zwp_tablet_v2 *tablet, struct wl_surface *surface) {
2789 // NOTE: Works pretty much like wl_pointer::enter.
2790
2791 WindowState *ws = wl_surface_get_window_state(surface);
2792 if (!ws) {
2793 return;
2794 }
2795
2796 TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
2797 ERR_FAIL_NULL(ts);
2798
2799 ts->data_pending.proximity_serial = serial;
2800 ts->data_pending.proximal_id = ws->id;
2801 ts->data_pending.last_proximal_id = ws->id;
2802
2803 DEBUG_LOG_WAYLAND_THREAD(vformat("Tablet tool entered window %d.", ts->data_pending.proximal_id));
2804 }
2805
2806 void WaylandThread::_wp_tablet_tool_on_proximity_out(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2) {
2807 // NOTE: Works pretty much like wl_pointer::leave.
2808
2809 TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
2810 ERR_FAIL_NULL(ts);
2811
2812 if (ts->data_pending.proximal_id == DisplayServer::INVALID_WINDOW_ID) {
2813 // We're probably on a decoration or some other third-party thing.
2814 return;
2815 }
2816
2817 DisplayServer::WindowID id = ts->data_pending.proximal_id;
2818
2819 ts->data_pending.proximal_id = DisplayServer::INVALID_WINDOW_ID;
2820 ts->data_pending.pressed_button_mask.clear();
2821
2822 DEBUG_LOG_WAYLAND_THREAD(vformat("Tablet tool left window %d.", id));
2823 }
2824
2825 void WaylandThread::_wp_tablet_tool_on_down(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t serial) {
2826 // NOTE: Works pretty much like wl_pointer::button but only for a pressed left
2827 // button.
2828
2829 TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
2830 ERR_FAIL_NULL(ts);
2831
2832 TabletToolData &td = ts->data_pending;
2833
2834 td.pressed_button_mask.set_flag(mouse_button_to_mask(MouseButton::LEFT));
2835 td.last_button_pressed = MouseButton::LEFT;
2836 td.double_click_begun = true;
2837
2838 // The protocol doesn't cover this, but we can use this funky hack to make
2839 // double clicking work.
2840 td.button_time = OS::get_singleton()->get_ticks_msec();
2841 }
2842
2843 void WaylandThread::_wp_tablet_tool_on_up(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2) {
2844 // NOTE: Works pretty much like wl_pointer::button but only for a released left
2845 // button.
2846
2847 TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
2848 ERR_FAIL_NULL(ts);
2849
2850 TabletToolData &td = ts->data_pending;
2851
2852 td.pressed_button_mask.clear_flag(mouse_button_to_mask(MouseButton::LEFT));
2853
2854 // The protocol doesn't cover this, but we can use this funky hack to make
2855 // double clicking work.
2856 td.button_time = OS::get_singleton()->get_ticks_msec();
2857 }
2858
2859 void WaylandThread::_wp_tablet_tool_on_motion(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, wl_fixed_t x, wl_fixed_t y) {
2860 // NOTE: Works pretty much like wl_pointer::motion.
2861
2862 TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
2863 ERR_FAIL_NULL(ts);
2864
2865 TabletToolData &td = ts->data_pending;
2866
2867 td.position.x = wl_fixed_to_double(x);
2868 td.position.y = wl_fixed_to_double(y);
2869 }
2870
2871 void WaylandThread::_wp_tablet_tool_on_pressure(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t pressure) {
2872 TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
2873 ERR_FAIL_NULL(ts);
2874
2875 ts->data_pending.pressure = pressure;
2876 }
2877
2878 void WaylandThread::_wp_tablet_tool_on_distance(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t distance) {
2879 // Unsupported
2880 }
2881
2882 void WaylandThread::_wp_tablet_tool_on_tilt(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, wl_fixed_t tilt_x, wl_fixed_t tilt_y) {
2883 TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
2884 ERR_FAIL_NULL(ts);
2885
2886 TabletToolData &td = ts->data_pending;
2887
2888 td.tilt.x = wl_fixed_to_double(tilt_x);
2889 td.tilt.y = wl_fixed_to_double(tilt_y);
2890 }
2891
2892 void WaylandThread::_wp_tablet_tool_on_rotation(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, wl_fixed_t degrees) {
2893 // Unsupported.
2894 }
2895
2896 void WaylandThread::_wp_tablet_tool_on_slider(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, int32_t position) {
2897 // Unsupported.
2898 }
2899
2900 void WaylandThread::_wp_tablet_tool_on_wheel(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, wl_fixed_t degrees, int32_t clicks) {
2901 // TODO
2902 }
2903
2904 void WaylandThread::_wp_tablet_tool_on_button(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t serial, uint32_t button, uint32_t state) {
2905 // NOTE: Works pretty much like wl_pointer::button.
2906
2907 TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
2908 ERR_FAIL_NULL(ts);
2909
2910 TabletToolData &td = ts->data_pending;
2911
2912 MouseButton mouse_button = MouseButton::NONE;
2913
2914 if (button == BTN_STYLUS) {
2915 mouse_button = MouseButton::LEFT;
2916 }
2917
2918 if (button == BTN_STYLUS2) {
2919 mouse_button = MouseButton::RIGHT;
2920 }
2921
2922 if (mouse_button != MouseButton::NONE) {
2923 MouseButtonMask mask = mouse_button_to_mask(mouse_button);
2924
2925 if (state == ZWP_TABLET_TOOL_V2_BUTTON_STATE_PRESSED) {
2926 td.pressed_button_mask.set_flag(mask);
2927 td.last_button_pressed = mouse_button;
2928 td.double_click_begun = true;
2929 } else {
2930 td.pressed_button_mask.clear_flag(mask);
2931 }
2932
2933 // The protocol doesn't cover this, but we can use this funky hack to make
2934 // double clicking work.
2935 td.button_time = OS::get_singleton()->get_ticks_msec();
2936 }
2937 }
2938
2939 void WaylandThread::_wp_tablet_tool_on_frame(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t time) {
2940 // NOTE: Works pretty much like wl_pointer::frame.
2941
2942 TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
2943 ERR_FAIL_NULL(ts);
2944
2945 SeatState *ss = wl_seat_get_seat_state(ts->wl_seat);
2946 ERR_FAIL_NULL(ss);
2947
2948 WaylandThread *wayland_thread = ss->wayland_thread;
2949 ERR_FAIL_NULL(wayland_thread);
2950
2951 TabletToolData &old_td = ts->data;
2952 TabletToolData &td = ts->data_pending;
2953
2954 if (td.proximal_id != old_td.proximal_id) {
2955 if (old_td.proximal_id != DisplayServer::INVALID_WINDOW_ID) {
2956 Ref<WindowEventMessage> msg;
2957 msg.instantiate();
2958 msg->id = old_td.proximal_id;
2959 msg->event = DisplayServer::WINDOW_EVENT_MOUSE_EXIT;
2960
2961 wayland_thread->push_message(msg);
2962 }
2963
2964 if (td.proximal_id != DisplayServer::INVALID_WINDOW_ID) {
2965 Ref<WindowEventMessage> msg;
2966 msg.instantiate();
2967 msg->id = td.proximal_id;
2968 msg->event = DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
2969
2970 wayland_thread->push_message(msg);
2971 }
2972 }
2973
2974 if (td.proximal_id == DisplayServer::INVALID_WINDOW_ID) {
2975 // We're probably on a decoration or some other third-party thing. Let's
2976 // "commit" the data and call it a day.
2977 old_td = td;
2978 return;
2979 }
2980
2981 WindowState *ws = wayland_thread->window_get_state(td.proximal_id);
2982 ERR_FAIL_NULL(ws);
2983
2984 double scale = window_state_get_scale_factor(ws);
2985 if (old_td.position != td.position || old_td.tilt != td.tilt || old_td.pressure != td.pressure) {
2986 td.motion_time = time;
2987
2988 Ref<InputEventMouseMotion> mm;
2989 mm.instantiate();
2990
2991 mm->set_window_id(td.proximal_id);
2992
2993 // Set all pressed modifiers.
2994 mm->set_shift_pressed(ss->shift_pressed);
2995 mm->set_ctrl_pressed(ss->ctrl_pressed);
2996 mm->set_alt_pressed(ss->alt_pressed);
2997 mm->set_meta_pressed(ss->meta_pressed);
2998
2999 mm->set_button_mask(td.pressed_button_mask);
3000
3001 mm->set_global_position(td.position * scale);
3002 mm->set_position(td.position * scale);
3003
3004 // NOTE: The Godot API expects normalized values and we store them raw,
3005 // straight from the compositor, so we have to normalize them here.
3006
3007 // According to the tablet proto spec, tilt is expressed in degrees relative
3008 // to the Z axis of the tablet, so it shouldn't go over 90 degrees either way,
3009 // I think. We'll clamp it just in case.
3010 td.tilt = td.tilt.clampf(-90, 90);
3011
3012 mm->set_tilt(td.tilt / 90);
3013
3014 // The tablet proto spec explicitly says that pressure is defined as a value
3015 // between 0 to 65535.
3016 mm->set_pressure(td.pressure / (float)65535);
3017
3018 mm->set_pen_inverted(ts->is_eraser);
3019
3020 Vector2 pos_delta = (td.position - old_td.position) * scale;
3021
3022 mm->set_relative(pos_delta);
3023 mm->set_relative_screen_position(pos_delta);
3024
3025 uint32_t time_delta = td.motion_time - old_td.motion_time;
3026 mm->set_velocity((Vector2)pos_delta / time_delta);
3027
3028 Ref<InputEventMessage> inputev_msg;
3029 inputev_msg.instantiate();
3030
3031 inputev_msg->event = mm;
3032
3033 wayland_thread->push_message(inputev_msg);
3034 }
3035
3036 if (old_td.pressed_button_mask != td.pressed_button_mask) {
3037 td.button_time = time;
3038
3039 BitField<MouseButtonMask> pressed_mask_delta = old_td.pressed_button_mask.get_different(td.pressed_button_mask);
3040
3041 for (MouseButton test_button : { MouseButton::LEFT, MouseButton::RIGHT }) {
3042 MouseButtonMask test_button_mask = mouse_button_to_mask(test_button);
3043
3044 if (pressed_mask_delta.has_flag(test_button_mask)) {
3045 Ref<InputEventMouseButton> mb;
3046 mb.instantiate();
3047
3048 // Set all pressed modifiers.
3049 mb->set_shift_pressed(ss->shift_pressed);
3050 mb->set_ctrl_pressed(ss->ctrl_pressed);
3051 mb->set_alt_pressed(ss->alt_pressed);
3052 mb->set_meta_pressed(ss->meta_pressed);
3053
3054 mb->set_window_id(td.proximal_id);
3055 mb->set_position(td.position * scale);
3056 mb->set_global_position(td.position * scale);
3057
3058 mb->set_button_mask(td.pressed_button_mask);
3059 mb->set_button_index(test_button);
3060 mb->set_pressed(td.pressed_button_mask.has_flag(test_button_mask));
3061
3062 // We have to set the last position pressed here as we can't take for
3063 // granted what the individual events might have seen due to them not having
3064 // a garaunteed order.
3065 if (mb->is_pressed()) {
3066 td.last_pressed_position = td.position;
3067 }
3068
3069 if (old_td.double_click_begun && mb->is_pressed() && td.last_button_pressed == old_td.last_button_pressed && (td.button_time - old_td.button_time) < 400 && Vector2(td.last_pressed_position * scale).distance_to(Vector2(old_td.last_pressed_position * scale)) < 5) {
3070 td.double_click_begun = false;
3071 mb->set_double_click(true);
3072 }
3073
3074 Ref<InputEventMessage> msg;
3075 msg.instantiate();
3076
3077 msg->event = mb;
3078
3079 wayland_thread->push_message(msg);
3080 }
3081 }
3082 }
3083
3084 old_td = td;
3085 }
3086
3087 void WaylandThread::_wp_text_input_on_enter(void *data, struct zwp_text_input_v3 *wp_text_input_v3, struct wl_surface *surface) {
3088 SeatState *ss = (SeatState *)data;
3089 if (!ss) {
3090 return;
3091 }
3092
3093 WindowState *ws = wl_surface_get_window_state(surface);
3094 if (!ws) {
3095 return;
3096 }
3097
3098 ss->ime_window_id = ws->id;
3099 ss->ime_enabled = true;
3100 }
3101
3102 // NOTE: From now on, we must ignore all further events until an enter event.
3103 void WaylandThread::_wp_text_input_on_leave(void *data, struct zwp_text_input_v3 *wp_text_input_v3, struct wl_surface *surface) {
3104 SeatState *ss = (SeatState *)data;
3105 if (!ss) {
3106 return;
3107 }
3108
3109 if (ss->ime_window_id == DisplayServer::INVALID_WINDOW_ID) {
3110 return;
3111 }
3112
3113 Ref<IMEUpdateEventMessage> msg;
3114 msg.instantiate();
3115 msg->id = ss->ime_window_id;
3116 msg->text = String();
3117 msg->selection = Vector2i();
3118 ss->wayland_thread->push_message(msg);
3119
3120 ss->ime_window_id = DisplayServer::INVALID_WINDOW_ID;
3121 ss->ime_enabled = false;
3122 ss->ime_active = false;
3123 ss->ime_text = String();
3124 ss->ime_text_commit = String();
3125 ss->ime_cursor = Vector2i();
3126 }
3127
3128 void WaylandThread::_wp_text_input_on_preedit_string(void *data, struct zwp_text_input_v3 *wp_text_input_v3, const char *text, int32_t cursor_begin, int32_t cursor_end) {
3129 SeatState *ss = (SeatState *)data;
3130 if (!ss) {
3131 return;
3132 }
3133
3134 if (ss->ime_window_id == DisplayServer::INVALID_WINDOW_ID) {
3135 return;
3136 }
3137
3138 ss->ime_text = String::utf8(text);
3139
3140 // Convert cursor positions from UTF-8 to UTF-32 offset.
3141 int32_t cursor_begin_utf32 = 0;
3142 int32_t cursor_end_utf32 = 0;
3143 for (int i = 0; i < ss->ime_text.length(); i++) {
3144 uint32_t c = ss->ime_text[i];
3145 if (c <= 0x7f) { // 7 bits.
3146 cursor_begin -= 1;
3147 cursor_end -= 1;
3148 } else if (c <= 0x7ff) { // 11 bits
3149 cursor_begin -= 2;
3150 cursor_end -= 2;
3151 } else if (c <= 0xffff) { // 16 bits
3152 cursor_begin -= 3;
3153 cursor_end -= 3;
3154 } else if (c <= 0x001fffff) { // 21 bits
3155 cursor_begin -= 4;
3156 cursor_end -= 4;
3157 } else if (c <= 0x03ffffff) { // 26 bits
3158 cursor_begin -= 5;
3159 cursor_end -= 5;
3160 } else if (c <= 0x7fffffff) { // 31 bits
3161 cursor_begin -= 6;
3162 cursor_end -= 6;
3163 } else {
3164 cursor_begin -= 1;
3165 cursor_end -= 1;
3166 }
3167 if (cursor_begin == 0) {
3168 cursor_begin_utf32 = i + 1;
3169 }
3170 if (cursor_end == 0) {
3171 cursor_end_utf32 = i + 1;
3172 }
3173 if (cursor_begin <= 0 && cursor_end <= 0) {
3174 break;
3175 }
3176 }
3177 ss->ime_cursor = Vector2i(cursor_begin_utf32, cursor_end_utf32 - cursor_begin_utf32);
3178 }
3179
3180 void WaylandThread::_wp_text_input_on_commit_string(void *data, struct zwp_text_input_v3 *wp_text_input_v3, const char *text) {
3181 SeatState *ss = (SeatState *)data;
3182 if (!ss) {
3183 return;
3184 }
3185
3186 if (ss->ime_window_id == DisplayServer::INVALID_WINDOW_ID) {
3187 return;
3188 }
3189
3190 ss->ime_text_commit = String::utf8(text);
3191 }
3192
3193 void WaylandThread::_wp_text_input_on_delete_surrounding_text(void *data, struct zwp_text_input_v3 *wp_text_input_v3, uint32_t before_length, uint32_t after_length) {
3194 // Not implemented.
3195 }
3196
3197 void WaylandThread::_wp_text_input_on_done(void *data, struct zwp_text_input_v3 *wp_text_input_v3, uint32_t serial) {
3198 SeatState *ss = (SeatState *)data;
3199 if (!ss) {
3200 return;
3201 }
3202
3203 if (ss->ime_window_id == DisplayServer::INVALID_WINDOW_ID) {
3204 return;
3205 }
3206
3207 if (!ss->ime_text_commit.is_empty()) {
3208 Ref<IMECommitEventMessage> msg;
3209 msg.instantiate();
3210 msg->id = ss->ime_window_id;
3211 msg->text = ss->ime_text_commit;
3212 ss->wayland_thread->push_message(msg);
3213 } else {
3214 Ref<IMEUpdateEventMessage> msg;
3215 msg.instantiate();
3216 msg->id = ss->ime_window_id;
3217 msg->text = ss->ime_text;
3218 msg->selection = ss->ime_cursor;
3219 ss->wayland_thread->push_message(msg);
3220 }
3221
3222 ss->ime_text = String();
3223 ss->ime_text_commit = String();
3224 ss->ime_cursor = Vector2i();
3225 }
3226
3227 void WaylandThread::_xdg_activation_token_on_done(void *data, struct xdg_activation_token_v1 *xdg_activation_token, const char *token) {
3228 WindowState *ws = (WindowState *)data;
3229 ERR_FAIL_NULL(ws);
3230 ERR_FAIL_NULL(ws->wayland_thread);
3231 ERR_FAIL_NULL(ws->wl_surface);
3232
3233 xdg_activation_v1_activate(ws->wayland_thread->registry.xdg_activation, token, ws->wl_surface);
3234 xdg_activation_token_v1_destroy(xdg_activation_token);
3235
3236 DEBUG_LOG_WAYLAND_THREAD(vformat("Received activation token and requested window activation."));
3237 }
3238
3239 void WaylandThread::_godot_embedding_compositor_on_client(void *data, struct godot_embedding_compositor *godot_embedding_compositor, struct godot_embedded_client *godot_embedded_client, int32_t pid) {
3240 EmbeddingCompositorState *state = (EmbeddingCompositorState *)data;
3241 ERR_FAIL_NULL(state);
3242
3243 EmbeddedClientState *client_state = memnew(EmbeddedClientState);
3244 client_state->embedding_compositor = godot_embedding_compositor;
3245 client_state->pid = pid;
3246 godot_embedded_client_add_listener(godot_embedded_client, &godot_embedded_client_listener, client_state);
3247
3248 DEBUG_LOG_WAYLAND_THREAD(vformat("New client %d.", pid));
3249 state->clients.push_back(godot_embedded_client);
3250 }
3251
3252 void WaylandThread::_godot_embedded_client_on_disconnected(void *data, struct godot_embedded_client *godot_embedded_client) {
3253 EmbeddedClientState *state = (EmbeddedClientState *)data;
3254 ERR_FAIL_NULL(state);
3255
3256 EmbeddingCompositorState *ecomp_state = godot_embedding_compositor_get_state(state->embedding_compositor);
3257 ERR_FAIL_NULL(ecomp_state);
3258
3259 ecomp_state->clients.erase_unordered(godot_embedded_client);
3260 ecomp_state->mapped_clients.erase(state->pid);
3261
3262 memfree(state);
3263 godot_embedded_client_destroy(godot_embedded_client);
3264
3265 DEBUG_LOG_WAYLAND_THREAD(vformat("Client %d disconnected.", state->pid));
3266 }
3267
3268 void WaylandThread::_godot_embedded_client_on_window_embedded(void *data, struct godot_embedded_client *godot_embedded_client) {
3269 EmbeddedClientState *state = (EmbeddedClientState *)data;
3270 ERR_FAIL_NULL(state);
3271
3272 EmbeddingCompositorState *ecomp_state = godot_embedding_compositor_get_state(state->embedding_compositor);
3273 ERR_FAIL_NULL(ecomp_state);
3274
3275 state->window_mapped = true;
3276
3277 ERR_FAIL_COND_MSG(ecomp_state->mapped_clients.has(state->pid), "More than one Wayland client per PID tried to create a window.");
3278
3279 ecomp_state->mapped_clients[state->pid] = godot_embedded_client;
3280 }
3281
3282 void WaylandThread::_godot_embedded_client_on_window_focus_in(void *data, struct godot_embedded_client *godot_embedded_client) {
3283 EmbeddedClientState *state = (EmbeddedClientState *)data;
3284 ERR_FAIL_NULL(state);
3285
3286 EmbeddingCompositorState *ecomp_state = godot_embedding_compositor_get_state(state->embedding_compositor);
3287 ERR_FAIL_NULL(ecomp_state);
3288
3289 ecomp_state->focused_pid = state->pid;
3290 DEBUG_LOG_WAYLAND_THREAD(vformat("Embedded client pid %d focus in", state->pid));
3291 }
3292
3293 void WaylandThread::_godot_embedded_client_on_window_focus_out(void *data, struct godot_embedded_client *godot_embedded_client) {
3294 EmbeddedClientState *state = (EmbeddedClientState *)data;
3295 ERR_FAIL_NULL(state);
3296
3297 EmbeddingCompositorState *ecomp_state = godot_embedding_compositor_get_state(state->embedding_compositor);
3298 ERR_FAIL_NULL(ecomp_state);
3299
3300 ecomp_state->focused_pid = -1;
3301 DEBUG_LOG_WAYLAND_THREAD(vformat("Embedded client pid %d focus out", state->pid));
3302 }
3303
3304 void WaylandThread::_clipboard_send(Vector<uint8_t> &p_data, const char *p_media_type, int32_t p_fd) {
3305 ssize_t written_bytes = 0;
3306
3307 bool valid_mime = false;
3308
3309 if (strcmp(p_media_type, "text/plain;charset=utf-8") == 0) {
3310 valid_mime = true;
3311 } else if (strcmp(p_media_type, "text/plain") == 0) {
3312 valid_mime = true;
3313 }
3314
3315 if (!valid_mime) {
3316 DEBUG_LOG_WAYLAND_THREAD(vformat("Clipboard: Media type '%s' unknown, skipping.", p_media_type));
3317 return;
3318 }
3319
3320 written_bytes = write(p_fd, p_data.ptr(), p_data.size());
3321
3322 if (written_bytes > 0) {
3323 DEBUG_LOG_WAYLAND_THREAD(vformat("Clipboard: sent %d bytes.", written_bytes));
3324 } else if (written_bytes == 0) {
3325 DEBUG_LOG_WAYLAND_THREAD("Clipboard: no bytes sent.");
3326 } else {
3327 ERR_PRINT(vformat("Clipboard: write error %d.", errno));
3328 }
3329 }
3330
3331 // NOTE: This must be started after a valid wl_display is loaded.
3332 void WaylandThread::_poll_events_thread(void *p_data) {
3333 Thread::set_name("Wayland Events");
3334
3335 ThreadData *data = (ThreadData *)p_data;
3336 ERR_FAIL_NULL(data);
3337 ERR_FAIL_NULL(data->wl_display);
3338
3339 struct pollfd poll_fd = {};
3340 poll_fd.fd = wl_display_get_fd(data->wl_display);
3341 poll_fd.events = POLLIN;
3342
3343 while (true) {
3344 // Empty the event queue while it's full.
3345 while (wl_display_prepare_read(data->wl_display) != 0) {
3346 // We aren't using wl_display_dispatch(), instead "manually" handling events
3347 // through wl_display_dispatch_pending so that we can use a global mutex and
3348 // be sure that this and the main thread won't race over stuff, as long as
3349 // the main thread locks it too.
3350 //
3351 // Note that the main thread can still call wl_display_roundtrip as that
3352 // method directly handles all events, effectively bypassing this polling
3353 // loop and thus the mutex locking, avoiding a deadlock.
3354 //
3355 // WARNING: Never call `wl_display_roundtrip` inside event handlers or while
3356 // this mutex isn't held! `wl_display_roundtrip` manually handles new events
3357 // and if not properly gated it _will_ cause potentially stall-inducing race
3358 // conditions. Ask me how I know.
3359 MutexLock mutex_lock(data->mutex);
3360
3361 if (wl_display_dispatch_pending(data->wl_display) == -1) {
3362 // Oh no. We'll check and handle any display error below.
3363 break;
3364 }
3365 }
3366
3367 _wl_display_check_error(data->wl_display);
3368
3369 wl_display_flush(data->wl_display);
3370
3371 // Wait for the event file descriptor to have new data.
3372 poll(&poll_fd, 1, -1);
3373
3374 if (data->thread_done.is_set()) {
3375 wl_display_cancel_read(data->wl_display);
3376 break;
3377 }
3378
3379 if (poll_fd.revents | POLLIN) {
3380 // Load the queues with fresh new data.
3381 wl_display_read_events(data->wl_display);
3382 } else {
3383 // Oh well... Stop signaling that we want to read.
3384 wl_display_cancel_read(data->wl_display);
3385 }
3386
3387 // The docs advise to redispatch unconditionally and it looks like that if we
3388 // don't do this we can't catch protocol errors, which is bad.
3389 MutexLock mutex_lock(data->mutex);
3390 wl_display_dispatch_pending(data->wl_display);
3391 }
3392 }
3393
3394 struct wl_display *WaylandThread::get_wl_display() const {
3395 return wl_display;
3396 }
3397
3398 // NOTE: Stuff like libdecor can (and will) register foreign proxies which
3399 // aren't formatted as we like. This method is needed to detect whether a proxy
3400 // has our tag. Also, be careful! The proxy has to be manually tagged or it
3401 // won't be recognized.
3402 bool WaylandThread::wl_proxy_is_godot(struct wl_proxy *p_proxy) {
3403 ERR_FAIL_NULL_V(p_proxy, false);
3404
3405 return wl_proxy_get_tag(p_proxy) == &proxy_tag;
3406 }
3407
3408 void WaylandThread::wl_proxy_tag_godot(struct wl_proxy *p_proxy) {
3409 ERR_FAIL_NULL(p_proxy);
3410
3411 wl_proxy_set_tag(p_proxy, &proxy_tag);
3412 }
3413
3414 // Returns the wl_surface's `WindowState`, otherwise `nullptr`.
3415 // NOTE: This will fail if the surface isn't tagged as ours.
3416 WaylandThread::WindowState *WaylandThread::wl_surface_get_window_state(struct wl_surface *p_surface) {
3417 if (p_surface && wl_proxy_is_godot((wl_proxy *)p_surface)) {
3418 return (WindowState *)wl_surface_get_user_data(p_surface);
3419 }
3420
3421 return nullptr;
3422 }
3423
3424 // Returns the wl_outputs's `ScreenState`, otherwise `nullptr`.
3425 // NOTE: This will fail if the output isn't tagged as ours.
3426 WaylandThread::ScreenState *WaylandThread::wl_output_get_screen_state(struct wl_output *p_output) {
3427 if (p_output && wl_proxy_is_godot((wl_proxy *)p_output)) {
3428 return (ScreenState *)wl_output_get_user_data(p_output);
3429 }
3430
3431 return nullptr;
3432 }
3433
3434 // Returns the wl_seat's `SeatState`, otherwise `nullptr`.
3435 // NOTE: This will fail if the output isn't tagged as ours.
3436 WaylandThread::SeatState *WaylandThread::wl_seat_get_seat_state(struct wl_seat *p_seat) {
3437 if (p_seat && wl_proxy_is_godot((wl_proxy *)p_seat)) {
3438 return (SeatState *)wl_seat_get_user_data(p_seat);
3439 }
3440
3441 return nullptr;
3442 }
3443
3444 // Returns the wp_tablet_tool's `TabletToolState`, otherwise `nullptr`.
3445 // NOTE: This will fail if the output isn't tagged as ours.
3446 WaylandThread::TabletToolState *WaylandThread::wp_tablet_tool_get_state(struct zwp_tablet_tool_v2 *p_tool) {
3447 if (p_tool && wl_proxy_is_godot((wl_proxy *)p_tool)) {
3448 return (TabletToolState *)zwp_tablet_tool_v2_get_user_data(p_tool);
3449 }
3450
3451 return nullptr;
3452 }
3453 // Returns the wl_data_offer's `OfferState`, otherwise `nullptr`.
3454 // NOTE: This will fail if the output isn't tagged as ours.
3455 WaylandThread::OfferState *WaylandThread::wl_data_offer_get_offer_state(struct wl_data_offer *p_offer) {
3456 if (p_offer && wl_proxy_is_godot((wl_proxy *)p_offer)) {
3457 return (OfferState *)wl_data_offer_get_user_data(p_offer);
3458 }
3459
3460 return nullptr;
3461 }
3462
3463 // Returns the wl_data_offer's `OfferState`, otherwise `nullptr`.
3464 // NOTE: This will fail if the output isn't tagged as ours.
3465 WaylandThread::OfferState *WaylandThread::wp_primary_selection_offer_get_offer_state(struct zwp_primary_selection_offer_v1 *p_offer) {
3466 if (p_offer && wl_proxy_is_godot((wl_proxy *)p_offer)) {
3467 return (OfferState *)zwp_primary_selection_offer_v1_get_user_data(p_offer);
3468 }
3469
3470 return nullptr;
3471 }
3472
3473 WaylandThread::EmbeddingCompositorState *WaylandThread::godot_embedding_compositor_get_state(struct godot_embedding_compositor *p_compositor) {
3474 // NOTE: No need for tag check as it's a "fake" interface - nothing else exposes it.
3475 if (p_compositor) {
3476 return (EmbeddingCompositorState *)godot_embedding_compositor_get_user_data(p_compositor);
3477 }
3478
3479 return nullptr;
3480 }
3481
3482 // This is implemented as a method because this is the simplest way of
3483 // accounting for dynamic output scale changes.
3484 int WaylandThread::window_state_get_preferred_buffer_scale(WindowState *p_ws) {
3485 ERR_FAIL_NULL_V(p_ws, 1);
3486
3487 if (p_ws->preferred_fractional_scale > 0) {
3488 // We're scaling fractionally. Per spec, the buffer scale is always 1.
3489 return 1;
3490 }
3491
3492 if (p_ws->wl_outputs.is_empty()) {
3493 DEBUG_LOG_WAYLAND_THREAD("Window has no output associated, returning buffer scale of 1.");
3494 return 1;
3495 }
3496
3497 // TODO: Cache value?
3498 int max_size = 1;
3499
3500 // ================================ IMPORTANT =================================
3501 // NOTE: Due to a Godot limitation, we can't really rescale the whole UI yet.
3502 // Because of this reason, all platforms have resorted to forcing the highest
3503 // scale possible of a system on any window, despite of what screen it's onto.
3504 // On this backend everything's already in place for dynamic window scale
3505 // handling, but in the meantime we'll just select the biggest _global_ output.
3506 // To restore dynamic scale selection, simply iterate over `p_ws->wl_outputs`
3507 // instead.
3508 for (struct wl_output *wl_output : p_ws->registry->wl_outputs) {
3509 ScreenState *ss = wl_output_get_screen_state(wl_output);
3510
3511 if (ss && ss->pending_data.scale > max_size) {
3512 // NOTE: For some mystical reason, wl_output.done is emitted _after_ windows
3513 // get resized but the scale event gets sent _before_ that. I'm still leaning
3514 // towards the idea that rescaling when a window gets a resolution change is a
3515 // pretty good approach, but this means that we'll have to use the screen data
3516 // before it's "committed".
3517 // FIXME: Use the committed data. Somehow.
3518 max_size = ss->pending_data.scale;
3519 }
3520 }
3521
3522 return max_size;
3523 }
3524
3525 double WaylandThread::window_state_get_scale_factor(const WindowState *p_ws) {
3526 ERR_FAIL_NULL_V(p_ws, 1);
3527
3528 if (p_ws->fractional_scale > 0) {
3529 // The fractional scale amount takes priority.
3530 return p_ws->fractional_scale;
3531 }
3532
3533 return p_ws->buffer_scale;
3534 }
3535
3536 void WaylandThread::window_state_update_size(WindowState *p_ws, int p_width, int p_height) {
3537 ERR_FAIL_NULL(p_ws);
3538
3539 // Failsafe.
3540 p_width = MAX(p_width, 1);
3541 p_height = MAX(p_height, 1);
3542
3543 int preferred_buffer_scale = window_state_get_preferred_buffer_scale(p_ws);
3544 bool using_fractional = p_ws->preferred_fractional_scale > 0;
3545
3546 // If neither is true we no-op.
3547 bool scale_changed = false;
3548 bool size_changed = false;
3549
3550 if (p_ws->rect.size.width != p_width || p_ws->rect.size.height != p_height) {
3551 p_ws->rect.size.width = p_width;
3552 p_ws->rect.size.height = p_height;
3553
3554 size_changed = true;
3555 }
3556
3557 if (using_fractional && p_ws->fractional_scale != p_ws->preferred_fractional_scale) {
3558 p_ws->fractional_scale = p_ws->preferred_fractional_scale;
3559 scale_changed = true;
3560 }
3561
3562 if (p_ws->buffer_scale != preferred_buffer_scale) {
3563 // The buffer scale is always important, even if we use frac scaling.
3564 p_ws->buffer_scale = preferred_buffer_scale;
3565 p_ws->buffer_scale_changed = true;
3566
3567 if (!using_fractional) {
3568 // We don't bother updating everything else if it's turned on though.
3569 scale_changed = true;
3570 }
3571 }
3572
3573 if (p_ws->wl_surface) {
3574 if (p_ws->wp_viewport) {
3575 wp_viewport_set_destination(p_ws->wp_viewport, p_width, p_height);
3576 }
3577
3578 if (p_ws->xdg_surface) {
3579 xdg_surface_set_window_geometry(p_ws->xdg_surface, 0, 0, p_width, p_height);
3580 }
3581 }
3582
3583 #ifdef LIBDECOR_ENABLED
3584 if (p_ws->libdecor_frame) {
3585 struct libdecor_state *state = libdecor_state_new(p_width, p_height);
3586 libdecor_frame_commit(p_ws->libdecor_frame, state, p_ws->pending_libdecor_configuration);
3587 libdecor_state_free(state);
3588 p_ws->pending_libdecor_configuration = nullptr;
3589 }
3590 #endif
3591
3592 if (size_changed || scale_changed) {
3593 double win_scale = window_state_get_scale_factor(p_ws);
3594 Size2i scaled_size = scale_vector2i(p_ws->rect.size, win_scale);
3595
3596 if (using_fractional) {
3597 DEBUG_LOG_WAYLAND_THREAD(vformat("Resizing the window from %s to %s (fractional scale x%f).", p_ws->rect.size, scaled_size, p_ws->fractional_scale));
3598 } else {
3599 DEBUG_LOG_WAYLAND_THREAD(vformat("Resizing the window from %s to %s (buffer scale x%d).", p_ws->rect.size, scaled_size, p_ws->buffer_scale));
3600 }
3601
3602 // FIXME: Actually resize the hint instead of centering it.
3603 p_ws->wayland_thread->pointer_set_hint(scaled_size / 2);
3604
3605 Ref<WindowRectMessage> rect_msg;
3606 rect_msg.instantiate();
3607 rect_msg->id = p_ws->id;
3608 rect_msg->rect.position = scale_vector2i(p_ws->rect.position, win_scale);
3609 rect_msg->rect.size = scaled_size;
3610 p_ws->wayland_thread->push_message(rect_msg);
3611 }
3612
3613 if (scale_changed) {
3614 Ref<WindowEventMessage> dpi_msg;
3615 dpi_msg.instantiate();
3616 dpi_msg->id = p_ws->id;
3617 dpi_msg->event = DisplayServer::WINDOW_EVENT_DPI_CHANGE;
3618 p_ws->wayland_thread->push_message(dpi_msg);
3619 }
3620 }
3621
3622 // Scales a vector according to wp_fractional_scale's rules, where coordinates
3623 // must be scaled with away from zero half-rounding.
3624 Vector2i WaylandThread::scale_vector2i(const Vector2i &p_vector, double p_amount) {
3625 // This snippet is tiny, I know, but this is done a lot.
3626 int x = std::round(p_vector.x * p_amount);
3627 int y = std::round(p_vector.y * p_amount);
3628
3629 return Vector2i(x, y);
3630 }
3631
3632 void WaylandThread::seat_state_unlock_pointer(SeatState *p_ss) {
3633 ERR_FAIL_NULL(p_ss);
3634
3635 if (p_ss->wl_pointer == nullptr) {
3636 return;
3637 }
3638
3639 if (p_ss->wp_locked_pointer) {
3640 zwp_locked_pointer_v1_destroy(p_ss->wp_locked_pointer);
3641 p_ss->wp_locked_pointer = nullptr;
3642 }
3643
3644 if (p_ss->wp_confined_pointer) {
3645 zwp_confined_pointer_v1_destroy(p_ss->wp_confined_pointer);
3646 p_ss->wp_confined_pointer = nullptr;
3647 }
3648 }
3649
3650 void WaylandThread::seat_state_lock_pointer(SeatState *p_ss) {
3651 ERR_FAIL_NULL(p_ss);
3652
3653 if (p_ss->wl_pointer == nullptr) {
3654 WARN_PRINT("Can't lock - no pointer?");
3655 return;
3656 }
3657
3658 if (registry.wp_pointer_constraints == nullptr) {
3659 WARN_PRINT("Can't lock - no constraints global.");
3660 return;
3661 }
3662
3663 if (p_ss->wp_locked_pointer == nullptr) {
3664 struct wl_surface *locked_surface = window_get_wl_surface(p_ss->pointer_data.last_pointed_id);
3665 if (locked_surface == nullptr) {
3666 locked_surface = window_get_wl_surface(DisplayServer::MAIN_WINDOW_ID);
3667 }
3668 ERR_FAIL_NULL(locked_surface);
3669
3670 p_ss->wp_locked_pointer = zwp_pointer_constraints_v1_lock_pointer(registry.wp_pointer_constraints, locked_surface, p_ss->wl_pointer, nullptr, ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
3671 }
3672 }
3673
3674 void WaylandThread::seat_state_set_hint(SeatState *p_ss, int p_x, int p_y) {
3675 if (p_ss->wp_locked_pointer == nullptr) {
3676 return;
3677 }
3678
3679 zwp_locked_pointer_v1_set_cursor_position_hint(p_ss->wp_locked_pointer, wl_fixed_from_int(p_x), wl_fixed_from_int(p_y));
3680 }
3681
3682 void WaylandThread::seat_state_confine_pointer(SeatState *p_ss) {
3683 ERR_FAIL_NULL(p_ss);
3684
3685 if (p_ss->wl_pointer == nullptr) {
3686 return;
3687 }
3688
3689 if (registry.wp_pointer_constraints == nullptr) {
3690 return;
3691 }
3692
3693 if (p_ss->wp_confined_pointer == nullptr) {
3694 struct wl_surface *confined_surface = window_get_wl_surface(p_ss->pointer_data.last_pointed_id);
3695 ERR_FAIL_NULL(confined_surface);
3696
3697 p_ss->wp_confined_pointer = zwp_pointer_constraints_v1_confine_pointer(registry.wp_pointer_constraints, confined_surface, p_ss->wl_pointer, nullptr, ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
3698 }
3699 }
3700
3701 void WaylandThread::seat_state_update_cursor(SeatState *p_ss) {
3702 ERR_FAIL_NULL(p_ss);
3703
3704 WaylandThread *thread = p_ss->wayland_thread;
3705 ERR_FAIL_NULL(p_ss->wayland_thread);
3706
3707 if (!p_ss->wl_pointer || !p_ss->cursor_surface) {
3708 return;
3709 }
3710
3711 // NOTE: Those values are valid by default and will hide the cursor when
3712 // unchanged.
3713 struct wl_buffer *cursor_buffer = nullptr;
3714 uint32_t hotspot_x = 0;
3715 uint32_t hotspot_y = 0;
3716 int scale = 1;
3717
3718 if (thread->cursor_visible) {
3719 DisplayServer::CursorShape shape = thread->cursor_shape;
3720
3721 struct CustomCursor *custom_cursor = thread->custom_cursors.getptr(shape);
3722
3723 if (custom_cursor) {
3724 cursor_buffer = custom_cursor->wl_buffer;
3725 hotspot_x = custom_cursor->hotspot.x;
3726 hotspot_y = custom_cursor->hotspot.y;
3727
3728 // We can't really reasonably scale custom cursors, so we'll let the
3729 // compositor do it for us (badly).
3730 scale = 1;
3731 } else if (thread->registry.wp_cursor_shape_manager) {
3732 wp_cursor_shape_device_v1_shape wp_shape = thread->standard_cursors[shape];
3733 wp_cursor_shape_device_v1_set_shape(p_ss->wp_cursor_shape_device, p_ss->pointer_enter_serial, wp_shape);
3734
3735 // We should avoid calling the `wl_pointer_set_cursor` at the end of this method.
3736 return;
3737 } else {
3738 struct wl_cursor *wl_cursor = thread->wl_cursors[shape];
3739
3740 if (!wl_cursor) {
3741 return;
3742 }
3743
3744 int frame_idx = 0;
3745
3746 if (wl_cursor->image_count > 1) {
3747 // The cursor is animated.
3748 frame_idx = wl_cursor_frame(wl_cursor, p_ss->cursor_time_ms);
3749
3750 if (!p_ss->cursor_frame_callback) {
3751 // Since it's animated, we'll re-update it the next frame.
3752 p_ss->cursor_frame_callback = wl_surface_frame(p_ss->cursor_surface);
3753 wl_callback_add_listener(p_ss->cursor_frame_callback, &cursor_frame_callback_listener, p_ss);
3754 }
3755 }
3756
3757 struct wl_cursor_image *wl_cursor_image = wl_cursor->images[frame_idx];
3758
3759 scale = thread->cursor_scale;
3760
3761 cursor_buffer = wl_cursor_image_get_buffer(wl_cursor_image);
3762
3763 // As the surface's buffer is scaled (thus the surface is smaller) and the
3764 // hotspot must be expressed in surface-local coordinates, we need to scale
3765 // it down accordingly.
3766 hotspot_x = wl_cursor_image->hotspot_x / scale;
3767 hotspot_y = wl_cursor_image->hotspot_y / scale;
3768 }
3769 }
3770
3771 wl_pointer_set_cursor(p_ss->wl_pointer, p_ss->pointer_enter_serial, p_ss->cursor_surface, hotspot_x, hotspot_y);
3772 wl_surface_set_buffer_scale(p_ss->cursor_surface, scale);
3773 wl_surface_attach(p_ss->cursor_surface, cursor_buffer, 0, 0);
3774 wl_surface_damage_buffer(p_ss->cursor_surface, 0, 0, INT_MAX, INT_MAX);
3775
3776 wl_surface_commit(p_ss->cursor_surface);
3777 }
3778
3779 void WaylandThread::seat_state_echo_keys(SeatState *p_ss) {
3780 ERR_FAIL_NULL(p_ss);
3781
3782 if (p_ss->wl_keyboard == nullptr) {
3783 return;
3784 }
3785
3786 // TODO: Comment and document out properly this block of code.
3787 // In short, this implements key repeating.
3788 if (p_ss->repeat_key_delay_msec && p_ss->repeating_keycode != XKB_KEYCODE_INVALID) {
3789 uint64_t current_ticks = OS::get_singleton()->get_ticks_msec();
3790 uint64_t delayed_start_ticks = p_ss->last_repeat_start_msec + p_ss->repeat_start_delay_msec;
3791
3792 if (p_ss->last_repeat_msec < delayed_start_ticks) {
3793 p_ss->last_repeat_msec = delayed_start_ticks;
3794 }
3795
3796 if (current_ticks >= delayed_start_ticks) {
3797 uint64_t ticks_delta = current_ticks - p_ss->last_repeat_msec;
3798
3799 int keys_amount = (ticks_delta / p_ss->repeat_key_delay_msec);
3800
3801 for (int i = 0; i < keys_amount; i++) {
3802 _seat_state_handle_xkb_keycode(p_ss, p_ss->repeating_keycode, true, true);
3803 }
3804
3805 p_ss->last_repeat_msec += ticks_delta - (ticks_delta % p_ss->repeat_key_delay_msec);
3806 }
3807 }
3808 }
3809
3810 void WaylandThread::push_message(Ref<Message> message) {
3811 messages.push_back(message);
3812 }
3813
3814 bool WaylandThread::has_message() {
3815 return messages.front() != nullptr;
3816 }
3817
3818 Ref<WaylandThread::Message> WaylandThread::pop_message() {
3819 if (messages.front() != nullptr) {
3820 Ref<Message> msg = messages.front()->get();
3821 messages.pop_front();
3822 return msg;
3823 }
3824
3825 // This method should only be called if `has_messages` returns true but if
3826 // that isn't the case we'll just return an invalid `Ref`. After all, due to
3827 // its `InputEvent`-like interface, we still have to dynamically cast and check
3828 // the `Ref`'s validity anyways.
3829 return Ref<Message>();
3830 }
3831
3832 void WaylandThread::window_create(DisplayServer::WindowID p_window_id, const Size2i &p_size, DisplayServer::WindowID p_parent_id) {
3833 ERR_FAIL_COND(windows.has(p_window_id));
3834 WindowState &ws = windows[p_window_id];
3835
3836 ws.id = p_window_id;
3837
3838 ws.registry = &registry;
3839 ws.wayland_thread = this;
3840
3841 ws.rect.size = p_size.maxi(1);
3842
3843 ws.wl_surface = wl_compositor_create_surface(registry.wl_compositor);
3844 wl_proxy_tag_godot((struct wl_proxy *)ws.wl_surface);
3845 wl_surface_add_listener(ws.wl_surface, &wl_surface_listener, &ws);
3846
3847 if (registry.wp_viewporter) {
3848 ws.wp_viewport = wp_viewporter_get_viewport(registry.wp_viewporter, ws.wl_surface);
3849
3850 if (registry.wp_fractional_scale_manager) {
3851 ws.wp_fractional_scale = wp_fractional_scale_manager_v1_get_fractional_scale(registry.wp_fractional_scale_manager, ws.wl_surface);
3852 wp_fractional_scale_v1_add_listener(ws.wp_fractional_scale, &wp_fractional_scale_listener, &ws);
3853 }
3854 }
3855
3856 bool decorated = false;
3857
3858 #ifdef LIBDECOR_ENABLED
3859 if (!decorated && libdecor_context) {
3860 ws.libdecor_frame = libdecor_decorate(libdecor_context, ws.wl_surface, (struct libdecor_frame_interface *)&libdecor_frame_interface, &ws);
3861 libdecor_frame_map(ws.libdecor_frame);
3862
3863 if (registry.xdg_toplevel_icon_manager) {
3864 xdg_toplevel *toplevel = libdecor_frame_get_xdg_toplevel(ws.libdecor_frame);
3865 if (toplevel != nullptr) {
3866 xdg_toplevel_icon_manager_v1_set_icon(registry.xdg_toplevel_icon_manager, toplevel, xdg_icon);
3867 }
3868 }
3869
3870 decorated = true;
3871 }
3872 #endif
3873
3874 if (!decorated) {
3875 // libdecor has failed loading or is disabled, we shall handle xdg_toplevel
3876 // creation and decoration ourselves (and by decorating for now I just mean
3877 // asking for SSDs and hoping for the best).
3878 ws.xdg_surface = xdg_wm_base_get_xdg_surface(registry.xdg_wm_base, ws.wl_surface);
3879 xdg_surface_add_listener(ws.xdg_surface, &xdg_surface_listener, &ws);
3880
3881 ws.xdg_toplevel = xdg_surface_get_toplevel(ws.xdg_surface);
3882 xdg_toplevel_add_listener(ws.xdg_toplevel, &xdg_toplevel_listener, &ws);
3883
3884 if (registry.xdg_decoration_manager) {
3885 ws.xdg_toplevel_decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(registry.xdg_decoration_manager, ws.xdg_toplevel);
3886 zxdg_toplevel_decoration_v1_add_listener(ws.xdg_toplevel_decoration, &xdg_toplevel_decoration_listener, &ws);
3887
3888 decorated = true;
3889 }
3890
3891 if (registry.xdg_toplevel_icon_manager) {
3892 xdg_toplevel_icon_manager_v1_set_icon(registry.xdg_toplevel_icon_manager, ws.xdg_toplevel, xdg_icon);
3893 }
3894 }
3895
3896 if (p_parent_id != DisplayServer::INVALID_WINDOW_ID) {
3897 // NOTE: It's important to set the parent ASAP to avoid misunderstandings with
3898 // the compositor. For example, niri immediately resizes the window to full
3899 // size as soon as it's configured if it's not parented to another toplevel.
3900 window_set_parent(p_window_id, p_parent_id);
3901 }
3902
3903 ws.frame_callback = wl_surface_frame(ws.wl_surface);
3904 wl_callback_add_listener(ws.frame_callback, &frame_wl_callback_listener, &ws);
3905
3906 if (registry.xdg_exporter_v2) {
3907 ws.xdg_exported_v2 = zxdg_exporter_v2_export_toplevel(registry.xdg_exporter_v2, ws.wl_surface);
3908 zxdg_exported_v2_add_listener(ws.xdg_exported_v2, &xdg_exported_v2_listener, &ws);
3909 } else if (registry.xdg_exporter_v1) {
3910 ws.xdg_exported_v1 = zxdg_exporter_v1_export(registry.xdg_exporter_v1, ws.wl_surface);
3911 zxdg_exported_v1_add_listener(ws.xdg_exported_v1, &xdg_exported_v1_listener, &ws);
3912 }
3913
3914 wl_surface_commit(ws.wl_surface);
3915 }
3916
3917 void WaylandThread::window_create_popup(DisplayServer::WindowID p_window_id, DisplayServer::WindowID p_parent_id, Rect2i p_rect) {
3918 ERR_FAIL_COND(windows.has(p_window_id));
3919 ERR_FAIL_COND(!windows.has(p_parent_id));
3920
3921 WindowState &ws = windows[p_window_id];
3922 WindowState &parent = windows[p_parent_id];
3923
3924 double parent_scale = window_state_get_scale_factor(&parent);
3925
3926 p_rect.position = scale_vector2i(p_rect.position, 1.0 / parent_scale);
3927 p_rect.size = scale_vector2i(p_rect.size, 1.0 / parent_scale);
3928 // We manually scaled based on the parent. If we don't set the relevant fields,
3929 // the resizing routines will get confused and scale once more.
3930 ws.preferred_fractional_scale = parent.preferred_fractional_scale;
3931 ws.fractional_scale = parent.fractional_scale;
3932 ws.buffer_scale = parent.buffer_scale;
3933
3934 ws.id = p_window_id;
3935 ws.parent_id = p_parent_id;
3936 ws.registry = &registry;
3937 ws.wayland_thread = this;
3938
3939 ws.rect = p_rect;
3940
3941 ws.wl_surface = wl_compositor_create_surface(registry.wl_compositor);
3942 wl_proxy_tag_godot((struct wl_proxy *)ws.wl_surface);
3943 wl_surface_add_listener(ws.wl_surface, &wl_surface_listener, &ws);
3944
3945 if (registry.wp_viewporter) {
3946 ws.wp_viewport = wp_viewporter_get_viewport(registry.wp_viewporter, ws.wl_surface);
3947
3948 if (registry.wp_fractional_scale_manager) {
3949 ws.wp_fractional_scale = wp_fractional_scale_manager_v1_get_fractional_scale(registry.wp_fractional_scale_manager, ws.wl_surface);
3950 wp_fractional_scale_v1_add_listener(ws.wp_fractional_scale, &wp_fractional_scale_listener, &ws);
3951 }
3952 }
3953
3954 ws.xdg_surface = xdg_wm_base_get_xdg_surface(registry.xdg_wm_base, ws.wl_surface);
3955 xdg_surface_add_listener(ws.xdg_surface, &xdg_surface_listener, &ws);
3956
3957 Rect2i positioner_rect;
3958 positioner_rect.size = parent.rect.size;
3959 struct xdg_surface *parent_xdg_surface = parent.xdg_surface;
3960
3961 Point2i offset = ws.rect.position - parent.rect.position;
3962
3963 #ifdef LIBDECOR_ENABLED
3964 if (!parent_xdg_surface && parent.libdecor_frame) {
3965 parent_xdg_surface = libdecor_frame_get_xdg_surface(parent.libdecor_frame);
3966
3967 int corner_x = 0;
3968 int corner_y = 0;
3969 libdecor_frame_translate_coordinate(parent.libdecor_frame, 0, 0, &corner_x, &corner_y);
3970
3971 positioner_rect.position.x = corner_x;
3972 positioner_rect.position.y = corner_y;
3973
3974 positioner_rect.size.width -= corner_x;
3975 positioner_rect.size.height -= corner_y;
3976 }
3977 #endif
3978
3979 ERR_FAIL_NULL(parent_xdg_surface);
3980
3981 struct xdg_positioner *xdg_positioner = xdg_wm_base_create_positioner(registry.xdg_wm_base);
3982 xdg_positioner_set_size(xdg_positioner, ws.rect.size.width, ws.rect.size.height);
3983 xdg_positioner_set_anchor(xdg_positioner, XDG_POSITIONER_ANCHOR_TOP_LEFT);
3984 xdg_positioner_set_gravity(xdg_positioner, XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT);
3985 xdg_positioner_set_constraint_adjustment(xdg_positioner, XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X | XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y | XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X | XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y);
3986 xdg_positioner_set_anchor_rect(xdg_positioner, positioner_rect.position.x, positioner_rect.position.y, positioner_rect.size.width, positioner_rect.size.height);
3987 xdg_positioner_set_offset(xdg_positioner, offset.x, offset.y);
3988
3989 ws.xdg_popup = xdg_surface_get_popup(ws.xdg_surface, parent_xdg_surface, xdg_positioner);
3990 xdg_popup_add_listener(ws.xdg_popup, &xdg_popup_listener, &ws);
3991
3992 xdg_positioner_destroy(xdg_positioner);
3993
3994 ws.frame_callback = wl_surface_frame(ws.wl_surface);
3995 wl_callback_add_listener(ws.frame_callback, &frame_wl_callback_listener, &ws);
3996
3997 wl_surface_commit(ws.wl_surface);
3998 }
3999
4000 void WaylandThread::window_destroy(DisplayServer::WindowID p_window_id) {
4001 ERR_FAIL_COND(!windows.has(p_window_id));
4002 WindowState &ws = windows[p_window_id];
4003
4004 if (ws.xdg_popup) {
4005 xdg_popup_destroy(ws.xdg_popup);
4006 }
4007
4008 if (ws.xdg_toplevel_decoration) {
4009 zxdg_toplevel_decoration_v1_destroy(ws.xdg_toplevel_decoration);
4010 }
4011
4012 if (ws.xdg_toplevel) {
4013 xdg_toplevel_destroy(ws.xdg_toplevel);
4014 }
4015
4016 #ifdef LIBDECOR_ENABLED
4017 if (ws.libdecor_frame) {
4018 libdecor_frame_unref(ws.libdecor_frame);
4019 }
4020 #endif // LIBDECOR_ENABLED
4021
4022 if (ws.wp_fractional_scale) {
4023 wp_fractional_scale_v1_destroy(ws.wp_fractional_scale);
4024 }
4025
4026 if (ws.wp_viewport) {
4027 wp_viewport_destroy(ws.wp_viewport);
4028 }
4029
4030 if (ws.frame_callback) {
4031 wl_callback_destroy(ws.frame_callback);
4032 }
4033
4034 if (ws.xdg_surface) {
4035 xdg_surface_destroy(ws.xdg_surface);
4036 }
4037
4038 if (ws.wl_surface) {
4039 wl_surface_destroy(ws.wl_surface);
4040 }
4041
4042 // Before continuing, let's handle any leftover event that might still refer to
4043 // this window.
4044 wl_display_roundtrip(wl_display);
4045
4046 // We can already clean up here, we're done.
4047 windows.erase(p_window_id);
4048 }
4049
4050 struct wl_surface *WaylandThread::window_get_wl_surface(DisplayServer::WindowID p_window_id) const {
4051 const WindowState *ws = windows.getptr(p_window_id);
4052 if (ws) {
4053 return ws->wl_surface;
4054 }
4055
4056 return nullptr;
4057 }
4058
4059 WaylandThread::WindowState *WaylandThread::window_get_state(DisplayServer::WindowID p_window_id) {
4060 return windows.getptr(p_window_id);
4061 }
4062
4063 const WaylandThread::WindowState *WaylandThread::window_get_state(DisplayServer::WindowID p_window_id) const {
4064 return windows.getptr(p_window_id);
4065 }
4066
4067 Size2i WaylandThread::window_set_size(DisplayServer::WindowID p_window_id, const Size2i &p_size) {
4068 ERR_FAIL_COND_V(!windows.has(p_window_id), p_size);
4069 WindowState &ws = windows[p_window_id];
4070
4071 double window_scale = window_state_get_scale_factor(&ws);
4072
4073 if (ws.maximized) {
4074 // Can't do anything.
4075 return scale_vector2i(ws.rect.size, window_scale);
4076 }
4077
4078 Size2i new_size = scale_vector2i(p_size, 1 / window_scale);
4079
4080 if (ws.tiled_left && ws.tiled_right) {
4081 // Tiled left and right, we shouldn't change from our current width or else
4082 // it'll look wonky.
4083 new_size.width = ws.rect.size.width;
4084 }
4085
4086 if (ws.tiled_top && ws.tiled_bottom) {
4087 // Tiled top and bottom. Same as above, but for the height.
4088 new_size.height = ws.rect.size.height;
4089 }
4090
4091 if (ws.resizing && ws.rect.size.width > 0 && ws.rect.size.height > 0) {
4092 // The spec says that we shall not resize further than the config size. We can
4093 // resize less than that though.
4094 new_size = new_size.min(ws.rect.size);
4095 }
4096
4097 // NOTE: Older versions of libdecor (~2022) do not have a way to get the max
4098 // content size. Let's also check for its pointer so that we can preserve
4099 // compatibility with older distros.
4100 if (ws.libdecor_frame && libdecor_frame_get_max_content_size) {
4101 int max_width = new_size.width;
4102 int max_height = new_size.height;
4103
4104 // NOTE: Max content size is dynamic on libdecor, as plugins can override it
4105 // to accommodate their decorations.
4106 libdecor_frame_get_max_content_size(ws.libdecor_frame, &max_width, &max_height);
4107
4108 if (max_width > 0 && max_height > 0) {
4109 new_size.width = MIN(new_size.width, max_width);
4110 new_size.height = MIN(new_size.height, max_height);
4111 }
4112 }
4113
4114 window_state_update_size(&ws, new_size.width, new_size.height);
4115
4116 return scale_vector2i(new_size, window_scale).maxi(1);
4117 }
4118
4119 void WaylandThread::beep() const {
4120 if (registry.xdg_system_bell) {
4121 xdg_system_bell_v1_ring(registry.xdg_system_bell, nullptr);
4122 }
4123 }
4124
4125 void WaylandThread::window_start_drag(DisplayServer::WindowID p_window_id) {
4126 ERR_FAIL_COND(!windows.has(p_window_id));
4127 WindowState &ws = windows[p_window_id];
4128 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
4129
4130 if (ss && ws.xdg_toplevel) {
4131 xdg_toplevel_move(ws.xdg_toplevel, ss->wl_seat, ss->pointer_data.button_serial);
4132 }
4133
4134 #ifdef LIBDECOR_ENABLED
4135 if (ws.libdecor_frame) {
4136 libdecor_frame_move(ws.libdecor_frame, ss->wl_seat, ss->pointer_data.button_serial);
4137 }
4138 #endif
4139 }
4140
4141 void WaylandThread::window_start_resize(DisplayServer::WindowResizeEdge p_edge, DisplayServer::WindowID p_window) {
4142 ERR_FAIL_COND(!windows.has(p_window));
4143 WindowState &ws = windows[p_window];
4144 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
4145
4146 if (ss && ws.xdg_toplevel) {
4147 xdg_toplevel_resize_edge edge = XDG_TOPLEVEL_RESIZE_EDGE_NONE;
4148 switch (p_edge) {
4149 case DisplayServer::WINDOW_EDGE_TOP_LEFT: {
4150 edge = XDG_TOPLEVEL_RESIZE_EDGE_TOP_LEFT;
4151 } break;
4152 case DisplayServer::WINDOW_EDGE_TOP: {
4153 edge = XDG_TOPLEVEL_RESIZE_EDGE_TOP;
4154 } break;
4155 case DisplayServer::WINDOW_EDGE_TOP_RIGHT: {
4156 edge = XDG_TOPLEVEL_RESIZE_EDGE_TOP_RIGHT;
4157 } break;
4158 case DisplayServer::WINDOW_EDGE_LEFT: {
4159 edge = XDG_TOPLEVEL_RESIZE_EDGE_LEFT;
4160 } break;
4161 case DisplayServer::WINDOW_EDGE_RIGHT: {
4162 edge = XDG_TOPLEVEL_RESIZE_EDGE_RIGHT;
4163 } break;
4164 case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: {
4165 edge = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_LEFT;
4166 } break;
4167 case DisplayServer::WINDOW_EDGE_BOTTOM: {
4168 edge = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM;
4169 } break;
4170 case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: {
4171 edge = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT;
4172 } break;
4173 default:
4174 break;
4175 }
4176 xdg_toplevel_resize(ws.xdg_toplevel, ss->wl_seat, ss->pointer_data.button_serial, edge);
4177 }
4178
4179 #ifdef LIBDECOR_ENABLED
4180 if (ws.libdecor_frame) {
4181 libdecor_resize_edge edge = LIBDECOR_RESIZE_EDGE_NONE;
4182 switch (p_edge) {
4183 case DisplayServer::WINDOW_EDGE_TOP_LEFT: {
4184 edge = LIBDECOR_RESIZE_EDGE_TOP_LEFT;
4185 } break;
4186 case DisplayServer::WINDOW_EDGE_TOP: {
4187 edge = LIBDECOR_RESIZE_EDGE_TOP;
4188 } break;
4189 case DisplayServer::WINDOW_EDGE_TOP_RIGHT: {
4190 edge = LIBDECOR_RESIZE_EDGE_TOP_RIGHT;
4191 } break;
4192 case DisplayServer::WINDOW_EDGE_LEFT: {
4193 edge = LIBDECOR_RESIZE_EDGE_LEFT;
4194 } break;
4195 case DisplayServer::WINDOW_EDGE_RIGHT: {
4196 edge = LIBDECOR_RESIZE_EDGE_RIGHT;
4197 } break;
4198 case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: {
4199 edge = LIBDECOR_RESIZE_EDGE_BOTTOM_LEFT;
4200 } break;
4201 case DisplayServer::WINDOW_EDGE_BOTTOM: {
4202 edge = LIBDECOR_RESIZE_EDGE_BOTTOM;
4203 } break;
4204 case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: {
4205 edge = LIBDECOR_RESIZE_EDGE_BOTTOM_RIGHT;
4206 } break;
4207 default:
4208 break;
4209 }
4210 libdecor_frame_resize(ws.libdecor_frame, ss->wl_seat, ss->pointer_data.button_serial, edge);
4211 }
4212 #endif
4213 }
4214
4215 void WaylandThread::window_set_parent(DisplayServer::WindowID p_window_id, DisplayServer::WindowID p_parent_id) {
4216 ERR_FAIL_COND(!windows.has(p_window_id));
4217 ERR_FAIL_COND(!windows.has(p_parent_id));
4218
4219 WindowState &child = windows[p_window_id];
4220 child.parent_id = p_parent_id;
4221
4222 WindowState &parent = windows[p_parent_id];
4223
4224 // NOTE: We can't really unparent as, at the time of writing, libdecor
4225 // segfaults when trying to set a null parent. Hopefully unparenting is not
4226 // that common. Bummer.
4227
4228 #ifdef LIBDECOR_ENABLED
4229 if (child.libdecor_frame && parent.libdecor_frame) {
4230 libdecor_frame_set_parent(child.libdecor_frame, parent.libdecor_frame);
4231 return;
4232 }
4233 #endif
4234
4235 if (child.xdg_toplevel && parent.xdg_toplevel) {
4236 xdg_toplevel_set_parent(child.xdg_toplevel, parent.xdg_toplevel);
4237 }
4238 }
4239
4240 void WaylandThread::window_set_max_size(DisplayServer::WindowID p_window_id, const Size2i &p_size) {
4241 ERR_FAIL_COND(!windows.has(p_window_id));
4242 WindowState &ws = windows[p_window_id];
4243
4244 Vector2i logical_max_size = scale_vector2i(p_size, 1 / window_state_get_scale_factor(&ws));
4245
4246 if (ws.wl_surface && ws.xdg_toplevel) {
4247 xdg_toplevel_set_max_size(ws.xdg_toplevel, logical_max_size.width, logical_max_size.height);
4248 }
4249
4250 #ifdef LIBDECOR_ENABLED
4251 if (ws.libdecor_frame) {
4252 libdecor_frame_set_max_content_size(ws.libdecor_frame, logical_max_size.width, logical_max_size.height);
4253 }
4254
4255 // FIXME: I'm not sure whether we have to commit the surface for this to apply.
4256 #endif
4257 }
4258
4259 void WaylandThread::window_set_min_size(DisplayServer::WindowID p_window_id, const Size2i &p_size) {
4260 ERR_FAIL_COND(!windows.has(p_window_id));
4261 WindowState &ws = windows[p_window_id];
4262
4263 Size2i logical_min_size = scale_vector2i(p_size, 1 / window_state_get_scale_factor(&ws));
4264
4265 if (ws.wl_surface && ws.xdg_toplevel) {
4266 xdg_toplevel_set_min_size(ws.xdg_toplevel, logical_min_size.width, logical_min_size.height);
4267 }
4268
4269 #ifdef LIBDECOR_ENABLED
4270 if (ws.libdecor_frame) {
4271 libdecor_frame_set_min_content_size(ws.libdecor_frame, logical_min_size.width, logical_min_size.height);
4272 }
4273
4274 // FIXME: I'm not sure whether we have to commit the surface for this to apply.
4275 #endif
4276 }
4277
4278 bool WaylandThread::window_can_set_mode(DisplayServer::WindowID p_window_id, DisplayServer::WindowMode p_window_mode) const {
4279 ERR_FAIL_COND_V(!windows.has(p_window_id), false);
4280 const WindowState &ws = windows[p_window_id];
4281
4282 switch (p_window_mode) {
4283 case DisplayServer::WINDOW_MODE_WINDOWED: {
4284 // Looks like it's guaranteed.
4285 return true;
4286 };
4287
4288 case DisplayServer::WINDOW_MODE_MINIMIZED: {
4289 #ifdef LIBDECOR_ENABLED
4290 if (ws.libdecor_frame) {
4291 return libdecor_frame_has_capability(ws.libdecor_frame, LIBDECOR_ACTION_MINIMIZE);
4292 }
4293 #endif // LIBDECOR_ENABLED
4294
4295 return ws.can_minimize;
4296 };
4297
4298 case DisplayServer::WINDOW_MODE_MAXIMIZED: {
4299 if (ws.libdecor_frame) {
4300 // NOTE: libdecor doesn't seem to have a maximize capability query?
4301 // The fact that there's a fullscreen one makes me suspicious. Anyways,
4302 // let's act as if we always can.
4303 return true;
4304 }
4305 return ws.can_maximize;
4306 };
4307
4308 case DisplayServer::WINDOW_MODE_FULLSCREEN:
4309 case DisplayServer::WINDOW_MODE_EXCLUSIVE_FULLSCREEN: {
4310 #ifdef LIBDECOR_ENABLED
4311 if (ws.libdecor_frame) {
4312 return libdecor_frame_has_capability(ws.libdecor_frame, LIBDECOR_ACTION_FULLSCREEN);
4313 }
4314 #endif // LIBDECOR_ENABLED
4315
4316 return ws.can_fullscreen;
4317 };
4318 }
4319
4320 return false;
4321 }
4322
4323 void WaylandThread::window_try_set_mode(DisplayServer::WindowID p_window_id, DisplayServer::WindowMode p_window_mode) {
4324 ERR_FAIL_COND(!windows.has(p_window_id));
4325 WindowState &ws = windows[p_window_id];
4326
4327 if (ws.mode == p_window_mode) {
4328 return;
4329 }
4330
4331 // Don't waste time with hidden windows and whatnot. Behave like it worked.
4332 #ifdef LIBDECOR_ENABLED
4333 if ((!ws.wl_surface || !ws.xdg_toplevel) && !ws.libdecor_frame) {
4334 #else
4335 if (!ws.wl_surface || !ws.xdg_toplevel) {
4336 #endif // LIBDECOR_ENABLED
4337 ws.mode = p_window_mode;
4338 return;
4339 }
4340
4341 // Return back to a windowed state so that we can apply what the user asked.
4342 switch (ws.mode) {
4343 case DisplayServer::WINDOW_MODE_WINDOWED: {
4344 // Do nothing.
4345 } break;
4346
4347 case DisplayServer::WINDOW_MODE_MINIMIZED: {
4348 // We can't do much according to the xdg_shell protocol. I have no idea
4349 // whether this implies that we should return or who knows what. For now
4350 // we'll do nothing.
4351 // TODO: Test this properly.
4352 } break;
4353
4354 case DisplayServer::WINDOW_MODE_MAXIMIZED: {
4355 // Try to unmaximize. This isn't garaunteed to work actually, so we'll have
4356 // to check whether something changed.
4357 if (ws.xdg_toplevel) {
4358 xdg_toplevel_unset_maximized(ws.xdg_toplevel);
4359 }
4360
4361 #ifdef LIBDECOR_ENABLED
4362 if (ws.libdecor_frame) {
4363 libdecor_frame_unset_maximized(ws.libdecor_frame);
4364 }
4365 #endif // LIBDECOR_ENABLED
4366 } break;
4367
4368 case DisplayServer::WINDOW_MODE_FULLSCREEN:
4369 case DisplayServer::WINDOW_MODE_EXCLUSIVE_FULLSCREEN: {
4370 // Same thing as above, unset fullscreen and check later if it worked.
4371 if (ws.xdg_toplevel) {
4372 xdg_toplevel_unset_fullscreen(ws.xdg_toplevel);
4373 }
4374
4375 #ifdef LIBDECOR_ENABLED
4376 if (ws.libdecor_frame) {
4377 libdecor_frame_unset_fullscreen(ws.libdecor_frame);
4378 }
4379 #endif // LIBDECOR_ENABLED
4380 } break;
4381 }
4382 // Roundtrip and hope that something changed.
4383 // TODO: Async?
4384 wl_display_roundtrip(wl_display);
4385
4386 if (ws.mode != DisplayServer::WINDOW_MODE_WINDOWED) {
4387 // The compositor refused our "normalization" request. It'd be useless or
4388 // unpredictable to attempt setting a new state. We're done.
4389 return;
4390 }
4391
4392 // Ask the compositor to set the state indicated by the new mode.
4393 switch (p_window_mode) {
4394 case DisplayServer::WINDOW_MODE_WINDOWED: {
4395 // Do nothing. We're already windowed.
4396 } break;
4397
4398 case DisplayServer::WINDOW_MODE_MINIMIZED: {
4399 if (!window_can_set_mode(p_window_id, p_window_mode)) {
4400 // Minimization is special (read below). Better not mess with it if the
4401 // compositor explicitly announces that it doesn't support it.
4402 break;
4403 }
4404
4405 if (ws.xdg_toplevel) {
4406 xdg_toplevel_set_minimized(ws.xdg_toplevel);
4407 }
4408
4409 #ifdef LIBDECOR_ENABLED
4410 if (ws.libdecor_frame) {
4411 libdecor_frame_set_minimized(ws.libdecor_frame);
4412 }
4413 #endif // LIBDECOR_ENABLED
4414 // We have no way to actually detect this state, so we'll have to report it
4415 // manually to the engine (hoping that it worked). In the worst case it'll
4416 // get reset by the next configure event.
4417 ws.mode = DisplayServer::WINDOW_MODE_MINIMIZED;
4418 } break;
4419
4420 case DisplayServer::WINDOW_MODE_MAXIMIZED: {
4421 if (ws.xdg_toplevel) {
4422 xdg_toplevel_set_maximized(ws.xdg_toplevel);
4423 }
4424
4425 #ifdef LIBDECOR_ENABLED
4426 if (ws.libdecor_frame) {
4427 libdecor_frame_set_maximized(ws.libdecor_frame);
4428 }
4429 #endif // LIBDECOR_ENABLED
4430 } break;
4431
4432 case DisplayServer::WINDOW_MODE_FULLSCREEN:
4433 case DisplayServer::WINDOW_MODE_EXCLUSIVE_FULLSCREEN: {
4434 if (ws.xdg_toplevel) {
4435 xdg_toplevel_set_fullscreen(ws.xdg_toplevel, nullptr);
4436 }
4437
4438 #ifdef LIBDECOR_ENABLED
4439 if (ws.libdecor_frame) {
4440 libdecor_frame_set_fullscreen(ws.libdecor_frame, nullptr);
4441 }
4442 #endif // LIBDECOR_ENABLED
4443 } break;
4444
4445 default: {
4446 } break;
4447 }
4448 }
4449
4450 void WaylandThread::window_set_borderless(DisplayServer::WindowID p_window_id, bool p_borderless) {
4451 ERR_FAIL_COND(!windows.has(p_window_id));
4452 WindowState &ws = windows[p_window_id];
4453
4454 if (ws.xdg_toplevel_decoration) {
4455 if (p_borderless) {
4456 // We implement borderless windows by simply asking the compositor to let
4457 // us handle decorations (we don't).
4458 zxdg_toplevel_decoration_v1_set_mode(ws.xdg_toplevel_decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE);
4459 } else {
4460 zxdg_toplevel_decoration_v1_set_mode(ws.xdg_toplevel_decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
4461 }
4462 }
4463
4464 #ifdef LIBDECOR_ENABLED
4465 if (ws.libdecor_frame) {
4466 bool visible_current = libdecor_frame_is_visible(ws.libdecor_frame);
4467 bool visible_target = !p_borderless;
4468
4469 // NOTE: We have to do this otherwise we trip on a libdecor bug where it's
4470 // possible to destroy the frame more than once, by setting the visibility
4471 // to false multiple times and thus crashing.
4472 if (visible_current != visible_target) {
4473 print_verbose(vformat("Setting libdecor frame visibility to %s", visible_target));
4474 libdecor_frame_set_visibility(ws.libdecor_frame, visible_target);
4475 }
4476 }
4477 #endif // LIBDECOR_ENABLED
4478 }
4479
4480 void WaylandThread::window_set_title(DisplayServer::WindowID p_window_id, const String &p_title) {
4481 ERR_FAIL_COND(!windows.has(p_window_id));
4482 WindowState &ws = windows[p_window_id];
4483
4484 #ifdef LIBDECOR_ENABLED
4485 if (ws.libdecor_frame) {
4486 libdecor_frame_set_title(ws.libdecor_frame, p_title.utf8().get_data());
4487 }
4488 #endif // LIBDECOR_ENABLE
4489
4490 if (ws.xdg_toplevel) {
4491 xdg_toplevel_set_title(ws.xdg_toplevel, p_title.utf8().get_data());
4492 }
4493 }
4494
4495 void WaylandThread::window_set_app_id(DisplayServer::WindowID p_window_id, const String &p_app_id) {
4496 ERR_FAIL_COND(!windows.has(p_window_id));
4497 WindowState &ws = windows[p_window_id];
4498
4499 #ifdef LIBDECOR_ENABLED
4500 if (ws.libdecor_frame) {
4501 libdecor_frame_set_app_id(ws.libdecor_frame, p_app_id.utf8().get_data());
4502 return;
4503 }
4504 #endif // LIBDECOR_ENABLED
4505
4506 if (ws.xdg_toplevel) {
4507 xdg_toplevel_set_app_id(ws.xdg_toplevel, p_app_id.utf8().get_data());
4508 return;
4509 }
4510 }
4511
4512 void WaylandThread::set_icon(const Ref<Image> &p_icon) {
4513 ERR_FAIL_COND(p_icon.is_null());
4514
4515 Size2i icon_size = p_icon->get_size();
4516 ERR_FAIL_COND(icon_size.width != icon_size.height);
4517
4518 if (!registry.xdg_toplevel_icon_manager) {
4519 return;
4520 }
4521
4522 if (xdg_icon) {
4523 xdg_toplevel_icon_v1_destroy(xdg_icon);
4524 }
4525
4526 if (icon_buffer) {
4527 wl_buffer_destroy(icon_buffer);
4528 }
4529
4530 // NOTE: The stride is the width of the icon in bytes.
4531 uint32_t icon_stride = icon_size.width * 4;
4532 uint32_t data_size = icon_stride * icon_size.height;
4533
4534 // We need a shared memory object file descriptor in order to create a
4535 // wl_buffer through wl_shm.
4536 int fd = WaylandThread::_allocate_shm_file(data_size);
4537 ERR_FAIL_COND(fd == -1);
4538
4539 uint32_t *buffer_data = (uint32_t *)mmap(nullptr, data_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
4540
4541 // Create the Wayland buffer.
4542 struct wl_shm_pool *shm_pool = wl_shm_create_pool(registry.wl_shm, fd, data_size);
4543 icon_buffer = wl_shm_pool_create_buffer(shm_pool, 0, icon_size.width, icon_size.height, icon_stride, WL_SHM_FORMAT_ARGB8888);
4544 wl_shm_pool_destroy(shm_pool);
4545
4546 // Fill the cursor buffer with the image data.
4547 for (uint32_t index = 0; index < (uint32_t)(icon_size.width * icon_size.height); index++) {
4548 int row_index = index / icon_size.width;
4549 int column_index = (index % icon_size.width);
4550
4551 buffer_data[index] = p_icon->get_pixel(column_index, row_index).to_argb32();
4552
4553 // Wayland buffers, unless specified, require associated alpha, so we'll just
4554 // associate the alpha in-place.
4555 uint8_t *pixel_data = (uint8_t *)&buffer_data[index];
4556 pixel_data[0] = pixel_data[0] * pixel_data[3] / 255;
4557 pixel_data[1] = pixel_data[1] * pixel_data[3] / 255;
4558 pixel_data[2] = pixel_data[2] * pixel_data[3] / 255;
4559 }
4560
4561 xdg_icon = xdg_toplevel_icon_manager_v1_create_icon(registry.xdg_toplevel_icon_manager);
4562 xdg_toplevel_icon_v1_add_buffer(xdg_icon, icon_buffer, icon_size.width);
4563
4564 if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {
4565 // Setting a name allows the godot icon to be overridden by a system theme.
4566 // We only want the project manager and editor to get themed,
4567 // Games will get icons with the protocol and themed icons with .desktop entries.
4568 // NOTE: should be synced with the icon name in misc/dist/linuxbsd/Godot.desktop
4569 xdg_toplevel_icon_v1_set_name(xdg_icon, "godot");
4570 }
4571
4572 for (KeyValue<DisplayServer::WindowID, WindowState> &pair : windows) {
4573 WindowState &ws = pair.value;
4574 #ifdef LIBDECOR_ENABLED
4575 if (ws.libdecor_frame) {
4576 xdg_toplevel *toplevel = libdecor_frame_get_xdg_toplevel(ws.libdecor_frame);
4577 ERR_FAIL_NULL(toplevel);
4578 xdg_toplevel_icon_manager_v1_set_icon(registry.xdg_toplevel_icon_manager, toplevel, xdg_icon);
4579 }
4580 #endif
4581 if (ws.xdg_toplevel) {
4582 xdg_toplevel_icon_manager_v1_set_icon(registry.xdg_toplevel_icon_manager, ws.xdg_toplevel, xdg_icon);
4583 }
4584 }
4585 }
4586
4587 DisplayServer::WindowMode WaylandThread::window_get_mode(DisplayServer::WindowID p_window_id) const {
4588 ERR_FAIL_COND_V(!windows.has(p_window_id), DisplayServer::WINDOW_MODE_WINDOWED);
4589 const WindowState &ws = windows[p_window_id];
4590
4591 return ws.mode;
4592 }
4593
4594 void WaylandThread::window_request_attention(DisplayServer::WindowID p_window_id) {
4595 ERR_FAIL_COND(!windows.has(p_window_id));
4596 WindowState &ws = windows[p_window_id];
4597
4598 if (registry.xdg_activation) {
4599 // Window attention requests are done through the XDG activation protocol.
4600 xdg_activation_token_v1 *xdg_activation_token = xdg_activation_v1_get_activation_token(registry.xdg_activation);
4601 xdg_activation_token_v1_add_listener(xdg_activation_token, &xdg_activation_token_listener, &ws);
4602 xdg_activation_token_v1_commit(xdg_activation_token);
4603 }
4604 }
4605
4606 void WaylandThread::window_set_idle_inhibition(DisplayServer::WindowID p_window_id, bool p_enable) {
4607 ERR_FAIL_COND(!windows.has(p_window_id));
4608 WindowState &ws = windows[p_window_id];
4609
4610 if (p_enable) {
4611 if (ws.registry->wp_idle_inhibit_manager && !ws.wp_idle_inhibitor) {
4612 ERR_FAIL_NULL(ws.wl_surface);
4613 ws.wp_idle_inhibitor = zwp_idle_inhibit_manager_v1_create_inhibitor(ws.registry->wp_idle_inhibit_manager, ws.wl_surface);
4614 }
4615 } else {
4616 if (ws.wp_idle_inhibitor) {
4617 zwp_idle_inhibitor_v1_destroy(ws.wp_idle_inhibitor);
4618 ws.wp_idle_inhibitor = nullptr;
4619 }
4620 }
4621 }
4622
4623 bool WaylandThread::window_get_idle_inhibition(DisplayServer::WindowID p_window_id) const {
4624 ERR_FAIL_COND_V(!windows.has(p_window_id), false);
4625 const WindowState &ws = windows[p_window_id];
4626
4627 return ws.wp_idle_inhibitor != nullptr;
4628 }
4629
4630 WaylandThread::ScreenData WaylandThread::screen_get_data(int p_screen) const {
4631 ERR_FAIL_INDEX_V(p_screen, registry.wl_outputs.size(), ScreenData());
4632
4633 return wl_output_get_screen_state(registry.wl_outputs.get(p_screen))->data;
4634 }
4635
4636 int WaylandThread::get_screen_count() const {
4637 return registry.wl_outputs.size();
4638 }
4639
4640 DisplayServer::WindowID WaylandThread::pointer_get_pointed_window_id() const {
4641 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
4642
4643 if (ss) {
4644 // Let's determine the most recently used tablet tool.
4645 TabletToolState *max_ts = nullptr;
4646 for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
4647 TabletToolState *ts = wp_tablet_tool_get_state(tool);
4648 ERR_CONTINUE(ts == nullptr);
4649
4650 TabletToolData &td = ts->data;
4651
4652 if (!max_ts) {
4653 max_ts = ts;
4654 continue;
4655 }
4656
4657 if (MAX(td.button_time, td.motion_time) > MAX(max_ts->data.button_time, max_ts->data.motion_time)) {
4658 max_ts = ts;
4659 }
4660 }
4661
4662 const PointerData &pd = ss->pointer_data;
4663
4664 if (max_ts) {
4665 TabletToolData &td = max_ts->data;
4666 if (MAX(td.button_time, td.motion_time) > MAX(pd.button_time, pd.motion_time)) {
4667 return td.proximal_id;
4668 }
4669 }
4670
4671 return ss->pointer_data.pointed_id;
4672 }
4673
4674 return DisplayServer::INVALID_WINDOW_ID;
4675 }
4676 DisplayServer::WindowID WaylandThread::pointer_get_last_pointed_window_id() const {
4677 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
4678
4679 if (ss) {
4680 // Let's determine the most recently used tablet tool.
4681 TabletToolState *max_ts = nullptr;
4682 for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
4683 TabletToolState *ts = wp_tablet_tool_get_state(tool);
4684 ERR_CONTINUE(ts == nullptr);
4685
4686 TabletToolData &td = ts->data;
4687
4688 if (!max_ts) {
4689 max_ts = ts;
4690 continue;
4691 }
4692
4693 if (MAX(td.button_time, td.motion_time) > MAX(max_ts->data.button_time, max_ts->data.motion_time)) {
4694 max_ts = ts;
4695 }
4696 }
4697
4698 const PointerData &pd = ss->pointer_data;
4699
4700 if (max_ts) {
4701 TabletToolData &td = max_ts->data;
4702 if (MAX(td.button_time, td.motion_time) > MAX(pd.button_time, pd.motion_time)) {
4703 return td.last_proximal_id;
4704 }
4705 }
4706
4707 return ss->pointer_data.last_pointed_id;
4708 }
4709
4710 return DisplayServer::INVALID_WINDOW_ID;
4711 }
4712
4713 void WaylandThread::pointer_set_constraint(PointerConstraint p_constraint) {
4714 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
4715
4716 if (ss) {
4717 seat_state_unlock_pointer(ss);
4718
4719 if (p_constraint == PointerConstraint::LOCKED) {
4720 seat_state_lock_pointer(ss);
4721 } else if (p_constraint == PointerConstraint::CONFINED) {
4722 seat_state_confine_pointer(ss);
4723 }
4724 }
4725
4726 pointer_constraint = p_constraint;
4727 }
4728
4729 void WaylandThread::pointer_set_hint(const Point2i &p_hint) {
4730 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
4731 if (!ss) {
4732 return;
4733 }
4734
4735 WindowState *ws = window_get_state(ss->pointer_data.pointed_id);
4736
4737 int hint_x = 0;
4738 int hint_y = 0;
4739
4740 if (ws) {
4741 // NOTE: It looks like it's not really recommended to convert from
4742 // "godot-space" to "wayland-space" and in general I received mixed feelings
4743 // discussing about this. I'm not really sure about the maths behind this but,
4744 // oh well, we're setting a cursor hint. ¯\_(ツ)_/¯
4745 // See: https://oftc.irclog.whitequark.org/wayland/2023-08-23#1692756914-1692816818
4746 hint_x = std::round(p_hint.x / window_state_get_scale_factor(ws));
4747 hint_y = std::round(p_hint.y / window_state_get_scale_factor(ws));
4748 }
4749
4750 if (ss) {
4751 seat_state_set_hint(ss, hint_x, hint_y);
4752 }
4753 }
4754
4755 WaylandThread::PointerConstraint WaylandThread::pointer_get_constraint() const {
4756 return pointer_constraint;
4757 }
4758
4759 BitField<MouseButtonMask> WaylandThread::pointer_get_button_mask() const {
4760 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
4761
4762 if (ss) {
4763 return ss->pointer_data.pressed_button_mask;
4764 }
4765
4766 return BitField<MouseButtonMask>();
4767 }
4768
4769 Error WaylandThread::init() {
4770 #ifdef SOWRAP_ENABLED
4771 #ifdef DEBUG_ENABLED
4772 int dylibloader_verbose = 1;
4773 #else
4774 int dylibloader_verbose = 0;
4775 #endif // DEBUG_ENABLED
4776
4777 if (initialize_wayland_client(dylibloader_verbose) != 0) {
4778 WARN_PRINT("Can't load the Wayland client library.");
4779 return ERR_CANT_CREATE;
4780 }
4781
4782 if (initialize_wayland_cursor(dylibloader_verbose) != 0) {
4783 WARN_PRINT("Can't load the Wayland cursor library.");
4784 return ERR_CANT_CREATE;
4785 }
4786
4787 if (initialize_xkbcommon(dylibloader_verbose) != 0) {
4788 WARN_PRINT("Can't load the XKBcommon library.");
4789 return ERR_CANT_CREATE;
4790 }
4791 #endif // SOWRAP_ENABLED
4792
4793 KeyMappingXKB::initialize();
4794
4795 String embedder_socket_path;
4796
4797 #ifdef TOOLS_ENABLED
4798 bool embedder_enabled = true;
4799
4800 if (OS::get_singleton()->get_environment("GODOT_WAYLAND_DISABLE_EMBEDDER") == "1") {
4801 print_verbose("Disabling Wayland embedder as per GODOT_WAYLAND_DISABLE_EMBEDDER.");
4802 embedder_enabled = false;
4803 }
4804
4805 if (embedder_enabled && Engine::get_singleton()->is_editor_hint() && !Engine::get_singleton()->is_project_manager_hint()) {
4806 print_verbose("Initializing Wayland embedder.");
4807 Error embedder_status = embedder.init();
4808 ERR_FAIL_COND_V_MSG(embedder_status != OK, ERR_CANT_CREATE, "Can't initialize Wayland embedder.");
4809
4810 embedder_socket_path = embedder.get_socket_path();
4811 ERR_FAIL_COND_V_MSG(embedder_socket_path.is_empty(), ERR_CANT_CREATE, "Wayland embedder returned invalid path.");
4812
4813 OS::get_singleton()->set_environment("GODOT_WAYLAND_DISPLAY", embedder_socket_path);
4814 }
4815 #endif // TOOLS_ENABLED
4816
4817 if (Engine::get_singleton()->is_embedded_in_editor()) {
4818 embedder_socket_path = OS::get_singleton()->get_environment("GODOT_WAYLAND_DISPLAY");
4819 #if 0
4820 // Debug
4821 OS::get_singleton()->set_environment("WAYLAND_DEBUG", "1");
4822 int fd = open("/tmp/gdembedded.log", O_CREAT | O_RDWR, 0666);
4823 dup2(fd, 1);
4824 dup2(fd, 2);
4825 #endif
4826 }
4827
4828 if (embedder_socket_path.is_empty()) {
4829 print_verbose("Connecting to the default Wayland display.");
4830 wl_display = wl_display_connect(nullptr);
4831 } else {
4832 print_verbose("Connecting to the Wayland embedder display.");
4833 wl_display = wl_display_connect(embedder_socket_path.utf8().get_data());
4834 }
4835
4836 ERR_FAIL_NULL_V_MSG(wl_display, ERR_CANT_CREATE, "Can't connect to a Wayland display.");
4837
4838 thread_data.wl_display = wl_display;
4839
4840 #ifdef LIBDECOR_ENABLED
4841 bool libdecor_found = true;
4842
4843 bool skip_libdecor = OS::get_singleton()->get_environment("GODOT_WAYLAND_DISABLE_LIBDECOR") == "1";
4844
4845 #ifdef SOWRAP_ENABLED
4846 if (!skip_libdecor && initialize_libdecor(dylibloader_verbose) != 0) {
4847 libdecor_found = false;
4848 }
4849 #endif // SOWRAP_ENABLED
4850
4851 if (skip_libdecor) {
4852 print_verbose("Skipping libdecor check because GODOT_WAYLAND_DISABLE_LIBDECOR is set to 1.");
4853 } else {
4854 if (libdecor_found) {
4855 libdecor_context = libdecor_new(wl_display, (struct libdecor_interface *)&libdecor_interface);
4856 } else {
4857 print_verbose("libdecor not found. Client-side decorations disabled.");
4858 }
4859 }
4860 #endif // LIBDECOR_ENABLED
4861
4862 wl_registry = wl_display_get_registry(wl_display);
4863
4864 ERR_FAIL_NULL_V_MSG(wl_registry, ERR_UNAVAILABLE, "Can't obtain the Wayland registry global.");
4865
4866 registry.wayland_thread = this;
4867
4868 wl_registry_add_listener(wl_registry, &wl_registry_listener, &registry);
4869
4870 // Wait for registry to get notified from the compositor.
4871 // TODO: Async?
4872 wl_display_roundtrip(wl_display);
4873
4874 ERR_FAIL_NULL_V_MSG(registry.wl_shm, ERR_UNAVAILABLE, "Can't obtain the Wayland shared memory global.");
4875 ERR_FAIL_NULL_V_MSG(registry.wl_compositor, ERR_UNAVAILABLE, "Can't obtain the Wayland compositor global.");
4876 ERR_FAIL_NULL_V_MSG(registry.xdg_wm_base, ERR_UNAVAILABLE, "Can't obtain the Wayland XDG shell global.");
4877
4878 // Embedded games can't access the decoration and icon protocol.
4879 if (!Engine::get_singleton()->is_embedded_in_editor()) {
4880 if (!registry.xdg_decoration_manager) {
4881 #ifdef LIBDECOR_ENABLED
4882 WARN_PRINT("Can't obtain the XDG decoration manager. Libdecor will be used for drawing CSDs, if available.");
4883 #else
4884 WARN_PRINT("Can't obtain the XDG decoration manager. Decorations won't show up.");
4885 #endif // LIBDECOR_ENABLED
4886 }
4887
4888 if (!registry.xdg_toplevel_icon_manager_name) {
4889 WARN_PRINT("xdg-toplevel-icon protocol not found! Cannot set window icon.");
4890 }
4891 }
4892
4893 if (!registry.xdg_activation) {
4894 WARN_PRINT("Can't obtain the XDG activation global. Attention requesting won't work!");
4895 }
4896
4897 #ifndef DBUS_ENABLED
4898 if (!registry.wp_idle_inhibit_manager) {
4899 WARN_PRINT("Can't obtain the idle inhibition manager. The screen might turn off even after calling screen_set_keep_on()!");
4900 }
4901 #endif // DBUS_ENABLED
4902
4903 if (!registry.wp_fifo_manager_name) {
4904 WARN_PRINT("FIFO protocol not found! Frame pacing will be degraded.");
4905 }
4906
4907 // Wait for seat capabilities.
4908 // TODO: Async?
4909 wl_display_roundtrip(wl_display);
4910
4911 cursor_theme_name = OS::get_singleton()->get_environment("XCURSOR_THEME");
4912
4913 unscaled_cursor_size = OS::get_singleton()->get_environment("XCURSOR_SIZE").to_int();
4914 if (unscaled_cursor_size <= 0) {
4915 print_verbose("Detected invalid cursor size preference, defaulting to 24.");
4916 unscaled_cursor_size = 24;
4917 }
4918
4919 // NOTE: The scale is useful here as it might've been updated by _update_scale.
4920 bool cursor_theme_loaded = _load_cursor_theme(unscaled_cursor_size * cursor_scale);
4921
4922 if (!cursor_theme_loaded) {
4923 return ERR_CANT_CREATE;
4924 }
4925
4926 // Update the cursor.
4927 cursor_set_shape(DisplayServer::CURSOR_ARROW);
4928
4929 events_thread.start(_poll_events_thread, &thread_data);
4930
4931 initialized = true;
4932 return OK;
4933 }
4934
4935 void WaylandThread::cursor_set_visible(bool p_visible) {
4936 cursor_visible = p_visible;
4937
4938 for (struct wl_seat *wl_seat : registry.wl_seats) {
4939 SeatState *ss = wl_seat_get_seat_state(wl_seat);
4940 ERR_FAIL_NULL(ss);
4941
4942 seat_state_update_cursor(ss);
4943 }
4944 }
4945
4946 void WaylandThread::cursor_set_shape(DisplayServer::CursorShape p_cursor_shape) {
4947 cursor_shape = p_cursor_shape;
4948
4949 for (struct wl_seat *wl_seat : registry.wl_seats) {
4950 SeatState *ss = wl_seat_get_seat_state(wl_seat);
4951 ERR_FAIL_NULL(ss);
4952
4953 seat_state_update_cursor(ss);
4954 }
4955 }
4956
4957 void WaylandThread::cursor_shape_set_custom_image(DisplayServer::CursorShape p_cursor_shape, Ref<Image> p_image, const Point2i &p_hotspot) {
4958 ERR_FAIL_COND(p_image.is_null());
4959
4960 Size2i image_size = p_image->get_size();
4961
4962 // NOTE: The stride is the width of the image in bytes.
4963 unsigned int image_stride = image_size.width * 4;
4964 unsigned int data_size = image_stride * image_size.height;
4965
4966 // We need a shared memory object file descriptor in order to create a
4967 // wl_buffer through wl_shm.
4968 int fd = WaylandThread::_allocate_shm_file(data_size);
4969 ERR_FAIL_COND(fd == -1);
4970
4971 CustomCursor &cursor = custom_cursors[p_cursor_shape];
4972 cursor.hotspot = p_hotspot;
4973
4974 if (cursor.wl_buffer) {
4975 // Clean up the old Wayland buffer.
4976 wl_buffer_destroy(cursor.wl_buffer);
4977 }
4978
4979 if (cursor.buffer_data) {
4980 // Clean up the old buffer data.
4981 munmap(cursor.buffer_data, cursor.buffer_data_size);
4982 }
4983
4984 cursor.buffer_data = (uint32_t *)mmap(nullptr, data_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
4985 cursor.buffer_data_size = data_size;
4986
4987 // Create the Wayland buffer.
4988 struct wl_shm_pool *wl_shm_pool = wl_shm_create_pool(registry.wl_shm, fd, data_size);
4989 // TODO: Make sure that WL_SHM_FORMAT_ARGB8888 format is supported. It
4990 // technically isn't garaunteed to be supported, but I think that'd be a
4991 // pretty unlikely thing to stumble upon.
4992 cursor.wl_buffer = wl_shm_pool_create_buffer(wl_shm_pool, 0, image_size.width, image_size.height, image_stride, WL_SHM_FORMAT_ARGB8888);
4993 wl_shm_pool_destroy(wl_shm_pool);
4994
4995 // Fill the cursor buffer with the image data.
4996 for (unsigned int index = 0; index < (unsigned int)(image_size.width * image_size.height); index++) {
4997 int row_index = std::floor(index / image_size.width);
4998 int column_index = (index % int(image_size.width));
4999
5000 cursor.buffer_data[index] = p_image->get_pixel(column_index, row_index).to_argb32();
5001
5002 // Wayland buffers, unless specified, require associated alpha, so we'll just
5003 // associate the alpha in-place.
5004 uint8_t *pixel_data = (uint8_t *)&cursor.buffer_data[index];
5005 pixel_data[0] = pixel_data[0] * pixel_data[3] / 255;
5006 pixel_data[1] = pixel_data[1] * pixel_data[3] / 255;
5007 pixel_data[2] = pixel_data[2] * pixel_data[3] / 255;
5008 }
5009 }
5010
5011 void WaylandThread::cursor_shape_clear_custom_image(DisplayServer::CursorShape p_cursor_shape) {
5012 if (custom_cursors.has(p_cursor_shape)) {
5013 CustomCursor cursor = custom_cursors[p_cursor_shape];
5014 custom_cursors.erase(p_cursor_shape);
5015
5016 if (cursor.wl_buffer) {
5017 wl_buffer_destroy(cursor.wl_buffer);
5018 }
5019
5020 if (cursor.buffer_data) {
5021 munmap(cursor.buffer_data, cursor.buffer_data_size);
5022 }
5023 }
5024 }
5025
5026 void WaylandThread::window_set_ime_active(const bool p_active, DisplayServer::WindowID p_window_id) {
5027 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
5028
5029 if (ss && ss->wp_text_input && ss->ime_enabled) {
5030 if (p_active) {
5031 ss->ime_active = true;
5032 zwp_text_input_v3_enable(ss->wp_text_input);
5033 zwp_text_input_v3_set_cursor_rectangle(ss->wp_text_input, ss->ime_rect.position.x, ss->ime_rect.position.y, ss->ime_rect.size.x, ss->ime_rect.size.y);
5034 } else {
5035 ss->ime_active = false;
5036 ss->ime_text = String();
5037 ss->ime_text_commit = String();
5038 ss->ime_cursor = Vector2i();
5039 zwp_text_input_v3_disable(ss->wp_text_input);
5040 }
5041 zwp_text_input_v3_commit(ss->wp_text_input);
5042 }
5043 }
5044
5045 void WaylandThread::window_set_ime_position(const Point2i &p_pos, DisplayServer::WindowID p_window_id) {
5046 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
5047
5048 if (ss && ss->wp_text_input && ss->ime_enabled) {
5049 ss->ime_rect = Rect2i(p_pos, Size2i(1, 10));
5050 zwp_text_input_v3_set_cursor_rectangle(ss->wp_text_input, ss->ime_rect.position.x, ss->ime_rect.position.y, ss->ime_rect.size.x, ss->ime_rect.size.y);
5051 zwp_text_input_v3_commit(ss->wp_text_input);
5052 }
5053 }
5054
5055 int WaylandThread::keyboard_get_layout_count() const {
5056 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
5057
5058 if (ss && ss->xkb_keymap) {
5059 return xkb_keymap_num_layouts(ss->xkb_keymap);
5060 }
5061
5062 return 0;
5063 }
5064
5065 int WaylandThread::keyboard_get_current_layout_index() const {
5066 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
5067
5068 if (ss) {
5069 return ss->current_layout_index;
5070 }
5071
5072 return 0;
5073 }
5074
5075 void WaylandThread::keyboard_set_current_layout_index(int p_index) {
5076 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
5077
5078 if (ss) {
5079 ss->current_layout_index = p_index;
5080 }
5081 }
5082
5083 String WaylandThread::keyboard_get_layout_name(int p_index) const {
5084 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
5085
5086 if (ss && ss->xkb_keymap) {
5087 return String::utf8(xkb_keymap_layout_get_name(ss->xkb_keymap, p_index));
5088 }
5089
5090 return "";
5091 }
5092
5093 Key WaylandThread::keyboard_get_key_from_physical(Key p_key) const {
5094 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
5095
5096 if (ss && ss->xkb_state) {
5097 Key modifiers = p_key & KeyModifierMask::MODIFIER_MASK;
5098 Key keycode_no_mod = p_key & KeyModifierMask::CODE_MASK;
5099
5100 xkb_keycode_t xkb_keycode = KeyMappingXKB::get_xkb_keycode(keycode_no_mod);
5101 Key key = KeyMappingXKB::get_keycode(xkb_state_key_get_one_sym(ss->xkb_state, xkb_keycode));
5102 return (Key)(key | modifiers);
5103 }
5104
5105 return p_key;
5106 }
5107
5108 Key WaylandThread::keyboard_get_label_from_physical(Key p_key) const {
5109 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
5110
5111 if (ss && ss->xkb_state) {
5112 Key modifiers = p_key & KeyModifierMask::MODIFIER_MASK;
5113 Key keycode_no_mod = p_key & KeyModifierMask::CODE_MASK;
5114
5115 xkb_keycode_t xkb_keycode = KeyMappingXKB::get_xkb_keycode(keycode_no_mod);
5116 xkb_keycode_t xkb_keysym = xkb_state_key_get_one_sym(ss->xkb_state, xkb_keycode);
5117 char32_t chr = xkb_keysym_to_utf32(xkb_keysym_to_upper(xkb_keysym));
5118 if (chr != 0) {
5119 String keysym = String::chr(chr);
5120 Key key = fix_key_label(keysym[0], KeyMappingXKB::get_keycode(xkb_keysym));
5121 return (Key)(key | modifiers);
5122 }
5123 }
5124
5125 return p_key;
5126 }
5127
5128 void WaylandThread::keyboard_echo_keys() {
5129 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
5130
5131 if (ss) {
5132 seat_state_echo_keys(ss);
5133 }
5134 }
5135
5136 void WaylandThread::selection_set_text(const String &p_text) {
5137 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
5138
5139 if (registry.wl_data_device_manager == nullptr) {
5140 DEBUG_LOG_WAYLAND_THREAD("Couldn't set selection, wl_data_device_manager global not available.");
5141 return;
5142 }
5143
5144 if (ss == nullptr) {
5145 DEBUG_LOG_WAYLAND_THREAD("Couldn't set selection, current seat not set.");
5146 return;
5147 }
5148
5149 if (ss->wl_data_device == nullptr) {
5150 DEBUG_LOG_WAYLAND_THREAD("Couldn't set selection, seat doesn't have wl_data_device.");
5151 return;
5152 }
5153
5154 ss->selection_data = p_text.to_utf8_buffer();
5155
5156 if (ss->wl_data_source_selection != nullptr) {
5157 wl_data_source_destroy(ss->wl_data_source_selection);
5158 ss->wl_data_source_selection = nullptr;
5159 }
5160
5161 ss->wl_data_source_selection = wl_data_device_manager_create_data_source(registry.wl_data_device_manager);
5162 wl_data_source_add_listener(ss->wl_data_source_selection, &wl_data_source_listener, ss);
5163 wl_data_source_offer(ss->wl_data_source_selection, "text/plain;charset=utf-8");
5164 wl_data_source_offer(ss->wl_data_source_selection, "text/plain");
5165
5166 // TODO: Implement a good way of getting the latest serial from the user.
5167 wl_data_device_set_selection(ss->wl_data_device, ss->wl_data_source_selection, MAX(ss->pointer_data.button_serial, ss->last_key_pressed_serial));
5168
5169 // Wait for the message to get to the server before continuing, otherwise the
5170 // clipboard update might come with a delay.
5171 wl_display_roundtrip(wl_display);
5172 }
5173
5174 bool WaylandThread::selection_has_mime(const String &p_mime) const {
5175 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
5176
5177 if (ss == nullptr) {
5178 DEBUG_LOG_WAYLAND_THREAD("Couldn't get selection, current seat not set.");
5179 return false;
5180 }
5181
5182 OfferState *os = wl_data_offer_get_offer_state(ss->wl_data_offer_selection);
5183 if (!os) {
5184 return false;
5185 }
5186
5187 return os->mime_types.has(p_mime);
5188 }
5189
5190 Vector<uint8_t> WaylandThread::selection_get_mime(const String &p_mime) const {
5191 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
5192 if (ss == nullptr) {
5193 DEBUG_LOG_WAYLAND_THREAD("Couldn't get selection, current seat not set.");
5194 return Vector<uint8_t>();
5195 }
5196
5197 if (ss->wl_data_source_selection) {
5198 // We have a source so the stuff we're pasting is ours. We'll have to pass the
5199 // data directly or we'd stall waiting for Godot (ourselves) to send us the
5200 // data :P
5201
5202 OfferState *os = wl_data_offer_get_offer_state(ss->wl_data_offer_selection);
5203 ERR_FAIL_NULL_V(os, Vector<uint8_t>());
5204
5205 if (os->mime_types.has(p_mime)) {
5206 // All righty, we're offering this type. Let's just return the data as is.
5207 return ss->selection_data;
5208 }
5209
5210 // ... we don't offer that type. Oh well.
5211 return Vector<uint8_t>();
5212 }
5213
5214 return _wl_data_offer_read(wl_display, p_mime.utf8().get_data(), ss->wl_data_offer_selection);
5215 }
5216
5217 bool WaylandThread::primary_has_mime(const String &p_mime) const {
5218 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
5219
5220 if (ss == nullptr) {
5221 DEBUG_LOG_WAYLAND_THREAD("Couldn't get selection, current seat not set.");
5222 return false;
5223 }
5224
5225 OfferState *os = wp_primary_selection_offer_get_offer_state(ss->wp_primary_selection_offer);
5226 if (!os) {
5227 return false;
5228 }
5229
5230 return os->mime_types.has(p_mime);
5231 }
5232
5233 Vector<uint8_t> WaylandThread::primary_get_mime(const String &p_mime) const {
5234 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
5235 if (ss == nullptr) {
5236 DEBUG_LOG_WAYLAND_THREAD("Couldn't get primary, current seat not set.");
5237 return Vector<uint8_t>();
5238 }
5239
5240 if (ss->wp_primary_selection_source) {
5241 // We have a source so the stuff we're pasting is ours. We'll have to pass the
5242 // data directly or we'd stall waiting for Godot (ourselves) to send us the
5243 // data :P
5244
5245 OfferState *os = wp_primary_selection_offer_get_offer_state(ss->wp_primary_selection_offer);
5246 ERR_FAIL_NULL_V(os, Vector<uint8_t>());
5247
5248 if (os->mime_types.has(p_mime)) {
5249 // All righty, we're offering this type. Let's just return the data as is.
5250 return ss->primary_data;
5251 }
5252
5253 // ... we don't offer that type. Oh well.
5254 return Vector<uint8_t>();
5255 }
5256
5257 return _wp_primary_selection_offer_read(wl_display, p_mime.utf8().get_data(), ss->wp_primary_selection_offer);
5258 }
5259
5260 void WaylandThread::primary_set_text(const String &p_text) {
5261 SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
5262
5263 if (registry.wp_primary_selection_device_manager == nullptr) {
5264 DEBUG_LOG_WAYLAND_THREAD("Couldn't set primary, protocol not available.");
5265 return;
5266 }
5267
5268 if (ss == nullptr) {
5269 DEBUG_LOG_WAYLAND_THREAD("Couldn't set primary, current seat not set.");
5270 return;
5271 }
5272
5273 if (ss->wp_primary_selection_device == nullptr) {
5274 DEBUG_LOG_WAYLAND_THREAD("Couldn't set primary selection, seat doesn't have wp_primary_selection_device.");
5275 return;
5276 }
5277
5278 ss->primary_data = p_text.to_utf8_buffer();
5279
5280 if (ss->wp_primary_selection_source != nullptr) {
5281 zwp_primary_selection_source_v1_destroy(ss->wp_primary_selection_source);
5282 ss->wp_primary_selection_source = nullptr;
5283 }
5284
5285 ss->wp_primary_selection_source = zwp_primary_selection_device_manager_v1_create_source(registry.wp_primary_selection_device_manager);
5286 zwp_primary_selection_source_v1_add_listener(ss->wp_primary_selection_source, &wp_primary_selection_source_listener, ss);
5287 zwp_primary_selection_source_v1_offer(ss->wp_primary_selection_source, "text/plain;charset=utf-8");
5288 zwp_primary_selection_source_v1_offer(ss->wp_primary_selection_source, "text/plain");
5289
5290 // TODO: Implement a good way of getting the latest serial from the user.
5291 zwp_primary_selection_device_v1_set_selection(ss->wp_primary_selection_device, ss->wp_primary_selection_source, MAX(ss->pointer_data.button_serial, ss->last_key_pressed_serial));
5292
5293 // Wait for the message to get to the server before continuing, otherwise the
5294 // clipboard update might come with a delay.
5295 wl_display_roundtrip(wl_display);
5296 }
5297
5298 void WaylandThread::commit_surfaces() {
5299 for (KeyValue<DisplayServer::WindowID, WindowState> &pair : windows) {
5300 wl_surface_commit(pair.value.wl_surface);
5301 }
5302 }
5303
5304 void WaylandThread::set_frame() {
5305 frame = true;
5306 }
5307
5308 bool WaylandThread::get_reset_frame() {
5309 bool old_frame = frame;
5310 frame = false;
5311
5312 return old_frame;
5313 }
5314
5315 // Wraps around `wl_display_dispatch_pending`. Judging from the docs, this
5316 // should work pretty much like `wl_display_dispatch_timeout`, which is only
5317 // available on somewhat recent versions of libwayland.
5318 //
5319 // This implementation is NOT based on libwayland's code.
5320 int WaylandThread::wait_events(int p_timeout_ms) {
5321 struct pollfd poll_fd;
5322 poll_fd.fd = wl_display_get_fd(wl_display);
5323 poll_fd.events = POLLIN | POLLHUP;
5324
5325 while (wl_display_prepare_read(wl_display) != 0) {
5326 // Event queue got already something.
5327 return wl_display_dispatch_pending(wl_display);
5328 }
5329
5330 wl_display_flush(wl_display);
5331
5332 // Wait for the event file descriptor to have new data.
5333 poll(&poll_fd, 1, p_timeout_ms);
5334
5335 if (poll_fd.revents | POLLIN) {
5336 // Load the queues with fresh new data.
5337 wl_display_read_events(wl_display);
5338 } else {
5339 // Oh well... Stop signaling that we want to read.
5340 wl_display_cancel_read(wl_display);
5341
5342 // We've got no new events :(
5343 return 0;
5344 }
5345
5346 // Let's try dispatching now...
5347 return wl_display_dispatch_pending(wl_display);
5348 }
5349
5350 // Dispatches events until a frame event is received, a window is reported as
5351 // suspended or the timeout expires.
5352 bool WaylandThread::wait_frame_suspend_ms(int p_timeout) {
5353 // This is a bit of a chicken and egg thing... Looks like the main event loop
5354 // has to call its rightfully forever-blocking poll right in between
5355 // `wl_display_prepare_read` and `wl_display_read`. This means, that it will
5356 // basically be guaranteed to stay stuck in a "prepare read" state, where it
5357 // will block any other attempt at reading the display fd, such as ours. The
5358 // solution? Let's make sure the mutex is locked (it should)...
5359 MutexLock mutex_lock(mutex);
5360
5361 if (is_suspended()) {
5362 return false;
5363 }
5364
5365 if (frame) {
5366 frame = false;
5367 return true;
5368 }
5369
5370 ERR_FAIL_COND_V(Thread::get_caller_id() == events_thread.get_id(), false);
5371
5372 // ...and unblock the main thread with a roundtrip!
5373 wl_display_roundtrip(wl_display);
5374
5375 if (is_suspended()) {
5376 // All windows are suspended! The compositor is telling us _explicitly_ that
5377 // we don't need to draw, without letting us guess through the frame event's
5378 // timing and stuff like that. Our job here is done.
5379 return false;
5380 }
5381
5382 if (frame) {
5383 // We already have a frame! Probably it got there while the caller locked :D
5384 frame = false;
5385 return true;
5386 }
5387
5388 for (int remaining_ms = p_timeout; remaining_ms > 0;) {
5389 int begin_ms = OS::get_singleton()->get_ticks_msec();
5390
5391 wait_events(remaining_ms);
5392
5393 _wl_display_check_error(wl_display);
5394
5395 if (is_suspended()) {
5396 return false;
5397 }
5398
5399 if (frame) {
5400 frame = false;
5401 return true;
5402 }
5403
5404 remaining_ms -= OS::get_singleton()->get_ticks_msec() - begin_ms;
5405 }
5406
5407 DEBUG_LOG_WAYLAND_THREAD("Frame timeout.");
5408 return false;
5409 }
5410
5411 uint64_t WaylandThread::window_get_last_frame_time(DisplayServer::WindowID p_window_id) const {
5412 ERR_FAIL_COND_V(!windows.has(p_window_id), false);
5413 return windows[p_window_id].last_frame_time;
5414 }
5415
5416 bool WaylandThread::window_is_suspended(DisplayServer::WindowID p_window_id) const {
5417 ERR_FAIL_COND_V(!windows.has(p_window_id), false);
5418 return windows[p_window_id].suspended;
5419 }
5420
5421 bool WaylandThread::is_fifo_available() const {
5422 return registry.wp_fifo_manager_name != 0;
5423 }
5424
5425 bool WaylandThread::is_suspended() const {
5426 for (const KeyValue<DisplayServer::WindowID, WindowState> &E : windows) {
5427 if (!E.value.suspended) {
5428 return false;
5429 }
5430 }
5431
5432 return true;
5433 }
5434
5435 bool WaylandThread::window_wait_ready(DisplayServer::WindowID p_window_id, int p_timeout_ms) {
5436 MutexLock mutex_lock(mutex);
5437
5438 WindowState *ws = windows.getptr(p_window_id);
5439 ERR_FAIL_NULL_V(ws, false);
5440
5441 if (ws->ready) {
5442 return true;
5443 }
5444
5445 // See _poll_events_thread.
5446 ERR_FAIL_COND_V(Thread::get_caller_id() == events_thread.get_id(), ws->ready);
5447
5448 // Unlock the thread.
5449 wl_display_roundtrip(wl_display);
5450
5451 if (ws->ready) {
5452 return true;
5453 }
5454
5455 for (int remaining_ms = p_timeout_ms; remaining_ms > 0;) {
5456 int begin_ms = OS::get_singleton()->get_ticks_msec();
5457
5458 wait_events(remaining_ms);
5459
5460 _wl_display_check_error(wl_display);
5461
5462 if (ws->ready) {
5463 return true;
5464 }
5465
5466 remaining_ms -= OS::get_singleton()->get_ticks_msec() - begin_ms;
5467 }
5468
5469 return false;
5470 }
5471
5472 struct godot_embedding_compositor *WaylandThread::get_embedding_compositor() {
5473 return registry.godot_embedding_compositor;
5474 }
5475
5476 OS::ProcessID WaylandThread::embedded_compositor_get_focused_pid() {
5477 EmbeddingCompositorState *ecomp_state = godot_embedding_compositor_get_state(registry.godot_embedding_compositor);
5478 ERR_FAIL_NULL_V(ecomp_state, -1);
5479
5480 return ecomp_state->focused_pid;
5481 }
5482
5483 void WaylandThread::destroy() {
5484 if (!initialized) {
5485 return;
5486 }
5487
5488 if (wl_display && events_thread.is_started()) {
5489 thread_data.thread_done.set();
5490
5491 // By sending a roundtrip message we're unblocking the polling thread so that
5492 // it can realize that it's done and also handle every event that's left.
5493 wl_display_roundtrip(wl_display);
5494
5495 events_thread.wait_to_finish();
5496 }
5497
5498 for (KeyValue<DisplayServer::WindowID, WindowState> &pair : windows) {
5499 WindowState &ws = pair.value;
5500 if (ws.wp_fractional_scale) {
5501 wp_fractional_scale_v1_destroy(ws.wp_fractional_scale);
5502 }
5503
5504 if (ws.wp_viewport) {
5505 wp_viewport_destroy(ws.wp_viewport);
5506 }
5507
5508 if (ws.frame_callback) {
5509 wl_callback_destroy(ws.frame_callback);
5510 }
5511
5512 #ifdef LIBDECOR_ENABLED
5513 if (ws.libdecor_frame) {
5514 libdecor_frame_close(ws.libdecor_frame);
5515 }
5516 #endif // LIBDECOR_ENABLED
5517
5518 if (ws.xdg_toplevel_decoration) {
5519 zxdg_toplevel_decoration_v1_destroy(ws.xdg_toplevel_decoration);
5520 }
5521
5522 if (ws.xdg_toplevel) {
5523 xdg_toplevel_destroy(ws.xdg_toplevel);
5524 }
5525
5526 if (ws.xdg_surface) {
5527 xdg_surface_destroy(ws.xdg_surface);
5528 }
5529
5530 if (ws.wl_surface) {
5531 wl_surface_destroy(ws.wl_surface);
5532 }
5533 }
5534
5535 for (struct wl_seat *wl_seat : registry.wl_seats) {
5536 SeatState *ss = wl_seat_get_seat_state(wl_seat);
5537 ERR_FAIL_NULL(ss);
5538
5539 wl_seat_destroy(wl_seat);
5540
5541 xkb_context_unref(ss->xkb_context);
5542 xkb_state_unref(ss->xkb_state);
5543 xkb_keymap_unref(ss->xkb_keymap);
5544 xkb_compose_table_unref(ss->xkb_compose_table);
5545 xkb_compose_state_unref(ss->xkb_compose_state);
5546
5547 if (ss->wl_keyboard) {
5548 wl_keyboard_destroy(ss->wl_keyboard);
5549 }
5550
5551 if (ss->keymap_buffer) {
5552 munmap((void *)ss->keymap_buffer, ss->keymap_buffer_size);
5553 }
5554
5555 if (ss->wl_pointer) {
5556 wl_pointer_destroy(ss->wl_pointer);
5557 }
5558
5559 if (ss->cursor_frame_callback) {
5560 // We don't need to set a null userdata for safety as the thread is done.
5561 wl_callback_destroy(ss->cursor_frame_callback);
5562 }
5563
5564 if (ss->cursor_surface) {
5565 wl_surface_destroy(ss->cursor_surface);
5566 }
5567
5568 if (ss->wl_data_device) {
5569 wl_data_device_destroy(ss->wl_data_device);
5570 }
5571
5572 if (ss->wp_cursor_shape_device) {
5573 wp_cursor_shape_device_v1_destroy(ss->wp_cursor_shape_device);
5574 }
5575
5576 if (ss->wp_relative_pointer) {
5577 zwp_relative_pointer_v1_destroy(ss->wp_relative_pointer);
5578 }
5579
5580 if (ss->wp_locked_pointer) {
5581 zwp_locked_pointer_v1_destroy(ss->wp_locked_pointer);
5582 }
5583
5584 if (ss->wp_confined_pointer) {
5585 zwp_confined_pointer_v1_destroy(ss->wp_confined_pointer);
5586 }
5587
5588 if (ss->wp_tablet_seat) {
5589 zwp_tablet_seat_v2_destroy(ss->wp_tablet_seat);
5590 }
5591
5592 for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
5593 TabletToolState *state = wp_tablet_tool_get_state(tool);
5594 if (state) {
5595 memdelete(state);
5596 }
5597
5598 zwp_tablet_tool_v2_destroy(tool);
5599 }
5600
5601 if (ss->wp_text_input) {
5602 zwp_text_input_v3_destroy(ss->wp_text_input);
5603 }
5604
5605 memdelete(ss);
5606 }
5607
5608 if (registry.wp_tablet_manager) {
5609 zwp_tablet_manager_v2_destroy(registry.wp_tablet_manager);
5610 }
5611
5612 if (registry.wp_text_input_manager) {
5613 zwp_text_input_manager_v3_destroy(registry.wp_text_input_manager);
5614 }
5615
5616 for (struct wl_output *wl_output : registry.wl_outputs) {
5617 ERR_FAIL_NULL(wl_output);
5618
5619 memdelete(wl_output_get_screen_state(wl_output));
5620 wl_output_destroy(wl_output);
5621 }
5622
5623 if (registry.godot_embedding_compositor) {
5624 EmbeddingCompositorState *es = godot_embedding_compositor_get_state(registry.godot_embedding_compositor);
5625 ERR_FAIL_NULL(es);
5626
5627 es->mapped_clients.clear();
5628
5629 for (struct godot_embedded_client *client : es->clients) {
5630 godot_embedded_client_destroy(client);
5631 }
5632 es->clients.clear();
5633
5634 memdelete(es);
5635
5636 godot_embedding_compositor_destroy(registry.godot_embedding_compositor);
5637 }
5638
5639 if (wl_cursor_theme) {
5640 wl_cursor_theme_destroy(wl_cursor_theme);
5641 }
5642
5643 if (registry.wp_idle_inhibit_manager) {
5644 zwp_idle_inhibit_manager_v1_destroy(registry.wp_idle_inhibit_manager);
5645 }
5646
5647 if (registry.wp_pointer_constraints) {
5648 zwp_pointer_constraints_v1_destroy(registry.wp_pointer_constraints);
5649 }
5650
5651 if (registry.wp_pointer_gestures) {
5652 zwp_pointer_gestures_v1_destroy(registry.wp_pointer_gestures);
5653 }
5654
5655 if (registry.wp_relative_pointer_manager) {
5656 zwp_relative_pointer_manager_v1_destroy(registry.wp_relative_pointer_manager);
5657 }
5658
5659 if (registry.xdg_activation) {
5660 xdg_activation_v1_destroy(registry.xdg_activation);
5661 }
5662
5663 if (registry.xdg_system_bell) {
5664 xdg_system_bell_v1_destroy(registry.xdg_system_bell);
5665 }
5666
5667 if (registry.xdg_toplevel_icon_manager) {
5668 xdg_toplevel_icon_manager_v1_destroy(registry.xdg_toplevel_icon_manager);
5669
5670 if (xdg_icon) {
5671 xdg_toplevel_icon_v1_destroy(xdg_icon);
5672 }
5673
5674 if (icon_buffer) {
5675 wl_buffer_destroy(icon_buffer);
5676 }
5677 }
5678
5679 if (registry.xdg_decoration_manager) {
5680 zxdg_decoration_manager_v1_destroy(registry.xdg_decoration_manager);
5681 }
5682
5683 if (registry.wp_cursor_shape_manager) {
5684 wp_cursor_shape_manager_v1_destroy(registry.wp_cursor_shape_manager);
5685 }
5686
5687 if (registry.wp_fractional_scale_manager) {
5688 wp_fractional_scale_manager_v1_destroy(registry.wp_fractional_scale_manager);
5689 }
5690
5691 if (registry.wp_viewporter) {
5692 wp_viewporter_destroy(registry.wp_viewporter);
5693 }
5694
5695 if (registry.xdg_wm_base) {
5696 xdg_wm_base_destroy(registry.xdg_wm_base);
5697 }
5698
5699 // NOTE: Deprecated.
5700 if (registry.xdg_exporter_v1) {
5701 zxdg_exporter_v1_destroy(registry.xdg_exporter_v1);
5702 }
5703
5704 if (registry.xdg_exporter_v2) {
5705 zxdg_exporter_v2_destroy(registry.xdg_exporter_v2);
5706 }
5707 if (registry.wl_shm) {
5708 wl_shm_destroy(registry.wl_shm);
5709 }
5710
5711 if (registry.wl_compositor) {
5712 wl_compositor_destroy(registry.wl_compositor);
5713 }
5714
5715 if (wl_registry) {
5716 wl_registry_destroy(wl_registry);
5717 }
5718
5719 wl_display_roundtrip(wl_display);
5720
5721 if (wl_display) {
5722 wl_display_disconnect(wl_display);
5723 }
5724 }
5725
5726 #endif // WAYLAND_ENABLED