From 46fb61bede4b65f724bd45cc0171254a5867ba2c Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 29 Aug 2021 16:57:41 +0200 Subject: [PATCH 1/6] input_state_wrap - do not attempt to access joypad or sec_joypad pointers if non-NULL --- retroarch.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/retroarch.c b/retroarch.c index 0cf42703d4..efa052d32e 100644 --- a/retroarch.c +++ b/retroarch.c @@ -478,8 +478,9 @@ static int16_t input_state_wrap( { if (id == RETRO_DEVICE_ID_JOYPAD_MASK) { - ret |= joypad->state( - joypad_info, binds[port], port); + if (joypad) + ret |= joypad->state( + joypad_info, binds[port], port); #ifdef HAVE_MFI if (sec_joypad) ret |= sec_joypad->state( @@ -504,27 +505,33 @@ static int16_t input_state_wrap( const uint32_t joyaxis = (bind_joyaxis != AXIS_NONE) ? bind_joyaxis : autobind_joyaxis; - if ((uint16_t)joykey != NO_BTN && joypad->button( - port, (uint16_t)joykey)) - return 1; - if (joyaxis != AXIS_NONE && - ((float)abs(joypad->axis(port, joyaxis)) - / 0x8000) > axis_threshold) - return 1; + if (joypad) + { + if ((uint16_t)joykey != NO_BTN && joypad->button( + port, (uint16_t)joykey)) + return 1; + if (joyaxis != AXIS_NONE && + ((float)abs(joypad->axis(port, joyaxis)) + / 0x8000) > axis_threshold) + return 1; + } #ifdef HAVE_MFI - if ((uint16_t)joykey != NO_BTN && sec_joypad->button( - port, (uint16_t)joykey)) - return 1; - if (joyaxis != AXIS_NONE && - ((float)abs(sec_joypad->axis(port, joyaxis)) - / 0x8000) > axis_threshold) - return 1; + if (sec_joypad) + { + if ((uint16_t)joykey != NO_BTN && sec_joypad->button( + port, (uint16_t)joykey)) + return 1; + if (joyaxis != AXIS_NONE && + ((float)abs(sec_joypad->axis(port, joyaxis)) + / 0x8000) > axis_threshold) + return 1; + } #endif } } } - if (current_input->input_state) + if (current_input && current_input->input_state) ret |= current_input->input_state( data, joypad, From f1f3998d280bc562851e9ad15ecbed140c51d5f2 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 29 Aug 2021 18:06:08 +0200 Subject: [PATCH 2/6] Prevent joypad objects being destroyed before their pointers are being NULLed. Another thread could access a "half destroyed" object before --- retroarch.c | 54 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/retroarch.c b/retroarch.c index efa052d32e..3b0c91d55e 100644 --- a/retroarch.c +++ b/retroarch.c @@ -21921,16 +21921,24 @@ void joypad_driver_reinit(void *data, const char *joypad_driver_name) return; if (input_driver_st->primary_joypad) - input_driver_st->primary_joypad->destroy(); - input_driver_st->primary_joypad = NULL; + { + const input_device_driver_t *tmp = input_driver_st->primary_joypad; + input_driver_st->primary_joypad = NULL; + tmp->destroy(); + } #ifdef HAVE_MFI if (input_driver_st->secondary_joypad) - input_driver_st->secondary_joypad->destroy(); - input_driver_st->secondary_joypad = NULL; + { + const input_device_driver_t *tmp = input_driver_st->secondary_joypad; + input_driver_st->secondary_joypad = NULL; + tmp->destroy(); + } #endif - input_driver_st->primary_joypad = input_joypad_init_driver(joypad_driver_name, data); + if (!input_driver_st->primary_joypad) + input_driver_st->primary_joypad = input_joypad_init_driver(joypad_driver_name, data); #ifdef HAVE_MFI - input_driver_st->secondary_joypad = input_joypad_init_driver("mfi", data); + if (!input_driver_st->secondary_joypad) + input_driver_st->secondary_joypad = input_joypad_init_driver("mfi", data); #endif } @@ -24840,16 +24848,18 @@ static void input_keys_pressed( void input_driver_init_joypads(void) { - struct rarch_state *p_rarch = &rarch_st; - input_driver_state_t *input_driver_st = &p_rarch->input_driver_state; - settings_t *settings = p_rarch->configuration_settings; - input_driver_st->primary_joypad = input_joypad_init_driver( + struct rarch_state *p_rarch = &rarch_st; + input_driver_state_t *input_driver_st = &p_rarch->input_driver_state; + settings_t *settings = p_rarch->configuration_settings; + if (!input_driver_st->primary_joypad) + input_driver_st->primary_joypad = input_joypad_init_driver( settings->arrays.input_joypad_driver, input_driver_st->current_data); #ifdef HAVE_MFI - input_driver_st->secondary_joypad = input_joypad_init_driver( - "mfi", - input_driver_st->current_data); + if (!input_driver_st->secondary_joypad) + input_driver_st->secondary_joypad = input_joypad_init_driver( + "mfi", + input_driver_st->current_data); #endif } @@ -29808,10 +29818,10 @@ static void video_driver_free_hw_context(struct rarch_state *p_rarch) static void video_driver_free_internal(struct rarch_state *p_rarch) { - input_driver_state_t *input_driver_st = &p_rarch->input_driver_state; + input_driver_state_t *input_driver_st = &p_rarch->input_driver_state; #ifdef HAVE_THREADS - bool is_threaded = VIDEO_DRIVER_IS_THREADED_INTERNAL(); + bool is_threaded = VIDEO_DRIVER_IS_THREADED_INTERNAL(); #endif #ifdef HAVE_VIDEO_LAYOUT @@ -29829,12 +29839,18 @@ static void video_driver_free_internal(struct rarch_state *p_rarch) if (input_driver_st->current_driver->free) input_driver_st->current_driver->free(input_driver_st->current_data); if (input_driver_st->primary_joypad) - input_driver_st->primary_joypad->destroy(); - input_driver_st->primary_joypad = NULL; + { + const input_device_driver_t *tmp = input_driver_st->primary_joypad; + input_driver_st->primary_joypad = NULL; + tmp->destroy(); + } #ifdef HAVE_MFI if (input_driver_st->secondary_joypad) - input_driver_st->secondary_joypad->destroy(); - input_driver_st->secondary_joypad = NULL; + { + const input_device_driver_t *tmp = input_driver_st->sec_joypad; + input_driver_st->secondary_joypad = NULL; + tmp->destroy(); + } #endif p_rarch->keyboard_mapping_blocked = false; p_rarch->input_driver_state.current_data = NULL; From 540d192ecbaec309fdec42b364d3d6a21efea026 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 29 Aug 2021 18:34:52 +0200 Subject: [PATCH 3/6] Buildfix for Apple + make sure primary_joypad/secondary_joypad goes through local pointers; cut down on HAVE_MFI ifdefs in retroarch.c --- retroarch.c | 149 +++++++++++++++++++++++++++++----------------------- 1 file changed, 82 insertions(+), 67 deletions(-) diff --git a/retroarch.c b/retroarch.c index 3b0c91d55e..e8a2578ca9 100644 --- a/retroarch.c +++ b/retroarch.c @@ -481,11 +481,9 @@ static int16_t input_state_wrap( if (joypad) ret |= joypad->state( joypad_info, binds[port], port); -#ifdef HAVE_MFI if (sec_joypad) ret |= sec_joypad->state( joypad_info, binds[port], port); -#endif } else { @@ -515,7 +513,6 @@ static int16_t input_state_wrap( / 0x8000) > axis_threshold) return 1; } -#ifdef HAVE_MFI if (sec_joypad) { if ((uint16_t)joykey != NO_BTN && sec_joypad->button( @@ -526,7 +523,6 @@ static int16_t input_state_wrap( / 0x8000) > axis_threshold) return 1; } -#endif } } } @@ -1365,16 +1361,26 @@ bool menu_input_key_bind_set_mode( { uint64_t current_usec; unsigned index_offset; - rarch_setting_t *setting = (rarch_setting_t*)data; - struct rarch_state *p_rarch = &rarch_st; - input_driver_state_t *input_driver_st = &p_rarch->input_driver_state; - menu_handle_t *menu = p_rarch->menu_driver_data; - menu_input_t *menu_input = &p_rarch->menu_input_state; - settings_t *settings = p_rarch->configuration_settings; - struct menu_bind_state *binds = &p_rarch->menu_input_binds; - uint64_t input_bind_hold_us = settings->uints.input_bind_hold + rarch_setting_t *setting = (rarch_setting_t*)data; + struct rarch_state *p_rarch = &rarch_st; + input_driver_state_t + *input_driver_st = &p_rarch->input_driver_state; + menu_handle_t *menu = p_rarch->menu_driver_data; + const input_device_driver_t + *joypad = input_driver_st->primary_joypad; +#ifdef HAVE_MFI + const input_device_driver_t + *sec_joypad = input_driver_st->secondary_joypad; +#else + const input_device_driver_t + *sec_joypad = NULL; +#endif + menu_input_t *menu_input = &p_rarch->menu_input_state; + settings_t *settings = p_rarch->configuration_settings; + struct menu_bind_state *binds = &p_rarch->menu_input_binds; + uint64_t input_bind_hold_us = settings->uints.input_bind_hold * 1000000; - uint64_t input_bind_timeout_us = settings->uints.input_bind_timeout + uint64_t input_bind_timeout_us = settings->uints.input_bind_timeout * 1000000; if (!setting || !menu) @@ -1383,21 +1389,19 @@ bool menu_input_key_bind_set_mode( binds, state, setting, settings) == -1) return false; - index_offset = setting->index_offset; - binds->port = settings->uints.input_joypad_index[index_offset]; + index_offset = setting->index_offset; + binds->port = settings->uints.input_joypad_index[ + index_offset]; menu_input_key_bind_poll_bind_get_rested_axes( - input_driver_st->primary_joypad, -#ifdef HAVE_MFI - input_driver_st->secondary_joypad, -#else - NULL, -#endif + joypad, + sec_joypad, binds); - menu_input_key_bind_poll_bind_state(p_rarch, settings->uints.input_joypad_index[binds->port], + menu_input_key_bind_poll_bind_state(p_rarch, + settings->uints.input_joypad_index[binds->port], binds, false); - current_usec = cpu_features_get_time_usec(); + current_usec = cpu_features_get_time_usec(); RARCH_TIMER_BEGIN_NEW_TIME_USEC( binds->timer_hold, @@ -13409,26 +13413,32 @@ static bool input_driver_ungrab_mouse(struct rarch_state *p_rarch) static void command_event_reinit(struct rarch_state *p_rarch, const int flags) { - settings_t *settings = p_rarch->configuration_settings; - input_driver_state_t *input_driver_st = &p_rarch->input_driver_state; - + settings_t *settings = p_rarch->configuration_settings; + input_driver_state_t + *input_driver_st = &p_rarch->input_driver_state; #ifdef HAVE_MENU - bool video_fullscreen = settings->bools.video_fullscreen; - bool adaptive_vsync = settings->bools.video_adaptive_vsync; - unsigned swap_interval = settings->uints.video_swap_interval; + bool video_fullscreen = settings->bools.video_fullscreen; + bool adaptive_vsync = settings->bools.video_adaptive_vsync; + unsigned swap_interval = settings->uints.video_swap_interval; +#endif + enum input_game_focus_cmd_type + game_focus_cmd = GAME_FOCUS_CMD_REAPPLY; + const input_device_driver_t + *joypad = input_driver_st->primary_joypad; +#ifdef HAVE_MFI + const input_device_driver_t + *sec_joypad = input_driver_st->secondary_joypad; +#else + const input_device_driver_t + *sec_joypad = NULL; #endif - enum input_game_focus_cmd_type game_focus_cmd = GAME_FOCUS_CMD_REAPPLY; video_driver_reinit(flags); /* Poll input to avoid possibly stale data to corrupt things. */ - if ( input_driver_st->primary_joypad && - input_driver_st->primary_joypad->poll) - input_driver_st->primary_joypad->poll(); -#ifdef HAVE_MFI - if ( input_driver_st->secondary_joypad && - input_driver_st->secondary_joypad->poll) - input_driver_st->secondary_joypad->poll(); -#endif + if ( joypad && joypad->poll) + joypad->poll(); + if ( sec_joypad && sec_joypad->poll) + sec_joypad->poll(); if ( p_rarch->input_driver_state.current_driver && p_rarch->input_driver_state.current_driver->poll) p_rarch->input_driver_state.current_driver->poll(p_rarch->input_driver_state.current_data); @@ -21442,6 +21452,8 @@ static void input_poll_overlay( unsigned device = ol->active->full_screen ? RARCH_DEVICE_POINTER_SCREEN : RETRO_DEVICE_POINTER; + const input_device_driver_t + *joypad = input_driver_st->primary_joypad; #ifdef HAVE_MFI const input_device_driver_t *sec_joypad = input_driver_st->secondary_joypad; @@ -21457,7 +21469,7 @@ static void input_poll_overlay( for (i = 0; current_input->input_state( input_data, - input_driver_st->primary_joypad, + joypad, sec_joypad, &joypad_info, NULL, @@ -21471,7 +21483,7 @@ static void input_poll_overlay( input_overlay_state_t polled_data; int16_t x = current_input->input_state( input_data, - input_driver_st->primary_joypad, + joypad, sec_joypad, &joypad_info, NULL, @@ -21482,7 +21494,7 @@ static void input_poll_overlay( RETRO_DEVICE_ID_POINTER_X); int16_t y = current_input->input_state( input_data, - input_driver_st->primary_joypad, + joypad, sec_joypad, &joypad_info, NULL, @@ -21961,8 +21973,11 @@ static void input_driver_poll(void) size_t i, j; rarch_joypad_info_t joypad_info[MAX_USERS]; struct rarch_state *p_rarch = &rarch_st; - input_driver_state_t *input_driver_st = &p_rarch->input_driver_state; + input_driver_state_t + *input_driver_st = &p_rarch->input_driver_state; settings_t *settings = p_rarch->configuration_settings; + const input_device_driver_t + *joypad = input_driver_st->primary_joypad; #ifdef HAVE_MFI const input_device_driver_t *sec_joypad = input_driver_st->secondary_joypad; @@ -21976,14 +21991,10 @@ static void input_driver_poll(void) bool input_remap_binds_enable = settings->bools.input_remap_binds_enable; uint8_t max_users = (uint8_t)settings->uints.input_max_users; - if ( input_driver_st->primary_joypad - && input_driver_st->primary_joypad->poll) - input_driver_st->primary_joypad->poll(); -#ifdef HAVE_MFI - if ( input_driver_st->secondary_joypad - && input_driver_st->secondary_joypad->poll) - input_driver_st->secondary_joypad->poll(); -#endif + if ( joypad && joypad->poll) + joypad->poll(); + if ( sec_joypad && sec_joypad->poll) + sec_joypad->poll(); if ( p_rarch->input_driver_state.current_driver && p_rarch->input_driver_state.current_driver->poll) p_rarch->input_driver_state.current_driver->poll(p_rarch->input_driver_state.current_data); @@ -22010,7 +22021,7 @@ static void input_driver_poll(void) input_state_wrap( p_rarch->input_driver_state.current_driver, p_rarch->input_driver_state.current_data, - input_driver_st->primary_joypad, + joypad, sec_joypad, &joypad_info[i], p_rarch->libretro_input_binds, @@ -22066,8 +22077,8 @@ static void input_driver_poll(void) bool poll_overlay = (p_rarch->overlay_ptr && p_rarch->overlay_ptr->alive); #endif input_mapper_t *handle = &p_rarch->input_driver_mapper; - const input_device_driver_t *joypad_driver - = input_driver_st->primary_joypad; + const input_device_driver_t + *joypad = input_driver_st->primary_joypad; float input_analog_deadzone = settings->floats.input_analog_deadzone; float input_analog_sensitivity = settings->floats.input_analog_sensitivity; @@ -22103,7 +22114,7 @@ static void input_driver_poll(void) case RETRO_DEVICE_JOYPAD: case RETRO_DEVICE_ANALOG: BIT256_CLEAR_ALL_PTR(¤t_inputs); - if (joypad_driver) + if (joypad) { unsigned k, j; int16_t ret = input_state_wrap( @@ -22130,7 +22141,8 @@ static void input_driver_poll(void) input_joypad_analog_button( input_analog_deadzone, input_analog_sensitivity, - joypad_driver, &joypad_info[i], + joypad, + &joypad_info[i], k, &p_rarch->libretro_input_binds[i][k] ); @@ -22154,7 +22166,7 @@ static void input_driver_poll(void) input_analog_dpad_mode, input_analog_deadzone, input_analog_sensitivity, - joypad_driver, + joypad, &joypad_info[i], k, j, @@ -25280,28 +25292,29 @@ static bool input_mouse_button_raw( unsigned port, unsigned id) { rarch_joypad_info_t joypad_info; - input_driver_state_t *input_driver_st = &p_rarch->input_driver_state; - settings_t *settings = p_rarch->configuration_settings; + input_driver_state_t *input_driver_st= &p_rarch->input_driver_state; + settings_t *settings= p_rarch->configuration_settings; + const input_device_driver_t *joypad = input_driver_st->primary_joypad; #ifdef HAVE_MFI const input_device_driver_t - *sec_joypad = input_driver_st->secondary_joypad; + *sec_joypad = input_driver_st->secondary_joypad; #else const input_device_driver_t - *sec_joypad = NULL; + *sec_joypad = NULL; #endif /*ignore axes*/ if (id == RETRO_DEVICE_ID_MOUSE_X || id == RETRO_DEVICE_ID_MOUSE_Y) return false; - joypad_info.axis_threshold = settings->floats.input_axis_threshold; - joypad_info.joy_idx = joy_idx; - joypad_info.auto_binds = input_autoconf_binds[joy_idx]; + joypad_info.axis_threshold = settings->floats.input_axis_threshold; + joypad_info.joy_idx = joy_idx; + joypad_info.auto_binds = input_autoconf_binds[joy_idx]; if (current_input->input_state) return current_input->input_state( p_rarch->input_driver_state.current_data, - input_driver_st->primary_joypad, + joypad, sec_joypad, &joypad_info, p_rarch->libretro_input_binds, @@ -29847,13 +29860,13 @@ static void video_driver_free_internal(struct rarch_state *p_rarch) #ifdef HAVE_MFI if (input_driver_st->secondary_joypad) { - const input_device_driver_t *tmp = input_driver_st->sec_joypad; + const input_device_driver_t *tmp = input_driver_st->secondary_joypad; input_driver_st->secondary_joypad = NULL; tmp->destroy(); } #endif p_rarch->keyboard_mapping_blocked = false; - p_rarch->input_driver_state.current_data = NULL; + p_rarch->input_driver_state.current_data = NULL; } if (p_rarch->video_driver_data @@ -37297,6 +37310,8 @@ static enum runloop_state runloop_check_state( #else bool menu_input_active = false; #endif + const input_device_driver_t *joypad = + input_driver_st->primary_joypad; #ifdef HAVE_MFI const input_device_driver_t *sec_joypad = input_driver_st->secondary_joypad; @@ -37405,7 +37420,7 @@ static enum runloop_state runloop_check_state( { if (current_input->input_state( p_rarch->input_driver_state.current_data, - input_driver_st->primary_joypad, + joypad, sec_joypad, &joypad_info, &binds, p_rarch->keyboard_mapping_blocked, From a9f1dd3647f260567b19bac573c75b4b3b454359 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 29 Aug 2021 18:41:58 +0200 Subject: [PATCH 4/6] input_driver_poll; remove shadowed variable menu_input_read_mouse_hw - cleanup + go through local pointer variable for primary_joypad --- retroarch.c | 104 ++++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 51 deletions(-) diff --git a/retroarch.c b/retroarch.c index e8a2578ca9..69c0635894 100644 --- a/retroarch.c +++ b/retroarch.c @@ -22077,8 +22077,6 @@ static void input_driver_poll(void) bool poll_overlay = (p_rarch->overlay_ptr && p_rarch->overlay_ptr->alive); #endif input_mapper_t *handle = &p_rarch->input_driver_mapper; - const input_device_driver_t - *joypad = input_driver_st->primary_joypad; float input_analog_deadzone = settings->floats.input_analog_deadzone; float input_analog_sensitivity = settings->floats.input_analog_sensitivity; @@ -23099,63 +23097,67 @@ static int16_t menu_input_read_mouse_hw( struct rarch_state *p_rarch, enum menu_input_mouse_hw_id id) { - rarch_joypad_info_t joypad_info; - unsigned type = 0; - unsigned device = RETRO_DEVICE_MOUSE; input_driver_state_t *input_driver_st = &p_rarch->input_driver_state; input_driver_t *current_input = input_driver_st->current_driver; + + if (current_input->input_state) + { + rarch_joypad_info_t joypad_info; + unsigned type = 0; + unsigned device = RETRO_DEVICE_MOUSE; + const input_device_driver_t + *joypad = input_driver_st->primary_joypad; #ifdef HAVE_MFI - const input_device_driver_t - *sec_joypad = input_driver_st->secondary_joypad; + const input_device_driver_t + *sec_joypad = input_driver_st->secondary_joypad; #else - const input_device_driver_t - *sec_joypad = NULL; + const input_device_driver_t + *sec_joypad = NULL; #endif - joypad_info.joy_idx = 0; - joypad_info.auto_binds = NULL; - joypad_info.axis_threshold = 0.0f; + joypad_info.joy_idx = 0; + joypad_info.auto_binds = NULL; + joypad_info.axis_threshold = 0.0f; - switch (id) - { - case MENU_MOUSE_X_AXIS: - device = RARCH_DEVICE_MOUSE_SCREEN; - type = RETRO_DEVICE_ID_MOUSE_X; - break; - case MENU_MOUSE_Y_AXIS: - device = RARCH_DEVICE_MOUSE_SCREEN; - type = RETRO_DEVICE_ID_MOUSE_Y; - break; - case MENU_MOUSE_LEFT_BUTTON: - type = RETRO_DEVICE_ID_MOUSE_LEFT; - break; - case MENU_MOUSE_RIGHT_BUTTON: - type = RETRO_DEVICE_ID_MOUSE_RIGHT; - break; - case MENU_MOUSE_WHEEL_UP: - type = RETRO_DEVICE_ID_MOUSE_WHEELUP; - break; - case MENU_MOUSE_WHEEL_DOWN: - type = RETRO_DEVICE_ID_MOUSE_WHEELDOWN; - break; - case MENU_MOUSE_HORIZ_WHEEL_UP: - type = RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP; - break; - case MENU_MOUSE_HORIZ_WHEEL_DOWN: - type = RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN; - break; + switch (id) + { + case MENU_MOUSE_X_AXIS: + device = RARCH_DEVICE_MOUSE_SCREEN; + type = RETRO_DEVICE_ID_MOUSE_X; + break; + case MENU_MOUSE_Y_AXIS: + device = RARCH_DEVICE_MOUSE_SCREEN; + type = RETRO_DEVICE_ID_MOUSE_Y; + break; + case MENU_MOUSE_LEFT_BUTTON: + type = RETRO_DEVICE_ID_MOUSE_LEFT; + break; + case MENU_MOUSE_RIGHT_BUTTON: + type = RETRO_DEVICE_ID_MOUSE_RIGHT; + break; + case MENU_MOUSE_WHEEL_UP: + type = RETRO_DEVICE_ID_MOUSE_WHEELUP; + break; + case MENU_MOUSE_WHEEL_DOWN: + type = RETRO_DEVICE_ID_MOUSE_WHEELDOWN; + break; + case MENU_MOUSE_HORIZ_WHEEL_UP: + type = RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP; + break; + case MENU_MOUSE_HORIZ_WHEEL_DOWN: + type = RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN; + break; + } + return current_input->input_state( + input_driver_st->current_data, + joypad, + sec_joypad, + &joypad_info, + NULL, + p_rarch->keyboard_mapping_blocked, + 0, device, 0, type); } - - if (!current_input->input_state) - return 0; - return current_input->input_state( - input_driver_st->current_data, - input_driver_st->primary_joypad, - sec_joypad, - &joypad_info, - NULL, - p_rarch->keyboard_mapping_blocked, - 0, device, 0, type); + return 0; } static void menu_input_get_mouse_hw_state( From 0a1affba7a0aa58f33a178dc54006e212a99655e Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 29 Aug 2021 19:05:38 +0200 Subject: [PATCH 5/6] Simplify menu_input_get_mouse_hw_state/menu_input_read_mouse_hw --- retroarch.c | 262 ++++++++++++++++++++++++++-------------------------- 1 file changed, 133 insertions(+), 129 deletions(-) diff --git a/retroarch.c b/retroarch.c index 69c0635894..a1f05e85a4 100644 --- a/retroarch.c +++ b/retroarch.c @@ -23094,70 +23094,65 @@ static void menu_input_driver_toggle( } static int16_t menu_input_read_mouse_hw( - struct rarch_state *p_rarch, + input_driver_t *current_input, + input_driver_state_t *input_driver_st, + bool keyboard_mapping_blocked, enum menu_input_mouse_hw_id id) { - input_driver_state_t *input_driver_st = &p_rarch->input_driver_state; - input_driver_t *current_input = input_driver_st->current_driver; - - if (current_input->input_state) - { - rarch_joypad_info_t joypad_info; - unsigned type = 0; - unsigned device = RETRO_DEVICE_MOUSE; - const input_device_driver_t - *joypad = input_driver_st->primary_joypad; + rarch_joypad_info_t joypad_info; + unsigned type = 0; + unsigned device = RETRO_DEVICE_MOUSE; + const input_device_driver_t + *joypad = input_driver_st->primary_joypad; #ifdef HAVE_MFI - const input_device_driver_t - *sec_joypad = input_driver_st->secondary_joypad; + const input_device_driver_t + *sec_joypad = input_driver_st->secondary_joypad; #else - const input_device_driver_t - *sec_joypad = NULL; + const input_device_driver_t + *sec_joypad = NULL; #endif - joypad_info.joy_idx = 0; - joypad_info.auto_binds = NULL; - joypad_info.axis_threshold = 0.0f; + joypad_info.joy_idx = 0; + joypad_info.auto_binds = NULL; + joypad_info.axis_threshold = 0.0f; - switch (id) - { - case MENU_MOUSE_X_AXIS: - device = RARCH_DEVICE_MOUSE_SCREEN; - type = RETRO_DEVICE_ID_MOUSE_X; - break; - case MENU_MOUSE_Y_AXIS: - device = RARCH_DEVICE_MOUSE_SCREEN; - type = RETRO_DEVICE_ID_MOUSE_Y; - break; - case MENU_MOUSE_LEFT_BUTTON: - type = RETRO_DEVICE_ID_MOUSE_LEFT; - break; - case MENU_MOUSE_RIGHT_BUTTON: - type = RETRO_DEVICE_ID_MOUSE_RIGHT; - break; - case MENU_MOUSE_WHEEL_UP: - type = RETRO_DEVICE_ID_MOUSE_WHEELUP; - break; - case MENU_MOUSE_WHEEL_DOWN: - type = RETRO_DEVICE_ID_MOUSE_WHEELDOWN; - break; - case MENU_MOUSE_HORIZ_WHEEL_UP: - type = RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP; - break; - case MENU_MOUSE_HORIZ_WHEEL_DOWN: - type = RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN; - break; - } - return current_input->input_state( - input_driver_st->current_data, - joypad, - sec_joypad, - &joypad_info, - NULL, - p_rarch->keyboard_mapping_blocked, - 0, device, 0, type); + switch (id) + { + case MENU_MOUSE_X_AXIS: + device = RARCH_DEVICE_MOUSE_SCREEN; + type = RETRO_DEVICE_ID_MOUSE_X; + break; + case MENU_MOUSE_Y_AXIS: + device = RARCH_DEVICE_MOUSE_SCREEN; + type = RETRO_DEVICE_ID_MOUSE_Y; + break; + case MENU_MOUSE_LEFT_BUTTON: + type = RETRO_DEVICE_ID_MOUSE_LEFT; + break; + case MENU_MOUSE_RIGHT_BUTTON: + type = RETRO_DEVICE_ID_MOUSE_RIGHT; + break; + case MENU_MOUSE_WHEEL_UP: + type = RETRO_DEVICE_ID_MOUSE_WHEELUP; + break; + case MENU_MOUSE_WHEEL_DOWN: + type = RETRO_DEVICE_ID_MOUSE_WHEELDOWN; + break; + case MENU_MOUSE_HORIZ_WHEEL_UP: + type = RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP; + break; + case MENU_MOUSE_HORIZ_WHEEL_DOWN: + type = RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN; + break; } - return 0; + return current_input->input_state( + input_driver_st->current_data, + joypad, + sec_joypad, + &joypad_info, + NULL, + keyboard_mapping_blocked, + 0, device, 0, type); } static void menu_input_get_mouse_hw_state( @@ -23169,8 +23164,13 @@ static void menu_input_get_mouse_hw_state( static int16_t last_y = 0; static bool last_select_pressed = false; static bool last_cancel_pressed = false; + input_driver_state_t + *input_driver_st = &p_rarch->input_driver_state; + input_driver_t + *current_input = input_driver_st->current_driver; bool mouse_enabled = settings->bools.menu_mouse_enable; menu_handle_t *menu = p_rarch->menu_driver_data; + bool keyboard_mapping_blocked = p_rarch->keyboard_mapping_blocked; bool menu_has_fb = (menu && menu->driver_ctx && @@ -23181,103 +23181,107 @@ static void menu_input_get_mouse_hw_state( bool overlay_active = overlay_enable && p_rarch->overlay_ptr && p_rarch->overlay_ptr->alive; if (overlay_active) - mouse_enabled = false; + mouse_enabled = false; #endif + bool state_inited = current_input && + current_input->input_state; /* Easiest to set inactive by default, and toggle * when input is detected */ - hw_state->active = false; - + hw_state->active = false; + hw_state->x = 0; + hw_state->y = 0; + hw_state->select_pressed = false; + hw_state->cancel_pressed = false; + hw_state->up_pressed = false; + hw_state->down_pressed = false; + hw_state->left_pressed = false; + hw_state->right_pressed = false; if (!mouse_enabled) - { - hw_state->x = 0; - hw_state->y = 0; - hw_state->select_pressed = false; - hw_state->cancel_pressed = false; - hw_state->up_pressed = false; - hw_state->down_pressed = false; - hw_state->left_pressed = false; - hw_state->right_pressed = false; return; + + /* X/Y position */ + if (state_inited) + { + if ((hw_state->x = menu_input_read_mouse_hw(current_input, + input_driver_st, + keyboard_mapping_blocked, MENU_MOUSE_X_AXIS)) != last_x) + hw_state->active = true; + if ((hw_state->y = menu_input_read_mouse_hw(current_input, + input_driver_st, + keyboard_mapping_blocked, MENU_MOUSE_Y_AXIS)) != last_y) + hw_state->active = true; } - /* X pos */ - hw_state->x = menu_input_read_mouse_hw(p_rarch, MENU_MOUSE_X_AXIS); - if (hw_state->x != last_x) - hw_state->active = true; - last_x = hw_state->x; + last_x = hw_state->x; + last_y = hw_state->y; - /* Y pos */ - hw_state->y = menu_input_read_mouse_hw(p_rarch, MENU_MOUSE_Y_AXIS); - if (hw_state->y != last_y) - hw_state->active = true; - last_y = hw_state->y; - - /* > X/Y adjustment */ + /* > X/Y position adjustment */ if (menu_has_fb) { /* RGUI uses a framebuffer texture + custom viewports, * which means we have to convert from screen space to * menu space... */ - struct video_viewport vp = {0}; - gfx_display_t *p_disp = &p_rarch->dispgfx; + struct video_viewport vp = {0}; + gfx_display_t *p_disp = &p_rarch->dispgfx; /* Read display/framebuffer info */ - unsigned fb_width = p_disp->framebuf_width; - unsigned fb_height = p_disp->framebuf_height; + unsigned fb_width = p_disp->framebuf_width; + unsigned fb_height = p_disp->framebuf_height; video_driver_get_viewport_info(&vp); - /* Adjust X pos */ - hw_state->x = (int16_t)(((float)(hw_state->x - vp.x) / (float)vp.width) * (float)fb_width); - hw_state->x = hw_state->x < 0 ? 0 : hw_state->x; - hw_state->x = hw_state->x >= fb_width ? fb_width - 1 : hw_state->x; + /* Adjust X position */ + hw_state->x = (int16_t)(((float)(hw_state->x - vp.x) / (float)vp.width) * (float)fb_width); + hw_state->x = (hw_state->x < 0) ? (0 ) : hw_state->x; + hw_state->x = (hw_state->x >= fb_width) ? (fb_width -1) : hw_state->x; - /* Adjust Y pos */ - hw_state->y = (int16_t)(((float)(hw_state->y - vp.y) / (float)vp.height) * (float)fb_height); - hw_state->y = hw_state->y < 0 ? 0 : hw_state->y; - hw_state->y = hw_state->y >= fb_height ? fb_height - 1 : hw_state->y; + /* Adjust Y position */ + hw_state->y = (int16_t)(((float)(hw_state->y - vp.y) / (float)vp.height) * (float)fb_height); + hw_state->y = (hw_state->y < 0) ? (0 ) : hw_state->y; + hw_state->y = (hw_state->y >= fb_height) ? (fb_height-1) : hw_state->y; + } + + if (state_inited) + { + /* Select (LMB) + * Note that releasing select also counts as activity */ + hw_state->select_pressed = (bool) + menu_input_read_mouse_hw(current_input, input_driver_st, + keyboard_mapping_blocked, MENU_MOUSE_LEFT_BUTTON); + /* Cancel (RMB) + * Note that releasing cancel also counts as activity */ + hw_state->cancel_pressed = (bool) + menu_input_read_mouse_hw(current_input, input_driver_st, + keyboard_mapping_blocked, MENU_MOUSE_RIGHT_BUTTON); + /* Up (mouse wheel up) */ + if ((hw_state->up_pressed = (bool) + menu_input_read_mouse_hw(current_input, input_driver_st, + keyboard_mapping_blocked, MENU_MOUSE_WHEEL_UP))) + hw_state->active = true; + /* Down (mouse wheel down) */ + if ((hw_state->down_pressed = (bool) + menu_input_read_mouse_hw(current_input, input_driver_st, + keyboard_mapping_blocked, MENU_MOUSE_WHEEL_DOWN))) + hw_state->active = true; + /* Left (mouse wheel horizontal left) */ + if ((hw_state->left_pressed = (bool) + menu_input_read_mouse_hw(current_input, input_driver_st, + keyboard_mapping_blocked, MENU_MOUSE_HORIZ_WHEEL_DOWN))) + hw_state->active = true; + /* Right (mouse wheel horizontal right) */ + if ((hw_state->right_pressed = (bool) + menu_input_read_mouse_hw(current_input, input_driver_st, + keyboard_mapping_blocked, MENU_MOUSE_HORIZ_WHEEL_UP))) + hw_state->active = true; } - /* Select (LMB) - * Note that releasing select also counts as activity */ - hw_state->select_pressed = (bool) - menu_input_read_mouse_hw(p_rarch, MENU_MOUSE_LEFT_BUTTON); if (hw_state->select_pressed || (hw_state->select_pressed != last_select_pressed)) - hw_state->active = true; - last_select_pressed = hw_state->select_pressed; - - /* Cancel (RMB) - * Note that releasing cancel also counts as activity */ - hw_state->cancel_pressed = (bool) - menu_input_read_mouse_hw(p_rarch, MENU_MOUSE_RIGHT_BUTTON); + hw_state->active = true; if (hw_state->cancel_pressed || (hw_state->cancel_pressed != last_cancel_pressed)) - hw_state->active = true; - last_cancel_pressed = hw_state->cancel_pressed; - - /* Up (mouse wheel up) */ - hw_state->up_pressed = (bool) - menu_input_read_mouse_hw(p_rarch, MENU_MOUSE_WHEEL_UP); - if (hw_state->up_pressed) - hw_state->active = true; - - /* Down (mouse wheel down) */ - hw_state->down_pressed = (bool) - menu_input_read_mouse_hw(p_rarch, MENU_MOUSE_WHEEL_DOWN); - if (hw_state->down_pressed) - hw_state->active = true; - - /* Left (mouse wheel horizontal left) */ - hw_state->left_pressed = (bool) - menu_input_read_mouse_hw(p_rarch, MENU_MOUSE_HORIZ_WHEEL_DOWN); - if (hw_state->left_pressed) - hw_state->active = true; - - /* Right (mouse wheel horizontal right) */ - hw_state->right_pressed = (bool) - menu_input_read_mouse_hw(p_rarch, MENU_MOUSE_HORIZ_WHEEL_UP); - if (hw_state->right_pressed) - hw_state->active = true; + hw_state->active = true; + last_select_pressed = hw_state->select_pressed; + last_cancel_pressed = hw_state->cancel_pressed; } static void menu_input_get_touchscreen_hw_state( From 24721e6a1dee280ea5c26952b63c040af6c43f67 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 29 Aug 2021 20:37:09 +0200 Subject: [PATCH 6/6] C89 buildfix for MSVC --- retroarch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/retroarch.c b/retroarch.c index a1f05e85a4..86cd2f1bd3 100644 --- a/retroarch.c +++ b/retroarch.c @@ -23175,6 +23175,8 @@ static void menu_input_get_mouse_hw_state( (menu && menu->driver_ctx && menu->driver_ctx->set_texture); + bool state_inited = current_input && + current_input->input_state; #ifdef HAVE_OVERLAY bool overlay_enable = settings->bools.input_overlay_enable; /* Menu pointer controls are ignored when overlays are enabled. */ @@ -23183,8 +23185,6 @@ static void menu_input_get_mouse_hw_state( if (overlay_active) mouse_enabled = false; #endif - bool state_inited = current_input && - current_input->input_state; /* Easiest to set inactive by default, and toggle * when input is detected */