1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright (C) 1997-2021 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 | #ifndef SDL_JOYSTICK_HIDAPI_H |
24 | #define SDL_JOYSTICK_HIDAPI_H |
25 | |
26 | #include "SDL_atomic.h" |
27 | #include "SDL_mutex.h" |
28 | #include "SDL_joystick.h" |
29 | #include "SDL_gamecontroller.h" |
30 | #include "../../hidapi/hidapi/hidapi.h" |
31 | #include "../usb_ids.h" |
32 | |
33 | /* This is the full set of HIDAPI drivers available */ |
34 | #define SDL_JOYSTICK_HIDAPI_GAMECUBE |
35 | #define SDL_JOYSTICK_HIDAPI_PS4 |
36 | #define SDL_JOYSTICK_HIDAPI_PS5 |
37 | #define SDL_JOYSTICK_HIDAPI_STADIA |
38 | #define SDL_JOYSTICK_HIDAPI_SWITCH |
39 | #define SDL_JOYSTICK_HIDAPI_XBOX360 |
40 | #define SDL_JOYSTICK_HIDAPI_XBOXONE |
41 | |
42 | #if defined(__IPHONEOS__) || defined(__TVOS__) || defined(__ANDROID__) |
43 | /* Very basic Steam Controller support on mobile devices */ |
44 | #define SDL_JOYSTICK_HIDAPI_STEAM |
45 | #endif |
46 | |
47 | /* The maximum size of a USB packet for HID devices */ |
48 | #define USB_PACKET_LENGTH 64 |
49 | |
50 | /* Forward declaration */ |
51 | struct _SDL_HIDAPI_DeviceDriver; |
52 | |
53 | typedef struct _SDL_HIDAPI_Device |
54 | { |
55 | char *name; |
56 | char *path; |
57 | Uint16 vendor_id; |
58 | Uint16 product_id; |
59 | Uint16 version; |
60 | char *serial; |
61 | SDL_JoystickGUID guid; |
62 | int interface_number; /* Available on Windows and Linux */ |
63 | int interface_class; |
64 | int interface_subclass; |
65 | int interface_protocol; |
66 | Uint16 usage_page; /* Available on Windows and Mac OS X */ |
67 | Uint16 usage; /* Available on Windows and Mac OS X */ |
68 | |
69 | struct _SDL_HIDAPI_DeviceDriver *driver; |
70 | void *context; |
71 | SDL_mutex *dev_lock; |
72 | hid_device *dev; |
73 | SDL_atomic_t rumble_pending; |
74 | int num_joysticks; |
75 | SDL_JoystickID *joysticks; |
76 | |
77 | /* Used during scanning for device changes */ |
78 | SDL_bool seen; |
79 | |
80 | /* Used to flag that the device is being updated */ |
81 | SDL_bool updating; |
82 | |
83 | struct _SDL_HIDAPI_Device *next; |
84 | } SDL_HIDAPI_Device; |
85 | |
86 | typedef struct _SDL_HIDAPI_DeviceDriver |
87 | { |
88 | const char *hint; |
89 | SDL_bool enabled; |
90 | SDL_bool (*IsSupportedDevice)(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol); |
91 | const char *(*GetDeviceName)(Uint16 vendor_id, Uint16 product_id); |
92 | SDL_bool (*InitDevice)(SDL_HIDAPI_Device *device); |
93 | int (*GetDevicePlayerIndex)(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id); |
94 | void (*SetDevicePlayerIndex)(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index); |
95 | SDL_bool (*UpdateDevice)(SDL_HIDAPI_Device *device); |
96 | SDL_bool (*OpenJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick); |
97 | int (*RumbleJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble); |
98 | int (*RumbleJoystickTriggers)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble); |
99 | SDL_bool (*HasJoystickLED)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick); |
100 | int (*SetJoystickLED)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue); |
101 | int (*SetJoystickSensorsEnabled)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled); |
102 | void (*CloseJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick); |
103 | void (*FreeDevice)(SDL_HIDAPI_Device *device); |
104 | |
105 | } SDL_HIDAPI_DeviceDriver; |
106 | |
107 | |
108 | /* HIDAPI device support */ |
109 | extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube; |
110 | extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS4; |
111 | extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS5; |
112 | extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverStadia; |
113 | extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteam; |
114 | extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch; |
115 | extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360; |
116 | extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360W; |
117 | extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne; |
118 | |
119 | /* Return true if a HID device is present and supported as a joystick */ |
120 | extern SDL_bool HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name); |
121 | |
122 | extern void HIDAPI_UpdateDevices(void); |
123 | extern SDL_bool HIDAPI_JoystickConnected(SDL_HIDAPI_Device *device, SDL_JoystickID *pJoystickID); |
124 | extern void HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID); |
125 | |
126 | extern void HIDAPI_DumpPacket(const char *prefix, Uint8 *data, int size); |
127 | |
128 | extern float HIDAPI_RemapVal(float val, float val_min, float val_max, float output_min, float output_max); |
129 | |
130 | #endif /* SDL_JOYSTICK_HIDAPI_H */ |
131 | |
132 | /* vi: set ts=4 sw=4 expandtab: */ |
133 | |