forked from libsdl-org/SDL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDL_waylandvideo.c
1630 lines (1378 loc) · 57.1 KB
/
SDL_waylandvideo.c
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
/*
Simple DirectMedia Layer
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#ifdef SDL_VIDEO_DRIVER_WAYLAND
#include "../../core/linux/SDL_system_theme.h"
#include "../../events/SDL_events_c.h"
#include "SDL_waylandclipboard.h"
#include "SDL_waylandevents_c.h"
#include "SDL_waylandkeyboard.h"
#include "SDL_waylandmessagebox.h"
#include "SDL_waylandmouse.h"
#include "SDL_waylandopengles.h"
#include "SDL_waylandvideo.h"
#include "SDL_waylandvulkan.h"
#include "SDL_waylandwindow.h"
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <xkbcommon/xkbcommon.h>
#include <wayland-util.h>
#include "alpha-modifier-v1-client-protocol.h"
#include "cursor-shape-v1-client-protocol.h"
#include "fractional-scale-v1-client-protocol.h"
#include "frog-color-management-v1-client-protocol.h"
#include "idle-inhibit-unstable-v1-client-protocol.h"
#include "input-timestamps-unstable-v1-client-protocol.h"
#include "keyboard-shortcuts-inhibit-unstable-v1-client-protocol.h"
#include "pointer-constraints-unstable-v1-client-protocol.h"
#include "primary-selection-unstable-v1-client-protocol.h"
#include "relative-pointer-unstable-v1-client-protocol.h"
#include "tablet-v2-client-protocol.h"
#include "text-input-unstable-v3-client-protocol.h"
#include "viewporter-client-protocol.h"
#include "xdg-activation-v1-client-protocol.h"
#include "xdg-decoration-unstable-v1-client-protocol.h"
#include "xdg-dialog-v1-client-protocol.h"
#include "xdg-foreign-unstable-v2-client-protocol.h"
#include "xdg-output-unstable-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h"
#include "xdg-toplevel-icon-v1-client-protocol.h"
#ifdef HAVE_LIBDECOR_H
#include <libdecor.h>
#endif
#define WAYLANDVID_DRIVER_NAME "wayland"
// Clamp certain core protocol versions on older versions of libwayland.
#if SDL_WAYLAND_CHECK_VERSION(1, 22, 0)
#define SDL_WL_COMPOSITOR_VERSION 6
#else
#define SDL_WL_COMPOSITOR_VERSION 4
#endif
#if SDL_WAYLAND_CHECK_VERSION(1, 22, 0)
#define SDL_WL_SEAT_VERSION 9
#elif SDL_WAYLAND_CHECK_VERSION(1, 21, 0)
#define SDL_WL_SEAT_VERSION 8
#else
#define SDL_WL_SEAT_VERSION 5
#endif
#if SDL_WAYLAND_CHECK_VERSION(1, 20, 0)
#define SDL_WL_OUTPUT_VERSION 4
#else
#define SDL_WL_OUTPUT_VERSION 3
#endif
#ifdef SDL_USE_LIBDBUS
#include "../../core/linux/SDL_dbus.h"
#define DISPLAY_INFO_NODE "org.gnome.Mutter.DisplayConfig"
#define DISPLAY_INFO_PATH "/org/gnome/Mutter/DisplayConfig"
#define DISPLAY_INFO_METHOD "GetCurrentState"
#endif
/* GNOME doesn't expose displays in any particular order, but we can find the
* primary display and its logical coordinates via a DBus method.
*/
static bool Wayland_GetGNOMEPrimaryDisplayCoordinates(int *x, int *y)
{
#ifdef SDL_USE_LIBDBUS
SDL_DBusContext *dbus = SDL_DBus_GetContext();
if (dbus == NULL) {
return false;
}
DBusMessage *reply = NULL;
DBusMessageIter iter[3];
DBusMessage *msg = dbus->message_new_method_call(DISPLAY_INFO_NODE,
DISPLAY_INFO_PATH,
DISPLAY_INFO_NODE,
DISPLAY_INFO_METHOD);
if (msg) {
reply = dbus->connection_send_with_reply_and_block(dbus->session_conn, msg, DBUS_TIMEOUT_USE_DEFAULT, NULL);
dbus->message_unref(msg);
}
if (reply) {
// Serial (don't care)
dbus->message_iter_init(reply, &iter[0]);
if (dbus->message_iter_get_arg_type(&iter[0]) != DBUS_TYPE_UINT32) {
goto error;
}
// Physical monitor array (don't care)
dbus->message_iter_next(&iter[0]);
if (dbus->message_iter_get_arg_type(&iter[0]) != DBUS_TYPE_ARRAY) {
goto error;
}
// Logical monitor array of structs
dbus->message_iter_next(&iter[0]);
if (dbus->message_iter_get_arg_type(&iter[0]) != DBUS_TYPE_ARRAY) {
goto error;
}
// First logical monitor struct
dbus->message_iter_recurse(&iter[0], &iter[1]);
if (dbus->message_iter_get_arg_type(&iter[1]) != DBUS_TYPE_STRUCT) {
goto error;
}
do {
int logical_x, logical_y;
dbus_bool_t primary;
// Logical X
dbus->message_iter_recurse(&iter[1], &iter[2]);
if (dbus->message_iter_get_arg_type(&iter[2]) != DBUS_TYPE_INT32) {
goto error;
}
dbus->message_iter_get_basic(&iter[2], &logical_x);
// Logical Y
dbus->message_iter_next(&iter[2]);
if (dbus->message_iter_get_arg_type(&iter[2]) != DBUS_TYPE_INT32) {
goto error;
}
dbus->message_iter_get_basic(&iter[2], &logical_y);
// Scale (don't care)
dbus->message_iter_next(&iter[2]);
if (dbus->message_iter_get_arg_type(&iter[2]) != DBUS_TYPE_DOUBLE) {
goto error;
}
// Transform (don't care)
dbus->message_iter_next(&iter[2]);
if (dbus->message_iter_get_arg_type(&iter[2]) != DBUS_TYPE_UINT32) {
goto error;
}
// Primary display boolean
dbus->message_iter_next(&iter[2]);
if (dbus->message_iter_get_arg_type(&iter[2]) != DBUS_TYPE_BOOLEAN) {
goto error;
}
dbus->message_iter_get_basic(&iter[2], &primary);
if (primary) {
*x = logical_x;
*y = logical_y;
// We found the primary display: success.
dbus->message_unref(reply);
return true;
}
} while (dbus->message_iter_next(&iter[1]));
}
error:
if (reply) {
dbus->message_unref(reply);
}
#endif
return false;
}
// Sort the list of displays into a deterministic order
static int SDLCALL Wayland_DisplayPositionCompare(const void *a, const void *b)
{
const SDL_DisplayData *da = *(SDL_DisplayData **)a;
const SDL_DisplayData *db = *(SDL_DisplayData **)b;
const bool a_at_origin = da->x == 0 && da->y == 0;
const bool b_at_origin = db->x == 0 && db->y == 0;
// Sort the display at 0,0 to be beginning of the list, as that will be the fallback primary.
if (a_at_origin && !b_at_origin) {
return -1;
}
if (b_at_origin && !a_at_origin) {
return 1;
}
if (da->x < db->x) {
return -1;
}
if (da->x > db->x) {
return 1;
}
if (da->y < db->y) {
return -1;
}
if (da->y > db->y) {
return 1;
}
// If no position information is available, use the connector name.
if (da->wl_output_name && db->wl_output_name) {
return SDL_strcmp(da->wl_output_name, db->wl_output_name);
}
return 0;
}
/* Wayland doesn't have the native concept of a primary display, but there are clients that
* will base their resolution lists on, or automatically make themselves fullscreen on, the
* first listed output, which can lead to problems if the first listed output isn't
* necessarily the best display for this. This attempts to find a primary display, first by
* querying the GNOME DBus property, then trying to determine the 'best' display if that fails.
* If all displays are equal, the one at position 0,0 will become the primary.
*
* The primary is determined by the following criteria, in order:
* - Landscape is preferred over portrait
* - The highest native resolution
* - TODO: A higher HDR range is preferred
* - Higher refresh is preferred (ignoring small differences)
* - Lower scale values are preferred (larger display)
*/
static int Wayland_GetPrimaryDisplay(SDL_VideoData *vid)
{
static const int REFRESH_DELTA = 4000;
// Query the DBus interface to see if the coordinates of the primary display are exposed.
int x, y;
if (Wayland_GetGNOMEPrimaryDisplayCoordinates(&x, &y)) {
for (int i = 0; i < vid->output_count; ++i) {
if (vid->output_list[i]->x == x && vid->output_list[i]->y == y) {
return i;
}
}
}
// Otherwise, choose the 'best' display.
int best_width = 0;
int best_height = 0;
double best_scale = 0.0;
int best_refresh = 0;
bool best_is_landscape = false;
int best_index = 0;
for (int i = 0; i < vid->output_count; ++i) {
const SDL_DisplayData *d = vid->output_list[i];
const bool is_landscape = d->orientation != SDL_ORIENTATION_PORTRAIT && d->orientation != SDL_ORIENTATION_PORTRAIT_FLIPPED;
bool have_new_best = false;
if (!best_is_landscape && is_landscape) { // Favor landscape over portrait displays.
have_new_best = true;
} else if (!best_is_landscape || is_landscape) { // Ignore portrait displays if a landscape was already found.
if (d->pixel_width > best_width || d->pixel_height > best_height) {
have_new_best = true;
} else if (d->pixel_width == best_width && d->pixel_height == best_height) {
if (d->refresh - best_refresh > REFRESH_DELTA) { // Favor a higher refresh rate, but ignore small differences (e.g. 59.97 vs 60.1)
have_new_best = true;
} else if (d->scale_factor < best_scale && SDL_abs(d->refresh - best_refresh) <= REFRESH_DELTA) {
// Prefer a lower scale display if the difference in refresh rate is small.
have_new_best = true;
}
}
}
if (have_new_best) {
best_width = d->pixel_width;
best_height = d->pixel_height;
best_scale = d->scale_factor;
best_refresh = d->refresh;
best_is_landscape = is_landscape;
best_index = i;
}
}
return best_index;
}
static void Wayland_SortOutputsByPriorityHint(SDL_VideoData *vid)
{
const char *name_hint = SDL_GetHint(SDL_HINT_VIDEO_DISPLAY_PRIORITY);
if (name_hint) {
char *saveptr;
char *str = SDL_strdup(name_hint);
SDL_DisplayData **sorted_list = SDL_malloc(sizeof(SDL_DisplayData *) * vid->output_count);
if (str && sorted_list) {
int sorted_index = 0;
// Sort the requested displays to the front of the list.
const char *token = SDL_strtok_r(str, ",", &saveptr);
while (token) {
for (int i = 0; i < vid->output_count; ++i) {
SDL_DisplayData *d = vid->output_list[i];
if (d && d->wl_output_name && SDL_strcmp(token, d->wl_output_name) == 0) {
sorted_list[sorted_index++] = d;
vid->output_list[i] = NULL;
break;
}
}
token = SDL_strtok_r(NULL, ",", &saveptr);
}
// Append the remaining outputs to the end of the list.
for (int i = 0; i < vid->output_count; ++i) {
if (vid->output_list[i]) {
sorted_list[sorted_index++] = vid->output_list[i];
}
}
// Copy the sorted list to the output list.
SDL_memcpy(vid->output_list, sorted_list, sizeof(SDL_DisplayData *) * vid->output_count);
}
SDL_free(str);
SDL_free(sorted_list);
}
}
static void Wayland_SortOutputs(SDL_VideoData *vid)
{
// Sort by position or connector name, so the order of outputs is deterministic.
SDL_qsort(vid->output_list, vid->output_count, sizeof(SDL_DisplayData *), Wayland_DisplayPositionCompare);
// Find a suitable primary display and move it to the front of the list.
const int primary_index = Wayland_GetPrimaryDisplay(vid);
if (primary_index) {
SDL_DisplayData *primary = vid->output_list[primary_index];
SDL_memmove(&vid->output_list[1], &vid->output_list[0], sizeof(SDL_DisplayData *) * primary_index);
vid->output_list[0] = primary;
}
// Apply the ordering hint, if specified.
Wayland_SortOutputsByPriorityHint(vid);
}
static void display_handle_done(void *data, struct wl_output *output);
// Initialization/Query functions
static bool Wayland_VideoInit(SDL_VideoDevice *_this);
static bool Wayland_GetDisplayBounds(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_Rect *rect);
static void Wayland_VideoQuit(SDL_VideoDevice *_this);
static const char *SDL_WAYLAND_surface_tag = "sdl-window";
static const char *SDL_WAYLAND_output_tag = "sdl-output";
void SDL_WAYLAND_register_surface(struct wl_surface *surface)
{
wl_proxy_set_tag((struct wl_proxy *)surface, &SDL_WAYLAND_surface_tag);
}
void SDL_WAYLAND_register_output(struct wl_output *output)
{
wl_proxy_set_tag((struct wl_proxy *)output, &SDL_WAYLAND_output_tag);
}
bool SDL_WAYLAND_own_surface(struct wl_surface *surface)
{
return wl_proxy_get_tag((struct wl_proxy *)surface) == &SDL_WAYLAND_surface_tag;
}
bool SDL_WAYLAND_own_output(struct wl_output *output)
{
return wl_proxy_get_tag((struct wl_proxy *)output) == &SDL_WAYLAND_output_tag;
}
/* External surfaces may have their own user data attached, the modification of which
* can cause problems with external toolkits. Instead, external windows are kept in
* their own list, and a search is conducted to find a matching surface.
*/
static struct wl_list external_window_list;
void Wayland_AddWindowDataToExternalList(SDL_WindowData *data)
{
WAYLAND_wl_list_insert(&external_window_list, &data->external_window_list_link);
}
void Wayland_RemoveWindowDataFromExternalList(SDL_WindowData *data)
{
WAYLAND_wl_list_remove(&data->external_window_list_link);
}
SDL_WindowData *Wayland_GetWindowDataForOwnedSurface(struct wl_surface *surface)
{
if (SDL_WAYLAND_own_surface(surface)) {
return (SDL_WindowData *)wl_surface_get_user_data(surface);
} else if (!WAYLAND_wl_list_empty(&external_window_list)) {
SDL_WindowData *p;
wl_list_for_each (p, &external_window_list, external_window_list_link) {
if (p->surface == surface) {
return p;
}
}
}
return NULL;
}
static void Wayland_DeleteDevice(SDL_VideoDevice *device)
{
SDL_VideoData *data = device->internal;
if (data->display && !data->display_externally_owned) {
WAYLAND_wl_display_flush(data->display);
WAYLAND_wl_display_disconnect(data->display);
SDL_ClearProperty(SDL_GetGlobalProperties(), SDL_PROP_GLOBAL_VIDEO_WAYLAND_WL_DISPLAY_POINTER);
}
if (device->wakeup_lock) {
SDL_DestroyMutex(device->wakeup_lock);
}
SDL_free(data);
SDL_free(device);
SDL_WAYLAND_UnloadSymbols();
}
typedef struct
{
bool has_fifo_v1;
} SDL_WaylandPreferredData;
static void wayland_preferred_check_handle_global(void *data, struct wl_registry *registry, uint32_t id,
const char *interface, uint32_t version)
{
SDL_WaylandPreferredData *d = data;
if (SDL_strcmp(interface, "wp_fifo_manager_v1") == 0) {
d->has_fifo_v1 = true;
}
}
static void wayland_preferred_check_remove_global(void *data, struct wl_registry *registry, uint32_t id)
{
// No need to do anything here.
}
static const struct wl_registry_listener preferred_registry_listener = {
wayland_preferred_check_handle_global,
wayland_preferred_check_remove_global
};
static bool Wayland_IsPreferred(struct wl_display *display)
{
struct wl_registry *registry = wl_display_get_registry(display);
SDL_WaylandPreferredData preferred_data = { 0 };
if (!registry) {
SDL_SetError("Failed to get the Wayland registry");
return false;
}
wl_registry_add_listener(registry, &preferred_registry_listener, &preferred_data);
WAYLAND_wl_display_roundtrip(display);
wl_registry_destroy(registry);
return preferred_data.has_fifo_v1;
}
static SDL_VideoDevice *Wayland_CreateDevice(bool require_preferred_protocols)
{
SDL_VideoDevice *device;
SDL_VideoData *data;
struct SDL_WaylandInput *input;
struct wl_display *display = SDL_GetPointerProperty(SDL_GetGlobalProperties(),
SDL_PROP_GLOBAL_VIDEO_WAYLAND_WL_DISPLAY_POINTER, NULL);
bool display_is_external = !!display;
// Are we trying to connect to or are currently in a Wayland session?
if (!SDL_getenv("WAYLAND_DISPLAY")) {
const char *session = SDL_getenv("XDG_SESSION_TYPE");
if (session && SDL_strcasecmp(session, "wayland") != 0) {
return NULL;
}
}
if (!SDL_WAYLAND_LoadSymbols()) {
return NULL;
}
if (!display) {
display = WAYLAND_wl_display_connect(NULL);
if (!display) {
SDL_WAYLAND_UnloadSymbols();
return NULL;
}
}
/*
* If we are checking for preferred Wayland, then let's query for
* fifo-v1's existence, so we don't regress GPU-bound performance
* and frame-pacing by default due to swapchain starvation.
*/
if (require_preferred_protocols && !Wayland_IsPreferred(display)) {
if (!display_is_external) {
WAYLAND_wl_display_disconnect(display);
}
SDL_WAYLAND_UnloadSymbols();
return NULL;
}
data = SDL_calloc(1, sizeof(*data));
if (!data) {
if (!display_is_external) {
WAYLAND_wl_display_disconnect(display);
}
SDL_WAYLAND_UnloadSymbols();
return NULL;
}
input = SDL_calloc(1, sizeof(*input));
if (!input) {
SDL_free(data);
if (!display_is_external) {
WAYLAND_wl_display_disconnect(display);
}
SDL_WAYLAND_UnloadSymbols();
return NULL;
}
input->display = data;
input->sx_w = wl_fixed_from_int(0);
input->sy_w = wl_fixed_from_int(0);
input->xkb.current_group = XKB_GROUP_INVALID;
data->initializing = true;
data->display = display;
data->input = input;
data->display_externally_owned = display_is_external;
data->scale_to_display_enabled = SDL_GetHintBoolean(SDL_HINT_VIDEO_WAYLAND_SCALE_TO_DISPLAY, false);
WAYLAND_wl_list_init(&external_window_list);
// Initialize all variables that we clean on shutdown
device = SDL_calloc(1, sizeof(SDL_VideoDevice));
if (!device) {
SDL_free(input);
SDL_free(data);
if (!display_is_external) {
WAYLAND_wl_display_disconnect(display);
}
SDL_WAYLAND_UnloadSymbols();
return NULL;
}
if (!display_is_external) {
SDL_SetPointerProperty(SDL_GetGlobalProperties(),
SDL_PROP_GLOBAL_VIDEO_WAYLAND_WL_DISPLAY_POINTER, display);
}
device->internal = data;
device->wakeup_lock = SDL_CreateMutex();
// Set the function pointers
device->VideoInit = Wayland_VideoInit;
device->VideoQuit = Wayland_VideoQuit;
device->GetDisplayBounds = Wayland_GetDisplayBounds;
device->SuspendScreenSaver = Wayland_SuspendScreenSaver;
device->PumpEvents = Wayland_PumpEvents;
device->WaitEventTimeout = Wayland_WaitEventTimeout;
device->SendWakeupEvent = Wayland_SendWakeupEvent;
#ifdef SDL_VIDEO_OPENGL_EGL
device->GL_SwapWindow = Wayland_GLES_SwapWindow;
device->GL_GetSwapInterval = Wayland_GLES_GetSwapInterval;
device->GL_SetSwapInterval = Wayland_GLES_SetSwapInterval;
device->GL_MakeCurrent = Wayland_GLES_MakeCurrent;
device->GL_CreateContext = Wayland_GLES_CreateContext;
device->GL_LoadLibrary = Wayland_GLES_LoadLibrary;
device->GL_UnloadLibrary = Wayland_GLES_UnloadLibrary;
device->GL_GetProcAddress = Wayland_GLES_GetProcAddress;
device->GL_DestroyContext = Wayland_GLES_DestroyContext;
device->GL_GetEGLSurface = Wayland_GLES_GetEGLSurface;
#endif
device->CreateSDLWindow = Wayland_CreateWindow;
device->ShowWindow = Wayland_ShowWindow;
device->HideWindow = Wayland_HideWindow;
device->RaiseWindow = Wayland_RaiseWindow;
device->SetWindowFullscreen = Wayland_SetWindowFullscreen;
device->MaximizeWindow = Wayland_MaximizeWindow;
device->MinimizeWindow = Wayland_MinimizeWindow;
device->SetWindowMouseRect = Wayland_SetWindowMouseRect;
device->SetWindowMouseGrab = Wayland_SetWindowMouseGrab;
device->SetWindowKeyboardGrab = Wayland_SetWindowKeyboardGrab;
device->RestoreWindow = Wayland_RestoreWindow;
device->SetWindowBordered = Wayland_SetWindowBordered;
device->SetWindowResizable = Wayland_SetWindowResizable;
device->SetWindowPosition = Wayland_SetWindowPosition;
device->SetWindowSize = Wayland_SetWindowSize;
device->SetWindowMinimumSize = Wayland_SetWindowMinimumSize;
device->SetWindowMaximumSize = Wayland_SetWindowMaximumSize;
device->SetWindowParent = Wayland_SetWindowParent;
device->SetWindowModal = Wayland_SetWindowModal;
device->SetWindowOpacity = Wayland_SetWindowOpacity;
device->SetWindowTitle = Wayland_SetWindowTitle;
device->SetWindowIcon = Wayland_SetWindowIcon;
device->GetWindowSizeInPixels = Wayland_GetWindowSizeInPixels;
device->GetWindowContentScale = Wayland_GetWindowContentScale;
device->GetDisplayForWindow = Wayland_GetDisplayForWindow;
device->DestroyWindow = Wayland_DestroyWindow;
device->SetWindowHitTest = Wayland_SetWindowHitTest;
device->FlashWindow = Wayland_FlashWindow;
device->HasScreenKeyboardSupport = Wayland_HasScreenKeyboardSupport;
device->ShowWindowSystemMenu = Wayland_ShowWindowSystemMenu;
device->SyncWindow = Wayland_SyncWindow;
#ifdef SDL_USE_LIBDBUS
if (SDL_SystemTheme_Init())
device->system_theme = SDL_SystemTheme_Get();
#endif
device->GetTextMimeTypes = Wayland_GetTextMimeTypes;
device->SetClipboardData = Wayland_SetClipboardData;
device->GetClipboardData = Wayland_GetClipboardData;
device->HasClipboardData = Wayland_HasClipboardData;
device->StartTextInput = Wayland_StartTextInput;
device->StopTextInput = Wayland_StopTextInput;
device->UpdateTextInputArea = Wayland_UpdateTextInputArea;
#ifdef SDL_VIDEO_VULKAN
device->Vulkan_LoadLibrary = Wayland_Vulkan_LoadLibrary;
device->Vulkan_UnloadLibrary = Wayland_Vulkan_UnloadLibrary;
device->Vulkan_GetInstanceExtensions = Wayland_Vulkan_GetInstanceExtensions;
device->Vulkan_CreateSurface = Wayland_Vulkan_CreateSurface;
device->Vulkan_DestroySurface = Wayland_Vulkan_DestroySurface;
device->Vulkan_GetPresentationSupport = Wayland_Vulkan_GetPresentationSupport;
#endif
device->free = Wayland_DeleteDevice;
device->device_caps = VIDEO_DEVICE_CAPS_MODE_SWITCHING_EMULATED |
VIDEO_DEVICE_CAPS_HAS_POPUP_WINDOW_SUPPORT |
VIDEO_DEVICE_CAPS_SENDS_FULLSCREEN_DIMENSIONS |
VIDEO_DEVICE_CAPS_SENDS_DISPLAY_CHANGES |
VIDEO_DEVICE_CAPS_DISABLE_MOUSE_WARP_ON_FULLSCREEN_TRANSITIONS |
VIDEO_DEVICE_CAPS_SENDS_HDR_CHANGES;
return device;
}
static SDL_VideoDevice *Wayland_Preferred_CreateDevice(void)
{
return Wayland_CreateDevice(true);
}
static SDL_VideoDevice *Wayland_Fallback_CreateDevice(void)
{
return Wayland_CreateDevice(false);
}
VideoBootStrap Wayland_preferred_bootstrap = {
WAYLANDVID_DRIVER_NAME, "SDL Wayland video driver",
Wayland_Preferred_CreateDevice,
Wayland_ShowMessageBox
};
VideoBootStrap Wayland_bootstrap = {
WAYLANDVID_DRIVER_NAME, "SDL Wayland video driver",
Wayland_Fallback_CreateDevice,
Wayland_ShowMessageBox
};
static void xdg_output_handle_logical_position(void *data, struct zxdg_output_v1 *xdg_output,
int32_t x, int32_t y)
{
SDL_DisplayData *internal = (SDL_DisplayData *)data;
internal->x = x;
internal->y = y;
internal->has_logical_position = true;
}
static void xdg_output_handle_logical_size(void *data, struct zxdg_output_v1 *xdg_output,
int32_t width, int32_t height)
{
SDL_DisplayData *internal = (SDL_DisplayData *)data;
internal->logical_width = width;
internal->logical_height = height;
internal->has_logical_size = true;
}
static void xdg_output_handle_done(void *data, struct zxdg_output_v1 *xdg_output)
{
SDL_DisplayData *internal = (void *)data;
/*
* xdg-output.done events are deprecated and only apply below version 3 of the protocol.
* A wl-output.done event will be emitted in version 3 or higher.
*/
if (zxdg_output_v1_get_version(internal->xdg_output) < 3) {
display_handle_done(data, internal->output);
}
}
static void xdg_output_handle_name(void *data, struct zxdg_output_v1 *xdg_output,
const char *name)
{
SDL_DisplayData *internal = (SDL_DisplayData *)data;
// Deprecated as of wl_output v4.
if (wl_output_get_version(internal->output) < WL_OUTPUT_NAME_SINCE_VERSION &&
internal->display == 0) {
SDL_free(internal->wl_output_name);
internal->wl_output_name = SDL_strdup(name);
}
}
static void xdg_output_handle_description(void *data, struct zxdg_output_v1 *xdg_output,
const char *description)
{
SDL_DisplayData *internal = (SDL_DisplayData *)data;
// Deprecated as of wl_output v4.
if (wl_output_get_version(internal->output) < WL_OUTPUT_DESCRIPTION_SINCE_VERSION &&
internal->display == 0) {
// xdg-output descriptions, if available, supersede wl-output model names.
SDL_free(internal->placeholder.name);
internal->placeholder.name = SDL_strdup(description);
}
}
static const struct zxdg_output_v1_listener xdg_output_listener = {
xdg_output_handle_logical_position,
xdg_output_handle_logical_size,
xdg_output_handle_done,
xdg_output_handle_name,
xdg_output_handle_description,
};
static void AddEmulatedModes(SDL_DisplayData *dispdata, int native_width, int native_height)
{
struct EmulatedMode
{
int w;
int h;
};
// Resolution lists courtesy of XWayland
const struct EmulatedMode mode_list[] = {
// 16:9 (1.77)
{ 7680, 4320 },
{ 6144, 3160 },
{ 5120, 2880 },
{ 4096, 2304 },
{ 3840, 2160 },
{ 3200, 1800 },
{ 2880, 1620 },
{ 2560, 1440 },
{ 2048, 1152 },
{ 1920, 1080 },
{ 1600, 900 },
{ 1368, 768 },
{ 1280, 720 },
{ 864, 486 },
// 16:10 (1.6)
{ 2560, 1600 },
{ 1920, 1200 },
{ 1680, 1050 },
{ 1440, 900 },
{ 1280, 800 },
// 3:2 (1.5)
{ 720, 480 },
// 4:3 (1.33)
{ 2048, 1536 },
{ 1920, 1440 },
{ 1600, 1200 },
{ 1440, 1080 },
{ 1400, 1050 },
{ 1280, 1024 },
{ 1280, 960 },
{ 1152, 864 },
{ 1024, 768 },
{ 800, 600 },
{ 640, 480 }
};
int i;
SDL_DisplayMode mode;
SDL_VideoDisplay *dpy = dispdata->display ? SDL_GetVideoDisplay(dispdata->display) : &dispdata->placeholder;
const bool rot_90 = native_width < native_height; // Reverse width/height for portrait displays.
for (i = 0; i < SDL_arraysize(mode_list); ++i) {
SDL_zero(mode);
mode.format = dpy->desktop_mode.format;
mode.refresh_rate_numerator = dpy->desktop_mode.refresh_rate_numerator;
mode.refresh_rate_denominator = dpy->desktop_mode.refresh_rate_denominator;
if (rot_90) {
mode.w = mode_list[i].h;
mode.h = mode_list[i].w;
} else {
mode.w = mode_list[i].w;
mode.h = mode_list[i].h;
}
// Only add modes that are smaller than the native mode.
if ((mode.w < native_width && mode.h < native_height) ||
(mode.w < native_width && mode.h == native_height) ||
(mode.w == native_width && mode.h < native_height)) {
SDL_AddFullscreenDisplayMode(dpy, &mode);
}
}
}
static void display_handle_geometry(void *data,
struct wl_output *output,
int x, int y,
int physical_width,
int physical_height,
int subpixel,
const char *make,
const char *model,
int transform)
{
SDL_DisplayData *internal = (SDL_DisplayData *)data;
// Apply the change from wl-output only if xdg-output is not supported
if (!internal->has_logical_position) {
internal->x = x;
internal->y = y;
}
internal->physical_width_mm = physical_width;
internal->physical_height_mm = physical_height;
// The model is only used for the output name if wl_output or xdg-output haven't provided a description.
if (internal->display == 0 && !internal->placeholder.name) {
internal->placeholder.name = SDL_strdup(model);
}
internal->transform = transform;
#define TF_CASE(in, out) \
case WL_OUTPUT_TRANSFORM_##in: \
internal->orientation = SDL_ORIENTATION_##out; \
break;
if (internal->physical_width_mm >= internal->physical_height_mm) {
switch (transform) {
TF_CASE(NORMAL, LANDSCAPE)
TF_CASE(90, PORTRAIT)
TF_CASE(180, LANDSCAPE_FLIPPED)
TF_CASE(270, PORTRAIT_FLIPPED)
TF_CASE(FLIPPED, LANDSCAPE_FLIPPED)
TF_CASE(FLIPPED_90, PORTRAIT_FLIPPED)
TF_CASE(FLIPPED_180, LANDSCAPE)
TF_CASE(FLIPPED_270, PORTRAIT)
}
} else {
switch (transform) {
TF_CASE(NORMAL, PORTRAIT)
TF_CASE(90, LANDSCAPE)
TF_CASE(180, PORTRAIT_FLIPPED)
TF_CASE(270, LANDSCAPE_FLIPPED)
TF_CASE(FLIPPED, PORTRAIT_FLIPPED)
TF_CASE(FLIPPED_90, LANDSCAPE_FLIPPED)
TF_CASE(FLIPPED_180, PORTRAIT)
TF_CASE(FLIPPED_270, LANDSCAPE)
}
}
#undef TF_CASE
}
static void display_handle_mode(void *data,
struct wl_output *output,
uint32_t flags,
int width,
int height,
int refresh)
{
SDL_DisplayData *internal = (SDL_DisplayData *)data;
if (flags & WL_OUTPUT_MODE_CURRENT) {
internal->pixel_width = width;
internal->pixel_height = height;
/*
* Don't rotate this yet, wl-output coordinates are transformed in
* handle_done and xdg-output coordinates are pre-transformed.
*/
if (!internal->has_logical_size) {
internal->logical_width = width;
internal->logical_height = height;
}
internal->refresh = refresh;
}
}
static void display_handle_done(void *data,
struct wl_output *output)
{
const bool mode_emulation_enabled = SDL_GetHintBoolean(SDL_HINT_VIDEO_WAYLAND_MODE_EMULATION, true);
SDL_DisplayData *internal = (SDL_DisplayData *)data;
SDL_VideoData *video = internal->videodata;
SDL_DisplayMode native_mode, desktop_mode;
/*
* When using xdg-output, two wl-output.done events will be emitted:
* one at the completion of wl-display and one at the completion of xdg-output.
*
* All required events must be received before proceeding.
*/
const int event_await_count = 1 + (internal->xdg_output != NULL);
internal->wl_output_done_count = SDL_min(internal->wl_output_done_count + 1, event_await_count + 1);
if (internal->wl_output_done_count < event_await_count) {
return;
}
// If the display was already created, reset and rebuild the mode list.
SDL_VideoDisplay *dpy = SDL_GetVideoDisplay(internal->display);
if (dpy) {
SDL_ResetFullscreenDisplayModes(dpy);
}
// The native display resolution
SDL_zero(native_mode);
native_mode.format = SDL_PIXELFORMAT_XRGB8888;
// Transform the pixel values, if necessary.
if (internal->transform & WL_OUTPUT_TRANSFORM_90) {
native_mode.w = internal->pixel_height;
native_mode.h = internal->pixel_width;
} else {
native_mode.w = internal->pixel_width;
native_mode.h = internal->pixel_height;
}
native_mode.refresh_rate_numerator = internal->refresh;
native_mode.refresh_rate_denominator = 1000;
if (internal->has_logical_size) { // If xdg-output is present...
if (native_mode.w != internal->logical_width || native_mode.h != internal->logical_height) {
// ...and the compositor scales the logical viewport...
if (video->viewporter) {
// ...and viewports are supported, calculate the true scale of the output.
internal->scale_factor = (double)native_mode.w / (double)internal->logical_width;
} else {
// ...otherwise, the 'native' pixel values are a multiple of the logical screen size.
internal->pixel_width = internal->logical_width * (int)internal->scale_factor;
internal->pixel_height = internal->logical_height * (int)internal->scale_factor;
}
} else {
/* ...and the output viewport is not scaled in the global compositing
* space, the output dimensions need to be divided by the scale factor.
*/
internal->logical_width /= (int)internal->scale_factor;
internal->logical_height /= (int)internal->scale_factor;
}
} else {
/* Calculate the points from the pixel values, if xdg-output isn't present.
* Use the native mode pixel values since they are pre-transformed.
*/
internal->logical_width = native_mode.w / (int)internal->scale_factor;
internal->logical_height = native_mode.h / (int)internal->scale_factor;
}
// The scaled desktop mode
SDL_zero(desktop_mode);
desktop_mode.format = SDL_PIXELFORMAT_XRGB8888;
if (!video->scale_to_display_enabled) {
desktop_mode.w = internal->logical_width;