forked from libsdl-org/SDL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSDL_waylandwindow.c
2980 lines (2561 loc) · 122 KB
/
SDL_waylandwindow.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 <[email protected]>
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 "../SDL_sysvideo.h"
#include "../../events/SDL_events_c.h"
#include "../../core/unix/SDL_appid.h"
#include "../SDL_egl_c.h"
#include "SDL_waylandevents_c.h"
#include "SDL_waylandwindow.h"
#include "SDL_waylandvideo.h"
#include "../../SDL_hints_c.h"
#include "alpha-modifier-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h"
#include "xdg-decoration-unstable-v1-client-protocol.h"
#include "idle-inhibit-unstable-v1-client-protocol.h"
#include "xdg-activation-v1-client-protocol.h"
#include "viewporter-client-protocol.h"
#include "fractional-scale-v1-client-protocol.h"
#include "xdg-foreign-unstable-v2-client-protocol.h"
#include "xdg-dialog-v1-client-protocol.h"
#include "frog-color-management-v1-client-protocol.h"
#include "xdg-toplevel-icon-v1-client-protocol.h"
#ifdef HAVE_LIBDECOR_H
#include <libdecor.h>
#endif
static double GetWindowScale(SDL_Window *window)
{
return (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) || window->internal->scale_to_display ? window->internal->scale_factor : 1.0;
}
// These are point->pixel->point round trip safe; the inverse is not round trip safe due to rounding.
static int PointToPixel(SDL_Window *window, int point)
{
/* Rounds halfway away from zero as per the Wayland fractional scaling protocol spec.
* Wayland scale units are in units of 1/120, so the offset is required to correct for
* rounding errors when using certain scale values.
*/
return SDL_max((int)SDL_lround((double)point * GetWindowScale(window) + 1e-6), 1);
}
static int PixelToPoint(SDL_Window *window, int pixel)
{
return SDL_max((int)SDL_lround((double)pixel / GetWindowScale(window)), 1);
}
/* According to the Wayland spec:
*
* "If the [fullscreen] surface doesn't cover the whole output, the compositor will
* position the surface in the center of the output and compensate with border fill
* covering the rest of the output. The content of the border fill is undefined, but
* should be assumed to be in some way that attempts to blend into the surrounding area
* (e.g. solid black)."
*
* - KDE, as of 5.27, still doesn't do this
* - GNOME prior to 43 didn't do this (older versions are still found in many LTS distros)
*
* Default to 'stretch' for now, until things have moved forward enough that the default
* can be changed to 'aspect'.
*/
enum WaylandModeScale
{
WAYLAND_MODE_SCALE_UNDEFINED,
WAYLAND_MODE_SCALE_ASPECT,
WAYLAND_MODE_SCALE_STRETCH,
WAYLAND_MODE_SCALE_NONE
};
static enum WaylandModeScale GetModeScaleMethod(void)
{
static enum WaylandModeScale scale_mode = WAYLAND_MODE_SCALE_UNDEFINED;
if (scale_mode == WAYLAND_MODE_SCALE_UNDEFINED) {
const char *scale_hint = SDL_GetHint(SDL_HINT_VIDEO_WAYLAND_MODE_SCALING);
if (scale_hint) {
if (!SDL_strcasecmp(scale_hint, "aspect")) {
scale_mode = WAYLAND_MODE_SCALE_ASPECT;
} else if (!SDL_strcasecmp(scale_hint, "none")) {
scale_mode = WAYLAND_MODE_SCALE_NONE;
} else {
scale_mode = WAYLAND_MODE_SCALE_STRETCH;
}
} else {
scale_mode = WAYLAND_MODE_SCALE_STRETCH;
}
}
return scale_mode;
}
static void GetBufferSize(SDL_Window *window, int *width, int *height)
{
SDL_WindowData *data = window->internal;
int buf_width;
int buf_height;
// Exclusive fullscreen modes always have a pixel density of 1
if (data->is_fullscreen && window->fullscreen_exclusive) {
buf_width = window->current_fullscreen_mode.w;
buf_height = window->current_fullscreen_mode.h;
} else if (!data->scale_to_display) {
// Round fractional backbuffer sizes halfway away from zero.
buf_width = PointToPixel(window, data->requested.logical_width);
buf_height = PointToPixel(window, data->requested.logical_height);
} else {
buf_width = data->requested.pixel_width;
buf_height = data->requested.pixel_height;
}
if (width) {
*width = buf_width;
}
if (height) {
*height = buf_height;
}
}
static void SetMinMaxDimensions(SDL_Window *window)
{
SDL_WindowData *wind = window->internal;
int min_width, min_height, max_width, max_height;
if ((window->flags & SDL_WINDOW_FULLSCREEN) || wind->fullscreen_deadline_count) {
min_width = 0;
min_height = 0;
max_width = 0;
max_height = 0;
} else if (window->flags & SDL_WINDOW_RESIZABLE) {
int adj_w = SDL_max(window->min_w, wind->system_limits.min_width);
int adj_h = SDL_max(window->min_h, wind->system_limits.min_height);
if (wind->scale_to_display) {
adj_w = PixelToPoint(window, adj_w);
adj_h = PixelToPoint(window, adj_h);
}
min_width = adj_w;
min_height = adj_h;
adj_w = window->max_w ? SDL_max(window->max_w, wind->system_limits.min_width) : 0;
adj_h = window->max_h ? SDL_max(window->max_h, wind->system_limits.min_height) : 0;
if (wind->scale_to_display) {
adj_w = PixelToPoint(window, adj_w);
adj_h = PixelToPoint(window, adj_h);
}
max_width = adj_w;
max_height = adj_h;
} else {
min_width = wind->current.logical_width;
min_height = wind->current.logical_height;
max_width = wind->current.logical_width;
max_height = wind->current.logical_height;
}
#ifdef HAVE_LIBDECOR_H
if (wind->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_LIBDECOR) {
if (!wind->shell_surface.libdecor.initial_configure_seen || !wind->shell_surface.libdecor.frame) {
return; // Can't do anything yet, wait for ShowWindow
}
/* No need to change these values if the window is non-resizable,
* as libdecor will just overwrite them internally.
*/
if (libdecor_frame_has_capability(wind->shell_surface.libdecor.frame, LIBDECOR_ACTION_RESIZE)) {
libdecor_frame_set_min_content_size(wind->shell_surface.libdecor.frame,
min_width,
min_height);
libdecor_frame_set_max_content_size(wind->shell_surface.libdecor.frame,
max_width,
max_height);
}
} else
#endif
if (wind->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_XDG_TOPLEVEL) {
if (wind->shell_surface.xdg.toplevel.xdg_toplevel == NULL) {
return; // Can't do anything yet, wait for ShowWindow
}
xdg_toplevel_set_min_size(wind->shell_surface.xdg.toplevel.xdg_toplevel,
min_width,
min_height);
xdg_toplevel_set_max_size(wind->shell_surface.xdg.toplevel.xdg_toplevel,
max_width,
max_height);
}
}
static void EnsurePopupPositionIsValid(SDL_Window *window, int *x, int *y)
{
int adj_count = 0;
/* Per the xdg-positioner spec, child popup windows must intersect or at
* least be partially adjacent to the parent window.
*
* Failure to ensure this on a compositor that enforces this restriction
* can result in behavior ranging from the window being spuriously closed
* to a protocol violation.
*/
if (*x + window->w < 0) {
*x = -window->w;
++adj_count;
}
if (*y + window->h < 0) {
*y = -window->h;
++adj_count;
}
if (*x > window->parent->w) {
*x = window->parent->w;
++adj_count;
}
if (*y > window->parent->h) {
*y = window->parent->h;
++adj_count;
}
/* If adjustment was required on the x and y axes, the popup is aligned with
* the parent corner-to-corner and is neither overlapping nor adjacent, so it
* must be nudged by 1 to be considered adjacent.
*/
if (adj_count > 1) {
*x += *x < 0 ? 1 : -1;
}
}
static void AdjustPopupOffset(SDL_Window *popup, int *x, int *y)
{
// Adjust the popup positioning, if necessary
#ifdef HAVE_LIBDECOR_H
if (popup->parent->internal->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_LIBDECOR) {
int adj_x, adj_y;
libdecor_frame_translate_coordinate(popup->parent->internal->shell_surface.libdecor.frame,
*x, *y, &adj_x, &adj_y);
*x = adj_x;
*y = adj_y;
}
#endif
}
static void RepositionPopup(SDL_Window *window, bool use_current_position)
{
SDL_WindowData *wind = window->internal;
if (wind->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_XDG_POPUP &&
wind->shell_surface.xdg.popup.xdg_positioner &&
xdg_popup_get_version(wind->shell_surface.xdg.popup.xdg_popup) >= XDG_POPUP_REPOSITION_SINCE_VERSION) {
int x = use_current_position ? window->x : window->floating.x;
int y = use_current_position ? window->y : window->floating.y;
EnsurePopupPositionIsValid(window, &x, &y);
if (wind->scale_to_display) {
x = PixelToPoint(window->parent, x);
y = PixelToPoint(window->parent, y);
}
AdjustPopupOffset(window, &x, &y);
xdg_positioner_set_anchor_rect(wind->shell_surface.xdg.popup.xdg_positioner, 0, 0, window->parent->internal->current.logical_width, window->parent->internal->current.logical_height);
xdg_positioner_set_size(wind->shell_surface.xdg.popup.xdg_positioner, wind->current.logical_width, wind->current.logical_height);
xdg_positioner_set_offset(wind->shell_surface.xdg.popup.xdg_positioner, x, y);
xdg_popup_reposition(wind->shell_surface.xdg.popup.xdg_popup,
wind->shell_surface.xdg.popup.xdg_positioner,
0);
}
}
static void SetSurfaceOpaqueRegion(SDL_WindowData *wind, bool is_opaque)
{
SDL_VideoData *viddata = wind->waylandData;
if (is_opaque) {
struct wl_region *region = wl_compositor_create_region(viddata->compositor);
wl_region_add(region, 0, 0,
wind->current.logical_width, wind->current.logical_height);
wl_surface_set_opaque_region(wind->surface, region);
wl_region_destroy(region);
} else {
wl_surface_set_opaque_region(wind->surface, NULL);
}
}
static bool ConfigureWindowGeometry(SDL_Window *window)
{
SDL_WindowData *data = window->internal;
const double scale_factor = GetWindowScale(window);
const int old_pixel_width = data->current.pixel_width;
const int old_pixel_height = data->current.pixel_height;
int window_width, window_height;
bool window_size_changed;
// Throttle interactive resize events to once per refresh cycle to prevent lag.
if (data->resizing) {
data->resizing = false;
if (data->drop_interactive_resizes) {
return false;
} else {
data->drop_interactive_resizes = true;
}
}
// Set the drawable backbuffer size.
GetBufferSize(window, &data->current.pixel_width, &data->current.pixel_height);
const bool buffer_size_changed = data->current.pixel_width != old_pixel_width ||
data->current.pixel_height != old_pixel_height;
if (data->egl_window && buffer_size_changed) {
WAYLAND_wl_egl_window_resize(data->egl_window,
data->current.pixel_width,
data->current.pixel_height,
0, 0);
}
if (data->is_fullscreen && window->fullscreen_exclusive) {
int output_width;
int output_height;
window_width = window->current_fullscreen_mode.w;
window_height = window->current_fullscreen_mode.h;
output_width = data->requested.logical_width;
output_height = data->requested.logical_height;
switch (GetModeScaleMethod()) {
case WAYLAND_MODE_SCALE_NONE:
/* The Wayland spec states that the advertised fullscreen dimensions are a maximum.
* Windows can request a smaller size, but exceeding these dimensions is a protocol violation,
* thus, modes that exceed the output size still need to be scaled with a viewport.
*/
if (window_width <= output_width && window_height <= output_height) {
output_width = window_width;
output_height = window_height;
break;
}
SDL_FALLTHROUGH;
case WAYLAND_MODE_SCALE_ASPECT:
{
const float output_ratio = (float)output_width / (float)output_height;
const float mode_ratio = (float)window_width / (float)window_height;
if (output_ratio > mode_ratio) {
output_width = SDL_lroundf((float)window_width * ((float)output_height / (float)window_height));
} else if (output_ratio < mode_ratio) {
output_height = SDL_lroundf((float)window_height * ((float)output_width / (float)window_width));
}
} break;
default:
break;
}
window_size_changed = window_width != window->w || window_height != window->h ||
data->current.logical_width != output_width || data->current.logical_height != output_height;
if (window_size_changed || buffer_size_changed) {
if (data->viewport) {
wp_viewport_set_destination(data->viewport, output_width, output_height);
data->current.logical_width = output_width;
data->current.logical_height = output_height;
} else {
// Calculate the integer scale from the mode and output.
const int32_t int_scale = SDL_max(window->current_fullscreen_mode.w / output_width, 1);
wl_surface_set_buffer_scale(data->surface, int_scale);
data->current.logical_width = window->current_fullscreen_mode.w;
data->current.logical_height = window->current_fullscreen_mode.h;
}
data->pointer_scale.x = (double)window_width / (double)data->current.logical_width;
data->pointer_scale.y = (double)window_height / (double)data->current.logical_height;
}
} else {
window_width = data->requested.logical_width;
window_height = data->requested.logical_height;
window_size_changed = window_width != data->current.logical_width || window_height != data->current.logical_height;
if (window_size_changed || buffer_size_changed) {
if (data->viewport) {
wp_viewport_set_destination(data->viewport, window_width, window_height);
} else if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
// Don't change this if the DPI awareness flag is unset, as an application may have set this manually on a custom or external surface.
wl_surface_set_buffer_scale(data->surface, (int32_t)scale_factor);
}
// Clamp the physical window size to the system minimum required size.
data->current.logical_width = SDL_max(window_width, data->system_limits.min_width);
data->current.logical_height = SDL_max(window_height, data->system_limits.min_height);
if (!data->scale_to_display) {
data->pointer_scale.x = 1.0;
data->pointer_scale.y = 1.0;
} else {
data->pointer_scale.x = scale_factor;
data->pointer_scale.y = scale_factor;
}
}
}
/*
* The surface geometry, opaque region and pointer confinement region only
* need to be recalculated if the output size has changed.
*/
if (window_size_changed) {
/* XXX: This is a hack and only set on the xdg-toplevel path when viewports
* aren't supported to avoid a potential protocol violation if a buffer
* with an old size is committed.
*/
if (!data->viewport && data->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_XDG_TOPLEVEL && data->shell_surface.xdg.surface) {
xdg_surface_set_window_geometry(data->shell_surface.xdg.surface, 0, 0, data->current.logical_width, data->current.logical_height);
}
SetSurfaceOpaqueRegion(data, !(window->flags & SDL_WINDOW_TRANSPARENT) && window->opacity == 1.0f);
// Ensure that child popup windows are still in bounds.
for (SDL_Window *child = window->first_child; child; child = child->next_sibling) {
RepositionPopup(child, true);
}
}
/* Update the min/max dimensions, primarily if the state was changed, and for non-resizable
* xdg-toplevel windows where the limits should match the window size.
*/
SetMinMaxDimensions(window);
// Unconditionally send the window and drawable size, the video core will deduplicate when required.
if (!data->scale_to_display) {
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, window_width, window_height);
} else {
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, data->current.pixel_width, data->current.pixel_height);
}
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED, data->current.pixel_width, data->current.pixel_height);
/* Send an exposure event if the window is in the shown state and the size has changed,
* even if the window is occluded, as the client needs to commit a new frame for the
* changes to take effect.
*
* The occlusion state is immediately set again afterward, if necessary.
*/
if (data->shell_surface_status == WAYLAND_SHELL_SURFACE_STATUS_SHOWN) {
if ((buffer_size_changed || window_size_changed) ||
(!data->suspended && (window->flags & SDL_WINDOW_OCCLUDED))) {
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_EXPOSED, 0, 0);
}
if (data->suspended) {
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_OCCLUDED, 0, 0);
}
}
return true;
}
static void CommitLibdecorFrame(SDL_Window *window)
{
#ifdef HAVE_LIBDECOR_H
SDL_WindowData *wind = window->internal;
if (wind->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_LIBDECOR && wind->shell_surface.libdecor.frame) {
struct libdecor_state *state = libdecor_state_new(wind->current.logical_width, wind->current.logical_height);
libdecor_frame_commit(wind->shell_surface.libdecor.frame, state, NULL);
libdecor_state_free(state);
}
#endif
}
static void fullscreen_deadline_handler(void *data, struct wl_callback *callback, uint32_t callback_data)
{
// Get the window from the ID as it may have been destroyed
SDL_WindowID windowID = (SDL_WindowID)((uintptr_t)data);
SDL_Window *window = SDL_GetWindowFromID(windowID);
if (window && window->internal) {
window->internal->fullscreen_deadline_count--;
}
wl_callback_destroy(callback);
}
static struct wl_callback_listener fullscreen_deadline_listener = {
fullscreen_deadline_handler
};
static void maximized_deadline_handler(void *data, struct wl_callback *callback, uint32_t callback_data)
{
// Get the window from the ID as it may have been destroyed
SDL_WindowID windowID = (SDL_WindowID)((uintptr_t)data);
SDL_Window *window = SDL_GetWindowFromID(windowID);
if (window && window->internal) {
window->internal->maximized_deadline_count--;
}
wl_callback_destroy(callback);
}
static struct wl_callback_listener maximized_deadline_listener = {
maximized_deadline_handler
};
static void FlushPendingEvents(SDL_Window *window)
{
while (window->internal->fullscreen_deadline_count || window->internal->maximized_deadline_count) {
WAYLAND_wl_display_roundtrip(window->internal->waylandData->display);
}
}
/* While we can't get window position from the compositor, we do at least know
* what monitor we're on, so let's send move events that put the window at the
* center of the whatever display the wl_surface_listener events give us.
*/
static void Wayland_move_window(SDL_Window *window)
{
SDL_WindowData *wind = window->internal;
SDL_DisplayData *display;
SDL_DisplayID *displays;
if (wind->outputs && wind->num_outputs) {
display = wind->outputs[wind->num_outputs - 1];
} else {
// A window may not be on any displays if minimized.
return;
}
displays = SDL_GetDisplays(NULL);
if (displays) {
for (int i = 0; displays[i]; ++i) {
if (SDL_GetDisplayDriverData(displays[i]) == display) {
/* We want to send a very very specific combination here:
*
* 1. A coordinate that tells the application what display we're on
* 2. Exactly (0, 0)
*
* Part 1 is useful information but is also really important for
* ensuring we end up on the right display for fullscreen, while
* part 2 is important because numerous applications use a specific
* combination of GetWindowPosition and GetGlobalMouseState, and of
* course neither are supported by Wayland. Since global mouse will
* fall back to just GetMouseState, we need the window position to
* be zero so the cursor math works without it going off in some
* random direction. See UE5 Editor for a notable example of this!
*
* This may be an issue some day if we're ever able to implement
* SDL_GetDisplayUsableBounds!
*
* -flibit
*/
if (wind->last_displayID != displays[i]) {
wind->last_displayID = displays[i];
if (wind->shell_surface_type != WAYLAND_SHELL_SURFACE_TYPE_XDG_POPUP) {
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MOVED, display->x, display->y);
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_DISPLAY_CHANGED, wind->last_displayID, 0);
}
}
break;
}
}
SDL_free(displays);
}
}
static void SetFullscreen(SDL_Window *window, struct wl_output *output)
{
SDL_WindowData *wind = window->internal;
SDL_VideoData *viddata = wind->waylandData;
#ifdef HAVE_LIBDECOR_H
if (wind->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_LIBDECOR) {
if (!wind->shell_surface.libdecor.frame) {
return; // Can't do anything yet, wait for ShowWindow
}
wind->fullscreen_exclusive = output ? window->fullscreen_exclusive : false;
++wind->fullscreen_deadline_count;
if (output) {
Wayland_SetWindowResizable(SDL_GetVideoDevice(), window, true);
wl_surface_commit(wind->surface);
libdecor_frame_set_fullscreen(wind->shell_surface.libdecor.frame, output);
} else {
libdecor_frame_unset_fullscreen(wind->shell_surface.libdecor.frame);
}
} else
#endif
if (wind->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_XDG_TOPLEVEL) {
if (wind->shell_surface.xdg.toplevel.xdg_toplevel == NULL) {
return; // Can't do anything yet, wait for ShowWindow
}
wind->fullscreen_exclusive = output ? window->fullscreen_exclusive : false;
++wind->fullscreen_deadline_count;
if (output) {
Wayland_SetWindowResizable(SDL_GetVideoDevice(), window, true);
wl_surface_commit(wind->surface);
xdg_toplevel_set_fullscreen(wind->shell_surface.xdg.toplevel.xdg_toplevel, output);
} else {
xdg_toplevel_unset_fullscreen(wind->shell_surface.xdg.toplevel.xdg_toplevel);
}
}
// Queue a deadline event
struct wl_callback *cb = wl_display_sync(viddata->display);
wl_callback_add_listener(cb, &fullscreen_deadline_listener, (void *)((uintptr_t)window->id));
}
static void UpdateWindowFullscreen(SDL_Window *window, bool fullscreen)
{
SDL_WindowData *wind = window->internal;
wind->is_fullscreen = fullscreen;
if (fullscreen) {
if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
SDL_copyp(&window->current_fullscreen_mode, &window->requested_fullscreen_mode);
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_ENTER_FULLSCREEN, 0, 0);
SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_ENTER, false);
/* Set the output for exclusive fullscreen windows when entering fullscreen from a
* compositor event, or if the fullscreen parameters were changed between the initial
* fullscreen request and now, to ensure that the window is on the correct output,
* as requested by the client.
*/
if (window->fullscreen_exclusive && (!wind->fullscreen_exclusive || !wind->fullscreen_was_positioned)) {
SDL_VideoDisplay *disp = SDL_GetVideoDisplay(window->current_fullscreen_mode.displayID);
if (disp) {
wind->fullscreen_was_positioned = true;
SetFullscreen(window, disp->internal->output);
}
}
}
} else {
// Don't change the fullscreen flags if the window is hidden or being hidden.
if ((window->flags & SDL_WINDOW_FULLSCREEN) && !window->is_hiding && !(window->flags & SDL_WINDOW_HIDDEN)) {
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_LEAVE_FULLSCREEN, 0, 0);
SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_LEAVE, false);
wind->fullscreen_was_positioned = false;
/* Send a move event, in case it was deferred while the fullscreen window was moving and
* on multiple outputs.
*/
Wayland_move_window(window);
}
}
}
static const struct wl_callback_listener surface_frame_listener;
static void surface_frame_done(void *data, struct wl_callback *cb, uint32_t time)
{
SDL_WindowData *wind = (SDL_WindowData *)data;
/* XXX: This is needed to work around an Nvidia egl-wayland bug due to buffer coordinates
* being used with wl_surface_damage, which causes part of the output to not be
* updated when using a viewport with an output region larger than the source region.
*/
if (wl_compositor_get_version(wind->waylandData->compositor) >= WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION) {
wl_surface_damage_buffer(wind->surface, 0, 0, SDL_MAX_SINT32, SDL_MAX_SINT32);
} else {
wl_surface_damage(wind->surface, 0, 0, SDL_MAX_SINT32, SDL_MAX_SINT32);
}
wind->drop_interactive_resizes = false;
if (wind->shell_surface_status == WAYLAND_SHELL_SURFACE_STATUS_WAITING_FOR_FRAME) {
wind->shell_surface_status = WAYLAND_SHELL_SURFACE_STATUS_SHOWN;
// If any child windows are waiting on this window to be shown, show them now
for (SDL_Window *w = wind->sdlwindow->first_child; w; w = w->next_sibling) {
if (w->internal->shell_surface_status == WAYLAND_SHELL_SURFACE_STATUS_SHOW_PENDING) {
Wayland_ShowWindow(SDL_GetVideoDevice(), w);
} else if (w->internal->reparenting_required) {
Wayland_SetWindowParent(SDL_GetVideoDevice(), w, w->parent);
if (w->flags & SDL_WINDOW_MODAL) {
Wayland_SetWindowModal(SDL_GetVideoDevice(), w, true);
}
}
}
/* If the window was initially set to the suspended state, send the occluded event now,
* as we don't want to mark the window as occluded until at least one frame has been submitted.
*/
if (wind->suspended) {
SDL_SendWindowEvent(wind->sdlwindow, SDL_EVENT_WINDOW_OCCLUDED, 0, 0);
}
}
wl_callback_destroy(cb);
wind->surface_frame_callback = wl_surface_frame(wind->surface);
wl_callback_add_listener(wind->surface_frame_callback, &surface_frame_listener, data);
}
static const struct wl_callback_listener surface_frame_listener = {
surface_frame_done
};
static const struct wl_callback_listener gles_swap_frame_listener;
static void gles_swap_frame_done(void *data, struct wl_callback *cb, uint32_t time)
{
SDL_WindowData *wind = (SDL_WindowData *)data;
SDL_SetAtomicInt(&wind->swap_interval_ready, 1); // mark window as ready to present again.
// reset this callback to fire again once a new frame was presented and compositor wants the next one.
wind->gles_swap_frame_callback = wl_surface_frame(wind->gles_swap_frame_surface_wrapper);
wl_callback_destroy(cb);
wl_callback_add_listener(wind->gles_swap_frame_callback, &gles_swap_frame_listener, data);
}
static const struct wl_callback_listener gles_swap_frame_listener = {
gles_swap_frame_done
};
static void handle_configure_xdg_shell_surface(void *data, struct xdg_surface *xdg, uint32_t serial)
{
SDL_WindowData *wind = (SDL_WindowData *)data;
SDL_Window *window = wind->sdlwindow;
if (ConfigureWindowGeometry(window)) {
xdg_surface_ack_configure(xdg, serial);
}
wind->shell_surface.xdg.initial_configure_seen = true;
}
static const struct xdg_surface_listener shell_surface_listener_xdg = {
handle_configure_xdg_shell_surface
};
static void handle_configure_xdg_toplevel(void *data,
struct xdg_toplevel *xdg_toplevel,
int32_t width,
int32_t height,
struct wl_array *states)
{
SDL_WindowData *wind = (SDL_WindowData *)data;
SDL_Window *window = wind->sdlwindow;
enum xdg_toplevel_state *state;
bool fullscreen = false;
bool maximized = false;
bool floating = true;
bool tiled = false;
bool active = false;
bool resizing = false;
bool suspended = false;
wl_array_for_each (state, states) {
switch (*state) {
case XDG_TOPLEVEL_STATE_FULLSCREEN:
fullscreen = true;
floating = false;
break;
case XDG_TOPLEVEL_STATE_MAXIMIZED:
maximized = true;
floating = false;
break;
case XDG_TOPLEVEL_STATE_RESIZING:
resizing = true;
break;
case XDG_TOPLEVEL_STATE_ACTIVATED:
active = true;
break;
case XDG_TOPLEVEL_STATE_TILED_LEFT:
case XDG_TOPLEVEL_STATE_TILED_RIGHT:
case XDG_TOPLEVEL_STATE_TILED_TOP:
case XDG_TOPLEVEL_STATE_TILED_BOTTOM:
tiled = true;
floating = false;
break;
case XDG_TOPLEVEL_STATE_SUSPENDED:
suspended = true;
break;
default:
break;
}
}
UpdateWindowFullscreen(window, fullscreen);
/* Always send a maximized/restore event; if the event is redundant it will
* automatically be discarded (see src/events/SDL_windowevents.c)
*
* No, we do not get minimize events from xdg-shell, however, the minimized
* state can be programmatically set. The meaning of 'minimized' is compositor
* dependent, but in general, we can assume that the flag should remain set until
* the next focused configure event occurs.
*/
if (active || !(window->flags & SDL_WINDOW_MINIMIZED)) {
if (window->flags & SDL_WINDOW_MINIMIZED) {
// If we were minimized, send a restored event before possibly sending maximized.
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESTORED, 0, 0);
}
SDL_SendWindowEvent(window,
(maximized && !fullscreen) ? SDL_EVENT_WINDOW_MAXIMIZED : SDL_EVENT_WINDOW_RESTORED,
0, 0);
}
if (!fullscreen) {
/* xdg_toplevel spec states that this is a suggestion.
* Ignore if less than or greater than max/min size.
*/
if (window->flags & SDL_WINDOW_RESIZABLE) {
if ((floating && wind->pending_restored_size) ||
width == 0 || height == 0) {
/* This happens when we're being restored from a non-floating state
* with a pending floating client size, or the compositor indicates
* that the size is up to the client, so use the cached window size here.
*/
if (floating) {
wind->pending_restored_size = false;
width = window->floating.w;
height = window->floating.h;
} else {
width = window->windowed.w;
height = window->windowed.h;
}
if (!wind->scale_to_display) {
wind->requested.logical_width = width;
wind->requested.logical_height = height;
} else {
wind->requested.pixel_width = width;
wind->requested.pixel_height = height;
width = wind->requested.logical_width = PixelToPoint(window, width);
height = wind->requested.logical_height = PixelToPoint(window, height);
}
} else {
/* Don't apply the supplied dimensions if they haven't changed from the last configuration
* event, or a newer size set programmatically can be overwritten by old data.
*/
if (width != wind->last_configure.width || height != wind->last_configure.height) {
wind->requested.logical_width = width;
wind->requested.logical_height = height;
if (wind->scale_to_display) {
wind->requested.pixel_width = PointToPixel(window, width);
wind->requested.pixel_height = PointToPixel(window, height);
}
}
}
} else {
/* If we're a fixed-size window, we know our size for sure.
* Always assume the configure is wrong.
*/
if (!wind->scale_to_display) {
width = wind->requested.logical_width = window->floating.w;
height = wind->requested.logical_height = window->floating.h;
} else {
wind->requested.pixel_width = window->floating.w;
wind->requested.pixel_height = window->floating.h;
width = wind->requested.logical_width = PixelToPoint(window, window->floating.w);
height = wind->requested.logical_height = PixelToPoint(window, window->floating.h);
}
}
/* Notes on the spec:
*
* - The content limits are only a hint, which the compositor is free to ignore,
* so apply them manually when appropriate.
*
* - Maximized windows must have their exact dimensions respected, thus they must
* not be resized, or a protocol violation can occur.
*
* - When resizing a window, the width/height are maximum values, so aspect ratio
* correction can't resize beyond the existing dimensions, or a protocol violation
* can occur. In practice, nothing seems to kill clients that do this, but doing
* so causes GNOME to glitch out.
*/
if (!maximized) {
if (!wind->scale_to_display) {
if (window->max_w > 0) {
wind->requested.logical_width = SDL_min(wind->requested.logical_width, window->max_w);
}
wind->requested.logical_width = SDL_max(wind->requested.logical_width, window->min_w);
if (window->max_h > 0) {
wind->requested.logical_height = SDL_min(wind->requested.logical_height, window->max_h);
}
wind->requested.logical_height = SDL_max(wind->requested.logical_height, window->min_h);
// Aspect correction.
const float aspect = (float)wind->requested.logical_width / (float)wind->requested.logical_height;
if (window->min_aspect != 0.f && aspect < window->min_aspect) {
wind->requested.logical_height = SDL_lroundf((float)wind->requested.logical_width / window->min_aspect);
} else if (window->max_aspect != 0.f && aspect > window->max_aspect) {
wind->requested.logical_width = SDL_lroundf((float)wind->requested.logical_height * window->max_aspect);
}
} else {
if (window->max_w > 0) {
wind->requested.pixel_width = SDL_min(wind->requested.pixel_width, window->max_w);
}
wind->requested.pixel_width = SDL_max(wind->requested.pixel_width, window->min_w);
if (window->max_h > 0) {
wind->requested.pixel_height = SDL_min(wind->requested.pixel_height, window->max_h);
}
wind->requested.pixel_height = SDL_max(wind->requested.pixel_height, window->min_h);
// Aspect correction.
const float aspect = (float)wind->requested.pixel_width / (float)wind->requested.pixel_height;
if (window->min_aspect != 0.f && aspect < window->min_aspect) {
wind->requested.pixel_height = SDL_lroundf((float)wind->requested.pixel_width / window->min_aspect);
} else if (window->max_aspect != 0.f && aspect > window->max_aspect) {
wind->requested.pixel_width = SDL_lroundf((float)wind->requested.pixel_height * window->max_aspect);
}
wind->requested.logical_width = PixelToPoint(window, wind->requested.pixel_width);
wind->requested.logical_height = PixelToPoint(window, wind->requested.pixel_height);
}
}
} else {
// Fullscreen windows know their exact size.
if (width == 0 || height == 0) {
width = wind->requested.logical_width;
height = wind->requested.logical_height;
} else {
wind->requested.logical_width = width;
wind->requested.logical_height = height;
}
if (wind->scale_to_display) {
wind->requested.pixel_width = PointToPixel(window, width);
wind->requested.pixel_height = PointToPixel(window, height);
}
}
wind->last_configure.width = width;
wind->last_configure.height = height;
wind->floating = floating;
wind->suspended = suspended;
wind->active = active;
window->tiled = tiled;
wind->resizing = resizing;
if (wind->shell_surface_status == WAYLAND_SHELL_SURFACE_STATUS_WAITING_FOR_CONFIGURE) {
wind->shell_surface_status = WAYLAND_SHELL_SURFACE_STATUS_WAITING_FOR_FRAME;
}
}
static void handle_close_xdg_toplevel(void *data, struct xdg_toplevel *xdg_toplevel)
{
SDL_WindowData *window = (SDL_WindowData *)data;
SDL_SendWindowEvent(window->sdlwindow, SDL_EVENT_WINDOW_CLOSE_REQUESTED, 0, 0);
}
static void handle_xdg_configure_toplevel_bounds(void *data,
struct xdg_toplevel *xdg_toplevel,
int32_t width, int32_t height)
{
// NOP
}
static void handle_xdg_toplevel_wm_capabilities(void *data,
struct xdg_toplevel *xdg_toplevel,
struct wl_array *capabilities)
{
SDL_WindowData *wind = (SDL_WindowData *)data;
enum xdg_toplevel_wm_capabilities *wm_cap;
wind->wm_caps = 0;
wl_array_for_each (wm_cap, capabilities) {
switch (*wm_cap) {
case XDG_TOPLEVEL_WM_CAPABILITIES_WINDOW_MENU:
wind->wm_caps |= WAYLAND_WM_CAPS_WINDOW_MENU;
break;
case XDG_TOPLEVEL_WM_CAPABILITIES_MAXIMIZE:
wind->wm_caps |= WAYLAND_WM_CAPS_MAXIMIZE;
break;
case XDG_TOPLEVEL_WM_CAPABILITIES_FULLSCREEN:
wind->wm_caps |= WAYLAND_WM_CAPS_FULLSCREEN;
break;
case XDG_TOPLEVEL_WM_CAPABILITIES_MINIMIZE:
wind->wm_caps |= WAYLAND_WM_CAPS_MINIMIZE;
break;
default:
break;
}
}