SDL: testwm.c: show all modes of all displays in the on-screen list (#7323)

From 1f46986d33f907d217f2170118a7accda8fc0f7c Mon Sep 17 00:00:00 2001
From: Eric Wasylishen <[EMAIL REDACTED]>
Date: Tue, 14 Feb 2023 22:05:00 -0700
Subject: [PATCH] testwm.c: show all modes of all displays in the on-screen
 list (#7323)

* testwm.c: show all modes of all displays in the on-screen list

To allow testing https://github.com/libsdl-org/SDL/pull/7317
---
 test/testwm.c | 99 ++++++++++++++++++++++++++-------------------------
 1 file changed, 51 insertions(+), 48 deletions(-)

diff --git a/test/testwm.c b/test/testwm.c
index 3ea5d04d4f91..09668bf38863 100644
--- a/test/testwm.c
+++ b/test/testwm.c
@@ -40,7 +40,7 @@ static const char *cursorNames[] = {
 int system_cursor = -1;
 SDL_Cursor *cursor = NULL;
 SDL_bool relative_mode = SDL_FALSE;
-int highlighted_mode = -1;
+const SDL_DisplayMode *highlighted_mode = NULL;
 
 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
 static void
@@ -57,13 +57,13 @@ draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport)
     const SDL_DisplayMode **modes;
     char text[1024];
     const int lineHeight = 10;
-    const SDL_DisplayID displayID = SDL_GetDisplayForWindow(window);
-    int i;
+    int i, j;
     int column_chars = 0;
     int text_length;
     float x, y;
     float table_top;
     SDL_FPoint mouse_pos = { -1.0f, -1.0f };
+    SDL_DisplayID *display_ids;
 
     /* Get mouse position */
     if (SDL_GetMouseFocus() == window) {
@@ -96,49 +96,58 @@ draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport)
 
     /* Clear the cached mode under the mouse */
     if (window == SDL_GetMouseFocus()) {
-        highlighted_mode = -1;
+        highlighted_mode = NULL;
     }
 
-    modes = SDL_GetFullscreenDisplayModes(displayID, NULL);
-    for (i = 0; modes[i]; ++i) {
-        SDL_FRect cell_rect;
-        const SDL_DisplayMode *mode = modes[i];
-
-        (void)SDL_snprintf(text, sizeof text, "%d: %dx%d@%gHz",
-                           i, mode->pixel_w, mode->pixel_h, mode->refresh_rate);
-
-        /* Update column width */
-        text_length = (int)SDL_strlen(text);
-        column_chars = SDL_max(column_chars, text_length);
-
-        /* Check if under mouse */
-        cell_rect.x = x;
-        cell_rect.y = y;
-        cell_rect.w = (float)(text_length * FONT_CHARACTER_SIZE);
-        cell_rect.h = (float)lineHeight;
+    display_ids = SDL_GetDisplays(NULL);
+
+    if (display_ids) {
+        for (i = 0; display_ids[i]; ++i) {
+            const SDL_DisplayID display_id = display_ids[i];
+            modes = SDL_GetFullscreenDisplayModes(display_id, NULL);
+            for (j = 0; modes[j]; ++j) {
+                SDL_FRect cell_rect;
+                const SDL_DisplayMode *mode = modes[j];
+
+                (void)SDL_snprintf(text, sizeof text, "%s mode %d: %dx%d@%gHz",
+                                   SDL_GetDisplayName(display_id),
+                                   j, mode->pixel_w, mode->pixel_h, mode->refresh_rate);
+
+                /* Update column width */
+                text_length = (int)SDL_strlen(text);
+                column_chars = SDL_max(column_chars, text_length);
+
+                /* Check if under mouse */
+                cell_rect.x = x;
+                cell_rect.y = y;
+                cell_rect.w = (float)(text_length * FONT_CHARACTER_SIZE);
+                cell_rect.h = (float)lineHeight;
+
+                if (SDL_PointInRectFloat(&mouse_pos, &cell_rect)) {
+                    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
+
+                    /* Update cached mode under the mouse */
+                    if (window == SDL_GetMouseFocus()) {
+                        highlighted_mode = mode;
+                    }
+                } else {
+                    SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255);
+                }
 
-        if (SDL_PointInRectFloat(&mouse_pos, &cell_rect)) {
-            SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
+                SDLTest_DrawString(renderer, x, y, text);
+                y += lineHeight;
 
-            /* Update cached mode under the mouse */
-            if (window == SDL_GetMouseFocus()) {
-                highlighted_mode = i;
+                if ((y + lineHeight) > (viewport.y + viewport.h)) {
+                    /* Advance to next column */
+                    x += (column_chars + 1) * FONT_CHARACTER_SIZE;
+                    y = table_top;
+                    column_chars = 0;
+                }
             }
-        } else {
-            SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255);
-        }
-
-        SDLTest_DrawString(renderer, x, y, text);
-        y += lineHeight;
-
-        if ((y + lineHeight) > (viewport.y + viewport.h)) {
-            /* Advance to next column */
-            x += (column_chars + 1) * FONT_CHARACTER_SIZE;
-            y = table_top;
-            column_chars = 0;
+            SDL_free((void *)modes);
         }
+        SDL_free(display_ids);
     }
-    SDL_free((void *)modes);
 }
 
 void loop()
@@ -206,15 +215,9 @@ void loop()
         }
         if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) {
             SDL_Window *window = SDL_GetMouseFocus();
-            if (highlighted_mode != -1 && window != NULL) {
-                SDL_DisplayID displayID = SDL_GetDisplayForWindow(window);
-                int num_modes;
-                const SDL_DisplayMode **modes = SDL_GetFullscreenDisplayModes(displayID, &num_modes);
-                if (highlighted_mode < num_modes) {
-                    SDL_memcpy(&state->fullscreen_mode, modes[highlighted_mode], sizeof(state->fullscreen_mode));
-                    SDL_SetWindowFullscreenMode(window, modes[highlighted_mode]);
-                }
-                SDL_free((void *)modes);
+            if (highlighted_mode != NULL && window != NULL) {
+                SDL_memcpy(&state->fullscreen_mode, highlighted_mode, sizeof(state->fullscreen_mode));
+                SDL_SetWindowFullscreenMode(window, highlighted_mode);
             }
         }
     }