Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/sdl/joystick/SDL_sysjoystick.h
9903 views
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
#ifndef SDL_sysjoystick_h_
24
#define SDL_sysjoystick_h_
25
26
// This is the system specific header for the SDL joystick API
27
#include "SDL_joystick_c.h"
28
29
#include <stdlib.h>
30
31
// Set up for C function definitions, even when using C++
32
#ifdef __cplusplus
33
extern "C" {
34
#endif
35
36
// The SDL joystick structure
37
38
typedef struct SDL_JoystickAxisInfo
39
{
40
Sint16 initial_value; // Initial axis state
41
Sint16 value; // Current axis state
42
Sint16 zero; // Zero point on the axis (-32768 for triggers)
43
bool has_initial_value; // Whether we've seen a value on the axis yet
44
bool has_second_value; // Whether we've seen a second value on the axis yet
45
bool sent_initial_value; // Whether we've sent the initial axis value
46
bool sending_initial_value; // Whether we are sending the initial axis value
47
} SDL_JoystickAxisInfo;
48
49
typedef struct SDL_JoystickBallData
50
{
51
int dx;
52
int dy;
53
} SDL_JoystickBallData;
54
55
typedef struct SDL_JoystickTouchpadFingerInfo
56
{
57
bool down;
58
float x;
59
float y;
60
float pressure;
61
} SDL_JoystickTouchpadFingerInfo;
62
63
typedef struct SDL_JoystickTouchpadInfo
64
{
65
int nfingers;
66
SDL_JoystickTouchpadFingerInfo *fingers;
67
} SDL_JoystickTouchpadInfo;
68
69
typedef struct SDL_JoystickSensorInfo
70
{
71
SDL_SensorType type;
72
bool enabled;
73
float rate;
74
float data[3]; // If this needs to expand, update SDL_GamepadSensorEvent
75
} SDL_JoystickSensorInfo;
76
77
#define _guarded SDL_GUARDED_BY(SDL_joystick_lock)
78
79
struct SDL_Joystick
80
{
81
SDL_JoystickID instance_id _guarded; // Device instance, monotonically increasing from 0
82
char *name _guarded; // Joystick name - system dependent
83
char *path _guarded; // Joystick path - system dependent
84
char *serial _guarded; // Joystick serial
85
SDL_GUID guid _guarded; // Joystick guid
86
Uint16 firmware_version _guarded; // Firmware version, if available
87
Uint64 steam_handle _guarded; // Steam controller API handle
88
bool swap_face_buttons _guarded; // Whether we should swap face buttons
89
bool is_virtual _guarded; // Whether this is a virtual joystick
90
91
int naxes _guarded; // Number of axis controls on the joystick
92
SDL_JoystickAxisInfo *axes _guarded;
93
94
int nballs _guarded; // Number of trackballs on the joystick
95
SDL_JoystickBallData *balls _guarded; // Current ball motion deltas
96
97
int nhats _guarded; // Number of hats on the joystick
98
Uint8 *hats _guarded; // Current hat states
99
100
int nbuttons _guarded; // Number of buttons on the joystick
101
bool *buttons _guarded; // Current button states
102
103
int ntouchpads _guarded; // Number of touchpads on the joystick
104
SDL_JoystickTouchpadInfo *touchpads _guarded; // Current touchpad states
105
106
int nsensors _guarded; // Number of sensors on the joystick
107
int nsensors_enabled _guarded;
108
SDL_JoystickSensorInfo *sensors _guarded;
109
110
Uint16 low_frequency_rumble _guarded;
111
Uint16 high_frequency_rumble _guarded;
112
Uint64 rumble_expiration _guarded;
113
Uint64 rumble_resend _guarded;
114
115
Uint16 left_trigger_rumble _guarded;
116
Uint16 right_trigger_rumble _guarded;
117
Uint64 trigger_rumble_expiration _guarded;
118
Uint64 trigger_rumble_resend _guarded;
119
120
Uint8 led_red _guarded;
121
Uint8 led_green _guarded;
122
Uint8 led_blue _guarded;
123
Uint64 led_expiration _guarded;
124
125
bool attached _guarded;
126
SDL_JoystickConnectionState connection_state _guarded;
127
SDL_PowerState battery_state _guarded;
128
int battery_percent _guarded;
129
130
bool delayed_guide_button _guarded; // true if this device has the guide button event delayed
131
132
SDL_SensorID accel_sensor _guarded;
133
SDL_Sensor *accel _guarded;
134
SDL_SensorID gyro_sensor _guarded;
135
SDL_Sensor *gyro _guarded;
136
float sensor_transform[3][3] _guarded;
137
138
Uint64 update_complete _guarded;
139
140
struct SDL_JoystickDriver *driver _guarded;
141
142
struct joystick_hwdata *hwdata _guarded; // Driver dependent information
143
144
SDL_PropertiesID props _guarded;
145
146
int ref_count _guarded; // Reference count for multiple opens
147
148
struct SDL_Joystick *next _guarded; // pointer to next joystick we have allocated
149
};
150
151
#undef _guarded
152
153
// Device bus definitions
154
#define SDL_HARDWARE_BUS_UNKNOWN 0x00
155
#define SDL_HARDWARE_BUS_USB 0x03
156
#define SDL_HARDWARE_BUS_BLUETOOTH 0x05
157
#define SDL_HARDWARE_BUS_VIRTUAL 0xFF
158
159
// Macro to combine a USB vendor ID and product ID into a single Uint32 value
160
#define MAKE_VIDPID(VID, PID) (((Uint32)(VID)) << 16 | (PID))
161
162
typedef struct SDL_JoystickDriver
163
{
164
/* Function to scan the system for joysticks.
165
* Joystick 0 should be the system default joystick.
166
* This function should return 0, or -1 on an unrecoverable error.
167
*/
168
bool (*Init)(void);
169
170
// Function to return the number of joystick devices plugged in right now
171
int (*GetCount)(void);
172
173
// Function to cause any queued joystick insertions to be processed
174
void (*Detect)(void);
175
176
// Function to determine whether a device is currently detected by this driver
177
bool (*IsDevicePresent)(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name);
178
179
// Function to get the device-dependent name of a joystick
180
const char *(*GetDeviceName)(int device_index);
181
182
// Function to get the device-dependent path of a joystick
183
const char *(*GetDevicePath)(int device_index);
184
185
// Function to get the Steam virtual gamepad slot of a joystick
186
int (*GetDeviceSteamVirtualGamepadSlot)(int device_index);
187
188
// Function to get the player index of a joystick
189
int (*GetDevicePlayerIndex)(int device_index);
190
191
// Function to set the player index of a joystick
192
void (*SetDevicePlayerIndex)(int device_index, int player_index);
193
194
// Function to return the stable GUID for a plugged in device
195
SDL_GUID (*GetDeviceGUID)(int device_index);
196
197
// Function to get the current instance id of the joystick located at device_index
198
SDL_JoystickID (*GetDeviceInstanceID)(int device_index);
199
200
/* Function to open a joystick for use.
201
The joystick to open is specified by the device index.
202
This should fill the nbuttons and naxes fields of the joystick structure.
203
It returns 0, or -1 if there is an error.
204
*/
205
bool (*Open)(SDL_Joystick *joystick, int device_index);
206
207
// Rumble functionality
208
bool (*Rumble)(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble);
209
bool (*RumbleTriggers)(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble);
210
211
// LED functionality
212
bool (*SetLED)(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue);
213
214
// General effects
215
bool (*SendEffect)(SDL_Joystick *joystick, const void *data, int size);
216
217
// Sensor functionality
218
bool (*SetSensorsEnabled)(SDL_Joystick *joystick, bool enabled);
219
220
/* Function to update the state of a joystick - called as a device poll.
221
* This function shouldn't update the joystick structure directly,
222
* but instead should call SDL_PrivateJoystick*() to deliver events
223
* and update joystick device state.
224
*/
225
void (*Update)(SDL_Joystick *joystick);
226
227
// Function to close a joystick after use
228
void (*Close)(SDL_Joystick *joystick);
229
230
// Function to perform any system-specific joystick related cleanup
231
void (*Quit)(void);
232
233
// Function to get the autodetected controller mapping; returns false if there isn't any.
234
bool (*GetGamepadMapping)(int device_index, SDL_GamepadMapping *out);
235
236
} SDL_JoystickDriver;
237
238
// Windows and Mac OSX has a limit of MAX_DWORD / 1000, Linux kernel has a limit of 0xFFFF
239
#define SDL_MAX_RUMBLE_DURATION_MS 0xFFFF
240
241
/* Dualshock4 only rumbles for about 5 seconds max, resend rumble command every 2 seconds
242
* to make long rumble work. */
243
#define SDL_RUMBLE_RESEND_MS 2000
244
245
#define SDL_LED_MIN_REPEAT_MS 5000
246
247
// The available joystick drivers
248
extern SDL_JoystickDriver SDL_PRIVATE_JoystickDriver;
249
extern SDL_JoystickDriver SDL_ANDROID_JoystickDriver;
250
extern SDL_JoystickDriver SDL_BSD_JoystickDriver;
251
extern SDL_JoystickDriver SDL_DARWIN_JoystickDriver;
252
extern SDL_JoystickDriver SDL_DUMMY_JoystickDriver;
253
extern SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver;
254
extern SDL_JoystickDriver SDL_HAIKU_JoystickDriver;
255
extern SDL_JoystickDriver SDL_HIDAPI_JoystickDriver;
256
extern SDL_JoystickDriver SDL_RAWINPUT_JoystickDriver;
257
extern SDL_JoystickDriver SDL_IOS_JoystickDriver;
258
extern SDL_JoystickDriver SDL_LINUX_JoystickDriver;
259
extern SDL_JoystickDriver SDL_VIRTUAL_JoystickDriver;
260
extern SDL_JoystickDriver SDL_WGI_JoystickDriver;
261
extern SDL_JoystickDriver SDL_WINDOWS_JoystickDriver;
262
extern SDL_JoystickDriver SDL_WINMM_JoystickDriver;
263
extern SDL_JoystickDriver SDL_PS2_JoystickDriver;
264
extern SDL_JoystickDriver SDL_PSP_JoystickDriver;
265
extern SDL_JoystickDriver SDL_VITA_JoystickDriver;
266
extern SDL_JoystickDriver SDL_N3DS_JoystickDriver;
267
extern SDL_JoystickDriver SDL_GAMEINPUT_JoystickDriver;
268
269
// Ends C function definitions when using C++
270
#ifdef __cplusplus
271
}
272
#endif
273
274
#endif // SDL_sysjoystick_h_
275
276