2 // gamepad.cpp - Host joystick handling (using SDL)
5 // (C) 2013 Underground Software
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- -------------------------------------------------------------
11 // JLH 01/05/2013 Created this file
18 // Class member initialization
19 /*static*/ int Gamepad::numJoysticks = 0;
20 /*static*/ SDL_Joystick * Gamepad::pad[8];
21 /*static*/ const char * Gamepad::padName[8];
22 /*static*/ int Gamepad::numButtons[8];
23 /*static*/ int Gamepad::numHats[8];
24 /*static*/ int Gamepad::numAxes[8];
25 /*static*/ bool Gamepad::button[8][256];
26 /*static*/ uint8_t Gamepad::hat[8][32];
27 /*static*/ int32_t Gamepad::axis[8][32];
30 Gamepad::Gamepad(void)//: numJoysticks(0)
36 Gamepad::~Gamepad(void)
38 DeallocateJoysticks();
42 void Gamepad::AllocateJoysticks(void)
44 // DeallocateJoysticks();
45 numJoysticks = SDL_NumJoysticks();
51 for(int i=0; i<numJoysticks; i++)
53 pad[i] = SDL_JoystickOpen(i);
54 padName[i] = SDL_JoystickName(i);
55 numButtons[i] = numHats[i] = 0;
59 numButtons[i] = SDL_JoystickNumButtons(pad[i]);
60 numHats[i] = SDL_JoystickNumHats(pad[i]);
61 numAxes[i] = SDL_JoystickNumAxes(pad[i]);
65 WriteLog("Gamepad: Found %u joystick%s.\n", numJoysticks, (numJoysticks == 1 ? "" : "s"));
69 void Gamepad::DeallocateJoysticks(void)
71 for(int i=0; i<numJoysticks; i++)
72 SDL_JoystickClose(pad[i]);
76 const char * Gamepad::GetJoystickName(int joystickID)
82 return padName[joystickID];
86 bool Gamepad::GetState(int joystickID, int buttonID)
88 uint8_t hatMask[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
90 if (buttonID & JOY_BUTTON)
93 int buttonNum = (buttonID & JOY_BUTTON_MASK);
94 return button[joystickID][buttonNum];
96 else if (buttonID & JOY_HAT)
99 int hatNumber = (buttonID & JOY_HATNUM_MASK) >> 3;
100 uint8_t hatDirection = hatMask[buttonID & JOY_HATBUT_MASK];
101 return (hat[joystickID][hatNumber] & hatDirection ? true : false);
103 else if (buttonID & JOY_AXIS)
105 int axisNum = (buttonID & JOY_AXISNUM_MASK) >> 1;
106 int direction = (buttonID & JOY_AXISDIR_MASK);
108 if (axis[joystickID][axisNum] != 0)
110 if (axis[joystickID][axisNum] > 16000 && (direction == 0))
113 if (axis[joystickID][axisNum] < -16000 && (direction == 1))
118 // Default == failure
123 int Gamepad::CheckButtonPressed(void)
125 // This translates the hat direction to a mask index.
126 int hatNum[16] = { -1, 0, 1, -1, 2, -1, -1, -1,
127 3, -1, -1, -1, -1, -1, -1, -1 };
129 // Return single button ID being pressed (if any)
130 for(int i=0; i<numJoysticks; i++)
132 for(int j=0; j<numButtons[i]; j++)
135 return (JOY_BUTTON | j);
138 for(int j=0; j<numHats[i]; j++)
141 return (JOY_HAT | hatNum[hat[i][j]]);
144 for(int j=0; j<numAxes[i]; j++)
146 // We encode these as axis # (in bits 1-15), up or down in bit 0.
147 // if (axis[i][j] > 0)
148 if (axis[i][j] > 32000)
149 return (JOY_AXIS | (j << 1) | 0);
151 // if (axis[i][j] < 0)
152 if (axis[i][j] < -32000)
153 return (JOY_AXIS | (j << 1) | 1);
161 int Gamepad::GetButtonID(void)
163 // Return single button ID being pressed (if any)
168 int Gamepad::GetJoystickID(void)
170 // Return joystick ID of button being pressed (if any)
175 void Gamepad::Update(void)
177 // SDL_PollEvent(&event);
178 SDL_JoystickUpdate();
180 for(int i=0; i<numJoysticks; i++)
182 for(int j=0; j<numButtons[i]; j++)
183 button[i][j] = SDL_JoystickGetButton(pad[i], j);
185 for(int j=0; j<numHats[i]; j++)
186 hat[i][j] = SDL_JoystickGetHat(pad[i], j);
188 for(int j=0; j<numAxes[i]; j++)
189 axis[i][j] = SDL_JoystickGetAxis(pad[i], j);
195 // Need to test this. It may be that the only time joysticks are detected is
196 // when the program is first run. That would suck.
197 // Well, it turns out that SDL doesn't do hot plugging. :-(
198 void Gamepad::CheckConsistency(void)
200 int currentNumJoysticks = SDL_NumJoysticks();
202 // Check to see if the # of joysticks reported by SDL changed
203 if (currentNumJoysticks == numJoysticks)
206 // Either one or more joysticks were plugged in, or removed. Fix up our
207 // internal states to reflect this.