SDL 3.0
SDL_joystick.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 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
22/**
23 * \file SDL_joystick.h
24 *
25 * Include file for SDL joystick event handling
26 *
27 * The term "instance_id" is the current instantiation of a joystick device in the system, if the joystick is removed and then re-inserted
28 * then it will get a new instance_id, instance_id's are monotonically increasing identifiers of a joystick plugged in.
29 *
30 * The term "player_index" is the number assigned to a player on a specific
31 * controller. For XInput controllers this returns the XInput user index.
32 * Many joysticks will not be able to supply this information.
33 *
34 * The term JoystickGUID is a stable 128-bit identifier for a joystick device that does not change over time, it identifies class of
35 * the device (a X360 wired controller for example). This identifier is platform dependent.
36 */
37
38#ifndef SDL_joystick_h_
39#define SDL_joystick_h_
40
41#include <SDL3/SDL_stdinc.h>
42#include <SDL3/SDL_error.h>
43#include <SDL3/SDL_guid.h>
44#include <SDL3/SDL_mutex.h>
45#include <SDL3/SDL_properties.h>
46
47#include <SDL3/SDL_begin_code.h>
48/* Set up for C function definitions, even when using C++ */
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53/**
54 * \file SDL_joystick.h
55 *
56 * In order to use these functions, SDL_Init() must have been called
57 * with the ::SDL_INIT_JOYSTICK flag. This causes SDL to scan the system
58 * for joysticks, and load appropriate drivers.
59 *
60 * If you would like to receive joystick updates while the application
61 * is in the background, you should set the following hint before calling
62 * SDL_Init(): SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
63 */
64
65/**
66 * The joystick structure used to identify an SDL joystick
67 */
68#ifdef SDL_THREAD_SAFETY_ANALYSIS
69extern SDL_Mutex *SDL_joystick_lock;
70#endif
71struct SDL_Joystick;
73
74/* A structure that encodes the stable unique id for a joystick device */
76
77/**
78 * This is a unique ID for a joystick for the time it is connected to the system,
79 * and is never reused for the lifetime of the application. If the joystick is
80 * disconnected and reconnected, it will get a new ID.
81 *
82 * The ID value starts at 1 and increments from there. The value 0 is an invalid ID.
83 */
85
99
110
111#define SDL_JOYSTICK_AXIS_MAX 32767
112#define SDL_JOYSTICK_AXIS_MIN -32768
113
114/* Set max recognized G-force from accelerometer
115 See src/joystick/uikit/SDL_sysjoystick.m for notes on why this is needed
116 */
117#define SDL_IPHONE_MAX_GFORCE 5.0
118
119
120/* Function prototypes */
121
122/**
123 * Locking for atomic access to the joystick API
124 *
125 * The SDL joystick functions are thread-safe, however you can lock the
126 * joysticks while processing to guarantee that the joystick list won't change
127 * and joystick and gamepad events will not be delivered.
128 *
129 * \since This function is available since SDL 3.0.0.
130 */
131extern DECLSPEC void SDLCALL SDL_LockJoysticks(void) SDL_ACQUIRE(SDL_joystick_lock);
132
133/**
134 * Unlocking for atomic access to the joystick API
135 *
136 * \since This function is available since SDL 3.0.0.
137 */
138extern DECLSPEC void SDLCALL SDL_UnlockJoysticks(void) SDL_RELEASE(SDL_joystick_lock);
139
140/**
141 * Get a list of currently connected joysticks.
142 *
143 * \param count a pointer filled in with the number of joysticks returned
144 * \returns a 0 terminated array of joystick instance IDs which should be
145 * freed with SDL_free(), or NULL on error; call SDL_GetError() for
146 * more details.
147 *
148 * \since This function is available since SDL 3.0.0.
149 *
150 * \sa SDL_OpenJoystick
151 */
152extern DECLSPEC SDL_JoystickID *SDLCALL SDL_GetJoysticks(int *count);
153
154/**
155 * Get the implementation dependent name of a joystick.
156 *
157 * This can be called before any joysticks are opened.
158 *
159 * \param instance_id the joystick instance ID
160 * \returns the name of the selected joystick. If no name can be found, this
161 * function returns NULL; call SDL_GetError() for more information.
162 *
163 * \since This function is available since SDL 3.0.0.
164 *
165 * \sa SDL_GetJoystickName
166 * \sa SDL_OpenJoystick
167 */
168extern DECLSPEC const char *SDLCALL SDL_GetJoystickInstanceName(SDL_JoystickID instance_id);
169
170/**
171 * Get the implementation dependent path of a joystick.
172 *
173 * This can be called before any joysticks are opened.
174 *
175 * \param instance_id the joystick instance ID
176 * \returns the path of the selected joystick. If no path can be found, this
177 * function returns NULL; call SDL_GetError() for more information.
178 *
179 * \since This function is available since SDL 3.0.0.
180 *
181 * \sa SDL_GetJoystickPath
182 * \sa SDL_OpenJoystick
183 */
184extern DECLSPEC const char *SDLCALL SDL_GetJoystickInstancePath(SDL_JoystickID instance_id);
185
186/**
187 * Get the player index of a joystick.
188 *
189 * This can be called before any joysticks are opened.
190 *
191 * \param instance_id the joystick instance ID
192 * \returns the player index of a joystick, or -1 if it's not available
193 *
194 * \since This function is available since SDL 3.0.0.
195 *
196 * \sa SDL_GetJoystickPlayerIndex
197 * \sa SDL_OpenJoystick
198 */
199extern DECLSPEC int SDLCALL SDL_GetJoystickInstancePlayerIndex(SDL_JoystickID instance_id);
200
201/**
202 * Get the implementation-dependent GUID of a joystick.
203 *
204 * This can be called before any joysticks are opened.
205 *
206 * \param instance_id the joystick instance ID
207 * \returns the GUID of the selected joystick. If called on an invalid index,
208 * this function returns a zero GUID
209 *
210 * \since This function is available since SDL 3.0.0.
211 *
212 * \sa SDL_GetJoystickGUID
213 * \sa SDL_GetJoystickGUIDString
214 */
216
217/**
218 * Get the USB vendor ID of a joystick, if available.
219 *
220 * This can be called before any joysticks are opened. If the vendor ID isn't
221 * available this function returns 0.
222 *
223 * \param instance_id the joystick instance ID
224 * \returns the USB vendor ID of the selected joystick. If called on an
225 * invalid index, this function returns zero
226 *
227 * \since This function is available since SDL 3.0.0.
228 */
229extern DECLSPEC Uint16 SDLCALL SDL_GetJoystickInstanceVendor(SDL_JoystickID instance_id);
230
231/**
232 * Get the USB product ID of a joystick, if available.
233 *
234 * This can be called before any joysticks are opened. If the product ID isn't
235 * available this function returns 0.
236 *
237 * \param instance_id the joystick instance ID
238 * \returns the USB product ID of the selected joystick. If called on an
239 * invalid index, this function returns zero
240 *
241 * \since This function is available since SDL 3.0.0.
242 */
243extern DECLSPEC Uint16 SDLCALL SDL_GetJoystickInstanceProduct(SDL_JoystickID instance_id);
244
245/**
246 * Get the product version of a joystick, if available.
247 *
248 * This can be called before any joysticks are opened. If the product version
249 * isn't available this function returns 0.
250 *
251 * \param instance_id the joystick instance ID
252 * \returns the product version of the selected joystick. If called on an
253 * invalid index, this function returns zero
254 *
255 * \since This function is available since SDL 3.0.0.
256 */
258
259/**
260 * Get the type of a joystick, if available.
261 *
262 * This can be called before any joysticks are opened.
263 *
264 * \param instance_id the joystick instance ID
265 * \returns the SDL_JoystickType of the selected joystick. If called on an
266 * invalid index, this function returns `SDL_JOYSTICK_TYPE_UNKNOWN`
267 *
268 * \since This function is available since SDL 3.0.0.
269 */
271
272/**
273 * Open a joystick for use.
274 *
275 * The joystick subsystem must be initialized before a joystick can be opened
276 * for use.
277 *
278 * \param instance_id the joystick instance ID
279 * \returns a joystick identifier or NULL if an error occurred; call
280 * SDL_GetError() for more information.
281 *
282 * \since This function is available since SDL 3.0.0.
283 *
284 * \sa SDL_CloseJoystick
285 */
286extern DECLSPEC SDL_Joystick *SDLCALL SDL_OpenJoystick(SDL_JoystickID instance_id);
287
288/**
289 * Get the SDL_Joystick associated with an instance ID, if it has been opened.
290 *
291 * \param instance_id the instance ID to get the SDL_Joystick for
292 * \returns an SDL_Joystick on success or NULL on failure or if it hasn't been
293 * opened yet; call SDL_GetError() for more information.
294 *
295 * \since This function is available since SDL 3.0.0.
296 */
297extern DECLSPEC SDL_Joystick *SDLCALL SDL_GetJoystickFromInstanceID(SDL_JoystickID instance_id);
298
299/**
300 * Get the SDL_Joystick associated with a player index.
301 *
302 * \param player_index the player index to get the SDL_Joystick for
303 * \returns an SDL_Joystick on success or NULL on failure; call SDL_GetError()
304 * for more information.
305 *
306 * \since This function is available since SDL 3.0.0.
307 */
308extern DECLSPEC SDL_Joystick *SDLCALL SDL_GetJoystickFromPlayerIndex(int player_index);
309
310/**
311 * Attach a new virtual joystick.
312 *
313 * \param type type of joystick
314 * \param naxes number of axes
315 * \param nbuttons number of buttons
316 * \param nhats number of hats
317 * \returns the joystick instance ID, or 0 if an error occurred; call
318 * SDL_GetError() for more information.
319 *
320 * \since This function is available since SDL 3.0.0.
321 */
323 int naxes,
324 int nbuttons,
325 int nhats);
326
327/**
328 * The structure that defines an extended virtual joystick description
329 *
330 * The caller must zero the structure and then initialize the version with `SDL_VIRTUAL_JOYSTICK_DESC_VERSION` before passing it to SDL_AttachVirtualJoystickEx()
331 * All other elements of this structure are optional and can be left 0.
332 *
333 * \sa SDL_AttachVirtualJoystickEx
334 */
336{
337 Uint16 version; /**< `SDL_VIRTUAL_JOYSTICK_DESC_VERSION` */
338 Uint16 type; /**< `SDL_JoystickType` */
339 Uint16 naxes; /**< the number of axes on this joystick */
340 Uint16 nbuttons; /**< the number of buttons on this joystick */
341 Uint16 nhats; /**< the number of hats on this joystick */
342 Uint16 vendor_id; /**< the USB vendor ID of this joystick */
343 Uint16 product_id; /**< the USB product ID of this joystick */
344 Uint16 padding; /**< unused */
345 Uint32 button_mask; /**< A mask of which buttons are valid for this controller
346 e.g. (1 << SDL_GAMEPAD_BUTTON_SOUTH) */
347 Uint32 axis_mask; /**< A mask of which axes are valid for this controller
348 e.g. (1 << SDL_GAMEPAD_AXIS_LEFTX) */
349 const char *name; /**< the name of the joystick */
350
351 void *userdata; /**< User data pointer passed to callbacks */
352 void (SDLCALL *Update)(void *userdata); /**< Called when the joystick state should be updated */
353 void (SDLCALL *SetPlayerIndex)(void *userdata, int player_index); /**< Called when the player index is set */
354 int (SDLCALL *Rumble)(void *userdata, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble); /**< Implements SDL_RumbleJoystick() */
355 int (SDLCALL *RumbleTriggers)(void *userdata, Uint16 left_rumble, Uint16 right_rumble); /**< Implements SDL_RumbleJoystickTriggers() */
356 int (SDLCALL *SetLED)(void *userdata, Uint8 red, Uint8 green, Uint8 blue); /**< Implements SDL_SetJoystickLED() */
357 int (SDLCALL *SendEffect)(void *userdata, const void *data, int size); /**< Implements SDL_SendJoystickEffect() */
358
360
361/**
362 * The current version of the SDL_VirtualJoystickDesc structure
363 */
364#define SDL_VIRTUAL_JOYSTICK_DESC_VERSION 1
365
366/**
367 * Attach a new virtual joystick with extended properties.
368 *
369 * \param desc Joystick description
370 * \returns the joystick instance ID, or 0 if an error occurred; call
371 * SDL_GetError() for more information.
372 *
373 * \since This function is available since SDL 3.0.0.
374 */
376
377/**
378 * Detach a virtual joystick.
379 *
380 * \param instance_id the joystick instance ID, previously returned from
381 * SDL_AttachVirtualJoystick()
382 * \returns 0 on success or a negative error code on failure; call
383 * SDL_GetError() for more information.
384 *
385 * \since This function is available since SDL 3.0.0.
386 */
387extern DECLSPEC int SDLCALL SDL_DetachVirtualJoystick(SDL_JoystickID instance_id);
388
389/**
390 * Query whether or not a joystick is virtual.
391 *
392 * \param instance_id the joystick instance ID
393 * \returns SDL_TRUE if the joystick is virtual, SDL_FALSE otherwise.
394 *
395 * \since This function is available since SDL 3.0.0.
396 */
397extern DECLSPEC SDL_bool SDLCALL SDL_IsJoystickVirtual(SDL_JoystickID instance_id);
398
399/**
400 * Set values on an opened, virtual-joystick's axis.
401 *
402 * Please note that values set here will not be applied until the next call to
403 * SDL_UpdateJoysticks, which can either be called directly, or can be called
404 * indirectly through various other SDL APIs, including, but not limited to
405 * the following: SDL_PollEvent, SDL_PumpEvents, SDL_WaitEventTimeout,
406 * SDL_WaitEvent.
407 *
408 * Note that when sending trigger axes, you should scale the value to the full
409 * range of Sint16. For example, a trigger at rest would have the value of
410 * `SDL_JOYSTICK_AXIS_MIN`.
411 *
412 * \param joystick the virtual joystick on which to set state.
413 * \param axis the specific axis on the virtual joystick to set.
414 * \param value the new value for the specified axis.
415 * \returns 0 on success or a negative error code on failure; call
416 * SDL_GetError() for more information.
417 *
418 * \since This function is available since SDL 3.0.0.
419 */
420extern DECLSPEC int SDLCALL SDL_SetJoystickVirtualAxis(SDL_Joystick *joystick, int axis, Sint16 value);
421
422/**
423 * Set values on an opened, virtual-joystick's button.
424 *
425 * Please note that values set here will not be applied until the next call to
426 * SDL_UpdateJoysticks, which can either be called directly, or can be called
427 * indirectly through various other SDL APIs, including, but not limited to
428 * the following: SDL_PollEvent, SDL_PumpEvents, SDL_WaitEventTimeout,
429 * SDL_WaitEvent.
430 *
431 * \param joystick the virtual joystick on which to set state.
432 * \param button the specific button on the virtual joystick to set.
433 * \param value the new value for the specified button.
434 * \returns 0 on success or a negative error code on failure; call
435 * SDL_GetError() for more information.
436 *
437 * \since This function is available since SDL 3.0.0.
438 */
439extern DECLSPEC int SDLCALL SDL_SetJoystickVirtualButton(SDL_Joystick *joystick, int button, Uint8 value);
440
441/**
442 * Set values on an opened, virtual-joystick's hat.
443 *
444 * Please note that values set here will not be applied until the next call to
445 * SDL_UpdateJoysticks, which can either be called directly, or can be called
446 * indirectly through various other SDL APIs, including, but not limited to
447 * the following: SDL_PollEvent, SDL_PumpEvents, SDL_WaitEventTimeout,
448 * SDL_WaitEvent.
449 *
450 * \param joystick the virtual joystick on which to set state.
451 * \param hat the specific hat on the virtual joystick to set.
452 * \param value the new value for the specified hat.
453 * \returns 0 on success or a negative error code on failure; call
454 * SDL_GetError() for more information.
455 *
456 * \since This function is available since SDL 3.0.0.
457 */
458extern DECLSPEC int SDLCALL SDL_SetJoystickVirtualHat(SDL_Joystick *joystick, int hat, Uint8 value);
459
460/**
461 * Get the properties associated with a joystick.
462 *
463 * The following read-only properties are provided by SDL:
464 *
465 * - `SDL_PROP_JOYSTICK_CAP_MONO_LED_BOOLEAN`: true if this joystick has an
466 * LED that has adjustable brightness
467 * - `SDL_PROP_JOYSTICK_CAP_RGB_LED_BOOLEAN`: true if this joystick has an LED
468 * that has adjustable color
469 * - `SDL_PROP_JOYSTICK_CAP_PLAYER_LED_BOOLEAN`: true if this joystick has a
470 * player LED
471 * - `SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN`: true if this joystick has
472 * left/right rumble
473 * - `SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN`: true if this joystick has
474 * simple trigger rumble
475 *
476 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
477 * \returns a valid property ID on success or 0 on failure; call
478 * SDL_GetError() for more information.
479 *
480 * \since This function is available since SDL 3.0.0.
481 *
482 * \sa SDL_GetProperty
483 * \sa SDL_SetProperty
484 */
485extern DECLSPEC SDL_PropertiesID SDLCALL SDL_GetJoystickProperties(SDL_Joystick *joystick);
486
487#define SDL_PROP_JOYSTICK_CAP_MONO_LED_BOOLEAN "SDL.joystick.cap.mono_led"
488#define SDL_PROP_JOYSTICK_CAP_RGB_LED_BOOLEAN "SDL.joystick.cap.rgb_led"
489#define SDL_PROP_JOYSTICK_CAP_PLAYER_LED_BOOLEAN "SDL.joystick.cap.player_led"
490#define SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN "SDL.joystick.cap.rumble"
491#define SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN "SDL.joystick.cap.trigger_rumble"
492
493/**
494 * Get the implementation dependent name of a joystick.
495 *
496 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
497 * \returns the name of the selected joystick. If no name can be found, this
498 * function returns NULL; call SDL_GetError() for more information.
499 *
500 * \since This function is available since SDL 3.0.0.
501 *
502 * \sa SDL_GetJoystickInstanceName
503 * \sa SDL_OpenJoystick
504 */
505extern DECLSPEC const char *SDLCALL SDL_GetJoystickName(SDL_Joystick *joystick);
506
507/**
508 * Get the implementation dependent path of a joystick.
509 *
510 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
511 * \returns the path of the selected joystick. If no path can be found, this
512 * function returns NULL; call SDL_GetError() for more information.
513 *
514 * \since This function is available since SDL 3.0.0.
515 *
516 * \sa SDL_GetJoystickInstancePath
517 */
518extern DECLSPEC const char *SDLCALL SDL_GetJoystickPath(SDL_Joystick *joystick);
519
520/**
521 * Get the player index of an opened joystick.
522 *
523 * For XInput controllers this returns the XInput user index. Many joysticks
524 * will not be able to supply this information.
525 *
526 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
527 * \returns the player index, or -1 if it's not available.
528 *
529 * \since This function is available since SDL 3.0.0.
530 */
531extern DECLSPEC int SDLCALL SDL_GetJoystickPlayerIndex(SDL_Joystick *joystick);
532
533/**
534 * Set the player index of an opened joystick.
535 *
536 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
537 * \param player_index Player index to assign to this joystick, or -1 to clear
538 * the player index and turn off player LEDs.
539 * \returns 0 on success or a negative error code on failure; call
540 * SDL_GetError() for more information.
541 *
542 * \since This function is available since SDL 3.0.0.
543 */
544extern DECLSPEC int SDLCALL SDL_SetJoystickPlayerIndex(SDL_Joystick *joystick, int player_index);
545
546/**
547 * Get the implementation-dependent GUID for the joystick.
548 *
549 * This function requires an open joystick.
550 *
551 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
552 * \returns the GUID of the given joystick. If called on an invalid index,
553 * this function returns a zero GUID; call SDL_GetError() for more
554 * information.
555 *
556 * \since This function is available since SDL 3.0.0.
557 *
558 * \sa SDL_GetJoystickInstanceGUID
559 * \sa SDL_GetJoystickGUIDString
560 */
561extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_GetJoystickGUID(SDL_Joystick *joystick);
562
563/**
564 * Get the USB vendor ID of an opened joystick, if available.
565 *
566 * If the vendor ID isn't available this function returns 0.
567 *
568 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
569 * \returns the USB vendor ID of the selected joystick, or 0 if unavailable.
570 *
571 * \since This function is available since SDL 3.0.0.
572 */
573extern DECLSPEC Uint16 SDLCALL SDL_GetJoystickVendor(SDL_Joystick *joystick);
574
575/**
576 * Get the USB product ID of an opened joystick, if available.
577 *
578 * If the product ID isn't available this function returns 0.
579 *
580 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
581 * \returns the USB product ID of the selected joystick, or 0 if unavailable.
582 *
583 * \since This function is available since SDL 3.0.0.
584 */
585extern DECLSPEC Uint16 SDLCALL SDL_GetJoystickProduct(SDL_Joystick *joystick);
586
587/**
588 * Get the product version of an opened joystick, if available.
589 *
590 * If the product version isn't available this function returns 0.
591 *
592 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
593 * \returns the product version of the selected joystick, or 0 if unavailable.
594 *
595 * \since This function is available since SDL 3.0.0.
596 */
597extern DECLSPEC Uint16 SDLCALL SDL_GetJoystickProductVersion(SDL_Joystick *joystick);
598
599/**
600 * Get the firmware version of an opened joystick, if available.
601 *
602 * If the firmware version isn't available this function returns 0.
603 *
604 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
605 * \returns the firmware version of the selected joystick, or 0 if
606 * unavailable.
607 *
608 * \since This function is available since SDL 3.0.0.
609 */
610extern DECLSPEC Uint16 SDLCALL SDL_GetJoystickFirmwareVersion(SDL_Joystick *joystick);
611
612/**
613 * Get the serial number of an opened joystick, if available.
614 *
615 * Returns the serial number of the joystick, or NULL if it is not available.
616 *
617 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
618 * \returns the serial number of the selected joystick, or NULL if
619 * unavailable.
620 *
621 * \since This function is available since SDL 3.0.0.
622 */
623extern DECLSPEC const char * SDLCALL SDL_GetJoystickSerial(SDL_Joystick *joystick);
624
625/**
626 * Get the type of an opened joystick.
627 *
628 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
629 * \returns the SDL_JoystickType of the selected joystick.
630 *
631 * \since This function is available since SDL 3.0.0.
632 */
633extern DECLSPEC SDL_JoystickType SDLCALL SDL_GetJoystickType(SDL_Joystick *joystick);
634
635/**
636 * Get an ASCII string representation for a given SDL_JoystickGUID.
637 *
638 * You should supply at least 33 bytes for pszGUID.
639 *
640 * \param guid the SDL_JoystickGUID you wish to convert to string
641 * \param pszGUID buffer in which to write the ASCII string
642 * \param cbGUID the size of pszGUID
643 * \returns 0 on success or a negative error code on failure; call
644 * SDL_GetError() for more information.
645 *
646 * \since This function is available since SDL 3.0.0.
647 *
648 * \sa SDL_GetJoystickInstanceGUID
649 * \sa SDL_GetJoystickGUID
650 * \sa SDL_GetJoystickGUIDFromString
651 */
652extern DECLSPEC int SDLCALL SDL_GetJoystickGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID);
653
654/**
655 * Convert a GUID string into a SDL_JoystickGUID structure.
656 *
657 * Performs no error checking. If this function is given a string containing
658 * an invalid GUID, the function will silently succeed, but the GUID generated
659 * will not be useful.
660 *
661 * \param pchGUID string containing an ASCII representation of a GUID
662 * \returns a SDL_JoystickGUID structure.
663 *
664 * \since This function is available since SDL 3.0.0.
665 *
666 * \sa SDL_GetJoystickGUIDString
667 */
668extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_GetJoystickGUIDFromString(const char *pchGUID);
669
670/**
671 * Get the device information encoded in a SDL_JoystickGUID structure
672 *
673 * \param guid the SDL_JoystickGUID you wish to get info about
674 * \param vendor A pointer filled in with the device VID, or 0 if not
675 * available
676 * \param product A pointer filled in with the device PID, or 0 if not
677 * available
678 * \param version A pointer filled in with the device version, or 0 if not
679 * available
680 * \param crc16 A pointer filled in with a CRC used to distinguish different
681 * products with the same VID/PID, or 0 if not available
682 *
683 * \since This function is available since SDL 3.0.0.
684 *
685 * \sa SDL_GetJoystickInstanceGUID
686 */
687extern DECLSPEC void SDLCALL SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version, Uint16 *crc16);
688
689/**
690 * Get the status of a specified joystick.
691 *
692 * \param joystick the joystick to query
693 * \returns SDL_TRUE if the joystick has been opened, SDL_FALSE if it has not;
694 * call SDL_GetError() for more information.
695 *
696 * \since This function is available since SDL 3.0.0.
697 *
698 * \sa SDL_CloseJoystick
699 * \sa SDL_OpenJoystick
700 */
701extern DECLSPEC SDL_bool SDLCALL SDL_JoystickConnected(SDL_Joystick *joystick);
702
703/**
704 * Get the instance ID of an opened joystick.
705 *
706 * \param joystick an SDL_Joystick structure containing joystick information
707 * \returns the instance ID of the specified joystick on success or 0 on
708 * failure; call SDL_GetError() for more information.
709 *
710 * \since This function is available since SDL 3.0.0.
711 *
712 * \sa SDL_OpenJoystick
713 */
714extern DECLSPEC SDL_JoystickID SDLCALL SDL_GetJoystickInstanceID(SDL_Joystick *joystick);
715
716/**
717 * Get the number of general axis controls on a joystick.
718 *
719 * Often, the directional pad on a game controller will either look like 4
720 * separate buttons or a POV hat, and not axes, but all of this is up to the
721 * device and platform.
722 *
723 * \param joystick an SDL_Joystick structure containing joystick information
724 * \returns the number of axis controls/number of axes on success or a
725 * negative error code on failure; call SDL_GetError() for more
726 * information.
727 *
728 * \since This function is available since SDL 3.0.0.
729 *
730 * \sa SDL_GetJoystickAxis
731 * \sa SDL_OpenJoystick
732 */
733extern DECLSPEC int SDLCALL SDL_GetNumJoystickAxes(SDL_Joystick *joystick);
734
735/**
736 * Get the number of POV hats on a joystick.
737 *
738 * \param joystick an SDL_Joystick structure containing joystick information
739 * \returns the number of POV hats on success or a negative error code on
740 * failure; call SDL_GetError() for more information.
741 *
742 * \since This function is available since SDL 3.0.0.
743 *
744 * \sa SDL_GetJoystickHat
745 * \sa SDL_OpenJoystick
746 */
747extern DECLSPEC int SDLCALL SDL_GetNumJoystickHats(SDL_Joystick *joystick);
748
749/**
750 * Get the number of buttons on a joystick.
751 *
752 * \param joystick an SDL_Joystick structure containing joystick information
753 * \returns the number of buttons on success or a negative error code on
754 * failure; call SDL_GetError() for more information.
755 *
756 * \since This function is available since SDL 3.0.0.
757 *
758 * \sa SDL_GetJoystickButton
759 * \sa SDL_OpenJoystick
760 */
761extern DECLSPEC int SDLCALL SDL_GetNumJoystickButtons(SDL_Joystick *joystick);
762
763/**
764 * Set the state of joystick event processing.
765 *
766 * If joystick events are disabled, you must call SDL_UpdateJoysticks()
767 * yourself and check the state of the joystick when you want joystick
768 * information.
769 *
770 * \param enabled whether to process joystick events or not
771 *
772 * \since This function is available since SDL 3.0.0.
773 *
774 * \sa SDL_JoystickEventsEnabled
775 */
776extern DECLSPEC void SDLCALL SDL_SetJoystickEventsEnabled(SDL_bool enabled);
777
778/**
779 * Query the state of joystick event processing.
780 *
781 * If joystick events are disabled, you must call SDL_UpdateJoysticks()
782 * yourself and check the state of the joystick when you want joystick
783 * information.
784 *
785 * \returns SDL_TRUE if joystick events are being processed, SDL_FALSE
786 * otherwise.
787 *
788 * \since This function is available since SDL 3.0.0.
789 *
790 * \sa SDL_SetJoystickEventsEnabled
791 */
792extern DECLSPEC SDL_bool SDLCALL SDL_JoystickEventsEnabled(void);
793
794/**
795 * Update the current state of the open joysticks.
796 *
797 * This is called automatically by the event loop if any joystick events are
798 * enabled.
799 *
800 * \since This function is available since SDL 3.0.0.
801 */
802extern DECLSPEC void SDLCALL SDL_UpdateJoysticks(void);
803
804/**
805 * Get the current state of an axis control on a joystick.
806 *
807 * SDL makes no promises about what part of the joystick any given axis refers
808 * to. Your game should have some sort of configuration UI to let users
809 * specify what each axis should be bound to. Alternately, SDL's higher-level
810 * Game Controller API makes a great effort to apply order to this lower-level
811 * interface, so you know that a specific axis is the "left thumb stick," etc.
812 *
813 * The value returned by SDL_GetJoystickAxis() is a signed integer (-32768 to
814 * 32767) representing the current position of the axis. It may be necessary
815 * to impose certain tolerances on these values to account for jitter.
816 *
817 * \param joystick an SDL_Joystick structure containing joystick information
818 * \param axis the axis to query; the axis indices start at index 0
819 * \returns a 16-bit signed integer representing the current position of the
820 * axis or 0 on failure; call SDL_GetError() for more information.
821 *
822 * \since This function is available since SDL 3.0.0.
823 *
824 * \sa SDL_GetNumJoystickAxes
825 */
826extern DECLSPEC Sint16 SDLCALL SDL_GetJoystickAxis(SDL_Joystick *joystick,
827 int axis);
828
829/**
830 * Get the initial state of an axis control on a joystick.
831 *
832 * The state is a value ranging from -32768 to 32767.
833 *
834 * The axis indices start at index 0.
835 *
836 * \param joystick an SDL_Joystick structure containing joystick information
837 * \param axis the axis to query; the axis indices start at index 0
838 * \param state Upon return, the initial value is supplied here.
839 * \returns SDL_TRUE if this axis has any initial value, or SDL_FALSE if not.
840 *
841 * \since This function is available since SDL 3.0.0.
842 */
843extern DECLSPEC SDL_bool SDLCALL SDL_GetJoystickAxisInitialState(SDL_Joystick *joystick,
844 int axis, Sint16 *state);
845
846/**
847 * \name Hat positions
848 */
849/* @{ */
850#define SDL_HAT_CENTERED 0x00
851#define SDL_HAT_UP 0x01
852#define SDL_HAT_RIGHT 0x02
853#define SDL_HAT_DOWN 0x04
854#define SDL_HAT_LEFT 0x08
855#define SDL_HAT_RIGHTUP (SDL_HAT_RIGHT|SDL_HAT_UP)
856#define SDL_HAT_RIGHTDOWN (SDL_HAT_RIGHT|SDL_HAT_DOWN)
857#define SDL_HAT_LEFTUP (SDL_HAT_LEFT|SDL_HAT_UP)
858#define SDL_HAT_LEFTDOWN (SDL_HAT_LEFT|SDL_HAT_DOWN)
859/* @} */
860
861/**
862 * Get the current state of a POV hat on a joystick.
863 *
864 * The returned value will be one of the following positions:
865 *
866 * - `SDL_HAT_CENTERED`
867 * - `SDL_HAT_UP`
868 * - `SDL_HAT_RIGHT`
869 * - `SDL_HAT_DOWN`
870 * - `SDL_HAT_LEFT`
871 * - `SDL_HAT_RIGHTUP`
872 * - `SDL_HAT_RIGHTDOWN`
873 * - `SDL_HAT_LEFTUP`
874 * - `SDL_HAT_LEFTDOWN`
875 *
876 * \param joystick an SDL_Joystick structure containing joystick information
877 * \param hat the hat index to get the state from; indices start at index 0
878 * \returns the current hat position.
879 *
880 * \since This function is available since SDL 3.0.0.
881 *
882 * \sa SDL_GetNumJoystickHats
883 */
884extern DECLSPEC Uint8 SDLCALL SDL_GetJoystickHat(SDL_Joystick *joystick,
885 int hat);
886
887/**
888 * Get the current state of a button on a joystick.
889 *
890 * \param joystick an SDL_Joystick structure containing joystick information
891 * \param button the button index to get the state from; indices start at
892 * index 0
893 * \returns 1 if the specified button is pressed, 0 otherwise.
894 *
895 * \since This function is available since SDL 3.0.0.
896 *
897 * \sa SDL_GetNumJoystickButtons
898 */
899extern DECLSPEC Uint8 SDLCALL SDL_GetJoystickButton(SDL_Joystick *joystick,
900 int button);
901
902/**
903 * Start a rumble effect.
904 *
905 * Each call to this function cancels any previous rumble effect, and calling
906 * it with 0 intensity stops any rumbling.
907 *
908 * \param joystick The joystick to vibrate
909 * \param low_frequency_rumble The intensity of the low frequency (left)
910 * rumble motor, from 0 to 0xFFFF
911 * \param high_frequency_rumble The intensity of the high frequency (right)
912 * rumble motor, from 0 to 0xFFFF
913 * \param duration_ms The duration of the rumble effect, in milliseconds
914 * \returns 0, or -1 if rumble isn't supported on this joystick
915 *
916 * \since This function is available since SDL 3.0.0.
917 */
918extern DECLSPEC int SDLCALL SDL_RumbleJoystick(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms);
919
920/**
921 * Start a rumble effect in the joystick's triggers
922 *
923 * Each call to this function cancels any previous trigger rumble effect, and
924 * calling it with 0 intensity stops any rumbling.
925 *
926 * Note that this is rumbling of the _triggers_ and not the game controller as
927 * a whole. This is currently only supported on Xbox One controllers. If you
928 * want the (more common) whole-controller rumble, use SDL_RumbleJoystick()
929 * instead.
930 *
931 * \param joystick The joystick to vibrate
932 * \param left_rumble The intensity of the left trigger rumble motor, from 0
933 * to 0xFFFF
934 * \param right_rumble The intensity of the right trigger rumble motor, from 0
935 * to 0xFFFF
936 * \param duration_ms The duration of the rumble effect, in milliseconds
937 * \returns 0 on success or a negative error code on failure; call
938 * SDL_GetError() for more information.
939 *
940 * \since This function is available since SDL 3.0.0.
941 */
942extern DECLSPEC int SDLCALL SDL_RumbleJoystickTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms);
943
944/**
945 * Update a joystick's LED color.
946 *
947 * An example of a joystick LED is the light on the back of a PlayStation 4's
948 * DualShock 4 controller.
949 *
950 * For joysticks with a single color LED, the maximum of the RGB values will
951 * be used as the LED brightness.
952 *
953 * \param joystick The joystick to update
954 * \param red The intensity of the red LED
955 * \param green The intensity of the green LED
956 * \param blue The intensity of the blue LED
957 * \returns 0 on success or a negative error code on failure; call
958 * SDL_GetError() for more information.
959 *
960 * \since This function is available since SDL 3.0.0.
961 */
962extern DECLSPEC int SDLCALL SDL_SetJoystickLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue);
963
964/**
965 * Send a joystick specific effect packet
966 *
967 * \param joystick The joystick to affect
968 * \param data The data to send to the joystick
969 * \param size The size of the data to send to the joystick
970 * \returns 0 on success or a negative error code on failure; call
971 * SDL_GetError() for more information.
972 *
973 * \since This function is available since SDL 3.0.0.
974 */
975extern DECLSPEC int SDLCALL SDL_SendJoystickEffect(SDL_Joystick *joystick, const void *data, int size);
976
977/**
978 * Close a joystick previously opened with SDL_OpenJoystick().
979 *
980 * \param joystick The joystick device to close
981 *
982 * \since This function is available since SDL 3.0.0.
983 *
984 * \sa SDL_OpenJoystick
985 */
986extern DECLSPEC void SDLCALL SDL_CloseJoystick(SDL_Joystick *joystick);
987
988/**
989 * Get the battery level of a joystick as SDL_JoystickPowerLevel.
990 *
991 * \param joystick the SDL_Joystick to query
992 * \returns the current battery level as SDL_JoystickPowerLevel on success or
993 * `SDL_JOYSTICK_POWER_UNKNOWN` if it is unknown
994 *
995 * \since This function is available since SDL 3.0.0.
996 */
998
999/* Ends C function definitions when using C++ */
1000#ifdef __cplusplus
1001}
1002#endif
1003#include <SDL3/SDL_close_code.h>
1004
1005#endif /* SDL_joystick_h_ */
int SDL_SetJoystickVirtualAxis(SDL_Joystick *joystick, int axis, Sint16 value)
SDL_JoystickType
@ SDL_JOYSTICK_TYPE_DANCE_PAD
@ SDL_JOYSTICK_TYPE_GAMEPAD
@ SDL_JOYSTICK_TYPE_ARCADE_PAD
@ SDL_JOYSTICK_TYPE_UNKNOWN
@ SDL_JOYSTICK_TYPE_ARCADE_STICK
@ SDL_JOYSTICK_TYPE_WHEEL
@ SDL_JOYSTICK_TYPE_THROTTLE
@ SDL_JOYSTICK_TYPE_GUITAR
@ SDL_JOYSTICK_TYPE_FLIGHT_STICK
@ SDL_JOYSTICK_TYPE_DRUM_KIT
void SDL_UnlockJoysticks(void) SDL_RELEASE(SDL_joystick_lock)
SDL_Joystick * SDL_OpenJoystick(SDL_JoystickID instance_id)
void SDL_UpdateJoysticks(void)
int SDL_GetJoystickInstancePlayerIndex(SDL_JoystickID instance_id)
Uint32 SDL_JoystickID
Uint8 SDL_GetJoystickButton(SDL_Joystick *joystick, int button)
Uint16 SDL_GetJoystickInstanceProduct(SDL_JoystickID instance_id)
const char * SDL_GetJoystickInstanceName(SDL_JoystickID instance_id)
Uint16 SDL_GetJoystickVendor(SDL_Joystick *joystick)
SDL_JoystickPowerLevel
@ SDL_JOYSTICK_POWER_MAX
@ SDL_JOYSTICK_POWER_FULL
@ SDL_JOYSTICK_POWER_MEDIUM
@ SDL_JOYSTICK_POWER_EMPTY
@ SDL_JOYSTICK_POWER_UNKNOWN
@ SDL_JOYSTICK_POWER_WIRED
@ SDL_JOYSTICK_POWER_LOW
const char * SDL_GetJoystickInstancePath(SDL_JoystickID instance_id)
SDL_JoystickGUID SDL_GetJoystickGUID(SDL_Joystick *joystick)
SDL_JoystickID * SDL_GetJoysticks(int *count)
SDL_JoystickID SDL_AttachVirtualJoystickEx(const SDL_VirtualJoystickDesc *desc)
int SDL_SetJoystickVirtualButton(SDL_Joystick *joystick, int button, Uint8 value)
SDL_JoystickPowerLevel SDL_GetJoystickPowerLevel(SDL_Joystick *joystick)
Uint16 SDL_GetJoystickProductVersion(SDL_Joystick *joystick)
SDL_Joystick * SDL_GetJoystickFromInstanceID(SDL_JoystickID instance_id)
const char * SDL_GetJoystickPath(SDL_Joystick *joystick)
int SDL_DetachVirtualJoystick(SDL_JoystickID instance_id)
SDL_bool SDL_IsJoystickVirtual(SDL_JoystickID instance_id)
int SDL_SetJoystickLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
void SDL_SetJoystickEventsEnabled(SDL_bool enabled)
SDL_JoystickType SDL_GetJoystickInstanceType(SDL_JoystickID instance_id)
int SDL_SetJoystickVirtualHat(SDL_Joystick *joystick, int hat, Uint8 value)
struct SDL_Joystick SDL_Joystick
int SDL_RumbleJoystick(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
int SDL_GetNumJoystickHats(SDL_Joystick *joystick)
SDL_PropertiesID SDL_GetJoystickProperties(SDL_Joystick *joystick)
Uint16 SDL_GetJoystickProduct(SDL_Joystick *joystick)
SDL_JoystickType SDL_GetJoystickType(SDL_Joystick *joystick)
SDL_bool SDL_JoystickEventsEnabled(void)
SDL_JoystickGUID SDL_GetJoystickGUIDFromString(const char *pchGUID)
SDL_GUID SDL_JoystickGUID
Uint16 SDL_GetJoystickInstanceVendor(SDL_JoystickID instance_id)
int SDL_RumbleJoystickTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms)
int SDL_GetNumJoystickButtons(SDL_Joystick *joystick)
int SDL_SendJoystickEffect(SDL_Joystick *joystick, const void *data, int size)
void SDL_CloseJoystick(SDL_Joystick *joystick)
int SDL_GetNumJoystickAxes(SDL_Joystick *joystick)
SDL_JoystickGUID SDL_GetJoystickInstanceGUID(SDL_JoystickID instance_id)
void SDL_LockJoysticks(void) SDL_ACQUIRE(SDL_joystick_lock)
SDL_JoystickID SDL_AttachVirtualJoystick(SDL_JoystickType type, int naxes, int nbuttons, int nhats)
void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version, Uint16 *crc16)
int SDL_SetJoystickPlayerIndex(SDL_Joystick *joystick, int player_index)
Uint16 SDL_GetJoystickInstanceProductVersion(SDL_JoystickID instance_id)
const char * SDL_GetJoystickSerial(SDL_Joystick *joystick)
SDL_bool SDL_GetJoystickAxisInitialState(SDL_Joystick *joystick, int axis, Sint16 *state)
Uint8 SDL_GetJoystickHat(SDL_Joystick *joystick, int hat)
SDL_bool SDL_JoystickConnected(SDL_Joystick *joystick)
Uint16 SDL_GetJoystickFirmwareVersion(SDL_Joystick *joystick)
Sint16 SDL_GetJoystickAxis(SDL_Joystick *joystick, int axis)
int SDL_GetJoystickPlayerIndex(SDL_Joystick *joystick)
SDL_JoystickID SDL_GetJoystickInstanceID(SDL_Joystick *joystick)
const char * SDL_GetJoystickName(SDL_Joystick *joystick)
SDL_Joystick * SDL_GetJoystickFromPlayerIndex(int player_index)
int SDL_GetJoystickGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID)
#define SDL_ACQUIRE(x)
Definition SDL_mutex.h:73
struct SDL_Mutex SDL_Mutex
Definition SDL_mutex.h:132
#define SDL_RELEASE(x)
Definition SDL_mutex.h:79
Uint32 SDL_PropertiesID
uint8_t Uint8
Definition SDL_stdinc.h:150
uint16_t Uint16
Definition SDL_stdinc.h:162
SDL_MALLOC size_t size
Definition SDL_stdinc.h:400
int SDL_bool
Definition SDL_stdinc.h:137
int16_t Sint16
Definition SDL_stdinc.h:156
uint32_t Uint32
Definition SDL_stdinc.h:174
void(* SetPlayerIndex)(void *userdata, int player_index)
int(* SetLED)(void *userdata, Uint8 red, Uint8 green, Uint8 blue)
void(* Update)(void *userdata)
int(* SendEffect)(void *userdata, const void *data, int size)
int(* RumbleTriggers)(void *userdata, Uint16 left_rumble, Uint16 right_rumble)
int(* Rumble)(void *userdata, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)