SDL: wayland: Add support for maximized/restored events

From d32440359a50f1449f2753173b286c7e04af54ac Mon Sep 17 00:00:00 2001
From: Ethan Lee <[EMAIL REDACTED]>
Date: Mon, 17 May 2021 01:24:35 -0400
Subject: [PATCH] wayland: Add support for maximized/restored events

---
 src/video/wayland/SDL_waylandwindow.c | 34 +++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/src/video/wayland/SDL_waylandwindow.c b/src/video/wayland/SDL_waylandwindow.c
index a154785788..cbd7325787 100644
--- a/src/video/wayland/SDL_waylandwindow.c
+++ b/src/video/wayland/SDL_waylandwindow.c
@@ -272,9 +272,12 @@ handle_configure_zxdg_toplevel(void *data,
 
     enum zxdg_toplevel_v6_state *state;
     SDL_bool fullscreen = SDL_FALSE;
+    SDL_bool maximized = SDL_FALSE;
     wl_array_for_each(state, states) {
         if (*state == ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN) {
             fullscreen = SDL_TRUE;
+        } else if (*state == ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED) {
+            maximized = SDL_TRUE;
         }
     }
 
@@ -283,6 +286,7 @@ handle_configure_zxdg_toplevel(void *data,
             /* We might need to re-enter fullscreen after being restored from minimized */
             SDL_WaylandOutputData *driverdata = (SDL_WaylandOutputData *) SDL_GetDisplayForWindow(window)->driverdata;
             SetFullscreen(window, driverdata->output);
+            fullscreen = SDL_TRUE;
         }
 
         if (width == 0 || height == 0) {
@@ -310,6 +314,19 @@ handle_configure_zxdg_toplevel(void *data,
         }
     }
 
+    /* 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 zxdg-shell.
+     */
+    if (!fullscreen) {
+        SDL_SendWindowEvent(window,
+                            maximized ?
+                                SDL_WINDOWEVENT_MAXIMIZED :
+                                SDL_WINDOWEVENT_RESTORED,
+                            0, 0);
+    }
+
     if (width == 0 || height == 0) {
         wind->resize.width = window->w;
         wind->resize.height = window->h;
@@ -388,9 +405,12 @@ handle_configure_xdg_toplevel(void *data,
 
     enum xdg_toplevel_state *state;
     SDL_bool fullscreen = SDL_FALSE;
+    SDL_bool maximized = SDL_FALSE;
     wl_array_for_each(state, states) {
         if (*state == XDG_TOPLEVEL_STATE_FULLSCREEN) {
             fullscreen = SDL_TRUE;
+        } else if (*state == XDG_TOPLEVEL_STATE_MAXIMIZED) {
+            maximized = SDL_TRUE;
         }
     }
 
@@ -399,6 +419,7 @@ handle_configure_xdg_toplevel(void *data,
             /* We might need to re-enter fullscreen after being restored from minimized */
             SDL_WaylandOutputData *driverdata = (SDL_WaylandOutputData *) SDL_GetDisplayForWindow(window)->driverdata;
             SetFullscreen(window, driverdata->output);
+            fullscreen = SDL_TRUE;
         }
 
         if (width == 0 || height == 0) {
@@ -426,6 +447,19 @@ handle_configure_xdg_toplevel(void *data,
         }
     }
 
+    /* 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.
+     */
+    if (!fullscreen) {
+        SDL_SendWindowEvent(window,
+                            maximized ?
+                                SDL_WINDOWEVENT_MAXIMIZED :
+                                SDL_WINDOWEVENT_RESTORED,
+                            0, 0);
+    }
+
     if (width == 0 || height == 0) {
         wind->resize.width = window->w;
         wind->resize.height = window->h;