]> Shamusworld >> Repos - virtualjaguar/blobdiff - src/gui/gamepad.cpp
Fixed updated joystick handling, first major stab at gamepad profiles.
[virtualjaguar] / src / gui / gamepad.cpp
index 6ca2329a02cf20ab737bdca253cdc37bb7552f43..93989eed7ae7a975ecdec6d39469f23e06809da6 100644 (file)
 // Class member initialization
 /*static*/ int Gamepad::numJoysticks = 0;
 /*static*/ SDL_Joystick * Gamepad::pad[8];
+/*static*/ const char * Gamepad::padName[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)
@@ -48,12 +51,14 @@ void Gamepad::AllocateJoysticks(void)
        for(int i=0; i<numJoysticks; i++)
        {
                pad[i] = SDL_JoystickOpen(i);
-               numButtons[i] = numHats[i] = 0;
+               padName[i] = SDL_JoystickName(i);
+               numButtons[i] = numHats[i] = numAxes[i] = 0;
 
                if (pad[i])
                {
                        numButtons[i] = SDL_JoystickNumButtons(pad[i]);
                        numHats[i] = SDL_JoystickNumHats(pad[i]);
+                       numAxes[i] = SDL_JoystickNumAxes(pad[i]);
                }
        }
 
@@ -68,6 +73,16 @@ void Gamepad::DeallocateJoysticks(void)
 }
 
 
+const char * Gamepad::GetJoystickName(int joystickID)
+{
+       // Sanity check
+       if (joystickID >= 8)
+               return NULL;
+
+       return padName[joystickID];
+}
+
+
 bool Gamepad::GetState(int joystickID, int buttonID)
 {
        uint8_t hatMask[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
@@ -76,7 +91,6 @@ bool Gamepad::GetState(int joystickID, int buttonID)
        {
                // Handle SDL button
                int buttonNum = (buttonID & JOY_BUTTON_MASK);
-//             SDL_JoystickGetButton(pad[joystickID]);
                return button[joystickID][buttonNum];
        }
        else if (buttonID & JOY_HAT)
@@ -84,10 +98,29 @@ bool Gamepad::GetState(int joystickID, int buttonID)
                // Handle SDL hats
                int hatNumber = (buttonID & JOY_HATNUM_MASK) >> 3;
                uint8_t hatDirection = hatMask[buttonID & JOY_HATBUT_MASK];
-//             uint8 direction = SDL_JoystickGetHat(pad[joystickID], hatNumber);
-//             return (
                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);
+//printf("Checking pad #%u axis %u: axis = %i, direction = %u\n", joystickID, axisNum, axis[joystickID][axisNum], direction);
+
+               if (axis[joystickID][axisNum] != 0)
+               {
+                       if ((axis[joystickID][axisNum] > 32000) && (direction == 0))
+//{
+//printf("Axis + hit!\n");
+                               return true;
+//}
+
+                       if ((axis[joystickID][axisNum] < -32000) && (direction == 1))
+//{
+//printf("Axis - hit!\n");
+                               return true;
+//}
+               }
+       }
 
        // Default == failure
        return false;
@@ -114,6 +147,18 @@ int Gamepad::CheckButtonPressed(void)
                        if (hat[i][j])
                                return (JOY_HAT | hatNum[hat[i][j]]);
                }
+
+               for(int j=0; j<numAxes[i]; j++)
+               {
+                       // We encode these as axis # (in bits 1-15), up or down in bit 0.
+//                     if (axis[i][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;
@@ -146,6 +191,9 @@ void Gamepad::Update(void)
 
                for(int j=0; j<numHats[i]; j++)
                        hat[i][j] = SDL_JoystickGetHat(pad[i], j);
+
+               for(int j=0; j<numAxes[i]; j++)
+                       axis[i][j] = SDL_JoystickGetAxis(pad[i], j);
        }
 }
 
@@ -153,6 +201,7 @@ void Gamepad::Update(void)
 #if 0
 // Need to test this. It may be that the only time joysticks are detected is
 // when the program is first run. That would suck.
+// Well, it turns out that SDL doesn't do hot plugging. :-(
 void Gamepad::CheckConsistency(void)
 {
        int currentNumJoysticks = SDL_NumJoysticks();