From c956924edb771ae3c70c58788ee8715d38b7d9b1 Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Wed, 11 Oct 2017 13:56:40 -0400 Subject: [PATCH] allow hotplug to actually hotplug --- include/joy.h | 1 + src/io/joy.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++++ src/osapi/osapi.cpp | 7 ++-- 3 files changed, 89 insertions(+), 3 deletions(-) diff --git a/include/joy.h b/include/joy.h index cba7d13..2c6948c 100644 --- a/include/joy.h +++ b/include/joy.h @@ -105,6 +105,7 @@ extern int Dead_zone_size; // percentage of range that is dead zone int joy_init(); void joy_close(); +void joy_reinit(int with_index = -1); void joy_flush(); int joy_get_pos(int * x, int * y, int *z, int *r); int joy_down_count(int btn, int reset_count = 1); diff --git a/src/io/joy.cpp b/src/io/joy.cpp index c3e989e..fea096d 100644 --- a/src/io/joy.cpp +++ b/src/io/joy.cpp @@ -412,6 +412,90 @@ int joy_init() return num_sticks; } +void joy_reinit(int with_index) +{ + int i, num_sticks; + const char *ptr = NULL; + int Cur_joystick; + + if ( !Joy_inited ) { + joy_init(); + return; + } + + // close out what we have already opened... + joy_ff_shutdown(); + + joy_id = -1; + + if (SDL_JoystickGetAttached(sdljoy)) { + SDL_JoystickClose(sdljoy); + } + + sdljoy = NULL; + + // attempt to get a new joystick to use... + mprintf(("Re-Initializing Joystick...\n")); + + num_sticks = SDL_NumJoysticks(); + + if (num_sticks < 1) { + mprintf((" No joysticks found\n\n")); + return; + } + + if ( (with_index >= 0) && (with_index < num_sticks) ) { + Cur_joystick = with_index; + } else { + Cur_joystick = 0; + + ptr = os_config_read_string("Controls", "CurrentJoystick", NULL); + + if ( ptr && SDL_strlen(ptr) ) { + for (i = 0; i < num_sticks; i++) { + const char *jname = SDL_JoystickNameForIndex(i); + + if ( jname && !SDL_strcasecmp(ptr, jname) ) { + Cur_joystick = i; + break; + } + } + } + } + + sdljoy = SDL_JoystickOpen(Cur_joystick); + + if (sdljoy == NULL) { + mprintf((" Unable to init joystick %d (%s)\n\n", Cur_joystick, SDL_JoystickNameForIndex(Cur_joystick))); + return; + } + + mprintf((" Name : %s\n", SDL_JoystickName(sdljoy))); + mprintf((" Axes : %d\n", SDL_JoystickNumAxes(sdljoy))); + mprintf((" Buttons : %d\n", SDL_JoystickNumButtons(sdljoy))); + mprintf((" Hats : %d\n", SDL_JoystickNumHats(sdljoy))); + mprintf((" Haptic : %s\n", SDL_JoystickIsHaptic(sdljoy) ? "Yes" : "No")); + + joy_ff_init(); + + mprintf(("\n")); + + joy_id = SDL_JoystickInstanceID(sdljoy); + + joystick.num_axes = SDL_JoystickNumAxes(sdljoy); + + joy_flush(); + + // Fake a calibration + joy_set_cen(); + + for (i = 0; i < JOY_NUM_AXES; i++) { + joystick.axis_min[i] = 0; + joystick.axis_max[i] = 65536; + joystick.axis_current[i] = joystick.axis_center[i]; + } +} + void joy_set_cen() { if ( !Joy_inited ) { diff --git a/src/osapi/osapi.cpp b/src/osapi/osapi.cpp index 70ce708..a30f85b 100644 --- a/src/osapi/osapi.cpp +++ b/src/osapi/osapi.cpp @@ -395,15 +395,16 @@ void os_poll() */ case SDL_JOYDEVICEADDED: { if ( !Is_standalone ) { - joy_init(); + joy_reinit(e.jdevice.which); } break; } case SDL_JOYDEVICEREMOVED: { - if (e.jdevice.which == joystick_get_id()) { - joy_close(); + // if the active joystick is removed then maybe reinit + if ( !Is_standalone && (e.jdevice.which == joystick_get_id()) ) { + joy_reinit(); } break; -- 2.39.2