1 #include "InputManager.h"
2 
3 typedef struct SdlKeyName {
4   int key;
5   char name[50];
6 } SdlKeyName;
7 
8 static const SdlKeyName sdlKeyDictionnary[] = {
9   { SDLK_UNKNOWN,   "  "},
10   { SDLK_BACKSPACE, "Backspace"  },
11   { SDLK_TAB,       "Tab"        },
12   { SDLK_CLEAR,     "Clear"      },
13   { SDLK_RETURN,    "Return"     },
14   { SDLK_PAUSE,     "Pause"      },
15   { SDLK_ESCAPE,    "Escape"     },
16   { SDLK_SPACE,     "Space"      },
17   { SDLK_DELETE,    "Delete"     },
18   { SDLK_KP0,       "KP 0"       },
19   { SDLK_KP1,       "KP 1"       },
20   { SDLK_KP2,       "KP 2"       },
21   { SDLK_KP3,       "KP 3"       },
22   { SDLK_KP4,       "KP 4"       },
23   { SDLK_KP5,       "KP 5"       },
24   { SDLK_KP6,       "KP 6"       },
25   { SDLK_KP7,       "KP 7"       },
26   { SDLK_KP8,       "KP 8"       },
27   { SDLK_KP9,       "KP 9"       },
28   { SDLK_UP,        "Up Arrow"   },
29   { SDLK_DOWN,      "Down Arrow" },
30   { SDLK_LEFT,      "Left Arrow" },
31   { SDLK_RIGHT,     "Right Arrow"},
32   { SDLK_INSERT,    "Insert"     },
33   { SDLK_HOME,      "Home"       },
34   { SDLK_END,       "End"        },
35   { SDLK_PAGEUP,     "Page Up"   },
36   { SDLK_PAGEDOWN,   "Page Down" },
37   { SDLK_F1,         "F1"        },
38   { SDLK_F2,         "F2"        },
39   { SDLK_F3,         "F3"        },
40   { SDLK_F4,         "F4"        },
41   { SDLK_F5,         "F5"        },
42   { SDLK_F6,         "F6"        },
43   { SDLK_F7,         "F7"        },
44   { SDLK_F8,         "F8"        },
45   { SDLK_F9,         "F9"        },
46   { SDLK_F10,        "F10"       },
47   { SDLK_F11,        "F11"       },
48   { SDLK_F12,        "F12"       },
49   { SDLK_F13,        "F13"       },
50   { SDLK_F14,        "F14"       },
51   { SDLK_F15,        "F15"       },
52   { SDLK_NUMLOCK,    "Num Lock"  },
53   { SDLK_CAPSLOCK,   "Caps Lock" },
54   { SDLK_SCROLLOCK,  "Scroll Lock"},
55   { SDLK_RSHIFT,     "Right Shift"},
56   { SDLK_LSHIFT,     "Left Shift" },
57   { SDLK_RCTRL,     "Right Ctrl" },
58   { SDLK_LCTRL,     "Left Ctrl" },
59   { SDLK_RALT,     "Right Alt" },
60   { SDLK_LALT,     "Left Alt" },
61   { SDLK_RMETA,     "Right Meta" },
62   { SDLK_LMETA,     "Left Meta" },
63   { SDLK_RSUPER,     "Right Windows" },
64   { SDLK_LSUPER,     "Left Windows" },
65   { SDLK_MODE,     "Mode Shift" },
66   { SDLK_HELP,     "Help" },
67   { SDLK_PRINT,     "Print Screen" },
68   { SDLK_SYSREQ,     "Sys Rq" },
69   { SDLK_BREAK,     "Break" },
70   { SDLK_MENU,     "Menu" },
71   { SDLK_POWER,     "Power" },
72   { SDLK_EURO,     "Euro" }
73 };
74 
75 static const int sdlKeyDictionnarySize = sizeof(sdlKeyDictionnary) / sizeof(SdlKeyName);
76 
77 static SDL_Joystick *joystick[16];
78 static int numJoysticks;
79 static int axisSave[16][16];
80 
81 /* KEY Input */
82 
KeyInputSwitch(int keysym,bool isup)83 KeyInputSwitch::KeyInputSwitch(int keysym, bool isup) : InputSwitch(isup), keysym(keysym) {
84   keyName[0] = 0;
85 }
86 
name() const87 const char *KeyInputSwitch::name() const {
88 
89   if (keyName[0] == 0) {
90     for (int i = 0 ; i < sdlKeyDictionnarySize ; i++) {
91       if (sdlKeyDictionnary[i].key == keysym) {
92         strcpy(keyName, sdlKeyDictionnary[i].name);
93         break;
94       }
95     }
96     if (keyName[0] == 0) {
97       keyName[0] = (char)keysym;
98       keyName[1] = 0;
99     }
100   }
101   return keyName;
102 }
103 
id() const104 int KeyInputSwitch::id() const {
105   return (1000 - keysym);
106 }
107 
isArrowUp() const108 bool KeyInputSwitch::isArrowUp()   const {
109   return keysym == SDLK_UP;
110 }
isArrowDown() const111 bool KeyInputSwitch::isArrowDown() const {
112   return keysym == SDLK_DOWN;
113 }
isValidate() const114 bool KeyInputSwitch::isValidate()  const {
115   return keysym == SDLK_RETURN;
116 }
isCancel() const117 bool KeyInputSwitch::isCancel()    const {
118   return keysym == SDLK_ESCAPE;
119 }
isPause() const120 bool KeyInputSwitch::isPause()    const {
121   return keysym == SDLK_p;
122 }
123 
124 
125 /* JOY BUTTON */
126 
JoystickSwitch(int which,int button,bool isup)127 JoystickSwitch::JoystickSwitch(int which, int button, bool isup)
128 : InputSwitch(isup),which(which),button(button)
129 {
130   keyName[0] = 0;
131 }
132 
name() const133 const char *JoystickSwitch::name() const {
134 
135   if (keyName[0] == 0)
136     sprintf(keyName, "JOY%d - Button %d", which, button);
137   return keyName;
138 }
139 
id() const140 int JoystickSwitch::id() const {
141   return 2000 + which * 50 + button;
142 }
143 
isValidate() const144 bool JoystickSwitch::isValidate()  const {
145   return button == 0;
146 }
isCancel() const147 bool JoystickSwitch::isCancel()    const {
148   return button == 2;
149 }
150 
151 /* JOY AXIS SWITCH */
152 
JoystickAxisSwitch(int which,int axis,bool maximum,bool isup)153 JoystickAxisSwitch::JoystickAxisSwitch(int which, int axis, bool maximum, bool isup)
154   : InputSwitch(isup),which(which),axis(axis),maximum(maximum)
155 {
156   keyName[0] = 0;
157 }
158 
name() const159 const char *JoystickAxisSwitch::name() const {
160 
161   if (keyName[0] == 0)
162     sprintf(keyName, "JOY%d - Axis %d - %s", which, axis, (maximum?"Max":"Min"));
163   return keyName;
164 }
165 
id() const166 int JoystickAxisSwitch::id() const {
167   return 3000 + which * 50 + axis * 2 + (maximum?1:0);
168 }
169 
isArrowUp() const170 bool JoystickAxisSwitch::isArrowUp()   const {
171   return (axis == 1) && (!maximum);
172 }
isArrowDown() const173 bool JoystickAxisSwitch::isArrowDown() const {
174   return (axis == 1) && (maximum);
175 }
176 /** EVENT HANDLERS */
177 
waitInputSwitch()178 InputSwitch *waitInputSwitch()
179 {
180   SDL_Event e;
181   if (!SDL_WaitEvent(&e))
182     return NULL;
183 
184   return switchForEvent(&e);
185 }
186 
checkInputSwitch()187 InputSwitch *checkInputSwitch()
188 {
189   SDL_Event e;
190   if (!SDL_PollEvent(&e))
191     return NULL;
192 
193   return switchForEvent(&e);
194 }
195 
196 
197 
switchForEvent(SDL_Event * e)198 InputSwitch *switchForEvent(SDL_Event *e)
199 {
200   int prevaxis;
201   switch (e->type)
202   {
203     case SDL_JOYBUTTONDOWN:
204       return new JoystickSwitch(e->jbutton.which, e->jbutton.button, false);
205 
206     case SDL_JOYBUTTONUP:
207       return new JoystickSwitch(e->jbutton.which, e->jbutton.button, true);
208 
209     case SDL_JOYAXISMOTION:
210       prevaxis = axisSave[e->jaxis.which][e->jaxis.axis];
211       axisSave[e->jaxis.which][e->jaxis.axis] = e->jaxis.value;
212 
213       if ((e->jaxis.value > JOYSTICK_THRESHOLD) && (prevaxis <= JOYSTICK_THRESHOLD))
214           return new JoystickAxisSwitch(e->jaxis.which, e->jaxis.axis, true, false);
215       if ((e->jaxis.value <= JOYSTICK_THRESHOLD) && (prevaxis > JOYSTICK_THRESHOLD))
216           return new JoystickAxisSwitch(e->jaxis.which, e->jaxis.axis, true, true);
217       if ((e->jaxis.value < -JOYSTICK_THRESHOLD) && (prevaxis >= -JOYSTICK_THRESHOLD))
218           return new JoystickAxisSwitch(e->jaxis.which, e->jaxis.axis, false, false);
219       if ((e->jaxis.value >= -JOYSTICK_THRESHOLD) && (prevaxis < -JOYSTICK_THRESHOLD))
220           return new JoystickAxisSwitch(e->jaxis.which, e->jaxis.axis, false, true);
221 
222       return NULL;
223 
224     case SDL_KEYDOWN:
225       return new KeyInputSwitch(e->key.keysym.sym, false);
226     case SDL_KEYUP:
227       return new KeyInputSwitch(e->key.keysym.sym, true);
228     default:
229       return NULL;
230   }
231 }
232 
233 
initControllers()234 void initControllers()
235 {
236   numJoysticks = SDL_NumJoysticks();
237   for( int i=0 ; i < numJoysticks ; i++ )
238   {
239     joystick[numJoysticks - i - 1] = SDL_JoystickOpen(i);
240   }
241   SDL_JoystickEventState(SDL_ENABLE);
242 
243   for (int i = 0 ; i < 16 ; i++)
244     for (int j = 0 ; j < 16 ; j++)
245       axisSave[i][j] = 0;
246 }
247 
closeControllers()248 void closeControllers()
249 {
250   for( int i=0 ; i < numJoysticks ; i++ )
251   {
252     SDL_JoystickClose(joystick[i]);
253   }
254 }
255