1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2020 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_PS4
35 #define SDL_JOYSTICK_HIDAPI_SWITCH
36 #define SDL_JOYSTICK_HIDAPI_XBOX360
37 #define SDL_JOYSTICK_HIDAPI_XBOXONE
38 #define SDL_JOYSTICK_HIDAPI_GAMECUBE
39 
40 #ifdef __WINDOWS__
41 /* On Windows, Xbox One controllers are handled by the Xbox 360 driver */
42 #undef SDL_JOYSTICK_HIDAPI_XBOXONE
43 #endif
44 
45 #if defined(__IPHONEOS__) || defined(__TVOS__) || defined(__ANDROID__)
46 /* Very basic Steam Controller support on mobile devices */
47 #define SDL_JOYSTICK_HIDAPI_STEAM
48 #endif
49 
50 /* The maximum size of a USB packet for HID devices */
51 #define USB_PACKET_LENGTH   64
52 
53 /* Forward declaration */
54 struct _SDL_HIDAPI_DeviceDriver;
55 
56 typedef struct _SDL_HIDAPI_Device
57 {
58     char *name;
59     char *path;
60     Uint16 vendor_id;
61     Uint16 product_id;
62     Uint16 version;
63     SDL_JoystickGUID guid;
64     int interface_number;   /* Available on Windows and Linux */
65     int interface_class;
66     int interface_subclass;
67     int interface_protocol;
68     Uint16 usage_page;      /* Available on Windows and Mac OS X */
69     Uint16 usage;           /* Available on Windows and Mac OS X */
70 
71     struct _SDL_HIDAPI_DeviceDriver *driver;
72     void *context;
73     SDL_mutex *dev_lock;
74     hid_device *dev;
75     SDL_atomic_t rumble_pending;
76     int num_joysticks;
77     SDL_JoystickID *joysticks;
78 
79     /* Used during scanning for device changes */
80     SDL_bool seen;
81 
82     struct _SDL_HIDAPI_Device *next;
83 } SDL_HIDAPI_Device;
84 
85 typedef struct _SDL_HIDAPI_DeviceDriver
86 {
87     const char *hint;
88     SDL_bool enabled;
89     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);
90     const char *(*GetDeviceName)(Uint16 vendor_id, Uint16 product_id);
91     SDL_bool (*InitDevice)(SDL_HIDAPI_Device *device);
92     int (*GetDevicePlayerIndex)(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id);
93     void (*SetDevicePlayerIndex)(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index);
94     SDL_bool (*UpdateDevice)(SDL_HIDAPI_Device *device);
95     SDL_bool (*OpenJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick);
96     int (*RumbleJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble);
97     void (*CloseJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick);
98     void (*FreeDevice)(SDL_HIDAPI_Device *device);
99     void (*PostUpdate)(void);
100 #ifdef SDL_JOYSTICK_RAWINPUT
101     void (*HandleStatePacketFromRAWINPUT)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 *data, int size);
102 #endif
103 
104 } SDL_HIDAPI_DeviceDriver;
105 
106 
107 /* HIDAPI device support */
108 extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS4;
109 extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteam;
110 extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch;
111 extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360;
112 extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360W;
113 extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne;
114 extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube;
115 
116 /* Return true if a HID device is present and supported as a joystick */
117 extern SDL_bool HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name);
118 
119 extern void HIDAPI_UpdateDevices(void);
120 extern SDL_bool HIDAPI_JoystickConnected(SDL_HIDAPI_Device *device, SDL_JoystickID *pJoystickID, SDL_bool is_external);
121 extern void HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID, SDL_bool is_external);
122 
123 #endif /* SDL_JOYSTICK_HIDAPI_H */
124 
125 /* vi: set ts=4 sw=4 expandtab: */
126