From 03d824a6b34b4eb8e6a8749d61b24b185c879364 Mon Sep 17 00:00:00 2001 From: sorgelig Date: Mon, 22 Jun 2020 16:57:16 +0800 Subject: [PATCH] input: restore player gamepad after input devices re-enumeration. --- input.cpp | 120 +++++++++++++++++++++++++++++++++++-------------- input.h | 1 + joymapping.cpp | 51 ++++++--------------- joymapping.h | 2 +- menu.cpp | 29 +++++++----- 5 files changed, 119 insertions(+), 84 deletions(-) diff --git a/input.cpp b/input.cpp index 0d3e0e9..6dd2a0b 100644 --- a/input.cpp +++ b/input.cpp @@ -896,6 +896,8 @@ typedef struct } devInput; static devInput input[NUMDEV] = {}; +static devInput player_pad[NUMPLAYERS] = {}; +static devInput player_pdsp[NUMPLAYERS] = {}; #define BTN_NUM (sizeof(devInput::map) / sizeof(devInput::map[0])) @@ -1664,6 +1666,13 @@ static void joy_analog(int num, int axis, int offset) } } +void reset_players() +{ + for (int i = 0; i < NUMDEV; i++) input[i].num = 0; + memset(player_pad, 0, sizeof(player_pad)); + memset(player_pdsp, 0, sizeof(player_pdsp)); +} + static int ds_mouse_emu = 0; static void input_cb(struct input_event *ev, struct input_absinfo *absinfo, int dev) @@ -1694,29 +1703,6 @@ static void input_cb(struct input_event *ev, struct input_absinfo *absinfo, int int enter = (ev->type == EV_KEY && ev->code == KEY_ENTER); int origcode = ev->code; - if (!input[dev].num && ((ev->type == EV_KEY && ev->code >= 256) || (input[dev].quirk == QUIRK_PDSP && ev->type == EV_REL))) - { - for (uint8_t num = 1; num < NUMDEV + 1; num++) - { - int found = 0; - for (int i = 0; i < NUMDEV; i++) - { - // paddles/spinners overlay on top of other gamepad - if (!((input[dev].quirk == QUIRK_PDSP) ^ (input[i].quirk == QUIRK_PDSP))) - { - found = (input[i].num == num); - if (found) break; - } - } - - if (!found) - { - input[dev].num = num; - break; - } - } - } - if (!input[dev].has_mmap) { if (input[dev].quirk != QUIRK_PDSP) @@ -1737,12 +1723,6 @@ static void input_cb(struct input_event *ev, struct input_absinfo *absinfo, int { memset(input[dev].map, 0, sizeof(input[dev].map)); input[dev].map[map_paddle_btn()] = 0x120; - if (cfg.controller_info) - { - char str[32]; - sprintf(str, "P%d paddle/spinner", input[dev].num); - Info(str, cfg.controller_info * 1000); - } } else if (!load_map(get_map_name(dev, 0), &input[dev].map, sizeof(input[dev].map))) { @@ -1752,7 +1732,7 @@ static void input_cb(struct input_event *ev, struct input_absinfo *absinfo, int if (input[dev].has_mmap == 1) { // not defined try to guess the mapping - map_joystick(input[dev].map, input[dev].mmap, input[dev].num); + map_joystick(input[dev].map, input[dev].mmap); } else { @@ -1761,13 +1741,52 @@ static void input_cb(struct input_event *ev, struct input_absinfo *absinfo, int } input[dev].has_map++; } - else - { - map_joystick_show(input[dev].map, input[dev].mmap, input[dev].num); - } input[dev].has_map++; } + if (!input[dev].num && ((ev->type == EV_KEY && ev->code >= 256 && ev->value >= 1) || (input[dev].quirk == QUIRK_PDSP && ev->type == EV_REL))) + { + for (uint8_t num = 1; num < NUMDEV + 1; num++) + { + int found = 0; + for (int i = 0; i < NUMDEV; i++) + { + // paddles/spinners overlay on top of other gamepad + if (!((input[dev].quirk == QUIRK_PDSP) ^ (input[i].quirk == QUIRK_PDSP))) + { + found = (input[i].num == num); + if (found) break; + } + } + + if (!found) + { + input[dev].num = num; + if (num < NUMPLAYERS) + { + if(input[dev].quirk == QUIRK_PDSP) memcpy(&player_pdsp[num], &input[dev], sizeof(player_pdsp[0])); + else memcpy(&player_pad[num], &input[dev], sizeof(player_pad[0])); + } + break; + } + } + + if (cfg.controller_info) + { + if (input[dev].quirk == QUIRK_PDSP) + { + char str[32]; + sprintf(str, "P%d paddle/spinner", input[dev].num); + Info(str, cfg.controller_info * 1000); + } + else + { + map_joystick_show(input[dev].map, input[dev].mmap, input[dev].num); + } + } + printf("Device %d assigned to player %d\n", dev, input[dev].num); + } + int old_combo = input[dev].osd_combo; if (ev->type == EV_KEY) @@ -2743,6 +2762,23 @@ int input_test(int getchar) pool[i].events = 0; } + // check assigned pads + // remove from list if pad is disappeared + for (int i = 0; i < NUMPLAYERS; i++) + { + if (strlen(player_pad[i].devname)) + { + struct stat64 st = {}; + if (stat64(player_pad[i].devname, &st) < 0) memset(&player_pad[i], 0, sizeof(player_pad[i])); + } + + if (strlen(player_pdsp[i].devname)) + { + struct stat64 st = {}; + if (stat64(player_pdsp[i].devname, &st) < 0) memset(&player_pdsp[i], 0, sizeof(player_pdsp[i])); + } + } + int n = 0; DIR *d = opendir("/dev/input"); if (d) @@ -2911,6 +2947,22 @@ int input_test(int getchar) for (int i = 0; i < n; i++) { printf("opened %d(%2d): %s (%04x:%04x) %d \"%s\" \"%s\"\n", i, input[i].bind, input[i].devname, input[i].vid, input[i].pid, input[i].quirk, input[i].id, input[i].name); + + // restore players + devInput *player = (input[i].quirk == QUIRK_PDSP) ? player_pdsp : player_pad; + for (int k = 0; k < NUMPLAYERS; k++) + { + if (strlen(player[k].devname) && !strcmp(player[k].devname, input[i].devname)) + { + printf("restore player %d\n", k); + + int dev = i; + if (input[i].bind) dev = input[i].bind; + input[dev].num = k; + memcpy(input[dev].jkmap, player[k].jkmap, sizeof(input[dev].jkmap)); + input[dev].lightgun = player[k].lightgun; + } + } } } cur_leds |= 0x80; diff --git a/input.h b/input.h index 696e22e..5d7de4c 100644 --- a/input.h +++ b/input.h @@ -89,6 +89,7 @@ uint16_t get_map_vid(); uint16_t get_map_pid(); int has_default_map(); void send_map_cmd(int key); +void reset_players(); uint32_t get_key_mod(); uint32_t get_ps2_code(uint16_t key); diff --git a/joymapping.cpp b/joymapping.cpp index aae5733..b56abfc 100644 --- a/joymapping.cpp +++ b/joymapping.cpp @@ -114,9 +114,8 @@ static int is_fire(char* name) return 0; } -void map_joystick(uint32_t *map, uint32_t *mmap, int num) +void map_joystick(uint32_t *map, uint32_t *mmap) { - static char mapinfo[1024]; /* attemps to centrally defined core joy mapping to the joystick declaredy by a core config string we use the names declared by core with some special handling for specific edge cases @@ -125,8 +124,6 @@ void map_joystick(uint32_t *map, uint32_t *mmap, int num) A, B, X, Y, L, R, Select, Start */ read_buttons(); - sprintf(mapinfo, "P%d map:", num); - if (!num) sprintf(mapinfo, "Map:"); map[SYS_BTN_RIGHT] = mmap[SYS_BTN_RIGHT] & 0xFFFF; map[SYS_BTN_LEFT] = mmap[SYS_BTN_LEFT] & 0xFFFF; @@ -159,21 +156,17 @@ void map_joystick(uint32_t *map, uint32_t *mmap, int num) char *p = strchr(btn_name, '|'); if (p) *p = 0; - int mapped = 1; - if(!strcasecmp(btn_name, "A") || !strcasecmp(btn_name, "Jump") || is_fire(btn_name) == 1) { map[idx] = mmap[SYS_BTN_A]; - strcat(mapinfo, "\n[A]"); } else if(!strcasecmp(btn_name, "B") || is_fire(btn_name) == 2) { map[idx] = mmap[SYS_BTN_B]; - strcat(mapinfo, "\n[B]"); } else if(!strcasecmp(btn_name, "X") @@ -181,7 +174,6 @@ void map_joystick(uint32_t *map, uint32_t *mmap, int num) || is_fire(btn_name) == 3) { map[idx] = mmap[SYS_BTN_X]; - strcat(mapinfo, "\n[X]"); } else if(!strcasecmp(btn_name, "Y") @@ -189,7 +181,6 @@ void map_joystick(uint32_t *map, uint32_t *mmap, int num) || is_fire(btn_name) == 4) { map[idx] = mmap[SYS_BTN_Y]; - strcat(mapinfo, "\n[Y]"); } // Genesis C and Z and TG16 V and VI @@ -198,14 +189,12 @@ void map_joystick(uint32_t *map, uint32_t *mmap, int num) || !strcasecmp(btn_name, "Coin")) { map[idx] = mmap[SYS_BTN_R]; - strcat(mapinfo, "\n[R]"); } else if(!strcasecmp(btn_name, "L") || !strcasecmp(btn_name, "LT")) { map[idx] = mmap[SYS_BTN_L]; - strcat(mapinfo, "\n[L]"); } else if(!strcasecmp(btn_name, "Select") @@ -214,7 +203,6 @@ void map_joystick(uint32_t *map, uint32_t *mmap, int num) || !strcasecmp(btn_name, "Start 2P")) { map[idx] = mmap[SYS_BTN_SELECT]; - strcat(mapinfo, "\n[\x96]"); } else if(!strcasecmp(btn_name, "Start") @@ -223,24 +211,10 @@ void map_joystick(uint32_t *map, uint32_t *mmap, int num) || !strcasecmp(btn_name, "Start 1P")) { map[idx] = mmap[SYS_BTN_START]; - strcat(mapinfo, "\n[\x16]"); - } - - else mapped = 0; - - if (map[idx] && mapped) - { - strcat(mapinfo, ": "); - strcat(mapinfo, joy_names[i]); } n++; } - - if (cfg.controller_info) - { - Info(mapinfo, cfg.controller_info*1000); - } } int map_paddle_btn() @@ -267,7 +241,7 @@ static const char* get_std_name(uint16_t code, uint32_t *mmap) if (code == mmap[SYS_BTN_SELECT]) return "[\x96]"; if (code == mmap[SYS_BTN_START ]) return "[\x16]"; - return "[ ]"; + return code ? "[ ]" : NULL; } void map_joystick_show(uint32_t *map, uint32_t *mmap, int num) @@ -275,22 +249,23 @@ void map_joystick_show(uint32_t *map, uint32_t *mmap, int num) static char mapinfo[1024]; read_buttons(); - sprintf(mapinfo, "P%d, map:", num); - if(!num) sprintf(mapinfo, "Map:"); + sprintf(mapinfo, "Map (P%d):", num); + if (!num) sprintf(mapinfo, " Map:"); // loop through core requested buttons and construct result map for (int i = 0; i < joy_count; i++) { if (!strcmp(joy_names[i], "-")) continue; - strcat(mapinfo, "\n"); - strcat(mapinfo, get_std_name((uint16_t)(map[i + DPAD_COUNT]), mmap)); - strcat(mapinfo, ": "); - strcat(mapinfo, joy_names[i]); + const char *btn = get_std_name((uint16_t)(map[i + DPAD_COUNT]), mmap); + if (btn) + { + strcat(mapinfo, "\n"); + strcat(mapinfo, btn); + strcat(mapinfo, ": "); + strcat(mapinfo, joy_names[i]); + } } - if (cfg.controller_info) - { - Info(mapinfo, cfg.controller_info * 1000); - } + Info(mapinfo, (cfg.controller_info ? cfg.controller_info : 6) * 1000); } diff --git a/joymapping.h b/joymapping.h index 58d3ec2..7ccc8ad 100644 --- a/joymapping.h +++ b/joymapping.h @@ -7,7 +7,7 @@ #include -void map_joystick(uint32_t *map, uint32_t *mmap, int num); +void map_joystick(uint32_t *map, uint32_t *mmap); void map_joystick_show(uint32_t *map, uint32_t *mmap, int num); int map_paddle_btn(); diff --git a/menu.cpp b/menu.cpp index 8c48187..3ec2edb 100644 --- a/menu.cpp +++ b/menu.cpp @@ -1985,21 +1985,23 @@ void HandleUI(void) while(1) { n = 0; - menumask = 0xf807; + menumask = 0xf80F; if (!menusub) firstmenu = 0; adjvisible = 0; MenuWrite(n++, " Core \x16", menusub == 0, 0); + MenuWrite(n++); sprintf(s, " Define %s buttons ", is_menu() ? "System" : user_io_get_core_name_ex()); s[27] = '\x16'; s[28] = 0; MenuWrite(n++, s, menusub == 1, 0); MenuWrite(n++, " Button/Key remap for game \x16", menusub == 2, 0); + MenuWrite(n++, " Reset player assignment", menusub == 3, 0); if (user_io_get_uart_mode()) { - menumask |= 0x8; + menumask |= 0x10; MenuWrite(n++); int mode = GetUARTMode(); const char *p = config_uart_msg[(is_st() && mode == 3) ? 4 : mode]; @@ -2007,14 +2009,14 @@ void HandleUI(void) sprintf(s, " UART mode (%s) ",p); s[27] = '\x16'; s[28] = 0; - MenuWrite(n++, s, menusub == 3); + MenuWrite(n++, s, menusub == 4); } if (video_get_scaler_flt() >= 0 && !cfg.direct_video) { MenuWrite(n++); menumask |= 0x60; - sprintf(s, " Scale Filter - %s", config_scaler_msg[video_get_scaler_flt() ? 1 : 0]); + sprintf(s, " Scale filter - %s", config_scaler_msg[video_get_scaler_flt() ? 1 : 0]); MenuWrite(n++, s, menusub == 5); memset(s, 0, sizeof(s)); @@ -2032,7 +2034,7 @@ void HandleUI(void) { MenuWrite(n++); menumask |= 0x180; - sprintf(s, " Gamma Correction - %s", config_gamma_msg[video_get_gamma_en() ? 1 : 0]); + sprintf(s, " Gamma correction - %s", config_gamma_msg[video_get_gamma_en() ? 1 : 0]); MenuWrite(n++, s, menusub == 7); memset(s, 0, sizeof(s)); @@ -2050,7 +2052,7 @@ void HandleUI(void) { MenuWrite(n++); menumask |= 0x600; - sprintf(s, " Audio Filter - %s", config_afilter_msg[audio_filter_en() ? 1 : 0]); + sprintf(s, " Audio filter - %s", config_afilter_msg[audio_filter_en() ? 1 : 0]); MenuWrite(n++, s, menusub == 9); memset(s, 0, sizeof(s)); @@ -2154,6 +2156,11 @@ void HandleUI(void) break; case 3: + reset_players(); + menustate = MENU_NONE1; + break; + + case 4: { menustate = MENU_UART1; @@ -2459,7 +2466,7 @@ void HandleUI(void) if (menu) { menustate = MENU_8BIT_SYSTEM1; - menusub = 3; + menusub = 4; break; } @@ -2533,7 +2540,7 @@ void HandleUI(void) SetUARTMode(0); SetUARTMode(mode); menustate = MENU_8BIT_SYSTEM1; - menusub = 3; + menusub = 4; } } break; @@ -2544,7 +2551,7 @@ void HandleUI(void) sprintf(s, "uartmode.%s", user_io_get_core_name_ex()); FileSaveConfig(s, &mode, 4); menustate = MENU_8BIT_SYSTEM1; - menusub = 3; + menusub = 4; } break; @@ -3165,7 +3172,7 @@ void HandleUI(void) if (menu | select | left) { menustate = MENU_8BIT_SYSTEM1; - menusub = 12; + menusub = 14; } break; @@ -4272,7 +4279,7 @@ void HandleUI(void) if (menu || (select && (menusub == 1))) // exit menu { menustate = MENU_8BIT_SYSTEM1; - menusub = 4; + menusub = 11; } break;