test/controllermap.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 28 Dec 2016 20:11:12 +0100
changeset 10729 9059ffccccb2
parent 10726 893faaa67394
child 10737 3406a0f8b041
permissions -rw-r--r--
Fixed warning about unused variable in controllermap program.
     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 
   309     /* Create a window to display joystick axis position */
   310     window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
   311                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
   312                               SCREEN_HEIGHT, 0);
   313     if (window == NULL) {
   314         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
   315         return;
   316     }
   317 
   318     screen = SDL_CreateRenderer(window, -1, 0);
   319     if (screen == NULL) {
   320         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
   321         SDL_DestroyWindow(window);
   322         return;
   323     }
   324     
   325     background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
   326     button = LoadTexture(screen, "button.bmp", SDL_TRUE);
   327     axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
   328     SDL_RaiseWindow(window);
   329 
   330     /* scale for platforms that don't give you the window size you asked for. */
   331     SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
   332 
   333     /* Print info about the joystick we are watching */
   334     name = SDL_JoystickName(joystick);
   335     SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
   336            name ? name : "Unknown Joystick");
   337     SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
   338            SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
   339            SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
   340     
   341     SDL_Log("\n\n\
   342     ====================================================================================\n\
   343     Press the buttons on your controller when indicated\n\
   344     (Your controller may look different than the picture)\n\
   345     If you want to correct a mistake, press backspace or the back button on your device\n\
   346     To skip a button, press SPACE or click/touch the screen\n\
   347     To exit, press ESC\n\
   348     ====================================================================================\n");
   349 
   350     nJoystickID = SDL_JoystickInstanceID(joystick);
   351 
   352     s_nNumAxes = SDL_JoystickNumAxes(joystick);
   353     s_arrAxisState = SDL_calloc(s_nNumAxes, sizeof(*s_arrAxisState));
   354 
   355     /* Loop, getting joystick events! */
   356     while (!done && !s_bBindingComplete) {
   357         int iElement = s_arrBindingOrder[s_iCurrentBinding];
   358 
   359         switch (s_arrBindingDisplay[iElement].marker) {
   360             case MARKER_AXIS:
   361                 marker = axis;
   362                 break;
   363             case MARKER_BUTTON:
   364                 marker = button;
   365                 break;
   366             default:
   367                 break;
   368         }
   369         
   370         dst.x = s_arrBindingDisplay[iElement].x;
   371         dst.y = s_arrBindingDisplay[iElement].y;
   372         SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h);
   373 
   374         if (SDL_GetTicks() - alpha_ticks > 5) {
   375             alpha_ticks = SDL_GetTicks();
   376             alpha += alpha_step;
   377             if (alpha == 255) {
   378                 alpha_step = -1;
   379             }
   380             if (alpha < 128) {
   381                 alpha_step = 1;
   382             }
   383         }
   384 
   385         SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
   386         SDL_RenderClear(screen);
   387         SDL_RenderCopy(screen, background, NULL, NULL);
   388         SDL_SetTextureAlphaMod(marker, alpha);
   389         SDL_SetTextureColorMod(marker, 10, 255, 21);
   390         SDL_RenderCopyEx(screen, marker, NULL, &dst, s_arrBindingDisplay[iElement].angle, NULL, SDL_FLIP_NONE);
   391         SDL_RenderPresent(screen);
   392             
   393         while (SDL_PollEvent(&event) > 0) {
   394             switch (event.type) {
   395             case SDL_JOYDEVICEREMOVED:
   396                 if (event.jaxis.which == nJoystickID) {
   397                     done = SDL_TRUE;
   398                 }
   399                 break;
   400             case SDL_JOYAXISMOTION:
   401                 if (event.jaxis.which == nJoystickID) {
   402                     AxisState *pAxisState = &s_arrAxisState[event.jaxis.axis];
   403                     int nValue = event.jaxis.value;
   404                     int nCurrentDistance, nFarthestDistance;
   405                     if (!pAxisState->m_bMoving) {
   406                         pAxisState->m_bMoving = SDL_TRUE;
   407                         pAxisState->m_nStartingValue = nValue;
   408                         pAxisState->m_nFarthestValue = nValue;
   409                     }
   410                     nCurrentDistance = SDL_abs(nValue - pAxisState->m_nStartingValue);
   411                     nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);
   412                     if (nCurrentDistance > nFarthestDistance) {
   413                         pAxisState->m_nFarthestValue = nValue;
   414                     }
   415                     if (nCurrentDistance <= 8000 && nFarthestDistance >= 20000) {
   416                         /* We've gone out and back, let's bind this axis */
   417                         SDL_GameControllerExtendedBind binding;
   418                         SDL_zero(binding);
   419                         binding.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
   420                         binding.value.axis.axis = event.jaxis.axis;
   421                         binding.value.axis.axis_min = StandardizeAxisValue(pAxisState->m_nStartingValue);
   422                         binding.value.axis.axis_max = StandardizeAxisValue(pAxisState->m_nFarthestValue);
   423                         ConfigureBinding(&binding);
   424                     }
   425                 }
   426                 break;
   427             case SDL_JOYHATMOTION:
   428                 if (event.jhat.which == nJoystickID) {
   429                     if (event.jhat.value != SDL_HAT_CENTERED) {
   430                         SDL_GameControllerExtendedBind binding;
   431                         SDL_zero(binding);
   432                         binding.bindType = SDL_CONTROLLER_BINDTYPE_HAT;
   433                         binding.value.hat.hat = event.jhat.hat;
   434                         binding.value.hat.hat_mask = event.jhat.value;
   435                         ConfigureBinding(&binding);
   436                     }
   437                 }
   438                 break;
   439             case SDL_JOYBALLMOTION:
   440                 break;
   441             case SDL_JOYBUTTONDOWN:
   442                 if (event.jbutton.which == nJoystickID) {
   443                     SDL_GameControllerExtendedBind binding;
   444                     SDL_zero(binding);
   445                     binding.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
   446                     binding.value.button = event.jbutton.button;
   447                     ConfigureBinding(&binding);
   448                 }
   449                 break;
   450             case SDL_FINGERDOWN:
   451             case SDL_MOUSEBUTTONDOWN:
   452                 /* Skip this step */
   453                 SetCurrentBinding(s_iCurrentBinding + 1);
   454                 break;
   455             case SDL_KEYDOWN:
   456                 if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
   457                     SetCurrentBinding(s_iCurrentBinding - 1);
   458                     break;
   459                 }
   460                 if (event.key.keysym.sym == SDLK_SPACE) {
   461                     SetCurrentBinding(s_iCurrentBinding + 1);
   462                     break;
   463                 }
   464 
   465                 if ((event.key.keysym.sym != SDLK_ESCAPE)) {
   466                     break;
   467                 }
   468                 /* Fall through to signal quit */
   469             case SDL_QUIT:
   470                 done = SDL_TRUE;
   471                 break;
   472             default:
   473                 break;
   474             }
   475         }
   476 
   477         SDL_Delay(15);
   478 
   479         /* Wait 100 ms for joystick events to stop coming in,
   480            in case a controller sends multiple events for a single control (e.g. axis and button for trigger)
   481         */
   482         if (s_unPendingAdvanceTime && SDL_GetTicks() - s_unPendingAdvanceTime >= 100) {
   483             SetCurrentBinding(s_iCurrentBinding + 1);
   484         }
   485     }
   486 
   487     if (s_bBindingComplete) {
   488         char mapping[1024];
   489         char trimmed_name[128];
   490         char *spot;
   491         int iIndex;
   492         char pszElement[12];
   493 
   494         SDL_strlcpy(trimmed_name, name, SDL_arraysize(trimmed_name));
   495         while (SDL_isspace(trimmed_name[0])) {
   496             SDL_memmove(&trimmed_name[0], &trimmed_name[1], SDL_strlen(trimmed_name));
   497         }
   498         while (trimmed_name[0] && SDL_isspace(trimmed_name[SDL_strlen(trimmed_name) - 1])) {
   499             trimmed_name[SDL_strlen(trimmed_name) - 1] = '\0';
   500         }
   501         while ((spot = SDL_strchr(trimmed_name, ',')) != NULL) {
   502             SDL_memmove(spot, spot + 1, SDL_strlen(spot));
   503         }
   504 
   505         /* Initialize mapping with GUID and name */
   506         SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), mapping, SDL_arraysize(mapping));
   507         SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   508         SDL_strlcat(mapping, trimmed_name, SDL_arraysize(mapping));
   509         SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   510         SDL_strlcat(mapping, "platform:", SDL_arraysize(mapping));
   511         SDL_strlcat(mapping, SDL_GetPlatform(), SDL_arraysize(mapping));
   512         SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   513 
   514         for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
   515             SDL_GameControllerExtendedBind *pBinding = &s_arrBindings[iIndex];
   516             if (pBinding->bindType == SDL_CONTROLLER_BINDTYPE_NONE) {
   517                 continue;
   518             }
   519 
   520             if (iIndex < SDL_CONTROLLER_BUTTON_MAX) {
   521                 SDL_GameControllerButton eButton = (SDL_GameControllerButton)iIndex;
   522                 SDL_strlcat(mapping, SDL_GameControllerGetStringForButton(eButton), SDL_arraysize(mapping));
   523             } else {
   524                 const char *pszAxisName;
   525                 switch (iIndex - SDL_CONTROLLER_BUTTON_MAX) {
   526                 case SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE:
   527                     if (!BMergeAxisBindings(iIndex)) {
   528                         SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
   529                     }
   530                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTX);
   531                     break;
   532                 case SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE:
   533                     SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
   534                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTX);
   535                     break;
   536                 case SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE:
   537                     if (!BMergeAxisBindings(iIndex)) {
   538                         SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
   539                     }
   540                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTY);
   541                     break;
   542                 case SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE:
   543                     SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
   544                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTY);
   545                     break;
   546                 case SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE:
   547                     if (!BMergeAxisBindings(iIndex)) {
   548                         SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
   549                     }
   550                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTX);
   551                     break;
   552                 case SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE:
   553                     SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
   554                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTX);
   555                     break;
   556                 case SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE:
   557                     if (!BMergeAxisBindings(iIndex)) {
   558                         SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
   559                     }
   560                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTY);
   561                     break;
   562                 case SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE:
   563                     SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
   564                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTY);
   565                     break;
   566                 case SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT:
   567                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_TRIGGERLEFT);
   568                     break;
   569                 case SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT:
   570                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
   571                     break;
   572                 }
   573                 SDL_strlcat(mapping, pszAxisName, SDL_arraysize(mapping));
   574             }
   575             SDL_strlcat(mapping, ":", SDL_arraysize(mapping));
   576 
   577             pszElement[0] = '\0';
   578             switch (pBinding->bindType) {
   579             case SDL_CONTROLLER_BINDTYPE_BUTTON:
   580                 SDL_snprintf(pszElement, sizeof(pszElement), "b%d", pBinding->value.button);
   581                 break;
   582             case SDL_CONTROLLER_BINDTYPE_AXIS:
   583                 if (pBinding->value.axis.axis_min == 0 && pBinding->value.axis.axis_max == SDL_JOYSTICK_AXIS_MIN) {
   584                     /* The negative half axis */
   585                     SDL_snprintf(pszElement, sizeof(pszElement), "-a%d", pBinding->value.axis.axis);
   586                 } else if (pBinding->value.axis.axis_min == 0 && pBinding->value.axis.axis_max == SDL_JOYSTICK_AXIS_MAX) {
   587                     /* The positive half axis */
   588                     SDL_snprintf(pszElement, sizeof(pszElement), "+a%d", pBinding->value.axis.axis);
   589                 } else {
   590                     SDL_snprintf(pszElement, sizeof(pszElement), "a%d", pBinding->value.axis.axis);
   591                     if (pBinding->value.axis.axis_min > pBinding->value.axis.axis_max) {
   592                         /* Invert the axis */
   593                         SDL_strlcat(pszElement, "~", SDL_arraysize(pszElement));
   594                     }
   595                 }
   596                 break;
   597             case SDL_CONTROLLER_BINDTYPE_HAT:
   598                 SDL_snprintf(pszElement, sizeof(pszElement), "h%d.%d", pBinding->value.hat.hat, pBinding->value.hat.hat_mask);
   599                 break;
   600             default:
   601                 SDL_assert(!"Unknown bind type");
   602                 break;
   603             }
   604             SDL_strlcat(mapping, pszElement, SDL_arraysize(mapping));
   605             SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   606         }
   607 
   608         SDL_Log("Mapping:\n\n%s\n\n", mapping);
   609         /* Print to stdout as well so the user can cat the output somewhere */
   610         printf("%s\n", mapping);
   611     }
   612 
   613     SDL_free(s_arrAxisState);
   614     s_arrAxisState = NULL;
   615     
   616     SDL_DestroyRenderer(screen);
   617     SDL_DestroyWindow(window);
   618 }
   619 
   620 int
   621 main(int argc, char *argv[])
   622 {
   623     const char *name;
   624     int i;
   625     SDL_Joystick *joystick;
   626 
   627     /* Enable standard application logging */
   628     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   629 
   630     /* Initialize SDL (Note: video is required to start event loop) */
   631     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   632         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   633         exit(1);
   634     }
   635 
   636     /* Print information about the joysticks */
   637     SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
   638     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   639         name = SDL_JoystickNameForIndex(i);
   640         SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   641         joystick = SDL_JoystickOpen(i);
   642         if (joystick == NULL) {
   643             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
   644                     SDL_GetError());
   645         } else {
   646             char guid[64];
   647             SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
   648                                       guid, sizeof (guid));
   649             SDL_Log("       axes: %d\n", SDL_JoystickNumAxes(joystick));
   650             SDL_Log("      balls: %d\n", SDL_JoystickNumBalls(joystick));
   651             SDL_Log("       hats: %d\n", SDL_JoystickNumHats(joystick));
   652             SDL_Log("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
   653             SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
   654             SDL_Log("       guid: %s\n", guid);
   655             SDL_Log("    VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick));
   656             SDL_JoystickClose(joystick);
   657         }
   658     }
   659 
   660 #ifdef __ANDROID__
   661     if (SDL_NumJoysticks() > 0) {
   662 #else
   663     if (argv[1]) {
   664 #endif
   665         int device;
   666 #ifdef __ANDROID__
   667         device = 0;
   668 #else
   669         device = atoi(argv[1]);
   670 #endif
   671         joystick = SDL_JoystickOpen(device);
   672         if (joystick == NULL) {
   673             SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
   674         } else {
   675             WatchJoystick(joystick);
   676             SDL_JoystickClose(joystick);
   677         }
   678     }
   679     else {
   680         SDL_Log("\n\nUsage: ./controllermap number\nFor example: ./controllermap 0\nOr: ./controllermap 0 >> gamecontrollerdb.txt");
   681     }
   682     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   683 
   684     return 0;
   685 }
   686 
   687 #else
   688 
   689 int
   690 main(int argc, char *argv[])
   691 {
   692     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
   693     exit(1);
   694 }
   695 
   696 #endif
   697 
   698 /* vi: set ts=4 sw=4 expandtab: */