Debian Codegraph @

src/cdogs/SDL_JoystickButtonNames/SDL_joystickbuttonnames.c

raw symbols SCIP
1 /*
2 Copyright (c) 2015, Cong Xu
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6
7 Redistributions of source code must retain the above copyright notice, this
8 list of conditions and the following disclaimer.
9 Redistributions in binary form must reproduce the above copyright notice,
10 this list of conditions and the following disclaimer in the documentation
11 and/or other materials provided with the distribution.
12
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 POSSIBILITY OF SUCH DAMAGE.
24 */
25 #include "SDL_joystickbuttonnames.h"
26
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <SDL_pixels.h>
33
34
35 typedef struct
36 {
37 SDL_JoystickGUID guid;
38 char joystickName[256];
39 char buttonNames[SDL_CONTROLLER_BUTTON_MAX][256];
40 SDL_Color buttonColors[SDL_CONTROLLER_BUTTON_MAX];
41 char axisNames[SDL_CONTROLLER_AXIS_MAX][256];
42 SDL_Color axisColors[SDL_CONTROLLER_AXIS_MAX];
43 } JoystickButtonNames;
44
45 const char *err = NULL;
46 JoystickButtonNames *jbn = NULL;
47 int nJBN = 0;
48 JoystickButtonNames jDefault;
49 #include "db.h"
50
51
52 static JoystickButtonNames DefaultJoystickButtonNames(void);
53 static int ReadMappingsString(const char *s);
54
55 int SDLJBN_Init(void)
56 {
57 // Don't reinitialise
58 if (jbn != NULL) return 0;
59
60 jDefault = DefaultJoystickButtonNames();
61
62 ReadMappingsString(db);
63
64 return 0;
65 }
66
67 void SDLJBN_Quit(void)
68 {
69 SDL_free(jbn);
70 jbn = NULL;
71 }
72
73 int SDLJBN_AddMappingsFromFile(const char *file)
74 {
75 SDL_RWops *rwops = SDL_RWFromFile(file, "r");
76 if (rwops == NULL)
77 {
78 err = "Cannot open file";
79 return -1;
80 }
81 return SDLJBN_AddMappingsFromRW(rwops, 1);
82 }
83
84 int SDLJBN_AddMappingsFromRW(SDL_RWops *rwops, int freerw)
85 {
86 int ret = 0;
87 char *s = NULL;
88
89 SDLJBN_Init();
90 if (rwops == NULL)
91 {
92 ret = -1;
93 goto bail;
94 }
95
96 // Read file into memory
97 const Sint64 fsize = rwops->size(rwops);
98 if (fsize == -1)
99 {
100 err = "Cannot find file size";
101 ret = -1;
102 goto bail;
103 }
104 s = SDL_malloc((size_t)fsize + 1);
105 if (s == NULL)
106 {
107 err = "Out of memory";
108 ret = -1;
109 goto bail;
110 }
111 if (SDL_RWread(rwops, s, (size_t)fsize, 1) == 0)
112 {
113 err = "Cannot read file";
114 ret = -1;
115 goto bail;
116 }
117 s[fsize] = '\0';
118
119 ret = ReadMappingsString(s);
120
121 bail:
122 if (rwops != NULL && freerw)
123 {
124 SDL_RWclose(rwops);
125 }
126 SDL_free(s);
127 return ret;
128 }
129
130 static int ReadMappingsString(const char *s)
131 {
132 #define READ_TOKEN(buf, p, end)\
133 if (end == NULL)\
134 {\
135 strcpy(buf, p);\
136 p = NULL;\
137 }\
138 else\
139 {\
140 strncpy(buf, p, end - p);\
141 buf[end - p] = '\0';\
142 p = end + 1;\
143 }
144
145 // Read compiled string button names into memory
146 // Search for a matching GUID + joystickName in the db
147 int read = 0;
148 for (const char *cur = s; cur;)
149 {
150 const char *nl = strchr(cur, '\n');
151 char line[2048];
152 READ_TOKEN(line, cur, nl);
153
154 char buf[256];
155
156 // Check for the platform string
157 sprintf(buf, "platform:%s", SDL_GetPlatform());
158 if (strstr(line, buf) == NULL) continue;
159
160 #define STR_NOT_EQ(expected, actualP, actualEnd)\
161 strlen(expected) != (actualEnd) - (actualP) || \
162 strncmp(expected, actualP, (actualEnd) - (actualP)) != 0
163 const char *curL = line;
164 // Ignore hash comments
165 if (*curL == '#') continue;
166
167 const char *nextComma;
168 JoystickButtonNames j = jDefault;
169
170 // Read GUID
171 nextComma = strchr(curL, ',');
172 if (nextComma == NULL || cur == nextComma) continue;
173 READ_TOKEN(buf, curL, nextComma);
174 j.guid = SDL_JoystickGetGUIDFromString(buf);
175
176 // Read joystick name
177 nextComma = strchr(curL, ',');
178 if (nextComma == NULL || curL == nextComma) continue;
179 READ_TOKEN(j.joystickName, curL, nextComma);
180
181 // Check if GUID+joystick name already exists
182 bool exists = false;
183 for (int i = 0; i < nJBN; i++)
184 {
185 const JoystickButtonNames *jp = jbn + i;
186 if (memcmp(&jp->guid, &j.guid, sizeof j.guid) == 0 &&
187 strcmp(jp->joystickName, j.joystickName) == 0)
188 {
189 exists = true;
190 break;
191 }
192 }
193 if (exists) continue;
194
195 // Read name and colors
196 for (;; curL = nextComma + 1)
197 {
198 nextComma = strchr(curL, ',');
199 if (nextComma == NULL) break;
200
201 const char *nextColon;
202
203 nextColon = strchr(curL, ':');
204 if (nextColon == NULL || curL == nextColon) continue;
205 READ_TOKEN(buf, curL, nextColon);
206 const SDL_GameControllerButton button =
207 SDL_GameControllerGetButtonFromString(buf);
208 const SDL_GameControllerAxis axis =
209 SDL_GameControllerGetAxisFromString(buf);
210 char *name;
211 SDL_Color *color;
212 if (button != SDL_CONTROLLER_BUTTON_INVALID)
213 {
214 name = j.buttonNames[(int)button];
215 color = &j.buttonColors[(int)button];
216 }
217 else if (axis != SDL_CONTROLLER_AXIS_INVALID)
218 {
219 name = j.axisNames[(int)axis];
220 color = &j.axisColors[(int)axis];
221 }
222 else
223 {
224 continue;
225 }
226 // Read the real button/axis name
227 nextColon = strchr(curL, ':');
228 if (nextColon == NULL) continue;
229 READ_TOKEN(name, curL, nextColon);
230 // R
231 nextColon = strchr(curL, ':');
232 if (nextColon == NULL) continue;
233 READ_TOKEN(buf, curL, nextColon);
234 color->r = (Uint8)atoi(buf);
235 // G
236 nextColon = strchr(curL, ':');
237 if (nextColon == NULL) continue;
238 READ_TOKEN(buf, curL, nextColon);
239 color->g = (Uint8)atoi(buf);
240 // B
241 READ_TOKEN(buf, curL, nextComma);
242 color->b = (Uint8)atoi(buf);
243
244 color->a = 255;
245 }
246 nJBN++;
247 read++;
248 jbn = SDL_realloc(jbn, nJBN * sizeof *jbn);
249 memcpy(jbn + nJBN - 1, &j, sizeof j);
250 }
251 return read;
252 }
253
254 static const char *DefaultButtonName(SDL_GameControllerButton button);
255 static const char *DefaultAxisName(SDL_GameControllerAxis axis);
256 static SDL_Color DefaultButtonColor(SDL_GameControllerButton button);
257 static SDL_Color DefaultAxisColor(SDL_GameControllerAxis axis);
258
259 static JoystickButtonNames DefaultJoystickButtonNames(void)
260 {
261 JoystickButtonNames j;
262 memset(&j, 0, sizeof j);
263 for (SDL_GameControllerButton button = SDL_CONTROLLER_BUTTON_A;
264 button < SDL_CONTROLLER_BUTTON_MAX;
265 button++)
266 {
267 strcpy(j.buttonNames[(int)button], DefaultButtonName(button));
268 j.buttonColors[(int)button] = DefaultButtonColor(button);
269 }
270 for (SDL_GameControllerAxis axis = SDL_CONTROLLER_AXIS_LEFTX;
271 axis < SDL_CONTROLLER_AXIS_MAX;
272 axis++)
273 {
274 strcpy(j.axisNames[(int)axis], DefaultAxisName(axis));
275 j.axisColors[(int)axis] = DefaultAxisColor(axis);
276 }
277 return j;
278 }
279
280 static const char *DefaultButtonName(SDL_GameControllerButton button)
281 {
282 switch (button)
283 {
284 case SDL_CONTROLLER_BUTTON_A: return "A";
285 case SDL_CONTROLLER_BUTTON_B: return "B";
286 case SDL_CONTROLLER_BUTTON_X: return "X";
287 case SDL_CONTROLLER_BUTTON_Y: return "Y";
288 case SDL_CONTROLLER_BUTTON_BACK: return "Back";
289 case SDL_CONTROLLER_BUTTON_GUIDE: return "Guide";
290 case SDL_CONTROLLER_BUTTON_START: return "Start";
291 case SDL_CONTROLLER_BUTTON_LEFTSTICK: return "Left Stick";
292 case SDL_CONTROLLER_BUTTON_RIGHTSTICK: return "Right Stick";
293 case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: return "LB";
294 case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: return "RB";
295 case SDL_CONTROLLER_BUTTON_DPAD_UP: return "D-pad Up";
296 case SDL_CONTROLLER_BUTTON_DPAD_DOWN: return "D-pad Down";
297 case SDL_CONTROLLER_BUTTON_DPAD_LEFT: return "D-pad Left";
298 case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: return "D-pad Right";
299 default: return "";
300 }
301 }
302
303 static const char *DefaultAxisName(SDL_GameControllerAxis axis)
304 {
305 switch (axis)
306 {
307 case SDL_CONTROLLER_AXIS_LEFTX: return "Left Stick X";
308 case SDL_CONTROLLER_AXIS_LEFTY: return "Left Stick Y";
309 case SDL_CONTROLLER_AXIS_RIGHTX: return "Right Stick X";
310 case SDL_CONTROLLER_AXIS_RIGHTY: return "Right Stick Y";
311 case SDL_CONTROLLER_AXIS_TRIGGERLEFT: return "LT";
312 case SDL_CONTROLLER_AXIS_TRIGGERRIGHT: return "RT";
313 default: return "";
314 }
315 }
316
317 static SDL_Color NewColor(Uint8 r, Uint8 g, Uint8 b);
318
319 static SDL_Color DefaultButtonColor(SDL_GameControllerButton button)
320 {
321 // Default colors for Xbox 360 controller
322 switch (button)
323 {
324 case SDL_CONTROLLER_BUTTON_A: return NewColor(96, 160, 0);
325 case SDL_CONTROLLER_BUTTON_B: return NewColor(240, 0, 0);
326 case SDL_CONTROLLER_BUTTON_X: return NewColor(0, 96, 208);
327 case SDL_CONTROLLER_BUTTON_Y: return NewColor(255, 160, 0);
328 case SDL_CONTROLLER_BUTTON_BACK: return NewColor(224, 224, 224);
329 case SDL_CONTROLLER_BUTTON_GUIDE: return NewColor(128, 176, 0);
330 case SDL_CONTROLLER_BUTTON_START: return NewColor(224, 224, 224);
331 case SDL_CONTROLLER_BUTTON_LEFTSTICK: return NewColor(96, 128, 128);
332 case SDL_CONTROLLER_BUTTON_RIGHTSTICK: return NewColor(96, 128, 128);
333 case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: return NewColor(224, 224, 224);
334 case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: return NewColor(224, 224, 224);
335 case SDL_CONTROLLER_BUTTON_DPAD_UP: return NewColor(96, 128, 128);
336 case SDL_CONTROLLER_BUTTON_DPAD_DOWN: return NewColor(96, 128, 128);
337 case SDL_CONTROLLER_BUTTON_DPAD_LEFT: return NewColor(96, 128, 128);
338 case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: return NewColor(96, 128, 128);
339 default: return NewColor(0, 0, 0);
340 }
341 }
342
343 static SDL_Color DefaultAxisColor(SDL_GameControllerAxis axis)
344 {
345 // Default colors for Xbox 360 controller
346 switch (axis)
347 {
348 case SDL_CONTROLLER_AXIS_LEFTX: return NewColor(96, 128, 128);
349 case SDL_CONTROLLER_AXIS_LEFTY: return NewColor(96, 128, 128);
350 case SDL_CONTROLLER_AXIS_RIGHTX: return NewColor(96, 128, 128);
351 case SDL_CONTROLLER_AXIS_RIGHTY: return NewColor(96, 128, 128);
352 case SDL_CONTROLLER_AXIS_TRIGGERLEFT: return NewColor(224, 224, 224);
353 case SDL_CONTROLLER_AXIS_TRIGGERRIGHT: return NewColor(224, 224, 224);
354 default: return NewColor(0, 0, 0);
355 }
356 }
357
358 int SDLJBN_GetButtonNameAndColor(SDL_Joystick *joystick,
359 SDL_GameControllerButton button,
360 char *s, Uint8 *r, Uint8 *g, Uint8 *b)
361 {
362 SDLJBN_Init();
363
364 if (joystick == NULL)
365 {
366 err = "joystick is NULL";
367 return -1;
368 }
369 if (button < SDL_CONTROLLER_BUTTON_A ||
370 button >= SDL_CONTROLLER_BUTTON_MAX)
371 {
372 err = "button is invalid";
373 return -1;
374 }
375 // Use defaults first
376 if (s) strcpy(s, jDefault.buttonNames[(int)button]);
377 if (r) *r = jDefault.buttonColors[(int)button].r;
378 if (g) *g = jDefault.buttonColors[(int)button].g;
379 if (b) *b = jDefault.buttonColors[(int)button].b;
380
381 const SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
382 const char *joystickName = SDL_JoystickName(joystick);
383 // Search for a matching GUID + joystickName in the db
384 for (int i = 0; i < nJBN; i++)
385 {
386 const JoystickButtonNames *j = jbn + i;
387 if (memcmp(&j->guid, &guid, sizeof guid) == 0 &&
388 strcmp(j->joystickName, joystickName) == 0)
389 {
390 // GUID and joystick name match; read name and colors
391 if (s && strlen(j->buttonNames[(int)button]) > 0)
392 strcpy(s, j->buttonNames[(int)button]);
393 if (r) *r = j->buttonColors[(int)button].r;
394 if (g) *g = j->buttonColors[(int)button].g;
395 if (b) *b = j->buttonColors[(int)button].b;
396 break;
397 }
398 }
399
400 return 0;
401 }
402
403
404 int SDLJBN_GetAxisNameAndColor(SDL_Joystick *joystick,
405 SDL_GameControllerAxis axis,
406 char *s, Uint8 *r, Uint8 *g, Uint8 *b)
407 {
408 SDLJBN_Init();
409
410 if (joystick == NULL)
411 {
412 err = "joystick is NULL";
413 return -1;
414 }
415 if (axis < SDL_CONTROLLER_AXIS_LEFTX || axis >= SDL_CONTROLLER_AXIS_MAX)
416 {
417 err = "axis is invalid";
418 return -1;
419 }
420 // Use defaults first
421 if (s) strcpy(s, jDefault.axisNames[(int)axis]);
422 if (r) *r = jDefault.axisColors[(int)axis].r;
423 if (g) *g = jDefault.axisColors[(int)axis].g;
424 if (b) *b = jDefault.axisColors[(int)axis].b;
425
426 const SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
427 const char *joystickName = SDL_JoystickName(joystick);
428 // Search for a matching GUID + joystickName in the db
429 for (int i = 0; i < nJBN; i++)
430 {
431 const JoystickButtonNames *j = jbn + i;
432 if (memcmp(&j->guid, &guid, sizeof guid) == 0 &&
433 strcmp(j->joystickName, joystickName) == 0)
434 {
435 // GUID and joystick name match; read name and colors
436 if (s && strlen(j->axisNames[(int)axis]) > 0)
437 strcpy(s, j->axisNames[(int)axis]);
438 if (r) *r = j->axisColors[(int)axis].r;
439 if (g) *g = j->axisColors[(int)axis].g;
440 if (b) *b = j->axisColors[(int)axis].b;
441 break;
442 }
443 }
444
445 return 0;
446 }
447
448 static SDL_Color NewColor(Uint8 r, Uint8 g, Uint8 b)
449 {
450 SDL_Color c;
451 c.r = r;
452 c.g = g;
453 c.b = b;
454 c.a = 255;
455 return c;
456 }
457
458 const char *SDLJBN_GetError(void)
459 {
460 return err;
461 }