SDL: wayland: Swap emulated mode dimensions in more cases

From a20516d4f35f49ba00588324a611297c6b1cd17c Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Mon, 16 May 2022 10:35:56 -0400
Subject: [PATCH] wayland: Swap emulated mode dimensions in more cases

Some compositors (GNOME for example) don't set the transform flag when dealing with portrait mode displays, so the video modes won't have the width/height swapped in all cases where they should be.  Check for both the 90/270 degree transform flag and if the display is taller than it is wide when determining whether to swap the width and height of the emulated video modes, and adjust the comparison logic when size testing against the native mode to account for this.
---
 src/video/wayland/SDL_waylandvideo.c | 30 ++++++++++++++++------------
 1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/src/video/wayland/SDL_waylandvideo.c b/src/video/wayland/SDL_waylandvideo.c
index 4698cc53116..15349b20526 100644
--- a/src/video/wayland/SDL_waylandvideo.c
+++ b/src/video/wayland/SDL_waylandvideo.c
@@ -416,23 +416,25 @@ AddEmulatedModes(SDL_VideoDisplay *dpy, SDL_bool rot_90)
     };
 
     int i;
+    SDL_DisplayMode mode;
     const int native_width  = dpy->display_modes->w;
     const int native_height = dpy->display_modes->h;
 
     for (i = 0; i < SDL_arraysize(mode_list); ++i) {
-        /* Only add modes that are smaller than the native mode */
-        if ((mode_list[i].w < native_width && mode_list[i].h < native_height) ||
-            (mode_list[i].w < native_width && mode_list[i].h == native_height)) {
-            SDL_DisplayMode mode = *dpy->display_modes;
-
-            if (rot_90) {
-                mode.w = mode_list[i].h;
-                mode.h = mode_list[i].w;
-            } else {
-                mode.w = mode_list[i].w;
-                mode.h = mode_list[i].h;
-            }
+        mode = *dpy->display_modes;
+
+        if (rot_90) {
+            mode.w = mode_list[i].h;
+            mode.h = mode_list[i].w;
+        } else {
+            mode.w = mode_list[i].w;
+            mode.h = mode_list[i].h;
+        }
 
+        /* Only add modes that are smaller than the native mode. */
+        if ((mode.w < native_width && mode.h < native_height) ||
+            (mode.w < native_width && mode.h == native_height) ||
+            (mode.w == native_width && mode.h < native_height)) {
             SDL_AddDisplayMode(dpy, &mode);
         }
     }
@@ -646,7 +648,9 @@ display_handle_done(void *data,
 
     /* Add emulated modes if wp_viewporter is supported and mode emulation is enabled. */
     if (video->viewporter && mode_emulation_enabled) {
-        AddEmulatedModes(dpy, (driverdata->transform & WL_OUTPUT_TRANSFORM_90) != 0);
+        const SDL_bool rot_90 = ((driverdata->transform & WL_OUTPUT_TRANSFORM_90) != 0) ||
+                                (driverdata->width < driverdata->height);
+        AddEmulatedModes(dpy, rot_90);
     }
 
     if (driverdata->index == -1) {