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 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
464 * \returns a valid property ID on success or 0 on failure; call
465 * SDL_GetError() for more information.
466 *
467 * \since This function is available since SDL 3.0.0.
468 *
469 * \sa SDL_GetProperty
470 * \sa SDL_SetProperty
471 */
472extern DECLSPEC SDL_PropertiesID SDLCALL SDL_GetJoystickProperties(SDL_Joystick *joystick);
473
474/**
475 * Get the implementation dependent name of a joystick.
476 *
477 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
478 * \returns the name of the selected joystick. If no name can be found, this
479 * function returns NULL; call SDL_GetError() for more information.
480 *
481 * \since This function is available since SDL 3.0.0.
482 *
483 * \sa SDL_GetJoystickInstanceName
484 * \sa SDL_OpenJoystick
485 */
486extern DECLSPEC const char *SDLCALL SDL_GetJoystickName(SDL_Joystick *joystick);
487
488/**
489 * Get the implementation dependent path of a joystick.
490 *
491 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
492 * \returns the path of the selected joystick. If no path can be found, this
493 * function returns NULL; call SDL_GetError() for more information.
494 *
495 * \since This function is available since SDL 3.0.0.
496 *
497 * \sa SDL_GetJoystickInstancePath
498 */
499extern DECLSPEC const char *SDLCALL SDL_GetJoystickPath(SDL_Joystick *joystick);
500
501/**
502 * Get the player index of an opened joystick.
503 *
504 * For XInput controllers this returns the XInput user index. Many joysticks
505 * will not be able to supply this information.
506 *
507 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
508 * \returns the player index, or -1 if it's not available.
509 *
510 * \since This function is available since SDL 3.0.0.
511 */
512extern DECLSPEC int SDLCALL SDL_GetJoystickPlayerIndex(SDL_Joystick *joystick);
513
514/**
515 * Set the player index of an opened joystick.
516 *
517 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
518 * \param player_index Player index to assign to this joystick, or -1 to clear
519 * the player index and turn off player LEDs.
520 * \returns 0 on success or a negative error code on failure; call
521 * SDL_GetError() for more information.
522 *
523 * \since This function is available since SDL 3.0.0.
524 */
525extern DECLSPEC int SDLCALL SDL_SetJoystickPlayerIndex(SDL_Joystick *joystick, int player_index);
526
527/**
528 * Get the implementation-dependent GUID for the joystick.
529 *
530 * This function requires an open joystick.
531 *
532 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
533 * \returns the GUID of the given joystick. If called on an invalid index,
534 * this function returns a zero GUID; call SDL_GetError() for more
535 * information.
536 *
537 * \since This function is available since SDL 3.0.0.
538 *
539 * \sa SDL_GetJoystickInstanceGUID
540 * \sa SDL_GetJoystickGUIDString
541 */
542extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_GetJoystickGUID(SDL_Joystick *joystick);
543
544/**
545 * Get the USB vendor ID of an opened joystick, if available.
546 *
547 * If the vendor ID isn't available this function returns 0.
548 *
549 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
550 * \returns the USB vendor ID of the selected joystick, or 0 if unavailable.
551 *
552 * \since This function is available since SDL 3.0.0.
553 */
554extern DECLSPEC Uint16 SDLCALL SDL_GetJoystickVendor(SDL_Joystick *joystick);
555
556/**
557 * Get the USB product ID of an opened joystick, if available.
558 *
559 * If the product ID isn't available this function returns 0.
560 *
561 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
562 * \returns the USB product ID of the selected joystick, or 0 if unavailable.
563 *
564 * \since This function is available since SDL 3.0.0.
565 */
566extern DECLSPEC Uint16 SDLCALL SDL_GetJoystickProduct(SDL_Joystick *joystick);
567
568/**
569 * Get the product version of an opened joystick, if available.
570 *
571 * If the product version isn't available this function returns 0.
572 *
573 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
574 * \returns the product version of the selected joystick, or 0 if unavailable.
575 *
576 * \since This function is available since SDL 3.0.0.
577 */
578extern DECLSPEC Uint16 SDLCALL SDL_GetJoystickProductVersion(SDL_Joystick *joystick);
579
580/**
581 * Get the firmware version of an opened joystick, if available.
582 *
583 * If the firmware version isn't available this function returns 0.
584 *
585 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
586 * \returns the firmware version of the selected joystick, or 0 if
587 * unavailable.
588 *
589 * \since This function is available since SDL 3.0.0.
590 */
591extern DECLSPEC Uint16 SDLCALL SDL_GetJoystickFirmwareVersion(SDL_Joystick *joystick);
592
593/**
594 * Get the serial number of an opened joystick, if available.
595 *
596 * Returns the serial number of the joystick, or NULL if it is not available.
597 *
598 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
599 * \returns the serial number of the selected joystick, or NULL if
600 * unavailable.
601 *
602 * \since This function is available since SDL 3.0.0.
603 */
604extern DECLSPEC const char * SDLCALL SDL_GetJoystickSerial(SDL_Joystick *joystick);
605
606/**
607 * Get the type of an opened joystick.
608 *
609 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
610 * \returns the SDL_JoystickType of the selected joystick.
611 *
612 * \since This function is available since SDL 3.0.0.
613 */
614extern DECLSPEC SDL_JoystickType SDLCALL SDL_GetJoystickType(SDL_Joystick *joystick);
615
616/**
617 * Get an ASCII string representation for a given SDL_JoystickGUID.
618 *
619 * You should supply at least 33 bytes for pszGUID.
620 *
621 * \param guid the SDL_JoystickGUID you wish to convert to string
622 * \param pszGUID buffer in which to write the ASCII string
623 * \param cbGUID the size of pszGUID
624 * \returns 0 on success or a negative error code on failure; call
625 * SDL_GetError() for more information.
626 *
627 * \since This function is available since SDL 3.0.0.
628 *
629 * \sa SDL_GetJoystickInstanceGUID
630 * \sa SDL_GetJoystickGUID
631 * \sa SDL_GetJoystickGUIDFromString
632 */
633extern DECLSPEC int SDLCALL SDL_GetJoystickGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID);
634
635/**
636 * Convert a GUID string into a SDL_JoystickGUID structure.
637 *
638 * Performs no error checking. If this function is given a string containing
639 * an invalid GUID, the function will silently succeed, but the GUID generated
640 * will not be useful.
641 *
642 * \param pchGUID string containing an ASCII representation of a GUID
643 * \returns a SDL_JoystickGUID structure.
644 *
645 * \since This function is available since SDL 3.0.0.
646 *
647 * \sa SDL_GetJoystickGUIDString
648 */
649extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_GetJoystickGUIDFromString(const char *pchGUID);
650
651/**
652 * Get the device information encoded in a SDL_JoystickGUID structure
653 *
654 * \param guid the SDL_JoystickGUID you wish to get info about
655 * \param vendor A pointer filled in with the device VID, or 0 if not
656 * available
657 * \param product A pointer filled in with the device PID, or 0 if not
658 * available
659 * \param version A pointer filled in with the device version, or 0 if not
660 * available
661 * \param crc16 A pointer filled in with a CRC used to distinguish different
662 * products with the same VID/PID, or 0 if not available
663 *
664 * \since This function is available since SDL 3.0.0.
665 *
666 * \sa SDL_GetJoystickInstanceGUID
667 */
668extern DECLSPEC void SDLCALL SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version, Uint16 *crc16);
669
670/**
671 * Get the status of a specified joystick.
672 *
673 * \param joystick the joystick to query
674 * \returns SDL_TRUE if the joystick has been opened, SDL_FALSE if it has not;
675 * call SDL_GetError() for more information.
676 *
677 * \since This function is available since SDL 3.0.0.
678 *
679 * \sa SDL_CloseJoystick
680 * \sa SDL_OpenJoystick
681 */
682extern DECLSPEC SDL_bool SDLCALL SDL_JoystickConnected(SDL_Joystick *joystick);
683
684/**
685 * Get the instance ID of an opened joystick.
686 *
687 * \param joystick an SDL_Joystick structure containing joystick information
688 * \returns the instance ID of the specified joystick on success or 0 on
689 * failure; call SDL_GetError() for more information.
690 *
691 * \since This function is available since SDL 3.0.0.
692 *
693 * \sa SDL_OpenJoystick
694 */
695extern DECLSPEC SDL_JoystickID SDLCALL SDL_GetJoystickInstanceID(SDL_Joystick *joystick);
696
697/**
698 * Get the number of general axis controls on a joystick.
699 *
700 * Often, the directional pad on a game controller will either look like 4
701 * separate buttons or a POV hat, and not axes, but all of this is up to the
702 * device and platform.
703 *
704 * \param joystick an SDL_Joystick structure containing joystick information
705 * \returns the number of axis controls/number of axes on success or a
706 * negative error code on failure; call SDL_GetError() for more
707 * information.
708 *
709 * \since This function is available since SDL 3.0.0.
710 *
711 * \sa SDL_GetJoystickAxis
712 * \sa SDL_OpenJoystick
713 */
714extern DECLSPEC int SDLCALL SDL_GetNumJoystickAxes(SDL_Joystick *joystick);
715
716/**
717 * Get the number of POV hats on a joystick.
718 *
719 * \param joystick an SDL_Joystick structure containing joystick information
720 * \returns the number of POV hats on success or a negative error code on
721 * failure; call SDL_GetError() for more information.
722 *
723 * \since This function is available since SDL 3.0.0.
724 *
725 * \sa SDL_GetJoystickHat
726 * \sa SDL_OpenJoystick
727 */
728extern DECLSPEC int SDLCALL SDL_GetNumJoystickHats(SDL_Joystick *joystick);
729
730/**
731 * Get the number of buttons on a joystick.
732 *
733 * \param joystick an SDL_Joystick structure containing joystick information
734 * \returns the number of buttons on success or a negative error code on
735 * failure; call SDL_GetError() for more information.
736 *
737 * \since This function is available since SDL 3.0.0.
738 *
739 * \sa SDL_GetJoystickButton
740 * \sa SDL_OpenJoystick
741 */
742extern DECLSPEC int SDLCALL SDL_GetNumJoystickButtons(SDL_Joystick *joystick);
743
744/**
745 * Set the state of joystick event processing.
746 *
747 * If joystick events are disabled, you must call SDL_UpdateJoysticks()
748 * yourself and check the state of the joystick when you want joystick
749 * information.
750 *
751 * \param enabled whether to process joystick events or not
752 *
753 * \since This function is available since SDL 3.0.0.
754 *
755 * \sa SDL_JoystickEventsEnabled
756 */
757extern DECLSPEC void SDLCALL SDL_SetJoystickEventsEnabled(SDL_bool enabled);
758
759/**
760 * Query the state of joystick event processing.
761 *
762 * If joystick events are disabled, you must call SDL_UpdateJoysticks()
763 * yourself and check the state of the joystick when you want joystick
764 * information.
765 *
766 * \returns SDL_TRUE if joystick events are being processed, SDL_FALSE
767 * otherwise.
768 *
769 * \since This function is available since SDL 3.0.0.
770 *
771 * \sa SDL_SetJoystickEventsEnabled
772 */
773extern DECLSPEC SDL_bool SDLCALL SDL_JoystickEventsEnabled(void);
774
775/**
776 * Update the current state of the open joysticks.
777 *
778 * This is called automatically by the event loop if any joystick events are
779 * enabled.
780 *
781 * \since This function is available since SDL 3.0.0.
782 */
783extern DECLSPEC void SDLCALL SDL_UpdateJoysticks(void);
784
785/**
786 * Get the current state of an axis control on a joystick.
787 *
788 * SDL makes no promises about what part of the joystick any given axis refers
789 * to. Your game should have some sort of configuration UI to let users
790 * specify what each axis should be bound to. Alternately, SDL's higher-level
791 * Game Controller API makes a great effort to apply order to this lower-level
792 * interface, so you know that a specific axis is the "left thumb stick," etc.
793 *
794 * The value returned by SDL_GetJoystickAxis() is a signed integer (-32768 to
795 * 32767) representing the current position of the axis. It may be necessary
796 * to impose certain tolerances on these values to account for jitter.
797 *
798 * \param joystick an SDL_Joystick structure containing joystick information
799 * \param axis the axis to query; the axis indices start at index 0
800 * \returns a 16-bit signed integer representing the current position of the
801 * axis or 0 on failure; call SDL_GetError() for more information.
802 *
803 * \since This function is available since SDL 3.0.0.
804 *
805 * \sa SDL_GetNumJoystickAxes
806 */
807extern DECLSPEC Sint16 SDLCALL SDL_GetJoystickAxis(SDL_Joystick *joystick,
808 int axis);
809
810/**
811 * Get the initial state of an axis control on a joystick.
812 *
813 * The state is a value ranging from -32768 to 32767.
814 *
815 * The axis indices start at index 0.
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 * \param state Upon return, the initial value is supplied here.
820 * \returns SDL_TRUE if this axis has any initial value, or SDL_FALSE if not.
821 *
822 * \since This function is available since SDL 3.0.0.
823 */
824extern DECLSPEC SDL_bool SDLCALL SDL_GetJoystickAxisInitialState(SDL_Joystick *joystick,
825 int axis, Sint16 *state);
826
827/**
828 * \name Hat positions
829 */
830/* @{ */
831#define SDL_HAT_CENTERED 0x00
832#define SDL_HAT_UP 0x01
833#define SDL_HAT_RIGHT 0x02
834#define SDL_HAT_DOWN 0x04
835#define SDL_HAT_LEFT 0x08
836#define SDL_HAT_RIGHTUP (SDL_HAT_RIGHT|SDL_HAT_UP)
837#define SDL_HAT_RIGHTDOWN (SDL_HAT_RIGHT|SDL_HAT_DOWN)
838#define SDL_HAT_LEFTUP (SDL_HAT_LEFT|SDL_HAT_UP)
839#define SDL_HAT_LEFTDOWN (SDL_HAT_LEFT|SDL_HAT_DOWN)
840/* @} */
841
842/**
843 * Get the current state of a POV hat on a joystick.
844 *
845 * The returned value will be one of the following positions:
846 *
847 * - `SDL_HAT_CENTERED`
848 * - `SDL_HAT_UP`
849 * - `SDL_HAT_RIGHT`
850 * - `SDL_HAT_DOWN`
851 * - `SDL_HAT_LEFT`
852 * - `SDL_HAT_RIGHTUP`
853 * - `SDL_HAT_RIGHTDOWN`
854 * - `SDL_HAT_LEFTUP`
855 * - `SDL_HAT_LEFTDOWN`
856 *
857 * \param joystick an SDL_Joystick structure containing joystick information
858 * \param hat the hat index to get the state from; indices start at index 0
859 * \returns the current hat position.
860 *
861 * \since This function is available since SDL 3.0.0.
862 *
863 * \sa SDL_GetNumJoystickHats
864 */
865extern DECLSPEC Uint8 SDLCALL SDL_GetJoystickHat(SDL_Joystick *joystick,
866 int hat);
867
868/**
869 * Get the current state of a button on a joystick.
870 *
871 * \param joystick an SDL_Joystick structure containing joystick information
872 * \param button the button index to get the state from; indices start at
873 * index 0
874 * \returns 1 if the specified button is pressed, 0 otherwise.
875 *
876 * \since This function is available since SDL 3.0.0.
877 *
878 * \sa SDL_GetNumJoystickButtons
879 */
880extern DECLSPEC Uint8 SDLCALL SDL_GetJoystickButton(SDL_Joystick *joystick,
881 int button);
882
883/**
884 * Start a rumble effect.
885 *
886 * Each call to this function cancels any previous rumble effect, and calling
887 * it with 0 intensity stops any rumbling.
888 *
889 * \param joystick The joystick to vibrate
890 * \param low_frequency_rumble The intensity of the low frequency (left)
891 * rumble motor, from 0 to 0xFFFF
892 * \param high_frequency_rumble The intensity of the high frequency (right)
893 * rumble motor, from 0 to 0xFFFF
894 * \param duration_ms The duration of the rumble effect, in milliseconds
895 * \returns 0, or -1 if rumble isn't supported on this joystick
896 *
897 * \since This function is available since SDL 3.0.0.
898 *
899 * \sa SDL_JoystickHasRumble
900 */
901extern DECLSPEC int SDLCALL SDL_RumbleJoystick(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms);
902
903/**
904 * Start a rumble effect in the joystick's triggers
905 *
906 * Each call to this function cancels any previous trigger rumble effect, and
907 * calling it with 0 intensity stops any rumbling.
908 *
909 * Note that this is rumbling of the _triggers_ and not the game controller as
910 * a whole. This is currently only supported on Xbox One controllers. If you
911 * want the (more common) whole-controller rumble, use SDL_RumbleJoystick()
912 * instead.
913 *
914 * \param joystick The joystick to vibrate
915 * \param left_rumble The intensity of the left trigger rumble motor, from 0
916 * to 0xFFFF
917 * \param right_rumble The intensity of the right trigger rumble motor, from 0
918 * to 0xFFFF
919 * \param duration_ms The duration of the rumble effect, in milliseconds
920 * \returns 0 on success or a negative error code on failure; call
921 * SDL_GetError() for more information.
922 *
923 * \since This function is available since SDL 3.0.0.
924 *
925 * \sa SDL_JoystickHasRumbleTriggers
926 */
927extern DECLSPEC int SDLCALL SDL_RumbleJoystickTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms);
928
929/**
930 * Query whether a joystick has an LED.
931 *
932 * An example of a joystick LED is the light on the back of a PlayStation 4's
933 * DualShock 4 controller.
934 *
935 * \param joystick The joystick to query
936 * \returns SDL_TRUE if the joystick has a modifiable LED, SDL_FALSE
937 * otherwise.
938 *
939 * \since This function is available since SDL 3.0.0.
940 */
941extern DECLSPEC SDL_bool SDLCALL SDL_JoystickHasLED(SDL_Joystick *joystick);
942
943/**
944 * Query whether a joystick has rumble support.
945 *
946 * \param joystick The joystick to query
947 * \returns SDL_TRUE if the joystick has rumble, SDL_FALSE otherwise.
948 *
949 * \since This function is available since SDL 3.0.0.
950 *
951 * \sa SDL_RumbleJoystick
952 */
953extern DECLSPEC SDL_bool SDLCALL SDL_JoystickHasRumble(SDL_Joystick *joystick);
954
955/**
956 * Query whether a joystick has rumble support on triggers.
957 *
958 * \param joystick The joystick to query
959 * \returns SDL_TRUE if the joystick has trigger rumble, SDL_FALSE otherwise.
960 *
961 * \since This function is available since SDL 3.0.0.
962 *
963 * \sa SDL_RumbleJoystickTriggers
964 */
965extern DECLSPEC SDL_bool SDLCALL SDL_JoystickHasRumbleTriggers(SDL_Joystick *joystick);
966
967/**
968 * Update a joystick's LED color.
969 *
970 * An example of a joystick LED is the light on the back of a PlayStation 4's
971 * DualShock 4 controller.
972 *
973 * \param joystick The joystick to update
974 * \param red The intensity of the red LED
975 * \param green The intensity of the green LED
976 * \param blue The intensity of the blue LED
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_SetJoystickLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue);
983
984/**
985 * Send a joystick specific effect packet
986 *
987 * \param joystick The joystick to affect
988 * \param data The data to send to the joystick
989 * \param size The size of the data to send to the joystick
990 * \returns 0 on success or a negative error code on failure; call
991 * SDL_GetError() for more information.
992 *
993 * \since This function is available since SDL 3.0.0.
994 */
995extern DECLSPEC int SDLCALL SDL_SendJoystickEffect(SDL_Joystick *joystick, const void *data, int size);
996
997/**
998 * Close a joystick previously opened with SDL_OpenJoystick().
999 *
1000 * \param joystick The joystick device to close
1001 *
1002 * \since This function is available since SDL 3.0.0.
1003 *
1004 * \sa SDL_OpenJoystick
1005 */
1006extern DECLSPEC void SDLCALL SDL_CloseJoystick(SDL_Joystick *joystick);
1007
1008/**
1009 * Get the battery level of a joystick as SDL_JoystickPowerLevel.
1010 *
1011 * \param joystick the SDL_Joystick to query
1012 * \returns the current battery level as SDL_JoystickPowerLevel on success or
1013 * `SDL_JOYSTICK_POWER_UNKNOWN` if it is unknown
1014 *
1015 * \since This function is available since SDL 3.0.0.
1016 */
1018
1019/* Ends C function definitions when using C++ */
1020#ifdef __cplusplus
1021}
1022#endif
1023#include <SDL3/SDL_close_code.h>
1024
1025#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_bool SDL_JoystickHasLED(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)
SDL_bool SDL_JoystickHasRumbleTriggers(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
SDL_bool SDL_JoystickHasRumble(SDL_Joystick *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)