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 * 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 * \returns the joystick instance ID, or 0 if an error occurred; call
313 * SDL_GetError() for more information.
314 *
315 * \since This function is available since SDL 3.0.0.
316 */
318 int naxes,
319 int nbuttons,
320 int nhats);
321
322/**
323 * The structure that defines an extended virtual joystick description
324 *
325 * The caller must zero the structure and then initialize the version with `SDL_VIRTUAL_JOYSTICK_DESC_VERSION` before passing it to SDL_AttachVirtualJoystickEx()
326 * All other elements of this structure are optional and can be left 0.
327 *
328 * \sa SDL_AttachVirtualJoystickEx
329 */
331{
332 Uint16 version; /**< `SDL_VIRTUAL_JOYSTICK_DESC_VERSION` */
333 Uint16 type; /**< `SDL_JoystickType` */
334 Uint16 naxes; /**< the number of axes on this joystick */
335 Uint16 nbuttons; /**< the number of buttons on this joystick */
336 Uint16 nhats; /**< the number of hats on this joystick */
337 Uint16 vendor_id; /**< the USB vendor ID of this joystick */
338 Uint16 product_id; /**< the USB product ID of this joystick */
339 Uint16 padding; /**< unused */
340 Uint32 button_mask; /**< A mask of which buttons are valid for this controller
341 e.g. (1 << SDL_GAMEPAD_BUTTON_A) */
342 Uint32 axis_mask; /**< A mask of which axes are valid for this controller
343 e.g. (1 << SDL_GAMEPAD_AXIS_LEFTX) */
344 const char *name; /**< the name of the joystick */
345
346 void *userdata; /**< User data pointer passed to callbacks */
347 void (SDLCALL *Update)(void *userdata); /**< Called when the joystick state should be updated */
348 void (SDLCALL *SetPlayerIndex)(void *userdata, int player_index); /**< Called when the player index is set */
349 int (SDLCALL *Rumble)(void *userdata, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble); /**< Implements SDL_RumbleJoystick() */
350 int (SDLCALL *RumbleTriggers)(void *userdata, Uint16 left_rumble, Uint16 right_rumble); /**< Implements SDL_RumbleJoystickTriggers() */
351 int (SDLCALL *SetLED)(void *userdata, Uint8 red, Uint8 green, Uint8 blue); /**< Implements SDL_SetJoystickLED() */
352 int (SDLCALL *SendEffect)(void *userdata, const void *data, int size); /**< Implements SDL_SendJoystickEffect() */
353
355
356/**
357 * \brief The current version of the SDL_VirtualJoystickDesc structure
358 */
359#define SDL_VIRTUAL_JOYSTICK_DESC_VERSION 1
360
361/**
362 * Attach a new virtual joystick with extended properties.
363 *
364 * \returns the joystick instance ID, or 0 if an error occurred; call
365 * SDL_GetError() for more information.
366 *
367 * \since This function is available since SDL 3.0.0.
368 */
370
371/**
372 * Detach a virtual joystick.
373 *
374 * \param instance_id the joystick instance ID, previously returned from
375 * SDL_AttachVirtualJoystick()
376 * \returns 0 on success, or -1 if an error occurred.
377 *
378 * \since This function is available since SDL 3.0.0.
379 */
380extern DECLSPEC int SDLCALL SDL_DetachVirtualJoystick(SDL_JoystickID instance_id);
381
382/**
383 * Query whether or not a joystick is virtual.
384 *
385 * \param instance_id the joystick instance ID
386 * \returns SDL_TRUE if the joystick is virtual, SDL_FALSE otherwise.
387 *
388 * \since This function is available since SDL 3.0.0.
389 */
390extern DECLSPEC SDL_bool SDLCALL SDL_IsJoystickVirtual(SDL_JoystickID instance_id);
391
392/**
393 * Set values on an opened, virtual-joystick's axis.
394 *
395 * Please note that values set here will not be applied until the next call to
396 * SDL_UpdateJoysticks, which can either be called directly, or can be called
397 * indirectly through various other SDL APIs, including, but not limited to
398 * the following: SDL_PollEvent, SDL_PumpEvents, SDL_WaitEventTimeout,
399 * SDL_WaitEvent.
400 *
401 * Note that when sending trigger axes, you should scale the value to the full
402 * range of Sint16. For example, a trigger at rest would have the value of
403 * `SDL_JOYSTICK_AXIS_MIN`.
404 *
405 * \param joystick the virtual joystick on which to set state.
406 * \param axis the specific axis on the virtual joystick to set.
407 * \param value the new value for the specified axis.
408 * \returns 0 on success, -1 on error.
409 *
410 * \since This function is available since SDL 3.0.0.
411 */
412extern DECLSPEC int SDLCALL SDL_SetJoystickVirtualAxis(SDL_Joystick *joystick, int axis, Sint16 value);
413
414/**
415 * Set values on an opened, virtual-joystick's button.
416 *
417 * Please note that values set here will not be applied until the next call to
418 * SDL_UpdateJoysticks, which can either be called directly, or can be called
419 * indirectly through various other SDL APIs, including, but not limited to
420 * the following: SDL_PollEvent, SDL_PumpEvents, SDL_WaitEventTimeout,
421 * SDL_WaitEvent.
422 *
423 * \param joystick the virtual joystick on which to set state.
424 * \param button the specific button on the virtual joystick to set.
425 * \param value the new value for the specified button.
426 * \returns 0 on success, -1 on error.
427 *
428 * \since This function is available since SDL 3.0.0.
429 */
430extern DECLSPEC int SDLCALL SDL_SetJoystickVirtualButton(SDL_Joystick *joystick, int button, Uint8 value);
431
432/**
433 * Set values on an opened, virtual-joystick's hat.
434 *
435 * Please note that values set here will not be applied until the next call to
436 * SDL_UpdateJoysticks, which can either be called directly, or can be called
437 * indirectly through various other SDL APIs, including, but not limited to
438 * the following: SDL_PollEvent, SDL_PumpEvents, SDL_WaitEventTimeout,
439 * SDL_WaitEvent.
440 *
441 * \param joystick the virtual joystick on which to set state.
442 * \param hat the specific hat on the virtual joystick to set.
443 * \param value the new value for the specified hat.
444 * \returns 0 on success, -1 on error.
445 *
446 * \since This function is available since SDL 3.0.0.
447 */
448extern DECLSPEC int SDLCALL SDL_SetJoystickVirtualHat(SDL_Joystick *joystick, int hat, Uint8 value);
449
450/**
451 * Get the implementation dependent name of a joystick.
452 *
453 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
454 * \returns the name of the selected joystick. If no name can be found, this
455 * function returns NULL; call SDL_GetError() for more information.
456 *
457 * \since This function is available since SDL 3.0.0.
458 *
459 * \sa SDL_GetJoystickInstanceName
460 * \sa SDL_OpenJoystick
461 */
462extern DECLSPEC const char *SDLCALL SDL_GetJoystickName(SDL_Joystick *joystick);
463
464/**
465 * Get the implementation dependent path of a joystick.
466 *
467 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
468 * \returns the path of the selected joystick. If no path can be found, this
469 * function returns NULL; call SDL_GetError() for more information.
470 *
471 * \since This function is available since SDL 3.0.0.
472 *
473 * \sa SDL_GetJoystickInstancePath
474 */
475extern DECLSPEC const char *SDLCALL SDL_GetJoystickPath(SDL_Joystick *joystick);
476
477/**
478 * Get the player index of an opened joystick.
479 *
480 * For XInput controllers this returns the XInput user index. Many joysticks
481 * will not be able to supply this information.
482 *
483 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
484 * \returns the player index, or -1 if it's not available.
485 *
486 * \since This function is available since SDL 3.0.0.
487 */
488extern DECLSPEC int SDLCALL SDL_GetJoystickPlayerIndex(SDL_Joystick *joystick);
489
490/**
491 * Set the player index of an opened joystick.
492 *
493 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
494 * \param player_index Player index to assign to this joystick, or -1 to clear
495 * the player index and turn off player LEDs.
496 * \returns 0 on success or a negative error code on failure; call
497 * SDL_GetError() for more information.
498 *
499 * \since This function is available since SDL 3.0.0.
500 */
501extern DECLSPEC int SDLCALL SDL_SetJoystickPlayerIndex(SDL_Joystick *joystick, int player_index);
502
503/**
504 * Get the implementation-dependent GUID for the joystick.
505 *
506 * This function requires an open joystick.
507 *
508 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
509 * \returns the GUID of the given joystick. If called on an invalid index,
510 * this function returns a zero GUID; call SDL_GetError() for more
511 * information.
512 *
513 * \since This function is available since SDL 3.0.0.
514 *
515 * \sa SDL_GetJoystickInstanceGUID
516 * \sa SDL_GetJoystickGUIDString
517 */
518extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_GetJoystickGUID(SDL_Joystick *joystick);
519
520/**
521 * Get the USB vendor ID of an opened joystick, if available.
522 *
523 * If the vendor ID isn't available this function returns 0.
524 *
525 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
526 * \returns the USB vendor ID of the selected joystick, or 0 if unavailable.
527 *
528 * \since This function is available since SDL 3.0.0.
529 */
530extern DECLSPEC Uint16 SDLCALL SDL_GetJoystickVendor(SDL_Joystick *joystick);
531
532/**
533 * Get the USB product ID of an opened joystick, if available.
534 *
535 * If the product ID isn't available this function returns 0.
536 *
537 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
538 * \returns the USB product ID of the selected joystick, or 0 if unavailable.
539 *
540 * \since This function is available since SDL 3.0.0.
541 */
542extern DECLSPEC Uint16 SDLCALL SDL_GetJoystickProduct(SDL_Joystick *joystick);
543
544/**
545 * Get the product version of an opened joystick, if available.
546 *
547 * If the product version isn't available this function returns 0.
548 *
549 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
550 * \returns the product version 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_GetJoystickProductVersion(SDL_Joystick *joystick);
555
556/**
557 * Get the firmware version of an opened joystick, if available.
558 *
559 * If the firmware version isn't available this function returns 0.
560 *
561 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
562 * \returns the firmware version of the selected joystick, or 0 if
563 * unavailable.
564 *
565 * \since This function is available since SDL 3.0.0.
566 */
567extern DECLSPEC Uint16 SDLCALL SDL_GetJoystickFirmwareVersion(SDL_Joystick *joystick);
568
569/**
570 * Get the serial number of an opened joystick, if available.
571 *
572 * Returns the serial number of the joystick, or NULL if it is not available.
573 *
574 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
575 * \returns the serial number of the selected joystick, or NULL if
576 * unavailable.
577 *
578 * \since This function is available since SDL 3.0.0.
579 */
580extern DECLSPEC const char * SDLCALL SDL_GetJoystickSerial(SDL_Joystick *joystick);
581
582/**
583 * Get the type of an opened joystick.
584 *
585 * \param joystick the SDL_Joystick obtained from SDL_OpenJoystick()
586 * \returns the SDL_JoystickType of the selected joystick.
587 *
588 * \since This function is available since SDL 3.0.0.
589 */
590extern DECLSPEC SDL_JoystickType SDLCALL SDL_GetJoystickType(SDL_Joystick *joystick);
591
592/**
593 * Get an ASCII string representation for a given SDL_JoystickGUID.
594 *
595 * You should supply at least 33 bytes for pszGUID.
596 *
597 * \param guid the SDL_JoystickGUID you wish to convert to string
598 * \param pszGUID buffer in which to write the ASCII string
599 * \param cbGUID the size of pszGUID
600 *
601 * \since This function is available since SDL 3.0.0.
602 *
603 * \sa SDL_GetJoystickInstanceGUID
604 * \sa SDL_GetJoystickGUID
605 * \sa SDL_GetJoystickGUIDFromString
606 */
607extern DECLSPEC void SDLCALL SDL_GetJoystickGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID);
608
609/**
610 * Convert a GUID string into a SDL_JoystickGUID structure.
611 *
612 * Performs no error checking. If this function is given a string containing
613 * an invalid GUID, the function will silently succeed, but the GUID generated
614 * will not be useful.
615 *
616 * \param pchGUID string containing an ASCII representation of a GUID
617 * \returns a SDL_JoystickGUID structure.
618 *
619 * \since This function is available since SDL 3.0.0.
620 *
621 * \sa SDL_GetJoystickGUIDString
622 */
623extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_GetJoystickGUIDFromString(const char *pchGUID);
624
625/**
626 * Get the device information encoded in a SDL_JoystickGUID structure
627 *
628 * \param guid the SDL_JoystickGUID you wish to get info about
629 * \param vendor A pointer filled in with the device VID, or 0 if not
630 * available
631 * \param product A pointer filled in with the device PID, or 0 if not
632 * available
633 * \param version A pointer filled in with the device version, or 0 if not
634 * available
635 * \param crc16 A pointer filled in with a CRC used to distinguish different
636 * products with the same VID/PID, or 0 if not available
637 *
638 * \since This function is available since SDL 3.0.0.
639 *
640 * \sa SDL_GetJoystickInstanceGUID
641 */
642extern DECLSPEC void SDLCALL SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version, Uint16 *crc16);
643
644/**
645 * Get the status of a specified joystick.
646 *
647 * \param joystick the joystick to query
648 * \returns SDL_TRUE if the joystick has been opened, SDL_FALSE if it has not;
649 * call SDL_GetError() for more information.
650 *
651 * \since This function is available since SDL 3.0.0.
652 *
653 * \sa SDL_CloseJoystick
654 * \sa SDL_OpenJoystick
655 */
656extern DECLSPEC SDL_bool SDLCALL SDL_JoystickConnected(SDL_Joystick *joystick);
657
658/**
659 * Get the instance ID of an opened joystick.
660 *
661 * \param joystick an SDL_Joystick structure containing joystick information
662 * \returns the instance ID of the specified joystick on success or 0 on
663 * failure; call SDL_GetError() for more information.
664 *
665 * \since This function is available since SDL 3.0.0.
666 *
667 * \sa SDL_OpenJoystick
668 */
669extern DECLSPEC SDL_JoystickID SDLCALL SDL_GetJoystickInstanceID(SDL_Joystick *joystick);
670
671/**
672 * Get the number of general axis controls on a joystick.
673 *
674 * Often, the directional pad on a game controller will either look like 4
675 * separate buttons or a POV hat, and not axes, but all of this is up to the
676 * device and platform.
677 *
678 * \param joystick an SDL_Joystick structure containing joystick information
679 * \returns the number of axis controls/number of axes on success or a
680 * negative error code on failure; call SDL_GetError() for more
681 * information.
682 *
683 * \since This function is available since SDL 3.0.0.
684 *
685 * \sa SDL_GetJoystickAxis
686 * \sa SDL_OpenJoystick
687 */
688extern DECLSPEC int SDLCALL SDL_GetNumJoystickAxes(SDL_Joystick *joystick);
689
690/**
691 * Get the number of POV hats on a joystick.
692 *
693 * \param joystick an SDL_Joystick structure containing joystick information
694 * \returns the number of POV hats on success or a negative error code on
695 * failure; call SDL_GetError() for more information.
696 *
697 * \since This function is available since SDL 3.0.0.
698 *
699 * \sa SDL_GetJoystickHat
700 * \sa SDL_OpenJoystick
701 */
702extern DECLSPEC int SDLCALL SDL_GetNumJoystickHats(SDL_Joystick *joystick);
703
704/**
705 * Get the number of buttons on a joystick.
706 *
707 * \param joystick an SDL_Joystick structure containing joystick information
708 * \returns the number of buttons on success or a negative error code on
709 * failure; call SDL_GetError() for more information.
710 *
711 * \since This function is available since SDL 3.0.0.
712 *
713 * \sa SDL_GetJoystickButton
714 * \sa SDL_OpenJoystick
715 */
716extern DECLSPEC int SDLCALL SDL_GetNumJoystickButtons(SDL_Joystick *joystick);
717
718/**
719 * Set the state of joystick event processing.
720 *
721 * If joystick events are disabled, you must call SDL_UpdateJoysticks()
722 * yourself and check the state of the joystick when you want joystick
723 * information.
724 *
725 * \param enabled whether to process joystick events or not
726 *
727 * \since This function is available since SDL 3.0.0.
728 *
729 * \sa SDL_JoystickEventsEnabled
730 */
731extern DECLSPEC void SDLCALL SDL_SetJoystickEventsEnabled(SDL_bool enabled);
732
733/**
734 * Query the state of joystick event processing.
735 *
736 * If joystick events are disabled, you must call SDL_UpdateJoysticks()
737 * yourself and check the state of the joystick when you want joystick
738 * information.
739 *
740 * \returns SDL_TRUE if joystick events are being processed, SDL_FALSE
741 * otherwise.
742 *
743 * \since This function is available since SDL 3.0.0.
744 *
745 * \sa SDL_SetJoystickEventsEnabled
746 */
747extern DECLSPEC SDL_bool SDLCALL SDL_JoystickEventsEnabled(void);
748
749/**
750 * Update the current state of the open joysticks.
751 *
752 * This is called automatically by the event loop if any joystick events are
753 * enabled.
754 *
755 * \since This function is available since SDL 3.0.0.
756 */
757extern DECLSPEC void SDLCALL SDL_UpdateJoysticks(void);
758
759/**
760 * Get the current state of an axis control on a joystick.
761 *
762 * SDL makes no promises about what part of the joystick any given axis refers
763 * to. Your game should have some sort of configuration UI to let users
764 * specify what each axis should be bound to. Alternately, SDL's higher-level
765 * Game Controller API makes a great effort to apply order to this lower-level
766 * interface, so you know that a specific axis is the "left thumb stick," etc.
767 *
768 * The value returned by SDL_GetJoystickAxis() is a signed integer (-32768 to
769 * 32767) representing the current position of the axis. It may be necessary
770 * to impose certain tolerances on these values to account for jitter.
771 *
772 * \param joystick an SDL_Joystick structure containing joystick information
773 * \param axis the axis to query; the axis indices start at index 0
774 * \returns a 16-bit signed integer representing the current position of the
775 * axis or 0 on failure; call SDL_GetError() for more information.
776 *
777 * \since This function is available since SDL 3.0.0.
778 *
779 * \sa SDL_GetNumJoystickAxes
780 */
781extern DECLSPEC Sint16 SDLCALL SDL_GetJoystickAxis(SDL_Joystick *joystick,
782 int axis);
783
784/**
785 * Get the initial state of an axis control on a joystick.
786 *
787 * The state is a value ranging from -32768 to 32767.
788 *
789 * The axis indices start at index 0.
790 *
791 * \param joystick an SDL_Joystick structure containing joystick information
792 * \param axis the axis to query; the axis indices start at index 0
793 * \param state Upon return, the initial value is supplied here.
794 * \return SDL_TRUE if this axis has any initial value, or SDL_FALSE if not.
795 *
796 * \since This function is available since SDL 3.0.0.
797 */
798extern DECLSPEC SDL_bool SDLCALL SDL_GetJoystickAxisInitialState(SDL_Joystick *joystick,
799 int axis, Sint16 *state);
800
801/**
802 * \name Hat positions
803 */
804/* @{ */
805#define SDL_HAT_CENTERED 0x00
806#define SDL_HAT_UP 0x01
807#define SDL_HAT_RIGHT 0x02
808#define SDL_HAT_DOWN 0x04
809#define SDL_HAT_LEFT 0x08
810#define SDL_HAT_RIGHTUP (SDL_HAT_RIGHT|SDL_HAT_UP)
811#define SDL_HAT_RIGHTDOWN (SDL_HAT_RIGHT|SDL_HAT_DOWN)
812#define SDL_HAT_LEFTUP (SDL_HAT_LEFT|SDL_HAT_UP)
813#define SDL_HAT_LEFTDOWN (SDL_HAT_LEFT|SDL_HAT_DOWN)
814/* @} */
815
816/**
817 * Get the current state of a POV hat on a joystick.
818 *
819 * The returned value will be one of the following positions:
820 *
821 * - `SDL_HAT_CENTERED`
822 * - `SDL_HAT_UP`
823 * - `SDL_HAT_RIGHT`
824 * - `SDL_HAT_DOWN`
825 * - `SDL_HAT_LEFT`
826 * - `SDL_HAT_RIGHTUP`
827 * - `SDL_HAT_RIGHTDOWN`
828 * - `SDL_HAT_LEFTUP`
829 * - `SDL_HAT_LEFTDOWN`
830 *
831 * \param joystick an SDL_Joystick structure containing joystick information
832 * \param hat the hat index to get the state from; indices start at index 0
833 * \returns the current hat position.
834 *
835 * \since This function is available since SDL 3.0.0.
836 *
837 * \sa SDL_GetNumJoystickHats
838 */
839extern DECLSPEC Uint8 SDLCALL SDL_GetJoystickHat(SDL_Joystick *joystick,
840 int hat);
841
842/**
843 * Get the current state of a button on a joystick.
844 *
845 * \param joystick an SDL_Joystick structure containing joystick information
846 * \param button the button index to get the state from; indices start at
847 * index 0
848 * \returns 1 if the specified button is pressed, 0 otherwise.
849 *
850 * \since This function is available since SDL 3.0.0.
851 *
852 * \sa SDL_GetNumJoystickButtons
853 */
854extern DECLSPEC Uint8 SDLCALL SDL_GetJoystickButton(SDL_Joystick *joystick,
855 int button);
856
857/**
858 * Start a rumble effect.
859 *
860 * Each call to this function cancels any previous rumble effect, and calling
861 * it with 0 intensity stops any rumbling.
862 *
863 * \param joystick The joystick to vibrate
864 * \param low_frequency_rumble The intensity of the low frequency (left)
865 * rumble motor, from 0 to 0xFFFF
866 * \param high_frequency_rumble The intensity of the high frequency (right)
867 * rumble motor, from 0 to 0xFFFF
868 * \param duration_ms The duration of the rumble effect, in milliseconds
869 * \returns 0, or -1 if rumble isn't supported on this joystick
870 *
871 * \since This function is available since SDL 3.0.0.
872 *
873 * \sa SDL_JoystickHasRumble
874 */
875extern DECLSPEC int SDLCALL SDL_RumbleJoystick(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms);
876
877/**
878 * Start a rumble effect in the joystick's triggers
879 *
880 * Each call to this function cancels any previous trigger rumble effect, and
881 * calling it with 0 intensity stops any rumbling.
882 *
883 * Note that this is rumbling of the _triggers_ and not the game controller as
884 * a whole. This is currently only supported on Xbox One controllers. If you
885 * want the (more common) whole-controller rumble, use SDL_RumbleJoystick()
886 * instead.
887 *
888 * \param joystick The joystick to vibrate
889 * \param left_rumble The intensity of the left trigger rumble motor, from 0
890 * to 0xFFFF
891 * \param right_rumble The intensity of the right trigger rumble motor, from 0
892 * to 0xFFFF
893 * \param duration_ms The duration of the rumble effect, in milliseconds
894 * \returns 0, or -1 if trigger rumble isn't supported on this joystick
895 *
896 * \since This function is available since SDL 3.0.0.
897 *
898 * \sa SDL_JoystickHasRumbleTriggers
899 */
900extern DECLSPEC int SDLCALL SDL_RumbleJoystickTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms);
901
902/**
903 * Query whether a joystick has an LED.
904 *
905 * An example of a joystick LED is the light on the back of a PlayStation 4's
906 * DualShock 4 controller.
907 *
908 * \param joystick The joystick to query
909 * \return SDL_TRUE if the joystick has a modifiable LED, SDL_FALSE otherwise.
910 *
911 * \since This function is available since SDL 3.0.0.
912 */
913extern DECLSPEC SDL_bool SDLCALL SDL_JoystickHasLED(SDL_Joystick *joystick);
914
915/**
916 * Query whether a joystick has rumble support.
917 *
918 * \param joystick The joystick to query
919 * \return SDL_TRUE if the joystick has rumble, SDL_FALSE otherwise.
920 *
921 * \since This function is available since SDL 3.0.0.
922 *
923 * \sa SDL_RumbleJoystick
924 */
925extern DECLSPEC SDL_bool SDLCALL SDL_JoystickHasRumble(SDL_Joystick *joystick);
926
927/**
928 * Query whether a joystick has rumble support on triggers.
929 *
930 * \param joystick The joystick to query
931 * \return SDL_TRUE if the joystick has trigger rumble, SDL_FALSE otherwise.
932 *
933 * \since This function is available since SDL 3.0.0.
934 *
935 * \sa SDL_RumbleJoystickTriggers
936 */
937extern DECLSPEC SDL_bool SDLCALL SDL_JoystickHasRumbleTriggers(SDL_Joystick *joystick);
938
939/**
940 * Update a joystick's LED color.
941 *
942 * An example of a joystick LED is the light on the back of a PlayStation 4's
943 * DualShock 4 controller.
944 *
945 * \param joystick The joystick to update
946 * \param red The intensity of the red LED
947 * \param green The intensity of the green LED
948 * \param blue The intensity of the blue LED
949 * \returns 0 on success, -1 if this joystick does not have a modifiable LED
950 *
951 * \since This function is available since SDL 3.0.0.
952 */
953extern DECLSPEC int SDLCALL SDL_SetJoystickLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue);
954
955/**
956 * Send a joystick specific effect packet
957 *
958 * \param joystick The joystick to affect
959 * \param data The data to send to the joystick
960 * \param size The size of the data to send to the joystick
961 * \returns 0, or -1 if this joystick or driver doesn't support effect packets
962 *
963 * \since This function is available since SDL 3.0.0.
964 */
965extern DECLSPEC int SDLCALL SDL_SendJoystickEffect(SDL_Joystick *joystick, const void *data, int size);
966
967/**
968 * Close a joystick previously opened with SDL_OpenJoystick().
969 *
970 * \param joystick The joystick device to close
971 * \returns 0 on success or a negative error code on failure; call
972 * SDL_GetError() for more information.
973 *
974 * \since This function is available since SDL 3.0.0.
975 *
976 * \sa SDL_OpenJoystick
977 */
978extern DECLSPEC int SDLCALL SDL_CloseJoystick(SDL_Joystick *joystick);
979
980/**
981 * Get the battery level of a joystick as SDL_JoystickPowerLevel.
982 *
983 * \param joystick the SDL_Joystick to query
984 * \returns the current battery level as SDL_JoystickPowerLevel on success or
985 * `SDL_JOYSTICK_POWER_UNKNOWN` if it is unknown
986 *
987 * \since This function is available since SDL 3.0.0.
988 */
990
991/* Ends C function definitions when using C++ */
992#ifdef __cplusplus
993}
994#endif
995#include <SDL3/SDL_close_code.h>
996
997#endif /* SDL_joystick_h_ */
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_CloseJoystick(SDL_Joystick *joystick)
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)
int SDL_GetNumJoystickAxes(SDL_Joystick *joystick)
SDL_JoystickGUID SDL_GetJoystickInstanceGUID(SDL_JoystickID instance_id)
void SDL_LockJoysticks(void) SDL_ACQUIRE(SDL_joystick_lock)
void SDL_GetJoystickGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID)
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)
#define SDL_ACQUIRE(...)
Definition: SDL_mutex.h:69
#define SDL_RELEASE(...)
Definition: SDL_mutex.h:75
struct SDL_mutex SDL_mutex
Definition: SDL_mutex.h:133
typedef void(APIENTRYP PFNGLDRAWRANGEELEMENTSPROC)(GLenum mode
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1564
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1967
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1564
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLbyte GLbyte blue
GLsizeiptr size
GLbyte green
GLsizei const GLfloat * value
uint8_t Uint8
Definition: SDL_stdinc.h:147
uint16_t Uint16
Definition: SDL_stdinc.h:159
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:348
int(* SetLED)(void *userdata, Uint8 red, Uint8 green, Uint8 blue)
Definition: SDL_joystick.h:351
void(* Update)(void *userdata)
Definition: SDL_joystick.h:347
int(* SendEffect)(void *userdata, const void *data, int size)
Definition: SDL_joystick.h:352
int(* RumbleTriggers)(void *userdata, Uint16 left_rumble, Uint16 right_rumble)
Definition: SDL_joystick.h:350
int(* Rumble)(void *userdata, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
Definition: SDL_joystick.h:349