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