From 5287559aeebbe34e86afad7b80b8f79d036ec1a8 Mon Sep 17 00:00:00 2001 From: krcroft Date: Sun, 19 Apr 2020 11:49:15 -0700 Subject: [PATCH] Don't initialize and manage joysticks in SDLmain Two notable changes: 1. Eliminates the joystick setup from SDLmain, allowing the mapper's QueryJoysticks() function to be the sole setup and configuration point for both the SDL Joystick subsystem and DOSBox's internal configuration of the joysticks. 2. SDLmain's event loop previously perform some joystick- specific timing and always called the Mapper's UpdateJoysticks function; neither of which are needed if the user has disabled joystick support or if joysticks aren't physically present. This update now make this entire process conditional on both of the latter (which is set by the Mapper's QueryJoysticks). --- include/mapper.h | 3 ++- src/gui/sdl_mapper.cpp | 11 ++++++++--- src/gui/sdlmain.cpp | 26 +++++++++----------------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/include/mapper.h b/include/mapper.h index 674a9363..975d865d 100644 --- a/include/mapper.h +++ b/include/mapper.h @@ -36,7 +36,8 @@ void MAPPER_BindKeys(); void MAPPER_StartUp(Section * sec); void MAPPER_Run(bool pressed); void MAPPER_DisplayUI(); -void MAPPER_LosingFocus(void); +void MAPPER_LosingFocus(); +bool MAPPER_IsUsingJoysticks(); std::vector MAPPER_GetEventNames(const std::string &prefix); void MAPPER_AutoType(std::vector &sequence, const uint32_t wait_ms, diff --git a/src/gui/sdl_mapper.cpp b/src/gui/sdl_mapper.cpp index 0d549c8d..e884de5a 100644 --- a/src/gui/sdl_mapper.cpp +++ b/src/gui/sdl_mapper.cpp @@ -2500,9 +2500,10 @@ void BIND_MappingEvents() { /** * Queries SDL's joysticks and sets joytype accordingly. * If no joysticks are valid then joytype is left at JOY_NONE. - * Also resets mapper.sticks.num_groups to 0 and - * mapper.sticks.num to the number of found SDL joysticks. - */ + * Also resets mapper.sticks.num_groups to 0, mapper.sticks.num + * to the number of found SDL joysticks, and enables the boolean + * joysticks_active if joystick support is enabled and are present. + */ static void QueryJoysticks() { // Initialize SDL's Joystick and Event subsystems, if needed if (SDL_WasInit(SDL_INIT_JOYSTICK) != SDL_INIT_JOYSTICK) @@ -2597,6 +2598,10 @@ static void CreateBindGroups() { } } +bool MAPPER_IsUsingJoysticks() { + return (mapper.sticks.num > 0); +} + #if defined (REDUCE_JOYSTICK_POLLING) void MAPPER_UpdateJoysticks() { for (Bitu i=0; i 20) { - poll_delay = time; - if (sdl.num_joysticks > 0) SDL_JoystickUpdate(); - MAPPER_UpdateJoysticks(); + if (MAPPER_IsUsingJoysticks()) { + static int poll_delay = 0; + int time = GetTicks(); + if (time - poll_delay > 20) { + poll_delay = time; + SDL_JoystickUpdate(); + MAPPER_UpdateJoysticks(); + } } #endif while (SDL_PollEvent(&event)) { @@ -3151,18 +3152,9 @@ int main(int argc, char* argv[]) { // Once initialized, ensure we clean up SDL for all exit conditions atexit(SDL_Quit); -#ifndef DISABLE_JOYSTICK - //Initialise Joystick separately. This way we can warn when it fails instead - //of exiting the application - if( SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0 ) - LOG_MSG("Failed to init joystick support"); -#endif - sdl.laltstate = SDL_KEYUP; sdl.raltstate = SDL_KEYUP; - sdl.num_joysticks=SDL_NumJoysticks(); - CROSS_DetermineConfigPaths(); /* Parse configuration files */