Commit ad6bcb7d authored by Richard Gobeille's avatar Richard Gobeille
Browse files

engine: add joyHasButton() function to determine whether a controller has a...

engine: add joyHasButton() function to determine whether a controller has a particular button or not

This is needed because not all controllers supported by SDL's game controller API support all buttons exposed by said API, leaving gaps between valid button indexes.
parent 3cd951d7
......@@ -212,6 +212,7 @@ typedef struct
int32_t numButtons;
int32_t numHats;
int32_t isGameController;
uint32_t validButtons;
} controllerinput_t;
extern controllerinput_t joystick;
......@@ -272,6 +273,7 @@ void mouseLockToWindow(char a);
int32_t mouseReadButtons(void);
void mouseReadPos(int32_t *x, int32_t *y);
bool joyHasButton(int button);
void joyReadButtons(int32_t *pResult);
void joySetDeadZone(int32_t axis, uint16_t dead, uint16_t satur);
void joyGetDeadZone(int32_t axis, uint16_t *dead, uint16_t *satur);
......
......@@ -156,6 +156,7 @@ controllerinput_t joystick;
void joySetCallback(void (*callback)(int32_t, int32_t)) { joystick.pCallback = callback; }
void joyReadButtons(int32_t *pResult) { *pResult = appactive ? joystick.bits : 0; }
bool joyHasButton(int button) { return !!(joystick.validButtons & (1 << button)); }
#if defined __linux || defined EDUKE32_BSD || defined __APPLE__
# include <sys/mman.h>
......
......@@ -957,6 +957,7 @@ void joyScanDevices()
joystick.numAxes = SDL_CONTROLLER_AXIS_MAX;
joystick.numButtons = SDL_CONTROLLER_BUTTON_MAX;
joystick.validButtons = UINT32_MAX;
#if SDL_MINOR_VERSION > 0 || SDL_PATCHLEVEL >= 14
if (EDUKE32_SDL_LINKED_PREREQ(linked, 2, 0, 14))
{
......@@ -965,10 +966,14 @@ void joyScanDevices()
if (SDL_GameControllerHasAxis(controller, (SDL_GameControllerAxis)j))
joystick.numAxes = j + 1;
joystick.validButtons = 0;
joystick.numButtons = 0;
for (int j = 0; j < SDL_CONTROLLER_BUTTON_MAX; ++j)
if (SDL_GameControllerHasButton(controller, (SDL_GameControllerButton)j))
{
joystick.numButtons = j + 1;
joystick.validButtons |= (1 << j);
}
}
#endif
joystick.isGameController = 1;
......@@ -996,6 +1001,7 @@ void joyScanDevices()
joystick.numButtons = min(32, SDL_JoystickNumButtons(joydev));
joystick.numHats = min((36 - joystick.numButtons) / 4, SDL_JoystickNumHats(joydev));
joystick.validButtons = UINT32_MAX;
joystick.isGameController = 0;
buildprint("Joystick ", i+1, " has ", joystick.numAxes, " axes, ", joystick.numButtons, " buttons, ");
......
......@@ -1028,6 +1028,7 @@ static BOOL InitDirectInput(void)
joystick.numAxes = (uint8_t)didc.dwAxes;
joystick.numButtons = min<uint8_t>(32,didc.dwButtons);
joystick.numHats = (uint8_t)didc.dwPOVs;
joystick.validButtons = UINT32_MAX;
initprintf("Controller has %d axes, %d buttons, and %d hat(s).\n",joystick.numAxes,joystick.numButtons,joystick.numHats);
axisdefs = (struct _joydef *)Xcalloc(didc.dwAxes, sizeof(struct _joydef));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment