Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

391 рядки
13 KiB

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 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_gamecontroller.h
  20. *
  21. * Include file for SDL game controller event handling
  22. */
  23. #ifndef SDL_gamecontroller_h_
  24. #define SDL_gamecontroller_h_
  25. #include "SDL_stdinc.h"
  26. #include "SDL_error.h"
  27. #include "SDL_rwops.h"
  28. #include "SDL_joystick.h"
  29. #include "begin_code.h"
  30. /* Set up for C function definitions, even when using C++ */
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /**
  35. * \file SDL_gamecontroller.h
  36. *
  37. * In order to use these functions, SDL_Init() must have been called
  38. * with the ::SDL_INIT_GAMECONTROLLER flag. This causes SDL to scan the system
  39. * for game controllers, and load appropriate drivers.
  40. *
  41. * If you would like to receive controller updates while the application
  42. * is in the background, you should set the following hint before calling
  43. * SDL_Init(): SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
  44. */
  45. /**
  46. * The gamecontroller structure used to identify an SDL game controller
  47. */
  48. struct _SDL_GameController;
  49. typedef struct _SDL_GameController SDL_GameController;
  50. typedef enum
  51. {
  52. SDL_CONTROLLER_BINDTYPE_NONE = 0,
  53. SDL_CONTROLLER_BINDTYPE_BUTTON,
  54. SDL_CONTROLLER_BINDTYPE_AXIS,
  55. SDL_CONTROLLER_BINDTYPE_HAT
  56. } SDL_GameControllerBindType;
  57. /**
  58. * Get the SDL joystick layer binding for this controller button/axis mapping
  59. */
  60. typedef struct SDL_GameControllerButtonBind
  61. {
  62. SDL_GameControllerBindType bindType;
  63. union
  64. {
  65. int button;
  66. int axis;
  67. struct {
  68. int hat;
  69. int hat_mask;
  70. } hat;
  71. } value;
  72. } SDL_GameControllerButtonBind;
  73. /**
  74. * To count the number of game controllers in the system for the following:
  75. * int nJoysticks = SDL_NumJoysticks();
  76. * int nGameControllers = 0;
  77. * for (int i = 0; i < nJoysticks; i++) {
  78. * if (SDL_IsGameController(i)) {
  79. * nGameControllers++;
  80. * }
  81. * }
  82. *
  83. * Using the SDL_HINT_GAMECONTROLLERCONFIG hint or the SDL_GameControllerAddMapping() you can add support for controllers SDL is unaware of or cause an existing controller to have a different binding. The format is:
  84. * guid,name,mappings
  85. *
  86. * Where GUID is the string value from SDL_JoystickGetGUIDString(), name is the human readable string for the device and mappings are controller mappings to joystick ones.
  87. * Under Windows there is a reserved GUID of "xinput" that covers any XInput devices.
  88. * The mapping format for joystick is:
  89. * bX - a joystick button, index X
  90. * hX.Y - hat X with value Y
  91. * aX - axis X of the joystick
  92. * Buttons can be used as a controller axis and vice versa.
  93. *
  94. * This string shows an example of a valid mapping for a controller
  95. * "03000000341a00003608000000000000,PS3 Controller,a:b1,b:b2,y:b3,x:b0,start:b9,guide:b12,back:b8,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftshoulder:b4,rightshoulder:b5,leftstick:b10,rightstick:b11,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7",
  96. *
  97. */
  98. /**
  99. * Load a set of mappings from a seekable SDL data stream (memory or file), filtered by the current SDL_GetPlatform()
  100. * A community sourced database of controllers is available at https://raw.github.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt
  101. *
  102. * If \c freerw is non-zero, the stream will be closed after being read.
  103. *
  104. * \return number of mappings added, -1 on error
  105. */
  106. extern DECLSPEC int SDLCALL SDL_GameControllerAddMappingsFromRW(SDL_RWops * rw, int freerw);
  107. /**
  108. * Load a set of mappings from a file, filtered by the current SDL_GetPlatform()
  109. *
  110. * Convenience macro.
  111. */
  112. #define SDL_GameControllerAddMappingsFromFile(file) SDL_GameControllerAddMappingsFromRW(SDL_RWFromFile(file, "rb"), 1)
  113. /**
  114. * Add or update an existing mapping configuration
  115. *
  116. * \return 1 if mapping is added, 0 if updated, -1 on error
  117. */
  118. extern DECLSPEC int SDLCALL SDL_GameControllerAddMapping(const char* mappingString);
  119. /**
  120. * Get the number of mappings installed
  121. *
  122. * \return the number of mappings
  123. */
  124. extern DECLSPEC int SDLCALL SDL_GameControllerNumMappings(void);
  125. /**
  126. * Get the mapping at a particular index.
  127. *
  128. * \return the mapping string. Must be freed with SDL_free(). Returns NULL if the index is out of range.
  129. */
  130. extern DECLSPEC char * SDLCALL SDL_GameControllerMappingForIndex(int mapping_index);
  131. /**
  132. * Get a mapping string for a GUID
  133. *
  134. * \return the mapping string. Must be freed with SDL_free(). Returns NULL if no mapping is available
  135. */
  136. extern DECLSPEC char * SDLCALL SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid);
  137. /**
  138. * Get a mapping string for an open GameController
  139. *
  140. * \return the mapping string. Must be freed with SDL_free(). Returns NULL if no mapping is available
  141. */
  142. extern DECLSPEC char * SDLCALL SDL_GameControllerMapping(SDL_GameController * gamecontroller);
  143. /**
  144. * Is the joystick on this index supported by the game controller interface?
  145. */
  146. extern DECLSPEC SDL_bool SDLCALL SDL_IsGameController(int joystick_index);
  147. /**
  148. * Get the implementation dependent name of a game controller.
  149. * This can be called before any controllers are opened.
  150. * If no name can be found, this function returns NULL.
  151. */
  152. extern DECLSPEC const char *SDLCALL SDL_GameControllerNameForIndex(int joystick_index);
  153. /**
  154. * Get the mapping of a game controller.
  155. * This can be called before any controllers are opened.
  156. *
  157. * \return the mapping string. Must be freed with SDL_free(). Returns NULL if no mapping is available
  158. */
  159. extern DECLSPEC char *SDLCALL SDL_GameControllerMappingForDeviceIndex(int joystick_index);
  160. /**
  161. * Open a game controller for use.
  162. * The index passed as an argument refers to the N'th game controller on the system.
  163. * This index is not the value which will identify this controller in future
  164. * controller events. The joystick's instance id (::SDL_JoystickID) will be
  165. * used there instead.
  166. *
  167. * \return A controller identifier, or NULL if an error occurred.
  168. */
  169. extern DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerOpen(int joystick_index);
  170. /**
  171. * Return the SDL_GameController associated with an instance id.
  172. */
  173. extern DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerFromInstanceID(SDL_JoystickID joyid);
  174. /**
  175. * Return the name for this currently opened controller
  176. */
  177. extern DECLSPEC const char *SDLCALL SDL_GameControllerName(SDL_GameController *gamecontroller);
  178. /**
  179. * Get the player index of an opened game controller, or -1 if it's not available
  180. *
  181. * For XInput controllers this returns the XInput user index.
  182. */
  183. extern DECLSPEC int SDLCALL SDL_GameControllerGetPlayerIndex(SDL_GameController *gamecontroller);
  184. /**
  185. * Get the USB vendor ID of an opened controller, if available.
  186. * If the vendor ID isn't available this function returns 0.
  187. */
  188. extern DECLSPEC Uint16 SDLCALL SDL_GameControllerGetVendor(SDL_GameController * gamecontroller);
  189. /**
  190. * Get the USB product ID of an opened controller, if available.
  191. * If the product ID isn't available this function returns 0.
  192. */
  193. extern DECLSPEC Uint16 SDLCALL SDL_GameControllerGetProduct(SDL_GameController * gamecontroller);
  194. /**
  195. * Get the product version of an opened controller, if available.
  196. * If the product version isn't available this function returns 0.
  197. */
  198. extern DECLSPEC Uint16 SDLCALL SDL_GameControllerGetProductVersion(SDL_GameController * gamecontroller);
  199. /**
  200. * Returns SDL_TRUE if the controller has been opened and currently connected,
  201. * or SDL_FALSE if it has not.
  202. */
  203. extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerGetAttached(SDL_GameController *gamecontroller);
  204. /**
  205. * Get the underlying joystick object used by a controller
  206. */
  207. extern DECLSPEC SDL_Joystick *SDLCALL SDL_GameControllerGetJoystick(SDL_GameController *gamecontroller);
  208. /**
  209. * Enable/disable controller event polling.
  210. *
  211. * If controller events are disabled, you must call SDL_GameControllerUpdate()
  212. * yourself and check the state of the controller when you want controller
  213. * information.
  214. *
  215. * The state can be one of ::SDL_QUERY, ::SDL_ENABLE or ::SDL_IGNORE.
  216. */
  217. extern DECLSPEC int SDLCALL SDL_GameControllerEventState(int state);
  218. /**
  219. * Update the current state of the open game controllers.
  220. *
  221. * This is called automatically by the event loop if any game controller
  222. * events are enabled.
  223. */
  224. extern DECLSPEC void SDLCALL SDL_GameControllerUpdate(void);
  225. /**
  226. * The list of axes available from a controller
  227. *
  228. * Thumbstick axis values range from SDL_JOYSTICK_AXIS_MIN to SDL_JOYSTICK_AXIS_MAX,
  229. * and are centered within ~8000 of zero, though advanced UI will allow users to set
  230. * or autodetect the dead zone, which varies between controllers.
  231. *
  232. * Trigger axis values range from 0 to SDL_JOYSTICK_AXIS_MAX.
  233. */
  234. typedef enum
  235. {
  236. SDL_CONTROLLER_AXIS_INVALID = -1,
  237. SDL_CONTROLLER_AXIS_LEFTX,
  238. SDL_CONTROLLER_AXIS_LEFTY,
  239. SDL_CONTROLLER_AXIS_RIGHTX,
  240. SDL_CONTROLLER_AXIS_RIGHTY,
  241. SDL_CONTROLLER_AXIS_TRIGGERLEFT,
  242. SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
  243. SDL_CONTROLLER_AXIS_MAX
  244. } SDL_GameControllerAxis;
  245. /**
  246. * turn this string into a axis mapping
  247. */
  248. extern DECLSPEC SDL_GameControllerAxis SDLCALL SDL_GameControllerGetAxisFromString(const char *pchString);
  249. /**
  250. * turn this axis enum into a string mapping
  251. */
  252. extern DECLSPEC const char* SDLCALL SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis axis);
  253. /**
  254. * Get the SDL joystick layer binding for this controller button mapping
  255. */
  256. extern DECLSPEC SDL_GameControllerButtonBind SDLCALL
  257. SDL_GameControllerGetBindForAxis(SDL_GameController *gamecontroller,
  258. SDL_GameControllerAxis axis);
  259. /**
  260. * Get the current state of an axis control on a game controller.
  261. *
  262. * The state is a value ranging from -32768 to 32767 (except for the triggers,
  263. * which range from 0 to 32767).
  264. *
  265. * The axis indices start at index 0.
  266. */
  267. extern DECLSPEC Sint16 SDLCALL
  268. SDL_GameControllerGetAxis(SDL_GameController *gamecontroller,
  269. SDL_GameControllerAxis axis);
  270. /**
  271. * The list of buttons available from a controller
  272. */
  273. typedef enum
  274. {
  275. SDL_CONTROLLER_BUTTON_INVALID = -1,
  276. SDL_CONTROLLER_BUTTON_A,
  277. SDL_CONTROLLER_BUTTON_B,
  278. SDL_CONTROLLER_BUTTON_X,
  279. SDL_CONTROLLER_BUTTON_Y,
  280. SDL_CONTROLLER_BUTTON_BACK,
  281. SDL_CONTROLLER_BUTTON_GUIDE,
  282. SDL_CONTROLLER_BUTTON_START,
  283. SDL_CONTROLLER_BUTTON_LEFTSTICK,
  284. SDL_CONTROLLER_BUTTON_RIGHTSTICK,
  285. SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
  286. SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
  287. SDL_CONTROLLER_BUTTON_DPAD_UP,
  288. SDL_CONTROLLER_BUTTON_DPAD_DOWN,
  289. SDL_CONTROLLER_BUTTON_DPAD_LEFT,
  290. SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
  291. SDL_CONTROLLER_BUTTON_MAX
  292. } SDL_GameControllerButton;
  293. /**
  294. * turn this string into a button mapping
  295. */
  296. extern DECLSPEC SDL_GameControllerButton SDLCALL SDL_GameControllerGetButtonFromString(const char *pchString);
  297. /**
  298. * turn this button enum into a string mapping
  299. */
  300. extern DECLSPEC const char* SDLCALL SDL_GameControllerGetStringForButton(SDL_GameControllerButton button);
  301. /**
  302. * Get the SDL joystick layer binding for this controller button mapping
  303. */
  304. extern DECLSPEC SDL_GameControllerButtonBind SDLCALL
  305. SDL_GameControllerGetBindForButton(SDL_GameController *gamecontroller,
  306. SDL_GameControllerButton button);
  307. /**
  308. * Get the current state of a button on a game controller.
  309. *
  310. * The button indices start at index 0.
  311. */
  312. extern DECLSPEC Uint8 SDLCALL SDL_GameControllerGetButton(SDL_GameController *gamecontroller,
  313. SDL_GameControllerButton button);
  314. /**
  315. * Trigger a rumble effect
  316. * Each call to this function cancels any previous rumble effect, and calling it with 0 intensity stops any rumbling.
  317. *
  318. * \param gamecontroller The controller to vibrate
  319. * \param low_frequency_rumble The intensity of the low frequency (left) rumble motor, from 0 to 0xFFFF
  320. * \param high_frequency_rumble The intensity of the high frequency (right) rumble motor, from 0 to 0xFFFF
  321. * \param duration_ms The duration of the rumble effect, in milliseconds
  322. *
  323. * \return 0, or -1 if rumble isn't supported on this joystick
  324. */
  325. extern DECLSPEC int SDLCALL SDL_GameControllerRumble(SDL_GameController *gamecontroller, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms);
  326. /**
  327. * Close a controller previously opened with SDL_GameControllerOpen().
  328. */
  329. extern DECLSPEC void SDLCALL SDL_GameControllerClose(SDL_GameController *gamecontroller);
  330. /* Ends C function definitions when using C++ */
  331. #ifdef __cplusplus
  332. }
  333. #endif
  334. #include "close_code.h"
  335. #endif /* SDL_gamecontroller_h_ */
  336. /* vi: set ts=4 sw=4 expandtab: */