Skip to content

Commit 8263c3a

Browse files
committed
Move libusb support into a shared location
1 parent 45f9326 commit 8263c3a

File tree

7 files changed

+269
-184
lines changed

7 files changed

+269
-184
lines changed

VisualC-GDK/SDL/SDL.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@
765765
<ClCompile Include="..\..\src\loadso\windows\SDL_sysloadso.c" />
766766
<ClCompile Include="..\..\src\locale\SDL_locale.c" />
767767
<ClCompile Include="..\..\src\locale\windows\SDL_syslocale.c" />
768+
<ClCompile Include="..\..\src\misc\SDL_libusb.c" />
768769
<ClCompile Include="..\..\src\misc\SDL_url.c" />
769770
<ClCompile Include="..\..\src\misc\windows\SDL_sysurl.c" />
770771
<ClCompile Include="..\..\src\power\SDL_power.c" />

VisualC-GDK/SDL/SDL.vcxproj.filters

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<ClCompile Include="..\..\src\loadso\windows\SDL_sysloadso.c" />
9999
<ClCompile Include="..\..\src\locale\SDL_locale.c" />
100100
<ClCompile Include="..\..\src\locale\windows\SDL_syslocale.c" />
101+
<ClCompile Include="..\..\src\misc\SDL_libusb.c" />
101102
<ClCompile Include="..\..\src\misc\SDL_url.c" />
102103
<ClCompile Include="..\..\src\misc\windows\SDL_sysurl.c" />
103104
<ClCompile Include="..\..\src\power\SDL_power.c" />

VisualC/SDL/SDL.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@
637637
<ClCompile Include="..\..\src\loadso\windows\SDL_sysloadso.c" />
638638
<ClCompile Include="..\..\src\locale\SDL_locale.c" />
639639
<ClCompile Include="..\..\src\locale\windows\SDL_syslocale.c" />
640+
<ClCompile Include="..\..\src\misc\SDL_libusb.c" />
640641
<ClCompile Include="..\..\src\misc\SDL_url.c" />
641642
<ClCompile Include="..\..\src\misc\windows\SDL_sysurl.c" />
642643
<ClCompile Include="..\..\src\power\SDL_power.c" />

VisualC/SDL/SDL.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,9 @@
11371137
<ClCompile Include="..\..\src\loadso\windows\SDL_sysloadso.c">
11381138
<Filter>loadso\windows</Filter>
11391139
</ClCompile>
1140+
<ClCompile Include="..\..\src\misc\SDL_libusb.c">
1141+
<Filter>misc</Filter>
1142+
</ClCompile>
11401143
<ClCompile Include="..\..\src\misc\SDL_url.c">
11411144
<Filter>misc</Filter>
11421145
</ClCompile>

src/hidapi/SDL_hidapi.c

Lines changed: 61 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -689,105 +689,40 @@ typedef struct DRIVER_hid_device_ DRIVER_hid_device;
689689
#ifdef HAVE_LIBUSB
690690
// libusb HIDAPI Implementation
691691

692-
// Include this now, for our dynamically-loaded libusb context
693-
#include <libusb.h>
694-
695-
static struct
696-
{
697-
SDL_SharedObject *libhandle;
698-
699-
/* *INDENT-OFF* */ // clang-format off
700-
int (LIBUSB_CALL *init)(libusb_context **ctx);
701-
void (LIBUSB_CALL *exit)(libusb_context *ctx);
702-
ssize_t (LIBUSB_CALL *get_device_list)(libusb_context *ctx, libusb_device ***list);
703-
void (LIBUSB_CALL *free_device_list)(libusb_device **list, int unref_devices);
704-
int (LIBUSB_CALL *get_device_descriptor)(libusb_device *dev, struct libusb_device_descriptor *desc);
705-
int (LIBUSB_CALL *get_active_config_descriptor)(libusb_device *dev, struct libusb_config_descriptor **config);
706-
int (LIBUSB_CALL *get_config_descriptor)(
707-
libusb_device *dev,
708-
uint8_t config_index,
709-
struct libusb_config_descriptor **config
710-
);
711-
void (LIBUSB_CALL *free_config_descriptor)(struct libusb_config_descriptor *config);
712-
uint8_t (LIBUSB_CALL *get_bus_number)(libusb_device *dev);
713-
int (LIBUSB_CALL *get_port_numbers)(libusb_device *dev, uint8_t *port_numbers, int port_numbers_len);
714-
uint8_t (LIBUSB_CALL *get_device_address)(libusb_device *dev);
715-
int (LIBUSB_CALL *open)(libusb_device *dev, libusb_device_handle **dev_handle);
716-
void (LIBUSB_CALL *close)(libusb_device_handle *dev_handle);
717-
libusb_device *(LIBUSB_CALL *get_device)(libusb_device_handle *dev_handle);
718-
int (LIBUSB_CALL *claim_interface)(libusb_device_handle *dev_handle, int interface_number);
719-
int (LIBUSB_CALL *release_interface)(libusb_device_handle *dev_handle, int interface_number);
720-
int (LIBUSB_CALL *kernel_driver_active)(libusb_device_handle *dev_handle, int interface_number);
721-
int (LIBUSB_CALL *detach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
722-
int (LIBUSB_CALL *attach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
723-
int (LIBUSB_CALL *set_interface_alt_setting)(libusb_device_handle *dev, int interface_number, int alternate_setting);
724-
struct libusb_transfer * (LIBUSB_CALL *alloc_transfer)(int iso_packets);
725-
int (LIBUSB_CALL *submit_transfer)(struct libusb_transfer *transfer);
726-
int (LIBUSB_CALL *cancel_transfer)(struct libusb_transfer *transfer);
727-
void (LIBUSB_CALL *free_transfer)(struct libusb_transfer *transfer);
728-
int (LIBUSB_CALL *control_transfer)(
729-
libusb_device_handle *dev_handle,
730-
uint8_t request_type,
731-
uint8_t bRequest,
732-
uint16_t wValue,
733-
uint16_t wIndex,
734-
unsigned char *data,
735-
uint16_t wLength,
736-
unsigned int timeout
737-
);
738-
int (LIBUSB_CALL *interrupt_transfer)(
739-
libusb_device_handle *dev_handle,
740-
unsigned char endpoint,
741-
unsigned char *data,
742-
int length,
743-
int *actual_length,
744-
unsigned int timeout
745-
);
746-
int (LIBUSB_CALL *bulk_transfer)(
747-
libusb_device_handle *dev_handle,
748-
unsigned char endpoint,
749-
unsigned char *data,
750-
int length,
751-
int *transferred,
752-
unsigned int timeout
753-
);
754-
int (LIBUSB_CALL *handle_events)(libusb_context *ctx);
755-
int (LIBUSB_CALL *handle_events_completed)(libusb_context *ctx, int *completed);
756-
const char * (LIBUSB_CALL *error_name)(int errcode);
757-
/* *INDENT-ON* */ // clang-format on
758-
759-
} libusb_ctx;
760-
761-
#define libusb_init libusb_ctx.init
762-
#define libusb_exit libusb_ctx.exit
763-
#define libusb_get_device_list libusb_ctx.get_device_list
764-
#define libusb_free_device_list libusb_ctx.free_device_list
765-
#define libusb_get_device_descriptor libusb_ctx.get_device_descriptor
766-
#define libusb_get_active_config_descriptor libusb_ctx.get_active_config_descriptor
767-
#define libusb_get_config_descriptor libusb_ctx.get_config_descriptor
768-
#define libusb_free_config_descriptor libusb_ctx.free_config_descriptor
769-
#define libusb_get_bus_number libusb_ctx.get_bus_number
770-
#define libusb_get_port_numbers libusb_ctx.get_port_numbers
771-
#define libusb_get_device_address libusb_ctx.get_device_address
772-
#define libusb_open libusb_ctx.open
773-
#define libusb_close libusb_ctx.close
774-
#define libusb_get_device libusb_ctx.get_device
775-
#define libusb_claim_interface libusb_ctx.claim_interface
776-
#define libusb_release_interface libusb_ctx.release_interface
777-
#define libusb_kernel_driver_active libusb_ctx.kernel_driver_active
778-
#define libusb_detach_kernel_driver libusb_ctx.detach_kernel_driver
779-
#define libusb_attach_kernel_driver libusb_ctx.attach_kernel_driver
780-
#define libusb_set_interface_alt_setting libusb_ctx.set_interface_alt_setting
781-
#define libusb_alloc_transfer libusb_ctx.alloc_transfer
782-
#define libusb_submit_transfer libusb_ctx.submit_transfer
783-
#define libusb_cancel_transfer libusb_ctx.cancel_transfer
784-
#define libusb_free_transfer libusb_ctx.free_transfer
785-
#define libusb_control_transfer libusb_ctx.control_transfer
786-
#define libusb_interrupt_transfer libusb_ctx.interrupt_transfer
787-
#define libusb_bulk_transfer libusb_ctx.bulk_transfer
788-
#define libusb_handle_events libusb_ctx.handle_events
789-
#define libusb_handle_events_completed libusb_ctx.handle_events_completed
790-
#define libusb_error_name libusb_ctx.error_name
692+
#include "../misc/SDL_libusb.h"
693+
694+
static SDL_LibUSBContext *libusb_ctx;
695+
696+
#define libusb_init libusb_ctx->init
697+
#define libusb_exit libusb_ctx->exit
698+
#define libusb_get_device_list libusb_ctx->get_device_list
699+
#define libusb_free_device_list libusb_ctx->free_device_list
700+
#define libusb_get_device_descriptor libusb_ctx->get_device_descriptor
701+
#define libusb_get_active_config_descriptor libusb_ctx->get_active_config_descriptor
702+
#define libusb_get_config_descriptor libusb_ctx->get_config_descriptor
703+
#define libusb_free_config_descriptor libusb_ctx->free_config_descriptor
704+
#define libusb_get_bus_number libusb_ctx->get_bus_number
705+
#define libusb_get_port_numbers libusb_ctx->get_port_numbers
706+
#define libusb_get_device_address libusb_ctx->get_device_address
707+
#define libusb_open libusb_ctx->open
708+
#define libusb_close libusb_ctx->close
709+
#define libusb_get_device libusb_ctx->get_device
710+
#define libusb_claim_interface libusb_ctx->claim_interface
711+
#define libusb_release_interface libusb_ctx->release_interface
712+
#define libusb_kernel_driver_active libusb_ctx->kernel_driver_active
713+
#define libusb_detach_kernel_driver libusb_ctx->detach_kernel_driver
714+
#define libusb_attach_kernel_driver libusb_ctx->attach_kernel_driver
715+
#define libusb_set_interface_alt_setting libusb_ctx->set_interface_alt_setting
716+
#define libusb_alloc_transfer libusb_ctx->alloc_transfer
717+
#define libusb_submit_transfer libusb_ctx->submit_transfer
718+
#define libusb_cancel_transfer libusb_ctx->cancel_transfer
719+
#define libusb_free_transfer libusb_ctx->free_transfer
720+
#define libusb_control_transfer libusb_ctx->control_transfer
721+
#define libusb_interrupt_transfer libusb_ctx->interrupt_transfer
722+
#define libusb_bulk_transfer libusb_ctx->bulk_transfer
723+
#define libusb_handle_events libusb_ctx->handle_events
724+
#define libusb_handle_events_completed libusb_ctx->handle_events_completed
725+
#define libusb_error_name libusb_ctx->error_name
791726

792727
struct LIBUSB_hid_device_;
793728
typedef struct LIBUSB_hid_device_ LIBUSB_hid_device;
@@ -1179,71 +1114,15 @@ int SDL_hid_init(void)
11791114
if (!SDL_GetHintBoolean(SDL_HINT_HIDAPI_LIBUSB, true)) {
11801115
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
11811116
"libusb disabled with SDL_HINT_HIDAPI_LIBUSB");
1182-
libusb_ctx.libhandle = NULL;
11831117
} else {
11841118
++attempts;
1185-
#ifdef SDL_LIBUSB_DYNAMIC
1186-
libusb_ctx.libhandle = SDL_LoadObject(SDL_LIBUSB_DYNAMIC);
1187-
#else
1188-
libusb_ctx.libhandle = (void *)1;
1189-
#endif
1190-
if (libusb_ctx.libhandle != NULL) {
1191-
bool loaded = true;
1192-
#ifdef SDL_LIBUSB_DYNAMIC
1193-
#define LOAD_LIBUSB_SYMBOL(type, func) \
1194-
if ((libusb_ctx.func = (type)SDL_LoadFunction(libusb_ctx.libhandle, "libusb_" #func)) == NULL) { \
1195-
loaded = false; \
1196-
}
1197-
#else
1198-
#define LOAD_LIBUSB_SYMBOL(type, func) \
1199-
libusb_ctx.func = libusb_##func;
1200-
#endif
1201-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context **), init)
1202-
LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_context *), exit)
1203-
LOAD_LIBUSB_SYMBOL(ssize_t (LIBUSB_CALL *)(libusb_context *, libusb_device ***), get_device_list)
1204-
LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device **, int), free_device_list)
1205-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_device_descriptor *), get_device_descriptor)
1206-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_config_descriptor **), get_active_config_descriptor)
1207-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, uint8_t, struct libusb_config_descriptor **), get_config_descriptor)
1208-
LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_config_descriptor *), free_config_descriptor)
1209-
LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_bus_number)
1210-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *dev, uint8_t *port_numbers, int port_numbers_len), get_port_numbers)
1211-
LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_device_address)
1212-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, libusb_device_handle **), open)
1213-
LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device_handle *), close)
1214-
LOAD_LIBUSB_SYMBOL(libusb_device * (LIBUSB_CALL *)(libusb_device_handle *dev_handle), get_device)
1215-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), claim_interface)
1216-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), release_interface)
1217-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), kernel_driver_active)
1218-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), detach_kernel_driver)
1219-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), attach_kernel_driver)
1220-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int, int), set_interface_alt_setting)
1221-
LOAD_LIBUSB_SYMBOL(struct libusb_transfer * (LIBUSB_CALL *)(int), alloc_transfer)
1222-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), submit_transfer)
1223-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), cancel_transfer)
1224-
LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_transfer *), free_transfer)
1225-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, uint8_t, uint8_t, uint16_t, uint16_t, unsigned char *, uint16_t, unsigned int), control_transfer)
1226-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), interrupt_transfer)
1227-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), bulk_transfer)
1228-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *), handle_events)
1229-
LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *, int *), handle_events_completed)
1230-
LOAD_LIBUSB_SYMBOL(const char * (LIBUSB_CALL *)(int), error_name)
1231-
#undef LOAD_LIBUSB_SYMBOL
1232-
1233-
if (!loaded) {
1234-
#ifdef SDL_LIBUSB_DYNAMIC
1235-
SDL_UnloadObject(libusb_ctx.libhandle);
1236-
#endif
1237-
libusb_ctx.libhandle = NULL;
1238-
// SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, SDL_LIBUSB_DYNAMIC " found but could not load function");
1239-
} else if (LIBUSB_hid_init() < 0) {
1240-
#ifdef SDL_LIBUSB_DYNAMIC
1241-
SDL_UnloadObject(libusb_ctx.libhandle);
1242-
#endif
1243-
libusb_ctx.libhandle = NULL;
1244-
} else {
1245-
++success;
1246-
}
1119+
if (!SDL_InitLibUSB(&libusb_ctx)) {
1120+
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, "Couldn't load libusb");
1121+
} else if (LIBUSB_hid_init() < 0) {
1122+
SDL_QuitLibUSB();
1123+
libusb_ctx = NULL;
1124+
} else {
1125+
++success;
12471126
}
12481127
}
12491128
#endif // HAVE_LIBUSB
@@ -1297,12 +1176,10 @@ int SDL_hid_exit(void)
12971176
#endif // HAVE_PLATFORM_BACKEND
12981177

12991178
#ifdef HAVE_LIBUSB
1300-
if (libusb_ctx.libhandle) {
1179+
if (libusb_ctx) {
13011180
result |= LIBUSB_hid_exit();
1302-
#ifdef SDL_LIBUSB_DYNAMIC
1303-
SDL_UnloadObject(libusb_ctx.libhandle);
1304-
#endif
1305-
libusb_ctx.libhandle = NULL;
1181+
SDL_QuitLibUSB();
1182+
libusb_ctx = NULL;
13061183
}
13071184
#endif // HAVE_LIBUSB
13081185

@@ -1443,7 +1320,7 @@ struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned
14431320
#endif
14441321

14451322
#ifdef HAVE_LIBUSB
1446-
if (libusb_ctx.libhandle) {
1323+
if (libusb_ctx) {
14471324
usb_devs = LIBUSB_hid_enumerate(vendor_id, product_id);
14481325

14491326
if (use_libusb_whitelist) {
@@ -1544,7 +1421,7 @@ SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id
15441421
#endif // HAVE_DRIVER_BACKEND
15451422

15461423
#ifdef HAVE_LIBUSB
1547-
if (libusb_ctx.libhandle != NULL) {
1424+
if (libusb_ctx) {
15481425
pDevice = LIBUSB_hid_open(vendor_id, product_id, serial_number);
15491426
if (pDevice != NULL) {
15501427
return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
@@ -1583,7 +1460,7 @@ SDL_hid_device *SDL_hid_open_path(const char *path)
15831460
#endif // HAVE_DRIVER_BACKEND
15841461

15851462
#ifdef HAVE_LIBUSB
1586-
if (libusb_ctx.libhandle != NULL) {
1463+
if (libusb_ctx) {
15871464
pDevice = LIBUSB_hid_open_path(path);
15881465
if (pDevice != NULL) {
15891466
return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
@@ -1724,46 +1601,46 @@ void SDL_EnableGameCubeAdaptors(void)
17241601
ssize_t i, num_devs;
17251602
int kernel_detached = 0;
17261603

1727-
if (libusb_ctx.libhandle == NULL) {
1604+
if (!libusb_ctx) {
17281605
return;
17291606
}
17301607

1731-
if (libusb_ctx.init(&context) == 0) {
1732-
num_devs = libusb_ctx.get_device_list(context, &devs);
1608+
if (libusb_ctx->init(&context) == 0) {
1609+
num_devs = libusb_ctx->get_device_list(context, &devs);
17331610
for (i = 0; i < num_devs; ++i) {
1734-
if (libusb_ctx.get_device_descriptor(devs[i], &desc) != 0) {
1611+
if (libusb_ctx->get_device_descriptor(devs[i], &desc) != 0) {
17351612
continue;
17361613
}
17371614

17381615
if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) {
17391616
continue;
17401617
}
17411618

1742-
if (libusb_ctx.open(devs[i], &handle) != 0) {
1619+
if (libusb_ctx->open(devs[i], &handle) != 0) {
17431620
continue;
17441621
}
17451622

1746-
if (libusb_ctx.kernel_driver_active(handle, 0)) {
1747-
if (libusb_ctx.detach_kernel_driver(handle, 0) == 0) {
1623+
if (libusb_ctx->kernel_driver_active(handle, 0)) {
1624+
if (libusb_ctx->detach_kernel_driver(handle, 0) == 0) {
17481625
kernel_detached = 1;
17491626
}
17501627
}
17511628

1752-
if (libusb_ctx.claim_interface(handle, 0) == 0) {
1753-
libusb_ctx.control_transfer(handle, 0x21, 11, 0x0001, 0, NULL, 0, 1000);
1754-
libusb_ctx.release_interface(handle, 0);
1629+
if (libusb_ctx->claim_interface(handle, 0) == 0) {
1630+
libusb_ctx->control_transfer(handle, 0x21, 11, 0x0001, 0, NULL, 0, 1000);
1631+
libusb_ctx->release_interface(handle, 0);
17551632
}
17561633

17571634
if (kernel_detached) {
1758-
libusb_ctx.attach_kernel_driver(handle, 0);
1635+
libusb_ctx->attach_kernel_driver(handle, 0);
17591636
}
17601637

1761-
libusb_ctx.close(handle);
1638+
libusb_ctx->close(handle);
17621639
}
17631640

1764-
libusb_ctx.free_device_list(devs, 1);
1641+
libusb_ctx->free_device_list(devs, 1);
17651642

1766-
libusb_ctx.exit(context);
1643+
libusb_ctx->exit(context);
17671644
}
17681645
#endif // HAVE_LIBUSB
17691646
}

0 commit comments

Comments
 (0)