From 9d399cea8466c7e4a6a1336dafdc830c4680d977 Mon Sep 17 00:00:00 2001 From: Shamus Hammons Date: Sat, 23 Feb 2013 21:31:41 -0600 Subject: [PATCH] Added analog axis support to Gamepad module. This is necessary because some gamepads that have directional pads don't report as a hat (which you would expect) but report themselves as analog axes that provide only the extreme ends of each axis. Such "digi-log" joysticks should now work and be configurable. --- src/gui/controllerwidget.cpp | 7 +++++++ src/gui/controllerwidget.h | 1 + src/gui/gamepad.cpp | 32 ++++++++++++++++++++++++++++++++ src/gui/gamepad.h | 13 +++++++++---- src/gui/keygrabber.cpp | 2 +- src/gui/mainwin.cpp | 6 ++---- src/settings.h | 5 ----- 7 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/gui/controllerwidget.cpp b/src/gui/controllerwidget.cpp index 79e91b6..0386334 100644 --- a/src/gui/controllerwidget.cpp +++ b/src/gui/controllerwidget.cpp @@ -44,6 +44,8 @@ char ControllerWidget::keyName2[64][16] = { char ControllerWidget::hatName[4][16] = { "Up", "Rt", "Dn", "Lf" }; +char ControllerWidget::axisName[2][8] = { "+", "-" }; + // This is hard-coded crap. It's crap-tastic! // These are the positions to draw the button names at, ordered by the BUTTON_* sequence // found in joystick.h. @@ -147,6 +149,11 @@ void ControllerWidget::paintEvent(QPaintEvent * /*event*/) DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1], QString("j%1").arg(hatName[keys[i] & JOY_BUTTON_MASK])); } + else if (keys[i] & JOY_AXIS) + { + DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1], + QString("JA%1%2").arg((keys[i] & JOY_AXISNUM_MASK) >> 1).arg(axisName[keys[i] & JOY_AXISDIR_MASK])); + } #endif else DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1], QString("???")); diff --git a/src/gui/controllerwidget.h b/src/gui/controllerwidget.h index eada2d6..b1065cd 100644 --- a/src/gui/controllerwidget.h +++ b/src/gui/controllerwidget.h @@ -37,6 +37,7 @@ class ControllerWidget: public QWidget static char keyName1[96][16]; static char keyName2[64][16]; static char hatName[4][16]; + static char axisName[2][8]; static int buttonPos[21][2]; }; diff --git a/src/gui/gamepad.cpp b/src/gui/gamepad.cpp index 1ef1a4e..84307f8 100644 --- a/src/gui/gamepad.cpp +++ b/src/gui/gamepad.cpp @@ -20,8 +20,10 @@ /*static*/ SDL_Joystick * Gamepad::pad[8]; /*static*/ int Gamepad::numButtons[8]; /*static*/ int Gamepad::numHats[8]; +/*static*/ int Gamepad::numAxes[8]; /*static*/ bool Gamepad::button[8][256]; /*static*/ uint8_t Gamepad::hat[8][32]; +/*static*/ int32_t Gamepad::axis[8][32]; Gamepad::Gamepad(void)//: numJoysticks(0) @@ -54,6 +56,7 @@ void Gamepad::AllocateJoysticks(void) { numButtons[i] = SDL_JoystickNumButtons(pad[i]); numHats[i] = SDL_JoystickNumHats(pad[i]); + numAxes[i] = SDL_JoystickNumAxes(pad[i]); } } @@ -85,6 +88,20 @@ bool Gamepad::GetState(int joystickID, int buttonID) uint8_t hatDirection = hatMask[buttonID & JOY_HATBUT_MASK]; return (hat[joystickID][hatNumber] & hatDirection ? true : false); } + else if (buttonID & JOY_AXIS) + { + int axisNum = (buttonID & JOY_AXISNUM_MASK) >> 1; + int direction = (buttonID & JOY_AXISDIR_MASK); + + if (axis[joystickID][axisNum] != 0) + { + if (axis[joystickID][axisNum] > 0 && (direction == 0)) + return true; + + if (axis[joystickID][axisNum] < 0 && (direction == 1)) + return true; + } + } // Default == failure return false; @@ -111,6 +128,18 @@ int Gamepad::CheckButtonPressed(void) if (hat[i][j]) return (JOY_HAT | hatNum[hat[i][j]]); } + + for(int j=0; j 0) + if (axis[i][j] > 32000) + return (JOY_AXIS | (j << 1) | 0); + +// if (axis[i][j] < 0) + if (axis[i][j] < -32000) + return (JOY_AXIS | (j << 1) | 1); + } } return -1; @@ -143,6 +172,9 @@ void Gamepad::Update(void) for(int j=0; j #include "SDL.h" @@ -45,9 +48,11 @@ class Gamepad static int numJoysticks; static SDL_Joystick * pad[8]; static int numButtons[8]; + static int numAxes[8]; static int numHats[8]; static bool button[8][256]; static uint8_t hat[8][32]; + static int32_t axis[8][32]; }; #endif // __GAMEPAD_H__ diff --git a/src/gui/keygrabber.cpp b/src/gui/keygrabber.cpp index 7efe537..b9b285c 100644 --- a/src/gui/keygrabber.cpp +++ b/src/gui/keygrabber.cpp @@ -70,7 +70,7 @@ void KeyGrabber::CheckGamepad(void) { button = Gamepad::CheckButtonPressed(); - if (button == -1) + if (button == -1) return; buttonDown = true; diff --git a/src/gui/mainwin.cpp b/src/gui/mainwin.cpp index 87e6e3f..0e19488 100644 --- a/src/gui/mainwin.cpp +++ b/src/gui/mainwin.cpp @@ -346,8 +346,6 @@ MainWin::MainWin(bool autoRun): running(true), powerButtonOn(false), // Prevent the scanner from running... return; } -// else -// memcpy(jagMemSpace + 0xE00000, jaguarBootROM, 0x20000); // Otherwise, use the stock BIOS // Run the scanner if nothing passed in and *not* Alpine mode... // NB: Really need to look into caching the info scanned in here... @@ -523,10 +521,10 @@ void MainWin::HandleGamepads(void) for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++) { - if (vjs.p1KeyBindings[i] & (JOY_BUTTON | JOY_HAT)) + if (vjs.p1KeyBindings[i] & (JOY_BUTTON | JOY_HAT | JOY_AXIS)) joypad_0_buttons[i] = (Gamepad::GetState(0, vjs.p1KeyBindings[i]) ? 0x01 : 0x00); - if (vjs.p2KeyBindings[i] & (JOY_BUTTON | JOY_HAT)) + if (vjs.p2KeyBindings[i] & (JOY_BUTTON | JOY_HAT | JOY_AXIS)) joypad_1_buttons[i] = (Gamepad::GetState(1, vjs.p2KeyBindings[i]) ? 0x01 : 0x00); } } diff --git a/src/settings.h b/src/settings.h index bb160d5..9824e40 100644 --- a/src/settings.h +++ b/src/settings.h @@ -58,11 +58,6 @@ enum { RT_NORMAL = 0, RT_TV = 1 }; enum { BT_K_SERIES, BT_M_SERIES, BT_STUBULATOR_1, BT_STUBULATOR_2 }; -// Exported functions - -//void LoadVJSettings(void); -//void SaveVJSettings(void); - // Exported variables extern VJSettings vjs; -- 2.37.2