Fixed handling joysticks that send multiple events for a single control, e.g. both a button and axis event for a trigger.
authorSam Lantinga <slouken@libsdl.org>
Thu, 15 Dec 2016 14:27:22 -0800
changeset 10705997239dcb197
parent 10704 485f1ea7f8a4
child 10706 71ce5d7d2610
Fixed handling joysticks that send multiple events for a single control, e.g. both a button and axis event for a trigger.
Tested with the 8Bitdo NES30 Pro on Linux
test/controllermap.c
     1.1 --- a/test/controllermap.c	Wed Dec 14 06:25:09 2016 -0800
     1.2 +++ b/test/controllermap.c	Thu Dec 15 14:27:22 2016 -0800
     1.3 @@ -32,16 +32,66 @@
     1.4  #define MARKER_BUTTON 1
     1.5  #define MARKER_AXIS 2
     1.6  
     1.7 -typedef struct MappingStep
     1.8 +#define BINDING_COUNT (SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_AXIS_MAX)
     1.9 +
    1.10 +static struct 
    1.11  {
    1.12      int x, y;
    1.13      double angle;
    1.14      int marker;
    1.15 -    char *field;
    1.16 -    int axis, button, hat, hat_value;
    1.17 -    char mapping[4096];
    1.18 -}MappingStep;
    1.19  
    1.20 +} s_arrBindingDisplay[BINDING_COUNT] = {
    1.21 +    { 387, 167, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_A */
    1.22 +    { 431, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_B */
    1.23 +    { 342, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_X */
    1.24 +    { 389, 101, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_Y */
    1.25 +    { 174, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_BACK */
    1.26 +    { 233, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_GUIDE */
    1.27 +    { 289, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_START */
    1.28 +    {  75, 154, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_LEFTSTICK */
    1.29 +    { 305, 230, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_RIGHTSTICK */
    1.30 +    {  77,  40, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_LEFTSHOULDER */
    1.31 +    { 396,  36, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_RIGHTSHOULDER */
    1.32 +    { 154, 188, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_UP */
    1.33 +    { 154, 249, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_DOWN */
    1.34 +    { 116, 217, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_LEFT */
    1.35 +    { 186, 217, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_RIGHT */
    1.36 +    {  75, 154, 0.0,   MARKER_AXIS }, /* SDL_CONTROLLER_AXIS_LEFTX */
    1.37 +    {  75, 154, 90.0,  MARKER_AXIS }, /* SDL_CONTROLLER_AXIS_LEFTY */
    1.38 +    { 305, 230, 0.0,   MARKER_AXIS }, /* SDL_CONTROLLER_AXIS_RIGHTX */
    1.39 +    { 305, 230, 90.0,  MARKER_AXIS }, /* SDL_CONTROLLER_AXIS_RIGHTY */
    1.40 +    {  91,   0, 90.0,  MARKER_AXIS }, /* SDL_CONTROLLER_AXIS_TRIGGERLEFT */
    1.41 +    { 375,   0, 90.0,  MARKER_AXIS }, /* SDL_CONTROLLER_AXIS_TRIGGERRIGHT */
    1.42 +};
    1.43 +
    1.44 +static int s_arrBindingOrder[BINDING_COUNT] = {
    1.45 +    SDL_CONTROLLER_BUTTON_A,
    1.46 +    SDL_CONTROLLER_BUTTON_B,
    1.47 +    SDL_CONTROLLER_BUTTON_Y,
    1.48 +    SDL_CONTROLLER_BUTTON_X,
    1.49 +    SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_AXIS_LEFTX,
    1.50 +    SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_AXIS_LEFTY,
    1.51 +    SDL_CONTROLLER_BUTTON_LEFTSTICK,
    1.52 +    SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_AXIS_RIGHTX,
    1.53 +    SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_AXIS_RIGHTY,
    1.54 +    SDL_CONTROLLER_BUTTON_RIGHTSTICK,
    1.55 +    SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
    1.56 +    SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_AXIS_TRIGGERLEFT,
    1.57 +    SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
    1.58 +    SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
    1.59 +    SDL_CONTROLLER_BUTTON_DPAD_UP,
    1.60 +    SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
    1.61 +    SDL_CONTROLLER_BUTTON_DPAD_DOWN,
    1.62 +    SDL_CONTROLLER_BUTTON_DPAD_LEFT,
    1.63 +    SDL_CONTROLLER_BUTTON_BACK,
    1.64 +    SDL_CONTROLLER_BUTTON_GUIDE,
    1.65 +    SDL_CONTROLLER_BUTTON_START,
    1.66 +};
    1.67 +
    1.68 +static SDL_GameControllerButtonBind s_arrBindings[BINDING_COUNT];
    1.69 +static int s_iCurrentBinding;
    1.70 +static Uint32 s_unPendingAdvanceTime;
    1.71 +static SDL_bool s_bBindingComplete;
    1.72  
    1.73  SDL_Texture *
    1.74  LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
    1.75 @@ -93,45 +143,99 @@
    1.76      return texture;
    1.77  }
    1.78  
    1.79 -static SDL_bool
    1.80 +void SetCurrentBinding(int iBinding)
    1.81 +{
    1.82 +    SDL_GameControllerButtonBind *pBinding;
    1.83 +
    1.84 +    if (iBinding < 0) {
    1.85 +        return;
    1.86 +    }
    1.87 +
    1.88 +    if (iBinding == BINDING_COUNT) {
    1.89 +        s_bBindingComplete = SDL_TRUE;
    1.90 +        return;
    1.91 +    }
    1.92 +
    1.93 +    s_iCurrentBinding = iBinding;
    1.94 +
    1.95 +    pBinding = &s_arrBindings[s_arrBindingOrder[s_iCurrentBinding]];
    1.96 +    SDL_zerop(pBinding);
    1.97 +
    1.98 +    s_unPendingAdvanceTime = 0;
    1.99 +}
   1.100 +
   1.101 +
   1.102 +static void
   1.103 +ConfigureBinding(const SDL_GameControllerButtonBind *pBinding)
   1.104 +{
   1.105 +    SDL_GameControllerButtonBind *pCurrent;
   1.106 +    int iIndex;
   1.107 +    int iCurrentElement = s_arrBindingOrder[s_iCurrentBinding];
   1.108 +
   1.109 +    /* Do we already have this binding? */
   1.110 +    for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
   1.111 +        if (SDL_memcmp(pBinding, &s_arrBindings[iIndex], sizeof(*pBinding)) == 0) {
   1.112 +            if (iIndex == SDL_CONTROLLER_BUTTON_A && iCurrentElement != SDL_CONTROLLER_BUTTON_B) {
   1.113 +                /* Skip to the next binding */
   1.114 +                SetCurrentBinding(s_iCurrentBinding + 1);
   1.115 +                return;
   1.116 +            }
   1.117 +
   1.118 +            if (iIndex == SDL_CONTROLLER_BUTTON_B) {
   1.119 +                /* Go back to the previous binding */
   1.120 +                SetCurrentBinding(s_iCurrentBinding - 1);
   1.121 +                return;
   1.122 +            }
   1.123 +
   1.124 +            /* Already have this binding, ignore it */
   1.125 +            return;
   1.126 +        }
   1.127 +    }
   1.128 +
   1.129 +    /* Should the new binding override the existing one? */
   1.130 +    pCurrent = &s_arrBindings[iCurrentElement];
   1.131 +    if (pCurrent->bindType != SDL_CONTROLLER_BINDTYPE_NONE) {
   1.132 +        SDL_bool bNativeDPad, bCurrentDPad;
   1.133 +        SDL_bool bNativeAxis, bCurrentAxis;
   1.134 +        
   1.135 +        bNativeDPad = (iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_UP ||
   1.136 +                       iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_DOWN ||
   1.137 +                       iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_LEFT ||
   1.138 +                       iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
   1.139 +        bCurrentDPad = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_HAT);
   1.140 +        if (bNativeDPad == bCurrentDPad) {
   1.141 +            /* We already have a binding of the type we want, ignore the new one */
   1.142 +            return;
   1.143 +        }
   1.144 +
   1.145 +        bNativeAxis = (iCurrentElement >= SDL_CONTROLLER_BUTTON_MAX);
   1.146 +        bCurrentAxis = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_AXIS);
   1.147 +        if (bNativeAxis == bCurrentAxis) {
   1.148 +            /* We already have a binding of the type we want, ignore the new one */
   1.149 +            return;
   1.150 +        }
   1.151 +    }
   1.152 +
   1.153 +    *pCurrent = *pBinding;
   1.154 +
   1.155 +    s_unPendingAdvanceTime = SDL_GetTicks();
   1.156 +}
   1.157 +
   1.158 +static void
   1.159  WatchJoystick(SDL_Joystick * joystick)
   1.160  {
   1.161      SDL_Window *window = NULL;
   1.162      SDL_Renderer *screen = NULL;
   1.163      SDL_Texture *background, *button, *axis, *marker;
   1.164      const char *name = NULL;
   1.165 -    SDL_bool retval = SDL_FALSE;
   1.166 -    SDL_bool done = SDL_FALSE, next=SDL_FALSE;
   1.167 +    SDL_bool done = SDL_FALSE;
   1.168      SDL_Event event;
   1.169      SDL_Rect dst;
   1.170 -    int s, _s;
   1.171      Uint8 alpha=200, alpha_step = -1;
   1.172      Uint32 alpha_ticks = 0;
   1.173 -    char mapping[4096], temp[4096];
   1.174 -    MappingStep *step, *prev_step;
   1.175 -    MappingStep steps[] = {
   1.176 -        {342, 132,  0.0,  MARKER_BUTTON, "x", -1, -1, -1, -1, ""},
   1.177 -        {387, 167,  0.0,  MARKER_BUTTON, "a", -1, -1, -1, -1, ""},
   1.178 -        {431, 132,  0.0,  MARKER_BUTTON, "b", -1, -1, -1, -1, ""},
   1.179 -        {389, 101,  0.0,  MARKER_BUTTON, "y", -1, -1, -1, -1, ""},
   1.180 -        {174, 132,  0.0,  MARKER_BUTTON, "back", -1, -1, -1, -1, ""},
   1.181 -        {233, 132,  0.0,  MARKER_BUTTON, "guide", -1, -1, -1, -1, ""},
   1.182 -        {289, 132,  0.0,  MARKER_BUTTON, "start", -1, -1, -1, -1, ""},        
   1.183 -        {116, 217,  0.0,  MARKER_BUTTON, "dpleft", -1, -1, -1, -1, ""},
   1.184 -        {154, 249,  0.0,  MARKER_BUTTON, "dpdown", -1, -1, -1, -1, ""},
   1.185 -        {186, 217,  0.0,  MARKER_BUTTON, "dpright", -1, -1, -1, -1, ""},
   1.186 -        {154, 188,  0.0,  MARKER_BUTTON, "dpup", -1, -1, -1, -1, ""},
   1.187 -        {77,  40,   0.0,  MARKER_BUTTON, "leftshoulder", -1, -1, -1, -1, ""},
   1.188 -        {91, 0,    0.0,  MARKER_BUTTON, "lefttrigger", -1, -1, -1, -1, ""},
   1.189 -        {396, 36,   0.0,  MARKER_BUTTON, "rightshoulder", -1, -1, -1, -1, ""},
   1.190 -        {375, 0,    0.0,  MARKER_BUTTON, "righttrigger", -1, -1, -1, -1, ""},
   1.191 -        {75,  154,  0.0,  MARKER_BUTTON, "leftstick", -1, -1, -1, -1, ""},
   1.192 -        {305, 230,  0.0,  MARKER_BUTTON, "rightstick", -1, -1, -1, -1, ""},
   1.193 -        {75,  154,  0.0,  MARKER_AXIS,   "leftx", -1, -1, -1, -1, ""},
   1.194 -        {75,  154,  90.0, MARKER_AXIS,   "lefty", -1, -1, -1, -1, ""},        
   1.195 -        {305, 230,  0.0,  MARKER_AXIS,   "rightx", -1, -1, -1, -1, ""},
   1.196 -        {305, 230,  90.0, MARKER_AXIS,   "righty", -1, -1, -1, -1, ""},
   1.197 -    };
   1.198 +    Uint32 bound_ticks = 0;
   1.199 +    SDL_JoystickID nJoystickID;
   1.200 +    Uint32 unDeflectedAxes = 0;
   1.201  
   1.202      /* Create a window to display joystick axis position */
   1.203      window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
   1.204 @@ -139,14 +243,14 @@
   1.205                                SCREEN_HEIGHT, 0);
   1.206      if (window == NULL) {
   1.207          SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
   1.208 -        return SDL_FALSE;
   1.209 +        return;
   1.210      }
   1.211  
   1.212      screen = SDL_CreateRenderer(window, -1, 0);
   1.213      if (screen == NULL) {
   1.214          SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
   1.215          SDL_DestroyWindow(window);
   1.216 -        return SDL_FALSE;
   1.217 +        return;
   1.218      }
   1.219      
   1.220      background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
   1.221 @@ -173,23 +277,14 @@
   1.222      To skip a button, press SPACE or click/touch the screen\n\
   1.223      To exit, press ESC\n\
   1.224      ====================================================================================\n");
   1.225 -    
   1.226 -    /* Initialize mapping with GUID and name */
   1.227 -    SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), temp, SDL_arraysize(temp));
   1.228 -    SDL_snprintf(mapping, SDL_arraysize(mapping), "%s,%s,platform:%s,",
   1.229 -        temp, name ? name : "Unknown Joystick", SDL_GetPlatform());
   1.230 +
   1.231 +    nJoystickID = SDL_JoystickInstanceID(joystick);
   1.232  
   1.233      /* Loop, getting joystick events! */
   1.234 -    for(s=0; s<SDL_arraysize(steps) && !done;) {
   1.235 -        /* blank screen, set up for drawing this frame. */
   1.236 -        step = &steps[s];
   1.237 -        SDL_strlcpy(step->mapping, mapping, SDL_arraysize(step->mapping));
   1.238 -        step->axis = -1;
   1.239 -        step->button = -1;
   1.240 -        step->hat = -1;
   1.241 -        step->hat_value = -1;
   1.242 -        
   1.243 -        switch(step->marker) {
   1.244 +    while (!done && !s_bBindingComplete) {
   1.245 +        int iElement = s_arrBindingOrder[s_iCurrentBinding];
   1.246 +
   1.247 +        switch (s_arrBindingDisplay[iElement].marker) {
   1.248              case MARKER_AXIS:
   1.249                  marker = axis;
   1.250                  break;
   1.251 @@ -200,137 +295,182 @@
   1.252                  break;
   1.253          }
   1.254          
   1.255 -        dst.x = step->x;
   1.256 -        dst.y = step->y;
   1.257 +        dst.x = s_arrBindingDisplay[iElement].x;
   1.258 +        dst.y = s_arrBindingDisplay[iElement].y;
   1.259          SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h);
   1.260 -        next=SDL_FALSE;
   1.261  
   1.262 -        SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
   1.263 -
   1.264 -        while (!done && !next) {
   1.265 -            if (SDL_GetTicks() - alpha_ticks > 5) {
   1.266 -                alpha_ticks = SDL_GetTicks();
   1.267 -                alpha += alpha_step;
   1.268 -                if (alpha == 255) {
   1.269 -                    alpha_step = -1;
   1.270 -                }
   1.271 -                if (alpha < 128) {
   1.272 -                    alpha_step = 1;
   1.273 -                }
   1.274 +        if (SDL_GetTicks() - alpha_ticks > 5) {
   1.275 +            alpha_ticks = SDL_GetTicks();
   1.276 +            alpha += alpha_step;
   1.277 +            if (alpha == 255) {
   1.278 +                alpha_step = -1;
   1.279              }
   1.280 -            
   1.281 -            SDL_RenderClear(screen);
   1.282 -            SDL_RenderCopy(screen, background, NULL, NULL);
   1.283 -            SDL_SetTextureAlphaMod(marker, alpha);
   1.284 -            SDL_SetTextureColorMod(marker, 10, 255, 21);
   1.285 -            SDL_RenderCopyEx(screen, marker, NULL, &dst, step->angle, NULL, SDL_FLIP_NONE);
   1.286 -            SDL_RenderPresent(screen);
   1.287 -            
   1.288 -            if (SDL_PollEvent(&event)) {
   1.289 -                switch (event.type) {
   1.290 -                case SDL_JOYAXISMOTION:
   1.291 -                    if ((event.jaxis.value > 20000 || event.jaxis.value < -20000) && event.jaxis.value != -32768) {
   1.292 -                        for (_s = 0; _s < s; _s++) {
   1.293 -                            if (steps[_s].axis == event.jaxis.axis) {
   1.294 -                                break;
   1.295 -                            }
   1.296 -                        }
   1.297 -                        if (_s == s) {
   1.298 -                            step->axis = event.jaxis.axis;
   1.299 -                            SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
   1.300 -                            SDL_snprintf(temp, SDL_arraysize(temp), ":a%u,", event.jaxis.axis);
   1.301 -                            SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
   1.302 -                            s++;
   1.303 -                            next=SDL_TRUE;
   1.304 -                        }
   1.305 -                    }
   1.306 -                    
   1.307 -                    break;
   1.308 -                case SDL_JOYHATMOTION:
   1.309 -                        if (event.jhat.value == SDL_HAT_CENTERED) {
   1.310 -                            break;  /* ignore centering, we're probably just coming back to the center from the previous item we set. */
   1.311 -                        }
   1.312 -                        for (_s = 0; _s < s; _s++) {
   1.313 -                            if (steps[_s].hat == event.jhat.hat && steps[_s].hat_value == event.jhat.value) {
   1.314 -                                break;
   1.315 -                            }
   1.316 -                        }
   1.317 -                        if (_s == s) {
   1.318 -                            step->hat = event.jhat.hat;
   1.319 -                            step->hat_value = event.jhat.value;
   1.320 -                            SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
   1.321 -                            SDL_snprintf(temp, SDL_arraysize(temp), ":h%u.%u,", event.jhat.hat, event.jhat.value );
   1.322 -                            SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
   1.323 -                            s++;
   1.324 -                            next=SDL_TRUE;
   1.325 -                        }
   1.326 -                    break;
   1.327 -                case SDL_JOYBALLMOTION:
   1.328 -                    break;
   1.329 -                case SDL_JOYBUTTONUP:
   1.330 -                    for (_s = 0; _s < s; _s++) {
   1.331 -                        if (steps[_s].button == event.jbutton.button) {
   1.332 -                            break;
   1.333 -                        }
   1.334 -                    }
   1.335 -                    if (_s == s) {
   1.336 -                        step->button = event.jbutton.button;
   1.337 -                        SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
   1.338 -                        SDL_snprintf(temp, SDL_arraysize(temp), ":b%u,", event.jbutton.button);
   1.339 -                        SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
   1.340 -                        s++;
   1.341 -                        next=SDL_TRUE;
   1.342 -                    }
   1.343 -                    break;
   1.344 -                case SDL_FINGERDOWN:
   1.345 -                case SDL_MOUSEBUTTONDOWN:
   1.346 -                    /* Skip this step */
   1.347 -                    s++;
   1.348 -                    next=SDL_TRUE;
   1.349 -                    break;
   1.350 -                case SDL_KEYDOWN:
   1.351 -                    if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
   1.352 -                        /* Undo! */
   1.353 -                        if (s > 0) {
   1.354 -                            prev_step = &steps[--s];
   1.355 -                            SDL_strlcpy(mapping, prev_step->mapping, SDL_arraysize(prev_step->mapping));
   1.356 -                            next = SDL_TRUE;
   1.357 -                        }
   1.358 -                        break;
   1.359 -                    }
   1.360 -                    if (event.key.keysym.sym == SDLK_SPACE) {
   1.361 -                        /* Skip this step */
   1.362 -                        s++;
   1.363 -                        next=SDL_TRUE;
   1.364 -                        break;
   1.365 -                    }
   1.366 -                    
   1.367 -                    if ((event.key.keysym.sym != SDLK_ESCAPE)) {
   1.368 -                        break;
   1.369 -                    }
   1.370 -                    /* Fall through to signal quit */
   1.371 -                case SDL_QUIT:
   1.372 -                    done = SDL_TRUE;
   1.373 -                    break;
   1.374 -                default:
   1.375 -                    break;
   1.376 -                }
   1.377 +            if (alpha < 128) {
   1.378 +                alpha_step = 1;
   1.379              }
   1.380          }
   1.381  
   1.382 +        SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
   1.383 +        SDL_RenderClear(screen);
   1.384 +        SDL_RenderCopy(screen, background, NULL, NULL);
   1.385 +        SDL_SetTextureAlphaMod(marker, alpha);
   1.386 +        SDL_SetTextureColorMod(marker, 10, 255, 21);
   1.387 +        SDL_RenderCopyEx(screen, marker, NULL, &dst, s_arrBindingDisplay[iElement].angle, NULL, SDL_FLIP_NONE);
   1.388 +        SDL_RenderPresent(screen);
   1.389 +            
   1.390 +        while (SDL_PollEvent(&event) > 0) {
   1.391 +            switch (event.type) {
   1.392 +            case SDL_JOYDEVICEREMOVED:
   1.393 +                if (event.jaxis.which == nJoystickID) {
   1.394 +                    done = SDL_TRUE;
   1.395 +                }
   1.396 +                break;
   1.397 +            case SDL_JOYAXISMOTION:
   1.398 +                if (event.jaxis.which == nJoystickID) {
   1.399 +                    uint32_t unAxisMask = (1 << event.jaxis.axis);
   1.400 +                    SDL_bool bDeflected = (event.jaxis.value <= -20000 || event.jaxis.value >= 20000);
   1.401 +                    if (bDeflected && !(unDeflectedAxes & unAxisMask)) {
   1.402 +                        SDL_GameControllerButtonBind binding;
   1.403 +                        SDL_zero(binding);
   1.404 +                        binding.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
   1.405 +                        binding.value.axis = event.jaxis.axis;
   1.406 +                        ConfigureBinding(&binding);
   1.407 +                    }
   1.408 +                    if (bDeflected) {
   1.409 +                        unDeflectedAxes |= unAxisMask;
   1.410 +                    } else {
   1.411 +                        unDeflectedAxes &= ~unAxisMask;
   1.412 +                    }
   1.413 +                }
   1.414 +                break;
   1.415 +            case SDL_JOYHATMOTION:
   1.416 +                if (event.jhat.which == nJoystickID) {
   1.417 +                    if (event.jhat.value != SDL_HAT_CENTERED) {
   1.418 +                        SDL_GameControllerButtonBind binding;
   1.419 +                        SDL_zero(binding);
   1.420 +                        binding.bindType = SDL_CONTROLLER_BINDTYPE_HAT;
   1.421 +                        binding.value.hat.hat = event.jhat.hat;
   1.422 +                        binding.value.hat.hat_mask = event.jhat.value;
   1.423 +                        ConfigureBinding(&binding);
   1.424 +                    }
   1.425 +                }
   1.426 +                break;
   1.427 +            case SDL_JOYBALLMOTION:
   1.428 +                break;
   1.429 +            case SDL_JOYBUTTONDOWN:
   1.430 +                if (event.jbutton.which == nJoystickID) {
   1.431 +                    SDL_GameControllerButtonBind binding;
   1.432 +                    SDL_zero(binding);
   1.433 +                    binding.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
   1.434 +                    binding.value.button = event.jbutton.button;
   1.435 +                    ConfigureBinding(&binding);
   1.436 +                }
   1.437 +                break;
   1.438 +            case SDL_FINGERDOWN:
   1.439 +            case SDL_MOUSEBUTTONDOWN:
   1.440 +                /* Skip this step */
   1.441 +                SetCurrentBinding(s_iCurrentBinding + 1);
   1.442 +                break;
   1.443 +            case SDL_KEYDOWN:
   1.444 +                if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
   1.445 +                    SetCurrentBinding(s_iCurrentBinding - 1);
   1.446 +                    break;
   1.447 +                }
   1.448 +                if (event.key.keysym.sym == SDLK_SPACE) {
   1.449 +                    SetCurrentBinding(s_iCurrentBinding + 1);
   1.450 +                    break;
   1.451 +                }
   1.452 +
   1.453 +                if ((event.key.keysym.sym != SDLK_ESCAPE)) {
   1.454 +                    break;
   1.455 +                }
   1.456 +                /* Fall through to signal quit */
   1.457 +            case SDL_QUIT:
   1.458 +                done = SDL_TRUE;
   1.459 +                break;
   1.460 +            default:
   1.461 +                break;
   1.462 +            }
   1.463 +        }
   1.464 +
   1.465 +        SDL_Delay(15);
   1.466 +
   1.467 +        /* Wait 100 ms for joystick events to stop coming in,
   1.468 +           in case a controller sends multiple events for a single control (e.g. axis and button for trigger)
   1.469 +        */
   1.470 +        if (s_unPendingAdvanceTime && SDL_GetTicks() - s_unPendingAdvanceTime >= 100) {
   1.471 +            SetCurrentBinding(s_iCurrentBinding + 1);
   1.472 +        }
   1.473      }
   1.474  
   1.475 -    if (s == SDL_arraysize(steps) ) {
   1.476 +    if (s_bBindingComplete) {
   1.477 +        char mapping[1024];
   1.478 +        char trimmed_name[128];
   1.479 +        char *spot;
   1.480 +        int iIndex;
   1.481 +        char pszElement[12];
   1.482 +
   1.483 +        SDL_strlcpy(trimmed_name, name, SDL_arraysize(trimmed_name));
   1.484 +        while (SDL_isspace(trimmed_name[0])) {
   1.485 +            SDL_memmove(&trimmed_name[0], &trimmed_name[1], SDL_strlen(trimmed_name));
   1.486 +        }
   1.487 +        while (trimmed_name[0] && SDL_isspace(trimmed_name[SDL_strlen(trimmed_name) - 1])) {
   1.488 +            trimmed_name[SDL_strlen(trimmed_name) - 1] = '\0';
   1.489 +        }
   1.490 +        while ((spot = SDL_strchr(trimmed_name, ',')) != NULL) {
   1.491 +            SDL_memmove(spot, spot + 1, SDL_strlen(spot));
   1.492 +        }
   1.493 +
   1.494 +        /* Initialize mapping with GUID and name */
   1.495 +        SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), mapping, SDL_arraysize(mapping));
   1.496 +        SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   1.497 +        SDL_strlcat(mapping, trimmed_name, SDL_arraysize(mapping));
   1.498 +        SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   1.499 +        SDL_strlcat(mapping, "platform:", SDL_arraysize(mapping));
   1.500 +        SDL_strlcat(mapping, SDL_GetPlatform(), SDL_arraysize(mapping));
   1.501 +        SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   1.502 +
   1.503 +        for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
   1.504 +            SDL_GameControllerButtonBind *pBinding = &s_arrBindings[iIndex];
   1.505 +            if (pBinding->bindType == SDL_CONTROLLER_BINDTYPE_NONE) {
   1.506 +                continue;
   1.507 +            }
   1.508 +
   1.509 +            if (iIndex < SDL_CONTROLLER_BUTTON_MAX) {
   1.510 +                SDL_GameControllerButton eButton = (SDL_GameControllerButton)iIndex;
   1.511 +                SDL_strlcat(mapping, SDL_GameControllerGetStringForButton(eButton), SDL_arraysize(mapping));
   1.512 +            } else {
   1.513 +                SDL_GameControllerAxis eAxis = (SDL_GameControllerAxis)(iIndex - SDL_CONTROLLER_BUTTON_MAX);
   1.514 +                SDL_strlcat(mapping, SDL_GameControllerGetStringForAxis(eAxis), SDL_arraysize(mapping));
   1.515 +            }
   1.516 +            SDL_strlcat(mapping, ":", SDL_arraysize(mapping));
   1.517 +
   1.518 +            pszElement[0] = '\0';
   1.519 +            switch (pBinding->bindType) {
   1.520 +            case SDL_CONTROLLER_BINDTYPE_BUTTON:
   1.521 +                SDL_snprintf(pszElement, sizeof(pszElement), "b%d", pBinding->value.button);
   1.522 +                break;
   1.523 +            case SDL_CONTROLLER_BINDTYPE_AXIS:
   1.524 +                SDL_snprintf(pszElement, sizeof(pszElement), "a%d", pBinding->value.axis);
   1.525 +                break;
   1.526 +            case SDL_CONTROLLER_BINDTYPE_HAT:
   1.527 +                SDL_snprintf(pszElement, sizeof(pszElement), "h%d.%d", pBinding->value.hat.hat, pBinding->value.hat.hat_mask);
   1.528 +                break;
   1.529 +            default:
   1.530 +                SDL_assert(!"Unknown bind type");
   1.531 +                break;
   1.532 +            }
   1.533 +            SDL_strlcat(mapping, pszElement, SDL_arraysize(mapping));
   1.534 +            SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
   1.535 +        }
   1.536 +
   1.537          SDL_Log("Mapping:\n\n%s\n\n", mapping);
   1.538          /* Print to stdout as well so the user can cat the output somewhere */
   1.539          printf("%s\n", mapping);
   1.540      }
   1.541      
   1.542 -    while(SDL_PollEvent(&event)) {};
   1.543 -    
   1.544      SDL_DestroyRenderer(screen);
   1.545      SDL_DestroyWindow(window);
   1.546 -    return retval;
   1.547  }
   1.548  
   1.549  int
   1.550 @@ -378,9 +518,6 @@
   1.551  #else
   1.552      if (argv[1]) {
   1.553  #endif
   1.554 -        SDL_bool reportederror = SDL_FALSE;
   1.555 -        SDL_bool keepGoing = SDL_TRUE;
   1.556 -        SDL_Event event;
   1.557          int device;
   1.558  #ifdef __ANDROID__
   1.559          device = 0;
   1.560 @@ -388,34 +525,11 @@
   1.561          device = atoi(argv[1]);
   1.562  #endif
   1.563          joystick = SDL_JoystickOpen(device);
   1.564 -
   1.565 -        while ( keepGoing ) {
   1.566 -            if (joystick == NULL) {
   1.567 -                if ( !reportederror ) {
   1.568 -                    SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
   1.569 -                    keepGoing = SDL_FALSE;
   1.570 -                    reportederror = SDL_TRUE;
   1.571 -                }
   1.572 -            } else {
   1.573 -                reportederror = SDL_FALSE;
   1.574 -                keepGoing = WatchJoystick(joystick);
   1.575 -                SDL_JoystickClose(joystick);
   1.576 -            }
   1.577 -
   1.578 -            joystick = NULL;
   1.579 -            if (keepGoing) {
   1.580 -                SDL_Log("Waiting for attach\n");
   1.581 -            }
   1.582 -            while (keepGoing) {
   1.583 -                SDL_WaitEvent(&event);
   1.584 -                if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
   1.585 -                    || (event.type == SDL_MOUSEBUTTONDOWN)) {
   1.586 -                    keepGoing = SDL_FALSE;
   1.587 -                } else if (event.type == SDL_JOYDEVICEADDED) {
   1.588 -                    joystick = SDL_JoystickOpen(device);
   1.589 -                    break;
   1.590 -                }
   1.591 -            }
   1.592 +        if (joystick == NULL) {
   1.593 +            SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
   1.594 +        } else {
   1.595 +            WatchJoystick(joystick);
   1.596 +            SDL_JoystickClose(joystick);
   1.597          }
   1.598      }
   1.599      else {