@@ -21,6 +21,8 @@ LOG_CHANNEL(hid_log, "HID");
2121#ifdef ANDROID
2222std::vector<android_usb_device> g_android_usb_devices;
2323std::mutex g_android_usb_devices_mutex;
24+ #elif defined(__APPLE__)
25+ std::mutex g_hid_mutex;
2426#endif
2527
2628struct hid_instance
@@ -91,7 +93,7 @@ hid_device* HidDevice::open()
9193#ifdef ANDROID
9294 hidDevice = hid_libusb_wrap_sys_device (path, -1 );
9395#elif defined(__APPLE__)
94- std::unique_lock static_lock (s_hid_mutex , std::defer_lock);
96+ std::unique_lock static_lock (g_hid_mutex , std::defer_lock);
9597 if (!static_lock.try_lock ())
9698 {
9799 // 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()
108110 return hidDevice;
109111}
110112
113+ void HidDevice::close (hid_device* dev)
114+ {
115+ if (!dev) return ;
116+
117+ #if defined(__APPLE__)
118+ Emu.BlockingCallFromMainThread ([dev]()
119+ {
120+ if (dev)
121+ {
122+ hid_close (dev);
123+ }
124+ }, false );
125+ #else
126+ hid_close (dev);
127+ #endif
128+ }
129+
111130void HidDevice::close ()
112131{
132+ #if defined(__APPLE__)
133+ if (hidDevice)
134+ {
135+ Emu.BlockingCallFromMainThread ([this ]()
136+ {
137+ if (hidDevice)
138+ {
139+ hid_close (hidDevice);
140+ hidDevice = nullptr ;
141+ }
142+ }, false );
143+ }
144+ #else
113145 if (hidDevice)
114146 {
115147 hid_close (hidDevice);
116148 hidDevice = nullptr ;
117149 }
150+ #endif
151+
118152#ifdef _WIN32
119153 if (bt_device)
120154 {
@@ -124,11 +158,6 @@ void HidDevice::close()
124158#endif
125159}
126160
127- #if defined(__APPLE__)
128- template <class Device >
129- std::mutex hid_pad_handler<Device>::s_hid_mutex;
130- #endif
131-
132161template <class Device >
133162hid_pad_handler<Device>::hid_pad_handler(pad_handler type, std::vector<id_pair> ids)
134163 : PadHandlerBase(type), m_ids(std::move(ids))
@@ -142,7 +171,7 @@ hid_pad_handler<Device>::~hid_pad_handler()
142171 m_enumeration_thread.reset ();
143172
144173#if defined(__APPLE__)
145- std::lock_guard static_lock (s_hid_mutex );
174+ std::lock_guard static_lock (g_hid_mutex );
146175#endif
147176
148177 for (auto & controller : m_controllers)
@@ -241,7 +270,7 @@ void hid_pad_handler<Device>::enumerate_devices()
241270 {
242271#if defined(__APPLE__)
243272 // Let's make sure hid_enumerate is only done one thread at a time
244- std::lock_guard static_lock (s_hid_mutex );
273+ std::lock_guard static_lock (g_hid_mutex );
245274 Emu.BlockingCallFromMainThread ([&]()
246275 {
247276#endif
@@ -317,7 +346,7 @@ void hid_pad_handler<Device>::update_devices()
317346 }
318347
319348#if defined(__APPLE__)
320- std::lock_guard static_lock (s_hid_mutex );
349+ std::lock_guard static_lock (g_hid_mutex );
321350#endif
322351
323352 // Scrap devices that are not in the new list
0 commit comments