SDL  2.0
SDL_gamecontroller.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_gamecontroller.h
24  *
25  * Include file for SDL game controller event handling
26  */
27 
28 #ifndef SDL_gamecontroller_h_
29 #define SDL_gamecontroller_h_
30 
31 #include "SDL_stdinc.h"
32 #include "SDL_error.h"
33 #include "SDL_rwops.h"
34 #include "SDL_sensor.h"
35 #include "SDL_joystick.h"
36 
37 #include "begin_code.h"
38 /* Set up for C function definitions, even when using C++ */
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * \file SDL_gamecontroller.h
45  *
46  * In order to use these functions, SDL_Init() must have been called
47  * with the ::SDL_INIT_GAMECONTROLLER flag. This causes SDL to scan the system
48  * for game controllers, and load appropriate drivers.
49  *
50  * If you would like to receive controller updates while the application
51  * is in the background, you should set the following hint before calling
52  * SDL_Init(): SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
53  */
54 
55 /**
56  * The gamecontroller structure used to identify an SDL game controller
57  */
58 struct _SDL_GameController;
59 typedef struct _SDL_GameController SDL_GameController;
60 
61 typedef enum
62 {
74 
75 typedef enum
76 {
82 
83 /**
84  * Get the SDL joystick layer binding for this controller button/axis mapping
85  */
87 {
89  union
90  {
91  int button;
92  int axis;
93  struct {
94  int hat;
95  int hat_mask;
96  } hat;
97  } value;
98 
100 
101 
102 /**
103  * To count the number of game controllers in the system for the following:
104  *
105  * ```c
106  * int nJoysticks = SDL_NumJoysticks();
107  * int nGameControllers = 0;
108  * for (int i = 0; i < nJoysticks; i++) {
109  * if (SDL_IsGameController(i)) {
110  * nGameControllers++;
111  * }
112  * }
113  * ```
114  *
115  * Using the SDL_HINT_GAMECONTROLLERCONFIG hint or the SDL_GameControllerAddMapping() you can add support for controllers SDL is unaware of or cause an existing controller to have a different binding. The format is:
116  * guid,name,mappings
117  *
118  * Where GUID is the string value from SDL_JoystickGetGUIDString(), name is the human readable string for the device and mappings are controller mappings to joystick ones.
119  * Under Windows there is a reserved GUID of "xinput" that covers any XInput devices.
120  * The mapping format for joystick is:
121  * bX - a joystick button, index X
122  * hX.Y - hat X with value Y
123  * aX - axis X of the joystick
124  * Buttons can be used as a controller axis and vice versa.
125  *
126  * This string shows an example of a valid mapping for a controller
127  *
128  * ```c
129  * "03000000341a00003608000000000000,PS3 Controller,a:b1,b:b2,y:b3,x:b0,start:b9,guide:b12,back:b8,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftshoulder:b4,rightshoulder:b5,leftstick:b10,rightstick:b11,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7",
130  * ```
131  */
132 
133 /**
134  * Load a set of Game Controller mappings from a seekable SDL data stream.
135  *
136  * You can call this function several times, if needed, to load different
137  * database files.
138  *
139  * If a new mapping is loaded for an already known controller GUID, the later
140  * version will overwrite the one currently loaded.
141  *
142  * Mappings not belonging to the current platform or with no platform field
143  * specified will be ignored (i.e. mappings for Linux will be ignored in
144  * Windows, etc).
145  *
146  * This function will load the text database entirely in memory before
147  * processing it, so take this into consideration if you are in a memory
148  * constrained environment.
149  *
150  * \param rw the data stream for the mappings to be added
151  * \param freerw non-zero to close the stream after being read
152  * \returns the number of mappings added or -1 on error; call SDL_GetError()
153  * for more information.
154  *
155  * \since This function is available since SDL 2.0.2.
156  *
157  * \sa SDL_GameControllerAddMapping
158  * \sa SDL_GameControllerAddMappingsFromFile
159  * \sa SDL_GameControllerMappingForGUID
160  */
161 extern DECLSPEC int SDLCALL SDL_GameControllerAddMappingsFromRW(SDL_RWops * rw, int freerw);
162 
163 /**
164  * Load a set of mappings from a file, filtered by the current SDL_GetPlatform()
165  *
166  * Convenience macro.
167  */
168 #define SDL_GameControllerAddMappingsFromFile(file) SDL_GameControllerAddMappingsFromRW(SDL_RWFromFile(file, "rb"), 1)
169 
170 /**
171  * Add support for controllers that SDL is unaware of or to cause an existing
172  * controller to have a different binding.
173  *
174  * The mapping string has the format "GUID,name,mapping", where GUID is the
175  * string value from SDL_JoystickGetGUIDString(), name is the human readable
176  * string for the device and mappings are controller mappings to joystick
177  * ones. Under Windows there is a reserved GUID of "xinput" that covers all
178  * XInput devices. The mapping format for joystick is: {| |bX |a joystick
179  * button, index X |- |hX.Y |hat X with value Y |- |aX |axis X of the joystick
180  * |} Buttons can be used as a controller axes and vice versa.
181  *
182  * This string shows an example of a valid mapping for a controller:
183  *
184  * ```c
185  * "341a3608000000000000504944564944,Afterglow PS3 Controller,a:b1,b:b2,y:b3,x:b0,start:b9,guide:b12,back:b8,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftshoulder:b4,rightshoulder:b5,leftstick:b10,rightstick:b11,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7"
186  * ```
187  *
188  * \param mappingString the mapping string
189  * \returns 1 if a new mapping is added, 0 if an existing mapping is updated,
190  * -1 on error; call SDL_GetError() for more information.
191  *
192  * \since This function is available since SDL 2.0.0.
193  *
194  * \sa SDL_GameControllerMapping
195  * \sa SDL_GameControllerMappingForGUID
196  */
197 extern DECLSPEC int SDLCALL SDL_GameControllerAddMapping(const char* mappingString);
198 
199 /**
200  * Get the number of mappings installed.
201  *
202  * \returns the number of mappings.
203  *
204  * \since This function is available since SDL 2.0.6.
205  */
206 extern DECLSPEC int SDLCALL SDL_GameControllerNumMappings(void);
207 
208 /**
209  * Get the mapping at a particular index.
210  *
211  * \returns the mapping string. Must be freed with SDL_free(). Returns NULL if
212  * the index is out of range.
213  *
214  * \since This function is available since SDL 2.0.6.
215  */
216 extern DECLSPEC char * SDLCALL SDL_GameControllerMappingForIndex(int mapping_index);
217 
218 /**
219  * Get the game controller mapping string for a given GUID.
220  *
221  * The returned string must be freed with SDL_free().
222  *
223  * \param guid a structure containing the GUID for which a mapping is desired
224  * \returns a mapping string or NULL on error; call SDL_GetError() for more
225  * information.
226  *
227  * \since This function is available since SDL 2.0.0.
228  *
229  * \sa SDL_JoystickGetDeviceGUID
230  * \sa SDL_JoystickGetGUID
231  */
232 extern DECLSPEC char * SDLCALL SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid);
233 
234 /**
235  * Get the current mapping of a Game Controller.
236  *
237  * The returned string must be freed with SDL_free().
238  *
239  * Details about mappings are discussed with SDL_GameControllerAddMapping().
240  *
241  * \param gamecontroller the game controller you want to get the current
242  * mapping for
243  * \returns a string that has the controller's mapping or NULL if no mapping
244  * is available; call SDL_GetError() for more information.
245  *
246  * \since This function is available since SDL 2.0.0.
247  *
248  * \sa SDL_GameControllerAddMapping
249  * \sa SDL_GameControllerMappingForGUID
250  */
251 extern DECLSPEC char * SDLCALL SDL_GameControllerMapping(SDL_GameController *gamecontroller);
252 
253 /**
254  * Check if the given joystick is supported by the game controller interface.
255  *
256  * `joystick_index` is the same as the `device_index` passed to
257  * SDL_JoystickOpen().
258  *
259  * \param joystick_index the device_index of a device, up to
260  * SDL_NumJoysticks()
261  * \returns SDL_TRUE if the given joystick is supported by the game controller
262  * interface, SDL_FALSE if it isn't or it's an invalid index.
263  *
264  * \since This function is available since SDL 2.0.0.
265  *
266  * \sa SDL_GameControllerNameForIndex
267  * \sa SDL_GameControllerOpen
268  */
269 extern DECLSPEC SDL_bool SDLCALL SDL_IsGameController(int joystick_index);
270 
271 /**
272  * Get the implementation dependent name for the game controller.
273  *
274  * This function can be called before any controllers are opened.
275  *
276  * `joystick_index` is the same as the `device_index` passed to
277  * SDL_JoystickOpen().
278  *
279  * \param joystick_index the device_index of a device, from zero to
280  * SDL_NumJoysticks()-1
281  * \returns the implementation-dependent name for the game controller, or NULL
282  * if there is no name or the index is invalid.
283  *
284  * \since This function is available since SDL 2.0.0.
285  *
286  * \sa SDL_GameControllerName
287  * \sa SDL_GameControllerOpen
288  * \sa SDL_IsGameController
289  */
290 extern DECLSPEC const char *SDLCALL SDL_GameControllerNameForIndex(int joystick_index);
291 
292 /**
293  * Get the implementation dependent path for the game controller.
294  *
295  * This function can be called before any controllers are opened.
296  *
297  * `joystick_index` is the same as the `device_index` passed to
298  * SDL_JoystickOpen().
299  *
300  * \param joystick_index the device_index of a device, from zero to
301  * SDL_NumJoysticks()-1
302  * \returns the implementation-dependent path for the game controller, or NULL
303  * if there is no path or the index is invalid.
304  *
305  * \since This function is available since SDL 2.24.0.
306  *
307  * \sa SDL_GameControllerPath
308  */
309 extern DECLSPEC const char *SDLCALL SDL_GameControllerPathForIndex(int joystick_index);
310 
311 /**
312  * Get the type of a game controller.
313  *
314  * This can be called before any controllers are opened.
315  *
316  * \param joystick_index the device_index of a device, from zero to
317  * SDL_NumJoysticks()-1
318  * \returns the controller type.
319  *
320  * \since This function is available since SDL 2.0.12.
321  */
322 extern DECLSPEC SDL_GameControllerType SDLCALL SDL_GameControllerTypeForIndex(int joystick_index);
323 
324 /**
325  * Get the mapping of a game controller.
326  *
327  * This can be called before any controllers are opened.
328  *
329  * \param joystick_index the device_index of a device, from zero to
330  * SDL_NumJoysticks()-1
331  * \returns the mapping string. Must be freed with SDL_free(). Returns NULL if
332  * no mapping is available.
333  *
334  * \since This function is available since SDL 2.0.9.
335  */
336 extern DECLSPEC char *SDLCALL SDL_GameControllerMappingForDeviceIndex(int joystick_index);
337 
338 /**
339  * Open a game controller for use.
340  *
341  * `joystick_index` is the same as the `device_index` passed to
342  * SDL_JoystickOpen().
343  *
344  * The index passed as an argument refers to the N'th game controller on the
345  * system. This index is not the value which will identify this controller in
346  * future controller events. The joystick's instance id (SDL_JoystickID) will
347  * be used there instead.
348  *
349  * \param joystick_index the device_index of a device, up to
350  * SDL_NumJoysticks()
351  * \returns a gamecontroller identifier or NULL if an error occurred; call
352  * SDL_GetError() for more information.
353  *
354  * \since This function is available since SDL 2.0.0.
355  *
356  * \sa SDL_GameControllerClose
357  * \sa SDL_GameControllerNameForIndex
358  * \sa SDL_IsGameController
359  */
360 extern DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerOpen(int joystick_index);
361 
362 /**
363  * Get the SDL_GameController associated with an instance id.
364  *
365  * \param joyid the instance id to get the SDL_GameController for
366  * \returns an SDL_GameController on success or NULL on failure; call
367  * SDL_GetError() for more information.
368  *
369  * \since This function is available since SDL 2.0.4.
370  */
372 
373 /**
374  * Get the SDL_GameController associated with a player index.
375  *
376  * Please note that the player index is _not_ the device index, nor is it the
377  * instance id!
378  *
379  * \param player_index the player index, which is not the device index or the
380  * instance id!
381  * \returns the SDL_GameController associated with a player index.
382  *
383  * \since This function is available since SDL 2.0.12.
384  *
385  * \sa SDL_GameControllerGetPlayerIndex
386  * \sa SDL_GameControllerSetPlayerIndex
387  */
388 extern DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerFromPlayerIndex(int player_index);
389 
390 /**
391  * Get the implementation-dependent name for an opened game controller.
392  *
393  * This is the same name as returned by SDL_GameControllerNameForIndex(), but
394  * it takes a controller identifier instead of the (unstable) device index.
395  *
396  * \param gamecontroller a game controller identifier previously returned by
397  * SDL_GameControllerOpen()
398  * \returns the implementation dependent name for the game controller, or NULL
399  * if there is no name or the identifier passed is invalid.
400  *
401  * \since This function is available since SDL 2.0.0.
402  *
403  * \sa SDL_GameControllerNameForIndex
404  * \sa SDL_GameControllerOpen
405  */
406 extern DECLSPEC const char *SDLCALL SDL_GameControllerName(SDL_GameController *gamecontroller);
407 
408 /**
409  * Get the implementation-dependent path for an opened game controller.
410  *
411  * This is the same path as returned by SDL_GameControllerNameForIndex(), but
412  * it takes a controller identifier instead of the (unstable) device index.
413  *
414  * \param gamecontroller a game controller identifier previously returned by
415  * SDL_GameControllerOpen()
416  * \returns the implementation dependent path for the game controller, or NULL
417  * if there is no path or the identifier passed is invalid.
418  *
419  * \since This function is available since SDL 2.24.0.
420  *
421  * \sa SDL_GameControllerPathForIndex
422  */
423 extern DECLSPEC const char *SDLCALL SDL_GameControllerPath(SDL_GameController *gamecontroller);
424 
425 /**
426  * Get the type of this currently opened controller
427  *
428  * This is the same name as returned by SDL_GameControllerTypeForIndex(), but
429  * it takes a controller identifier instead of the (unstable) device index.
430  *
431  * \param gamecontroller the game controller object to query.
432  * \returns the controller type.
433  *
434  * \since This function is available since SDL 2.0.12.
435  */
436 extern DECLSPEC SDL_GameControllerType SDLCALL SDL_GameControllerGetType(SDL_GameController *gamecontroller);
437 
438 /**
439  * Get the player index of an opened game controller.
440  *
441  * For XInput controllers this returns the XInput user index.
442  *
443  * \param gamecontroller the game controller object to query.
444  * \returns the player index for controller, or -1 if it's not available.
445  *
446  * \since This function is available since SDL 2.0.9.
447  */
448 extern DECLSPEC int SDLCALL SDL_GameControllerGetPlayerIndex(SDL_GameController *gamecontroller);
449 
450 /**
451  * Set the player index of an opened game controller.
452  *
453  * \param gamecontroller the game controller object to adjust.
454  * \param player_index Player index to assign to this controller.
455  *
456  * \since This function is available since SDL 2.0.12.
457  */
458 extern DECLSPEC void SDLCALL SDL_GameControllerSetPlayerIndex(SDL_GameController *gamecontroller, int player_index);
459 
460 /**
461  * Get the USB vendor ID of an opened controller, if available.
462  *
463  * If the vendor ID isn't available this function returns 0.
464  *
465  * \param gamecontroller the game controller object to query.
466  * \return the USB vendor ID, or zero if unavailable.
467  *
468  * \since This function is available since SDL 2.0.6.
469  */
470 extern DECLSPEC Uint16 SDLCALL SDL_GameControllerGetVendor(SDL_GameController *gamecontroller);
471 
472 /**
473  * Get the USB product ID of an opened controller, if available.
474  *
475  * If the product ID isn't available this function returns 0.
476  *
477  * \param gamecontroller the game controller object to query.
478  * \return the USB product ID, or zero if unavailable.
479  *
480  * \since This function is available since SDL 2.0.6.
481  */
482 extern DECLSPEC Uint16 SDLCALL SDL_GameControllerGetProduct(SDL_GameController *gamecontroller);
483 
484 /**
485  * Get the product version of an opened controller, if available.
486  *
487  * If the product version isn't available this function returns 0.
488  *
489  * \param gamecontroller the game controller object to query.
490  * \return the USB product version, or zero if unavailable.
491  *
492  * \since This function is available since SDL 2.0.6.
493  */
494 extern DECLSPEC Uint16 SDLCALL SDL_GameControllerGetProductVersion(SDL_GameController *gamecontroller);
495 
496 /**
497  * Get the firmware version of an opened controller, if available.
498  *
499  * If the firmware version isn't available this function returns 0.
500  *
501  * \param gamecontroller the game controller object to query.
502  * \return the controller firmware version, or zero if unavailable.
503  *
504  * \since This function is available since SDL 2.24.0.
505  */
506 extern DECLSPEC Uint16 SDLCALL SDL_GameControllerGetFirmwareVersion(SDL_GameController *gamecontroller);
507 
508 /**
509  * Get the serial number of an opened controller, if available.
510  *
511  * Returns the serial number of the controller, or NULL if it is not
512  * available.
513  *
514  * \param gamecontroller the game controller object to query.
515  * \return the serial number, or NULL if unavailable.
516  *
517  * \since This function is available since SDL 2.0.14.
518  */
519 extern DECLSPEC const char * SDLCALL SDL_GameControllerGetSerial(SDL_GameController *gamecontroller);
520 
521 /**
522  * Check if a controller has been opened and is currently connected.
523  *
524  * \param gamecontroller a game controller identifier previously returned by
525  * SDL_GameControllerOpen()
526  * \returns SDL_TRUE if the controller has been opened and is currently
527  * connected, or SDL_FALSE if not.
528  *
529  * \since This function is available since SDL 2.0.0.
530  *
531  * \sa SDL_GameControllerClose
532  * \sa SDL_GameControllerOpen
533  */
534 extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerGetAttached(SDL_GameController *gamecontroller);
535 
536 /**
537  * Get the Joystick ID from a Game Controller.
538  *
539  * This function will give you a SDL_Joystick object, which allows you to use
540  * the SDL_Joystick functions with a SDL_GameController object. This would be
541  * useful for getting a joystick's position at any given time, even if it
542  * hasn't moved (moving it would produce an event, which would have the axis'
543  * value).
544  *
545  * The pointer returned is owned by the SDL_GameController. You should not
546  * call SDL_JoystickClose() on it, for example, since doing so will likely
547  * cause SDL to crash.
548  *
549  * \param gamecontroller the game controller object that you want to get a
550  * joystick from
551  * \returns a SDL_Joystick object; call SDL_GetError() for more information.
552  *
553  * \since This function is available since SDL 2.0.0.
554  */
555 extern DECLSPEC SDL_Joystick *SDLCALL SDL_GameControllerGetJoystick(SDL_GameController *gamecontroller);
556 
557 /**
558  * Query or change current state of Game Controller events.
559  *
560  * If controller events are disabled, you must call SDL_GameControllerUpdate()
561  * yourself and check the state of the controller when you want controller
562  * information.
563  *
564  * Any number can be passed to SDL_GameControllerEventState(), but only -1, 0,
565  * and 1 will have any effect. Other numbers will just be returned.
566  *
567  * \param state can be one of `SDL_QUERY`, `SDL_IGNORE`, or `SDL_ENABLE`
568  * \returns the same value passed to the function, with exception to -1
569  * (SDL_QUERY), which will return the current state.
570  *
571  * \since This function is available since SDL 2.0.0.
572  *
573  * \sa SDL_JoystickEventState
574  */
575 extern DECLSPEC int SDLCALL SDL_GameControllerEventState(int state);
576 
577 /**
578  * Manually pump game controller updates if not using the loop.
579  *
580  * This function is called automatically by the event loop if events are
581  * enabled. Under such circumstances, it will not be necessary to call this
582  * function.
583  *
584  * \since This function is available since SDL 2.0.0.
585  */
586 extern DECLSPEC void SDLCALL SDL_GameControllerUpdate(void);
587 
588 
589 /**
590  * The list of axes available from a controller
591  *
592  * Thumbstick axis values range from SDL_JOYSTICK_AXIS_MIN to SDL_JOYSTICK_AXIS_MAX,
593  * and are centered within ~8000 of zero, though advanced UI will allow users to set
594  * or autodetect the dead zone, which varies between controllers.
595  *
596  * Trigger axis values range from 0 to SDL_JOYSTICK_AXIS_MAX.
597  */
598 typedef enum
599 {
609 
610 /**
611  * Convert a string into SDL_GameControllerAxis enum.
612  *
613  * This function is called internally to translate SDL_GameController mapping
614  * strings for the underlying joystick device into the consistent
615  * SDL_GameController mapping. You do not normally need to call this function
616  * unless you are parsing SDL_GameController mappings in your own code.
617  *
618  * Note specially that "righttrigger" and "lefttrigger" map to
619  * `SDL_CONTROLLER_AXIS_TRIGGERRIGHT` and `SDL_CONTROLLER_AXIS_TRIGGERLEFT`,
620  * respectively.
621  *
622  * \param str string representing a SDL_GameController axis
623  * \returns the SDL_GameControllerAxis enum corresponding to the input string,
624  * or `SDL_CONTROLLER_AXIS_INVALID` if no match was found.
625  *
626  * \since This function is available since SDL 2.0.0.
627  *
628  * \sa SDL_GameControllerGetStringForAxis
629  */
630 extern DECLSPEC SDL_GameControllerAxis SDLCALL SDL_GameControllerGetAxisFromString(const char *str);
631 
632 /**
633  * Convert from an SDL_GameControllerAxis enum to a string.
634  *
635  * The caller should not SDL_free() the returned string.
636  *
637  * \param axis an enum value for a given SDL_GameControllerAxis
638  * \returns a string for the given axis, or NULL if an invalid axis is
639  * specified. The string returned is of the format used by
640  * SDL_GameController mapping strings.
641  *
642  * \since This function is available since SDL 2.0.0.
643  *
644  * \sa SDL_GameControllerGetAxisFromString
645  */
646 extern DECLSPEC const char* SDLCALL SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis axis);
647 
648 /**
649  * Get the SDL joystick layer binding for a controller axis mapping.
650  *
651  * \param gamecontroller a game controller
652  * \param axis an axis enum value (one of the SDL_GameControllerAxis values)
653  * \returns a SDL_GameControllerButtonBind describing the bind. On failure
654  * (like the given Controller axis doesn't exist on the device), its
655  * `.bindType` will be `SDL_CONTROLLER_BINDTYPE_NONE`.
656  *
657  * \since This function is available since SDL 2.0.0.
658  *
659  * \sa SDL_GameControllerGetBindForButton
660  */
661 extern DECLSPEC SDL_GameControllerButtonBind SDLCALL
664 
665 /**
666  * Query whether a game controller has a given axis.
667  *
668  * This merely reports whether the controller's mapping defined this axis, as
669  * that is all the information SDL has about the physical device.
670  *
671  * \param gamecontroller a game controller
672  * \param axis an axis enum value (an SDL_GameControllerAxis value)
673  * \returns SDL_TRUE if the controller has this axis, SDL_FALSE otherwise.
674  *
675  * \since This function is available since SDL 2.0.14.
676  */
677 extern DECLSPEC SDL_bool SDLCALL
679 
680 /**
681  * Get the current state of an axis control on a game controller.
682  *
683  * The axis indices start at index 0.
684  *
685  * The state is a value ranging from -32768 to 32767. Triggers, however, range
686  * from 0 to 32767 (they never return a negative value).
687  *
688  * \param gamecontroller a game controller
689  * \param axis an axis index (one of the SDL_GameControllerAxis values)
690  * \returns axis state (including 0) on success or 0 (also) on failure; call
691  * SDL_GetError() for more information.
692  *
693  * \since This function is available since SDL 2.0.0.
694  *
695  * \sa SDL_GameControllerGetButton
696  */
697 extern DECLSPEC Sint16 SDLCALL
699 
700 /**
701  * The list of buttons available from a controller
702  */
703 typedef enum
704 {
721  SDL_CONTROLLER_BUTTON_MISC1, /* Xbox Series X share button, PS5 microphone button, Nintendo Switch Pro capture button, Amazon Luna microphone button */
722  SDL_CONTROLLER_BUTTON_PADDLE1, /* Xbox Elite paddle P1 */
723  SDL_CONTROLLER_BUTTON_PADDLE2, /* Xbox Elite paddle P3 */
724  SDL_CONTROLLER_BUTTON_PADDLE3, /* Xbox Elite paddle P2 */
725  SDL_CONTROLLER_BUTTON_PADDLE4, /* Xbox Elite paddle P4 */
726  SDL_CONTROLLER_BUTTON_TOUCHPAD, /* PS4/PS5 touchpad button */
729 
730 /**
731  * Convert a string into an SDL_GameControllerButton enum.
732  *
733  * This function is called internally to translate SDL_GameController mapping
734  * strings for the underlying joystick device into the consistent
735  * SDL_GameController mapping. You do not normally need to call this function
736  * unless you are parsing SDL_GameController mappings in your own code.
737  *
738  * \param str string representing a SDL_GameController axis
739  * \returns the SDL_GameControllerButton enum corresponding to the input
740  * string, or `SDL_CONTROLLER_AXIS_INVALID` if no match was found.
741  *
742  * \since This function is available since SDL 2.0.0.
743  */
744 extern DECLSPEC SDL_GameControllerButton SDLCALL SDL_GameControllerGetButtonFromString(const char *str);
745 
746 /**
747  * Convert from an SDL_GameControllerButton enum to a string.
748  *
749  * The caller should not SDL_free() the returned string.
750  *
751  * \param button an enum value for a given SDL_GameControllerButton
752  * \returns a string for the given button, or NULL if an invalid axis is
753  * specified. The string returned is of the format used by
754  * SDL_GameController mapping strings.
755  *
756  * \since This function is available since SDL 2.0.0.
757  *
758  * \sa SDL_GameControllerGetButtonFromString
759  */
760 extern DECLSPEC const char* SDLCALL SDL_GameControllerGetStringForButton(SDL_GameControllerButton button);
761 
762 /**
763  * Get the SDL joystick layer binding for a controller button mapping.
764  *
765  * \param gamecontroller a game controller
766  * \param button an button enum value (an SDL_GameControllerButton value)
767  * \returns a SDL_GameControllerButtonBind describing the bind. On failure
768  * (like the given Controller button doesn't exist on the device),
769  * its `.bindType` will be `SDL_CONTROLLER_BINDTYPE_NONE`.
770  *
771  * \since This function is available since SDL 2.0.0.
772  *
773  * \sa SDL_GameControllerGetBindForAxis
774  */
775 extern DECLSPEC SDL_GameControllerButtonBind SDLCALL
777  SDL_GameControllerButton button);
778 
779 /**
780  * Query whether a game controller has a given button.
781  *
782  * This merely reports whether the controller's mapping defined this button,
783  * as that is all the information SDL has about the physical device.
784  *
785  * \param gamecontroller a game controller
786  * \param button a button enum value (an SDL_GameControllerButton value)
787  * \returns SDL_TRUE if the controller has this button, SDL_FALSE otherwise.
788  *
789  * \since This function is available since SDL 2.0.14.
790  */
791 extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerHasButton(SDL_GameController *gamecontroller,
792  SDL_GameControllerButton button);
793 
794 /**
795  * Get the current state of a button on a game controller.
796  *
797  * \param gamecontroller a game controller
798  * \param button a button index (one of the SDL_GameControllerButton values)
799  * \returns 1 for pressed state or 0 for not pressed state or error; call
800  * SDL_GetError() for more information.
801  *
802  * \since This function is available since SDL 2.0.0.
803  *
804  * \sa SDL_GameControllerGetAxis
805  */
806 extern DECLSPEC Uint8 SDLCALL SDL_GameControllerGetButton(SDL_GameController *gamecontroller,
807  SDL_GameControllerButton button);
808 
809 /**
810  * Get the number of touchpads on a game controller.
811  *
812  * \since This function is available since SDL 2.0.14.
813  */
814 extern DECLSPEC int SDLCALL SDL_GameControllerGetNumTouchpads(SDL_GameController *gamecontroller);
815 
816 /**
817  * Get the number of supported simultaneous fingers on a touchpad on a game
818  * controller.
819  *
820  * \since This function is available since SDL 2.0.14.
821  */
822 extern DECLSPEC int SDLCALL SDL_GameControllerGetNumTouchpadFingers(SDL_GameController *gamecontroller, int touchpad);
823 
824 /**
825  * Get the current state of a finger on a touchpad on a game controller.
826  *
827  * \since This function is available since SDL 2.0.14.
828  */
829 extern DECLSPEC int SDLCALL SDL_GameControllerGetTouchpadFinger(SDL_GameController *gamecontroller, int touchpad, int finger, Uint8 *state, float *x, float *y, float *pressure);
830 
831 /**
832  * Return whether a game controller has a particular sensor.
833  *
834  * \param gamecontroller The controller to query
835  * \param type The type of sensor to query
836  * \returns SDL_TRUE if the sensor exists, SDL_FALSE otherwise.
837  *
838  * \since This function is available since SDL 2.0.14.
839  */
840 extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerHasSensor(SDL_GameController *gamecontroller, SDL_SensorType type);
841 
842 /**
843  * Set whether data reporting for a game controller sensor is enabled.
844  *
845  * \param gamecontroller The controller to update
846  * \param type The type of sensor to enable/disable
847  * \param enabled Whether data reporting should be enabled
848  * \returns 0 or -1 if an error occurred.
849  *
850  * \since This function is available since SDL 2.0.14.
851  */
852 extern DECLSPEC int SDLCALL SDL_GameControllerSetSensorEnabled(SDL_GameController *gamecontroller, SDL_SensorType type, SDL_bool enabled);
853 
854 /**
855  * Query whether sensor data reporting is enabled for a game controller.
856  *
857  * \param gamecontroller The controller to query
858  * \param type The type of sensor to query
859  * \returns SDL_TRUE if the sensor is enabled, SDL_FALSE otherwise.
860  *
861  * \since This function is available since SDL 2.0.14.
862  */
863 extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerIsSensorEnabled(SDL_GameController *gamecontroller, SDL_SensorType type);
864 
865 /**
866  * Get the data rate (number of events per second) of a game controller
867  * sensor.
868  *
869  * \param gamecontroller The controller to query
870  * \param type The type of sensor to query
871  * \return the data rate, or 0.0f if the data rate is not available.
872  *
873  * \since This function is available since SDL 2.0.16.
874  */
875 extern DECLSPEC float SDLCALL SDL_GameControllerGetSensorDataRate(SDL_GameController *gamecontroller, SDL_SensorType type);
876 
877 /**
878  * Get the current state of a game controller sensor.
879  *
880  * The number of values and interpretation of the data is sensor dependent.
881  * See SDL_sensor.h for the details for each type of sensor.
882  *
883  * \param gamecontroller The controller to query
884  * \param type The type of sensor to query
885  * \param data A pointer filled with the current sensor state
886  * \param num_values The number of values to write to data
887  * \return 0 or -1 if an error occurred.
888  *
889  * \since This function is available since SDL 2.0.14.
890  */
891 extern DECLSPEC int SDLCALL SDL_GameControllerGetSensorData(SDL_GameController *gamecontroller, SDL_SensorType type, float *data, int num_values);
892 
893 /**
894  * Start a rumble effect on a game controller.
895  *
896  * Each call to this function cancels any previous rumble effect, and calling
897  * it with 0 intensity stops any rumbling.
898  *
899  * \param gamecontroller The controller to vibrate
900  * \param low_frequency_rumble The intensity of the low frequency (left)
901  * rumble motor, from 0 to 0xFFFF
902  * \param high_frequency_rumble The intensity of the high frequency (right)
903  * rumble motor, from 0 to 0xFFFF
904  * \param duration_ms The duration of the rumble effect, in milliseconds
905  * \returns 0, or -1 if rumble isn't supported on this controller
906  *
907  * \since This function is available since SDL 2.0.9.
908  *
909  * \sa SDL_GameControllerHasRumble
910  */
911 extern DECLSPEC int SDLCALL SDL_GameControllerRumble(SDL_GameController *gamecontroller, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms);
912 
913 /**
914  * Start a rumble effect in the game controller's triggers.
915  *
916  * Each call to this function cancels any previous trigger rumble effect, and
917  * calling it with 0 intensity stops any rumbling.
918  *
919  * Note that this is rumbling of the _triggers_ and not the game controller as
920  * a whole. This is currently only supported on Xbox One controllers. If you
921  * want the (more common) whole-controller rumble, use
922  * SDL_GameControllerRumble() instead.
923  *
924  * \param gamecontroller The controller to vibrate
925  * \param left_rumble The intensity of the left trigger rumble motor, from 0
926  * to 0xFFFF
927  * \param right_rumble The intensity of the right trigger rumble motor, from 0
928  * to 0xFFFF
929  * \param duration_ms The duration of the rumble effect, in milliseconds
930  * \returns 0, or -1 if trigger rumble isn't supported on this controller
931  *
932  * \since This function is available since SDL 2.0.14.
933  *
934  * \sa SDL_GameControllerHasRumbleTriggers
935  */
936 extern DECLSPEC int SDLCALL SDL_GameControllerRumbleTriggers(SDL_GameController *gamecontroller, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms);
937 
938 /**
939  * Query whether a game controller has an LED.
940  *
941  * \param gamecontroller The controller to query
942  * \returns SDL_TRUE, or SDL_FALSE if this controller does not have a
943  * modifiable LED
944  *
945  * \since This function is available since SDL 2.0.14.
946  */
947 extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerHasLED(SDL_GameController *gamecontroller);
948 
949 /**
950  * Query whether a game controller has rumble support.
951  *
952  * \param gamecontroller The controller to query
953  * \returns SDL_TRUE, or SDL_FALSE if this controller does not have rumble
954  * support
955  *
956  * \since This function is available since SDL 2.0.18.
957  *
958  * \sa SDL_GameControllerRumble
959  */
960 extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerHasRumble(SDL_GameController *gamecontroller);
961 
962 /**
963  * Query whether a game controller has rumble support on triggers.
964  *
965  * \param gamecontroller The controller to query
966  * \returns SDL_TRUE, or SDL_FALSE if this controller does not have trigger
967  * rumble support
968  *
969  * \since This function is available since SDL 2.0.18.
970  *
971  * \sa SDL_GameControllerRumbleTriggers
972  */
973 extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerHasRumbleTriggers(SDL_GameController *gamecontroller);
974 
975 /**
976  * Update a game controller's LED color.
977  *
978  * \param gamecontroller The controller to update
979  * \param red The intensity of the red LED
980  * \param green The intensity of the green LED
981  * \param blue The intensity of the blue LED
982  * \returns 0, or -1 if this controller does not have a modifiable LED
983  *
984  * \since This function is available since SDL 2.0.14.
985  */
986 extern DECLSPEC int SDLCALL SDL_GameControllerSetLED(SDL_GameController *gamecontroller, Uint8 red, Uint8 green, Uint8 blue);
987 
988 /**
989  * Send a controller specific effect packet
990  *
991  * \param gamecontroller The controller to affect
992  * \param data The data to send to the controller
993  * \param size The size of the data to send to the controller
994  * \returns 0, or -1 if this controller or driver doesn't support effect
995  * packets
996  *
997  * \since This function is available since SDL 2.0.16.
998  */
999 extern DECLSPEC int SDLCALL SDL_GameControllerSendEffect(SDL_GameController *gamecontroller, const void *data, int size);
1000 
1001 /**
1002  * Close a game controller previously opened with SDL_GameControllerOpen().
1003  *
1004  * \param gamecontroller a game controller identifier previously returned by
1005  * SDL_GameControllerOpen()
1006  *
1007  * \since This function is available since SDL 2.0.0.
1008  *
1009  * \sa SDL_GameControllerOpen
1010  */
1011 extern DECLSPEC void SDLCALL SDL_GameControllerClose(SDL_GameController *gamecontroller);
1012 
1013 /**
1014  * Return the sfSymbolsName for a given button on a game controller on Apple
1015  * platforms.
1016  *
1017  * \param gamecontroller the controller to query
1018  * \param button a button on the game controller
1019  * \returns the sfSymbolsName or NULL if the name can't be found
1020  *
1021  * \since This function is available since SDL 2.0.18.
1022  *
1023  * \sa SDL_GameControllerGetAppleSFSymbolsNameForAxis
1024  */
1025 extern DECLSPEC const char* SDLCALL SDL_GameControllerGetAppleSFSymbolsNameForButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button);
1026 
1027 /**
1028  * Return the sfSymbolsName for a given axis on a game controller on Apple
1029  * platforms.
1030  *
1031  * \param gamecontroller the controller to query
1032  * \param axis an axis on the game controller
1033  * \returns the sfSymbolsName or NULL if the name can't be found
1034  *
1035  * \since This function is available since SDL 2.0.18.
1036  *
1037  * \sa SDL_GameControllerGetAppleSFSymbolsNameForButton
1038  */
1039 extern DECLSPEC const char* SDLCALL SDL_GameControllerGetAppleSFSymbolsNameForAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis);
1040 
1041 
1042 /* Ends C function definitions when using C++ */
1043 #ifdef __cplusplus
1044 }
1045 #endif
1046 #include "close_code.h"
1047 
1048 #endif /* SDL_gamecontroller_h_ */
1049 
1050 /* vi: set ts=4 sw=4 expandtab: */
SDL_GameController * SDL_GameControllerFromInstanceID(SDL_JoystickID joyid)
SDL_bool SDL_GameControllerHasSensor(SDL_GameController *gamecontroller, SDL_SensorType type)
void SDL_GameControllerUpdate(void)
int SDL_GameControllerRumbleTriggers(SDL_GameController *gamecontroller, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms)
int SDL_GameControllerAddMappingsFromRW(SDL_RWops *rw, int freerw)
char * SDL_GameControllerMappingForIndex(int mapping_index)
const char * SDL_GameControllerName(SDL_GameController *gamecontroller)
SDL_bool SDL_GameControllerHasLED(SDL_GameController *gamecontroller)
SDL_GameControllerType SDL_GameControllerTypeForIndex(int joystick_index)
int SDL_GameControllerNumMappings(void)
void SDL_GameControllerClose(SDL_GameController *gamecontroller)
int SDL_GameControllerEventState(int state)
int SDL_GameControllerRumble(SDL_GameController *gamecontroller, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
SDL_bool SDL_GameControllerHasRumble(SDL_GameController *gamecontroller)
SDL_Joystick * SDL_GameControllerGetJoystick(SDL_GameController *gamecontroller)
SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
const char * SDL_GameControllerGetAppleSFSymbolsNameForButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
SDL_bool SDL_GameControllerIsSensorEnabled(SDL_GameController *gamecontroller, SDL_SensorType type)
int SDL_GameControllerGetTouchpadFinger(SDL_GameController *gamecontroller, int touchpad, int finger, Uint8 *state, float *x, float *y, float *pressure)
SDL_bool SDL_IsGameController(int joystick_index)
const char * SDL_GameControllerGetStringForButton(SDL_GameControllerButton button)
SDL_GameController * SDL_GameControllerFromPlayerIndex(int player_index)
int SDL_GameControllerAddMapping(const char *mappingString)
char * SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
const char * SDL_GameControllerGetSerial(SDL_GameController *gamecontroller)
int SDL_GameControllerGetNumTouchpadFingers(SDL_GameController *gamecontroller, int touchpad)
const char * SDL_GameControllerPathForIndex(int joystick_index)
SDL_bool SDL_GameControllerHasAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
int SDL_GameControllerSetSensorEnabled(SDL_GameController *gamecontroller, SDL_SensorType type, SDL_bool enabled)
const char * SDL_GameControllerNameForIndex(int joystick_index)
float SDL_GameControllerGetSensorDataRate(SDL_GameController *gamecontroller, SDL_SensorType type)
const char * SDL_GameControllerPath(SDL_GameController *gamecontroller)
int SDL_GameControllerGetSensorData(SDL_GameController *gamecontroller, SDL_SensorType type, float *data, int num_values)
Uint16 SDL_GameControllerGetVendor(SDL_GameController *gamecontroller)
SDL_GameControllerAxis
@ SDL_CONTROLLER_AXIS_LEFTX
@ SDL_CONTROLLER_AXIS_TRIGGERRIGHT
@ SDL_CONTROLLER_AXIS_INVALID
@ SDL_CONTROLLER_AXIS_RIGHTY
@ SDL_CONTROLLER_AXIS_RIGHTX
@ SDL_CONTROLLER_AXIS_MAX
@ SDL_CONTROLLER_AXIS_TRIGGERLEFT
@ SDL_CONTROLLER_AXIS_LEFTY
SDL_GameControllerButton
@ SDL_CONTROLLER_BUTTON_B
@ SDL_CONTROLLER_BUTTON_BACK
@ SDL_CONTROLLER_BUTTON_LEFTSTICK
@ SDL_CONTROLLER_BUTTON_START
@ SDL_CONTROLLER_BUTTON_PADDLE2
@ SDL_CONTROLLER_BUTTON_PADDLE1
@ SDL_CONTROLLER_BUTTON_DPAD_LEFT
@ SDL_CONTROLLER_BUTTON_RIGHTSHOULDER
@ SDL_CONTROLLER_BUTTON_INVALID
@ SDL_CONTROLLER_BUTTON_DPAD_DOWN
@ SDL_CONTROLLER_BUTTON_TOUCHPAD
@ SDL_CONTROLLER_BUTTON_DPAD_UP
@ SDL_CONTROLLER_BUTTON_MAX
@ SDL_CONTROLLER_BUTTON_LEFTSHOULDER
@ SDL_CONTROLLER_BUTTON_GUIDE
@ SDL_CONTROLLER_BUTTON_DPAD_RIGHT
@ SDL_CONTROLLER_BUTTON_MISC1
@ SDL_CONTROLLER_BUTTON_X
@ SDL_CONTROLLER_BUTTON_RIGHTSTICK
@ SDL_CONTROLLER_BUTTON_PADDLE3
@ SDL_CONTROLLER_BUTTON_Y
@ SDL_CONTROLLER_BUTTON_A
@ SDL_CONTROLLER_BUTTON_PADDLE4
struct _SDL_GameController SDL_GameController
const char * SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis axis)
Uint16 SDL_GameControllerGetProductVersion(SDL_GameController *gamecontroller)
SDL_GameController * SDL_GameControllerOpen(int joystick_index)
SDL_GameControllerAxis SDL_GameControllerGetAxisFromString(const char *str)
int SDL_GameControllerSetLED(SDL_GameController *gamecontroller, Uint8 red, Uint8 green, Uint8 blue)
const char * SDL_GameControllerGetAppleSFSymbolsNameForAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
Uint8 SDL_GameControllerGetButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
Uint16 SDL_GameControllerGetFirmwareVersion(SDL_GameController *gamecontroller)
Uint16 SDL_GameControllerGetProduct(SDL_GameController *gamecontroller)
char * SDL_GameControllerMapping(SDL_GameController *gamecontroller)
int SDL_GameControllerGetNumTouchpads(SDL_GameController *gamecontroller)
char * SDL_GameControllerMappingForDeviceIndex(int joystick_index)
Sint16 SDL_GameControllerGetAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
int SDL_GameControllerGetPlayerIndex(SDL_GameController *gamecontroller)
SDL_GameControllerType SDL_GameControllerGetType(SDL_GameController *gamecontroller)
void SDL_GameControllerSetPlayerIndex(SDL_GameController *gamecontroller, int player_index)
SDL_bool SDL_GameControllerHasButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
SDL_bool SDL_GameControllerGetAttached(SDL_GameController *gamecontroller)
int SDL_GameControllerSendEffect(SDL_GameController *gamecontroller, const void *data, int size)
SDL_GameControllerType
@ SDL_CONTROLLER_TYPE_VIRTUAL
@ SDL_CONTROLLER_TYPE_XBOX360
@ SDL_CONTROLLER_TYPE_PS4
@ SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO
@ SDL_CONTROLLER_TYPE_GOOGLE_STADIA
@ SDL_CONTROLLER_TYPE_AMAZON_LUNA
@ SDL_CONTROLLER_TYPE_PS3
@ SDL_CONTROLLER_TYPE_PS5
@ SDL_CONTROLLER_TYPE_XBOXONE
@ SDL_CONTROLLER_TYPE_UNKNOWN
SDL_GameControllerButton SDL_GameControllerGetButtonFromString(const char *str)
SDL_GameControllerBindType
@ SDL_CONTROLLER_BINDTYPE_AXIS
@ SDL_CONTROLLER_BINDTYPE_HAT
@ SDL_CONTROLLER_BINDTYPE_NONE
@ SDL_CONTROLLER_BINDTYPE_BUTTON
SDL_bool SDL_GameControllerHasRumbleTriggers(SDL_GameController *gamecontroller)
struct _SDL_Joystick SDL_Joystick
Definition: SDL_joystick.h:70
Sint32 SDL_JoystickID
Definition: SDL_joystick.h:82
SDL_SensorType
Definition: SDL_sensor.h:70
uint8_t Uint8
Definition: SDL_stdinc.h:202
uint16_t Uint16
Definition: SDL_stdinc.h:214
SDL_bool
Definition: SDL_stdinc.h:185
int16_t Sint16
Definition: SDL_stdinc.h:208
uint32_t Uint32
Definition: SDL_stdinc.h:226
SDL_GameControllerBindType bindType
union SDL_GameControllerButtonBind::@0 value