|
25 | 25 | #include "bflib_dernc.h" |
26 | 26 | #include "bflib_keybrd.h" |
27 | 27 | #include "bflib_video.h" |
| 28 | +#include "bflib_joyst.h" |
28 | 29 | #include "frontmenu_options.h" |
29 | 30 | #include "config.h" |
30 | 31 | #include "front_input.h" |
@@ -158,6 +159,40 @@ static const struct { unsigned char code; const char *name; } keycode_table[] = |
158 | 159 | }; |
159 | 160 | #define KEYCODE_TABLE_SIZE ((int)(sizeof(keycode_table)/sizeof(keycode_table[0]))) |
160 | 161 |
|
| 162 | +static const struct { TbControllerButtons button; const char *name; } controller_button_table[] = { |
| 163 | + { CBtn_A, "A" }, |
| 164 | + { CBtn_B, "B" }, |
| 165 | + { CBtn_X, "X" }, |
| 166 | + { CBtn_Y, "Y" }, |
| 167 | + { CBtn_BACK, "BACK" }, |
| 168 | + { CBtn_START, "START" }, |
| 169 | + { CBtn_LEFTSTICK, "LEFTSTICK" }, |
| 170 | + { CBtn_RIGHTSTICK, "RIGHTSTICK" }, |
| 171 | + { CBtn_LEFTSHOULDER, "LEFTSHOULDER" }, |
| 172 | + { CBtn_RIGHTSHOULDER, "RIGHTSHOULDER" }, |
| 173 | + { CBtn_DPAD_UP, "DPAD_UP" }, |
| 174 | + { CBtn_DPAD_DOWN, "DPAD_DOWN" }, |
| 175 | + { CBtn_DPAD_LEFT, "DPAD_LEFT" }, |
| 176 | + { CBtn_DPAD_RIGHT, "DPAD_RIGHT" }, |
| 177 | + { CBtn_MISC1, "MISC1" }, |
| 178 | + { CBtn_PADDLE1, "PADDLE1" }, |
| 179 | + { CBtn_PADDLE2, "PADDLE2" }, |
| 180 | + { CBtn_PADDLE3, "PADDLE3" }, |
| 181 | + { CBtn_PADDLE4, "PADDLE4" }, |
| 182 | + { CBtn_TOUCHPAD, "TOUCHPAD" }, |
| 183 | + { CBtn_L2, "L2" }, |
| 184 | + { CBtn_R2, "R2" }, |
| 185 | + { CBtn_LS_UP, "LS_UP" }, |
| 186 | + { CBtn_LS_DOWN, "LS_DOWN" }, |
| 187 | + { CBtn_LS_LEFT, "LS_LEFT" }, |
| 188 | + { CBtn_LS_RIGHT, "LS_RIGHT" }, |
| 189 | + { CBtn_RS_UP, "RS_UP" }, |
| 190 | + { CBtn_RS_DOWN, "RS_DOWN" }, |
| 191 | + { CBtn_RS_LEFT, "RS_LEFT" }, |
| 192 | + { CBtn_RS_RIGHT, "RS_RIGHT" }, |
| 193 | +}; |
| 194 | +#define CONTROLLER_BUTTON_TABLE_SIZE ((int)(sizeof(controller_button_table)/sizeof(controller_button_table[0]))) |
| 195 | + |
161 | 196 | static const char *keycode_to_name(unsigned char code) |
162 | 197 | { |
163 | 198 | for (int i = 0; i < KEYCODE_TABLE_SIZE; i++) |
@@ -237,6 +272,90 @@ static unsigned char name_to_kmod(const char *name) |
237 | 272 | return mods; |
238 | 273 | } |
239 | 274 |
|
| 275 | +static void controller_buttons_to_name(TbControllerButtons buttons, char *buf, size_t buflen) |
| 276 | +{ |
| 277 | + size_t len = 0; |
| 278 | + |
| 279 | + if (buflen == 0) |
| 280 | + return; |
| 281 | + |
| 282 | + if (buttons == CBtn_NONE) |
| 283 | + { |
| 284 | + snprintf(buf, buflen, "NONE"); |
| 285 | + return; |
| 286 | + } |
| 287 | + |
| 288 | + buf[0] = '\0'; |
| 289 | + for (int i = 0; i < CONTROLLER_BUTTON_TABLE_SIZE; i++) |
| 290 | + { |
| 291 | + int written; |
| 292 | + |
| 293 | + if ((buttons & controller_button_table[i].button) == 0) |
| 294 | + continue; |
| 295 | + |
| 296 | + if (len >= buflen) |
| 297 | + break; |
| 298 | + |
| 299 | + written = snprintf(buf + len, buflen - len, "%s%s", (len > 0) ? "|" : "", controller_button_table[i].name); |
| 300 | + if (written < 0) |
| 301 | + break; |
| 302 | + if ((size_t)written >= buflen - len) |
| 303 | + { |
| 304 | + len = buflen; |
| 305 | + break; |
| 306 | + } |
| 307 | + len += (size_t)written; |
| 308 | + } |
| 309 | + |
| 310 | + if (len == 0) |
| 311 | + snprintf(buf, buflen, "NONE"); |
| 312 | +} |
| 313 | + |
| 314 | +static TbControllerButtons name_to_controller_buttons(const char *name) |
| 315 | +{ |
| 316 | + TbControllerButtons buttons = CBtn_NONE; |
| 317 | + char token[32]; |
| 318 | + int token_len = 0; |
| 319 | + |
| 320 | + if (name == NULL) |
| 321 | + return buttons; |
| 322 | + |
| 323 | + for (const char *p = name;; p++) |
| 324 | + { |
| 325 | + unsigned char c = (unsigned char)(*p); |
| 326 | + TbBool token_end = (c == '\0' || c == '|' || c == '+' || c == ',' || isspace(c)); |
| 327 | + |
| 328 | + if (!token_end) |
| 329 | + { |
| 330 | + if (token_len < (int)sizeof(token) - 1) |
| 331 | + token[token_len++] = (char)toupper(c); |
| 332 | + continue; |
| 333 | + } |
| 334 | + |
| 335 | + if (token_len > 0) |
| 336 | + { |
| 337 | + token[token_len] = '\0'; |
| 338 | + if (strcmp(token, "NONE") != 0) |
| 339 | + { |
| 340 | + for (int i = 0; i < CONTROLLER_BUTTON_TABLE_SIZE; i++) |
| 341 | + { |
| 342 | + if (strcmp(token, controller_button_table[i].name) == 0) |
| 343 | + { |
| 344 | + buttons |= controller_button_table[i].button; |
| 345 | + break; |
| 346 | + } |
| 347 | + } |
| 348 | + } |
| 349 | + token_len = 0; |
| 350 | + } |
| 351 | + |
| 352 | + if (c == '\0') |
| 353 | + break; |
| 354 | + } |
| 355 | + |
| 356 | + return buttons; |
| 357 | +} |
| 358 | + |
240 | 359 | #ifdef __cplusplus |
241 | 360 | } |
242 | 361 | #endif |
@@ -363,10 +482,18 @@ TbBool load_settings(void) |
363 | 482 | { |
364 | 483 | VALUE *vcode = value_dict_get(val, "code"); |
365 | 484 | VALUE *vmods = value_dict_get(val, "mods"); |
| 485 | + VALUE *vcontroller_buttons = value_dict_get(val, "controller_buttons"); |
366 | 486 | if (vcode && value_type(vcode) == VALUE_STRING) |
367 | 487 | settings.kbkeys[i].code = name_to_keycode(value_string(vcode)); |
368 | 488 | if (vmods && value_type(vmods) == VALUE_STRING) |
369 | 489 | settings.kbkeys[i].mods = name_to_kmod(value_string(vmods)); |
| 490 | + if (vcontroller_buttons) |
| 491 | + { |
| 492 | + if (value_type(vcontroller_buttons) == VALUE_STRING) |
| 493 | + settings.kbkeys[i].controller_buttons = name_to_controller_buttons(value_string(vcontroller_buttons)); |
| 494 | + else if (value_type(vcontroller_buttons) == VALUE_INT32) |
| 495 | + settings.kbkeys[i].controller_buttons = (TbControllerButtons)(unsigned int)value_int32(vcontroller_buttons); |
| 496 | + } |
370 | 497 | } |
371 | 498 | } |
372 | 499 | } |
@@ -433,11 +560,14 @@ short save_settings(void) |
433 | 560 | for (int i = 0; i < GAME_KEYS_COUNT; i++) |
434 | 561 | { |
435 | 562 | char mods_buf[32]; |
| 563 | + char controller_buttons_buf[512]; |
436 | 564 | kmod_to_name(settings.kbkeys[i].mods, mods_buf, sizeof(mods_buf)); |
437 | | - TOSAVE("%s = { code = \"%s\", mods = \"%s\" }\n", |
| 565 | + controller_buttons_to_name(settings.kbkeys[i].controller_buttons, controller_buttons_buf, sizeof(controller_buttons_buf)); |
| 566 | + TOSAVE("%s = { code = \"%s\", mods = \"%s\", controller_buttons = \"%s\" }\n", |
438 | 567 | game_key_settings[i].toml_name, |
439 | 568 | keycode_to_name(settings.kbkeys[i].code), |
440 | | - mods_buf); |
| 569 | + mods_buf, |
| 570 | + controller_buttons_buf); |
441 | 571 | } |
442 | 572 | #undef TOSAVE |
443 | 573 |
|
|
0 commit comments