Skip to content

Commit e261d63

Browse files
committed
Only use usb_mutex if we're reading via control transfer
1 parent fdceca9 commit e261d63

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Xb2XInput/XboxController.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,11 @@ void CALLBACK XboxController::OnVigemNotification(PVIGEM_CLIENT Client, PVIGEM_T
278278
controller.output_prev_.Rumble.wLeftMotorSpeed = _byteswap_ushort(LargeMotor); // why do these need to be byteswapped???
279279
controller.output_prev_.Rumble.wRightMotorSpeed = _byteswap_ushort(SmallMotor);
280280

281-
libusb_control_transfer(controller.usb_handle_, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE,
282-
HID_SET_REPORT, (HID_REPORT_TYPE_OUTPUT << 8) | 0x00, 0, (unsigned char*)&controller.output_prev_, sizeof(XboxOutputReport), 1000);
281+
{
282+
std::lock_guard<std::mutex> guard(usb_mutex_);
283+
libusb_control_transfer(controller.usb_handle_, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE,
284+
HID_SET_REPORT, (HID_REPORT_TYPE_OUTPUT << 8) | 0x00, 0, (unsigned char*)&controller.output_prev_, sizeof(XboxOutputReport), 1000);
285+
}
283286

284287
break;
285288
}
@@ -325,15 +328,18 @@ bool XboxController::update()
325328

326329
// if we have interrupt endpoints use those for better compatibility, otherwise fallback to control transfers
327330
memset(&input_prev_, 0, sizeof(XboxInputReport));
328-
std::lock_guard<std::mutex> guard(usb_mutex_);
329331
int length = 0;
330332
int ret = -1;
333+
331334
if (endpoint_in_)
332335
ret = libusb_interrupt_transfer(usb_handle_, endpoint_in_, (unsigned char*)&input_prev_, sizeof(XboxInputReport), &length, 0);
333336

334337
if (ret < 0)
338+
{
339+
std::lock_guard<std::mutex> guard(usb_mutex_);
335340
ret = libusb_control_transfer(usb_handle_, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE,
336341
HID_GET_REPORT, (HID_REPORT_TYPE_INPUT << 8) | 0x00, 0, (unsigned char*)&input_prev_, sizeof(XboxInputReport), 1000);
342+
}
337343

338344
if (ret < 0)
339345
{

0 commit comments

Comments
 (0)