Simplify parse_hat function

This commit is contained in:
twinaphex 2021-03-20 13:24:10 +01:00
parent 6c9d4d2702
commit b51bb2bc66

View File

@ -25951,48 +25951,40 @@ unsigned input_config_translate_str_to_bind_id(const char *str)
return RARCH_BIND_LIST_END; return RARCH_BIND_LIST_END;
} }
static void parse_hat(struct retro_keybind *bind, const char *str) static uint16_t input_config_parse_hat(const char *dir)
{ {
uint16_t hat_dir = 0;
char *dir = NULL;
uint16_t hat = strtoul(str, &dir, 0);
if (!dir)
{
RARCH_WARN("[Input]: Found invalid hat in config!\n");
return;
}
if ( dir[0] == 'u' if ( dir[0] == 'u'
&& dir[1] == 'p' && dir[1] == 'p'
&& dir[2] == '\0' && dir[2] == '\0'
) )
hat_dir = HAT_UP_MASK; return HAT_UP_MASK;
else if ( dir[0] == 'd' else if (
&& dir[1] == 'o' dir[0] == 'd'
&& dir[2] == 'w' && dir[1] == 'o'
&& dir[3] == 'n' && dir[2] == 'w'
&& dir[4] == '\0' && dir[3] == 'n'
) && dir[4] == '\0'
hat_dir = HAT_DOWN_MASK; )
else if ( dir[0] == 'l' return HAT_DOWN_MASK;
&& dir[1] == 'e' else if (
&& dir[2] == 'f' dir[0] == 'l'
&& dir[3] == 't' && dir[1] == 'e'
&& dir[4] == '\0' && dir[2] == 'f'
) && dir[3] == 't'
hat_dir = HAT_LEFT_MASK; && dir[4] == '\0'
else if ( dir[0] == 'r' )
&& dir[1] == 'i' return HAT_LEFT_MASK;
&& dir[2] == 'g' else if (
&& dir[3] == 'h' dir[0] == 'r'
&& dir[4] == 't' && dir[1] == 'i'
&& dir[5] == '\0' && dir[2] == 'g'
) && dir[3] == 'h'
hat_dir = HAT_RIGHT_MASK; && dir[4] == 't'
&& dir[5] == '\0'
)
return HAT_RIGHT_MASK;
if (hat_dir) return 0;
bind->joykey = HAT_MAP(hat, hat_dir);
} }
static void input_config_parse_joy_button( static void input_config_parse_joy_button(
@ -26028,8 +26020,15 @@ static void input_config_parse_joy_button(
if (*btn == 'h') if (*btn == 'h')
{ {
const char *str = btn + 1; const char *str = btn + 1;
/* Parse hat? */
if (str && ISDIGIT((int)*str)) if (str && ISDIGIT((int)*str))
parse_hat(bind, str); {
char *dir = NULL;
uint16_t hat = strtoul(str, &dir, 0);
uint16_t hat_dir = dir ? input_config_parse_hat(dir) : 0;
if (hat_dir)
bind->joykey = HAT_MAP(hat, hat_dir);
}
} }
else else
bind->joykey = strtoull(tmp, NULL, 0); bind->joykey = strtoull(tmp, NULL, 0);