Fixed initializing the Nyko and EVORETRO GameCube adaptors

This requires root on most Linux distributions, as we have to directly send USB messages to the devices to enable input reports.
This commit is contained in:
Sam Lantinga
2021-01-21 22:30:34 -08:00
parent b6ae9a7cba
commit 3527b49459
3 changed files with 82 additions and 0 deletions

View File

@@ -955,6 +955,57 @@ HID_API_EXPORT const wchar_t* HID_API_CALL hid_error(hid_device *device)
return wrapper->backend->hid_error(wrapper->device);
}
/* This is needed to enable input for Nyko and EVORETRO GameCube adaptors */
void SDL_EnableGameCubeAdaptors(void)
{
#ifdef SDL_LIBUSB_DYNAMIC
libusb_context *usb_context = NULL;
libusb_device **devs = NULL;
libusb_device_handle *handle = NULL;
struct libusb_device_descriptor desc;
ssize_t i, num_devs;
int kernel_detached = 0;
if (libusb_init(&usb_context) == 0) {
num_devs = libusb_get_device_list(usb_context, &devs);
for (i = 0; i < num_devs; ++i) {
if (libusb_get_device_descriptor(devs[i], &desc) != 0) {
continue;
}
if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) {
continue;
}
if (libusb_open(devs[i], &handle) != 0) {
continue;
}
if (libusb_kernel_driver_active(handle, 0)) {
if (libusb_detach_kernel_driver(handle, 0) == 0) {
kernel_detached = 1;
}
}
if (libusb_claim_interface(handle, 0) == 0) {
libusb_control_transfer(handle, 0x21, 11, 0x0001, 0, NULL, 0, 1000);
libusb_release_interface(handle, 0);
}
if (kernel_detached) {
libusb_attach_kernel_driver(handle, 0);
}
libusb_close(handle);
}
libusb_free_device_list(devs, 1);
libusb_exit(usb_context);
}
#endif /* SDL_LIBUSB_DYNAMIC */
}
#endif /* SDL_JOYSTICK_HIDAPI */
/* vi: set sts=4 ts=4 sw=4 expandtab: */