SDL_gamecontroller.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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_JOYSTICK 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. /* The gamecontroller structure used to identify an SDL game controller */
  46. struct _SDL_GameController;
  47. typedef struct _SDL_GameController SDL_GameController;
  48. typedef enum
  49. {
  50. SDL_CONTROLLER_BINDTYPE_NONE = 0,
  51. SDL_CONTROLLER_BINDTYPE_BUTTON,
  52. SDL_CONTROLLER_BINDTYPE_AXIS,
  53. SDL_CONTROLLER_BINDTYPE_HAT
  54. } SDL_GameControllerBindType;
  55. /**
  56. * Get the SDL joystick layer binding for this controller button/axis mapping
  57. */
  58. typedef struct SDL_GameControllerButtonBind
  59. {
  60. SDL_GameControllerBindType bindType;
  61. union
  62. {
  63. int button;
  64. int axis;
  65. struct {
  66. int hat;
  67. int hat_mask;
  68. } hat;
  69. } value;
  70. } SDL_GameControllerButtonBind;
  71. /**
  72. * To count the number of game controllers in the system for the following:
  73. * int nJoysticks = SDL_NumJoysticks();
  74. * int nGameControllers = 0;
  75. * for ( int i = 0; i < nJoysticks; i++ ) {
  76. * if ( SDL_IsGameController(i) ) {
  77. * nGameControllers++;
  78. * }
  79. * }
  80. *
  81. * 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:
  82. * guid,name,mappings
  83. *
  84. * 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.
  85. * Under Windows there is a reserved GUID of "xinput" that covers any XInput devices.
  86. * The mapping format for joystick is:
  87. * bX - a joystick button, index X
  88. * hX.Y - hat X with value Y
  89. * aX - axis X of the joystick
  90. * Buttons can be used as a controller axis and vice versa.
  91. *
  92. * This string shows an example of a valid mapping for a controller
  93. * "341a3608000000000000504944564944,Afterglow PS3 Controller,a:b1,b:b2,y:b3,x:b0,start:b9,guide:b12,back:b8,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftshoulder:b4,rightshoulder:b5,leftstick:b10,rightstick:b11,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7",
  94. *
  95. */
  96. /**
  97. * Load a set of mappings from a seekable SDL data stream (memory or file), filtered by the current SDL_GetPlatform()
  98. * A community sourced database of controllers is available at https://raw.github.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt
  99. *
  100. * If \c freerw is non-zero, the stream will be closed after being read.
  101. *
  102. * \return number of mappings added, -1 on error
  103. */
  104. extern DECLSPEC int SDLCALL SDL_GameControllerAddMappingsFromRW( SDL_RWops * rw, int freerw );
  105. /**
  106. * Load a set of mappings from a file, filtered by the current SDL_GetPlatform()
  107. *
  108. * Convenience macro.
  109. */
  110. #define SDL_GameControllerAddMappingsFromFile(file) SDL_GameControllerAddMappingsFromRW(SDL_RWFromFile(file, "rb"), 1)
  111. /**
  112. * Add or update an existing mapping configuration
  113. *
  114. * \return 1 if mapping is added, 0 if updated, -1 on error
  115. */
  116. extern DECLSPEC int SDLCALL SDL_GameControllerAddMapping( const char* mappingString );
  117. /**
  118. * Get a mapping string for a GUID
  119. *
  120. * \return the mapping string. Must be freed with SDL_free. Returns NULL if no mapping is available
  121. */
  122. extern DECLSPEC char * SDLCALL SDL_GameControllerMappingForGUID( SDL_JoystickGUID guid );
  123. /**
  124. * Get a mapping string for an open GameController
  125. *
  126. * \return the mapping string. Must be freed with SDL_free. Returns NULL if no mapping is available
  127. */
  128. extern DECLSPEC char * SDLCALL SDL_GameControllerMapping( SDL_GameController * gamecontroller );
  129. /**
  130. * Is the joystick on this index supported by the game controller interface?
  131. */
  132. extern DECLSPEC SDL_bool SDLCALL SDL_IsGameController(int joystick_index);
  133. /**
  134. * Get the implementation dependent name of a game controller.
  135. * This can be called before any controllers are opened.
  136. * If no name can be found, this function returns NULL.
  137. */
  138. extern DECLSPEC const char *SDLCALL SDL_GameControllerNameForIndex(int joystick_index);
  139. /**
  140. * Open a game controller for use.
  141. * The index passed as an argument refers to the N'th game controller on the system.
  142. * This index is the value which will identify this controller in future controller
  143. * events.
  144. *
  145. * \return A controller identifier, or NULL if an error occurred.
  146. */
  147. extern DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerOpen(int joystick_index);
  148. /**
  149. * Return the name for this currently opened controller
  150. */
  151. extern DECLSPEC const char *SDLCALL SDL_GameControllerName(SDL_GameController *gamecontroller);
  152. /**
  153. * Returns SDL_TRUE if the controller has been opened and currently connected,
  154. * or SDL_FALSE if it has not.
  155. */
  156. extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerGetAttached(SDL_GameController *gamecontroller);
  157. /**
  158. * Get the underlying joystick object used by a controller
  159. */
  160. extern DECLSPEC SDL_Joystick *SDLCALL SDL_GameControllerGetJoystick(SDL_GameController *gamecontroller);
  161. /**
  162. * Enable/disable controller event polling.
  163. *
  164. * If controller events are disabled, you must call SDL_GameControllerUpdate()
  165. * yourself and check the state of the controller when you want controller
  166. * information.
  167. *
  168. * The state can be one of ::SDL_QUERY, ::SDL_ENABLE or ::SDL_IGNORE.
  169. */
  170. extern DECLSPEC int SDLCALL SDL_GameControllerEventState(int state);
  171. /**
  172. * Update the current state of the open game controllers.
  173. *
  174. * This is called automatically by the event loop if any game controller
  175. * events are enabled.
  176. */
  177. extern DECLSPEC void SDLCALL SDL_GameControllerUpdate(void);
  178. /**
  179. * The list of axes available from a controller
  180. */
  181. typedef enum
  182. {
  183. SDL_CONTROLLER_AXIS_INVALID = -1,
  184. SDL_CONTROLLER_AXIS_LEFTX,
  185. SDL_CONTROLLER_AXIS_LEFTY,
  186. SDL_CONTROLLER_AXIS_RIGHTX,
  187. SDL_CONTROLLER_AXIS_RIGHTY,
  188. SDL_CONTROLLER_AXIS_TRIGGERLEFT,
  189. SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
  190. SDL_CONTROLLER_AXIS_MAX
  191. } SDL_GameControllerAxis;
  192. /**
  193. * turn this string into a axis mapping
  194. */
  195. extern DECLSPEC SDL_GameControllerAxis SDLCALL SDL_GameControllerGetAxisFromString(const char *pchString);
  196. /**
  197. * turn this axis enum into a string mapping
  198. */
  199. extern DECLSPEC const char* SDLCALL SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis axis);
  200. /**
  201. * Get the SDL joystick layer binding for this controller button mapping
  202. */
  203. extern DECLSPEC SDL_GameControllerButtonBind SDLCALL
  204. SDL_GameControllerGetBindForAxis(SDL_GameController *gamecontroller,
  205. SDL_GameControllerAxis axis);
  206. /**
  207. * Get the current state of an axis control on a game controller.
  208. *
  209. * The state is a value ranging from -32768 to 32767.
  210. *
  211. * The axis indices start at index 0.
  212. */
  213. extern DECLSPEC Sint16 SDLCALL
  214. SDL_GameControllerGetAxis(SDL_GameController *gamecontroller,
  215. SDL_GameControllerAxis axis);
  216. /**
  217. * The list of buttons available from a controller
  218. */
  219. typedef enum
  220. {
  221. SDL_CONTROLLER_BUTTON_INVALID = -1,
  222. SDL_CONTROLLER_BUTTON_A,
  223. SDL_CONTROLLER_BUTTON_B,
  224. SDL_CONTROLLER_BUTTON_X,
  225. SDL_CONTROLLER_BUTTON_Y,
  226. SDL_CONTROLLER_BUTTON_BACK,
  227. SDL_CONTROLLER_BUTTON_GUIDE,
  228. SDL_CONTROLLER_BUTTON_START,
  229. SDL_CONTROLLER_BUTTON_LEFTSTICK,
  230. SDL_CONTROLLER_BUTTON_RIGHTSTICK,
  231. SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
  232. SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
  233. SDL_CONTROLLER_BUTTON_DPAD_UP,
  234. SDL_CONTROLLER_BUTTON_DPAD_DOWN,
  235. SDL_CONTROLLER_BUTTON_DPAD_LEFT,
  236. SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
  237. SDL_CONTROLLER_BUTTON_MAX
  238. } SDL_GameControllerButton;
  239. /**
  240. * turn this string into a button mapping
  241. */
  242. extern DECLSPEC SDL_GameControllerButton SDLCALL SDL_GameControllerGetButtonFromString(const char *pchString);
  243. /**
  244. * turn this button enum into a string mapping
  245. */
  246. extern DECLSPEC const char* SDLCALL SDL_GameControllerGetStringForButton(SDL_GameControllerButton button);
  247. /**
  248. * Get the SDL joystick layer binding for this controller button mapping
  249. */
  250. extern DECLSPEC SDL_GameControllerButtonBind SDLCALL
  251. SDL_GameControllerGetBindForButton(SDL_GameController *gamecontroller,
  252. SDL_GameControllerButton button);
  253. /**
  254. * Get the current state of a button on a game controller.
  255. *
  256. * The button indices start at index 0.
  257. */
  258. extern DECLSPEC Uint8 SDLCALL SDL_GameControllerGetButton(SDL_GameController *gamecontroller,
  259. SDL_GameControllerButton button);
  260. /**
  261. * Close a controller previously opened with SDL_GameControllerOpen().
  262. */
  263. extern DECLSPEC void SDLCALL SDL_GameControllerClose(SDL_GameController *gamecontroller);
  264. /* Ends C function definitions when using C++ */
  265. #ifdef __cplusplus
  266. }
  267. #endif
  268. #include "close_code.h"
  269. #endif /* _SDL_gamecontroller_h */
  270. /* vi: set ts=4 sw=4 expandtab: */