Skip to content

Commit ae38cef

Browse files
committed
input/macOs: Call hid_open_path on the main thread
1 parent e36f55e commit ae38cef

File tree

7 files changed

+37
-35
lines changed

7 files changed

+37
-35
lines changed

rpcs3/Input/ds3_pad_handler.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,17 +513,12 @@ PadHandlerBase::connection ds3_pad_handler::update_connection(const std::shared_
513513

514514
if (dev->hidDevice == nullptr)
515515
{
516-
#ifdef ANDROID
517-
if (hid_device* hid_dev = hid_libusb_wrap_sys_device(dev->path, -1))
518-
#else
519-
if (hid_device* hid_dev = hid_open_path(dev->path.c_str()))
520-
#endif
516+
if (hid_device* hid_dev = dev->open())
521517
{
522518
if (hid_set_nonblocking(hid_dev, 1) == -1)
523519
{
524520
ds3_log.error("Reconnecting Device %s: hid_set_nonblocking failed with error %s", dev->path, hid_error(hid_dev));
525521
}
526-
dev->hidDevice = hid_dev;
527522
}
528523
else
529524
{

rpcs3/Input/ds4_pad_handler.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -837,19 +837,13 @@ PadHandlerBase::connection ds4_pad_handler::update_connection(const std::shared_
837837
if (dev->hidDevice == nullptr)
838838
{
839839
// try to reconnect
840-
#ifdef ANDROID
841-
if (hid_device* hid_dev = hid_libusb_wrap_sys_device(dev->path, -1))
842-
#else
843-
if (hid_device* hid_dev = hid_open_path(dev->path.c_str()))
844-
#endif
840+
if (hid_device* hid_dev = dev->open())
845841
{
846842
if (hid_set_nonblocking(hid_dev, 1) == -1)
847843
{
848844
ds4_log.error("Reconnecting Device %s: hid_set_nonblocking failed with error %s", dev->path, hid_error(hid_dev));
849845
}
850846

851-
dev->hidDevice = hid_dev;
852-
853847
if (!dev->has_calib_data)
854848
{
855849
dev->has_calib_data = GetCalibrationData(dev);

rpcs3/Input/dualsense_pad_handler.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -568,19 +568,13 @@ PadHandlerBase::connection dualsense_pad_handler::update_connection(const std::s
568568
if (dev->hidDevice == nullptr)
569569
{
570570
// try to reconnect
571-
#ifdef ANDROID
572-
if (hid_device* hid_dev = hid_libusb_wrap_sys_device(dev->path, -1))
573-
#else
574-
if (hid_device* hid_dev = hid_open_path(dev->path.c_str()))
575-
#endif
571+
if (hid_device* hid_dev = dev->open())
576572
{
577573
if (hid_set_nonblocking(hid_dev, 1) == -1)
578574
{
579575
dualsense_log.error("Reconnecting Device %s: hid_set_nonblocking failed with error %s", dev->path, hid_error(hid_dev));
580576
}
581577

582-
dev->hidDevice = hid_dev;
583-
584578
if (!dev->has_calib_data)
585579
{
586580
dev->has_calib_data = get_calibration_data(dev);

rpcs3/Input/hid_pad_handler.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
89111
void 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

rpcs3/Input/hid_pad_handler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ enum CalibIndex
5656
class HidDevice : public PadDevice
5757
{
5858
public:
59+
hid_device* open();
5960
void close();
6061

6162
hid_device* hidDevice{nullptr};

rpcs3/Input/ps_move_handler.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -434,17 +434,12 @@ PadHandlerBase::connection ps_move_handler::update_connection(const std::shared_
434434
move_device->hidDevice = dev;
435435
}
436436
#else
437-
#ifdef ANDROID
438-
if (hid_device* dev = hid_libusb_wrap_sys_device(move_device->path, -1))
439-
#else
440-
if (hid_device* dev = hid_open_path(move_device->path.c_str()))
441-
#endif
437+
if (hid_device* dev = move_device->open())
442438
{
443439
if (hid_set_nonblocking(dev, 1) == -1)
444440
{
445441
move_log.error("Reconnecting Device %s: hid_set_nonblocking failed with error %s", move_device->path, hid_error(dev));
446442
}
447-
move_device->hidDevice = dev;
448443
}
449444
#endif
450445
else

rpcs3/Input/skateboard_pad_handler.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,22 +239,16 @@ PadHandlerBase::connection skateboard_pad_handler::update_connection(const std::
239239
if (dev->hidDevice == nullptr)
240240
{
241241
// try to reconnect
242-
#ifdef ANDROID
243-
if (hid_device* hid_dev = hid_libusb_wrap_sys_device(dev->path, -1))
244-
#else
245-
if (hid_device* hid_dev = hid_open_path(dev->path.c_str()))
246-
#endif
242+
if (hid_device* hid_dev = dev->open())
247243
{
248244
if (hid_set_nonblocking(hid_dev, 1) == -1)
249245
{
250246
skateboard_log.error("Reconnecting Device %s: hid_set_nonblocking failed with error %s", dev->path, hid_error(hid_dev));
251247
}
252-
dev->hidDevice = hid_dev;
253248
}
254249
else
255250
{
256251
// nope, not there
257-
skateboard_log.error("Device %s: disconnected", dev->path);
258252
return connection::disconnected;
259253
}
260254
}

0 commit comments

Comments
 (0)