@@ -67,7 +67,7 @@ struct hid_instance
6767 {
6868 error_code = hid_init ();
6969 hid_darwin_set_open_exclusive (0 );
70- });
70+ }, false );
7171#else
7272 const int error_code = hid_init ();
7373#endif
@@ -86,6 +86,28 @@ struct hid_instance
8686 std::mutex m_hid_mutex;
8787};
8888
89+ hid_device* HidDevice::open ()
90+ {
91+ #ifdef ANDROID
92+ hidDevice = hid_libusb_wrap_sys_device (path, -1 );
93+ #elif defined(__APPLE__)
94+ std::unique_lock static_lock (s_hid_mutex, std::defer_lock);
95+ if (!static_lock.try_lock ())
96+ {
97+ // The enumeration thread is busy. If we lock and open the device, we might get input stutter on other devices.
98+ return nullptr ;
99+ }
100+ Emu.BlockingCallFromMainThread ([this ]()
101+ {
102+ hidDevice = hid_open_path (path.c_str ());
103+ }, false );
104+ #else
105+ hidDevice = hid_open_path (path.data ());
106+ #endif
107+
108+ return hidDevice;
109+ }
110+
89111void HidDevice::close ()
90112{
91113 if (hidDevice)
@@ -245,7 +267,7 @@ void hid_pad_handler<Device>::enumerate_devices()
245267 }
246268 hid_free_enumeration (head);
247269#if defined(__APPLE__)
248- });
270+ }, false );
249271#endif
250272 }
251273#endif
@@ -333,6 +355,13 @@ void hid_pad_handler<Device>::update_devices()
333355
334356#ifdef ANDROID
335357 if (hid_device* dev = hid_libusb_wrap_sys_device (path, -1 ))
358+ #elif defined(__APPLE__)
359+ hid_device* dev = nullptr ;
360+ Emu.BlockingCallFromMainThread ([&]()
361+ {
362+ dev = hid_open_path (path.c_str ());
363+ }, false );
364+ if (dev)
336365#else
337366 if (hid_device* dev = hid_open_path (path.c_str ()))
338367#endif
0 commit comments