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