SDL_gamecontroller.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2013 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_joystick.h"
  28. #include "begin_code.h"
  29. /* Set up for C function definitions, even when using C++ */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /**
  34. * \file SDL_gamecontroller.h
  35. *
  36. * In order to use these functions, SDL_Init() must have been called
  37. * with the ::SDL_INIT_JOYSTICK flag. This causes SDL to scan the system
  38. * for game controllers, and load appropriate drivers.
  39. *
  40. * If you would like to receive controller updates while the application
  41. * is in the background, you should set the following hint before calling
  42. * SDL_Init(): SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
  43. */
  44. /* The gamecontroller structure used to identify an SDL game controller */
  45. struct _SDL_GameController;
  46. typedef struct _SDL_GameController SDL_GameController;
  47. typedef enum
  48. {
  49. SDL_CONTROLLER_BINDTYPE_NONE = 0,
  50. SDL_CONTROLLER_BINDTYPE_BUTTON,
  51. SDL_CONTROLLER_BINDTYPE_AXIS,
  52. SDL_CONTROLLER_BINDTYPE_HAT
  53. } SDL_GameControllerBindType;
  54. /**
  55. * Get the SDL joystick layer binding for this controller button/axis mapping
  56. */
  57. typedef struct SDL_GameControllerButtonBind
  58. {
  59. SDL_GameControllerBindType bindType;
  60. union
  61. {
  62. int button;
  63. int axis;
  64. struct {
  65. int hat;
  66. int hat_mask;
  67. } hat;
  68. } value;
  69. } SDL_GameControllerButtonBind;
  70. /**
  71. * To count the number of game controllers in the system for the following:
  72. * int nJoysticks = SDL_NumJoysticks();
  73. * int nGameControllers = 0;
  74. * for ( int i = 0; i < nJoysticks; i++ ) {
  75. * if ( SDL_IsGameController(i) ) {
  76. * nGameControllers++;
  77. * }
  78. * }
  79. *
  80. * 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:
  81. * guid,name,mappings
  82. *
  83. * 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.
  84. * Under Windows there is a reserved GUID of "xinput" that covers any XInput devices.
  85. * The mapping format for joystick is:
  86. * bX - a joystick button, index X
  87. * hX.Y - hat X with value Y
  88. * aX - axis X of the joystick
  89. * Buttons can be used as a controller axis and vice versa.
  90. *
  91. * This string shows an example of a valid mapping for a controller
  92. * "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",
  93. *
  94. */
  95. /**
  96. * Add or update an existing mapping configuration
  97. *
  98. * \return 1 if mapping is added, 0 if updated, -1 on error
  99. */
  100. extern DECLSPEC int SDLCALL SDL_GameControllerAddMapping( const char* mappingString );
  101. /**
  102. * Get a mapping string for a GUID
  103. *
  104. * \return the mapping string. Must be freed with SDL_free. Returns NULL if no mapping is available
  105. */
  106. extern DECLSPEC char * SDLCALL SDL_GameControllerMappingForGUID( SDL_JoystickGUID guid );
  107. /**
  108. * Get a mapping string for an open GameController
  109. *
  110. * \return the mapping string. Must be freed with SDL_free. Returns NULL if no mapping is available
  111. */
  112. extern DECLSPEC char * SDLCALL SDL_GameControllerMapping( SDL_GameController * gamecontroller );
  113. /**
  114. * Is the joystick on this index supported by the game controller interface?
  115. */
  116. extern DECLSPEC SDL_bool SDLCALL SDL_IsGameController(int joystick_index);
  117. /**
  118. * Get the implementation dependent name of a game controller.
  119. * This can be called before any controllers are opened.
  120. * If no name can be found, this function returns NULL.
  121. */
  122. extern DECLSPEC const char *SDLCALL SDL_GameControllerNameForIndex(int joystick_index);
  123. /**
  124. * Open a game controller for use.
  125. * The index passed as an argument refers to the N'th game controller on the system.
  126. * This index is the value which will identify this controller in future controller
  127. * events.
  128. *
  129. * \return A controller identifier, or NULL if an error occurred.
  130. */
  131. extern DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerOpen(int joystick_index);
  132. /**
  133. * Return the name for this currently opened controller
  134. */
  135. extern DECLSPEC const char *SDLCALL SDL_GameControllerName(SDL_GameController *gamecontroller);
  136. /**
  137. * Returns SDL_TRUE if the controller has been opened and currently connected,
  138. * or SDL_FALSE if it has not.
  139. */
  140. extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerGetAttached(SDL_GameController *gamecontroller);
  141. /**
  142. * Get the underlying joystick object used by a controller
  143. */
  144. extern DECLSPEC SDL_Joystick *SDLCALL SDL_GameControllerGetJoystick(SDL_GameController *gamecontroller);
  145. /**
  146. * Enable/disable controller event polling.
  147. *
  148. * If controller events are disabled, you must call SDL_GameControllerUpdate()
  149. * yourself and check the state of the controller when you want controller
  150. * information.
  151. *
  152. * The state can be one of ::SDL_QUERY, ::SDL_ENABLE or ::SDL_IGNORE.
  153. */
  154. extern DECLSPEC int SDLCALL SDL_GameControllerEventState(int state);
  155. /**
  156. * Update the current state of the open game controllers.
  157. *
  158. * This is called automatically by the event loop if any game controller
  159. * events are enabled.
  160. */
  161. extern DECLSPEC void SDLCALL SDL_GameControllerUpdate(void);
  162. /**
  163. * The list of axes available from a controller
  164. */
  165. typedef enum
  166. {
  167. SDL_CONTROLLER_AXIS_INVALID = -1,
  168. SDL_CONTROLLER_AXIS_LEFTX,
  169. SDL_CONTROLLER_AXIS_LEFTY,
  170. SDL_CONTROLLER_AXIS_RIGHTX,
  171. SDL_CONTROLLER_AXIS_RIGHTY,
  172. SDL_CONTROLLER_AXIS_TRIGGERLEFT,
  173. SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
  174. SDL_CONTROLLER_AXIS_MAX
  175. } SDL_GameControllerAxis;
  176. /**
  177. * turn this string into a axis mapping
  178. */
  179. extern DECLSPEC SDL_GameControllerAxis SDLCALL SDL_GameControllerGetAxisFromString(const char *pchString);
  180. /**
  181. * turn this axis enum into a string mapping
  182. */
  183. extern DECLSPEC const char* SDLCALL SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis axis);
  184. /**
  185. * Get the SDL joystick layer binding for this controller button mapping
  186. */
  187. extern DECLSPEC SDL_GameControllerButtonBind SDLCALL
  188. SDL_GameControllerGetBindForAxis(SDL_GameController *gamecontroller,
  189. SDL_GameControllerAxis axis);
  190. /**
  191. * Get the current state of an axis control on a game controller.
  192. *
  193. * The state is a value ranging from -32768 to 32767.
  194. *
  195. * The axis indices start at index 0.
  196. */
  197. extern DECLSPEC Sint16 SDLCALL
  198. SDL_GameControllerGetAxis(SDL_GameController *gamecontroller,
  199. SDL_GameControllerAxis axis);
  200. /**
  201. * The list of buttons available from a controller
  202. */
  203. typedef enum
  204. {
  205. SDL_CONTROLLER_BUTTON_INVALID = -1,
  206. SDL_CONTROLLER_BUTTON_A,
  207. SDL_CONTROLLER_BUTTON_B,
  208. SDL_CONTROLLER_BUTTON_X,
  209. SDL_CONTROLLER_BUTTON_Y,
  210. SDL_CONTROLLER_BUTTON_BACK,
  211. SDL_CONTROLLER_BUTTON_GUIDE,
  212. SDL_CONTROLLER_BUTTON_START,
  213. SDL_CONTROLLER_BUTTON_LEFTSTICK,
  214. SDL_CONTROLLER_BUTTON_RIGHTSTICK,
  215. SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
  216. SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
  217. SDL_CONTROLLER_BUTTON_DPAD_UP,
  218. SDL_CONTROLLER_BUTTON_DPAD_DOWN,
  219. SDL_CONTROLLER_BUTTON_DPAD_LEFT,
  220. SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
  221. SDL_CONTROLLER_BUTTON_MAX
  222. } SDL_GameControllerButton;
  223. /**
  224. * turn this string into a button mapping
  225. */
  226. extern DECLSPEC SDL_GameControllerButton SDLCALL SDL_GameControllerGetButtonFromString(const char *pchString);
  227. /**
  228. * turn this button enum into a string mapping
  229. */
  230. extern DECLSPEC const char* SDLCALL SDL_GameControllerGetStringForButton(SDL_GameControllerButton button);
  231. /**
  232. * Get the SDL joystick layer binding for this controller button mapping
  233. */
  234. extern DECLSPEC SDL_GameControllerButtonBind SDLCALL
  235. SDL_GameControllerGetBindForButton(SDL_GameController *gamecontroller,
  236. SDL_GameControllerButton button);
  237. /**
  238. * Get the current state of a button on a game controller.
  239. *
  240. * The button indices start at index 0.
  241. */
  242. extern DECLSPEC Uint8 SDLCALL SDL_GameControllerGetButton(SDL_GameController *gamecontroller,
  243. SDL_GameControllerButton button);
  244. /**
  245. * Close a controller previously opened with SDL_GameControllerOpen().
  246. */
  247. extern DECLSPEC void SDLCALL SDL_GameControllerClose(SDL_GameController *gamecontroller);
  248. /* Ends C function definitions when using C++ */
  249. #ifdef __cplusplus
  250. }
  251. #endif
  252. #include "close_code.h"
  253. #endif /* _SDL_gamecontroller_h */
  254. /* vi: set ts=4 sw=4 expandtab: */