Skip to content

Commit

Permalink
Merge pull request #78 from bugfood/improve-startup
Browse files Browse the repository at this point in the history
Improve input-sdl startup time.
  • Loading branch information
richard42 authored Jan 29, 2019
2 parents 2320005 + 097fc83 commit 25c8ffc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 44 deletions.
38 changes: 1 addition & 37 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,6 @@ static const char * get_sdl_joystick_name(int iCtrlIdx)
{
static char JoyName[256];
const char *joySDLName;
int joyWasInit = SDL_WasInit(SDL_INIT_JOYSTICK);

/* initialize the joystick subsystem if necessary */
if (!joyWasInit)
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1)
{
DebugMessage(M64MSG_ERROR, "Couldn't init SDL joystick subsystem: %s", SDL_GetError() );
return NULL;
}

/* get the name of the corresponding joystick */
joySDLName = SDL_JoystickName(iCtrlIdx);
Expand All @@ -141,40 +132,13 @@ static const char * get_sdl_joystick_name(int iCtrlIdx)
JoyName[255] = 0;
}

/* quit the joystick subsystem if necessary */
if (!joyWasInit)
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);

/* if the SDL function had an error, then return NULL, otherwise return local copy of joystick name */
if (joySDLName == NULL)
return NULL;
else
return JoyName;
}

static int get_sdl_num_joysticks(void)
{
int numJoysticks = 0;
int joyWasInit = SDL_WasInit(SDL_INIT_JOYSTICK);

/* initialize the joystick subsystem if necessary */
if (!joyWasInit)
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1)
{
DebugMessage(M64MSG_ERROR, "Couldn't init SDL joystick subsystem: %s", SDL_GetError() );
return 0;
}

/* get thenumber of joysticks */
numJoysticks = SDL_NumJoysticks();

/* quit the joystick subsystem if necessary */
if (!joyWasInit)
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);

return numJoysticks;
}

/////////////////////////////////////
// load_controller_config()
// return value: 1 = OK
Expand Down Expand Up @@ -540,7 +504,7 @@ void load_configuration(int bPreConfig)
int ControlDevice[4];
char DeviceName[4][256];
int ActiveControllers = 0;
int sdlNumJoysticks = get_sdl_num_joysticks();
int sdlNumJoysticks = SDL_NumJoysticks();
float fVersion = 0.0f;
const char *sdl_name;
int ControllersFound = 0;
Expand Down
29 changes: 22 additions & 7 deletions src/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ EXPORT m64p_error CALL PluginStartup(m64p_dynlib_handle CoreLibHandle, void *Con
ptr_CoreGetAPIVersions CoreAPIVersionFunc;

int i, ConfigAPIVersion, DebugAPIVersion, VidextAPIVersion;
int joyWasInit;

if (l_PluginInit)
return M64ERR_ALREADY_INIT;
Expand Down Expand Up @@ -206,9 +207,22 @@ EXPORT m64p_error CALL PluginStartup(m64p_dynlib_handle CoreLibHandle, void *Con
for (i = 0; i < 4; i++)
controller[i].control = temp_core_controlinfo + i;

/* initialize the joystick subsystem if necessary */
joyWasInit = SDL_WasInit(SDL_INIT_JOYSTICK);
if (!joyWasInit)
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1)
{
DebugMessage(M64MSG_ERROR, "Couldn't init SDL joystick subsystem: %s", SDL_GetError() );
return M64ERR_SYSTEM_FAIL;
}

/* read plugin config from core config database, auto-config if necessary and update core database */
load_configuration(1);

/* quit the joystick subsystem if necessary */
if (!joyWasInit)
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);

l_PluginInit = 1;
return M64ERR_SUCCESS;
}
Expand Down Expand Up @@ -715,13 +729,6 @@ EXPORT void CALL GetKeys( int Control, BUTTONS *Keys )

static void InitiateJoysticks(int cntrl)
{
// init SDL joystick subsystem
if (!SDL_WasInit(SDL_INIT_JOYSTICK))
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) {
DebugMessage(M64MSG_ERROR, "Couldn't init SDL joystick subsystem: %s", SDL_GetError() );
return;
}

if (controller[cntrl].device >= 0) {
controller[cntrl].joystick = SDL_JoystickOpen(controller[cntrl].device);
if (!controller[cntrl].joystick)
Expand Down Expand Up @@ -910,6 +917,14 @@ EXPORT void CALL InitiateControllers(CONTROL_INFO ControlInfo)
for (i = 0; i < 4; i++)
controller[i].control = ControlInfo.Controls + i;

/* initialize the joystick subsystem if necessary (and leave it initialized) */
if (! SDL_WasInit(SDL_INIT_JOYSTICK))
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1)
{
DebugMessage(M64MSG_ERROR, "Couldn't init SDL joystick subsystem: %s", SDL_GetError() );
return;
}

// read configuration
load_configuration(0);

Expand Down

0 comments on commit 25c8ffc

Please sign in to comment.