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