SDL_joystick.h 37 KB

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