gamecontroller.nim 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import "../sdl2"
  2. import "joystick"
  3. discard """
  4. Simple DirectMedia Layer
  5. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  6. This software is provided 'as-is', without any express or implied
  7. warranty. In no event will the authors be held liable for any damages
  8. arising from the use of this software.
  9. Permission is granted to anyone to use this software for any purpose,
  10. including commercial applications, and to alter it and redistribute it
  11. freely, subject to the following restrictions:
  12. 1. The origin of this software must not be misrepresented; you must not
  13. claim that you wrote the original software. If you use this software
  14. in a product, an acknowledgment in the product documentation would be
  15. appreciated but is not required.
  16. 2. Altered source versions must be plainly marked as such, and must not be
  17. misrepresented as being the original software.
  18. 3. This notice may not be removed or altered from any source distribution.
  19. \file SDL_gamecontroller.h
  20. Include file for SDL game controller event handling
  21. """
  22. ##
  23. # \file SDL_gamecontroller.h
  24. #
  25. # In order to use these functions, SDL_Init() must have been called
  26. # with the ::SDL_INIT_JOYSTICK flag. This causes SDL to scan the system
  27. # for game controllers, and load appropriate drivers.
  28. #
  29. # If you would like to receive controller updates while the application
  30. # is in the background, you should set the following hint before calling
  31. # SDL_Init(): SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
  32. #
  33. # The gamecontroller structure used to identify an SDL game controller# /
  34. type
  35. GameController* = object
  36. GameControllerPtr* = ptr GameController
  37. GameControllerBindType* {.size: sizeof(cint).} = enum
  38. SDL_CONTROLLER_BINDTYPE_NONE,
  39. SDL_CONTROLLER_BINDTYPE_BUTTON,
  40. SDL_CONTROLLER_BINDTYPE_AXIS,
  41. SDL_CONTROLLER_BINDTYPE_HAT
  42. # Get the SDL joystick layer binding for this controller button/axis mapping
  43. type
  44. GameControllerButtonBind* = object
  45. case bindType*: GameControllerBindType
  46. of SDL_CONTROLLER_BINDTYPE_NONE:
  47. nil
  48. of SDL_CONTROLLER_BINDTYPE_BUTTON:
  49. button*: cint
  50. of SDL_CONTROLLER_BINDTYPE_AXIS:
  51. axis*: cint
  52. of SDL_CONTROLLER_BINDTYPE_HAT:
  53. hat*, hatMask*: cint
  54. when defined(SDL_Static):
  55. static: echo "SDL_Static option is deprecated and will soon be removed. Instead please use --dynlibOverride:SDL2."
  56. else:
  57. {.push callConv: cdecl, dynlib: LibName.}
  58. ##
  59. # To count the number of game controllers in the system for the following:
  60. # int nJoysticks = SDL_NumJoysticks();
  61. # int nGameControllers = 0;
  62. # for ( int i = 0; i < nJoysticks; i++ ) {
  63. # if ( SDL_IsGameController(i) ) {
  64. # nGameControllers++;
  65. # }
  66. # }
  67. #
  68. # 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:
  69. # guid,name,mappings
  70. #
  71. # 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.
  72. # Under Windows there is a reserved GUID of "xinput" that covers any XInput devices.
  73. # The mapping format for joystick is:
  74. # bX - a joystick button, index X
  75. # hX.Y - hat X with value Y
  76. # aX - axis X of the joystick
  77. # Buttons can be used as a controller axis and vice versa.
  78. #
  79. # This string shows an example of a valid mapping for a controller
  80. # "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",
  81. #
  82. # /
  83. ##
  84. # Add or update an existing mapping configuration
  85. #
  86. # \return 1 if mapping is added, 0 if updated, -1 on error
  87. # /
  88. proc gameControllerAddMapping* (mappingString: cstring): cint {.
  89. importc: "SDL_GameControllerAddMapping".}
  90. ##
  91. # Get a mapping string for a GUID
  92. #
  93. # \return the mapping string. Must be freed with SDL_free. Returns NULL if no mapping is available
  94. # /
  95. proc gameControllerMappingForGUID* (guid: JoystickGuid): cstring {.
  96. importc: "SDL_GameControllerMappingForGUID".}
  97. ##
  98. # Get a mapping string for an open GameController
  99. #
  100. # \return the mapping string. Must be freed with SDL_free. Returns NULL if no mapping is available
  101. # /
  102. proc mapping* (gameController: GameControllerPtr): cstring {.
  103. importc: "SDL_GameControllerMapping".}
  104. ##
  105. # Is the joystick on this index supported by the game controller interface?
  106. # /
  107. proc isGameController* (joystickIndex: cint): Bool32 {.
  108. importc: "SDL_IsGameController".}
  109. ##
  110. # Get the implementation dependent name of a game controller.
  111. # This can be called before any controllers are opened.
  112. # If no name can be found, this function returns NULL.
  113. # /
  114. proc gameControllerNameForIndex* (joystickIndex: cint): cstring {.
  115. importc: "SDL_GameControllerNameForIndex".}
  116. ##
  117. # Open a game controller for use.
  118. # The index passed as an argument refers to the N'th game controller on the system.
  119. # This index is the value which will identify this controller in future controller
  120. # events.
  121. #
  122. # \return A controller identifier, or NULL if an error occurred.
  123. # /
  124. proc gameControllerOpen* (joystickIndex: cint): GameControllerPtr {.
  125. importc: "SDL_GameControllerOpen".}
  126. ##
  127. # Return the name for this currently opened controller
  128. # /
  129. proc name* (gameController: GameControllerPtr): cstring {.
  130. importc: "SDL_GameControllerName".}
  131. ##
  132. # Returns SDL_TRUE if the controller has been opened and currently connected,
  133. # or SDL_FALSE if it has not.
  134. # /
  135. proc getAttached* (gameController: GameControllerPtr): Bool32 {.
  136. importc: "SDL_GameControllerGetAttached".}
  137. ##
  138. # Get the underlying joystick object used by a controller
  139. # /
  140. proc getJoystick* (gameController: GameControllerPtr): JoystickPtr {.
  141. importc: "SDL_GameControllerGetJoystick".}
  142. ##
  143. # Enable/disable controller event polling.
  144. #
  145. # If controller events are disabled, you must call SDL_GameControllerUpdate()
  146. # yourself and check the state of the controller when you want controller
  147. # information.
  148. #
  149. # The state can be one of ::SDL_QUERY, ::SDL_ENABLE or ::SDL_IGNORE.
  150. # /
  151. proc gameControllerEventState* (state: cint): cint {.
  152. importc: "SDL_GameControllerEventState".}
  153. ##
  154. # Update the current state of the open game controllers.
  155. #
  156. # This is called automatically by the event loop if any game controller
  157. # events are enabled.
  158. # /
  159. proc gameControllerUpdate* () {.
  160. importc: "SDL_GameControllerUpdate".}
  161. ##
  162. # The list of axes available from a controller
  163. # /
  164. type
  165. GameControllerAxis* {.size: sizeof(cint).} = enum
  166. SDL_CONTROLLER_AXIS_INVALID = -1,
  167. SDL_CONTROLLER_AXIS_LEFTX,
  168. SDL_CONTROLLER_AXIS_LEFTY,
  169. SDL_CONTROLLER_AXIS_RIGHTX,
  170. SDL_CONTROLLER_AXIS_RIGHTY,
  171. SDL_CONTROLLER_AXIS_TRIGGERLEFT,
  172. SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
  173. SDL_CONTROLLER_AXIS_MAX
  174. converter toInt* (some: GameControllerAxis): uint8 = uint8(some)
  175. ##
  176. # turn this string into a axis mapping
  177. # /
  178. proc gameControllerGetAxisFromString* (pchString: cstring): GameControllerAxis {.
  179. importc: "SDL_GameControllerGetAxisFromString".}
  180. ##
  181. # turn this axis enum into a string mapping
  182. # /
  183. proc gameControllerGetStringForAxis* (axis: GameControllerAxis): cstring {.
  184. importc: "SDL_GameControllerGetStringForAxis".}
  185. ##
  186. # Get the SDL joystick layer binding for this controller button mapping
  187. # /
  188. proc getBindForAxis* (gameController: GameControllerPtr, axis: GameControllerAxis): GameControllerButtonBind {.
  189. importc: "SDL_GameControllerGetBindForAxis".}
  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. proc getAxis* (gameController: GameControllerPtr, axis: GameControllerAxis): int16 {.
  198. importc: "SDL_GameControllerGetAxis".}
  199. ##
  200. # The list of buttons available from a controller
  201. # /
  202. type
  203. GameControllerButton* {.size: sizeof(cint).} = enum
  204. SDL_CONTROLLER_BUTTON_INVALID = -1,
  205. SDL_CONTROLLER_BUTTON_A,
  206. SDL_CONTROLLER_BUTTON_B,
  207. SDL_CONTROLLER_BUTTON_X,
  208. SDL_CONTROLLER_BUTTON_Y,
  209. SDL_CONTROLLER_BUTTON_BACK,
  210. SDL_CONTROLLER_BUTTON_GUIDE,
  211. SDL_CONTROLLER_BUTTON_START,
  212. SDL_CONTROLLER_BUTTON_LEFTSTICK,
  213. SDL_CONTROLLER_BUTTON_RIGHTSTICK,
  214. SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
  215. SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
  216. SDL_CONTROLLER_BUTTON_DPAD_UP,
  217. SDL_CONTROLLER_BUTTON_DPAD_DOWN,
  218. SDL_CONTROLLER_BUTTON_DPAD_LEFT,
  219. SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
  220. SDL_CONTROLLER_BUTTON_MAX
  221. converter toInt* (some: GameControllerButton): uint8 = uint8(some)
  222. ##
  223. # turn this string into a button mapping
  224. # /
  225. proc gameControllerGetButtonFromString* (pchString: cstring): GameControllerButton {.
  226. importc: "SDL_GameControllerGetButtonFromString".}
  227. ##
  228. # turn this button enum into a string mapping
  229. # /
  230. proc gameControllerGetStringForButton* (button: GameControllerButton): cstring {.
  231. importc: "SDL_GameControllerGetStringForButton".}
  232. ##
  233. # Get the SDL joystick layer binding for this controller button mapping
  234. # /
  235. proc getBindForButton* (gameController: GameControllerPtr, button: GameControllerButton): GameControllerButtonBind {.
  236. importc: "SDL_GameControllerGetBindForButton".}
  237. ##
  238. # Get the current state of a button on a game controller.
  239. #
  240. # The button indices start at index 0.
  241. # /
  242. proc getButton* (gameController: GameControllerPtr, button: GameControllerButton): uint8 {.
  243. importc: "SDL_GameControllerGetButton".}
  244. ##
  245. # Close a controller previously opened with SDL_GameControllerOpen().
  246. # /
  247. proc close* (gameController: GameControllerPtr) {.
  248. importc: "SDL_GameControllerClose".}
  249. when not defined(SDL_Static):
  250. {.pop.}