diff --git a/rpcs3/Input/ds3_pad_handler.cpp b/rpcs3/Input/ds3_pad_handler.cpp index 7ce3660974..6fe7691b61 100644 --- a/rpcs3/Input/ds3_pad_handler.cpp +++ b/rpcs3/Input/ds3_pad_handler.cpp @@ -519,8 +519,7 @@ PadHandlerBase::connection ds3_pad_handler::update_connection(const std::shared_ if (get_data(dev) == DataStatus::ReadError) { // this also can mean disconnected, either way deal with it on next loop and reconnect - hid_close(dev->hidDevice); - dev->hidDevice = nullptr; + dev->close(); return connection::no_data; } diff --git a/rpcs3/Input/ds4_pad_handler.cpp b/rpcs3/Input/ds4_pad_handler.cpp index 71d69609ac..1813e0e3f4 100644 --- a/rpcs3/Input/ds4_pad_handler.cpp +++ b/rpcs3/Input/ds4_pad_handler.cpp @@ -527,8 +527,7 @@ void ds4_pad_handler::check_add_device(hid_device* hidDevice, std::string_view p if (!GetCalibrationData(device)) { ds4_log.error("check_add_device: GetCalibrationData failed!"); - hid_close(hidDevice); - device->hidDevice = nullptr; + device->close(); return; } @@ -553,8 +552,7 @@ void ds4_pad_handler::check_add_device(hid_device* hidDevice, std::string_view p if (hid_set_nonblocking(hidDevice, 1) == -1) { ds4_log.error("check_add_device: hid_set_nonblocking failed! Reason: %s", hid_error(hidDevice)); - hid_close(hidDevice); - device->hidDevice = nullptr; + device->close(); return; } @@ -790,8 +788,7 @@ PadHandlerBase::connection ds4_pad_handler::update_connection(const std::shared_ if (get_data(ds4_dev) == DataStatus::ReadError) { // this also can mean disconnected, either way deal with it on next loop and reconnect - hid_close(ds4_dev->hidDevice); - ds4_dev->hidDevice = nullptr; + ds4_dev->close(); return connection::no_data; } diff --git a/rpcs3/Input/dualsense_pad_handler.cpp b/rpcs3/Input/dualsense_pad_handler.cpp index 58856f89cd..0fe5126e56 100644 --- a/rpcs3/Input/dualsense_pad_handler.cpp +++ b/rpcs3/Input/dualsense_pad_handler.cpp @@ -155,8 +155,7 @@ void dualsense_pad_handler::check_add_device(hid_device* hidDevice, std::string_ if (!get_calibration_data(device)) { dualsense_log.error("check_add_device: get_calibration_data failed!"); - hid_close(hidDevice); - device->hidDevice = nullptr; + device->close(); return; } @@ -182,8 +181,7 @@ void dualsense_pad_handler::check_add_device(hid_device* hidDevice, std::string_ if (hid_set_nonblocking(hidDevice, 1) == -1) { dualsense_log.error("check_add_device: hid_set_nonblocking failed! Reason: %s", hid_error(hidDevice)); - hid_close(hidDevice); - device->hidDevice = nullptr; + device->close(); return; } @@ -569,8 +567,7 @@ PadHandlerBase::connection dualsense_pad_handler::update_connection(const std::s if (get_data(dualsense_dev) == DataStatus::ReadError) { // this also can mean disconnected, either way deal with it on next loop and reconnect - hid_close(dualsense_dev->hidDevice); - dualsense_dev->hidDevice = nullptr; + dualsense_dev->close(); return connection::no_data; } diff --git a/rpcs3/Input/hid_pad_handler.cpp b/rpcs3/Input/hid_pad_handler.cpp index 4d8a7f63e9..447960ce1e 100644 --- a/rpcs3/Input/hid_pad_handler.cpp +++ b/rpcs3/Input/hid_pad_handler.cpp @@ -20,6 +20,15 @@ LOG_CHANNEL(hid_log, "HID"); static std::mutex s_hid_mutex; // hid_pad_handler is created by pad_thread and pad_settings_dialog static u8 s_hid_instances{0}; +void HidDevice::close() +{ + if (hidDevice) + { + hid_close(hidDevice); + hidDevice = nullptr; + } +} + template hid_pad_handler::hid_pad_handler(pad_handler type, std::vector ids) : PadHandlerBase(type), m_ids(std::move(ids)) @@ -40,9 +49,9 @@ hid_pad_handler::~hid_pad_handler() for (auto& controller : m_controllers) { - if (controller.second && controller.second->hidDevice) + if (controller.second) { - hid_close(controller.second->hidDevice); + controller.second->close(); } } @@ -170,7 +179,7 @@ void hid_pad_handler::update_devices() { if (controller.second && !controller.second->path.empty() && !m_new_enumerated_devices.contains(controller.second->path)) { - hid_close(controller.second->hidDevice); + controller.second->close(); cfg_pad* config = controller.second->config; controller.second.reset(new Device()); controller.second->config = config; diff --git a/rpcs3/Input/hid_pad_handler.h b/rpcs3/Input/hid_pad_handler.h index d9a4db0682..7dcdae31e2 100644 --- a/rpcs3/Input/hid_pad_handler.h +++ b/rpcs3/Input/hid_pad_handler.h @@ -30,6 +30,8 @@ enum CalibIndex class HidDevice : public PadDevice { public: + void close(); + hid_device* hidDevice{nullptr}; std::string path; bool new_output_data{true}; diff --git a/rpcs3/Input/skateboard_pad_handler.cpp b/rpcs3/Input/skateboard_pad_handler.cpp index 93b4ae423f..64414c74eb 100644 --- a/rpcs3/Input/skateboard_pad_handler.cpp +++ b/rpcs3/Input/skateboard_pad_handler.cpp @@ -204,7 +204,7 @@ skateboard_pad_handler::DataStatus skateboard_pad_handler::get_data(skateboard_d if (res == -1) { // looks like controller disconnected or read error - skateboard_log.error("get_data ReadError", device->path); + skateboard_log.error("get_data ReadError: %s", hid_error(device->hidDevice)); return DataStatus::ReadError; } @@ -254,8 +254,7 @@ PadHandlerBase::connection skateboard_pad_handler::update_connection(const std:: if (get_data(skateboard_dev) == DataStatus::ReadError) { // this also can mean disconnected, either way deal with it on next loop and reconnect - hid_close(skateboard_dev->hidDevice); - skateboard_dev->hidDevice = nullptr; + skateboard_dev->close(); return connection::no_data; }