in_sdl: try to guess stick vs pressure-sensitive button 'axis'
authornotaz <notasas@gmail.com>
Fri, 23 May 2025 20:59:28 +0000 (23:59 +0300)
committeririxxxx <31696370+irixxxx@users.noreply.github.com>
Sun, 25 May 2025 20:52:46 +0000 (22:52 +0200)
if the user doesn't press anything on init/probe and the gamepad
is not broken it might work...

in_sdl.c

index 8c3f08a..539e6a2 100644 (file)
--- a/in_sdl.c
+++ b/in_sdl.c
@@ -31,6 +31,7 @@ struct in_sdl_state {
        int joy_numbuttons;
        int *joy_axis_keydown;
        int joy_hat_down;
+       unsigned int joy_axis_as_btn; // bitmask; vs axes of centered sticks
        unsigned int redraw:1;
        unsigned int abs_to_udlr:1;
        unsigned int hat_warned:1;
@@ -206,7 +207,7 @@ static void in_sdl_probe(const in_drv_t *drv)
        const char * const * key_names = in_sdl_keys;
        struct in_sdl_state *state;
        SDL_Joystick *joy;
-       int i, joycount;
+       int i, a, joycount;
        char name[256];
 
        if (pdata->key_names)
@@ -235,9 +236,17 @@ static void in_sdl_probe(const in_drv_t *drv)
                state->joy_id = i;
                state->joy_numbuttons = SDL_JoystickNumButtons(joy);
                state->drv = drv;
+               for (a = 0; a < state->joy_numaxes; a++)
+                       if (SDL_JoystickGetAxis(joy, a) < -16384)
+                               state->joy_axis_as_btn |= 1u << a;
 
                snprintf(name, sizeof(name), IN_SDL_PREFIX "%s", SDL_JoystickName(i));
                in_register(name, -1, state, SDLK_LAST, key_names, 0);
+
+               printf("  %s: %d buttons %d axes %d hat(s), "
+                       "guessed axis_as_btn mask: %x\n",
+                       name, state->joy_numbuttons, state->joy_numaxes,
+                       SDL_JoystickNumHats(joy), state->joy_axis_as_btn);
        }
 
        if (joycount > 0)
@@ -333,8 +342,8 @@ static int handle_joy_event(struct in_sdl_state *state, SDL_Event *event,
                        // some pressure sensitive buttons appear as an axis that goes
                        // from -32768 (released) to 32767 (pressed) and there is no
                        // way to distinguish from ones centered at 0? :(
-                       //else
-                       //      kc = kc_axis_base + event->jaxis.axis * 2;
+                       else if (!(state->joy_axis_as_btn & (1u << event->jaxis.axis)))
+                               kc = kc_axis_base + event->jaxis.axis * 2;
                        down = 1;
                }
                else if (event->jaxis.value > 16384) {