Skip to content

Commit 866daf2

Browse files
committed
input/macOs: Call hid_close on the main thread
1 parent ae38cef commit 866daf2

File tree

7 files changed

+46
-20
lines changed

7 files changed

+46
-20
lines changed

rpcs3/Input/ds3_pad_handler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ void ds3_pad_handler::check_add_device(hid_device* hidDevice, hid_enumerated_dev
259259
if (res <= 0 || buf[0] != 0x0)
260260
{
261261
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));
262-
hid_close(hidDevice);
262+
HidDevice::close(hidDevice);
263263
return;
264264
}
265265
}
@@ -270,7 +270,7 @@ void ds3_pad_handler::check_add_device(hid_device* hidDevice, hid_enumerated_dev
270270
if (res < 0)
271271
{
272272
ds3_log.error("check_add_device: hid_init_sixaxis_usb failed! (result=%d, error=%s)", res, hid_error(hidDevice));
273-
hid_close(hidDevice);
273+
HidDevice::close(hidDevice);
274274
return;
275275
}
276276
#endif
@@ -281,7 +281,7 @@ void ds3_pad_handler::check_add_device(hid_device* hidDevice, hid_enumerated_dev
281281
if (hid_set_nonblocking(hidDevice, 1) == -1)
282282
{
283283
ds3_log.error("check_add_device: hid_set_nonblocking failed! Reason: %s", hid_error(hidDevice));
284-
hid_close(hidDevice);
284+
HidDevice::close(hidDevice);
285285
return;
286286
}
287287

rpcs3/Input/ds4_pad_handler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ void ds4_pad_handler::check_add_device(hid_device* hidDevice, hid_enumerated_dev
589589
if (!devinfo)
590590
{
591591
ds4_log.error("check_add_device: hid_get_device_info failed! error=%s", hid_error(hidDevice));
592-
hid_close(hidDevice);
592+
HidDevice::close(hidDevice);
593593
return;
594594
}
595595

rpcs3/Input/dualsense_pad_handler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void dualsense_pad_handler::check_add_device(hid_device* hidDevice, hid_enumerat
134134
if (res < 0 || buf[0] != 0x09)
135135
{
136136
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));
137-
hid_close(hidDevice);
137+
HidDevice::close(hidDevice);
138138
return;
139139
}
140140

rpcs3/Input/hid_pad_handler.cpp

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ LOG_CHANNEL(hid_log, "HID");
2121
#ifdef ANDROID
2222
std::vector<android_usb_device> g_android_usb_devices;
2323
std::mutex g_android_usb_devices_mutex;
24+
#elif defined(__APPLE__)
25+
std::mutex g_hid_mutex;
2426
#endif
2527

2628
struct 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+
111130
void 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-
132161
template <class Device>
133162
hid_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

rpcs3/Input/hid_pad_handler.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class HidDevice : public PadDevice
5757
{
5858
public:
5959
hid_device* open();
60+
static void close(hid_device* dev);
6061
void close();
6162

6263
hid_device* hidDevice{nullptr};
@@ -110,10 +111,6 @@ class hid_pad_handler : public PadHandlerBase
110111
std::mutex m_enumeration_mutex;
111112
std::unique_ptr<named_thread<std::function<void()>>> m_enumeration_thread;
112113

113-
#if defined(__APPLE__)
114-
static std::mutex s_hid_mutex;
115-
#endif
116-
117114
void enumerate_devices();
118115
void update_devices();
119116
std::shared_ptr<Device> get_hid_device(const std::string& padId);

rpcs3/Input/ps_move_handler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ void ps_move_handler::check_add_device(hid_device* hidDevice, hid_enumerated_dev
303303
if (hid_set_nonblocking(hidDevice, 1) == -1)
304304
{
305305
move_log.error("check_add_device: hid_set_nonblocking failed! Reason: %s", hid_error(hidDevice));
306-
hid_close(hidDevice);
306+
HidDevice::close(hidDevice);
307307
return;
308308
}
309309
#endif

rpcs3/Input/skateboard_pad_handler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ void skateboard_pad_handler::check_add_device(hid_device* hidDevice, hid_enumera
177177
if (hid_set_nonblocking(hidDevice, 1) == -1)
178178
{
179179
skateboard_log.error("check_add_device: hid_set_nonblocking failed! Reason: %s", hid_error(hidDevice));
180-
hid_close(hidDevice);
180+
HidDevice::close(hidDevice);
181181
return;
182182
}
183183

0 commit comments

Comments
 (0)