More fixes for #542: Some joysticks have no Axes or Buttons, skip those

This commit is contained in:
Dimitris Panokostas 2019-12-09 18:15:05 +01:00
parent fbc0149be9
commit de7d4ef617
2 changed files with 68 additions and 46 deletions

View file

@ -729,7 +729,16 @@ static int init_joystick()
for (auto cpt = 0; cpt < nr_joysticks; cpt++)
{
joysticktable[cpt] = SDL_JoystickOpen(cpt);
if (SDL_JoystickNumAxes(joysticktable[cpt]) == 0
&& SDL_JoystickNumButtons(joysticktable[cpt]) == 0)
{
if (SDL_JoystickNameForIndex(cpt) != nullptr)
strncpy(joystick_name[cpt], SDL_JoystickNameForIndex(cpt), sizeof joystick_name[cpt] - 1);
write_log("Controller %s has no Axes or Buttons - Skipping... \n", joystick_name[cpt]);
SDL_JoystickClose(joysticktable[cpt]);
joysticktable[cpt] = nullptr;
}
if (joysticktable[cpt] != nullptr)
{
if (SDL_JoystickNameForIndex(cpt) != nullptr)
@ -867,7 +876,8 @@ static void close_joystick()
{
for (auto cpt = 0; cpt < nr_joysticks; cpt++)
{
SDL_JoystickClose(joysticktable[cpt]);
if (joysticktable[cpt] != nullptr)
SDL_JoystickClose(joysticktable[cpt]);
}
nr_joysticks = 0;
}

View file

@ -615,53 +615,56 @@ void checkInput()
break;
case SDL_JOYAXISMOTION:
// Deadzone
if (std::abs(gui_event.jaxis.value) >= 10000 || std::abs(gui_event.jaxis.value) <= 5000)
if (gui_joystick)
{
int axis_state = 0;
int axis = gui_event.jaxis.axis;
int value = gui_event.jaxis.value;
if (std::abs(value) < 10000)
axis_state = 0;
else
axis_state = value > 0 ? 1 : -1;
// Deadzone
if (std::abs(gui_event.jaxis.value) >= 10000 || std::abs(gui_event.jaxis.value) <= 5000)
{
int axis_state = 0;
int axis = gui_event.jaxis.axis;
int value = gui_event.jaxis.value;
if (std::abs(value) < 10000)
axis_state = 0;
else
axis_state = value > 0 ? 1 : -1;
if (joypad_axis_state[axis] == axis_state)
{
// ignore repeated axis movement state
break;
}
joypad_axis_state[axis] = axis_state;
if (joypad_axis_state[axis] == axis_state)
{
// ignore repeated axis movement state
break;
}
joypad_axis_state[axis] = axis_state;
if (get_joypad_axis_state(host_input_buttons[0].lstick_axis_y) == -1)
{
if (HandleNavigation(DIRECTION_UP))
continue; // Don't change value when enter Slider -> don't send event to control
PushFakeKey(SDLK_UP);
break;
if (get_joypad_axis_state(host_input_buttons[0].lstick_axis_y) == -1)
{
if (HandleNavigation(DIRECTION_UP))
continue; // Don't change value when enter Slider -> don't send event to control
PushFakeKey(SDLK_UP);
break;
}
if (get_joypad_axis_state(host_input_buttons[0].lstick_axis_y) == 1)
{
if (HandleNavigation(DIRECTION_DOWN))
continue; // Don't change value when enter Slider -> don't send event to control
PushFakeKey(SDLK_DOWN);
break;
}
if (get_joypad_axis_state(host_input_buttons[0].lstick_axis_x) == 1)
{
if (HandleNavigation(DIRECTION_RIGHT))
continue; // Don't change value when enter Slider -> don't send event to control
PushFakeKey(SDLK_RIGHT);
break;
}
if (get_joypad_axis_state(host_input_buttons[0].lstick_axis_x) == -1)
{
if (HandleNavigation(DIRECTION_LEFT))
continue; // Don't change value when enter Slider -> don't send event to control
PushFakeKey(SDLK_LEFT);
break;
}
}
if (get_joypad_axis_state(host_input_buttons[0].lstick_axis_y) == 1)
{
if (HandleNavigation(DIRECTION_DOWN))
continue; // Don't change value when enter Slider -> don't send event to control
PushFakeKey(SDLK_DOWN);
break;
}
if (get_joypad_axis_state(host_input_buttons[0].lstick_axis_x) == 1)
{
if (HandleNavigation(DIRECTION_RIGHT))
continue; // Don't change value when enter Slider -> don't send event to control
PushFakeKey(SDLK_RIGHT);
break;
}
if (get_joypad_axis_state(host_input_buttons[0].lstick_axis_x) == -1)
{
if (HandleNavigation(DIRECTION_LEFT))
continue; // Don't change value when enter Slider -> don't send event to control
PushFakeKey(SDLK_LEFT);
break;
}
}
}
break;
case SDL_KEYDOWN:
@ -778,7 +781,16 @@ void amiberry_gui_run()
if (gui_joystick_control && SDL_NumJoysticks() > 0)
{
gui_joystick = SDL_JoystickOpen(0);
joypad_axis_state.assign(SDL_JoystickNumAxes(gui_joystick), 0);
// Some joysticks have no axes or buttons (e.g. Wii Remote IR), skip those
if (SDL_JoystickNumAxes(gui_joystick) > 0 && SDL_JoystickNumButtons(gui_joystick) > 0)
{
joypad_axis_state.assign(SDL_JoystickNumAxes(gui_joystick), 0);
}
else
{
SDL_JoystickClose(gui_joystick);
gui_joystick = nullptr;
}
}
// Prepare the screen once