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