SDL_joystick.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
  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_NumJoysticks(), 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 JoystickGUID is a stable 128-bit identifier for a joystick device that does not change over time, it identifies class of
  30. * the device (a X360 wired controller for example). This identifier is platform dependent.
  31. *
  32. *
  33. */
  34. #ifndef SDL_joystick_h_
  35. #define SDL_joystick_h_
  36. #include "SDL_stdinc.h"
  37. #include "SDL_error.h"
  38. #include "begin_code.h"
  39. /* Set up for C function definitions, even when using C++ */
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /**
  44. * \file SDL_joystick.h
  45. *
  46. * In order to use these functions, SDL_Init() must have been called
  47. * with the ::SDL_INIT_JOYSTICK flag. This causes SDL to scan the system
  48. * for joysticks, and load appropriate drivers.
  49. *
  50. * If you would like to receive joystick updates while the application
  51. * is in the background, you should set the following hint before calling
  52. * SDL_Init(): SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
  53. */
  54. /**
  55. * The joystick structure used to identify an SDL joystick
  56. */
  57. struct _SDL_Joystick;
  58. typedef struct _SDL_Joystick SDL_Joystick;
  59. /* A structure that encodes the stable unique id for a joystick device */
  60. typedef struct {
  61. Uint8 data[16];
  62. } SDL_JoystickGUID;
  63. /**
  64. * This is a unique ID for a joystick for the time it is connected to the system,
  65. * and is never reused for the lifetime of the application. If the joystick is
  66. * disconnected and reconnected, it will get a new ID.
  67. *
  68. * The ID value starts at 0 and increments from there. The value -1 is an invalid ID.
  69. */
  70. typedef Sint32 SDL_JoystickID;
  71. typedef enum
  72. {
  73. SDL_JOYSTICK_TYPE_UNKNOWN,
  74. SDL_JOYSTICK_TYPE_GAMECONTROLLER,
  75. SDL_JOYSTICK_TYPE_WHEEL,
  76. SDL_JOYSTICK_TYPE_ARCADE_STICK,
  77. SDL_JOYSTICK_TYPE_FLIGHT_STICK,
  78. SDL_JOYSTICK_TYPE_DANCE_PAD,
  79. SDL_JOYSTICK_TYPE_GUITAR,
  80. SDL_JOYSTICK_TYPE_DRUM_KIT,
  81. SDL_JOYSTICK_TYPE_ARCADE_PAD,
  82. SDL_JOYSTICK_TYPE_THROTTLE
  83. } SDL_JoystickType;
  84. typedef enum
  85. {
  86. SDL_JOYSTICK_POWER_UNKNOWN = -1,
  87. SDL_JOYSTICK_POWER_EMPTY, /* <= 5% */
  88. SDL_JOYSTICK_POWER_LOW, /* <= 20% */
  89. SDL_JOYSTICK_POWER_MEDIUM, /* <= 70% */
  90. SDL_JOYSTICK_POWER_FULL, /* <= 100% */
  91. SDL_JOYSTICK_POWER_WIRED,
  92. SDL_JOYSTICK_POWER_MAX
  93. } SDL_JoystickPowerLevel;
  94. /* Set max recognized G-force from accelerometer
  95. See src/joystick/uikit/SDL_sysjoystick.m for notes on why this is needed
  96. */
  97. #define SDL_IPHONE_MAX_GFORCE 5.0
  98. /* Function prototypes */
  99. /**
  100. * Locking for multi-threaded access to the joystick API
  101. *
  102. * If you are using the joystick API or handling events from multiple threads
  103. * you should use these locking functions to protect access to the joysticks.
  104. *
  105. * In particular, you are guaranteed that the joystick list won't change, so
  106. * the API functions that take a joystick index will be valid, and joystick
  107. * and game controller events will not be delivered.
  108. */
  109. extern DECLSPEC void SDLCALL SDL_LockJoysticks(void);
  110. extern DECLSPEC void SDLCALL SDL_UnlockJoysticks(void);
  111. /**
  112. * Count the number of joysticks attached to the system right now
  113. */
  114. extern DECLSPEC int SDLCALL SDL_NumJoysticks(void);
  115. /**
  116. * Get the implementation dependent name of a joystick.
  117. * This can be called before any joysticks are opened.
  118. * If no name can be found, this function returns NULL.
  119. */
  120. extern DECLSPEC const char *SDLCALL SDL_JoystickNameForIndex(int device_index);
  121. /**
  122. * Get the player index of a joystick, or -1 if it's not available
  123. * This can be called before any joysticks are opened.
  124. */
  125. extern DECLSPEC int SDLCALL SDL_JoystickGetDevicePlayerIndex(int device_index);
  126. /**
  127. * Return the GUID for the joystick at this index
  128. * This can be called before any joysticks are opened.
  129. */
  130. extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetDeviceGUID(int device_index);
  131. /**
  132. * Get the USB vendor ID of a joystick, if available.
  133. * This can be called before any joysticks are opened.
  134. * If the vendor ID isn't available this function returns 0.
  135. */
  136. extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetDeviceVendor(int device_index);
  137. /**
  138. * Get the USB product ID of a joystick, if available.
  139. * This can be called before any joysticks are opened.
  140. * If the product ID isn't available this function returns 0.
  141. */
  142. extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetDeviceProduct(int device_index);
  143. /**
  144. * Get the product version of a joystick, if available.
  145. * This can be called before any joysticks are opened.
  146. * If the product version isn't available this function returns 0.
  147. */
  148. extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetDeviceProductVersion(int device_index);
  149. /**
  150. * Get the type of a joystick, if available.
  151. * This can be called before any joysticks are opened.
  152. */
  153. extern DECLSPEC SDL_JoystickType SDLCALL SDL_JoystickGetDeviceType(int device_index);
  154. /**
  155. * Get the instance ID of a joystick.
  156. * This can be called before any joysticks are opened.
  157. * If the index is out of range, this function will return -1.
  158. */
  159. extern DECLSPEC SDL_JoystickID SDLCALL SDL_JoystickGetDeviceInstanceID(int device_index);
  160. /**
  161. * Open a joystick for use.
  162. * The index passed as an argument refers to the N'th joystick on the system.
  163. * This index is not the value which will identify this joystick in future
  164. * joystick events. The joystick's instance id (::SDL_JoystickID) will be used
  165. * there instead.
  166. *
  167. * \return A joystick identifier, or NULL if an error occurred.
  168. */
  169. extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickOpen(int device_index);
  170. /**
  171. * Return the SDL_Joystick associated with an instance id.
  172. */
  173. extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickFromInstanceID(SDL_JoystickID instance_id);
  174. /**
  175. * Return the SDL_Joystick associated with a player index.
  176. */
  177. extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickFromPlayerIndex(int player_index);
  178. /**
  179. * Attaches a new virtual joystick.
  180. * Returns the joystick's device index, or -1 if an error occurred.
  181. */
  182. extern DECLSPEC int SDLCALL SDL_JoystickAttachVirtual(SDL_JoystickType type,
  183. int naxes,
  184. int nbuttons,
  185. int nhats);
  186. /**
  187. * Detaches a virtual joystick
  188. * Returns 0 on success, or -1 if an error occurred.
  189. */
  190. extern DECLSPEC int SDLCALL SDL_JoystickDetachVirtual(int device_index);
  191. /**
  192. * Indicates whether or not a virtual-joystick is at a given device index.
  193. */
  194. extern DECLSPEC SDL_bool SDLCALL SDL_JoystickIsVirtual(int device_index);
  195. /**
  196. * Set values on an opened, virtual-joystick's controls.
  197. * Please note that values set here will not be applied until the next
  198. * call to SDL_JoystickUpdate, which can either be called directly,
  199. * or can be called indirectly through various other SDL APIS,
  200. * including, but not limited to the following: SDL_PollEvent,
  201. * SDL_PumpEvents, SDL_WaitEventTimeout, SDL_WaitEvent.
  202. *
  203. * Returns 0 on success, -1 on error.
  204. */
  205. extern DECLSPEC int SDLCALL SDL_JoystickSetVirtualAxis(SDL_Joystick *joystick, int axis, Sint16 value);
  206. extern DECLSPEC int SDLCALL SDL_JoystickSetVirtualButton(SDL_Joystick *joystick, int button, Uint8 value);
  207. extern DECLSPEC int SDLCALL SDL_JoystickSetVirtualHat(SDL_Joystick *joystick, int hat, Uint8 value);
  208. /**
  209. * Return the name for this currently opened joystick.
  210. * If no name can be found, this function returns NULL.
  211. */
  212. extern DECLSPEC const char *SDLCALL SDL_JoystickName(SDL_Joystick *joystick);
  213. /**
  214. * Get the player index of an opened joystick, or -1 if it's not available
  215. *
  216. * For XInput controllers this returns the XInput user index.
  217. */
  218. extern DECLSPEC int SDLCALL SDL_JoystickGetPlayerIndex(SDL_Joystick *joystick);
  219. /**
  220. * Set the player index of an opened joystick
  221. */
  222. extern DECLSPEC void SDLCALL SDL_JoystickSetPlayerIndex(SDL_Joystick *joystick, int player_index);
  223. /**
  224. * Return the GUID for this opened joystick
  225. */
  226. extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetGUID(SDL_Joystick *joystick);
  227. /**
  228. * Get the USB vendor ID of an opened joystick, if available.
  229. * If the vendor ID isn't available this function returns 0.
  230. */
  231. extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetVendor(SDL_Joystick *joystick);
  232. /**
  233. * Get the USB product ID of an opened joystick, if available.
  234. * If the product ID isn't available this function returns 0.
  235. */
  236. extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetProduct(SDL_Joystick *joystick);
  237. /**
  238. * Get the product version of an opened joystick, if available.
  239. * If the product version isn't available this function returns 0.
  240. */
  241. extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetProductVersion(SDL_Joystick *joystick);
  242. /**
  243. * Get the serial number of an opened joystick, if available.
  244. *
  245. * Returns the serial number of the joystick, or NULL if it is not available.
  246. */
  247. extern DECLSPEC const char * SDLCALL SDL_JoystickGetSerial(SDL_Joystick *joystick);
  248. /**
  249. * Get the type of an opened joystick.
  250. */
  251. extern DECLSPEC SDL_JoystickType SDLCALL SDL_JoystickGetType(SDL_Joystick *joystick);
  252. /**
  253. * Return a string representation for this guid. pszGUID must point to at least 33 bytes
  254. * (32 for the string plus a NULL terminator).
  255. */
  256. extern DECLSPEC void SDLCALL SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID);
  257. /**
  258. * Convert a string into a joystick guid
  259. */
  260. extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetGUIDFromString(const char *pchGUID);
  261. /**
  262. * Returns SDL_TRUE if the joystick has been opened and currently connected, or SDL_FALSE if it has not.
  263. */
  264. extern DECLSPEC SDL_bool SDLCALL SDL_JoystickGetAttached(SDL_Joystick *joystick);
  265. /**
  266. * Get the instance ID of an opened joystick or -1 if the joystick is invalid.
  267. */
  268. extern DECLSPEC SDL_JoystickID SDLCALL SDL_JoystickInstanceID(SDL_Joystick *joystick);
  269. /**
  270. * Get the number of general axis controls on a joystick.
  271. */
  272. extern DECLSPEC int SDLCALL SDL_JoystickNumAxes(SDL_Joystick *joystick);
  273. /**
  274. * Get the number of trackballs on a joystick.
  275. *
  276. * Joystick trackballs have only relative motion events associated
  277. * with them and their state cannot be polled.
  278. */
  279. extern DECLSPEC int SDLCALL SDL_JoystickNumBalls(SDL_Joystick *joystick);
  280. /**
  281. * Get the number of POV hats on a joystick.
  282. */
  283. extern DECLSPEC int SDLCALL SDL_JoystickNumHats(SDL_Joystick *joystick);
  284. /**
  285. * Get the number of buttons on a joystick.
  286. */
  287. extern DECLSPEC int SDLCALL SDL_JoystickNumButtons(SDL_Joystick *joystick);
  288. /**
  289. * Update the current state of the open joysticks.
  290. *
  291. * This is called automatically by the event loop if any joystick
  292. * events are enabled.
  293. */
  294. extern DECLSPEC void SDLCALL SDL_JoystickUpdate(void);
  295. /**
  296. * Enable/disable joystick event polling.
  297. *
  298. * If joystick events are disabled, you must call SDL_JoystickUpdate()
  299. * yourself and check the state of the joystick when you want joystick
  300. * information.
  301. *
  302. * The state can be one of ::SDL_QUERY, ::SDL_ENABLE or ::SDL_IGNORE.
  303. */
  304. extern DECLSPEC int SDLCALL SDL_JoystickEventState(int state);
  305. #define SDL_JOYSTICK_AXIS_MAX 32767
  306. #define SDL_JOYSTICK_AXIS_MIN -32768
  307. /**
  308. * Get the current state of an axis control on a joystick.
  309. *
  310. * The state is a value ranging from -32768 to 32767.
  311. *
  312. * The axis indices start at index 0.
  313. */
  314. extern DECLSPEC Sint16 SDLCALL SDL_JoystickGetAxis(SDL_Joystick *joystick,
  315. int axis);
  316. /**
  317. * Get the initial state of an axis control on a joystick.
  318. *
  319. * The state is a value ranging from -32768 to 32767.
  320. *
  321. * The axis indices start at index 0.
  322. *
  323. * \return SDL_TRUE if this axis has any initial value, or SDL_FALSE if not.
  324. */
  325. extern DECLSPEC SDL_bool SDLCALL SDL_JoystickGetAxisInitialState(SDL_Joystick *joystick,
  326. int axis, Sint16 *state);
  327. /**
  328. * \name Hat positions
  329. */
  330. /* @{ */
  331. #define SDL_HAT_CENTERED 0x00
  332. #define SDL_HAT_UP 0x01
  333. #define SDL_HAT_RIGHT 0x02
  334. #define SDL_HAT_DOWN 0x04
  335. #define SDL_HAT_LEFT 0x08
  336. #define SDL_HAT_RIGHTUP (SDL_HAT_RIGHT|SDL_HAT_UP)
  337. #define SDL_HAT_RIGHTDOWN (SDL_HAT_RIGHT|SDL_HAT_DOWN)
  338. #define SDL_HAT_LEFTUP (SDL_HAT_LEFT|SDL_HAT_UP)
  339. #define SDL_HAT_LEFTDOWN (SDL_HAT_LEFT|SDL_HAT_DOWN)
  340. /* @} */
  341. /**
  342. * Get the current state of a POV hat on a joystick.
  343. *
  344. * The hat indices start at index 0.
  345. *
  346. * \return The return value is one of the following positions:
  347. * - ::SDL_HAT_CENTERED
  348. * - ::SDL_HAT_UP
  349. * - ::SDL_HAT_RIGHT
  350. * - ::SDL_HAT_DOWN
  351. * - ::SDL_HAT_LEFT
  352. * - ::SDL_HAT_RIGHTUP
  353. * - ::SDL_HAT_RIGHTDOWN
  354. * - ::SDL_HAT_LEFTUP
  355. * - ::SDL_HAT_LEFTDOWN
  356. */
  357. extern DECLSPEC Uint8 SDLCALL SDL_JoystickGetHat(SDL_Joystick *joystick,
  358. int hat);
  359. /**
  360. * Get the ball axis change since the last poll.
  361. *
  362. * \return 0, or -1 if you passed it invalid parameters.
  363. *
  364. * The ball indices start at index 0.
  365. */
  366. extern DECLSPEC int SDLCALL SDL_JoystickGetBall(SDL_Joystick *joystick,
  367. int ball, int *dx, int *dy);
  368. /**
  369. * Get the current state of a button on a joystick.
  370. *
  371. * The button indices start at index 0.
  372. */
  373. extern DECLSPEC Uint8 SDLCALL SDL_JoystickGetButton(SDL_Joystick *joystick,
  374. int button);
  375. /**
  376. * Start a rumble effect
  377. * Each call to this function cancels any previous rumble effect, and calling it with 0 intensity stops any rumbling.
  378. *
  379. * \param joystick The joystick to vibrate
  380. * \param low_frequency_rumble The intensity of the low frequency (left) rumble motor, from 0 to 0xFFFF
  381. * \param high_frequency_rumble The intensity of the high frequency (right) rumble motor, from 0 to 0xFFFF
  382. * \param duration_ms The duration of the rumble effect, in milliseconds
  383. *
  384. * \return 0, or -1 if rumble isn't supported on this joystick
  385. */
  386. extern DECLSPEC int SDLCALL SDL_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms);
  387. /**
  388. * Start a rumble effect in the joystick's triggers
  389. * Each call to this function cancels any previous trigger rumble effect, and calling it with 0 intensity stops any rumbling.
  390. *
  391. * \param joystick The joystick to vibrate
  392. * \param left_rumble The intensity of the left trigger rumble motor, from 0 to 0xFFFF
  393. * \param right_rumble The intensity of the right trigger rumble motor, from 0 to 0xFFFF
  394. * \param duration_ms The duration of the rumble effect, in milliseconds
  395. *
  396. * \return 0, or -1 if trigger rumble isn't supported on this joystick
  397. */
  398. extern DECLSPEC int SDLCALL SDL_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms);
  399. /**
  400. * Return whether a joystick has an LED
  401. *
  402. * \param joystick The joystick to query
  403. *
  404. * \return SDL_TRUE, or SDL_FALSE if this joystick does not have a modifiable LED
  405. */
  406. extern DECLSPEC SDL_bool SDLCALL SDL_JoystickHasLED(SDL_Joystick *joystick);
  407. /**
  408. * Update a joystick's LED color.
  409. *
  410. * \param joystick The joystick to update
  411. * \param red The intensity of the red LED
  412. * \param green The intensity of the green LED
  413. * \param blue The intensity of the blue LED
  414. *
  415. * \return 0, or -1 if this joystick does not have a modifiable LED
  416. */
  417. extern DECLSPEC int SDLCALL SDL_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue);
  418. /**
  419. * Close a joystick previously opened with SDL_JoystickOpen().
  420. */
  421. extern DECLSPEC void SDLCALL SDL_JoystickClose(SDL_Joystick *joystick);
  422. /**
  423. * Return the battery level of this joystick
  424. */
  425. extern DECLSPEC SDL_JoystickPowerLevel SDLCALL SDL_JoystickCurrentPowerLevel(SDL_Joystick *joystick);
  426. /* Ends C function definitions when using C++ */
  427. #ifdef __cplusplus
  428. }
  429. #endif
  430. #include "close_code.h"
  431. #endif /* SDL_joystick_h_ */
  432. /* vi: set ts=4 sw=4 expandtab: */