Made testgamecontroller minimally more useful.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 24 Feb 2013 22:57:25 -0500
changeset 6914 8a9915ab24f0
parent 6913 7e9c4d29e668
child 6915 d58bf9cfd709
Made testgamecontroller minimally more useful.
test/testgamecontroller.c
--- a/test/testgamecontroller.c	Sun Feb 24 22:56:35 2013 -0500
+++ b/test/testgamecontroller.c	Sun Feb 24 22:57:25 2013 -0500
@@ -40,9 +40,58 @@
     SDL_RenderFillRect(r, &area);
 }
 
+static const char *
+ControllerAxisName(const SDL_CONTROLLER_AXIS axis)
+{
+    switch (axis)
+    {
+        #define AXIS_CASE(ax) case SDL_CONTROLLER_AXIS_##ax: return #ax
+        AXIS_CASE(INVALID);
+        AXIS_CASE(LEFTX);
+        AXIS_CASE(LEFTY);
+        AXIS_CASE(RIGHTX);
+        AXIS_CASE(RIGHTY);
+        AXIS_CASE(TRIGGERLEFT);
+        AXIS_CASE(TRIGGERRIGHT);
+        #undef AXIS_CASE
+        default: return "???";
+    }
+}
+
+static const char *
+ControllerButtonName(const SDL_CONTROLLER_BUTTON button)
+{
+    switch (button)
+    {
+        #define BUTTON_CASE(btn) case SDL_CONTROLLER_BUTTON_##btn: return #btn
+        BUTTON_CASE(INVALID);
+        BUTTON_CASE(A);
+        BUTTON_CASE(B);
+        BUTTON_CASE(X);
+        BUTTON_CASE(Y);
+        BUTTON_CASE(BACK);
+        BUTTON_CASE(GUIDE);
+        BUTTON_CASE(START);
+        BUTTON_CASE(LEFTSTICK);
+        BUTTON_CASE(RIGHTSTICK);
+        BUTTON_CASE(LEFTSHOULDER);
+        BUTTON_CASE(RIGHTSHOULDER);
+        BUTTON_CASE(DPAD_UP);
+        BUTTON_CASE(DPAD_DOWN);
+        BUTTON_CASE(DPAD_LEFT);
+        BUTTON_CASE(DPAD_RIGHT);
+        #undef BUTTON_CASE
+        default: return "???";
+    }
+}
+
 void
 WatchGameController(SDL_GameController * gamecontroller)
 {
+    const char *controllername = gamecontroller ? SDL_GameControllerName(gamecontroller) : "???";
+    const char *basetitle = "Game Controller Test: ";
+    const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(controllername) + 1;
+    char *title = SDL_malloc(titlelen);
     SDL_Window *window = NULL;
     SDL_Renderer *screen = NULL;
     const char *name = NULL;
@@ -50,8 +99,12 @@
     SDL_Event event;
     int i;
 
+    if (title) {
+        SDL_snprintf(title, titlelen, "%s%s", basetitle, controllername);
+    }
+
     /* Create a window to display controller axis position */
-    window = SDL_CreateWindow("Game Controller Test", SDL_WINDOWPOS_CENTERED,
+    window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
                               SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
     if (window == NULL) {
@@ -84,17 +137,21 @@
         while (SDL_PollEvent(&event)) {
             switch (event.type) {
             case SDL_CONTROLLERAXISMOTION:
-                printf("Controller %d axis %d value: %d\n",
+                printf("Controller %d axis %d ('%s') value: %d\n",
                        event.caxis.which,
-                       event.caxis.axis, event.caxis.value);
+                       event.caxis.axis,
+                       ControllerAxisName(event.caxis.axis),
+                       event.caxis.value);
                 break;
             case SDL_CONTROLLERBUTTONDOWN:
-                printf("Controller %d button %d down\n",
-                       event.cbutton.which, event.cbutton.button);
+                printf("Controller %d button %d ('%s') down\n",
+                       event.cbutton.which, event.cbutton.button,
+                       ControllerButtonName(event.cbutton.button));
                 break;
             case SDL_CONTROLLERBUTTONUP:
-                printf("Controller %d button %d up\n",
-                       event.cbutton.which, event.cbutton.button);
+                printf("Controller %d button %d ('%s') up\n",
+                       event.cbutton.which, event.cbutton.button,
+                       ControllerButtonName(event.cbutton.button));
                 break;
             case SDL_KEYDOWN:
                 if (event.key.keysym.sym != SDLK_ESCAPE) {
@@ -103,7 +160,7 @@
                 /* Fall through to signal quit */
             case SDL_QUIT:
                 done = 1;
-				s_ForceQuit = SDL_TRUE;
+                s_ForceQuit = SDL_TRUE;
                 break;
             default:
                 break;