// JLH = James Hammons <jlhamm@acm.org>
//
// Who When What
-// --- ---------- -------------------------------------------------------------
+// --- ---------- ------------------------------------------------------------
// JLH 01/05/2013 Created this file
//
// Class member initialization
/*static*/ int Gamepad::numJoysticks = 0;
/*static*/ SDL_Joystick * Gamepad::pad[8];
+/*static*/ char Gamepad::padName[8][128];
/*static*/ int Gamepad::numButtons[8];
/*static*/ int Gamepad::numHats[8];
/*static*/ int Gamepad::numAxes[8];
for(int i=0; i<numJoysticks; i++)
{
pad[i] = SDL_JoystickOpen(i);
- numButtons[i] = numHats[i] = 0;
+ numButtons[i] = numHats[i] = numAxes[i] = 0;
+ // We need to copy the contents of this pointer, as SDL will change it
+ // willy nilly to suit itself
+// padName[i] = SDL_JoystickName(i);
+ strncpy(padName[i], SDL_JoystickName(i), 127);
+ padName[i][127] = 0; // Just in case name's length > 127
if (pad[i])
{
numButtons[i] = SDL_JoystickNumButtons(pad[i]);
numHats[i] = SDL_JoystickNumHats(pad[i]);
numAxes[i] = SDL_JoystickNumAxes(pad[i]);
+ WriteLog("Gamepad: Joystick #%i: %s\n", i, padName[i]);
}
}
WriteLog("Gamepad: Found %u joystick%s.\n", numJoysticks, (numJoysticks == 1 ? "" : "s"));
+#if 0
+for(int i=0; i<numJoysticks; i++)
+ printf("GAMEPAD::AllocateJoysticks: stick #%i = %s\n", i, padName[i]);
+#endif
}
}
+const char * Gamepad::GetJoystickName(int joystickID)
+{
+ // Sanity check
+ if (joystickID >= 8)
+ return NULL;
+
+//printf("GAMEPAD: Getting name (%s) for joystick #%i...\n", padName[joystickID], joystickID);
+ return padName[joystickID];
+}
+
+
bool Gamepad::GetState(int joystickID, int buttonID)
{
uint8_t hatMask[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
{
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] > 0 && (direction == 0))
+ if ((axis[joystickID][axisNum] > 32000) && (direction == 0))
+//{
+//printf("Axis + hit!\n");
return true;
+//}
- if (axis[joystickID][axisNum] < 0 && (direction == 1))
+ if ((axis[joystickID][axisNum] < -32000) && (direction == 1))
+//{
+//printf("Axis - hit!\n");
return true;
+//}
}
}
}
+//UNUSED
int Gamepad::CheckButtonPressed(void)
{
// This translates the hat direction to a mask index.
}
+// UNUSED
int Gamepad::GetButtonID(void)
{
// Return single button ID being pressed (if any)
}
+// UNUSED
int Gamepad::GetJoystickID(void)
{
// Return joystick ID of button being pressed (if any)
}
+// However, SDL 2 *does* support hot-plugging! :-D
#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.