SDL_joystick.h 36 KB

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