SDL: testcontroller: Don't query the mapping list until after they are available.

From c5daf8adb172f86bc04ede93385a7a06991a1c32 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Wed, 29 Nov 2023 10:33:31 -0500
Subject: [PATCH] testcontroller: Don't query the mapping list until after they
 are available.

The included ones need to wait until SDL_Init has run, or you'll get an empty
list, and we might also be adding more from an external gamecontrollerdb.txt
file, too.
---
 test/testcontroller.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/test/testcontroller.c b/test/testcontroller.c
index 3dbafb59603a..e7d7260bc6e0 100644
--- a/test/testcontroller.c
+++ b/test/testcontroller.c
@@ -1876,6 +1876,7 @@ int main(int argc, char *argv[])
     int screen_width, screen_height;
     SDL_Rect area;
     int gamepad_index = -1;
+    SDL_bool show_mappings = SDL_FALSE;
     SDLTest_CommonState *state;
 
     /* Initialize test framework */
@@ -1902,16 +1903,7 @@ int main(int argc, char *argv[])
         consumed = SDLTest_CommonArg(state, i);
         if (!consumed) {
             if (SDL_strcmp(argv[i], "--mappings") == 0) {
-                int map_i;
-                SDL_Log("Supported mappings:\n");
-                for (map_i = 0; map_i < SDL_GetNumGamepadMappings(); ++map_i) {
-                    char *mapping = SDL_GetGamepadMappingForIndex(map_i);
-                    if (mapping) {
-                        SDL_Log("\t%s\n", mapping);
-                        SDL_free(mapping);
-                    }
-                }
-                SDL_Log("\n");
+                show_mappings = SDL_TRUE;
                 consumed = 1;
             } else if (SDL_strcmp(argv[i], "--virtual") == 0) {
                 OpenVirtualGamepad();
@@ -1944,6 +1936,19 @@ int main(int argc, char *argv[])
 
     SDL_AddGamepadMappingsFromFile("gamecontrollerdb.txt");
 
+    if (show_mappings) {
+        int map_i;
+        SDL_Log("Supported mappings:\n");
+        for (map_i = 0; map_i < SDL_GetNumGamepadMappings(); ++map_i) {
+            char *mapping = SDL_GetGamepadMappingForIndex(map_i);
+            if (mapping) {
+                SDL_Log("\t%s\n", mapping);
+                SDL_free(mapping);
+            }
+        }
+        SDL_Log("\n");
+    }
+
     /* Create a window to display gamepad state */
     content_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
     if (content_scale == 0.0f) {