1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22#include "../../SDL_internal.h"
23
24#if SDL_VIDEO_DRIVER_WAYLAND && SDL_VIDEO_OPENGL_EGL
25
26#include "../SDL_sysvideo.h"
27#include "../../events/SDL_windowevents_c.h"
28#include "../SDL_egl_c.h"
29#include "SDL_waylandevents_c.h"
30#include "SDL_waylandwindow.h"
31#include "SDL_waylandvideo.h"
32#include "SDL_waylandtouch.h"
33#include "SDL_waylanddyn.h"
34#include "SDL_hints.h"
35
36#include "xdg-shell-client-protocol.h"
37#include "xdg-shell-unstable-v6-client-protocol.h"
38#include "xdg-decoration-unstable-v1-client-protocol.h"
39#include "idle-inhibit-unstable-v1-client-protocol.h"
40
41static float get_window_scale_factor(SDL_Window *window) {
42 return ((SDL_WindowData*)window->driverdata)->scale_factor;
43}
44
45static void
46CommitMinMaxDimensions(SDL_Window *window)
47{
48 SDL_WindowData *wind = window->driverdata;
49 SDL_VideoData *data = wind->waylandData;
50 int min_width, min_height, max_width, max_height;
51
52 if (window->flags & SDL_WINDOW_FULLSCREEN) {
53 min_width = 0;
54 min_height = 0;
55 max_width = 0;
56 max_height = 0;
57 } else if (window->flags & SDL_WINDOW_RESIZABLE) {
58 min_width = window->min_w;
59 min_height = window->min_h;
60 max_width = window->max_w;
61 max_height = window->max_h;
62 } else {
63 min_width = window->w;
64 min_height = window->h;
65 max_width = window->w;
66 max_height = window->h;
67 }
68
69 if (data->shell.xdg) {
70 xdg_toplevel_set_min_size(wind->shell_surface.xdg.roleobj.toplevel,
71 min_width,
72 min_height);
73 xdg_toplevel_set_max_size(wind->shell_surface.xdg.roleobj.toplevel,
74 max_width,
75 max_height);
76 } else if (data->shell.zxdg) {
77 zxdg_toplevel_v6_set_min_size(wind->shell_surface.zxdg.roleobj.toplevel,
78 min_width,
79 min_height);
80 zxdg_toplevel_v6_set_max_size(wind->shell_surface.zxdg.roleobj.toplevel,
81 max_width,
82 max_height);
83 }
84
85 wl_surface_commit(wind->surface);
86}
87
88static void
89SetFullscreen(SDL_Window *window, struct wl_output *output)
90{
91 SDL_WindowData *wind = window->driverdata;
92 SDL_VideoData *viddata = wind->waylandData;
93
94 /* The desktop may try to enforce min/max sizes here, so turn them off for
95 * fullscreen and on (if applicable) for windowed
96 */
97 CommitMinMaxDimensions(window);
98
99 if (viddata->shell.xdg) {
100 if (output) {
101 xdg_toplevel_set_fullscreen(wind->shell_surface.xdg.roleobj.toplevel, output);
102 } else {
103 xdg_toplevel_unset_fullscreen(wind->shell_surface.xdg.roleobj.toplevel);
104 }
105 } else if (viddata->shell.zxdg) {
106 if (output) {
107 zxdg_toplevel_v6_set_fullscreen(wind->shell_surface.zxdg.roleobj.toplevel, output);
108 } else {
109 zxdg_toplevel_v6_unset_fullscreen(wind->shell_surface.zxdg.roleobj.toplevel);
110 }
111 } else {
112 if (output) {
113 wl_shell_surface_set_fullscreen(wind->shell_surface.wl,
114 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
115 0, output);
116 } else {
117 wl_shell_surface_set_toplevel(wind->shell_surface.wl);
118 }
119 }
120
121 WAYLAND_wl_display_flush(viddata->display);
122}
123
124/* On modern desktops, we probably will use the xdg-shell protocol instead
125 of wl_shell, but wl_shell might be useful on older Wayland installs that
126 don't have the newer protocol, or embedded things that don't have a full
127 window manager. */
128
129static void
130handle_ping_wl_shell_surface(void *data, struct wl_shell_surface *shell_surface,
131 uint32_t serial)
132{
133 wl_shell_surface_pong(shell_surface, serial);
134}
135
136static void
137handle_configure_wl_shell_surface(void *data, struct wl_shell_surface *shell_surface,
138 uint32_t edges, int32_t width, int32_t height)
139{
140 SDL_WindowData *wind = (SDL_WindowData *)data;
141 SDL_Window *window = wind->sdlwindow;
142
143 /* wl_shell_surface spec states that this is a suggestion.
144 Ignore if less than or greater than max/min size. */
145
146 if (width == 0 || height == 0) {
147 return;
148 }
149
150 if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
151 if ((window->flags & SDL_WINDOW_RESIZABLE)) {
152 if (window->max_w > 0) {
153 width = SDL_min(width, window->max_w);
154 }
155 width = SDL_max(width, window->min_w);
156
157 if (window->max_h > 0) {
158 height = SDL_min(height, window->max_h);
159 }
160 height = SDL_max(height, window->min_h);
161 } else {
162 return;
163 }
164 }
165
166 wind->resize.width = width;
167 wind->resize.height = height;
168 wind->resize.pending = SDL_TRUE;
169
170 if (!(window->flags & SDL_WINDOW_OPENGL)) {
171 Wayland_HandlePendingResize(window); /* OpenGL windows handle this in SwapWindow */
172 }
173}
174
175static void
176handle_popup_done_wl_shell_surface(void *data, struct wl_shell_surface *shell_surface)
177{
178}
179
180static const struct wl_shell_surface_listener shell_surface_listener_wl = {
181 handle_ping_wl_shell_surface,
182 handle_configure_wl_shell_surface,
183 handle_popup_done_wl_shell_surface
184};
185
186
187
188
189static void
190handle_configure_zxdg_shell_surface(void *data, struct zxdg_surface_v6 *zxdg, uint32_t serial)
191{
192 SDL_WindowData *wind = (SDL_WindowData *)data;
193 SDL_Window *window = wind->sdlwindow;
194 struct wl_region *region;
195
196 if (!wind->shell_surface.zxdg.initial_configure_seen) {
197 window->w = 0;
198 window->h = 0;
199 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, wind->resize.width, wind->resize.height);
200 window->w = wind->resize.width;
201 window->h = wind->resize.height;
202
203 wl_surface_set_buffer_scale(wind->surface, get_window_scale_factor(window));
204 if (wind->egl_window) {
205 WAYLAND_wl_egl_window_resize(wind->egl_window, window->w * get_window_scale_factor(window), window->h * get_window_scale_factor(window), 0, 0);
206 }
207
208 zxdg_surface_v6_ack_configure(zxdg, serial);
209
210 region = wl_compositor_create_region(wind->waylandData->compositor);
211 wl_region_add(region, 0, 0, window->w, window->h);
212 wl_surface_set_opaque_region(wind->surface, region);
213 wl_region_destroy(region);
214
215 wind->shell_surface.zxdg.initial_configure_seen = SDL_TRUE;
216 } else {
217 wind->resize.pending = SDL_TRUE;
218 wind->resize.configure = SDL_TRUE;
219 wind->resize.serial = serial;
220 if (!(window->flags & SDL_WINDOW_OPENGL)) {
221 Wayland_HandlePendingResize(window); /* OpenGL windows handle this in SwapWindow */
222 }
223 }
224}
225
226static const struct zxdg_surface_v6_listener shell_surface_listener_zxdg = {
227 handle_configure_zxdg_shell_surface
228};
229
230
231static void
232handle_configure_zxdg_toplevel(void *data,
233 struct zxdg_toplevel_v6 *zxdg_toplevel_v6,
234 int32_t width,
235 int32_t height,
236 struct wl_array *states)
237{
238 SDL_WindowData *wind = (SDL_WindowData *)data;
239 SDL_Window *window = wind->sdlwindow;
240
241 enum zxdg_toplevel_v6_state *state;
242 SDL_bool fullscreen = SDL_FALSE;
243 wl_array_for_each(state, states) {
244 if (*state == ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN) {
245 fullscreen = SDL_TRUE;
246 }
247 }
248
249 if (!fullscreen) {
250 if (window->flags & SDL_WINDOW_FULLSCREEN) {
251 /* We might need to re-enter fullscreen after being restored from minimized */
252 SDL_WaylandOutputData *driverdata = (SDL_WaylandOutputData *) SDL_GetDisplayForWindow(window)->driverdata;
253 SetFullscreen(window, driverdata->output);
254 }
255
256 if (width == 0 || height == 0) {
257 width = window->windowed.w;
258 height = window->windowed.h;
259 }
260
261 /* zxdg_toplevel spec states that this is a suggestion.
262 Ignore if less than or greater than max/min size. */
263
264 if ((window->flags & SDL_WINDOW_RESIZABLE)) {
265 if (window->max_w > 0) {
266 width = SDL_min(width, window->max_w);
267 }
268 width = SDL_max(width, window->min_w);
269
270 if (window->max_h > 0) {
271 height = SDL_min(height, window->max_h);
272 }
273 height = SDL_max(height, window->min_h);
274 } else {
275 wind->resize.width = window->w;
276 wind->resize.height = window->h;
277 return;
278 }
279 }
280
281 if (width == 0 || height == 0) {
282 wind->resize.width = window->w;
283 wind->resize.height = window->h;
284 return;
285 }
286
287 wind->resize.width = width;
288 wind->resize.height = height;
289}
290
291static void
292handle_close_zxdg_toplevel(void *data, struct zxdg_toplevel_v6 *zxdg_toplevel_v6)
293{
294 SDL_WindowData *window = (SDL_WindowData *)data;
295 SDL_SendWindowEvent(window->sdlwindow, SDL_WINDOWEVENT_CLOSE, 0, 0);
296}
297
298static const struct zxdg_toplevel_v6_listener toplevel_listener_zxdg = {
299 handle_configure_zxdg_toplevel,
300 handle_close_zxdg_toplevel
301};
302
303
304
305static void
306handle_configure_xdg_shell_surface(void *data, struct xdg_surface *xdg, uint32_t serial)
307{
308 SDL_WindowData *wind = (SDL_WindowData *)data;
309 SDL_Window *window = wind->sdlwindow;
310 struct wl_region *region;
311
312 if (!wind->shell_surface.xdg.initial_configure_seen) {
313 window->w = 0;
314 window->h = 0;
315 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, wind->resize.width, wind->resize.height);
316 window->w = wind->resize.width;
317 window->h = wind->resize.height;
318
319 wl_surface_set_buffer_scale(wind->surface, get_window_scale_factor(window));
320 if (wind->egl_window) {
321 WAYLAND_wl_egl_window_resize(wind->egl_window, window->w * get_window_scale_factor(window), window->h * get_window_scale_factor(window), 0, 0);
322 }
323
324 xdg_surface_ack_configure(xdg, serial);
325
326 region = wl_compositor_create_region(wind->waylandData->compositor);
327 wl_region_add(region, 0, 0, window->w, window->h);
328 wl_surface_set_opaque_region(wind->surface, region);
329 wl_region_destroy(region);
330
331 wind->shell_surface.xdg.initial_configure_seen = SDL_TRUE;
332 } else {
333 wind->resize.pending = SDL_TRUE;
334 wind->resize.configure = SDL_TRUE;
335 wind->resize.serial = serial;
336 if (!(window->flags & SDL_WINDOW_OPENGL)) {
337 Wayland_HandlePendingResize(window); /* OpenGL windows handle this in SwapWindow */
338 }
339 }
340}
341
342static const struct xdg_surface_listener shell_surface_listener_xdg = {
343 handle_configure_xdg_shell_surface
344};
345
346
347static void
348handle_configure_xdg_toplevel(void *data,
349 struct xdg_toplevel *xdg_toplevel,
350 int32_t width,
351 int32_t height,
352 struct wl_array *states)
353{
354 SDL_WindowData *wind = (SDL_WindowData *)data;
355 SDL_Window *window = wind->sdlwindow;
356
357 enum xdg_toplevel_state *state;
358 SDL_bool fullscreen = SDL_FALSE;
359 wl_array_for_each(state, states) {
360 if (*state == XDG_TOPLEVEL_STATE_FULLSCREEN) {
361 fullscreen = SDL_TRUE;
362 }
363 }
364
365 if (!fullscreen) {
366 if (window->flags & SDL_WINDOW_FULLSCREEN) {
367 /* We might need to re-enter fullscreen after being restored from minimized */
368 SDL_WaylandOutputData *driverdata = (SDL_WaylandOutputData *) SDL_GetDisplayForWindow(window)->driverdata;
369 SetFullscreen(window, driverdata->output);
370 }
371
372 if (width == 0 || height == 0) {
373 width = window->windowed.w;
374 height = window->windowed.h;
375 }
376
377 /* xdg_toplevel spec states that this is a suggestion.
378 Ignore if less than or greater than max/min size. */
379
380 if ((window->flags & SDL_WINDOW_RESIZABLE)) {
381 if (window->max_w > 0) {
382 width = SDL_min(width, window->max_w);
383 }
384 width = SDL_max(width, window->min_w);
385
386 if (window->max_h > 0) {
387 height = SDL_min(height, window->max_h);
388 }
389 height = SDL_max(height, window->min_h);
390 } else {
391 wind->resize.width = window->w;
392 wind->resize.height = window->h;
393 return;
394 }
395 }
396
397 if (width == 0 || height == 0) {
398 wind->resize.width = window->w;
399 wind->resize.height = window->h;
400 return;
401 }
402
403 wind->resize.width = width;
404 wind->resize.height = height;
405}
406
407static void
408handle_close_xdg_toplevel(void *data, struct xdg_toplevel *xdg_toplevel)
409{
410 SDL_WindowData *window = (SDL_WindowData *)data;
411 SDL_SendWindowEvent(window->sdlwindow, SDL_WINDOWEVENT_CLOSE, 0, 0);
412}
413
414static const struct xdg_toplevel_listener toplevel_listener_xdg = {
415 handle_configure_xdg_toplevel,
416 handle_close_xdg_toplevel
417};
418
419
420
421
422#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
423static void
424handle_onscreen_visibility(void *data,
425 struct qt_extended_surface *qt_extended_surface, int32_t visible)
426{
427}
428
429static void
430handle_set_generic_property(void *data,
431 struct qt_extended_surface *qt_extended_surface, const char *name,
432 struct wl_array *value)
433{
434}
435
436static void
437handle_close(void *data, struct qt_extended_surface *qt_extended_surface)
438{
439 SDL_WindowData *window = (SDL_WindowData *)data;
440 SDL_SendWindowEvent(window->sdlwindow, SDL_WINDOWEVENT_CLOSE, 0, 0);
441}
442
443static const struct qt_extended_surface_listener extended_surface_listener = {
444 handle_onscreen_visibility,
445 handle_set_generic_property,
446 handle_close,
447};
448#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
449
450static void
451update_scale_factor(SDL_WindowData *window) {
452 float old_factor = window->scale_factor, new_factor = 0.0;
453 int i;
454
455 if (!(window->sdlwindow->flags & SDL_WINDOW_ALLOW_HIGHDPI)) {
456 return;
457 }
458
459 if (!window->num_outputs) {
460 new_factor = old_factor;
461 }
462
463 if (FULLSCREEN_VISIBLE(window->sdlwindow)) {
464 SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window->sdlwindow);
465 SDL_WaylandOutputData* driverdata = display->driverdata;
466 new_factor = driverdata->scale_factor;
467 }
468
469 for (i = 0; i < window->num_outputs; i++) {
470 SDL_WaylandOutputData* driverdata = wl_output_get_user_data(window->outputs[i]);
471 float factor = driverdata->scale_factor;
472 if (factor > new_factor) {
473 new_factor = factor;
474 }
475 }
476
477 if (new_factor != old_factor) {
478 /* force the resize event to trigger, as the logical size didn't change */
479 window->resize.width = window->sdlwindow->w;
480 window->resize.height = window->sdlwindow->h;
481 window->resize.scale_factor = new_factor;
482 window->resize.pending = SDL_TRUE;
483 if (!(window->sdlwindow->flags & SDL_WINDOW_OPENGL)) {
484 Wayland_HandlePendingResize(window->sdlwindow); /* OpenGL windows handle this in SwapWindow */
485 }
486 }
487}
488
489/* While we can't get window position from the compositor, we do at least know
490 * what monitor we're on, so let's send move events that put the window at the
491 * center of the whatever display the wl_surface_listener events give us.
492 */
493static void
494Wayland_move_window(SDL_Window *window,
495 SDL_WaylandOutputData *driverdata)
496{
497 int i, numdisplays = SDL_GetNumVideoDisplays();
498 for (i = 0; i < numdisplays; i += 1) {
499 if (SDL_GetDisplay(i)->driverdata == driverdata) {
500 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED,
501 SDL_WINDOWPOS_CENTERED_DISPLAY(i),
502 SDL_WINDOWPOS_CENTERED_DISPLAY(i));
503 break;
504 }
505 }
506}
507
508static void
509handle_surface_enter(void *data, struct wl_surface *surface,
510 struct wl_output *output)
511{
512 SDL_WindowData *window = data;
513
514 window->outputs = SDL_realloc(window->outputs, (window->num_outputs + 1) * sizeof *window->outputs);
515 window->outputs[window->num_outputs++] = output;
516 update_scale_factor(window);
517
518 Wayland_move_window(window->sdlwindow, wl_output_get_user_data(output));
519}
520
521static void
522handle_surface_leave(void *data, struct wl_surface *surface,
523 struct wl_output *output)
524{
525 SDL_WindowData *window = data;
526 int i, send_move_event = 0;
527
528 for (i = 0; i < window->num_outputs; i++) {
529 if (window->outputs[i] == output) { /* remove this one */
530 if (i == (window->num_outputs-1)) {
531 window->outputs[i] = NULL;
532 send_move_event = 1;
533 } else {
534 SDL_memmove(&window->outputs[i], &window->outputs[i+1], sizeof (output) * ((window->num_outputs - i) - 1));
535 }
536 window->num_outputs--;
537 i--;
538 }
539 }
540
541 if (window->num_outputs == 0) {
542 SDL_free(window->outputs);
543 window->outputs = NULL;
544 } else if (send_move_event) {
545 Wayland_move_window(window->sdlwindow,
546 wl_output_get_user_data(window->outputs[window->num_outputs - 1]));
547 }
548
549 update_scale_factor(window);
550}
551
552static const struct wl_surface_listener surface_listener = {
553 handle_surface_enter,
554 handle_surface_leave
555};
556
557SDL_bool
558Wayland_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
559{
560 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
561 const Uint32 version = SDL_VERSIONNUM((Uint32)info->version.major,
562 (Uint32)info->version.minor,
563 (Uint32)info->version.patch);
564
565 /* Before 2.0.6, it was possible to build an SDL with Wayland support
566 (SDL_SysWMinfo will be large enough to hold Wayland info), but build
567 your app against SDL headers that didn't have Wayland support
568 (SDL_SysWMinfo could be smaller than Wayland needs. This would lead
569 to an app properly using SDL_GetWindowWMInfo() but we'd accidentally
570 overflow memory on the stack or heap. To protect against this, we've
571 padded out the struct unconditionally in the headers and Wayland will
572 just return an error for older apps using this function. Those apps
573 will need to be recompiled against newer headers or not use Wayland,
574 maybe by forcing SDL_VIDEODRIVER=x11. */
575 if (version < SDL_VERSIONNUM(2, 0, 6)) {
576 info->subsystem = SDL_SYSWM_UNKNOWN;
577 SDL_SetError("Version must be 2.0.6 or newer");
578 return SDL_FALSE;
579 }
580
581 info->info.wl.display = data->waylandData->display;
582 info->info.wl.surface = data->surface;
583 info->info.wl.shell_surface = data->shell_surface.wl;
584 if (version >= SDL_VERSIONNUM(2, 0, 15)) {
585 info->info.wl.egl_window = data->egl_window;
586 }
587 info->subsystem = SDL_SYSWM_WAYLAND;
588
589 return SDL_TRUE;
590}
591
592int
593Wayland_SetWindowHitTest(SDL_Window *window, SDL_bool enabled)
594{
595 return 0; /* just succeed, the real work is done elsewhere. */
596}
597
598void Wayland_ShowWindow(_THIS, SDL_Window *window)
599{
600 SDL_WaylandOutputData *driverdata = (SDL_WaylandOutputData *) SDL_GetDisplayForWindow(window)->driverdata;
601 SetFullscreen(window, (window->flags & SDL_WINDOW_FULLSCREEN) ? driverdata->output : NULL);
602}
603
604#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
605static void SDLCALL
606QtExtendedSurface_OnHintChanged(void *userdata, const char *name,
607 const char *oldValue, const char *newValue)
608{
609 struct qt_extended_surface *qt_extended_surface = userdata;
610
611 if (name == NULL) {
612 return;
613 }
614
615 if (strcmp(name, SDL_HINT_QTWAYLAND_CONTENT_ORIENTATION) == 0) {
616 int32_t orientation = QT_EXTENDED_SURFACE_ORIENTATION_PRIMARYORIENTATION;
617
618 if (newValue != NULL) {
619 if (strcmp(newValue, "portrait") == 0) {
620 orientation = QT_EXTENDED_SURFACE_ORIENTATION_PORTRAITORIENTATION;
621 } else if (strcmp(newValue, "landscape") == 0) {
622 orientation = QT_EXTENDED_SURFACE_ORIENTATION_LANDSCAPEORIENTATION;
623 } else if (strcmp(newValue, "inverted-portrait") == 0) {
624 orientation = QT_EXTENDED_SURFACE_ORIENTATION_INVERTEDPORTRAITORIENTATION;
625 } else if (strcmp(newValue, "inverted-landscape") == 0) {
626 orientation = QT_EXTENDED_SURFACE_ORIENTATION_INVERTEDLANDSCAPEORIENTATION;
627 }
628 }
629
630 qt_extended_surface_set_content_orientation(qt_extended_surface, orientation);
631 } else if (strcmp(name, SDL_HINT_QTWAYLAND_WINDOW_FLAGS) == 0) {
632 uint32_t flags = 0;
633
634 if (newValue != NULL) {
635 char *tmp = strdup(newValue);
636 char *saveptr = NULL;
637
638 char *flag = strtok_r(tmp, " ", &saveptr);
639 while (flag) {
640 if (strcmp(flag, "OverridesSystemGestures") == 0) {
641 flags |= QT_EXTENDED_SURFACE_WINDOWFLAG_OVERRIDESSYSTEMGESTURES;
642 } else if (strcmp(flag, "StaysOnTop") == 0) {
643 flags |= QT_EXTENDED_SURFACE_WINDOWFLAG_STAYSONTOP;
644 } else if (strcmp(flag, "BypassWindowManager") == 0) {
645 // See https://github.com/qtproject/qtwayland/commit/fb4267103d
646 flags |= 4 /* QT_EXTENDED_SURFACE_WINDOWFLAG_BYPASSWINDOWMANAGER */;
647 }
648
649 flag = strtok_r(NULL, " ", &saveptr);
650 }
651
652 free(tmp);
653 }
654
655 qt_extended_surface_set_window_flags(qt_extended_surface, flags);
656 }
657}
658
659static void QtExtendedSurface_Subscribe(struct qt_extended_surface *surface, const char *name)
660{
661 SDL_AddHintCallback(name, QtExtendedSurface_OnHintChanged, surface);
662}
663
664static void QtExtendedSurface_Unsubscribe(struct qt_extended_surface *surface, const char *name)
665{
666 SDL_DelHintCallback(name, QtExtendedSurface_OnHintChanged, surface);
667}
668#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
669
670void
671Wayland_SetWindowFullscreen(_THIS, SDL_Window * window,
672 SDL_VideoDisplay * _display, SDL_bool fullscreen)
673{
674 struct wl_output *output = ((SDL_WaylandOutputData*) _display->driverdata)->output;
675 SetFullscreen(window, fullscreen ? output : NULL);
676}
677
678void
679Wayland_RestoreWindow(_THIS, SDL_Window * window)
680{
681 SDL_WindowData *wind = window->driverdata;
682 const SDL_VideoData *viddata = (const SDL_VideoData *) _this->driverdata;
683
684 if (viddata->shell.xdg) {
685 } else if (viddata->shell.zxdg) {
686 } else {
687 wl_shell_surface_set_toplevel(wind->shell_surface.wl);
688 }
689
690 WAYLAND_wl_display_flush( ((SDL_VideoData*)_this->driverdata)->display );
691}
692
693void
694Wayland_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
695{
696 SDL_WindowData *wind = window->driverdata;
697 const SDL_VideoData *viddata = (const SDL_VideoData *) _this->driverdata;
698 if ((viddata->decoration_manager) && (wind->server_decoration)) {
699 const enum zxdg_toplevel_decoration_v1_mode mode = bordered ? ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE : ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE;
700 zxdg_toplevel_decoration_v1_set_mode(wind->server_decoration, mode);
701 }
702}
703
704void
705Wayland_SetWindowResizable(_THIS, SDL_Window * window, SDL_bool resizable)
706{
707 CommitMinMaxDimensions(window);
708}
709
710void
711Wayland_MaximizeWindow(_THIS, SDL_Window * window)
712{
713 SDL_WindowData *wind = window->driverdata;
714 SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
715
716 if (viddata->shell.xdg) {
717 xdg_toplevel_set_maximized(wind->shell_surface.xdg.roleobj.toplevel);
718 } else if (viddata->shell.zxdg) {
719 zxdg_toplevel_v6_set_maximized(wind->shell_surface.zxdg.roleobj.toplevel);
720 } else {
721 wl_shell_surface_set_maximized(wind->shell_surface.wl, NULL);
722 }
723
724 WAYLAND_wl_display_flush( viddata->display );
725}
726
727void
728Wayland_MinimizeWindow(_THIS, SDL_Window * window)
729{
730 SDL_WindowData *wind = window->driverdata;
731 SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
732
733 if (viddata->shell.xdg) {
734 xdg_toplevel_set_minimized(wind->shell_surface.xdg.roleobj.toplevel);
735 } else if (viddata->shell.zxdg) {
736 zxdg_toplevel_v6_set_minimized(wind->shell_surface.zxdg.roleobj.toplevel);
737 }
738
739 WAYLAND_wl_display_flush(viddata->display);
740}
741
742void
743Wayland_SetWindowMouseGrab(_THIS, SDL_Window *window, SDL_bool grabbed)
744{
745 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
746
747 if (grabbed) {
748 Wayland_input_confine_pointer(window, data->input);
749 } else {
750 Wayland_input_unconfine_pointer(data->input);
751 }
752}
753
754void
755Wayland_SetWindowKeyboardGrab(_THIS, SDL_Window *window, SDL_bool grabbed)
756{
757 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
758
759 if (grabbed) {
760 Wayland_input_grab_keyboard(window, data->input);
761 } else {
762 Wayland_input_ungrab_keyboard(window);
763 }
764}
765
766int Wayland_CreateWindow(_THIS, SDL_Window *window)
767{
768 SDL_WindowData *data;
769 SDL_VideoData *c;
770 struct wl_region *region;
771
772 data = SDL_calloc(1, sizeof *data);
773 if (data == NULL)
774 return SDL_OutOfMemory();
775
776 c = _this->driverdata;
777 window->driverdata = data;
778
779 if (!(window->flags & SDL_WINDOW_VULKAN)) {
780 if (!(window->flags & SDL_WINDOW_OPENGL)) {
781 SDL_GL_LoadLibrary(NULL);
782 window->flags |= SDL_WINDOW_OPENGL;
783 }
784 }
785
786 if (window->x == SDL_WINDOWPOS_UNDEFINED) {
787 window->x = 0;
788 }
789 if (window->y == SDL_WINDOWPOS_UNDEFINED) {
790 window->y = 0;
791 }
792
793 data->waylandData = c;
794 data->sdlwindow = window;
795
796 data->scale_factor = 1.0;
797
798 if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
799 int i;
800 for (i=0; i < SDL_GetVideoDevice()->num_displays; i++) {
801 float scale = ((SDL_WaylandOutputData*)SDL_GetVideoDevice()->displays[i].driverdata)->scale_factor;
802 if (scale > data->scale_factor) {
803 data->scale_factor = scale;
804 }
805 }
806 }
807
808 data->resize.pending = SDL_FALSE;
809 data->resize.width = window->w;
810 data->resize.height = window->h;
811 data->resize.scale_factor = data->scale_factor;
812
813 data->outputs = NULL;
814 data->num_outputs = 0;
815
816 data->surface =
817 wl_compositor_create_surface(c->compositor);
818 wl_surface_add_listener(data->surface, &surface_listener, data);
819
820 if (c->shell.xdg) {
821 data->shell_surface.xdg.surface = xdg_wm_base_get_xdg_surface(c->shell.xdg, data->surface);
822 /* !!! FIXME: add popup role */
823 data->shell_surface.xdg.roleobj.toplevel = xdg_surface_get_toplevel(data->shell_surface.xdg.surface);
824 xdg_toplevel_add_listener(data->shell_surface.xdg.roleobj.toplevel, &toplevel_listener_xdg, data);
825 xdg_toplevel_set_app_id(data->shell_surface.xdg.roleobj.toplevel, c->classname);
826 } else if (c->shell.zxdg) {
827 data->shell_surface.zxdg.surface = zxdg_shell_v6_get_xdg_surface(c->shell.zxdg, data->surface);
828 /* !!! FIXME: add popup role */
829 data->shell_surface.zxdg.roleobj.toplevel = zxdg_surface_v6_get_toplevel(data->shell_surface.zxdg.surface);
830 zxdg_toplevel_v6_add_listener(data->shell_surface.zxdg.roleobj.toplevel, &toplevel_listener_zxdg, data);
831 zxdg_toplevel_v6_set_app_id(data->shell_surface.zxdg.roleobj.toplevel, c->classname);
832 } else {
833 data->shell_surface.wl = wl_shell_get_shell_surface(c->shell.wl, data->surface);
834 wl_shell_surface_set_class(data->shell_surface.wl, c->classname);
835 }
836
837#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
838 if (c->surface_extension) {
839 data->extended_surface = qt_surface_extension_get_extended_surface(
840 c->surface_extension, data->surface);
841
842 QtExtendedSurface_Subscribe(data->extended_surface, SDL_HINT_QTWAYLAND_CONTENT_ORIENTATION);
843 QtExtendedSurface_Subscribe(data->extended_surface, SDL_HINT_QTWAYLAND_WINDOW_FLAGS);
844 }
845#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
846
847 if (window->flags & SDL_WINDOW_OPENGL) {
848 data->egl_window = WAYLAND_wl_egl_window_create(data->surface,
849 window->w * data->scale_factor, window->h * data->scale_factor);
850
851 /* Create the GLES window surface */
852 data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->egl_window);
853
854 if (data->egl_surface == EGL_NO_SURFACE) {
855 return SDL_SetError("failed to create an EGL window surface");
856 }
857 }
858
859 if (c->shell.xdg) {
860 if (data->shell_surface.xdg.surface) {
861 xdg_surface_set_user_data(data->shell_surface.xdg.surface, data);
862 xdg_surface_add_listener(data->shell_surface.xdg.surface, &shell_surface_listener_xdg, data);
863 }
864 } else if (c->shell.zxdg) {
865 if (data->shell_surface.zxdg.surface) {
866 zxdg_surface_v6_set_user_data(data->shell_surface.zxdg.surface, data);
867 zxdg_surface_v6_add_listener(data->shell_surface.zxdg.surface, &shell_surface_listener_zxdg, data);
868 }
869 } else {
870 if (data->shell_surface.wl) {
871 wl_shell_surface_set_user_data(data->shell_surface.wl, data);
872 wl_shell_surface_add_listener(data->shell_surface.wl, &shell_surface_listener_wl, data);
873 }
874 }
875
876#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
877 if (data->extended_surface) {
878 qt_extended_surface_set_user_data(data->extended_surface, data);
879 qt_extended_surface_add_listener(data->extended_surface,
880 &extended_surface_listener, data);
881 }
882#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
883
884 if (c->decoration_manager && c->shell.xdg && data->shell_surface.xdg.surface) {
885 data->server_decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(c->decoration_manager, data->shell_surface.xdg.roleobj.toplevel);
886 if (data->server_decoration) {
887 const SDL_bool bordered = (window->flags & SDL_WINDOW_BORDERLESS) == 0;
888 const enum zxdg_toplevel_decoration_v1_mode mode = bordered ? ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE : ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE;
889 zxdg_toplevel_decoration_v1_set_mode(data->server_decoration, mode);
890 }
891 }
892
893 region = wl_compositor_create_region(c->compositor);
894 wl_region_add(region, 0, 0, window->w, window->h);
895 wl_surface_set_opaque_region(data->surface, region);
896 wl_region_destroy(region);
897
898 if (c->relative_mouse_mode) {
899 Wayland_input_lock_pointer(c->input);
900 }
901
902 /* This will call wl_surface_commit */
903 CommitMinMaxDimensions(window);
904 WAYLAND_wl_display_flush(c->display);
905
906 /* we have to wait until the surface gets a "configure" event, or
907 use of this surface will fail. This is a new rule for xdg_shell. */
908 if (c->shell.xdg) {
909 if (data->shell_surface.xdg.surface) {
910 while (!data->shell_surface.xdg.initial_configure_seen) {
911 WAYLAND_wl_display_flush(c->display);
912 WAYLAND_wl_display_dispatch(c->display);
913 }
914 }
915 } else if (c->shell.zxdg) {
916 if (data->shell_surface.zxdg.surface) {
917 while (!data->shell_surface.zxdg.initial_configure_seen) {
918 WAYLAND_wl_display_flush(c->display);
919 WAYLAND_wl_display_dispatch(c->display);
920 }
921 }
922 }
923
924 /* We may need to create an idle inhibitor for this new window */
925 Wayland_SuspendScreenSaver(_this);
926
927 return 0;
928}
929
930
931void
932Wayland_HandlePendingResize(SDL_Window *window)
933{
934 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
935
936 if (data->resize.pending) {
937 struct wl_region *region;
938 if (data->scale_factor != data->resize.scale_factor) {
939 window->w = 0;
940 window->h = 0;
941 }
942 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, data->resize.width, data->resize.height);
943 window->w = data->resize.width;
944 window->h = data->resize.height;
945 data->scale_factor = data->resize.scale_factor;
946 wl_surface_set_buffer_scale(data->surface, data->scale_factor);
947 if (data->egl_window) {
948 WAYLAND_wl_egl_window_resize(data->egl_window, window->w * data->scale_factor, window->h * data->scale_factor, 0, 0);
949 }
950
951 if (data->resize.configure) {
952 if (data->waylandData->shell.xdg) {
953 xdg_surface_ack_configure(data->shell_surface.xdg.surface, data->resize.serial);
954 } else if (data->waylandData->shell.zxdg) {
955 zxdg_surface_v6_ack_configure(data->shell_surface.zxdg.surface, data->resize.serial);
956 }
957 data->resize.configure = SDL_FALSE;
958 }
959
960 region = wl_compositor_create_region(data->waylandData->compositor);
961 wl_region_add(region, 0, 0, window->w, window->h);
962 wl_surface_set_opaque_region(data->surface, region);
963 wl_region_destroy(region);
964
965 data->resize.pending = SDL_FALSE;
966 }
967}
968
969void
970Wayland_SetWindowMinimumSize(_THIS, SDL_Window * window)
971{
972 CommitMinMaxDimensions(window);
973}
974
975void
976Wayland_SetWindowMaximumSize(_THIS, SDL_Window * window)
977{
978 CommitMinMaxDimensions(window);
979}
980
981void Wayland_SetWindowSize(_THIS, SDL_Window * window)
982{
983 SDL_VideoData *data = _this->driverdata;
984 SDL_WindowData *wind = window->driverdata;
985 struct wl_region *region;
986
987 wl_surface_set_buffer_scale(wind->surface, get_window_scale_factor(window));
988
989 if (wind->egl_window) {
990 WAYLAND_wl_egl_window_resize(wind->egl_window, window->w * get_window_scale_factor(window), window->h * get_window_scale_factor(window), 0, 0);
991 }
992
993 region = wl_compositor_create_region(data->compositor);
994 wl_region_add(region, 0, 0, window->w, window->h);
995 wl_surface_set_opaque_region(wind->surface, region);
996 wl_region_destroy(region);
997}
998
999void Wayland_SetWindowTitle(_THIS, SDL_Window * window)
1000{
1001 SDL_WindowData *wind = window->driverdata;
1002 SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
1003
1004 if (window->title != NULL) {
1005 if (viddata->shell.xdg) {
1006 xdg_toplevel_set_title(wind->shell_surface.xdg.roleobj.toplevel, window->title);
1007 } else if (viddata->shell.zxdg) {
1008 zxdg_toplevel_v6_set_title(wind->shell_surface.zxdg.roleobj.toplevel, window->title);
1009 } else {
1010 wl_shell_surface_set_title(wind->shell_surface.wl, window->title);
1011 }
1012 }
1013
1014 WAYLAND_wl_display_flush( ((SDL_VideoData*)_this->driverdata)->display );
1015}
1016
1017void
1018Wayland_SuspendScreenSaver(_THIS)
1019{
1020 SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
1021
1022#if SDL_USE_LIBDBUS
1023 if (SDL_DBus_ScreensaverInhibit(_this->suspend_screensaver)) {
1024 return;
1025 }
1026#endif
1027
1028 /* The idle_inhibit_unstable_v1 protocol suspends the screensaver
1029 on a per wl_surface basis, but SDL assumes that suspending
1030 the screensaver can be done independently of any window.
1031
1032 To reconcile these differences, we propagate the idle inhibit
1033 state to each window. If there is no window active, we will
1034 be able to inhibit idle once the first window is created.
1035 */
1036 if (data->idle_inhibit_manager) {
1037 SDL_Window *window = _this->windows;
1038 while (window) {
1039 SDL_WindowData *win_data = window->driverdata;
1040
1041 if (_this->suspend_screensaver && !win_data->idle_inhibitor) {
1042 win_data->idle_inhibitor =
1043 zwp_idle_inhibit_manager_v1_create_inhibitor(data->idle_inhibit_manager,
1044 win_data->surface);
1045 } else if (!_this->suspend_screensaver && win_data->idle_inhibitor) {
1046 zwp_idle_inhibitor_v1_destroy(win_data->idle_inhibitor);
1047 win_data->idle_inhibitor = NULL;
1048 }
1049
1050 window = window->next;
1051 }
1052 }
1053}
1054
1055void Wayland_DestroyWindow(_THIS, SDL_Window *window)
1056{
1057 SDL_VideoData *data = _this->driverdata;
1058 SDL_WindowData *wind = window->driverdata;
1059
1060 if (data) {
1061 if (wind->egl_surface) {
1062 SDL_EGL_DestroySurface(_this, wind->egl_surface);
1063 }
1064 if (wind->egl_window) {
1065 WAYLAND_wl_egl_window_destroy(wind->egl_window);
1066 }
1067
1068 if (wind->server_decoration) {
1069 zxdg_toplevel_decoration_v1_destroy(wind->server_decoration);
1070 }
1071
1072 if (wind->idle_inhibitor) {
1073 zwp_idle_inhibitor_v1_destroy(wind->idle_inhibitor);
1074 }
1075
1076 if (data->shell.xdg) {
1077 if (wind->shell_surface.xdg.roleobj.toplevel) {
1078 xdg_toplevel_destroy(wind->shell_surface.xdg.roleobj.toplevel);
1079 }
1080 if (wind->shell_surface.zxdg.surface) {
1081 xdg_surface_destroy(wind->shell_surface.xdg.surface);
1082 }
1083 } else if (data->shell.zxdg) {
1084 if (wind->shell_surface.zxdg.roleobj.toplevel) {
1085 zxdg_toplevel_v6_destroy(wind->shell_surface.zxdg.roleobj.toplevel);
1086 }
1087 if (wind->shell_surface.zxdg.surface) {
1088 zxdg_surface_v6_destroy(wind->shell_surface.zxdg.surface);
1089 }
1090 } else {
1091 if (wind->shell_surface.wl) {
1092 wl_shell_surface_destroy(wind->shell_surface.wl);
1093 }
1094 }
1095
1096 SDL_free(wind->outputs);
1097
1098#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
1099 if (wind->extended_surface) {
1100 QtExtendedSurface_Unsubscribe(wind->extended_surface, SDL_HINT_QTWAYLAND_CONTENT_ORIENTATION);
1101 QtExtendedSurface_Unsubscribe(wind->extended_surface, SDL_HINT_QTWAYLAND_WINDOW_FLAGS);
1102 qt_extended_surface_destroy(wind->extended_surface);
1103 }
1104#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
1105 wl_surface_destroy(wind->surface);
1106
1107 SDL_free(wind);
1108 WAYLAND_wl_display_flush(data->display);
1109 }
1110 window->driverdata = NULL;
1111}
1112
1113#endif /* SDL_VIDEO_DRIVER_WAYLAND && SDL_VIDEO_OPENGL_EGL */
1114
1115/* vi: set ts=4 sw=4 expandtab: */
1116