| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 146 |
|
| 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 |
|
| 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 |
|
| 165 |
if (*curL == '#') continue; |
| 166 |
|
| 167 |
const char *nextComma; |
| 168 |
JoystickButtonNames j = jDefault; |
| 169 |
|
| 170 |
|
| 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 |
|
| 177 |
nextComma = strchr(curL, ','); |
| 178 |
if (nextComma == NULL || curL == nextComma) continue; |
| 179 |
READ_TOKEN(j.joystickName, curL, nextComma); |
| 180 |
|
| 181 |
|
| 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 |
|
| 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 |
|
| 227 |
nextColon = strchr(curL, ':'); |
| 228 |
if (nextColon == NULL) continue; |
| 229 |
READ_TOKEN(name, curL, nextColon); |
| 230 |
|
| 231 |
nextColon = strchr(curL, ':'); |
| 232 |
if (nextColon == NULL) continue; |
| 233 |
READ_TOKEN(buf, curL, nextColon); |
| 234 |
color->r = (Uint8)atoi(buf); |
| 235 |
|
| 236 |
nextColon = strchr(curL, ':'); |
| 237 |
if (nextColon == NULL) continue; |
| 238 |
READ_TOKEN(buf, curL, nextColon); |
| 239 |
color->g = (Uint8)atoi(buf); |
| 240 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |