test/controllermap.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 27 Dec 2016 09:51:58 -0800
changeset 10726 893faaa67394
parent 10724 5ea5f198879f
child 10729 9059ffccccb2
permissions -rw-r--r--
Make sure we go all the way back (within the XBox controller dead zone) to prevent accidentally binding axes inverted
     1 /*
     2   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 /* Game controller mapping generator */
    14 /* Gabriel Jacobo <gabomdq@gmail.com> */
    15 
    16 #include <stdio.h>
    17 #include <stdlib.h>
    18 #include <string.h>
    19 
    20 #include "SDL.h"
    21 
    22 #ifndef SDL_JOYSTICK_DISABLED
    23 
    24 #ifdef __IPHONEOS__
    25 #define SCREEN_WIDTH    320
    26 #define SCREEN_HEIGHT   480
    27 #else
    28 #define SCREEN_WIDTH    512
    29 #define SCREEN_HEIGHT   320
    30 #endif
    31 
    32 #define MARKER_BUTTON 1
    33 #define MARKER_AXIS 2
    34 
    35 enum
    36 {
    37     SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE,
    38     SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE,
    39     SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE,
    40     SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE,
    41     SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE,
    42     SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE,
    43     SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE,
    44     SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE,
    45     SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT,
    46     SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT,
    47     SDL_CONTROLLER_BINDING_AXIS_MAX,
    48 };
    49 
    50 #define BINDING_COUNT (SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_MAX)
    51 
    52 static struct 
    53 {
    54     int x, y;
    55     double angle;
    56     int marker;
    57 
    58 } s_arrBindingDisplay[BINDING_COUNT] = {
    59     { 387, 167, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_A */
    60     { 431, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_B */
    61     { 342, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_X */
    62     { 389, 101, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_Y */
    63     { 174, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_BACK */
    64     { 233, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_GUIDE */
    65     { 289, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_START */
    66     {  75, 154, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_LEFTSTICK */
    67     { 305, 230, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_RIGHTSTICK */
    68     {  77,  40, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_LEFTSHOULDER */
    69     { 396,  36, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_RIGHTSHOULDER */
    70     { 154, 188, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_UP */
    71     { 154, 249, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_DOWN */
    72     { 116, 217, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_LEFT */
    73     { 186, 217, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_RIGHT */
    74     {  74, 153, 270.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE */
    75     {  74, 153, 90.0,  MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE */
    76     {  74, 153, 0.0,   MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE */
    77     {  74, 153, 180.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE */
    78     { 306, 231, 270.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE */
    79     { 306, 231, 90.0,  MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE */
    80     { 306, 231, 0.0,   MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE */
    81     { 306, 231, 180.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE */
    82     {  91, -20, 180.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT */
    83     { 375, -20, 180.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT */
    84 };
    85 
    86 static int s_arrBindingOrder[BINDING_COUNT] = {
    87     SDL_CONTROLLER_BUTTON_A,
    88     SDL_CONTROLLER_BUTTON_B,
    89     SDL_CONTROLLER_BUTTON_Y,
    90     SDL_CONTROLLER_BUTTON_X,
    91     SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE,
    92     SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE,
    93     SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE,
    94     SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE,
    95     SDL_CONTROLLER_BUTTON_LEFTSTICK,
    96     SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE,
    97     SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE,
    98     SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE,
    99     SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE,
   100     SDL_CONTROLLER_BUTTON_RIGHTSTICK,
   101     SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
   102     SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT,
   103     SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
   104     SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT,
   105     SDL_CONTROLLER_BUTTON_DPAD_UP,
   106     SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
   107     SDL_CONTROLLER_BUTTON_DPAD_DOWN,
   108     SDL_CONTROLLER_BUTTON_DPAD_LEFT,
   109     SDL_CONTROLLER_BUTTON_BACK,
   110     SDL_CONTROLLER_BUTTON_GUIDE,
   111     SDL_CONTROLLER_BUTTON_START,
   112 };
   113 
   114 typedef struct
   115 {
   116     SDL_GameControllerBindType bindType;
   117     union
   118     {
   119         int button;
   120 
   121         struct {
   122             int axis;
   123             int axis_min;
   124             int axis_max;
   125         } axis;
   126 
   127         struct {
   128             int hat;
   129             int hat_mask;
   130         } hat;
   131 
   132     } value;
   133 
   134 } SDL_GameControllerExtendedBind;
   135 
   136 static SDL_GameControllerExtendedBind s_arrBindings[BINDING_COUNT];
   137 
   138 typedef struct
   139 {
   140     SDL_bool m_bMoving;
   141     int m_nStartingValue;
   142     int m_nFarthestValue;
   143 } AxisState;
   144 
   145 static int s_nNumAxes;
   146 static AxisState *s_arrAxisState;
   147     
   148 static int s_iCurrentBinding;
   149 static Uint32 s_unPendingAdvanceTime;
   150 static SDL_bool s_bBindingComplete;
   151 
   152 SDL_Texture *
   153 LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
   154 {
   155     SDL_Surface *temp;
   156     SDL_Texture *texture;
   157 
   158     /* Load the sprite image */
   159     temp = SDL_LoadBMP(file);
   160     if (temp == NULL) {
   161         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
   162         return NULL;
   163     }
   164 
   165     /* Set transparent pixel as the pixel at (0,0) */
   166     if (transparent) {
   167         if (temp->format->palette) {
   168             SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
   169         }
   170     }
   171 
   172     /* Create textures from the image */
   173     texture = SDL_CreateTextureFromSurface(renderer, temp);
   174     if (!texture) {
   175         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
   176         SDL_FreeSurface(temp);
   177         return NULL;
   178     }
   179     SDL_FreeSurface(temp);
   180 
   181     /* We're ready to roll. :) */
   182     return texture;
   183 }
   184 
   185 static int
   186 StandardizeAxisValue(int nValue)
   187 {
   188     if (nValue > SDL_JOYSTICK_AXIS_MAX/2) {
   189         return SDL_JOYSTICK_AXIS_MAX;
   190     } else if (nValue < SDL_JOYSTICK_AXIS_MIN/2) {
   191         return SDL_JOYSTICK_AXIS_MIN;
   192     } else {
   193         return 0;
   194     }
   195 }
   196 
   197 static void
   198 SetCurrentBinding(int iBinding)
   199 {
   200     SDL_GameControllerExtendedBind *pBinding;
   201 
   202     if (iBinding < 0) {
   203         return;
   204     }
   205 
   206     if (iBinding == BINDING_COUNT) {
   207         s_bBindingComplete = SDL_TRUE;
   208         return;
   209     }
   210 
   211     s_iCurrentBinding = iBinding;
   212 
   213     pBinding = &s_arrBindings[s_arrBindingOrder[s_iCurrentBinding]];
   214     SDL_zerop(pBinding);
   215 
   216     SDL_memset(s_arrAxisState, 0, s_nNumAxes*sizeof(*s_arrAxisState));
   217 
   218     s_unPendingAdvanceTime = 0;
   219 }
   220 
   221 static void
   222 ConfigureBinding(const SDL_GameControllerExtendedBind *pBinding)
   223 {
   224     SDL_GameControllerExtendedBind *pCurrent;
   225     int iIndex;
   226     int iCurrentElement = s_arrBindingOrder[s_iCurrentBinding];
   227 
   228     /* Do we already have this binding? */
   229     for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
   230         if (SDL_memcmp(pBinding, &s_arrBindings[iIndex], sizeof(*pBinding)) == 0) {
   231             if (iIndex == SDL_CONTROLLER_BUTTON_A && iCurrentElement != SDL_CONTROLLER_BUTTON_B) {
   232                 /* Skip to the next binding */
   233                 SetCurrentBinding(s_iCurrentBinding + 1);
   234                 return;
   235             }
   236 
   237             if (iIndex == SDL_CONTROLLER_BUTTON_B) {
   238                 /* Go back to the previous binding */
   239                 SetCurrentBinding(s_iCurrentBinding - 1);
   240                 return;
   241             }
   242 
   243             /* Already have this binding, ignore it */
   244             return;
   245         }
   246     }
   247 
   248     /* Should the new binding override the existing one? */
   249     pCurrent = &s_arrBindings[iCurrentElement];
   250     if (pCurrent->bindType != SDL_CONTROLLER_BINDTYPE_NONE) {
   251         SDL_bool bNativeDPad, bCurrentDPad;
   252         SDL_bool bNativeAxis, bCurrentAxis;
   253         
   254         bNativeDPad = (iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_UP ||
   255                        iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_DOWN ||
   256                        iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_LEFT ||
   257                        iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
   258         bCurrentDPad = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_HAT);
   259         if (bNativeDPad == bCurrentDPad) {
   260             /* We already have a binding of the type we want, ignore the new one */
   261             return;
   262         }
   263 
   264         bNativeAxis = (iCurrentElement >= SDL_CONTROLLER_BUTTON_MAX);
   265         bCurrentAxis = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_AXIS);
   266         if (bNativeAxis == bCurrentAxis) {
   267             /* We already have a binding of the type we want, ignore the new one */
   268             return;
   269         }
   270     }
   271 
   272     *pCurrent = *pBinding;
   273 
   274     s_unPendingAdvanceTime = SDL_GetTicks();
   275 }
   276 
   277 static SDL_bool
   278 BMergeAxisBindings(int iIndex)
   279 {
   280     SDL_GameControllerExtendedBind *pBindingA = &s_arrBindings[iIndex];
   281     SDL_GameControllerExtendedBind *pBindingB = &s_arrBindings[iIndex+1];
   282     if (pBindingA->bindType == SDL_CONTROLLER_BINDTYPE_AXIS &&
   283         pBindingB->bindType == SDL_CONTROLLER_BINDTYPE_AXIS &&
   284         pBindingA->value.axis.axis == pBindingB->value.axis.axis) {
   285         if (pBindingA->value.axis.axis_min == pBindingB->value.axis.axis_min) {
   286             pBindingA->value.axis.axis_min = pBindingA->value.axis.axis_max;
   287             pBindingA->value.axis.axis_max = pBindingB->value.axis.axis_max;
   288             pBindingB->bindType = SDL_CONTROLLER_BINDTYPE_NONE;
   289             return SDL_TRUE;
   290         }
   291     }
   292     return SDL_FALSE;
   293 }
   294 
   295 static void
   296 WatchJoystick(SDL_Joystick * joystick)
   297 {
   298     SDL_Window *window = NULL;
   299     SDL_Renderer *screen = NULL;
   300     SDL_Texture *background, *button, *axis, *marker;
   301     const char *name = NULL;
   302     SDL_bool done = SDL_FALSE;
   303     SDL_Event event;
   304     SDL_Rect dst;
   305     Uint8 alpha=200, alpha_step = -1;
   306     Uint32 alpha_ticks = 0;
   307     SDL_JoystickID nJoystickID;
   308     Uint32 unDeflectedAxes = 0;
   309 
   310     /* Create a window to display joystick axis position */
   311     window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
   312                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
   313                               SCREEN_HEIGHT, 0);
   314     if (window == NULL) {
   315         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
   316         return;
   317     }
   318 
   319     screen = SDL_CreateRenderer(window, -1, 0);
   320     if (screen == NULL) {
   321         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
   322         SDL_DestroyWindow(window);
   323         return;
   324     }
   325     
   326     background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
   327     button = LoadTexture(screen, "button.bmp", SDL_TRUE);
   328     axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
   329     SDL_RaiseWindow(window);
   330 
   331     /* scale for platforms that don't give you the window size you asked for. */
   332     SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
   333 
   334     /* Print info about the joystick we are watching */
   335     name = SDL_JoystickName(joystick);
   336     SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
   337            name ? name : "Unknown Joystick");
   338     SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
   339            SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
   340            SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
   341     
   342     SDL_Log("\n\n\
   343     ====================================================================================\n\
   344     Press the buttons on your controller when indicated\n\
   345     (Your controller may look different than the picture)\n\
   346     If you want to correct a mistake, press backspace or the back button on your device\n\
   347     To skip a button, press SPACE or click/touch the screen\n\
   348     To exit, press ESC\n\
   349     ====================================================================================\n");
   350 
   351     nJoystickID = SDL_JoystickInstanceID(joystick);
   352 
   353     s_nNumAxes = SDL_JoystickNumAxes(joystick);
   354     s_arrAxisState = SDL_calloc(s_nNumAxes, sizeof(*s_arrAxisState));
   355 
   356     /* Loop, getting joystick events! */
   357     while (!done && !s_bBindingComplete) {
   358         int iElement = s_arrBindingOrder[s_iCurrentBinding];
   359 
   360         switch (s_arrBindingDisplay[iElement].marker) {
   361             case MARKER_AXIS:
   362                 marker = axis;
   363                 break;
   364             case MARKER_BUTTON:
   365                 marker = button;
   366                 break;
   367             default:
   368                 break;
   369         }
   370         
   371         dst.x = s_arrBindingDisplay[iElement].x;
   372         dst.y = s_arrBindingDisplay[iElement].y;
   373         SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h);
   374 
   375         if (SDL_GetTicks() - alpha_ticks > 5) {
   376             alpha_ticks = SDL_GetTicks();
   377             alpha += alpha_step;
   378             if (alpha == 255) {
   379                 alpha_step = -1;
   380             }
   381             if (alpha < 128) {
   382                 alpha_step = 1;
   383             }
   384         }
   385 
   386         SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
   387         SDL_RenderClear(screen);
   388         SDL_RenderCopy(screen, background, NULL, NULL);
   389         SDL_SetTextureAlphaMod(marker, alpha);
   390         SDL_SetTextureColorMod(marker, 10, 255, 21);
   391         SDL_RenderCopyEx(screen, marker, NULL, &dst, s_arrBindingDisplay[iElement].angle, NULL, SDL_FLIP_NONE);
   392         SDL_RenderPresent(screen);
   393             
   394         while (SDL_PollEvent(&event) > 0) {
   395             switch (event.type) {
   396             case SDL_JOYDEVICEREMOVED:
   397                 if (event.jaxis.which == nJoystickID) {
   398                     done = SDL_TRUE;
   399                 }
   400                 break;
   401             case SDL_JOYAXISMOTION:
   402                 if (event.jaxis.which == nJoystickID) {
   403                     AxisState *pAxisState = &s_arrAxisState[event.jaxis.axis];
   404                     int nValue = event.jaxis.value;
   405                     int nCurrentDistance, nFarthestDistance;
   406                     if (!pAxisState->m_bMoving) {
   407                         pAxisState->m_bMoving = SDL_TRUE;
   408                         pAxisState->m_nStartingValue = nValue;
   409                         pAxisState->m_nFarthestValue = nValue;
   410                     }
   411                     nCurrentDistance = SDL_abs(nValue - pAxisState->m_nStartingValue);
   412                     nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);
   413                     if (nCurrentDistance > nFarthestDistance) {
   414                         pAxisState->m_nFarthestValue = nValue;
   415                     }
   416                     if (nCurrentDistance <= 8000 && nFarthestDistance >= 20000) {
   417                         /* We've gone out and back, let's bind this axis */
   418                         SDL_GameControllerExtendedBind binding;
   419                         SDL_zero(binding);
   420                         binding.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
   421                         binding.value.axis.axis = event.jaxis.axis;
   422                         binding.value.axis.axis_min = StandardizeAxisValue(pAxisState->m_nStartingValue);
   423                         binding.value.axis.axis_max = StandardizeAxisValue(pAxisState->m_nFarthestValue);
   424                         ConfigureBinding(&binding);
   425                     }
   426                 }
   427                 break;
   428             case SDL_JOYHATMOTION:
   429                 if (event.jhat.which == nJoystickID) {
   430                     if (event.jhat.value != SDL_HAT_CENTERED) {
   431                         SDL_GameControllerExtendedBind binding;
   432                         SDL_zero(binding);
   433                         binding.bindType = SDL_CONTROLLER_BINDTYPE_HAT;
   434                         binding.value.hat.hat = event.jhat.hat;
   435                         binding.value.hat.hat_mask = event.jhat.value;
   436                         ConfigureBinding(&binding);
   437                     }
   438                 }
   439                 break;
   440             case SDL_JOYBALLMOTION:
   441                 break;
   442             case SDL_JOYBUTTONDOWN:
   443                 if (event.jbutton.which == nJoystickID) {
   444                     SDL_GameControllerExtendedBind binding;
   445                     SDL_zero(binding);
   446                     binding.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
   447                     binding.value.button = event.jbutton.button;
   448                     ConfigureBinding(&binding);
   449                 }
   450                 break;
   451             case SDL_FINGERDOWN:
   452             case SDL_MOUSEBUTTONDOWN:
   453                 /* Skip this step */
   454                 SetCurrentBinding(s_iCurrentBinding + 1);
   455                 break;
   456             case SDL_KEYDOWN:
   457                 if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
   458                     SetCurrentBinding(s_iCurrentBinding - 1);
   459                     break;
   460                 }
   461                 if (event.key.keysym.sym == SDLK_SPACE) {
   462                     SetCurrentBinding(s_iCurrentBinding + 1);
   463                     break;
   464                 }
   465 
   466                 if ((event.key.keysym.sym != SDLK_ESCAPE)) {
   467                     break;
   468                 }
   469                 /* Fall through to signal quit */
   470             case SDL_QUIT:
   471                 done = SDL_TRUE;
   472                 break;
   473             default:
   474                 break;
   475             }
   476         }
   477 
   478         SDL_Delay(15);
   479 
   480         /* Wait 100 ms for joystick events to stop coming in,
   481            in case a controller sends multiple events for a single control (e.g. axis and button for trigger)
   482         */
   483         if (s_unPendingAdvanceTime && SDL_GetTicks() - s_unPendingAdvanceTime >= 100) {
   484             SetCurrentBinding(s_iCurrentBinding + 1);
   485         }
   486     }
   487 
   488     if (s_bBindingComplete) {
   489         char mapping[1024];
   490         char trimmed_name[128];
   491         char *spot;
   492         int iIndex;
   493         char pszElement[12];
   494 
   495         SDL_strlcpy(trimmed_name, name, SDL_arraysize(trimmed_name));
   496         while (SDL_isspace(trimmed_name[0])) {
   497             SDL_memmove(&trimmed_name[0], &trimmed_name[1], SDL_strlen(trimmed_name));
   498         }
   499         while (trimmed_name[0] && SDL_isspace(trimmed_name[SDL_strlen(trimmed_name) - 1])) {
   500             trimmed_name[SDL_strlen(trimmed_name) - 1] = '\0';
   501         }
   502         while ((spot = SDL_strchr(trimmed_name, ',')) != NULL) {
   503             SDL_memmove(spot, spot + 1, SDL_strlen(spot));
   504         }
   505 
   506         /* Initialize mapping with GUID and name */
   507         SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), mapping, SDL_arraysize(mapping));
   508         SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   509         SDL_strlcat(mapping, trimmed_name, SDL_arraysize(mapping));
   510         SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   511         SDL_strlcat(mapping, "platform:", SDL_arraysize(mapping));
   512         SDL_strlcat(mapping, SDL_GetPlatform(), SDL_arraysize(mapping));
   513         SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   514 
   515         for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
   516             SDL_GameControllerExtendedBind *pBinding = &s_arrBindings[iIndex];
   517             if (pBinding->bindType == SDL_CONTROLLER_BINDTYPE_NONE) {
   518                 continue;
   519             }
   520 
   521             if (iIndex < SDL_CONTROLLER_BUTTON_MAX) {
   522                 SDL_GameControllerButton eButton = (SDL_GameControllerButton)iIndex;
   523                 SDL_strlcat(mapping, SDL_GameControllerGetStringForButton(eButton), SDL_arraysize(mapping));
   524             } else {
   525                 const char *pszAxisName;
   526                 switch (iIndex - SDL_CONTROLLER_BUTTON_MAX) {
   527                 case SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE:
   528                     if (!BMergeAxisBindings(iIndex)) {
   529                         SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
   530                     }
   531                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTX);
   532                     break;
   533                 case SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE:
   534                     SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
   535                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTX);
   536                     break;
   537                 case SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE:
   538                     if (!BMergeAxisBindings(iIndex)) {
   539                         SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
   540                     }
   541                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTY);
   542                     break;
   543                 case SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE:
   544                     SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
   545                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTY);
   546                     break;
   547                 case SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE:
   548                     if (!BMergeAxisBindings(iIndex)) {
   549                         SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
   550                     }
   551                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTX);
   552                     break;
   553                 case SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE:
   554                     SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
   555                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTX);
   556                     break;
   557                 case SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE:
   558                     if (!BMergeAxisBindings(iIndex)) {
   559                         SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
   560                     }
   561                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTY);
   562                     break;
   563                 case SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE:
   564                     SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
   565                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTY);
   566                     break;
   567                 case SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT:
   568                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_TRIGGERLEFT);
   569                     break;
   570                 case SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT:
   571                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
   572                     break;
   573                 }
   574                 SDL_strlcat(mapping, pszAxisName, SDL_arraysize(mapping));
   575             }
   576             SDL_strlcat(mapping, ":", SDL_arraysize(mapping));
   577 
   578             pszElement[0] = '\0';
   579             switch (pBinding->bindType) {
   580             case SDL_CONTROLLER_BINDTYPE_BUTTON:
   581                 SDL_snprintf(pszElement, sizeof(pszElement), "b%d", pBinding->value.button);
   582                 break;
   583             case SDL_CONTROLLER_BINDTYPE_AXIS:
   584                 if (pBinding->value.axis.axis_min == 0 && pBinding->value.axis.axis_max == SDL_JOYSTICK_AXIS_MIN) {
   585                     /* The negative half axis */
   586                     SDL_snprintf(pszElement, sizeof(pszElement), "-a%d", pBinding->value.axis.axis);
   587                 } else if (pBinding->value.axis.axis_min == 0 && pBinding->value.axis.axis_max == SDL_JOYSTICK_AXIS_MAX) {
   588                     /* The positive half axis */
   589                     SDL_snprintf(pszElement, sizeof(pszElement), "+a%d", pBinding->value.axis.axis);
   590                 } else {
   591                     SDL_snprintf(pszElement, sizeof(pszElement), "a%d", pBinding->value.axis.axis);
   592                     if (pBinding->value.axis.axis_min > pBinding->value.axis.axis_max) {
   593                         /* Invert the axis */
   594                         SDL_strlcat(pszElement, "~", SDL_arraysize(pszElement));
   595                     }
   596                 }
   597                 break;
   598             case SDL_CONTROLLER_BINDTYPE_HAT:
   599                 SDL_snprintf(pszElement, sizeof(pszElement), "h%d.%d", pBinding->value.hat.hat, pBinding->value.hat.hat_mask);
   600                 break;
   601             default:
   602                 SDL_assert(!"Unknown bind type");
   603                 break;
   604             }
   605             SDL_strlcat(mapping, pszElement, SDL_arraysize(mapping));
   606             SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   607         }
   608 
   609         SDL_Log("Mapping:\n\n%s\n\n", mapping);
   610         /* Print to stdout as well so the user can cat the output somewhere */
   611         printf("%s\n", mapping);
   612     }
   613 
   614     SDL_free(s_arrAxisState);
   615     s_arrAxisState = NULL;
   616     
   617     SDL_DestroyRenderer(screen);
   618     SDL_DestroyWindow(window);
   619 }
   620 
   621 int
   622 main(int argc, char *argv[])
   623 {
   624     const char *name;
   625     int i;
   626     SDL_Joystick *joystick;
   627 
   628     /* Enable standard application logging */
   629     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   630 
   631     /* Initialize SDL (Note: video is required to start event loop) */
   632     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   633         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   634         exit(1);
   635     }
   636 
   637     /* Print information about the joysticks */
   638     SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
   639     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   640         name = SDL_JoystickNameForIndex(i);
   641         SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   642         joystick = SDL_JoystickOpen(i);
   643         if (joystick == NULL) {
   644             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
   645                     SDL_GetError());
   646         } else {
   647             char guid[64];
   648             SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
   649                                       guid, sizeof (guid));
   650             SDL_Log("       axes: %d\n", SDL_JoystickNumAxes(joystick));
   651             SDL_Log("      balls: %d\n", SDL_JoystickNumBalls(joystick));
   652             SDL_Log("       hats: %d\n", SDL_JoystickNumHats(joystick));
   653             SDL_Log("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
   654             SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
   655             SDL_Log("       guid: %s\n", guid);
   656             SDL_Log("    VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick));
   657             SDL_JoystickClose(joystick);
   658         }
   659     }
   660 
   661 #ifdef __ANDROID__
   662     if (SDL_NumJoysticks() > 0) {
   663 #else
   664     if (argv[1]) {
   665 #endif
   666         int device;
   667 #ifdef __ANDROID__
   668         device = 0;
   669 #else
   670         device = atoi(argv[1]);
   671 #endif
   672         joystick = SDL_JoystickOpen(device);
   673         if (joystick == NULL) {
   674             SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
   675         } else {
   676             WatchJoystick(joystick);
   677             SDL_JoystickClose(joystick);
   678         }
   679     }
   680     else {
   681         SDL_Log("\n\nUsage: ./controllermap number\nFor example: ./controllermap 0\nOr: ./controllermap 0 >> gamecontrollerdb.txt");
   682     }
   683     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   684 
   685     return 0;
   686 }
   687 
   688 #else
   689 
   690 int
   691 main(int argc, char *argv[])
   692 {
   693     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
   694     exit(1);
   695 }
   696 
   697 #endif
   698 
   699 /* vi: set ts=4 sw=4 expandtab: */