From 918dc6db7bad46b9d3d482a4a2ad43a574cdb0c6 Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Sat, 1 Aug 2015 00:25:37 +0300 Subject: [PATCH] Fix gamepad reset Don't create duplicate keys for gamepads during reset (causing those keys to stop working) and properly offline all buttons and axes in gamepad that is offlined. --- include/core/joystickapi.hpp | 4 ++-- src/core/dummyjoy.cpp | 8 ++++---- src/core/keymapper.cpp | 6 +++--- src/library/gamepad.cpp | 17 ++++++++++++----- src/platform/evdev/joystick.cpp | 4 +++- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/include/core/joystickapi.hpp b/include/core/joystickapi.hpp index 12d926fd..7dbd25be 100644 --- a/include/core/joystickapi.hpp +++ b/include/core/joystickapi.hpp @@ -23,7 +23,7 @@ struct joystick_driver * - The call occurs in the main thread. * - Implemented by the joystick plugin. */ -void joystick_driver_init() throw(); +void joystick_driver_init(bool soft = false) throw(); /** * Joystick quit function. * @@ -31,7 +31,7 @@ void joystick_driver_init() throw(); * - The call occurs in the main thread. * - Implemented by the joystick plugin. */ -void joystick_driver_quit() throw(); +void joystick_driver_quit(bool soft = false) throw(); /** * Signal the joystick thread to quit. */ diff --git a/src/core/dummyjoy.cpp b/src/core/dummyjoy.cpp index 90fa9fc5..51d8d017 100644 --- a/src/core/dummyjoy.cpp +++ b/src/core/dummyjoy.cpp @@ -30,19 +30,19 @@ joystick_driver::joystick_driver(_joystick_driver drv) driver = drv; } -void joystick_driver_init() throw() +void joystick_driver_init(bool soft) throw() { - lsnes_gamepads_init(); + if(!soft) lsnes_gamepads_init(); driver.init(); joystick_thread_handle = new threads::thread(joystick_thread, 6); } -void joystick_driver_quit() throw() +void joystick_driver_quit(bool soft) throw() { driver.quit(); joystick_thread_handle->join(); joystick_thread_handle = NULL; - lsnes_gamepads_deinit(); + if(!soft) lsnes_gamepads_deinit(); } void joystick_driver_signal() throw() diff --git a/src/core/keymapper.cpp b/src/core/keymapper.cpp index 87a07a32..e71ed4aa 100644 --- a/src/core/keymapper.cpp +++ b/src/core/keymapper.cpp @@ -116,9 +116,9 @@ namespace command::fnptr<> reset_joysticks(lsnes_cmds, CKEYMAPPER::reset, []() throw(std::bad_alloc, std::runtime_error) { - joystick_driver_quit(); - lsnes_gamepads.offline_all(); //Not supposed to have online gamepads. - joystick_driver_init(); + joystick_driver_quit(true); + lsnes_gamepads.offline_all(); //Not supposed to have online gamepads when entering reset. + joystick_driver_init(true); messages << "Reset gamepads" << std::endl; }); diff --git a/src/library/gamepad.cpp b/src/library/gamepad.cpp index b91fc0cc..646caafa 100644 --- a/src/library/gamepad.cpp +++ b/src/library/gamepad.cpp @@ -104,19 +104,19 @@ void pad::set_online(bool status) else { online_flag = status; //Offline everything. - for(auto i : _axes) { + for(auto& i : _axes) { if(i.second.online) try { axes_off.push_back(i.first); } catch(...) {} i.second.online = false; } - for(auto i : _buttons) { + for(auto& i : _buttons) { if(i.second.online) try { buttons_off.push_back(i.first); } catch(...) {} i.second.online = false; } - for(auto i : _hats) { + for(auto& i : _hats) { if(i.second.online) try { hats_off.push_back(i.first); } catch(...) {} i.second.online = false; } - for(auto i : _axes_hat) { + for(auto& i : _axes_hat) { if(i.second->online) try { hats_off.push_back(i.first); } catch(...) {} i.second->online = false; } @@ -702,7 +702,14 @@ unsigned set::add(const std::string& name) threads::alock h(mlock); for(size_t i = 0; i < _gamepads.size(); i++) { if(!_gamepads[i]->online() && _gamepads[i]->name() == name) { - _gamepads[i]->set_online(true); + auto& gp = _gamepads[i]; + gp->set_online(true); + //Reset the functions. + gp->set_axis_cb(axis_fn); + gp->set_button_cb(button_fn); + gp->set_hat_cb(hat_fn); + gp->set_axismode_cb(amode_fn); + gp->set_newitem_cb(newitem_fn); return i; } } diff --git a/src/platform/evdev/joystick.cpp b/src/platform/evdev/joystick.cpp index 7cf6ebef..69a4581a 100644 --- a/src/platform/evdev/joystick.cpp +++ b/src/platform/evdev/joystick.cpp @@ -176,8 +176,10 @@ namespace if(r < 0 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) return 0; if(r < 0) { - if(errno == ENODEV) + if(errno == ENODEV) { + messages << "Joystick #" << gamepad_map[fd] << " disconnected." << std::endl; return -1; //Disconnected. + } messages << "Error reading from joystick (fd=" << fd << "): " << strerror(errno) << std::endl; return 0; -- 2.11.4.GIT