@@ -689,105 +689,40 @@ typedef struct DRIVER_hid_device_ DRIVER_hid_device;
689
689
#ifdef HAVE_LIBUSB
690
690
// libusb HIDAPI Implementation
691
691
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
791
726
792
727
struct LIBUSB_hid_device_ ;
793
728
typedef struct LIBUSB_hid_device_ LIBUSB_hid_device ;
@@ -1179,71 +1114,15 @@ int SDL_hid_init(void)
1179
1114
if (!SDL_GetHintBoolean (SDL_HINT_HIDAPI_LIBUSB , true)) {
1180
1115
SDL_LogDebug (SDL_LOG_CATEGORY_INPUT ,
1181
1116
"libusb disabled with SDL_HINT_HIDAPI_LIBUSB" );
1182
- libusb_ctx .libhandle = NULL ;
1183
1117
} else {
1184
1118
++ 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 ;
1247
1126
}
1248
1127
}
1249
1128
#endif // HAVE_LIBUSB
@@ -1297,12 +1176,10 @@ int SDL_hid_exit(void)
1297
1176
#endif // HAVE_PLATFORM_BACKEND
1298
1177
1299
1178
#ifdef HAVE_LIBUSB
1300
- if (libusb_ctx . libhandle ) {
1179
+ if (libusb_ctx ) {
1301
1180
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 ;
1306
1183
}
1307
1184
#endif // HAVE_LIBUSB
1308
1185
@@ -1443,7 +1320,7 @@ struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned
1443
1320
#endif
1444
1321
1445
1322
#ifdef HAVE_LIBUSB
1446
- if (libusb_ctx . libhandle ) {
1323
+ if (libusb_ctx ) {
1447
1324
usb_devs = LIBUSB_hid_enumerate (vendor_id , product_id );
1448
1325
1449
1326
if (use_libusb_whitelist ) {
@@ -1544,7 +1421,7 @@ SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id
1544
1421
#endif // HAVE_DRIVER_BACKEND
1545
1422
1546
1423
#ifdef HAVE_LIBUSB
1547
- if (libusb_ctx . libhandle != NULL ) {
1424
+ if (libusb_ctx ) {
1548
1425
pDevice = LIBUSB_hid_open (vendor_id , product_id , serial_number );
1549
1426
if (pDevice != NULL ) {
1550
1427
return CreateHIDDeviceWrapper (pDevice , & LIBUSB_Backend );
@@ -1583,7 +1460,7 @@ SDL_hid_device *SDL_hid_open_path(const char *path)
1583
1460
#endif // HAVE_DRIVER_BACKEND
1584
1461
1585
1462
#ifdef HAVE_LIBUSB
1586
- if (libusb_ctx . libhandle != NULL ) {
1463
+ if (libusb_ctx ) {
1587
1464
pDevice = LIBUSB_hid_open_path (path );
1588
1465
if (pDevice != NULL ) {
1589
1466
return CreateHIDDeviceWrapper (pDevice , & LIBUSB_Backend );
@@ -1724,46 +1601,46 @@ void SDL_EnableGameCubeAdaptors(void)
1724
1601
ssize_t i , num_devs ;
1725
1602
int kernel_detached = 0 ;
1726
1603
1727
- if (libusb_ctx . libhandle == NULL ) {
1604
+ if (! libusb_ctx ) {
1728
1605
return ;
1729
1606
}
1730
1607
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 );
1733
1610
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 ) {
1735
1612
continue ;
1736
1613
}
1737
1614
1738
1615
if (desc .idVendor != 0x057e || desc .idProduct != 0x0337 ) {
1739
1616
continue ;
1740
1617
}
1741
1618
1742
- if (libusb_ctx . open (devs [i ], & handle ) != 0 ) {
1619
+ if (libusb_ctx -> open (devs [i ], & handle ) != 0 ) {
1743
1620
continue ;
1744
1621
}
1745
1622
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 ) {
1748
1625
kernel_detached = 1 ;
1749
1626
}
1750
1627
}
1751
1628
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 );
1755
1632
}
1756
1633
1757
1634
if (kernel_detached ) {
1758
- libusb_ctx . attach_kernel_driver (handle , 0 );
1635
+ libusb_ctx -> attach_kernel_driver (handle , 0 );
1759
1636
}
1760
1637
1761
- libusb_ctx . close (handle );
1638
+ libusb_ctx -> close (handle );
1762
1639
}
1763
1640
1764
- libusb_ctx . free_device_list (devs , 1 );
1641
+ libusb_ctx -> free_device_list (devs , 1 );
1765
1642
1766
- libusb_ctx . exit (context );
1643
+ libusb_ctx -> exit (context );
1767
1644
}
1768
1645
#endif // HAVE_LIBUSB
1769
1646
}
0 commit comments