Cleanup menu_input_bind_dialog.c

This commit is contained in:
twinaphex 2020-06-10 17:54:19 +02:00
parent bc24092374
commit e0090010f4

View File

@ -69,33 +69,6 @@ struct menu_bind_state
/* TODO/FIXME - static public global variables */ /* TODO/FIXME - static public global variables */
static struct menu_bind_state menu_input_binds = {0}; static struct menu_bind_state menu_input_binds = {0};
static bool input_joypad_button_raw(const input_device_driver_t *drv,
unsigned port, unsigned button)
{
if (!drv)
return false;
return drv && drv->button(port, button);
}
static int16_t input_joypad_axis_raw(
const input_device_driver_t *drv,
unsigned port, unsigned axis)
{
if (!drv)
return 0;
return drv->axis(port, AXIS_POS(axis)) +
drv->axis(port, AXIS_NEG(axis));
}
static bool input_joypad_hat_raw(const input_device_driver_t *drv,
unsigned port, unsigned hat_dir, unsigned hat)
{
if (!drv)
return false;
return drv->button(port, HAT_MAP(hat, hat_dir));
}
static bool menu_input_key_bind_custom_bind_keyboard_cb( static bool menu_input_key_bind_custom_bind_keyboard_cb(
void *data, unsigned code) void *data, unsigned code)
{ {
@ -107,12 +80,12 @@ static bool menu_input_key_bind_custom_bind_keyboard_cb(
menu_input_binds.buffer.key = (enum retro_key)code; menu_input_binds.buffer.key = (enum retro_key)code;
/* write out the bind */ /* write out the bind */
*(menu_input_binds.output)=menu_input_binds.buffer; *(menu_input_binds.output) = menu_input_binds.buffer;
/* next bind */ /* next bind */
menu_input_binds.begin++; menu_input_binds.begin++;
menu_input_binds.output++; menu_input_binds.output++;
menu_input_binds.buffer=*(menu_input_binds.output); menu_input_binds.buffer =* (menu_input_binds.output);
rarch_timer_begin_new_time_us( rarch_timer_begin_new_time_us(
&menu_input_binds.timer_hold, input_bind_hold_us); &menu_input_binds.timer_hold, input_bind_hold_us);
rarch_timer_begin_new_time_us( rarch_timer_begin_new_time_us(
@ -201,14 +174,16 @@ static void menu_input_key_bind_poll_bind_get_rested_axes(
/* poll only the relevant port */ /* poll only the relevant port */
for (a = 0; a < MENU_MAX_AXES; a++) for (a = 0; a < MENU_MAX_AXES; a++)
state->axis_state[port].rested_axes[a] = state->axis_state[port].rested_axes[a] =
input_joypad_axis_raw(joypad, port, a); joypad->axis(port, AXIS_POS(a)) +
joypad->axis(port, AXIS_NEG(a));
if (sec_joypad) if (sec_joypad)
{ {
/* poll only the relevant port */ /* poll only the relevant port */
for (a = 0; a < MENU_MAX_AXES; a++) for (a = 0; a < MENU_MAX_AXES; a++)
state->axis_state[port].rested_axes[a] = state->axis_state[port].rested_axes[a] =
input_joypad_axis_raw(sec_joypad, port, a); sec_joypad->axis(port, AXIS_POS(a)) +
sec_joypad->axis(port, AXIS_NEG(a));
} }
} }
@ -219,32 +194,32 @@ static void menu_input_key_bind_poll_bind_state_internal(
bool timed_out) bool timed_out)
{ {
unsigned b, a, h; unsigned b, a, h;
if (!joypad) if (!joypad)
return; return;
if (joypad->poll) if (joypad->poll)
joypad->poll(); joypad->poll();
/* poll only the relevant port */ /* poll only the relevant port */
for (b = 0; b < MENU_MAX_BUTTONS; b++) for (b = 0; b < MENU_MAX_BUTTONS; b++)
state->state[port].buttons[b] = state->state[port].buttons[b] = joypad->button(port, b);
input_joypad_button_raw(joypad, port, b);
for (a = 0; a < MENU_MAX_AXES; a++) for (a = 0; a < MENU_MAX_AXES; a++)
state->state[port].axes[a] = state->state[port].axes[a] =
input_joypad_axis_raw(joypad, port, a); joypad->axis(port, AXIS_POS(a)) +
joypad->axis(port, AXIS_NEG(a));
for (h = 0; h < MENU_MAX_HATS; h++) for (h = 0; h < MENU_MAX_HATS; h++)
{ {
if (input_joypad_hat_raw(joypad, port, HAT_UP_MASK, h)) if (joypad->button(port, HAT_MAP(h, HAT_UP_MASK)))
state->state[port].hats[h] |= HAT_UP_MASK; state->state[port].hats[h] |= HAT_UP_MASK;
if (input_joypad_hat_raw(joypad, port, HAT_DOWN_MASK, h)) if (joypad->button(port, HAT_MAP(h, HAT_DOWN_MASK)))
state->state[port].hats[h] |= HAT_DOWN_MASK; state->state[port].hats[h] |= HAT_DOWN_MASK;
if (input_joypad_hat_raw(joypad, port, HAT_LEFT_MASK, h)) if (joypad->button(port, HAT_MAP(h, HAT_LEFT_MASK)))
state->state[port].hats[h] |= HAT_LEFT_MASK; state->state[port].hats[h] |= HAT_LEFT_MASK;
if (input_joypad_hat_raw(joypad, port, HAT_RIGHT_MASK, h)) if (joypad->button(port, HAT_MAP(h, HAT_RIGHT_MASK)))
state->state[port].hats[h] |= HAT_RIGHT_MASK; state->state[port].hats[h] |= HAT_RIGHT_MASK;
} }
} }
static void menu_input_key_bind_poll_bind_state( static void menu_input_key_bind_poll_bind_state(
@ -263,10 +238,10 @@ static void menu_input_key_bind_poll_bind_state(
memset(state->state, 0, sizeof(state->state)); memset(state->state, 0, sizeof(state->state));
/* poll mouse (on the relevant port) */ /* poll mouse (on the relevant port) */
for (b = 0; b < MENU_MAX_MBUTTONS; b++) for (b = 0; b < MENU_MAX_MBUTTONS; b++)
state->state[port].mouse_buttons[b] = state->state[port].mouse_buttons[b] =
input_mouse_button_raw(port, b); input_mouse_button_raw(port, b);
joypad_info.joy_idx = 0; joypad_info.joy_idx = 0;
joypad_info.auto_binds = NULL; joypad_info.auto_binds = NULL;
@ -290,11 +265,13 @@ bool menu_input_key_bind_set_mode(
{ {
unsigned index_offset; unsigned index_offset;
input_keyboard_ctx_wait_t keys; input_keyboard_ctx_wait_t keys;
rarch_setting_t *setting = (rarch_setting_t*)data; rarch_setting_t *setting = (rarch_setting_t*)data;
settings_t *settings = config_get_ptr(); settings_t *settings = config_get_ptr();
menu_handle_t *menu = menu_driver_get_ptr(); menu_handle_t *menu = menu_driver_get_ptr();
uint64_t input_bind_hold_us = settings->uints.input_bind_hold * 1000000; uint64_t input_bind_hold_us = settings->uints.input_bind_hold
uint64_t input_bind_timeout_us = settings->uints.input_bind_timeout * 1000000; * 1000000;
uint64_t input_bind_timeout_us = settings->uints.input_bind_timeout
* 1000000;
if (!setting || !menu) if (!setting || !menu)
return false; return false;
@ -309,8 +286,10 @@ bool menu_input_key_bind_set_mode(
menu_input_key_bind_poll_bind_state( menu_input_key_bind_poll_bind_state(
&menu_input_binds, false); &menu_input_binds, false);
rarch_timer_begin_new_time_us(&menu_input_binds.timer_hold, input_bind_hold_us); rarch_timer_begin_new_time_us(
rarch_timer_begin_new_time_us(&menu_input_binds.timer_timeout, input_bind_timeout_us); &menu_input_binds.timer_hold, input_bind_hold_us);
rarch_timer_begin_new_time_us(
&menu_input_binds.timer_timeout, input_bind_timeout_us);
keys.userdata = menu; keys.userdata = menu;
keys.cb = menu_input_key_bind_custom_bind_keyboard_cb; keys.cb = menu_input_key_bind_custom_bind_keyboard_cb;
@ -434,7 +413,8 @@ static bool menu_input_key_bind_poll_find_hold_pad(
unsigned p) unsigned p)
{ {
unsigned a, b, h; unsigned a, b, h;
const struct menu_bind_state_port *n = (const struct menu_bind_state_port*) const struct menu_bind_state_port *n =
(const struct menu_bind_state_port*)
&new_state->state[p]; &new_state->state[p];
for (b = 0; b < MENU_MAX_MBUTTONS; b++) for (b = 0; b < MENU_MAX_MBUTTONS; b++)
@ -483,7 +463,7 @@ static bool menu_input_key_bind_poll_find_hold_pad(
? AXIS_POS(a) : AXIS_NEG(a); ? AXIS_POS(a) : AXIS_NEG(a);
output->joykey = NO_BTN; output->joykey = NO_BTN;
return true; return true;
} }
} }
@ -503,7 +483,7 @@ static bool menu_input_key_bind_poll_find_hold_pad(
if (sane_trigger) if (sane_trigger)
{ {
output->joykey = HAT_MAP(h, sane_trigger); output->joykey = HAT_MAP(h, sane_trigger);
output->joyaxis = AXIS_NONE; output->joyaxis = AXIS_NONE;
return true; return true;
} }
@ -525,7 +505,7 @@ bool menu_input_key_bind_poll_find_hold(
for (i = 0; i < max_users; i++) for (i = 0; i < max_users; i++)
{ {
if (!menu_input_key_bind_poll_find_hold_pad(new_state, output, i)) if (!menu_input_key_bind_poll_find_hold_pad(new_state, output, i))
continue; continue;
return true; return true;
} }
@ -546,8 +526,9 @@ static bool menu_input_key_bind_poll_find_trigger(
for (i = 0; i < max_users; i++) for (i = 0; i < max_users; i++)
{ {
if (!menu_input_key_bind_poll_find_trigger_pad(state, new_state, output, i)) if (!menu_input_key_bind_poll_find_trigger_pad(
continue; state, new_state, output, i))
continue;
return true; return true;
} }
@ -578,13 +559,13 @@ bool menu_input_key_bind_iterate(menu_input_ctx_bind_t *bind,
return false; return false;
snprintf(bind->s, bind->len, snprintf(bind->s, bind->len,
"[%s]\npress keyboard, mouse or joypad\n(timeout %d %s)", "[%s]\nPress keyboard, mouse or joypad\n(Timeout %d %s)",
input_config_bind_map_get_desc( input_config_bind_map_get_desc(
menu_input_binds.begin - MENU_SETTINGS_BIND_BEGIN), menu_input_binds.begin - MENU_SETTINGS_BIND_BEGIN),
rarch_timer_get_timeout(&menu_input_binds.timer_timeout), rarch_timer_get_timeout(&menu_input_binds.timer_timeout),
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_SECONDS)); msg_hash_to_str(MENU_ENUM_LABEL_VALUE_SECONDS));
/*tick main timers*/ /* Tick main timers */
rarch_timer_tick(&menu_input_binds.timer_timeout, current_time); rarch_timer_tick(&menu_input_binds.timer_timeout, current_time);
rarch_timer_tick(&menu_input_binds.timer_hold, current_time); rarch_timer_tick(&menu_input_binds.timer_hold, current_time);
@ -629,46 +610,45 @@ bool menu_input_key_bind_iterate(menu_input_ctx_bind_t *bind,
menu_input_key_bind_poll_bind_state(&binds, timed_out); menu_input_key_bind_poll_bind_state(&binds, timed_out);
#ifdef ANDROID #ifdef ANDROID
/* Keep resetting bind during the hold period,
/*keep resetting bind during the hold period, or we'll potentially bind joystick and mouse, etc.*/ * or we'll potentially bind joystick and mouse, etc.*/
binds.buffer = *(binds.output); binds.buffer = *(binds.output);
if (menu_input_key_bind_poll_find_hold(&binds, &binds.buffer)) if (menu_input_key_bind_poll_find_hold(&binds, &binds.buffer))
{ {
/*inhibit timeout*/ /* Inhibit timeout*/
rarch_timer_begin_new_time_us(&binds.timer_timeout, input_bind_timeout_us); rarch_timer_begin_new_time_us(&binds.timer_timeout,
input_bind_timeout_us);
/*run hold timer*/ /* Run hold timer*/
rarch_timer_tick(&binds.timer_hold, current_time); rarch_timer_tick(&binds.timer_hold, current_time);
snprintf(bind->s, bind->len, snprintf(bind->s, bind->len,
"[%s]\npress keyboard, mouse or joypad\nand hold ...", "[%s]\npress keyboard, mouse or joypad\nand hold ...",
input_config_bind_map_get_desc( input_config_bind_map_get_desc(
menu_input_binds.begin - MENU_SETTINGS_BIND_BEGIN)); menu_input_binds.begin - MENU_SETTINGS_BIND_BEGIN));
/*hold complete?*/ /* Hold complete? */
if (rarch_timer_has_expired(&binds.timer_hold)) if (rarch_timer_has_expired(&binds.timer_hold))
complete = true; complete = true;
} }
else else
{ {
/*reset hold countdown*/ /* Reset hold countdown*/
rarch_timer_begin_new_time_us(&binds.timer_hold, input_bind_hold_us); rarch_timer_begin_new_time_us(&binds.timer_hold, input_bind_hold_us);
} }
#else #else
if ((binds.skip && !menu_input_binds.skip) || if ((binds.skip && !menu_input_binds.skip) ||
menu_input_key_bind_poll_find_trigger(&menu_input_binds, &binds, &(binds.buffer))) menu_input_key_bind_poll_find_trigger(
{ &menu_input_binds, &binds, &(binds.buffer)))
complete = true; complete = true;
}
#endif #endif
if (complete) if (complete)
{ {
input_driver_t *input_drv = input_get_ptr(); input_driver_t *input_drv = input_get_ptr();
/*update bind*/ /* Update bind */
*(binds.output) = binds.buffer; *(binds.output) = binds.buffer;
if (input_drv) if (input_drv)