Commit 8e3ec34d34af6aa3348b0dd9bbd3d8dee246e819

Ethan Lee 2021-04-20T12:40:40

wayland: Refactor toplevel mapping, implement HideWindow

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
diff --git a/src/video/wayland/SDL_waylandevents.c b/src/video/wayland/SDL_waylandevents.c
index 2657a27..ec288ca 100644
--- a/src/video/wayland/SDL_waylandevents.c
+++ b/src/video/wayland/SDL_waylandevents.c
@@ -331,11 +331,21 @@ ProcessHitTest(struct SDL_WaylandInput *input, uint32_t serial)
         switch (rc) {
             case SDL_HITTEST_DRAGGABLE:
                 if (input->display->shell.xdg) {
-                    xdg_toplevel_move(window_data->shell_surface.xdg.roleobj.toplevel, input->seat, serial);
+                    if (window_data->shell_surface.xdg.roleobj.toplevel) {
+                        xdg_toplevel_move(window_data->shell_surface.xdg.roleobj.toplevel,
+                                          input->seat,
+                                          serial);
+                    }
                 } else if (input->display->shell.zxdg) {
-                    zxdg_toplevel_v6_move(window_data->shell_surface.zxdg.roleobj.toplevel, input->seat, serial);
+                    if (window_data->shell_surface.zxdg.roleobj.toplevel) {
+                        zxdg_toplevel_v6_move(window_data->shell_surface.zxdg.roleobj.toplevel,
+                                              input->seat,
+                                              serial);
+                    }
                 } else {
-                    wl_shell_surface_move(window_data->shell_surface.wl, input->seat, serial);
+                    if (window_data->shell_surface.wl) {
+                        wl_shell_surface_move(window_data->shell_surface.wl, input->seat, serial);
+                    }
                 }
                 return SDL_TRUE;
 
@@ -348,11 +358,23 @@ ProcessHitTest(struct SDL_WaylandInput *input, uint32_t serial)
             case SDL_HITTEST_RESIZE_BOTTOMLEFT:
             case SDL_HITTEST_RESIZE_LEFT:
                 if (input->display->shell.xdg) {
-                    xdg_toplevel_resize(window_data->shell_surface.xdg.roleobj.toplevel, input->seat, serial, directions_zxdg[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
+                    if (window_data->shell_surface.xdg.roleobj.toplevel) {
+                        xdg_toplevel_resize(window_data->shell_surface.xdg.roleobj.toplevel,
+                                            input->seat,
+                                            serial,
+                                            directions_zxdg[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
+                    }
                 } else if (input->display->shell.zxdg) {
-                    zxdg_toplevel_v6_resize(window_data->shell_surface.zxdg.roleobj.toplevel, input->seat, serial, directions_zxdg[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
+                    if (window_data->shell_surface.zxdg.roleobj.toplevel) {
+                        zxdg_toplevel_v6_resize(window_data->shell_surface.zxdg.roleobj.toplevel,
+                                                input->seat,
+                                                serial,
+                                                directions_zxdg[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
+                    }
                 } else {
-                    wl_shell_surface_resize(window_data->shell_surface.wl, input->seat, serial, directions_wl[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
+                    if (window_data->shell_surface.wl) {
+                        wl_shell_surface_resize(window_data->shell_surface.wl, input->seat, serial, directions_wl[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
+                    }
                 }
                 return SDL_TRUE;
 
diff --git a/src/video/wayland/SDL_waylandvideo.c b/src/video/wayland/SDL_waylandvideo.c
index 3f66a67..759e0b1 100644
--- a/src/video/wayland/SDL_waylandvideo.c
+++ b/src/video/wayland/SDL_waylandvideo.c
@@ -198,6 +198,7 @@ Wayland_CreateDevice(int devindex)
 
     device->CreateSDLWindow = Wayland_CreateWindow;
     device->ShowWindow = Wayland_ShowWindow;
+    device->HideWindow = Wayland_HideWindow;
     device->SetWindowFullscreen = Wayland_SetWindowFullscreen;
     device->MaximizeWindow = Wayland_MaximizeWindow;
     device->MinimizeWindow = Wayland_MinimizeWindow;
diff --git a/src/video/wayland/SDL_waylandwindow.c b/src/video/wayland/SDL_waylandwindow.c
index 9a81047..c06616b 100644
--- a/src/video/wayland/SDL_waylandwindow.c
+++ b/src/video/wayland/SDL_waylandwindow.c
@@ -67,6 +67,9 @@ CommitMinMaxDimensions(SDL_Window *window)
     }
 
     if (data->shell.xdg) {
+        if (wind->shell_surface.xdg.roleobj.toplevel == NULL) {
+            return; /* Can't do anything yet, wait for ShowWindow */
+        }
         xdg_toplevel_set_min_size(wind->shell_surface.xdg.roleobj.toplevel,
                                   min_width,
                                   min_height);
@@ -74,6 +77,9 @@ CommitMinMaxDimensions(SDL_Window *window)
                                   max_width,
                                   max_height);
     } else if (data->shell.zxdg) {
+        if (wind->shell_surface.zxdg.roleobj.toplevel == NULL) {
+            return; /* Can't do anything yet, wait for ShowWindow */
+        }
         zxdg_toplevel_v6_set_min_size(wind->shell_surface.zxdg.roleobj.toplevel,
                                       min_width,
                                       min_height);
@@ -97,18 +103,27 @@ SetFullscreen(SDL_Window *window, struct wl_output *output)
     CommitMinMaxDimensions(window);
 
     if (viddata->shell.xdg) {
+        if (wind->shell_surface.xdg.roleobj.toplevel == NULL) {
+            return; /* Can't do anything yet, wait for ShowWindow */
+        }
         if (output) {
             xdg_toplevel_set_fullscreen(wind->shell_surface.xdg.roleobj.toplevel, output);
         } else {
             xdg_toplevel_unset_fullscreen(wind->shell_surface.xdg.roleobj.toplevel);
         }
     } else if (viddata->shell.zxdg) {
+        if (wind->shell_surface.zxdg.roleobj.toplevel == NULL) {
+            return; /* Can't do anything yet, wait for ShowWindow */
+        }
         if (output) {
             zxdg_toplevel_v6_set_fullscreen(wind->shell_surface.zxdg.roleobj.toplevel, output);
         } else {
             zxdg_toplevel_v6_unset_fullscreen(wind->shell_surface.zxdg.roleobj.toplevel);
         }
     } else {
+        if (wind->shell_surface.wl == NULL) {
+            return; /* Can't do anything yet, wait for ShowWindow */
+        }
         if (output) {
             wl_shell_surface_set_fullscreen(wind->shell_surface.wl,
                                             WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
@@ -603,9 +618,21 @@ Wayland_SetWindowModalFor(_THIS, SDL_Window *modal_window, SDL_Window *parent_wi
     SDL_WindowData *parent_data = parent_window->driverdata;
 
     if (viddata->shell.xdg) {
+        if (modal_data->shell_surface.xdg.roleobj.toplevel == NULL) {
+            return SDL_SetError("Modal window was hidden");
+        }
+        if (parent_data->shell_surface.xdg.roleobj.toplevel == NULL) {
+            return SDL_SetError("Parent window was hidden");
+        }
         xdg_toplevel_set_parent(modal_data->shell_surface.xdg.roleobj.toplevel,
                                 parent_data->shell_surface.xdg.roleobj.toplevel);
     } else if (viddata->shell.zxdg) {
+        if (modal_data->shell_surface.zxdg.roleobj.toplevel == NULL) {
+            return SDL_SetError("Modal window was hidden");
+        }
+        if (parent_data->shell_surface.zxdg.roleobj.toplevel == NULL) {
+            return SDL_SetError("Parent window was hidden");
+        }
         zxdg_toplevel_v6_set_parent(modal_data->shell_surface.zxdg.roleobj.toplevel,
                                     parent_data->shell_surface.zxdg.roleobj.toplevel);
     } else {
@@ -618,8 +645,112 @@ Wayland_SetWindowModalFor(_THIS, SDL_Window *modal_window, SDL_Window *parent_wi
 
 void Wayland_ShowWindow(_THIS, SDL_Window *window)
 {
-    SDL_WaylandOutputData *driverdata = (SDL_WaylandOutputData *) SDL_GetDisplayForWindow(window)->driverdata;
-    SetFullscreen(window, (window->flags & SDL_WINDOW_FULLSCREEN) ? driverdata->output : NULL);
+    SDL_VideoData *c = _this->driverdata;
+    SDL_WindowData *data = window->driverdata;
+    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
+
+    /* Detach any previous buffers before resetting everything, otherwise when
+     * calling this a second time you'll get an annoying protocol error
+     */
+    wl_surface_attach(data->surface, NULL, 0, 0);
+    wl_surface_commit(data->surface);
+
+    /* Create the shell surface and map the toplevel */
+    if (c->shell.xdg) {
+        data->shell_surface.xdg.surface = xdg_wm_base_get_xdg_surface(c->shell.xdg, data->surface);
+        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);
+
+        /* Create the window decorations */
+        if (c->decoration_manager) {
+            data->server_decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(c->decoration_manager, data->shell_surface.xdg.roleobj.toplevel);
+        }
+    } else if (c->shell.zxdg) {
+        data->shell_surface.zxdg.surface = zxdg_shell_v6_get_xdg_surface(c->shell.zxdg, data->surface);
+        zxdg_surface_v6_set_user_data(data->shell_surface.zxdg.surface, data);
+        zxdg_surface_v6_add_listener(data->shell_surface.zxdg.surface, &shell_surface_listener_zxdg, data);
+
+        /* !!! FIXME: add popup role */
+        data->shell_surface.zxdg.roleobj.toplevel = zxdg_surface_v6_get_toplevel(data->shell_surface.zxdg.surface);
+        zxdg_toplevel_v6_add_listener(data->shell_surface.zxdg.roleobj.toplevel, &toplevel_listener_zxdg, data);
+        zxdg_toplevel_v6_set_app_id(data->shell_surface.zxdg.roleobj.toplevel, c->classname);
+    } else {
+        data->shell_surface.wl = wl_shell_get_shell_surface(c->shell.wl, data->surface);
+        wl_shell_surface_set_class(data->shell_surface.wl, c->classname);
+        wl_shell_surface_set_user_data(data->shell_surface.wl, data);
+        wl_shell_surface_add_listener(data->shell_surface.wl, &shell_surface_listener_wl, data);
+    }
+
+    /* Restore state that was set prior to this call */
+    Wayland_SetWindowTitle(_this, window);
+    Wayland_SetWindowBordered(_this, window, (window->flags & SDL_WINDOW_BORDERLESS) == 0);
+    if (window->flags & SDL_WINDOW_MAXIMIZED) {
+        Wayland_MaximizeWindow(_this, window);
+    }
+    if (window->flags & SDL_WINDOW_MINIMIZED) {
+        Wayland_MinimizeWindow(_this, window);
+    }
+    Wayland_SetWindowFullscreen(_this, window, display, (window->flags & SDL_WINDOW_FULLSCREEN) != 0);
+
+    /* We have to wait until the surface gets a "configure" event, or use of
+     * this surface will fail. This is a new rule for xdg_shell.
+     */
+    if (c->shell.xdg) {
+        if (data->shell_surface.xdg.surface) {
+            while (!data->shell_surface.xdg.initial_configure_seen) {
+                WAYLAND_wl_display_flush(c->display);
+                WAYLAND_wl_display_dispatch(c->display);
+            }
+        }
+    } else if (c->shell.zxdg) {
+        if (data->shell_surface.zxdg.surface) {
+            while (!data->shell_surface.zxdg.initial_configure_seen) {
+                WAYLAND_wl_display_flush(c->display);
+                WAYLAND_wl_display_dispatch(c->display);
+            }
+        }
+    }
+}
+
+void Wayland_HideWindow(_THIS, SDL_Window *window)
+{
+    SDL_VideoData *data = _this->driverdata;
+    SDL_WindowData *wind = window->driverdata;
+
+    if (wind->server_decoration) {
+       zxdg_toplevel_decoration_v1_destroy(wind->server_decoration);
+       wind->server_decoration = NULL;
+    }
+
+    if (data->shell.xdg) {
+        if (wind->shell_surface.xdg.roleobj.toplevel) {
+            xdg_toplevel_destroy(wind->shell_surface.xdg.roleobj.toplevel);
+            wind->shell_surface.xdg.roleobj.toplevel = NULL;
+        }
+        if (wind->shell_surface.xdg.surface) {
+            xdg_surface_destroy(wind->shell_surface.xdg.surface);
+            wind->shell_surface.xdg.surface = NULL;
+        }
+    } else if (data->shell.zxdg) {
+        if (wind->shell_surface.zxdg.roleobj.toplevel) {
+            zxdg_toplevel_v6_destroy(wind->shell_surface.zxdg.roleobj.toplevel);
+            wind->shell_surface.zxdg.roleobj.toplevel = NULL;
+        }
+        if (wind->shell_surface.zxdg.surface) {
+            zxdg_surface_v6_destroy(wind->shell_surface.zxdg.surface);
+            wind->shell_surface.zxdg.surface = NULL;
+        }
+    } else {
+        if (wind->shell_surface.wl) {
+            wl_shell_surface_destroy(wind->shell_surface.wl);
+            wind->shell_surface.wl = NULL;
+        }
+    }
 }
 
 #ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
@@ -704,10 +835,19 @@ Wayland_RestoreWindow(_THIS, SDL_Window * window)
 
     /* Note that xdg-shell does NOT provide a way to unset minimize! */
     if (viddata->shell.xdg) {
+        if (wind->shell_surface.xdg.roleobj.toplevel == NULL) {
+            return; /* Can't do anything yet, wait for ShowWindow */
+        }
         xdg_toplevel_unset_maximized(wind->shell_surface.xdg.roleobj.toplevel);
     } else if (viddata->shell.zxdg) {
+        if (wind->shell_surface.zxdg.roleobj.toplevel == NULL) {
+            return; /* Can't do anything yet, wait for ShowWindow */
+        }
         zxdg_toplevel_v6_unset_maximized(wind->shell_surface.zxdg.roleobj.toplevel);
     } else {
+        if (wind->shell_surface.wl == NULL) {
+            return; /* Can't do anything yet, wait for ShowWindow */
+        }
         wl_shell_surface_set_toplevel(wind->shell_surface.wl);
     }
 
@@ -738,10 +878,19 @@ Wayland_MaximizeWindow(_THIS, SDL_Window * window)
     SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
 
     if (viddata->shell.xdg) {
+        if (wind->shell_surface.xdg.roleobj.toplevel == NULL) {
+            return; /* Can't do anything yet, wait for ShowWindow */
+        }
         xdg_toplevel_set_maximized(wind->shell_surface.xdg.roleobj.toplevel);
     } else if (viddata->shell.zxdg) {
+        if (wind->shell_surface.zxdg.roleobj.toplevel == NULL) {
+            return; /* Can't do anything yet, wait for ShowWindow */
+        }
         zxdg_toplevel_v6_set_maximized(wind->shell_surface.zxdg.roleobj.toplevel);
     } else {
+        if (wind->shell_surface.wl == NULL) {
+            return; /* Can't do anything yet, wait for ShowWindow */
+        }
         wl_shell_surface_set_maximized(wind->shell_surface.wl, NULL);
     }
 
@@ -755,8 +904,14 @@ Wayland_MinimizeWindow(_THIS, SDL_Window * window)
     SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
 
     if (viddata->shell.xdg) {
+        if (wind->shell_surface.xdg.roleobj.toplevel == NULL) {
+            return; /* Can't do anything yet, wait for ShowWindow */
+        }
         xdg_toplevel_set_minimized(wind->shell_surface.xdg.roleobj.toplevel);
     } else if (viddata->shell.zxdg) {
+        if (wind->shell_surface.zxdg.roleobj.toplevel == NULL) {
+            return; /* Can't do anything yet, wait for ShowWindow */
+        }
         zxdg_toplevel_v6_set_minimized(wind->shell_surface.zxdg.roleobj.toplevel);
     }
 
@@ -841,23 +996,6 @@ int Wayland_CreateWindow(_THIS, SDL_Window *window)
         wl_compositor_create_surface(c->compositor);
     wl_surface_add_listener(data->surface, &surface_listener, data);
 
-    if (c->shell.xdg) {
-        data->shell_surface.xdg.surface = xdg_wm_base_get_xdg_surface(c->shell.xdg, data->surface);
-        /* !!! FIXME: add popup role */
-        data->shell_surface.xdg.roleobj.toplevel = xdg_surface_get_toplevel(data->shell_surface.xdg.surface);
-        xdg_toplevel_add_listener(data->shell_surface.xdg.roleobj.toplevel, &toplevel_listener_xdg, data);
-        xdg_toplevel_set_app_id(data->shell_surface.xdg.roleobj.toplevel, c->classname);
-    } else if (c->shell.zxdg) {
-        data->shell_surface.zxdg.surface = zxdg_shell_v6_get_xdg_surface(c->shell.zxdg, data->surface);
-        /* !!! FIXME: add popup role */
-        data->shell_surface.zxdg.roleobj.toplevel = zxdg_surface_v6_get_toplevel(data->shell_surface.zxdg.surface);
-        zxdg_toplevel_v6_add_listener(data->shell_surface.zxdg.roleobj.toplevel, &toplevel_listener_zxdg, data);
-        zxdg_toplevel_v6_set_app_id(data->shell_surface.zxdg.roleobj.toplevel, c->classname);
-    } else {
-        data->shell_surface.wl = wl_shell_get_shell_surface(c->shell.wl, data->surface);
-        wl_shell_surface_set_class(data->shell_surface.wl, c->classname);
-    }
-
 #ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
     if (c->surface_extension) {
         data->extended_surface = qt_surface_extension_get_extended_surface(
@@ -880,23 +1018,6 @@ int Wayland_CreateWindow(_THIS, SDL_Window *window)
         }
     }
 
-    if (c->shell.xdg) {
-        if (data->shell_surface.xdg.surface) {
-            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);
-        }
-    } else if (c->shell.zxdg) {
-        if (data->shell_surface.zxdg.surface) {
-            zxdg_surface_v6_set_user_data(data->shell_surface.zxdg.surface, data);
-            zxdg_surface_v6_add_listener(data->shell_surface.zxdg.surface, &shell_surface_listener_zxdg, data);
-        }
-    } else {
-        if (data->shell_surface.wl) {
-            wl_shell_surface_set_user_data(data->shell_surface.wl, data);
-            wl_shell_surface_add_listener(data->shell_surface.wl, &shell_surface_listener_wl, data);
-        }
-    }
-
 #ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
     if (data->extended_surface) {
         qt_extended_surface_set_user_data(data->extended_surface, data);
@@ -905,15 +1026,6 @@ int Wayland_CreateWindow(_THIS, SDL_Window *window)
     }
 #endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
 
-    if (c->decoration_manager && c->shell.xdg && data->shell_surface.xdg.surface) {
-        data->server_decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(c->decoration_manager, data->shell_surface.xdg.roleobj.toplevel);
-        if (data->server_decoration) {
-            const SDL_bool bordered = (window->flags & SDL_WINDOW_BORDERLESS) == 0;
-            const enum zxdg_toplevel_decoration_v1_mode mode = bordered ? ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE : ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE;
-            zxdg_toplevel_decoration_v1_set_mode(data->server_decoration, mode);
-        }
-    }
-
     region = wl_compositor_create_region(c->compositor);
     wl_region_add(region, 0, 0, window->w, window->h);
     wl_surface_set_opaque_region(data->surface, region);
@@ -923,28 +1035,9 @@ int Wayland_CreateWindow(_THIS, SDL_Window *window)
         Wayland_input_lock_pointer(c->input);
     }
 
-    /* This will call wl_surface_commit */
-    CommitMinMaxDimensions(window);
+    wl_surface_commit(data->surface);
     WAYLAND_wl_display_flush(c->display);
 
-    /* we have to wait until the surface gets a "configure" event, or
-       use of this surface will fail. This is a new rule for xdg_shell. */
-    if (c->shell.xdg) {
-        if (data->shell_surface.xdg.surface) {
-            while (!data->shell_surface.xdg.initial_configure_seen) {
-                WAYLAND_wl_display_flush(c->display);
-                WAYLAND_wl_display_dispatch(c->display);
-            }
-        }
-    } else if (c->shell.zxdg) {
-        if (data->shell_surface.zxdg.surface) {
-            while (!data->shell_surface.zxdg.initial_configure_seen) {
-                WAYLAND_wl_display_flush(c->display);
-                WAYLAND_wl_display_dispatch(c->display);
-            }
-        }
-    }
-
     /* We may need to create an idle inhibitor for this new window */
     Wayland_SuspendScreenSaver(_this);
 
@@ -1027,10 +1120,19 @@ void Wayland_SetWindowTitle(_THIS, SDL_Window * window)
 
     if (window->title != NULL) {
         if (viddata->shell.xdg) {
+            if (wind->shell_surface.xdg.roleobj.toplevel == NULL) {
+                return; /* Can't do anything yet, wait for ShowWindow */
+            }
             xdg_toplevel_set_title(wind->shell_surface.xdg.roleobj.toplevel, window->title);
         } else if (viddata->shell.zxdg) {
+            if (wind->shell_surface.zxdg.roleobj.toplevel == NULL) {
+                return; /* Can't do anything yet, wait for ShowWindow */
+            }
             zxdg_toplevel_v6_set_title(wind->shell_surface.zxdg.roleobj.toplevel, window->title);
         } else {
+            if (wind->shell_surface.wl == NULL) {
+                return; /* Can'd do anything yet, wait for ShowWindow */
+            }
             wl_shell_surface_set_title(wind->shell_surface.wl, window->title);
         }
     }
@@ -1089,34 +1191,10 @@ void Wayland_DestroyWindow(_THIS, SDL_Window *window)
             WAYLAND_wl_egl_window_destroy(wind->egl_window);
         }
 
-        if (wind->server_decoration) {
-           zxdg_toplevel_decoration_v1_destroy(wind->server_decoration);
-        }
-
         if (wind->idle_inhibitor) {
             zwp_idle_inhibitor_v1_destroy(wind->idle_inhibitor);
         }
 
-        if (data->shell.xdg) {
-            if (wind->shell_surface.xdg.roleobj.toplevel) {
-                xdg_toplevel_destroy(wind->shell_surface.xdg.roleobj.toplevel);
-            }
-            if (wind->shell_surface.zxdg.surface) {
-                xdg_surface_destroy(wind->shell_surface.xdg.surface);
-            }
-        } else if (data->shell.zxdg) {
-            if (wind->shell_surface.zxdg.roleobj.toplevel) {
-                zxdg_toplevel_v6_destroy(wind->shell_surface.zxdg.roleobj.toplevel);
-            }
-            if (wind->shell_surface.zxdg.surface) {
-                zxdg_surface_v6_destroy(wind->shell_surface.zxdg.surface);
-            }
-        } else {
-            if (wind->shell_surface.wl) {
-                wl_shell_surface_destroy(wind->shell_surface.wl);
-            }
-        }
-
         SDL_free(wind->outputs);
 
 #ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
diff --git a/src/video/wayland/SDL_waylandwindow.h b/src/video/wayland/SDL_waylandwindow.h
index a4c9902..baddeab 100644
--- a/src/video/wayland/SDL_waylandwindow.h
+++ b/src/video/wayland/SDL_waylandwindow.h
@@ -85,6 +85,7 @@ typedef struct {
 } SDL_WindowData;
 
 extern void Wayland_ShowWindow(_THIS, SDL_Window *window);
+extern void Wayland_HideWindow(_THIS, SDL_Window *window);
 extern void Wayland_SetWindowFullscreen(_THIS, SDL_Window * window,
                                         SDL_VideoDisplay * _display,
                                         SDL_bool fullscreen);