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