Debian Codegraph @

src/cdogs/joystick.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2013-2015, 2018, 2024 Cong Xu
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29 #include "joystick.h"
30
31 #include <string.h>
32 #include <stdio.h>
33
34 #include "defs.h"
35 #include "events.h"
36 #include "log.h"
37 #include "SDL_JoystickButtonNames/SDL_joystickbuttonnames.h"
38
39
40 void JoyInit(CArray *joys)
41 {
42 char buf[CDOGS_PATH_MAX];
43 GetDataFilePath(buf, "data/gamecontrollerdb.txt");
44 if (SDL_GameControllerAddMappingsFromFile(buf) == -1)
45 {
46 LOG(LM_INPUT, LL_ERROR, "cannot load controller mappings file: %s",
47 SDL_GetError());
48 }
49 GetDataFilePath(buf, "data/gamecontrollerbuttondb.txt");
50 if (SDLJBN_AddMappingsFromFile(buf) == -1)
51 {
52 LOG(LM_INPUT, LL_ERROR, "cannot load button mappings file: %s",
53 SDLJBN_GetError());
54 }
55
56 CArrayInit(joys, sizeof(Joystick));
57
58 // Detect all current controllers
59 const int n = SDL_NumJoysticks();
60 LOG(LM_INPUT, LL_INFO, "%d controllers found", n);
61 if (n == 0)
62 {
63 return;
64 }
65 for (int i = 0; i < n; i++)
66 {
67 JoyAdded(i);
68 }
69 }
70
71 void JoyReset(CArray *joys)
72 {
73 CA_FOREACH(Joystick, j, *joys)
74 j->currentCmd = j->pressedCmd = j->previousCmd = 0;
75 CA_FOREACH_END()
76 }
77
78 void JoyLock(CArray *joys)
79 {
80 CA_FOREACH(Joystick, j, *joys)
81 j->lockedCmd = j->currentCmd;
82 CA_FOREACH_END()
83 }
84
85 static void JoyTerminateOne(Joystick *j)
86 {
87 SDL_GameControllerClose(j->gc);
88 SDL_HapticClose(j->haptic);
89 }
90
91 void JoyTerminate(CArray *joys)
92 {
93 CA_FOREACH(Joystick, j, *joys)
94 JoyTerminateOne(j);
95 CA_FOREACH_END()
96 CArrayTerminate(joys);
97 }
98
99 static Joystick *GetJoystick(const SDL_JoystickID id)
100 {
101 CA_FOREACH(Joystick, j, gEventHandlers.joysticks)
102 if (j->id == id) return j;
103 CA_FOREACH_END()
104 return NULL;
105 }
106
107 bool JoyIsDown(const SDL_JoystickID id, const int cmd)
108 {
109 const Joystick *j = GetJoystick(id);
110 return !!(j->currentCmd & ~j->lockedCmd & cmd);
111 }
112
113 bool JoyIsPressed(const SDL_JoystickID id, const int cmd)
114 {
115 return JoyIsDown(id, cmd) && !(GetJoystick(id)->previousCmd & cmd);
116 }
117
118 int JoyGetPressed(const SDL_JoystickID id)
119 {
120 const Joystick *j = GetJoystick(id);
121 // Pressed is current and not previous, so take bitwise complement
122 return j->currentCmd & ~j->previousCmd;
123 }
124
125 void JoyPrePoll(CArray *joys)
126 {
127 CA_FOREACH(Joystick, j, *joys)
128 j->pressedCmd = 0;
129 j->previousCmd = j->currentCmd;
130 CA_FOREACH_END()
131 }
132
133 SDL_JoystickID JoyAdded(const Sint32 which)
134 {
135 if (!SDL_IsGameController(which))
136 {
137 return -1;
138 }
139
140 Joystick j;
141 memset(&j, 0, sizeof j);
142 j.hapticEffectId = -1;
143 j.gc = SDL_GameControllerOpen(which);
144 if (j.gc == NULL)
145 {
146 LOG(LM_INPUT, LL_ERROR, "Failed to open game controller: %s",
147 SDL_GetError());
148 return -1;
149 }
150 j.j = SDL_GameControllerGetJoystick(j.gc);
151 if (j.j == NULL)
152 {
153 LOG(LM_INPUT, LL_ERROR, "Failed to open joystick: %s",
154 SDL_GetError());
155 return -1;
156 }
157 j.id = SDL_JoystickInstanceID(j.j);
158 if (j.id == -1)
159 {
160 LOG(LM_INPUT, LL_ERROR, "Failed to get joystick instance ID: %s",
161 SDL_GetError());
162 return -1;
163 }
164 const int isHaptic = SDL_JoystickIsHaptic(j.j);
165 if (isHaptic > 0)
166 {
167 j.haptic = SDL_HapticOpenFromJoystick(j.j);
168 if (j.haptic == NULL)
169 {
170 LOG(LM_INPUT, LL_ERROR, "Failed to open haptic: %s",
171 SDL_GetError());
172 }
173 else
174 {
175 if (SDL_HapticRumbleInit(j.haptic) != 0)
176 {
177 LOG(LM_INPUT, LL_ERROR, "Failed to init rumble: %s",
178 SDL_GetError());
179 }
180 const int hapticQuery = SDL_HapticQuery(j.haptic);
181 LOG(LM_INPUT, LL_INFO, "Haptic support: %x", hapticQuery);
182 if (hapticQuery & SDL_HAPTIC_CONSTANT)
183 {
184 SDL_HapticEffect he;
185 memset(&he, 0, sizeof he);
186 he.type = SDL_HAPTIC_CONSTANT;
187 he.constant.length = 100;
188 he.constant.level = 20000; // 20000/32767 strength
189 j.hapticEffectId = SDL_HapticNewEffect(j.haptic, &he);
190 if (j.hapticEffectId == -1)
191 {
192 LOG(LM_INPUT, LL_ERROR,
193 "Failed to create haptic effect: %s", SDL_GetError());
194 }
195 }
196 }
197 }
198 else if (isHaptic < 0)
199 {
200 LOG(LM_INPUT, LL_ERROR, "Failed to query haptic: %s", SDL_GetError());
201 }
202 CArrayPushBack(&gEventHandlers.joysticks, &j);
203 LOG(LM_INPUT, LL_INFO, "Added joystick index %d id %d", which, j.id);
204 return j.id;
205 }
206 void JoyRemoved(const Sint32 which)
207 {
208 LOG(LM_INPUT, LL_INFO, "Removed joystick id %d", which);
209 CA_FOREACH(Joystick, j, gEventHandlers.joysticks)
210 if (j->id == which)
211 {
212 JoyTerminateOne(j);
213 CArrayDelete(&gEventHandlers.joysticks, _ca_index);
214 return;
215 }
216 CA_FOREACH_END()
217 CASSERT(false, "Cannot find joystick");
218 }
219
220 static void JoyOnCmd(Joystick *j, const int cmd, const bool isDown);
221
222 int ControllerButtonToCmd(const Uint8 button);
223 void JoyOnButtonDown(const SDL_ControllerButtonEvent e)
224 {
225 LOG(LM_INPUT, LL_DEBUG, "Joystick %d button down %d", e.which, e.button);
226 JoyOnCmd(GetJoystick(e.which), ControllerButtonToCmd(e.button), true);
227 }
228 void JoyOnButtonUp(const SDL_ControllerButtonEvent e)
229 {
230 LOG(LM_INPUT, LL_DEBUG, "Joystick %d button up %d", e.which, e.button);
231 JoyOnCmd(GetJoystick(e.which), ControllerButtonToCmd(e.button), false);
232 }
233 int ControllerButtonToCmd(const Uint8 button)
234 {
235 switch (button)
236 {
237 case SDL_CONTROLLER_BUTTON_A: return CMD_BUTTON1;
238 case SDL_CONTROLLER_BUTTON_B: return CMD_BUTTON2;
239 case SDL_CONTROLLER_BUTTON_BACK: return CMD_MAP;
240 case SDL_CONTROLLER_BUTTON_START: return CMD_ESC;
241 case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: return CMD_GRENADE;
242 case SDL_CONTROLLER_BUTTON_DPAD_UP: return CMD_UP;
243 case SDL_CONTROLLER_BUTTON_DPAD_DOWN: return CMD_DOWN;
244 case SDL_CONTROLLER_BUTTON_DPAD_LEFT: return CMD_LEFT;
245 case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: return CMD_RIGHT;
246 default: return 0;
247 }
248 }
249 #define DEADZONE 16384
250 void JoyOnAxis(const SDL_ControllerAxisEvent e)
251 {
252 LOG(LM_INPUT, LL_DEBUG, "Joystick %d received axis %d, %d",
253 e.which, e.axis, e.value);
254 Joystick *j = GetJoystick(e.which);
255 switch (e.axis)
256 {
257 case SDL_CONTROLLER_AXIS_LEFTX:
258 JoyOnCmd(j, CMD_LEFT, e.value < -DEADZONE);
259 JoyOnCmd(j, CMD_RIGHT, e.value > DEADZONE);
260 break;
261 case SDL_CONTROLLER_AXIS_LEFTY:
262 JoyOnCmd(j, CMD_UP, e.value < -DEADZONE);
263 JoyOnCmd(j, CMD_DOWN, e.value > DEADZONE);
264 break;
265 case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
266 // Right trigger fire
267 JoyOnCmd(j, CMD_BUTTON1, e.value > DEADZONE);
268 break;
269 default:
270 // Ignore axis
271 break;
272 }
273 }
274
275 static void JoyOnCmd(Joystick *j, const int cmd, const bool isDown)
276 {
277 if (isDown)
278 {
279 j->currentCmd |= cmd;
280 }
281 else
282 {
283 if ((j->currentCmd & cmd) && !(j->previousCmd & cmd))
284 {
285 j->pressedCmd |= cmd;
286 }
287 j->currentCmd &= ~cmd;
288 j->lockedCmd &= ~cmd;
289 }
290 }
291
292 void JoyRumble(
293 const SDL_JoystickID id, const float strength, const Uint32 length)
294 {
295 Joystick *j = GetJoystick(id);
296 if (j->haptic == NULL) return;
297 if (SDL_HapticRumblePlay(j->haptic, strength, length) < 0)
298 {
299 LOG(LM_INPUT, LL_ERROR, "Failed to rumble: %s", SDL_GetError());
300 }
301 }
302 void JoyImpact(const SDL_JoystickID id)
303 {
304 Joystick *j = GetJoystick(id);
305 if (j->hapticEffectId != -1 &&
306 SDL_HapticRunEffect(j->haptic, j->hapticEffectId, 1) != 0)
307 {
308 LOG(LM_INPUT, LL_ERROR, "Failed to run haptic effect: %s",
309 SDL_GetError());
310 }
311 }
312
313 const char *JoyName(const SDL_JoystickID id)
314 {
315 const Joystick *j = GetJoystick(id);
316 const char *controllerName = SDL_GameControllerName(j->gc);
317 const char *joystickName = SDL_JoystickName(j->j);
318 // Try to use the more specific name
319 if (joystickName != NULL &&
320 strcmp(controllerName, "Generic DirectInput Controller") == 0)
321 {
322 return joystickName;
323 }
324 return controllerName;
325 }
326
327 static int CmdToControllerButton(const int cmd);
328 void JoyButtonNameColor(
329 const SDL_JoystickID id, const int cmd, char *buf, color_t *color)
330 {
331 Joystick *j = GetJoystick(id);
332 const int button = CmdToControllerButton(cmd);
333 int res;
334 if (color != NULL)
335 {
336 res = SDLJBN_GetButtonNameAndColor(
337 j->j, button, buf, &color->r, &color->g, &color->b);
338 }
339 else
340 {
341 res = SDLJBN_GetButtonName(j->j, button, buf);
342 }
343 if (res < 0)
344 {
345 LOG(LM_INPUT, LL_WARN, "Could not get button name/colour: %s",
346 SDLJBN_GetError());
347 strcpy(buf, "?");
348 }
349 }
350 static int CmdToControllerButton(const int cmd)
351 {
352 switch (cmd)
353 {
354 case CMD_BUTTON1: return SDL_CONTROLLER_BUTTON_A;
355 case CMD_BUTTON2: return SDL_CONTROLLER_BUTTON_B;
356 case CMD_GRENADE: return SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
357 case CMD_MAP: return SDL_CONTROLLER_BUTTON_BACK;
358 case CMD_ESC: return SDL_CONTROLLER_BUTTON_START;
359 case CMD_UP: return SDL_CONTROLLER_BUTTON_DPAD_UP;
360 case CMD_DOWN: return SDL_CONTROLLER_BUTTON_DPAD_DOWN;
361 case CMD_LEFT: return SDL_CONTROLLER_BUTTON_DPAD_LEFT;
362 case CMD_RIGHT: return SDL_CONTROLLER_BUTTON_DPAD_RIGHT;
363 default: return SDL_CONTROLLER_BUTTON_INVALID;
364 }
365 }