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