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