input/macOs: Call hid_close on the main thread
Some checks are pending
Generate Translation Template / Generate Translation Template (push) Waiting to run
Build RPCS3 / RPCS3 Linux ${{ matrix.os }} ${{ matrix.compiler }} (/rpcs3/.ci/build-linux.sh, gcc, rpcs3/rpcs3-ci-jammy:1.6, ubuntu-24.04) (push) Waiting to run
Build RPCS3 / RPCS3 Linux ${{ matrix.os }} ${{ matrix.compiler }} (a1d35836e8d45bfc6f63c26f0a3e5d46ef622fe1, rpcs3/rpcs3-binaries-linux-arm64, /rpcs3/.ci/build-linux-aarch64.sh, clang, rpcs3/rpcs3-ci-jammy-aarch64:1.6, ubuntu-24.04-arm) (push) Waiting to run
Build RPCS3 / RPCS3 Linux ${{ matrix.os }} ${{ matrix.compiler }} (d812f1254a1157c80fd402f94446310560f54e5f, rpcs3/rpcs3-binaries-linux, /rpcs3/.ci/build-linux.sh, clang, rpcs3/rpcs3-ci-jammy:1.6, ubuntu-24.04) (push) Waiting to run
Build RPCS3 / RPCS3 Mac ${{ matrix.name }} (51ae32f468089a8169aaf1567de355ff4a3e0842, rpcs3/rpcs3-binaries-mac, arch -X86_64 .ci/build-mac.sh, Intel) (push) Waiting to run
Build RPCS3 / RPCS3 Mac ${{ matrix.name }} (8e21bdbc40711a3fccd18fbf17b742348b0f4281, rpcs3/rpcs3-binaries-mac-arm64, .ci/build-mac-arm64.sh, Apple Silicon) (push) Waiting to run
Build RPCS3 / RPCS3 Windows (push) Waiting to run

This commit is contained in:
Megamouse 2025-06-07 11:12:46 +02:00
parent ae38cef7b1
commit 866daf2059
7 changed files with 46 additions and 20 deletions

View File

@ -259,7 +259,7 @@ void ds3_pad_handler::check_add_device(hid_device* hidDevice, hid_enumerated_dev
if (res <= 0 || buf[0] != 0x0) if (res <= 0 || buf[0] != 0x0)
{ {
ds3_log.error("check_add_device: hid_get_feature_report 0x0 failed! result=%d, buf[0]=0x%x, error=%s", res, buf[0], hid_error(hidDevice)); ds3_log.error("check_add_device: hid_get_feature_report 0x0 failed! result=%d, buf[0]=0x%x, error=%s", res, buf[0], hid_error(hidDevice));
hid_close(hidDevice); HidDevice::close(hidDevice);
return; return;
} }
} }
@ -270,7 +270,7 @@ void ds3_pad_handler::check_add_device(hid_device* hidDevice, hid_enumerated_dev
if (res < 0) if (res < 0)
{ {
ds3_log.error("check_add_device: hid_init_sixaxis_usb failed! (result=%d, error=%s)", res, hid_error(hidDevice)); ds3_log.error("check_add_device: hid_init_sixaxis_usb failed! (result=%d, error=%s)", res, hid_error(hidDevice));
hid_close(hidDevice); HidDevice::close(hidDevice);
return; return;
} }
#endif #endif
@ -281,7 +281,7 @@ void ds3_pad_handler::check_add_device(hid_device* hidDevice, hid_enumerated_dev
if (hid_set_nonblocking(hidDevice, 1) == -1) if (hid_set_nonblocking(hidDevice, 1) == -1)
{ {
ds3_log.error("check_add_device: hid_set_nonblocking failed! Reason: %s", hid_error(hidDevice)); ds3_log.error("check_add_device: hid_set_nonblocking failed! Reason: %s", hid_error(hidDevice));
hid_close(hidDevice); HidDevice::close(hidDevice);
return; return;
} }

View File

@ -589,7 +589,7 @@ void ds4_pad_handler::check_add_device(hid_device* hidDevice, hid_enumerated_dev
if (!devinfo) if (!devinfo)
{ {
ds4_log.error("check_add_device: hid_get_device_info failed! error=%s", hid_error(hidDevice)); ds4_log.error("check_add_device: hid_get_device_info failed! error=%s", hid_error(hidDevice));
hid_close(hidDevice); HidDevice::close(hidDevice);
return; return;
} }

View File

@ -134,7 +134,7 @@ void dualsense_pad_handler::check_add_device(hid_device* hidDevice, hid_enumerat
if (res < 0 || buf[0] != 0x09) if (res < 0 || buf[0] != 0x09)
{ {
dualsense_log.error("check_add_device: hid_get_feature_report 0x09 failed! result=%d, buf[0]=0x%x, error=%s", res, buf[0], hid_error(hidDevice)); dualsense_log.error("check_add_device: hid_get_feature_report 0x09 failed! result=%d, buf[0]=0x%x, error=%s", res, buf[0], hid_error(hidDevice));
hid_close(hidDevice); HidDevice::close(hidDevice);
return; return;
} }

View File

@ -21,6 +21,8 @@ LOG_CHANNEL(hid_log, "HID");
#ifdef ANDROID #ifdef ANDROID
std::vector<android_usb_device> g_android_usb_devices; std::vector<android_usb_device> g_android_usb_devices;
std::mutex g_android_usb_devices_mutex; std::mutex g_android_usb_devices_mutex;
#elif defined(__APPLE__)
std::mutex g_hid_mutex;
#endif #endif
struct hid_instance struct hid_instance
@ -91,7 +93,7 @@ hid_device* HidDevice::open()
#ifdef ANDROID #ifdef ANDROID
hidDevice = hid_libusb_wrap_sys_device(path, -1); hidDevice = hid_libusb_wrap_sys_device(path, -1);
#elif defined(__APPLE__) #elif defined(__APPLE__)
std::unique_lock static_lock(s_hid_mutex, std::defer_lock); std::unique_lock static_lock(g_hid_mutex, std::defer_lock);
if (!static_lock.try_lock()) if (!static_lock.try_lock())
{ {
// The enumeration thread is busy. If we lock and open the device, we might get input stutter on other devices. // The enumeration thread is busy. If we lock and open the device, we might get input stutter on other devices.
@ -108,13 +110,45 @@ hid_device* HidDevice::open()
return hidDevice; return hidDevice;
} }
void HidDevice::close(hid_device* dev)
{
if (!dev) return;
#if defined(__APPLE__)
Emu.BlockingCallFromMainThread([dev]()
{
if (dev)
{
hid_close(dev);
}
}, false);
#else
hid_close(dev);
#endif
}
void HidDevice::close() void HidDevice::close()
{ {
#if defined(__APPLE__)
if (hidDevice)
{
Emu.BlockingCallFromMainThread([this]()
{
if (hidDevice)
{
hid_close(hidDevice);
hidDevice = nullptr;
}
}, false);
}
#else
if (hidDevice) if (hidDevice)
{ {
hid_close(hidDevice); hid_close(hidDevice);
hidDevice = nullptr; hidDevice = nullptr;
} }
#endif
#ifdef _WIN32 #ifdef _WIN32
if (bt_device) if (bt_device)
{ {
@ -124,11 +158,6 @@ void HidDevice::close()
#endif #endif
} }
#if defined(__APPLE__)
template <class Device>
std::mutex hid_pad_handler<Device>::s_hid_mutex;
#endif
template <class Device> template <class Device>
hid_pad_handler<Device>::hid_pad_handler(pad_handler type, std::vector<id_pair> ids) hid_pad_handler<Device>::hid_pad_handler(pad_handler type, std::vector<id_pair> ids)
: PadHandlerBase(type), m_ids(std::move(ids)) : PadHandlerBase(type), m_ids(std::move(ids))
@ -142,7 +171,7 @@ hid_pad_handler<Device>::~hid_pad_handler()
m_enumeration_thread.reset(); m_enumeration_thread.reset();
#if defined(__APPLE__) #if defined(__APPLE__)
std::lock_guard static_lock(s_hid_mutex); std::lock_guard static_lock(g_hid_mutex);
#endif #endif
for (auto& controller : m_controllers) for (auto& controller : m_controllers)
@ -241,7 +270,7 @@ void hid_pad_handler<Device>::enumerate_devices()
{ {
#if defined(__APPLE__) #if defined(__APPLE__)
// Let's make sure hid_enumerate is only done one thread at a time // Let's make sure hid_enumerate is only done one thread at a time
std::lock_guard static_lock(s_hid_mutex); std::lock_guard static_lock(g_hid_mutex);
Emu.BlockingCallFromMainThread([&]() Emu.BlockingCallFromMainThread([&]()
{ {
#endif #endif
@ -317,7 +346,7 @@ void hid_pad_handler<Device>::update_devices()
} }
#if defined(__APPLE__) #if defined(__APPLE__)
std::lock_guard static_lock(s_hid_mutex); std::lock_guard static_lock(g_hid_mutex);
#endif #endif
// Scrap devices that are not in the new list // Scrap devices that are not in the new list

View File

@ -57,6 +57,7 @@ class HidDevice : public PadDevice
{ {
public: public:
hid_device* open(); hid_device* open();
static void close(hid_device* dev);
void close(); void close();
hid_device* hidDevice{nullptr}; hid_device* hidDevice{nullptr};
@ -110,10 +111,6 @@ protected:
std::mutex m_enumeration_mutex; std::mutex m_enumeration_mutex;
std::unique_ptr<named_thread<std::function<void()>>> m_enumeration_thread; std::unique_ptr<named_thread<std::function<void()>>> m_enumeration_thread;
#if defined(__APPLE__)
static std::mutex s_hid_mutex;
#endif
void enumerate_devices(); void enumerate_devices();
void update_devices(); void update_devices();
std::shared_ptr<Device> get_hid_device(const std::string& padId); std::shared_ptr<Device> get_hid_device(const std::string& padId);

View File

@ -303,7 +303,7 @@ void ps_move_handler::check_add_device(hid_device* hidDevice, hid_enumerated_dev
if (hid_set_nonblocking(hidDevice, 1) == -1) if (hid_set_nonblocking(hidDevice, 1) == -1)
{ {
move_log.error("check_add_device: hid_set_nonblocking failed! Reason: %s", hid_error(hidDevice)); move_log.error("check_add_device: hid_set_nonblocking failed! Reason: %s", hid_error(hidDevice));
hid_close(hidDevice); HidDevice::close(hidDevice);
return; return;
} }
#endif #endif

View File

@ -177,7 +177,7 @@ void skateboard_pad_handler::check_add_device(hid_device* hidDevice, hid_enumera
if (hid_set_nonblocking(hidDevice, 1) == -1) if (hid_set_nonblocking(hidDevice, 1) == -1)
{ {
skateboard_log.error("check_add_device: hid_set_nonblocking failed! Reason: %s", hid_error(hidDevice)); skateboard_log.error("check_add_device: hid_set_nonblocking failed! Reason: %s", hid_error(hidDevice));
hid_close(hidDevice); HidDevice::close(hidDevice);
return; return;
} }