Skip to content
0.2.0-dev Download ZIP
123 lines ·6.4 KB Raw
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
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), set_auto_detach_kernel_driver)
83 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int, int), set_interface_alt_setting)
84 LOAD_LIBUSB_SYMBOL(struct libusb_transfer * (LIBUSB_CALL *)(int), alloc_transfer)
85 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), submit_transfer)
86 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), cancel_transfer)
87 LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_transfer *), free_transfer)
88 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)
89 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), interrupt_transfer)
90 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), bulk_transfer)
91 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *), handle_events)
92 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *, int *), handle_events_completed)
93 LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_context *ctx), interrupt_event_handler)
94 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(uint32_t), has_capability)
95 LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *, int, int, int, int, int, libusb_hotplug_callback_fn, void *, libusb_hotplug_callback_handle *), hotplug_register_callback)
96 LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_context *, libusb_hotplug_callback_handle), hotplug_deregister_callback)
97 LOAD_LIBUSB_SYMBOL(const char * (LIBUSB_CALL *)(int), error_name)
98#undef LOAD_LIBUSB_SYMBOL
99 }
100 }
101
102 if (SDL_libusb_loaded) {
103 *ctx = &SDL_libusb_context;
104 return true;
105 } else {
106 SDL_QuitLibUSB();
107 *ctx = NULL;
108 return false;
109 }
110}
111
112void SDL_QuitLibUSB(void)
113{
114 if (SDL_AtomicDecRef(&SDL_libusb_refcount)) {
115 if (SDL_libusb_handle) {
116 SDL_UnloadObject(SDL_libusb_handle);
117 SDL_libusb_handle = NULL;
118 }
119 SDL_libusb_loaded = false;
120 }
121}
122
123#endif // HAVE_LIBUSB