test/controllermap.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 04 Jan 2017 10:28:07 -0800
changeset 10752 c9d3ff50dfcf
parent 10747 da1f4eeac65a
child 10824 e854440318d2
permissions -rw-r--r--
Added SDL_JoystickGetAxisInitialState() to get a joystick axis' initial value.
This is useful for controller mapping programs to determine an axis' zero state
     1 /*
     2   Copyright (C) 1997-2017 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     int iIndex;
   201     SDL_GameControllerExtendedBind *pBinding;
   202 
   203     if (iBinding < 0) {
   204         return;
   205     }
   206 
   207     if (iBinding == BINDING_COUNT) {
   208         s_bBindingComplete = SDL_TRUE;
   209         return;
   210     }
   211 
   212     s_iCurrentBinding = iBinding;
   213 
   214     pBinding = &s_arrBindings[s_arrBindingOrder[s_iCurrentBinding]];
   215     SDL_zerop(pBinding);
   216 
   217     for (iIndex = 0; iIndex < s_nNumAxes; ++iIndex) {
   218         s_arrAxisState[iIndex].m_nFarthestValue = s_arrAxisState[iIndex].m_nStartingValue;
   219     }
   220 
   221     s_unPendingAdvanceTime = 0;
   222 }
   223 
   224 static SDL_bool
   225 BBindingContainsBinding(const SDL_GameControllerExtendedBind *pBindingA, const SDL_GameControllerExtendedBind *pBindingB)
   226 {
   227     if (pBindingA->bindType != pBindingB->bindType)
   228     {
   229         return SDL_FALSE;
   230     }
   231     switch (pBindingA->bindType)
   232     {
   233     case SDL_CONTROLLER_BINDTYPE_AXIS:
   234         if (pBindingA->value.axis.axis != pBindingB->value.axis.axis) {
   235             return SDL_FALSE;
   236         }
   237         {
   238             int minA = SDL_min(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
   239             int maxA = SDL_max(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
   240             int minB = SDL_min(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
   241             int maxB = SDL_max(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
   242             return (minA <= minB && maxA >= maxB);
   243         }
   244         /* Not reached */
   245     default:
   246         return SDL_memcmp(pBindingA, pBindingB, sizeof(*pBindingA)) == 0;
   247     }
   248 }
   249 
   250 static void
   251 ConfigureBinding(const SDL_GameControllerExtendedBind *pBinding)
   252 {
   253     SDL_GameControllerExtendedBind *pCurrent;
   254     int iIndex;
   255     int iCurrentElement = s_arrBindingOrder[s_iCurrentBinding];
   256 
   257     /* Do we already have this binding? */
   258     for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
   259         pCurrent = &s_arrBindings[iIndex];
   260         if (BBindingContainsBinding(pCurrent, pBinding)) {
   261             if (iIndex == SDL_CONTROLLER_BUTTON_A && iCurrentElement != SDL_CONTROLLER_BUTTON_B) {
   262                 /* Skip to the next binding */
   263                 SetCurrentBinding(s_iCurrentBinding + 1);
   264                 return;
   265             }
   266 
   267             if (iIndex == SDL_CONTROLLER_BUTTON_B) {
   268                 /* Go back to the previous binding */
   269                 SetCurrentBinding(s_iCurrentBinding - 1);
   270                 return;
   271             }
   272 
   273             /* Already have this binding, ignore it */
   274             return;
   275         }
   276     }
   277 
   278     /* Should the new binding override the existing one? */
   279     pCurrent = &s_arrBindings[iCurrentElement];
   280     if (pCurrent->bindType != SDL_CONTROLLER_BINDTYPE_NONE) {
   281         SDL_bool bNativeDPad, bCurrentDPad;
   282         SDL_bool bNativeAxis, bCurrentAxis;
   283         
   284         bNativeDPad = (iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_UP ||
   285                        iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_DOWN ||
   286                        iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_LEFT ||
   287                        iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
   288         bCurrentDPad = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_HAT);
   289         if (bNativeDPad == bCurrentDPad) {
   290             /* We already have a binding of the type we want, ignore the new one */
   291             return;
   292         }
   293 
   294         bNativeAxis = (iCurrentElement >= SDL_CONTROLLER_BUTTON_MAX);
   295         bCurrentAxis = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_AXIS);
   296         if (bNativeAxis == bCurrentAxis &&
   297             (pBinding->bindType != SDL_CONTROLLER_BINDTYPE_AXIS ||
   298              pBinding->value.axis.axis != pCurrent->value.axis.axis)) {
   299             /* We already have a binding of the type we want, ignore the new one */
   300             return;
   301         }
   302     }
   303 
   304     *pCurrent = *pBinding;
   305 
   306     s_unPendingAdvanceTime = SDL_GetTicks();
   307 }
   308 
   309 static SDL_bool
   310 BMergeAxisBindings(int iIndex)
   311 {
   312     SDL_GameControllerExtendedBind *pBindingA = &s_arrBindings[iIndex];
   313     SDL_GameControllerExtendedBind *pBindingB = &s_arrBindings[iIndex+1];
   314     if (pBindingA->bindType == SDL_CONTROLLER_BINDTYPE_AXIS &&
   315         pBindingB->bindType == SDL_CONTROLLER_BINDTYPE_AXIS &&
   316         pBindingA->value.axis.axis == pBindingB->value.axis.axis) {
   317         if (pBindingA->value.axis.axis_min == pBindingB->value.axis.axis_min) {
   318             pBindingA->value.axis.axis_min = pBindingA->value.axis.axis_max;
   319             pBindingA->value.axis.axis_max = pBindingB->value.axis.axis_max;
   320             pBindingB->bindType = SDL_CONTROLLER_BINDTYPE_NONE;
   321             return SDL_TRUE;
   322         }
   323     }
   324     return SDL_FALSE;
   325 }
   326 
   327 static void
   328 WatchJoystick(SDL_Joystick * joystick)
   329 {
   330     SDL_Window *window = NULL;
   331     SDL_Renderer *screen = NULL;
   332     SDL_Texture *background, *button, *axis, *marker;
   333     const char *name = NULL;
   334     SDL_bool done = SDL_FALSE;
   335     SDL_Event event;
   336     SDL_Rect dst;
   337     Uint8 alpha=200, alpha_step = -1;
   338     Uint32 alpha_ticks = 0;
   339     SDL_JoystickID nJoystickID;
   340     int iIndex;
   341 
   342     /* Create a window to display joystick axis position */
   343     window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
   344                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
   345                               SCREEN_HEIGHT, 0);
   346     if (window == NULL) {
   347         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
   348         return;
   349     }
   350 
   351     screen = SDL_CreateRenderer(window, -1, 0);
   352     if (screen == NULL) {
   353         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
   354         SDL_DestroyWindow(window);
   355         return;
   356     }
   357     
   358     background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
   359     button = LoadTexture(screen, "button.bmp", SDL_TRUE);
   360     axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
   361     SDL_RaiseWindow(window);
   362 
   363     /* scale for platforms that don't give you the window size you asked for. */
   364     SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
   365 
   366     /* Print info about the joystick we are watching */
   367     name = SDL_JoystickName(joystick);
   368     SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
   369            name ? name : "Unknown Joystick");
   370     SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
   371            SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
   372            SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
   373     
   374     SDL_Log("\n\n\
   375     ====================================================================================\n\
   376     Press the buttons on your controller when indicated\n\
   377     (Your controller may look different than the picture)\n\
   378     If you want to correct a mistake, press backspace or the back button on your device\n\
   379     To skip a button, press SPACE or click/touch the screen\n\
   380     To exit, press ESC\n\
   381     ====================================================================================\n");
   382 
   383     nJoystickID = SDL_JoystickInstanceID(joystick);
   384 
   385     s_nNumAxes = SDL_JoystickNumAxes(joystick);
   386     s_arrAxisState = SDL_calloc(s_nNumAxes, sizeof(*s_arrAxisState));
   387     for (iIndex = 0; iIndex < s_nNumAxes; ++iIndex) {
   388         AxisState *pAxisState = &s_arrAxisState[iIndex];
   389         Sint16 nInitialValue;
   390         pAxisState->m_bMoving = SDL_JoystickGetAxisInitialState(joystick, iIndex, &nInitialValue);
   391         pAxisState->m_nStartingValue = nInitialValue;
   392         pAxisState->m_nFarthestValue = nInitialValue;
   393     }
   394 
   395     /* Loop, getting joystick events! */
   396     while (!done && !s_bBindingComplete) {
   397         int iElement = s_arrBindingOrder[s_iCurrentBinding];
   398 
   399         switch (s_arrBindingDisplay[iElement].marker) {
   400             case MARKER_AXIS:
   401                 marker = axis;
   402                 break;
   403             case MARKER_BUTTON:
   404                 marker = button;
   405                 break;
   406             default:
   407                 break;
   408         }
   409         
   410         dst.x = s_arrBindingDisplay[iElement].x;
   411         dst.y = s_arrBindingDisplay[iElement].y;
   412         SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h);
   413 
   414         if (SDL_GetTicks() - alpha_ticks > 5) {
   415             alpha_ticks = SDL_GetTicks();
   416             alpha += alpha_step;
   417             if (alpha == 255) {
   418                 alpha_step = -1;
   419             }
   420             if (alpha < 128) {
   421                 alpha_step = 1;
   422             }
   423         }
   424 
   425         SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
   426         SDL_RenderClear(screen);
   427         SDL_RenderCopy(screen, background, NULL, NULL);
   428         SDL_SetTextureAlphaMod(marker, alpha);
   429         SDL_SetTextureColorMod(marker, 10, 255, 21);
   430         SDL_RenderCopyEx(screen, marker, NULL, &dst, s_arrBindingDisplay[iElement].angle, NULL, SDL_FLIP_NONE);
   431         SDL_RenderPresent(screen);
   432             
   433         while (SDL_PollEvent(&event) > 0) {
   434             switch (event.type) {
   435             case SDL_JOYDEVICEREMOVED:
   436                 if (event.jaxis.which == nJoystickID) {
   437                     done = SDL_TRUE;
   438                 }
   439                 break;
   440             case SDL_JOYAXISMOTION:
   441                 if (event.jaxis.which == nJoystickID) {
   442                     AxisState *pAxisState = &s_arrAxisState[event.jaxis.axis];
   443                     int nValue = event.jaxis.value;
   444                     int nCurrentDistance, nFarthestDistance;
   445                     if (!pAxisState->m_bMoving) {
   446                         pAxisState->m_bMoving = SDL_TRUE;
   447                         pAxisState->m_nStartingValue = nValue;
   448                         pAxisState->m_nFarthestValue = nValue;
   449                     }
   450                     nCurrentDistance = SDL_abs(nValue - pAxisState->m_nStartingValue);
   451                     nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);
   452                     if (nCurrentDistance > nFarthestDistance) {
   453                         pAxisState->m_nFarthestValue = nValue;
   454                     }
   455                     if (nFarthestDistance >= 16000 && nCurrentDistance <= 10000) {
   456                         /* We've gone out far enough and started to come back, let's bind this axis */
   457                         SDL_GameControllerExtendedBind binding;
   458                         SDL_zero(binding);
   459                         binding.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
   460                         binding.value.axis.axis = event.jaxis.axis;
   461                         binding.value.axis.axis_min = StandardizeAxisValue(pAxisState->m_nStartingValue);
   462                         binding.value.axis.axis_max = StandardizeAxisValue(pAxisState->m_nFarthestValue);
   463                         ConfigureBinding(&binding);
   464                     }
   465                 }
   466                 break;
   467             case SDL_JOYHATMOTION:
   468                 if (event.jhat.which == nJoystickID) {
   469                     if (event.jhat.value != SDL_HAT_CENTERED) {
   470                         SDL_GameControllerExtendedBind binding;
   471                         SDL_zero(binding);
   472                         binding.bindType = SDL_CONTROLLER_BINDTYPE_HAT;
   473                         binding.value.hat.hat = event.jhat.hat;
   474                         binding.value.hat.hat_mask = event.jhat.value;
   475                         ConfigureBinding(&binding);
   476                     }
   477                 }
   478                 break;
   479             case SDL_JOYBALLMOTION:
   480                 break;
   481             case SDL_JOYBUTTONDOWN:
   482                 if (event.jbutton.which == nJoystickID) {
   483                     SDL_GameControllerExtendedBind binding;
   484                     SDL_zero(binding);
   485                     binding.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
   486                     binding.value.button = event.jbutton.button;
   487                     ConfigureBinding(&binding);
   488                 }
   489                 break;
   490             case SDL_FINGERDOWN:
   491             case SDL_MOUSEBUTTONDOWN:
   492                 /* Skip this step */
   493                 SetCurrentBinding(s_iCurrentBinding + 1);
   494                 break;
   495             case SDL_KEYDOWN:
   496                 if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
   497                     SetCurrentBinding(s_iCurrentBinding - 1);
   498                     break;
   499                 }
   500                 if (event.key.keysym.sym == SDLK_SPACE) {
   501                     SetCurrentBinding(s_iCurrentBinding + 1);
   502                     break;
   503                 }
   504 
   505                 if ((event.key.keysym.sym != SDLK_ESCAPE)) {
   506                     break;
   507                 }
   508                 /* Fall through to signal quit */
   509             case SDL_QUIT:
   510                 done = SDL_TRUE;
   511                 break;
   512             default:
   513                 break;
   514             }
   515         }
   516 
   517         SDL_Delay(15);
   518 
   519         /* Wait 100 ms for joystick events to stop coming in,
   520            in case a controller sends multiple events for a single control (e.g. axis and button for trigger)
   521         */
   522         if (s_unPendingAdvanceTime && SDL_GetTicks() - s_unPendingAdvanceTime >= 100) {
   523             SetCurrentBinding(s_iCurrentBinding + 1);
   524         }
   525     }
   526 
   527     if (s_bBindingComplete) {
   528         char mapping[1024];
   529         char trimmed_name[128];
   530         char *spot;
   531         int iIndex;
   532         char pszElement[12];
   533 
   534         SDL_strlcpy(trimmed_name, name, SDL_arraysize(trimmed_name));
   535         while (SDL_isspace(trimmed_name[0])) {
   536             SDL_memmove(&trimmed_name[0], &trimmed_name[1], SDL_strlen(trimmed_name));
   537         }
   538         while (trimmed_name[0] && SDL_isspace(trimmed_name[SDL_strlen(trimmed_name) - 1])) {
   539             trimmed_name[SDL_strlen(trimmed_name) - 1] = '\0';
   540         }
   541         while ((spot = SDL_strchr(trimmed_name, ',')) != NULL) {
   542             SDL_memmove(spot, spot + 1, SDL_strlen(spot));
   543         }
   544 
   545         /* Initialize mapping with GUID and name */
   546         SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), mapping, SDL_arraysize(mapping));
   547         SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   548         SDL_strlcat(mapping, trimmed_name, SDL_arraysize(mapping));
   549         SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   550         SDL_strlcat(mapping, "platform:", SDL_arraysize(mapping));
   551         SDL_strlcat(mapping, SDL_GetPlatform(), SDL_arraysize(mapping));
   552         SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   553 
   554         for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
   555             SDL_GameControllerExtendedBind *pBinding = &s_arrBindings[iIndex];
   556             if (pBinding->bindType == SDL_CONTROLLER_BINDTYPE_NONE) {
   557                 continue;
   558             }
   559 
   560             if (iIndex < SDL_CONTROLLER_BUTTON_MAX) {
   561                 SDL_GameControllerButton eButton = (SDL_GameControllerButton)iIndex;
   562                 SDL_strlcat(mapping, SDL_GameControllerGetStringForButton(eButton), SDL_arraysize(mapping));
   563             } else {
   564                 const char *pszAxisName;
   565                 switch (iIndex - SDL_CONTROLLER_BUTTON_MAX) {
   566                 case SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE:
   567                     if (!BMergeAxisBindings(iIndex)) {
   568                         SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
   569                     }
   570                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTX);
   571                     break;
   572                 case SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE:
   573                     SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
   574                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTX);
   575                     break;
   576                 case SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE:
   577                     if (!BMergeAxisBindings(iIndex)) {
   578                         SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
   579                     }
   580                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTY);
   581                     break;
   582                 case SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE:
   583                     SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
   584                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTY);
   585                     break;
   586                 case SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE:
   587                     if (!BMergeAxisBindings(iIndex)) {
   588                         SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
   589                     }
   590                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTX);
   591                     break;
   592                 case SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE:
   593                     SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
   594                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTX);
   595                     break;
   596                 case SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE:
   597                     if (!BMergeAxisBindings(iIndex)) {
   598                         SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
   599                     }
   600                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTY);
   601                     break;
   602                 case SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE:
   603                     SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
   604                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTY);
   605                     break;
   606                 case SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT:
   607                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_TRIGGERLEFT);
   608                     break;
   609                 case SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT:
   610                     pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
   611                     break;
   612                 }
   613                 SDL_strlcat(mapping, pszAxisName, SDL_arraysize(mapping));
   614             }
   615             SDL_strlcat(mapping, ":", SDL_arraysize(mapping));
   616 
   617             pszElement[0] = '\0';
   618             switch (pBinding->bindType) {
   619             case SDL_CONTROLLER_BINDTYPE_BUTTON:
   620                 SDL_snprintf(pszElement, sizeof(pszElement), "b%d", pBinding->value.button);
   621                 break;
   622             case SDL_CONTROLLER_BINDTYPE_AXIS:
   623                 if (pBinding->value.axis.axis_min == 0 && pBinding->value.axis.axis_max == SDL_JOYSTICK_AXIS_MIN) {
   624                     /* The negative half axis */
   625                     SDL_snprintf(pszElement, sizeof(pszElement), "-a%d", pBinding->value.axis.axis);
   626                 } else if (pBinding->value.axis.axis_min == 0 && pBinding->value.axis.axis_max == SDL_JOYSTICK_AXIS_MAX) {
   627                     /* The positive half axis */
   628                     SDL_snprintf(pszElement, sizeof(pszElement), "+a%d", pBinding->value.axis.axis);
   629                 } else {
   630                     SDL_snprintf(pszElement, sizeof(pszElement), "a%d", pBinding->value.axis.axis);
   631                     if (pBinding->value.axis.axis_min > pBinding->value.axis.axis_max) {
   632                         /* Invert the axis */
   633                         SDL_strlcat(pszElement, "~", SDL_arraysize(pszElement));
   634                     }
   635                 }
   636                 break;
   637             case SDL_CONTROLLER_BINDTYPE_HAT:
   638                 SDL_snprintf(pszElement, sizeof(pszElement), "h%d.%d", pBinding->value.hat.hat, pBinding->value.hat.hat_mask);
   639                 break;
   640             default:
   641                 SDL_assert(!"Unknown bind type");
   642                 break;
   643             }
   644             SDL_strlcat(mapping, pszElement, SDL_arraysize(mapping));
   645             SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   646         }
   647 
   648         SDL_Log("Mapping:\n\n%s\n\n", mapping);
   649         /* Print to stdout as well so the user can cat the output somewhere */
   650         printf("%s\n", mapping);
   651     }
   652 
   653     SDL_free(s_arrAxisState);
   654     s_arrAxisState = NULL;
   655     
   656     SDL_DestroyRenderer(screen);
   657     SDL_DestroyWindow(window);
   658 }
   659 
   660 int
   661 main(int argc, char *argv[])
   662 {
   663     const char *name;
   664     int i;
   665     SDL_Joystick *joystick;
   666 
   667     /* Enable standard application logging */
   668     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   669 
   670     /* Initialize SDL (Note: video is required to start event loop) */
   671     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   672         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   673         exit(1);
   674     }
   675 
   676     /* Print information about the joysticks */
   677     SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
   678     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   679         name = SDL_JoystickNameForIndex(i);
   680         SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   681         joystick = SDL_JoystickOpen(i);
   682         if (joystick == NULL) {
   683             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
   684                     SDL_GetError());
   685         } else {
   686             char guid[64];
   687             SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
   688                                       guid, sizeof (guid));
   689             SDL_Log("       axes: %d\n", SDL_JoystickNumAxes(joystick));
   690             SDL_Log("      balls: %d\n", SDL_JoystickNumBalls(joystick));
   691             SDL_Log("       hats: %d\n", SDL_JoystickNumHats(joystick));
   692             SDL_Log("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
   693             SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
   694             SDL_Log("       guid: %s\n", guid);
   695             SDL_Log("    VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick));
   696             SDL_JoystickClose(joystick);
   697         }
   698     }
   699 
   700 #ifdef __ANDROID__
   701     if (SDL_NumJoysticks() > 0) {
   702 #else
   703     if (argv[1]) {
   704 #endif
   705         int device;
   706 #ifdef __ANDROID__
   707         device = 0;
   708 #else
   709         device = atoi(argv[1]);
   710 #endif
   711         joystick = SDL_JoystickOpen(device);
   712         if (joystick == NULL) {
   713             SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
   714         } else {
   715             WatchJoystick(joystick);
   716             SDL_JoystickClose(joystick);
   717         }
   718     }
   719     else {
   720         SDL_Log("\n\nUsage: ./controllermap number\nFor example: ./controllermap 0\nOr: ./controllermap 0 >> gamecontrollerdb.txt");
   721     }
   722     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   723 
   724     return 0;
   725 }
   726 
   727 #else
   728 
   729 int
   730 main(int argc, char *argv[])
   731 {
   732     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
   733     exit(1);
   734 }
   735 
   736 #endif
   737 
   738 /* vi: set ts=4 sw=4 expandtab: */