Atlas - SDL_libusb.c

Home / ext / SDL / src / misc Lines: 1 | Size: 5988 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21#include "SDL_internal.h" 22 23#include "SDL_libusb.h" 24 25#ifdef HAVE_LIBUSB 26 27#ifdef SDL_LIBUSB_DYNAMIC 28SDL_ELF_NOTE_DLOPEN( 29 "libusb", 30 "Support for joysticks through libusb", 31 SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED, 32 SDL_LIBUSB_DYNAMIC 33) 34#endif 35 36static SDL_AtomicInt SDL_libusb_refcount; 37static bool SDL_libusb_loaded; 38static SDL_SharedObject *SDL_libusb_handle; 39static SDL_LibUSBContext SDL_libusb_context; 40 41bool SDL_InitLibUSB(SDL_LibUSBContext **ctx) 42{ 43 if (SDL_AtomicIncRef(&SDL_libusb_refcount) == 0) { 44#ifdef SDL_LIBUSB_DYNAMIC 45 SDL_libusb_handle = SDL_LoadObject(SDL_LIBUSB_DYNAMIC); 46 if (SDL_libusb_handle) 47#endif 48 { 49 SDL_libusb_loaded = true; 50#ifdef SDL_LIBUSB_DYNAMIC 51#define LOAD_LIBUSB_SYMBOL(type, func) \ 52 if ((SDL_libusb_context.func = (type)SDL_LoadFunction(SDL_libusb_handle, "libusb_" #func)) == NULL) { \ 53 SDL_libusb_loaded = false; \ 54 } 55#else 56#define LOAD_LIBUSB_SYMBOL(type, func) \ 57 SDL_libusb_context.func = libusb_##func; 58#endif 59 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context **), init) 60 LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_context *), exit) 61 LOAD_LIBUSB_SYMBOL(ssize_t (LIBUSB_CALL *)(libusb_context *, libusb_device ***), get_device_list) 62 LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device **, int), free_device_list) 63 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_device_descriptor *), get_device_descriptor) 64 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_config_descriptor **), get_active_config_descriptor) 65 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, uint8_t, struct libusb_config_descriptor **), get_config_descriptor) 66 LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_config_descriptor *), free_config_descriptor) 67 LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_bus_number) 68#ifdef SDL_PLATFORM_FREEBSD 69 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *dev, uint8_t *port_numbers, uint8_t port_numbers_len), get_port_numbers) 70#else 71 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *dev, uint8_t *port_numbers, int port_numbers_len), get_port_numbers) 72#endif 73 LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_device_address) 74 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, libusb_device_handle **), open) 75 LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device_handle *), close) 76 LOAD_LIBUSB_SYMBOL(libusb_device * (LIBUSB_CALL *)(libusb_device_handle *dev_handle), get_device) 77 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), claim_interface) 78 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), release_interface) 79 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), kernel_driver_active) 80 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), detach_kernel_driver) 81 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), attach_kernel_driver) 82 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int, int), set_interface_alt_setting) 83 LOAD_LIBUSB_SYMBOL(struct libusb_transfer * (LIBUSB_CALL *)(int), alloc_transfer) 84 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), submit_transfer) 85 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), cancel_transfer) 86 LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_transfer *), free_transfer) 87 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) 88 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), interrupt_transfer) 89 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), bulk_transfer) 90 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *), handle_events) 91 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *, int *), handle_events_completed) 92 LOAD_LIBUSB_SYMBOL(const char * (LIBUSB_CALL *)(int), error_name) 93#undef LOAD_LIBUSB_SYMBOL 94 } 95 } 96 97 if (SDL_libusb_loaded) { 98 *ctx = &SDL_libusb_context; 99 return true; 100 } else { 101 SDL_QuitLibUSB(); 102 *ctx = NULL; 103 return false; 104 } 105} 106 107void SDL_QuitLibUSB(void) 108{ 109 if (SDL_AtomicDecRef(&SDL_libusb_refcount)) { 110 if (SDL_libusb_handle) { 111 SDL_UnloadObject(SDL_libusb_handle); 112 SDL_libusb_handle = NULL; 113 } 114 SDL_libusb_loaded = false; 115 } 116} 117 118#endif // HAVE_LIBUSB 119
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.