forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwayland_thread.cpp
4559 lines (3526 loc) · 141 KB
/
wayland_thread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* wayland_thread.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "wayland_thread.h"
#ifdef WAYLAND_ENABLED
#ifdef __FreeBSD__
#include <dev/evdev/input-event-codes.h>
#else
// Assume Linux.
#include <linux/input-event-codes.h>
#endif
// For the actual polling thread.
#include <poll.h>
// For shared memory buffer creation.
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
// Fix the wl_array_for_each macro to work with C++. This is based on the
// original from `wayland-util.h` in the Wayland client library.
#undef wl_array_for_each
#define wl_array_for_each(pos, array) \
for (pos = (decltype(pos))(array)->data; (const char *)pos < ((const char *)(array)->data + (array)->size); (pos)++)
#define WAYLAND_THREAD_DEBUG_LOGS_ENABLED
#ifdef WAYLAND_THREAD_DEBUG_LOGS_ENABLED
#define DEBUG_LOG_WAYLAND_THREAD(...) print_verbose(__VA_ARGS__)
#else
#define DEBUG_LOG_WAYLAND_THREAD(...)
#endif
// Read the content pointed by fd into a Vector<uint8_t>.
Vector<uint8_t> WaylandThread::_read_fd(int fd) {
// This is pretty much an arbitrary size.
uint32_t chunk_size = 2048;
LocalVector<uint8_t> data;
data.resize(chunk_size);
uint32_t bytes_read = 0;
while (true) {
ssize_t last_bytes_read = read(fd, data.ptr() + bytes_read, chunk_size);
if (last_bytes_read < 0) {
ERR_PRINT(vformat("Read error %d.", errno));
data.clear();
break;
}
if (last_bytes_read == 0) {
// We're done, we've reached the EOF.
DEBUG_LOG_WAYLAND_THREAD(vformat("Done reading %d bytes.", bytes_read));
close(fd);
data.resize(bytes_read);
break;
}
DEBUG_LOG_WAYLAND_THREAD(vformat("Read chunk of %d bytes.", last_bytes_read));
bytes_read += last_bytes_read;
// Increase the buffer size by one chunk in preparation of the next read.
data.resize(bytes_read + chunk_size);
}
return data;
}
// Based on the wayland book's shared memory boilerplate (PD/CC0).
// See: https://wayland-book.com/surfaces/shared-memory.html
int WaylandThread::_allocate_shm_file(size_t size) {
int retries = 100;
do {
// Generate a random name.
char name[] = "/wl_shm-godot-XXXXXX";
for (long unsigned int i = sizeof(name) - 7; i < sizeof(name) - 1; i++) {
name[i] = Math::random('A', 'Z');
}
// Try to open a shared memory object with that name.
int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd >= 0) {
// Success, unlink its name as we just need the file descriptor.
shm_unlink(name);
// Resize the file to the requested length.
int ret;
do {
ret = ftruncate(fd, size);
} while (ret < 0 && errno == EINTR);
if (ret < 0) {
close(fd);
return -1;
}
return fd;
}
retries--;
} while (retries > 0 && errno == EEXIST);
return -1;
}
// Return the content of a wl_data_offer.
Vector<uint8_t> WaylandThread::_wl_data_offer_read(struct wl_display *p_display, const char *p_mime, struct wl_data_offer *p_offer) {
if (!p_offer) {
return Vector<uint8_t>();
}
int fds[2];
if (pipe(fds) == 0) {
wl_data_offer_receive(p_offer, p_mime, fds[1]);
// Let the compositor know about the pipe.
// NOTE: It's important to just flush and not roundtrip here as we would risk
// running some cleanup event, like for example `wl_data_device::leave`. We're
// going to wait for the message anyways as the read will probably block if
// the compositor doesn't read from the other end of the pipe.
wl_display_flush(p_display);
// Close the write end of the pipe, which we don't need and would otherwise
// just stall our next `read`s.
close(fds[1]);
return _read_fd(fds[0]);
}
return Vector<uint8_t>();
}
// Read the content of a wp_primary_selection_offer.
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) {
if (!p_offer) {
return Vector<uint8_t>();
}
int fds[2];
if (pipe(fds) == 0) {
zwp_primary_selection_offer_v1_receive(p_offer, p_mime, fds[1]);
// NOTE: It's important to just flush and not roundtrip here as we would risk
// running some cleanup event, like for example `wl_data_device::leave`. We're
// going to wait for the message anyways as the read will probably block if
// the compositor doesn't read from the other end of the pipe.
wl_display_flush(p_display);
// Close the write end of the pipe, which we don't need and would otherwise
// just stall our next `read`s.
close(fds[1]);
return _read_fd(fds[0]);
}
return Vector<uint8_t>();
}
// Sets up an `InputEventKey` and returns whether it has any meaningful value.
bool WaylandThread::_seat_state_configure_key_event(SeatState &p_ss, Ref<InputEventKey> p_event, xkb_keycode_t p_keycode, bool p_pressed) {
// TODO: Handle keys that release multiple symbols?
Key keycode = KeyMappingXKB::get_keycode(xkb_state_key_get_one_sym(p_ss.xkb_state, p_keycode));
Key physical_keycode = KeyMappingXKB::get_scancode(p_keycode);
KeyLocation key_location = KeyMappingXKB::get_location(p_keycode);
uint32_t unicode = xkb_state_key_get_utf32(p_ss.xkb_state, p_keycode);
if (keycode == Key::NONE) {
keycode = physical_keycode;
}
if (keycode >= Key::A + 32 && keycode <= Key::Z + 32) {
keycode -= 'a' - 'A';
}
if (physical_keycode == Key::NONE && keycode == Key::NONE && unicode == 0) {
return false;
}
p_event->set_window_id(DisplayServer::MAIN_WINDOW_ID);
// Set all pressed modifiers.
p_event->set_shift_pressed(p_ss.shift_pressed);
p_event->set_ctrl_pressed(p_ss.ctrl_pressed);
p_event->set_alt_pressed(p_ss.alt_pressed);
p_event->set_meta_pressed(p_ss.meta_pressed);
p_event->set_pressed(p_pressed);
p_event->set_keycode(keycode);
p_event->set_physical_keycode(physical_keycode);
p_event->set_location(key_location);
if (unicode != 0) {
p_event->set_key_label(fix_key_label(unicode, keycode));
} else {
p_event->set_key_label(keycode);
}
if (p_pressed) {
p_event->set_unicode(fix_unicode(unicode));
}
// Taken from DisplayServerX11.
if (p_event->get_keycode() == Key::BACKTAB) {
// Make it consistent across platforms.
p_event->set_keycode(Key::TAB);
p_event->set_physical_keycode(Key::TAB);
p_event->set_shift_pressed(true);
}
return true;
}
void WaylandThread::_set_current_seat(struct wl_seat *p_seat) {
if (p_seat == wl_seat_current) {
return;
}
SeatState *old_state = wl_seat_get_seat_state(wl_seat_current);
if (old_state) {
seat_state_unlock_pointer(old_state);
}
SeatState *new_state = wl_seat_get_seat_state(p_seat);
seat_state_unlock_pointer(new_state);
wl_seat_current = p_seat;
pointer_set_constraint(pointer_constraint);
}
// Returns whether it loaded the theme or not.
bool WaylandThread::_load_cursor_theme(int p_cursor_size) {
if (wl_cursor_theme) {
wl_cursor_theme_destroy(wl_cursor_theme);
wl_cursor_theme = nullptr;
}
if (cursor_theme_name.is_empty()) {
cursor_theme_name = "default";
}
print_verbose(vformat("Loading cursor theme \"%s\" size %d.", cursor_theme_name, p_cursor_size));
wl_cursor_theme = wl_cursor_theme_load(cursor_theme_name.utf8().get_data(), p_cursor_size, registry.wl_shm);
ERR_FAIL_NULL_V_MSG(wl_cursor_theme, false, "Can't load any cursor theme.");
static const char *cursor_names[] = {
"left_ptr",
"xterm",
"hand2",
"cross",
"watch",
"left_ptr_watch",
"fleur",
"dnd-move",
"crossed_circle",
"v_double_arrow",
"h_double_arrow",
"size_bdiag",
"size_fdiag",
"move",
"row_resize",
"col_resize",
"question_arrow"
};
static const char *cursor_names_fallback[] = {
nullptr,
nullptr,
"pointer",
"cross",
"wait",
"progress",
"grabbing",
"hand1",
"forbidden",
"ns-resize",
"ew-resize",
"fd_double_arrow",
"bd_double_arrow",
"fleur",
"sb_v_double_arrow",
"sb_h_double_arrow",
"help"
};
for (int i = 0; i < DisplayServer::CURSOR_MAX; i++) {
struct wl_cursor *cursor = wl_cursor_theme_get_cursor(wl_cursor_theme, cursor_names[i]);
if (!cursor && cursor_names_fallback[i]) {
cursor = wl_cursor_theme_get_cursor(wl_cursor_theme, cursor_names_fallback[i]);
}
if (cursor && cursor->image_count > 0) {
wl_cursors[i] = cursor;
} else {
wl_cursors[i] = nullptr;
print_verbose("Failed loading cursor: " + String(cursor_names[i]));
}
}
return true;
}
void WaylandThread::_update_scale(int p_scale) {
if (p_scale <= cursor_scale) {
return;
}
print_verbose(vformat("Bumping cursor scale to %d", p_scale));
// There's some display that's bigger than the cache, let's update it.
cursor_scale = p_scale;
if (wl_cursor_theme == nullptr) {
// Ugh. Either we're still initializing (this must've been called from the
// first roundtrips) or we had some error while doing so. We'll trust that it
// will be updated for us if needed.
return;
}
int cursor_size = unscaled_cursor_size * p_scale;
if (_load_cursor_theme(cursor_size)) {
for (struct wl_seat *wl_seat : registry.wl_seats) {
SeatState *ss = wl_seat_get_seat_state(wl_seat);
ERR_FAIL_NULL(ss);
seat_state_update_cursor(ss);
}
}
}
void WaylandThread::_wl_registry_on_global(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface, uint32_t version) {
RegistryState *registry = (RegistryState *)data;
ERR_FAIL_NULL(registry);
if (strcmp(interface, wl_shm_interface.name) == 0) {
registry->wl_shm = (struct wl_shm *)wl_registry_bind(wl_registry, name, &wl_shm_interface, 1);
registry->wl_shm_name = name;
return;
}
// NOTE: Deprecated.
if (strcmp(interface, zxdg_exporter_v1_interface.name) == 0) {
registry->xdg_exporter_v1 = (struct zxdg_exporter_v1 *)wl_registry_bind(wl_registry, name, &zxdg_exporter_v1_interface, 1);
registry->xdg_exporter_v1_name = name;
return;
}
if (strcmp(interface, zxdg_exporter_v2_interface.name) == 0) {
registry->xdg_exporter_v2 = (struct zxdg_exporter_v2 *)wl_registry_bind(wl_registry, name, &zxdg_exporter_v2_interface, 1);
registry->xdg_exporter_v2_name = name;
return;
}
if (strcmp(interface, wl_compositor_interface.name) == 0) {
registry->wl_compositor = (struct wl_compositor *)wl_registry_bind(wl_registry, name, &wl_compositor_interface, CLAMP((int)version, 1, 6));
registry->wl_compositor_name = name;
return;
}
if (strcmp(interface, wl_data_device_manager_interface.name) == 0) {
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));
registry->wl_data_device_manager_name = name;
// This global creates some seat data. Let's do that for the ones already available.
for (struct wl_seat *wl_seat : registry->wl_seats) {
SeatState *ss = wl_seat_get_seat_state(wl_seat);
ERR_FAIL_NULL(ss);
if (ss->wl_data_device == nullptr) {
ss->wl_data_device = wl_data_device_manager_get_data_device(registry->wl_data_device_manager, wl_seat);
wl_data_device_add_listener(ss->wl_data_device, &wl_data_device_listener, ss);
}
}
return;
}
if (strcmp(interface, wl_output_interface.name) == 0) {
struct wl_output *wl_output = (struct wl_output *)wl_registry_bind(wl_registry, name, &wl_output_interface, CLAMP((int)version, 1, 4));
wl_proxy_tag_godot((struct wl_proxy *)wl_output);
registry->wl_outputs.push_back(wl_output);
ScreenState *ss = memnew(ScreenState);
ss->wl_output_name = name;
ss->wayland_thread = registry->wayland_thread;
wl_proxy_tag_godot((struct wl_proxy *)wl_output);
wl_output_add_listener(wl_output, &wl_output_listener, ss);
return;
}
if (strcmp(interface, wl_seat_interface.name) == 0) {
struct wl_seat *wl_seat = (struct wl_seat *)wl_registry_bind(wl_registry, name, &wl_seat_interface, CLAMP((int)version, 1, 9));
wl_proxy_tag_godot((struct wl_proxy *)wl_seat);
SeatState *ss = memnew(SeatState);
ss->wl_seat = wl_seat;
ss->wl_seat_name = name;
ss->registry = registry;
ss->wayland_thread = registry->wayland_thread;
// Some extra stuff depends on other globals. We'll initialize them if the
// globals are already there, otherwise we'll have to do that once and if they
// get announced.
//
// NOTE: Don't forget to also bind/destroy with the respective global.
if (!ss->wl_data_device && registry->wl_data_device_manager) {
// Clipboard & DnD.
ss->wl_data_device = wl_data_device_manager_get_data_device(registry->wl_data_device_manager, wl_seat);
wl_data_device_add_listener(ss->wl_data_device, &wl_data_device_listener, ss);
}
if (!ss->wp_primary_selection_device && registry->wp_primary_selection_device_manager) {
// Primary selection.
ss->wp_primary_selection_device = zwp_primary_selection_device_manager_v1_get_device(registry->wp_primary_selection_device_manager, wl_seat);
zwp_primary_selection_device_v1_add_listener(ss->wp_primary_selection_device, &wp_primary_selection_device_listener, ss);
}
if (!ss->wp_tablet_seat && registry->wp_tablet_manager) {
// Tablet.
ss->wp_tablet_seat = zwp_tablet_manager_v2_get_tablet_seat(registry->wp_tablet_manager, wl_seat);
zwp_tablet_seat_v2_add_listener(ss->wp_tablet_seat, &wp_tablet_seat_listener, ss);
}
if (!ss->wp_text_input && registry->wp_text_input_manager) {
// IME.
ss->wp_text_input = zwp_text_input_manager_v3_get_text_input(registry->wp_text_input_manager, wl_seat);
zwp_text_input_v3_add_listener(ss->wp_text_input, &wp_text_input_listener, ss);
}
registry->wl_seats.push_back(wl_seat);
wl_seat_add_listener(wl_seat, &wl_seat_listener, ss);
if (registry->wayland_thread->wl_seat_current == nullptr) {
registry->wayland_thread->_set_current_seat(wl_seat);
}
return;
}
if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
registry->xdg_wm_base = (struct xdg_wm_base *)wl_registry_bind(wl_registry, name, &xdg_wm_base_interface, CLAMP((int)version, 1, 6));
registry->xdg_wm_base_name = name;
xdg_wm_base_add_listener(registry->xdg_wm_base, &xdg_wm_base_listener, nullptr);
return;
}
if (strcmp(interface, wp_viewporter_interface.name) == 0) {
registry->wp_viewporter = (struct wp_viewporter *)wl_registry_bind(wl_registry, name, &wp_viewporter_interface, 1);
registry->wp_viewporter_name = name;
}
if (strcmp(interface, wp_fractional_scale_manager_v1_interface.name) == 0) {
registry->wp_fractional_scale_manager = (struct wp_fractional_scale_manager_v1 *)wl_registry_bind(wl_registry, name, &wp_fractional_scale_manager_v1_interface, 1);
registry->wp_fractional_scale_manager_name = name;
// NOTE: We're not mapping the fractional scale object here because this is
// supposed to be a "startup global". If for some reason this isn't true (who
// knows), add a conditional branch for creating the add-on object.
}
if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
registry->xdg_decoration_manager = (struct zxdg_decoration_manager_v1 *)wl_registry_bind(wl_registry, name, &zxdg_decoration_manager_v1_interface, 1);
registry->xdg_decoration_manager_name = name;
return;
}
if (strcmp(interface, xdg_system_bell_v1_interface.name) == 0) {
registry->xdg_system_bell = (struct xdg_system_bell_v1 *)wl_registry_bind(wl_registry, name, &xdg_system_bell_v1_interface, 1);
registry->xdg_system_bell_name = name;
return;
}
if (strcmp(interface, xdg_activation_v1_interface.name) == 0) {
registry->xdg_activation = (struct xdg_activation_v1 *)wl_registry_bind(wl_registry, name, &xdg_activation_v1_interface, 1);
registry->xdg_activation_name = name;
return;
}
if (strcmp(interface, zwp_primary_selection_device_manager_v1_interface.name) == 0) {
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);
// This global creates some seat data. Let's do that for the ones already available.
for (struct wl_seat *wl_seat : registry->wl_seats) {
SeatState *ss = wl_seat_get_seat_state(wl_seat);
ERR_FAIL_NULL(ss);
if (!ss->wp_primary_selection_device && registry->wp_primary_selection_device_manager) {
ss->wp_primary_selection_device = zwp_primary_selection_device_manager_v1_get_device(registry->wp_primary_selection_device_manager, wl_seat);
zwp_primary_selection_device_v1_add_listener(ss->wp_primary_selection_device, &wp_primary_selection_device_listener, ss);
}
}
}
if (strcmp(interface, zwp_relative_pointer_manager_v1_interface.name) == 0) {
registry->wp_relative_pointer_manager = (struct zwp_relative_pointer_manager_v1 *)wl_registry_bind(wl_registry, name, &zwp_relative_pointer_manager_v1_interface, 1);
registry->wp_relative_pointer_manager_name = name;
return;
}
if (strcmp(interface, zwp_pointer_constraints_v1_interface.name) == 0) {
registry->wp_pointer_constraints = (struct zwp_pointer_constraints_v1 *)wl_registry_bind(wl_registry, name, &zwp_pointer_constraints_v1_interface, 1);
registry->wp_pointer_constraints_name = name;
return;
}
if (strcmp(interface, zwp_pointer_gestures_v1_interface.name) == 0) {
registry->wp_pointer_gestures = (struct zwp_pointer_gestures_v1 *)wl_registry_bind(wl_registry, name, &zwp_pointer_gestures_v1_interface, 1);
registry->wp_pointer_gestures_name = name;
return;
}
if (strcmp(interface, zwp_idle_inhibit_manager_v1_interface.name) == 0) {
registry->wp_idle_inhibit_manager = (struct zwp_idle_inhibit_manager_v1 *)wl_registry_bind(wl_registry, name, &zwp_idle_inhibit_manager_v1_interface, 1);
registry->wp_idle_inhibit_manager_name = name;
return;
}
if (strcmp(interface, zwp_tablet_manager_v2_interface.name) == 0) {
registry->wp_tablet_manager = (struct zwp_tablet_manager_v2 *)wl_registry_bind(wl_registry, name, &zwp_tablet_manager_v2_interface, 1);
registry->wp_tablet_manager_name = name;
// This global creates some seat data. Let's do that for the ones already available.
for (struct wl_seat *wl_seat : registry->wl_seats) {
SeatState *ss = wl_seat_get_seat_state(wl_seat);
ERR_FAIL_NULL(ss);
ss->wp_tablet_seat = zwp_tablet_manager_v2_get_tablet_seat(registry->wp_tablet_manager, wl_seat);
zwp_tablet_seat_v2_add_listener(ss->wp_tablet_seat, &wp_tablet_seat_listener, ss);
}
return;
}
if (strcmp(interface, zwp_text_input_manager_v3_interface.name) == 0) {
registry->wp_text_input_manager = (struct zwp_text_input_manager_v3 *)wl_registry_bind(wl_registry, name, &zwp_text_input_manager_v3_interface, 1);
registry->wp_text_input_manager_name = name;
// This global creates some seat data. Let's do that for the ones already available.
for (struct wl_seat *wl_seat : registry->wl_seats) {
SeatState *ss = wl_seat_get_seat_state(wl_seat);
ERR_FAIL_NULL(ss);
ss->wp_text_input = zwp_text_input_manager_v3_get_text_input(registry->wp_text_input_manager, wl_seat);
zwp_text_input_v3_add_listener(ss->wp_text_input, &wp_text_input_listener, ss);
}
return;
}
}
void WaylandThread::_wl_registry_on_global_remove(void *data, struct wl_registry *wl_registry, uint32_t name) {
RegistryState *registry = (RegistryState *)data;
ERR_FAIL_NULL(registry);
if (name == registry->wl_shm_name) {
if (registry->wl_shm) {
wl_shm_destroy(registry->wl_shm);
registry->wl_shm = nullptr;
}
registry->wl_shm_name = 0;
return;
}
// NOTE: Deprecated.
if (name == registry->xdg_exporter_v1_name) {
if (registry->xdg_exporter_v1) {
zxdg_exporter_v1_destroy(registry->xdg_exporter_v1);
registry->xdg_exporter_v1 = nullptr;
}
registry->xdg_exporter_v1_name = 0;
return;
}
if (name == registry->xdg_exporter_v2_name) {
if (registry->xdg_exporter_v2) {
zxdg_exporter_v2_destroy(registry->xdg_exporter_v2);
registry->xdg_exporter_v2 = nullptr;
}
registry->xdg_exporter_v2_name = 0;
return;
}
if (name == registry->wl_compositor_name) {
if (registry->wl_compositor) {
wl_compositor_destroy(registry->wl_compositor);
registry->wl_compositor = nullptr;
}
registry->wl_compositor_name = 0;
return;
}
if (name == registry->wl_data_device_manager_name) {
if (registry->wl_data_device_manager) {
wl_data_device_manager_destroy(registry->wl_data_device_manager);
registry->wl_data_device_manager = nullptr;
}
registry->wl_data_device_manager_name = 0;
// This global is used to create some seat data. Let's clean it.
for (struct wl_seat *wl_seat : registry->wl_seats) {
SeatState *ss = wl_seat_get_seat_state(wl_seat);
ERR_FAIL_NULL(ss);
if (ss->wl_data_device) {
wl_data_device_destroy(ss->wl_data_device);
ss->wl_data_device = nullptr;
}
ss->wl_data_device = nullptr;
}
return;
}
if (name == registry->xdg_wm_base_name) {
if (registry->xdg_wm_base) {
xdg_wm_base_destroy(registry->xdg_wm_base);
registry->xdg_wm_base = nullptr;
}
registry->xdg_wm_base_name = 0;
return;
}
if (name == registry->wp_viewporter_name) {
WindowState *ws = ®istry->wayland_thread->main_window;
if (registry->wp_viewporter) {
wp_viewporter_destroy(registry->wp_viewporter);
registry->wp_viewporter = nullptr;
}
if (ws->wp_viewport) {
wp_viewport_destroy(ws->wp_viewport);
ws->wp_viewport = nullptr;
}
registry->wp_viewporter_name = 0;
return;
}
if (name == registry->wp_fractional_scale_manager_name) {
WindowState *ws = ®istry->wayland_thread->main_window;
if (registry->wp_fractional_scale_manager) {
wp_fractional_scale_manager_v1_destroy(registry->wp_fractional_scale_manager);
registry->wp_fractional_scale_manager = nullptr;
}
if (ws->wp_fractional_scale) {
wp_fractional_scale_v1_destroy(ws->wp_fractional_scale);
ws->wp_fractional_scale = nullptr;
}
registry->wp_fractional_scale_manager_name = 0;
}
if (name == registry->xdg_decoration_manager_name) {
if (registry->xdg_decoration_manager) {
zxdg_decoration_manager_v1_destroy(registry->xdg_decoration_manager);
registry->xdg_decoration_manager = nullptr;
}
registry->xdg_decoration_manager_name = 0;
return;
}
if (name == registry->xdg_system_bell_name) {
if (registry->xdg_system_bell) {
xdg_system_bell_v1_destroy(registry->xdg_system_bell);
registry->xdg_system_bell = nullptr;
}
registry->xdg_system_bell_name = 0;
return;
}
if (name == registry->xdg_activation_name) {
if (registry->xdg_activation) {
xdg_activation_v1_destroy(registry->xdg_activation);
registry->xdg_activation = nullptr;
}
registry->xdg_activation_name = 0;
return;
}
if (name == registry->wp_primary_selection_device_manager_name) {
if (registry->wp_primary_selection_device_manager) {
zwp_primary_selection_device_manager_v1_destroy(registry->wp_primary_selection_device_manager);
registry->wp_primary_selection_device_manager = nullptr;
}
registry->wp_primary_selection_device_manager_name = 0;
// This global is used to create some seat data. Let's clean it.
for (struct wl_seat *wl_seat : registry->wl_seats) {
SeatState *ss = wl_seat_get_seat_state(wl_seat);
ERR_FAIL_NULL(ss);
if (ss->wp_primary_selection_device) {
zwp_primary_selection_device_v1_destroy(ss->wp_primary_selection_device);
ss->wp_primary_selection_device = nullptr;
}
if (ss->wp_primary_selection_source) {
zwp_primary_selection_source_v1_destroy(ss->wp_primary_selection_source);
ss->wp_primary_selection_source = nullptr;
}
if (ss->wp_primary_selection_offer) {
memfree(wp_primary_selection_offer_get_offer_state(ss->wp_primary_selection_offer));
zwp_primary_selection_offer_v1_destroy(ss->wp_primary_selection_offer);
ss->wp_primary_selection_offer = nullptr;
}
}
return;
}
if (name == registry->wp_relative_pointer_manager_name) {
if (registry->wp_relative_pointer_manager) {
zwp_relative_pointer_manager_v1_destroy(registry->wp_relative_pointer_manager);
registry->wp_relative_pointer_manager = nullptr;
}
registry->wp_relative_pointer_manager_name = 0;
// This global is used to create some seat data. Let's clean it.
for (struct wl_seat *wl_seat : registry->wl_seats) {
SeatState *ss = wl_seat_get_seat_state(wl_seat);
ERR_FAIL_NULL(ss);
if (ss->wp_relative_pointer) {
zwp_relative_pointer_v1_destroy(ss->wp_relative_pointer);
ss->wp_relative_pointer = nullptr;
}
}
return;
}
if (name == registry->wp_pointer_constraints_name) {
if (registry->wp_pointer_constraints) {
zwp_pointer_constraints_v1_destroy(registry->wp_pointer_constraints);
registry->wp_pointer_constraints = nullptr;
}
registry->wp_pointer_constraints_name = 0;
// This global is used to create some seat data. Let's clean it.
for (struct wl_seat *wl_seat : registry->wl_seats) {
SeatState *ss = wl_seat_get_seat_state(wl_seat);
ERR_FAIL_NULL(ss);
if (ss->wp_relative_pointer) {
zwp_relative_pointer_v1_destroy(ss->wp_relative_pointer);
ss->wp_relative_pointer = nullptr;
}
if (ss->wp_locked_pointer) {
zwp_locked_pointer_v1_destroy(ss->wp_locked_pointer);
ss->wp_locked_pointer = nullptr;
}
if (ss->wp_confined_pointer) {
zwp_confined_pointer_v1_destroy(ss->wp_confined_pointer);
ss->wp_confined_pointer = nullptr;
}
}
return;
}
if (name == registry->wp_pointer_gestures_name) {
if (registry->wp_pointer_gestures) {
zwp_pointer_gestures_v1_destroy(registry->wp_pointer_gestures);
}
registry->wp_pointer_gestures = nullptr;
registry->wp_pointer_gestures_name = 0;
// This global is used to create some seat data. Let's clean it.
for (struct wl_seat *wl_seat : registry->wl_seats) {
SeatState *ss = wl_seat_get_seat_state(wl_seat);
ERR_FAIL_NULL(ss);
if (ss->wp_pointer_gesture_pinch) {
zwp_pointer_gesture_pinch_v1_destroy(ss->wp_pointer_gesture_pinch);
ss->wp_pointer_gesture_pinch = nullptr;
}
}
return;
}
if (name == registry->wp_idle_inhibit_manager_name) {
if (registry->wp_idle_inhibit_manager) {
zwp_idle_inhibit_manager_v1_destroy(registry->wp_idle_inhibit_manager);
registry->wp_idle_inhibit_manager = nullptr;
}
registry->wp_idle_inhibit_manager_name = 0;
return;
}
if (name == registry->wp_tablet_manager_name) {
if (registry->wp_tablet_manager) {
zwp_tablet_manager_v2_destroy(registry->wp_tablet_manager);
registry->wp_tablet_manager = nullptr;
}
registry->wp_tablet_manager_name = 0;
// This global is used to create some seat data. Let's clean it.
for (struct wl_seat *wl_seat : registry->wl_seats) {
SeatState *ss = wl_seat_get_seat_state(wl_seat);
ERR_FAIL_NULL(ss);
for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
TabletToolState *state = wp_tablet_tool_get_state(tool);
if (state) {
memdelete(state);
}
zwp_tablet_tool_v2_destroy(tool);
}
ss->tablet_tools.clear();
}
return;
}
if (name == registry->wp_text_input_manager_name) {
if (registry->wp_text_input_manager) {
zwp_text_input_manager_v3_destroy(registry->wp_text_input_manager);
registry->wp_text_input_manager = nullptr;
}
registry->wp_text_input_manager_name = 0;
for (struct wl_seat *wl_seat : registry->wl_seats) {
SeatState *ss = wl_seat_get_seat_state(wl_seat);
ERR_FAIL_NULL(ss);
zwp_text_input_v3_destroy(ss->wp_text_input);
ss->wp_text_input = nullptr;
}
return;
}
{
// Iterate through all of the seats to find if any got removed.
List<struct wl_seat *>::Element *E = registry->wl_seats.front();
while (E) {
struct wl_seat *wl_seat = E->get();
List<struct wl_seat *>::Element *N = E->next();
SeatState *ss = wl_seat_get_seat_state(wl_seat);
ERR_FAIL_NULL(ss);
if (ss->wl_seat_name == name) {
if (wl_seat) {
wl_seat_destroy(wl_seat);
}
if (ss->wl_data_device) {
wl_data_device_destroy(ss->wl_data_device);
}
if (ss->wp_tablet_seat) {
zwp_tablet_seat_v2_destroy(ss->wp_tablet_seat);
for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
TabletToolState *state = wp_tablet_tool_get_state(tool);
if (state) {
memdelete(state);
}
zwp_tablet_tool_v2_destroy(tool);
}
}
memdelete(ss);
registry->wl_seats.erase(E);
return;
}
E = N;
}
}
{
// Iterate through all of the outputs to find if any got removed.
// FIXME: This is a very bruteforce approach.
List<struct wl_output *>::Element *it = registry->wl_outputs.front();
while (it) {
// Iterate through all of the screens to find if any got removed.
struct wl_output *wl_output = it->get();
ERR_FAIL_NULL(wl_output);
ScreenState *ss = wl_output_get_screen_state(wl_output);
if (ss->wl_output_name == name) {
registry->wl_outputs.erase(it);
memdelete(ss);
wl_output_destroy(wl_output);
return;
}
it = it->next();
}
}
}
void WaylandThread::_wl_surface_on_enter(void *data, struct wl_surface *wl_surface, struct wl_output *wl_output) {
if (!wl_output || !wl_proxy_is_godot((struct wl_proxy *)wl_output)) {
// This won't have the right data bound to it. Not worth it and would probably
// just break everything.
return;
}
WindowState *ws = (WindowState *)data;
ERR_FAIL_NULL(ws);
DEBUG_LOG_WAYLAND_THREAD(vformat("Window entered output %x.\n", (size_t)wl_output));
ws->wl_outputs.insert(wl_output);
// Workaround for buffer scaling as there's no guaranteed way of knowing the
// preferred scale.
// TODO: Skip this branch for newer `wl_surface`s once we add support for
// `wl_surface::preferred_buffer_scale`
if (ws->preferred_fractional_scale == 0) {
window_state_update_size(ws, ws->rect.size.width, ws->rect.size.height);
}
}