Commit c37090f9a47f9937385f38217ce69f7fe0e6d2e3

Ethan Lee 2022-04-18T12:30:08

wayland: Add support for TOOLTIP/POPUP_MENU

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
diff --git a/include/SDL_syswm.h b/include/SDL_syswm.h
index f7cd670..45f8e75 100644
--- a/include/SDL_syswm.h
+++ b/include/SDL_syswm.h
@@ -298,6 +298,8 @@ struct SDL_SysWMinfo
             struct wl_egl_window *egl_window;       /**< Wayland EGL window (native window) */
             struct xdg_surface *xdg_surface;        /**< Wayland xdg surface (window manager handle) */
             struct xdg_toplevel *xdg_toplevel;      /**< Wayland xdg toplevel role */
+            struct xdg_popup *xdg_popup;            /**< Wayland xdg popup role */
+            struct xdg_positioner *xdg_positioner;  /**< Wayland xdg positioner, for popup */
         } wl;
 #endif
 #if defined(SDL_VIDEO_DRIVER_MIR)  /* no longer available, left for API/ABI compatibility. Remove in 2.1! */
diff --git a/src/video/wayland/SDL_waylandevents.c b/src/video/wayland/SDL_waylandevents.c
index ba85c1d..e515d9d 100644
--- a/src/video/wayland/SDL_waylandevents.c
+++ b/src/video/wayland/SDL_waylandevents.c
@@ -476,6 +476,9 @@ ProcessHitTest(struct SDL_WaylandInput *input, uint32_t serial)
         const uint32_t *directions_libdecor = directions;
 #endif
 
+        /* Hit tests shouldn't apply to xdg_popups, right? */
+        SDL_assert(!WINDOW_IS_XDG_POPUP(window));
+
         switch (rc) {
             case SDL_HITTEST_DRAGGABLE:
 #ifdef HAVE_LIBDECOR_H
diff --git a/src/video/wayland/SDL_waylandvideo.c b/src/video/wayland/SDL_waylandvideo.c
index bce982f..0305c46 100644
--- a/src/video/wayland/SDL_waylandvideo.c
+++ b/src/video/wayland/SDL_waylandvideo.c
@@ -819,7 +819,7 @@ display_handle_global(void *data, struct wl_registry *registry, uint32_t id,
     } else if (SDL_strcmp(interface, "wl_seat") == 0) {
         Wayland_display_add_input(d, id, version);
     } else if (SDL_strcmp(interface, "xdg_wm_base") == 0) {
-        d->shell.xdg = wl_registry_bind(d->registry, id, &xdg_wm_base_interface, 1);
+        d->shell.xdg = wl_registry_bind(d->registry, id, &xdg_wm_base_interface, SDL_min(version, 3));
         xdg_wm_base_add_listener(d->shell.xdg, &shell_listener_xdg, NULL);
     } else if (SDL_strcmp(interface, "wl_shm") == 0) {
         d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
@@ -924,12 +924,6 @@ Wayland_VideoInit(_THIS)
     /* Don't have server-side decorations? Try client-side instead. */
     if (should_use_libdecor(data)) {
         data->shell.libdecor = libdecor_new(data->display, &libdecor_interface);
-
-        /* If libdecor works, we don't need xdg-shell anymore. */
-        if (data->shell.libdecor && data->shell.xdg) {
-            xdg_wm_base_destroy(data->shell.xdg);
-            data->shell.xdg = NULL;
-        }
     }
 #endif
 
diff --git a/src/video/wayland/SDL_waylandwindow.c b/src/video/wayland/SDL_waylandwindow.c
index 7c6eb64..9abf868 100644
--- a/src/video/wayland/SDL_waylandwindow.c
+++ b/src/video/wayland/SDL_waylandwindow.c
@@ -25,12 +25,14 @@
 
 #include "../SDL_sysvideo.h"
 #include "../../events/SDL_windowevents_c.h"
+#include "../../events/SDL_mouse_c.h"
 #include "../SDL_egl_c.h"
 #include "SDL_waylandevents_c.h"
 #include "SDL_waylandwindow.h"
 #include "SDL_waylandvideo.h"
 #include "SDL_waylandtouch.h"
 #include "SDL_hints.h"
+#include "SDL_events.h"
 
 #include "xdg-shell-client-protocol.h"
 #include "xdg-decoration-unstable-v1-client-protocol.h"
@@ -318,6 +320,15 @@ SetMinMaxDimensions(SDL_Window *window, SDL_bool commit)
     SDL_VideoData *viddata = wind->waylandData;
     int min_width, min_height, max_width, max_height;
 
+    /* Pop-ups don't get to change size */
+    if (WINDOW_IS_XDG_POPUP(window)) {
+        /* ... but we still want to commit, particularly for ShowWindow */
+        if (commit) {
+            wl_surface_commit(wind->surface);
+        }
+        return;
+    }
+
     if (window->flags & SDL_WINDOW_FULLSCREEN) {
         min_width = 0;
         min_height = 0;
@@ -336,7 +347,7 @@ SetMinMaxDimensions(SDL_Window *window, SDL_bool commit)
     }
 
 #ifdef HAVE_LIBDECOR_H
-    if (viddata->shell.libdecor) {
+    if (WINDOW_IS_LIBDECOR(viddata, window)) {
         if (wind->shell_surface.libdecor.frame == NULL) {
             return; /* Can't do anything yet, wait for ShowWindow */
         }
@@ -370,13 +381,22 @@ SetFullscreen(SDL_Window *window, struct wl_output *output, SDL_bool commit)
     SDL_WindowData *wind = window->driverdata;
     SDL_VideoData *viddata = wind->waylandData;
 
+    /* Pop-ups don't get to be fullscreened */
+    if (WINDOW_IS_XDG_POPUP(window)) {
+        /* ... but we still want to commit, particularly for ShowWindow */
+        if (commit) {
+            wl_surface_commit(wind->surface);
+        }
+        return;
+    }
+
     /* The desktop may try to enforce min/max sizes here, so turn them off for
      * fullscreen and on (if applicable) for windowed
      */
     SetMinMaxDimensions(window, SDL_FALSE);
 
 #ifdef HAVE_LIBDECOR_H
-    if (viddata->shell.libdecor) {
+    if (WINDOW_IS_LIBDECOR(viddata, window)) {
         if (wind->shell_surface.libdecor.frame == NULL) {
             return; /* Can't do anything yet, wait for ShowWindow */
         }
@@ -609,6 +629,60 @@ static const struct xdg_toplevel_listener toplevel_listener_xdg = {
     handle_close_xdg_toplevel
 };
 
+static void
+handle_configure_xdg_popup(void *data,
+                           struct xdg_popup *xdg_popup,
+                           int32_t x,
+                           int32_t y,
+                           int32_t width,
+                           int32_t height)
+{
+    /* No-op, we don't use x/y and width/height are fixed-size */
+}
+
+static void
+handle_done_xdg_popup(void *data, struct xdg_popup *xdg_popup)
+{
+    SDL_WindowData *window = (SDL_WindowData *)data;
+    SDL_SendWindowEvent(window->sdlwindow, SDL_WINDOWEVENT_CLOSE, 0, 0);
+}
+
+static void
+handle_repositioned_xdg_popup(void *data,
+                              struct xdg_popup *xdg_popup,
+                              uint32_t token)
+{
+    /* No-op, configure does all the work we care about */
+}
+
+static const struct xdg_popup_listener popup_listener_xdg = {
+    handle_configure_xdg_popup,
+    handle_done_xdg_popup,
+    handle_repositioned_xdg_popup
+};
+
+#define TOOLTIP_CURSOR_OFFSET 8 /* FIXME: Arbitrary, eyeballed from X tooltip */
+
+static int
+Wayland_PopupWatch(void *data, SDL_Event *event)
+{
+    if (event->type == SDL_MOUSEMOTION) {
+        SDL_Window *window = (SDL_Window *) data;
+        SDL_WindowData *data = window->driverdata;
+
+        /* Coordinates might be relative to the popup, which we don't want */
+        if (event->motion.windowID == data->shell_surface.xdg.roleobj.popup.parentID) {
+            xdg_positioner_set_offset(data->shell_surface.xdg.roleobj.popup.positioner,
+                                      event->motion.x + TOOLTIP_CURSOR_OFFSET,
+                                      event->motion.y + TOOLTIP_CURSOR_OFFSET);
+            xdg_popup_reposition(data->shell_surface.xdg.roleobj.popup.popup,
+                                 data->shell_surface.xdg.roleobj.popup.positioner,
+                                 0);
+        }
+    }
+    return 1;
+}
+
 #ifdef HAVE_LIBDECOR_H
 static void
 decoration_frame_configure(struct libdecor_frame *frame,
@@ -929,6 +1003,19 @@ static const struct wl_surface_listener surface_listener = {
     handle_surface_leave
 };
 
+static void
+Wayland_FillEmptyShellInfo(SDL_SysWMinfo * info, const Uint32 version)
+{
+    info->info.wl.xdg_surface = NULL;
+    if (version >= SDL_VERSIONNUM(2, 0, 17)) {
+        info->info.wl.xdg_toplevel = NULL;
+        if (version >= SDL_VERSIONNUM(2, 0, 22)) {
+            info->info.wl.xdg_popup = NULL;
+            info->info.wl.xdg_positioner = NULL;
+        }
+    }
+}
+
 SDL_bool
 Wayland_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
 {
@@ -961,23 +1048,40 @@ Wayland_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
         info->info.wl.egl_window = data->egl_window;
 
 #ifdef HAVE_LIBDECOR_H
-        if (viddata->shell.libdecor && data->shell_surface.libdecor.frame != NULL) {
-            info->info.wl.xdg_surface = libdecor_frame_get_xdg_surface(data->shell_surface.libdecor.frame);
-            if (version >= SDL_VERSIONNUM(2, 0, 17)) {
-                info->info.wl.xdg_toplevel = libdecor_frame_get_xdg_toplevel(data->shell_surface.libdecor.frame);
+        if (WINDOW_IS_LIBDECOR(viddata, window)) {
+            if (data->shell_surface.libdecor.frame != NULL) {
+                info->info.wl.xdg_surface = libdecor_frame_get_xdg_surface(data->shell_surface.libdecor.frame);
+                if (version >= SDL_VERSIONNUM(2, 0, 17)) {
+                    info->info.wl.xdg_toplevel = libdecor_frame_get_xdg_toplevel(data->shell_surface.libdecor.frame);
+                    if (version >= SDL_VERSIONNUM(2, 0, 22)) {
+                        info->info.wl.xdg_popup = NULL;
+                        info->info.wl.xdg_positioner = NULL;
+                    }
+                }
+            } else {
+                /* Not mapped yet */
+                Wayland_FillEmptyShellInfo(info, version);
             }
         } else
 #endif
         if (viddata->shell.xdg && data->shell_surface.xdg.surface != NULL) {
             info->info.wl.xdg_surface = data->shell_surface.xdg.surface;
             if (version >= SDL_VERSIONNUM(2, 0, 17)) {
-                info->info.wl.xdg_toplevel = data->shell_surface.xdg.roleobj.toplevel;
+                SDL_bool popup = WINDOW_IS_XDG_POPUP(window);
+                info->info.wl.xdg_toplevel = popup ? NULL : data->shell_surface.xdg.roleobj.toplevel;
+                if (version >= SDL_VERSIONNUM(2, 0, 22)) {
+                    if (popup) {
+                        info->info.wl.xdg_popup = data->shell_surface.xdg.roleobj.popup.popup;
+                        info->info.wl.xdg_positioner = data->shell_surface.xdg.roleobj.popup.positioner;
+                    } else {
+                        info->info.wl.xdg_popup = NULL;
+                        info->info.wl.xdg_positioner = NULL;
+                    }
+                }
             }
         } else {
-            info->info.wl.xdg_surface = NULL;
-            if (version >= SDL_VERSIONNUM(2, 0, 17)) {
-                info->info.wl.xdg_toplevel = NULL;
-            }
+            /* Either it's not mapped yet or we don't have a shell protocol */
+            Wayland_FillEmptyShellInfo(info, version);
         }
     }
 
@@ -1002,6 +1106,10 @@ Wayland_SetWindowModalFor(_THIS, SDL_Window *modal_window, SDL_Window *parent_wi
     SDL_WindowData *modal_data = modal_window->driverdata;
     SDL_WindowData *parent_data = parent_window->driverdata;
 
+    if (WINDOW_IS_XDG_POPUP(modal_window) || WINDOW_IS_XDG_POPUP(parent_window)) {
+        return SDL_SetError("Modal/Parent was a popup, not a toplevel");
+    }
+
 #ifdef HAVE_LIBDECOR_H
     if (viddata->shell.libdecor) {
         if (modal_data->shell_surface.libdecor.frame == NULL) {
@@ -1053,9 +1161,9 @@ void Wayland_ShowWindow(_THIS, SDL_Window *window)
     wl_surface_attach(data->surface, NULL, 0, 0);
     wl_surface_commit(data->surface);
 
-    /* Create the shell surface and map the toplevel */
+    /* Create the shell surface and map the toplevel/popup */
 #ifdef HAVE_LIBDECOR_H
-    if (c->shell.libdecor) {
+    if (WINDOW_IS_LIBDECOR(c, window)) {
         data->shell_surface.libdecor.frame = libdecor_decorate(c->shell.libdecor,
                                                                data->surface,
                                                                &libdecor_frame_interface,
@@ -1073,10 +1181,42 @@ void Wayland_ShowWindow(_THIS, SDL_Window *window)
         xdg_surface_set_user_data(data->shell_surface.xdg.surface, data);
         xdg_surface_add_listener(data->shell_surface.xdg.surface, &shell_surface_listener_xdg, data);
 
-        /* !!! FIXME: add popup role */
-        data->shell_surface.xdg.roleobj.toplevel = xdg_surface_get_toplevel(data->shell_surface.xdg.surface);
-        xdg_toplevel_set_app_id(data->shell_surface.xdg.roleobj.toplevel, c->classname);
-        xdg_toplevel_add_listener(data->shell_surface.xdg.roleobj.toplevel, &toplevel_listener_xdg, data);
+        if (WINDOW_IS_XDG_POPUP(window)) {
+            SDL_Mouse *mouse = SDL_GetMouse();
+            SDL_Window *focused = SDL_GetMouseFocus();
+            SDL_WindowData *focuseddata = focused->driverdata;
+
+            /* This popup may be a child of another popup! */
+            data->shell_surface.xdg.roleobj.popup.parentID = SDL_GetWindowID(focused);
+            data->shell_surface.xdg.roleobj.popup.child = NULL;
+            if (WINDOW_IS_XDG_POPUP(focused)) {
+                SDL_assert(focuseddata->shell_surface.xdg.roleobj.popup.child == NULL);
+                focuseddata->shell_surface.xdg.roleobj.popup.child = window;
+            }
+
+            /* Set up the positioner for the popup */
+            data->shell_surface.xdg.roleobj.popup.positioner = xdg_wm_base_create_positioner(c->shell.xdg);
+            xdg_positioner_set_offset(data->shell_surface.xdg.roleobj.popup.positioner,
+                                      mouse->x + TOOLTIP_CURSOR_OFFSET,
+                                      mouse->y + TOOLTIP_CURSOR_OFFSET);
+
+            /* Assign the popup role */
+            data->shell_surface.xdg.roleobj.popup.popup = xdg_surface_get_popup(data->shell_surface.xdg.surface,
+                                                                                focuseddata->shell_surface.xdg.surface,
+                                                                                data->shell_surface.xdg.roleobj.popup.positioner);
+            xdg_popup_add_listener(data->shell_surface.xdg.roleobj.popup.popup, &popup_listener_xdg, data);
+
+            /* For tooltips, track mouse motion so it follows the cursor */
+            if (window->flags & SDL_WINDOW_TOOLTIP) {
+                if (xdg_popup_get_version(data->shell_surface.xdg.roleobj.popup.popup) >= 3) {
+                    SDL_AddEventWatch(Wayland_PopupWatch, window);
+                }
+            }
+        } else {
+            data->shell_surface.xdg.roleobj.toplevel = xdg_surface_get_toplevel(data->shell_surface.xdg.surface);
+            xdg_toplevel_set_app_id(data->shell_surface.xdg.roleobj.toplevel, c->classname);
+            xdg_toplevel_add_listener(data->shell_surface.xdg.roleobj.toplevel, &toplevel_listener_xdg, data);
+        }
     }
 
     /* Restore state that was set prior to this call */
@@ -1092,7 +1232,7 @@ void Wayland_ShowWindow(_THIS, SDL_Window *window)
      * this surface will fail. This is a new rule for xdg_shell.
      */
 #ifdef HAVE_LIBDECOR_H
-    if (c->shell.libdecor) {
+    if (WINDOW_IS_LIBDECOR(c, window)) {
         if (data->shell_surface.libdecor.frame) {
             while (!data->shell_surface.libdecor.initial_configure_seen) {
                 WAYLAND_wl_display_flush(c->display);
@@ -1116,7 +1256,7 @@ void Wayland_ShowWindow(_THIS, SDL_Window *window)
         }
 
         /* Create the window decorations */
-        if (data->shell_surface.xdg.roleobj.toplevel && c->decoration_manager) {
+        if (!WINDOW_IS_XDG_POPUP(window) && data->shell_surface.xdg.roleobj.toplevel && c->decoration_manager) {
             data->server_decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(c->decoration_manager, data->shell_surface.xdg.roleobj.toplevel);
         }
     } else {
@@ -1129,11 +1269,11 @@ void Wayland_ShowWindow(_THIS, SDL_Window *window)
      * them immediately afterward.
      */
 #ifdef HAVE_LIBDECOR_H
-    if (c->shell.libdecor) {
+    if (WINDOW_IS_LIBDECOR(c, window)) {
         /* ... but don't call it redundantly for libdecor, the decorator
          * may not interpret a redundant call nicely and cause weird stuff to happen
          */
-        if (window->flags & SDL_WINDOW_BORDERLESS) {
+        if (data->shell_surface.libdecor.frame && window->flags & SDL_WINDOW_BORDERLESS) {
             Wayland_SetWindowBordered(_this, window, SDL_FALSE);
         }
     } else
@@ -1159,6 +1299,42 @@ void Wayland_ShowWindow(_THIS, SDL_Window *window)
     }
 }
 
+static void
+Wayland_ReleasePopup(_THIS, SDL_Window *popup)
+{
+    SDL_WindowData *popupdata;
+
+    /* Basic sanity checks to weed out the weird popup closures */
+    if (popup == NULL || popup->magic != &_this->window_magic) {
+        return;
+    }
+    popupdata = popup->driverdata;
+    if (popupdata == NULL) {
+        return;
+    }
+
+    /* This may already be freed by a parent popup! */
+    if (popupdata->shell_surface.xdg.roleobj.popup.popup == NULL) {
+        return;
+    }
+
+    /* Release the child _first_, otherwise a protocol error triggers */
+    if (popupdata->shell_surface.xdg.roleobj.popup.child != NULL) {
+        Wayland_ReleasePopup(_this, popupdata->shell_surface.xdg.roleobj.popup.child);
+        popupdata->shell_surface.xdg.roleobj.popup.child = NULL;
+    }
+
+    if (popup->flags & SDL_WINDOW_TOOLTIP) {
+        if (xdg_popup_get_version(popupdata->shell_surface.xdg.roleobj.popup.popup) >= 3) {
+            SDL_DelEventWatch(Wayland_PopupWatch, popup);
+        }
+    }
+    xdg_popup_destroy(popupdata->shell_surface.xdg.roleobj.popup.popup);
+    xdg_positioner_destroy(popupdata->shell_surface.xdg.roleobj.popup.positioner);
+    popupdata->shell_surface.xdg.roleobj.popup.popup = NULL;
+    popupdata->shell_surface.xdg.roleobj.popup.positioner = NULL;
+}
+
 void Wayland_HideWindow(_THIS, SDL_Window *window)
 {
     SDL_VideoData *data = _this->driverdata;
@@ -1170,7 +1346,7 @@ void Wayland_HideWindow(_THIS, SDL_Window *window)
     }
 
 #ifdef HAVE_LIBDECOR_H
-    if (data->shell.libdecor) {
+    if (WINDOW_IS_LIBDECOR(data, window)) {
         if (wind->shell_surface.libdecor.frame) {
             libdecor_frame_unref(wind->shell_surface.libdecor.frame);
             wind->shell_surface.libdecor.frame = NULL;
@@ -1178,7 +1354,9 @@ void Wayland_HideWindow(_THIS, SDL_Window *window)
     } else
 #endif
     if (data->shell.xdg) {
-        if (wind->shell_surface.xdg.roleobj.toplevel) {
+        if (WINDOW_IS_XDG_POPUP(window)) {
+            Wayland_ReleasePopup(_this, window);
+        } else if (wind->shell_surface.xdg.roleobj.toplevel) {
             xdg_toplevel_destroy(wind->shell_surface.xdg.roleobj.toplevel);
             wind->shell_surface.xdg.roleobj.toplevel = NULL;
         }
@@ -1395,13 +1573,17 @@ Wayland_RestoreWindow(_THIS, SDL_Window * window)
     SDL_WindowData *wind = window->driverdata;
     SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
 
+    if (WINDOW_IS_XDG_POPUP(window)) {
+        return;
+    }
+
     /* Set this flag now even if we never actually maximized, eventually
      * ShowWindow will take care of it along with the other window state.
      */
     window->flags &= ~SDL_WINDOW_MAXIMIZED;
 
 #ifdef HAVE_LIBDECOR_H
-    if (viddata->shell.libdecor) {
+    if (WINDOW_IS_LIBDECOR(viddata, window)) {
         if (wind->shell_surface.libdecor.frame == NULL) {
             return; /* Can't do anything yet, wait for ShowWindow */
         }
@@ -1424,8 +1606,13 @@ Wayland_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
 {
     SDL_WindowData *wind = window->driverdata;
     const SDL_VideoData *viddata = (const SDL_VideoData *) _this->driverdata;
+
+    if (WINDOW_IS_XDG_POPUP(window)) {
+        return;
+    }
+
 #ifdef HAVE_LIBDECOR_H
-    if (viddata->shell.libdecor) {
+    if (WINDOW_IS_LIBDECOR(viddata, window)) {
         if (wind->shell_surface.libdecor.frame) {
             libdecor_frame_set_visibility(wind->shell_surface.libdecor.frame, bordered);
         }
@@ -1444,7 +1631,7 @@ Wayland_SetWindowResizable(_THIS, SDL_Window * window, SDL_bool resizable)
     SDL_VideoData *data = _this->driverdata;
     const SDL_WindowData *wind = window->driverdata;
 
-    if (data->shell.libdecor) {
+    if (WINDOW_IS_LIBDECOR(data, window)) {
         if (wind->shell_surface.libdecor.frame == NULL) {
             return; /* Can't do anything yet, wait for ShowWindow */
         }
@@ -1466,6 +1653,10 @@ Wayland_MaximizeWindow(_THIS, SDL_Window * window)
     SDL_WindowData *wind = window->driverdata;
     SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
 
+    if (WINDOW_IS_XDG_POPUP(window)) {
+        return;
+    }
+
     if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
         return;
     }
@@ -1476,7 +1667,7 @@ Wayland_MaximizeWindow(_THIS, SDL_Window * window)
     window->flags |= SDL_WINDOW_MAXIMIZED;
 
 #ifdef HAVE_LIBDECOR_H
-    if (viddata->shell.libdecor) {
+    if (WINDOW_IS_LIBDECOR(viddata, window)) {
         if (wind->shell_surface.libdecor.frame == NULL) {
             return; /* Can't do anything yet, wait for ShowWindow */
         }
@@ -1499,8 +1690,12 @@ Wayland_MinimizeWindow(_THIS, SDL_Window * window)
     SDL_WindowData *wind = window->driverdata;
     SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
 
+    if (WINDOW_IS_XDG_POPUP(window)) {
+        return;
+    }
+
 #ifdef HAVE_LIBDECOR_H
-    if (viddata->shell.libdecor) {
+    if (WINDOW_IS_LIBDECOR(viddata, window)) {
         if (wind->shell_surface.libdecor.frame == NULL) {
             return; /* Can't do anything yet, wait for ShowWindow */
         }
@@ -1721,9 +1916,14 @@ Wayland_HandleResize(SDL_Window *window, int width, int height, float scale)
      * It doesn't fix the first frames after resize being glitched visually,
      * but at least lets us not be terminated by the compositor.
      * Can be removed once SDL's resize logic becomes compliant. */
-    if (viddata->shell.xdg && data->shell_surface.xdg.surface) {
-       xdg_surface_set_window_geometry(data->shell_surface.xdg.surface, 0, 0,
-                                       GetWindowWidth(window), GetWindowHeight(window));
+    if (
+#ifdef HAVE_LIBDECOR_H
+        !WINDOW_IS_LIBDECOR(viddata, window) &&
+#endif
+        viddata->shell.xdg &&
+        data->shell_surface.xdg.surface) {
+        xdg_surface_set_window_geometry(data->shell_surface.xdg.surface, 0, 0,
+                                        GetWindowWidth(window), GetWindowHeight(window));
     }
 
     /* Update the viewport */
@@ -1753,7 +1953,7 @@ void Wayland_SetWindowSize(_THIS, SDL_Window * window)
 
 #ifdef HAVE_LIBDECOR_H
     /* we must not resize the window while we have a static (non-floating) size */
-    if (data->shell.libdecor &&
+    if (WINDOW_IS_LIBDECOR(data, window) &&
         wind->shell_surface.libdecor.frame &&
         !libdecor_frame_is_floating(wind->shell_surface.libdecor.frame)) {
             /* Commit the resize when we re-enter floating state */
@@ -1772,7 +1972,7 @@ void Wayland_SetWindowSize(_THIS, SDL_Window * window)
     }
 
 #ifdef HAVE_LIBDECOR_H
-    if (data->shell.libdecor && wind->shell_surface.libdecor.frame) {
+    if (WINDOW_IS_LIBDECOR(data, window) && wind->shell_surface.libdecor.frame) {
         state = libdecor_state_new(GetWindowWidth(window), GetWindowHeight(window));
         libdecor_frame_commit(wind->shell_surface.libdecor.frame, state, NULL);
         libdecor_state_free(state);
@@ -1789,9 +1989,14 @@ void Wayland_SetWindowSize(_THIS, SDL_Window * window)
     wl_region_destroy(region);
 
     /* Update the geometry which may have been set by a hack in Wayland_HandleResize */
-    if (data->shell.xdg && wind->shell_surface.xdg.surface) {
-       xdg_surface_set_window_geometry(wind->shell_surface.xdg.surface, 0, 0,
-                                       GetWindowWidth(window), GetWindowHeight(window));
+    if (
+#ifdef HAVE_LIBDECOR_H
+        !WINDOW_IS_LIBDECOR(data, window) &&
+#endif
+        data->shell.xdg &&
+        wind->shell_surface.xdg.surface) {
+        xdg_surface_set_window_geometry(wind->shell_surface.xdg.surface, 0, 0,
+                                        GetWindowWidth(window), GetWindowHeight(window));
     }
 }
 
@@ -1800,9 +2005,13 @@ void Wayland_SetWindowTitle(_THIS, SDL_Window * window)
     SDL_WindowData *wind = window->driverdata;
     SDL_VideoData *viddata = _this->driverdata;
 
+    if (WINDOW_IS_XDG_POPUP(window)) {
+        return;
+    }
+
     if (window->title != NULL) {
 #ifdef HAVE_LIBDECOR_H
-        if (viddata->shell.libdecor) {
+        if (WINDOW_IS_LIBDECOR(viddata, window)) {
             if (wind->shell_surface.libdecor.frame == NULL) {
                 return; /* Can't do anything yet, wait for ShowWindow */
             }
diff --git a/src/video/wayland/SDL_waylandwindow.h b/src/video/wayland/SDL_waylandwindow.h
index 97ae5ec..9ce6879 100644
--- a/src/video/wayland/SDL_waylandwindow.h
+++ b/src/video/wayland/SDL_waylandwindow.h
@@ -36,16 +36,27 @@ typedef struct {
     struct xdg_surface *surface;
     union {
         struct xdg_toplevel *toplevel;
-        struct xdg_popup *popup;
+        struct {
+            struct xdg_popup *popup;
+            struct xdg_positioner *positioner;
+            Uint32 parentID;
+            SDL_Window *child;
+        } popup;
     } roleobj;
     SDL_bool initial_configure_seen;
 } SDL_xdg_shell_surface;
 
+#define WINDOW_IS_XDG_POPUP(window) \
+    (window->flags & (SDL_WINDOW_TOOLTIP | SDL_WINDOW_POPUP_MENU))
+
 #ifdef HAVE_LIBDECOR_H
 typedef struct {
     struct libdecor_frame *frame;
     SDL_bool initial_configure_seen;
 } SDL_libdecor_surface;
+
+#define WINDOW_IS_LIBDECOR(viddata, window) \
+    (viddata->shell.libdecor && !WINDOW_IS_XDG_POPUP(window))
 #endif
 
 typedef struct {