SDL_xinput.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  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. #include "SDL_internal.h"
  19. #ifndef SDL_xinput_h_
  20. #define SDL_xinput_h_
  21. #include "SDL_windows.h"
  22. #ifdef HAVE_XINPUT_H
  23. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  24. // Xbox supports an XInput wrapper which is a C++-only header...
  25. #include <math.h> // Required to compile with recent MSVC...
  26. #include <XInputOnGameInput.h>
  27. using namespace XInputOnGameInput;
  28. #else
  29. #include <xinput.h>
  30. #endif
  31. #endif // HAVE_XINPUT_H
  32. #ifndef XUSER_MAX_COUNT
  33. #define XUSER_MAX_COUNT 4
  34. #endif
  35. #ifndef XUSER_INDEX_ANY
  36. #define XUSER_INDEX_ANY 0x000000FF
  37. #endif
  38. #ifndef XINPUT_CAPS_FFB_SUPPORTED
  39. #define XINPUT_CAPS_FFB_SUPPORTED 0x0001
  40. #endif
  41. #ifndef XINPUT_CAPS_WIRELESS
  42. #define XINPUT_CAPS_WIRELESS 0x0002
  43. #endif
  44. #ifndef XINPUT_DEVSUBTYPE_UNKNOWN
  45. #define XINPUT_DEVSUBTYPE_UNKNOWN 0x00
  46. #endif
  47. #ifndef XINPUT_DEVSUBTYPE_GAMEPAD
  48. #define XINPUT_DEVSUBTYPE_GAMEPAD 0x01
  49. #endif
  50. #ifndef XINPUT_DEVSUBTYPE_WHEEL
  51. #define XINPUT_DEVSUBTYPE_WHEEL 0x02
  52. #endif
  53. #ifndef XINPUT_DEVSUBTYPE_ARCADE_STICK
  54. #define XINPUT_DEVSUBTYPE_ARCADE_STICK 0x03
  55. #endif
  56. #ifndef XINPUT_DEVSUBTYPE_FLIGHT_STICK
  57. #define XINPUT_DEVSUBTYPE_FLIGHT_STICK 0x04
  58. #endif
  59. #ifndef XINPUT_DEVSUBTYPE_DANCE_PAD
  60. #define XINPUT_DEVSUBTYPE_DANCE_PAD 0x05
  61. #endif
  62. #ifndef XINPUT_DEVSUBTYPE_GUITAR
  63. #define XINPUT_DEVSUBTYPE_GUITAR 0x06
  64. #endif
  65. #ifndef XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE
  66. #define XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE 0x07
  67. #endif
  68. #ifndef XINPUT_DEVSUBTYPE_DRUM_KIT
  69. #define XINPUT_DEVSUBTYPE_DRUM_KIT 0x08
  70. #endif
  71. #ifndef XINPUT_DEVSUBTYPE_GUITAR_BASS
  72. #define XINPUT_DEVSUBTYPE_GUITAR_BASS 0x0B
  73. #endif
  74. #ifndef XINPUT_DEVSUBTYPE_ARCADE_PAD
  75. #define XINPUT_DEVSUBTYPE_ARCADE_PAD 0x13
  76. #endif
  77. #ifndef XINPUT_FLAG_GAMEPAD
  78. #define XINPUT_FLAG_GAMEPAD 0x01
  79. #endif
  80. #ifndef XINPUT_GAMEPAD_DPAD_UP
  81. #define XINPUT_GAMEPAD_DPAD_UP 0x0001
  82. #endif
  83. #ifndef XINPUT_GAMEPAD_DPAD_DOWN
  84. #define XINPUT_GAMEPAD_DPAD_DOWN 0x0002
  85. #endif
  86. #ifndef XINPUT_GAMEPAD_DPAD_LEFT
  87. #define XINPUT_GAMEPAD_DPAD_LEFT 0x0004
  88. #endif
  89. #ifndef XINPUT_GAMEPAD_DPAD_RIGHT
  90. #define XINPUT_GAMEPAD_DPAD_RIGHT 0x0008
  91. #endif
  92. #ifndef XINPUT_GAMEPAD_START
  93. #define XINPUT_GAMEPAD_START 0x0010
  94. #endif
  95. #ifndef XINPUT_GAMEPAD_BACK
  96. #define XINPUT_GAMEPAD_BACK 0x0020
  97. #endif
  98. #ifndef XINPUT_GAMEPAD_LEFT_THUMB
  99. #define XINPUT_GAMEPAD_LEFT_THUMB 0x0040
  100. #endif
  101. #ifndef XINPUT_GAMEPAD_RIGHT_THUMB
  102. #define XINPUT_GAMEPAD_RIGHT_THUMB 0x0080
  103. #endif
  104. #ifndef XINPUT_GAMEPAD_LEFT_SHOULDER
  105. #define XINPUT_GAMEPAD_LEFT_SHOULDER 0x0100
  106. #endif
  107. #ifndef XINPUT_GAMEPAD_RIGHT_SHOULDER
  108. #define XINPUT_GAMEPAD_RIGHT_SHOULDER 0x0200
  109. #endif
  110. #ifndef XINPUT_GAMEPAD_A
  111. #define XINPUT_GAMEPAD_A 0x1000
  112. #endif
  113. #ifndef XINPUT_GAMEPAD_B
  114. #define XINPUT_GAMEPAD_B 0x2000
  115. #endif
  116. #ifndef XINPUT_GAMEPAD_X
  117. #define XINPUT_GAMEPAD_X 0x4000
  118. #endif
  119. #ifndef XINPUT_GAMEPAD_Y
  120. #define XINPUT_GAMEPAD_Y 0x8000
  121. #endif
  122. #ifndef XINPUT_GAMEPAD_GUIDE
  123. #define XINPUT_GAMEPAD_GUIDE 0x0400
  124. #endif
  125. #ifndef BATTERY_DEVTYPE_GAMEPAD
  126. #define BATTERY_DEVTYPE_GAMEPAD 0x00
  127. #endif
  128. #ifndef BATTERY_TYPE_DISCONNECTED
  129. #define BATTERY_TYPE_DISCONNECTED 0x00
  130. #endif
  131. #ifndef BATTERY_TYPE_WIRED
  132. #define BATTERY_TYPE_WIRED 0x01
  133. #endif
  134. #ifndef BATTERY_TYPE_UNKNOWN
  135. #define BATTERY_TYPE_UNKNOWN 0xFF
  136. #endif
  137. #ifndef BATTERY_LEVEL_EMPTY
  138. #define BATTERY_LEVEL_EMPTY 0x00
  139. #endif
  140. #ifndef BATTERY_LEVEL_LOW
  141. #define BATTERY_LEVEL_LOW 0x01
  142. #endif
  143. #ifndef BATTERY_LEVEL_MEDIUM
  144. #define BATTERY_LEVEL_MEDIUM 0x02
  145. #endif
  146. #ifndef BATTERY_LEVEL_FULL
  147. #define BATTERY_LEVEL_FULL 0x03
  148. #endif
  149. // Set up for C function definitions, even when using C++
  150. #ifdef __cplusplus
  151. extern "C" {
  152. #endif
  153. // typedef's for XInput structs we use
  154. // This is the same as XINPUT_BATTERY_INFORMATION, but always defined instead of just if WIN32_WINNT >= _WIN32_WINNT_WIN8
  155. typedef struct
  156. {
  157. BYTE BatteryType;
  158. BYTE BatteryLevel;
  159. } XINPUT_BATTERY_INFORMATION_EX;
  160. #ifndef HAVE_XINPUT_H
  161. typedef struct
  162. {
  163. WORD wButtons;
  164. BYTE bLeftTrigger;
  165. BYTE bRightTrigger;
  166. SHORT sThumbLX;
  167. SHORT sThumbLY;
  168. SHORT sThumbRX;
  169. SHORT sThumbRY;
  170. } XINPUT_GAMEPAD;
  171. typedef struct
  172. {
  173. DWORD dwPacketNumber;
  174. XINPUT_GAMEPAD Gamepad;
  175. } XINPUT_STATE;
  176. typedef struct
  177. {
  178. WORD wLeftMotorSpeed;
  179. WORD wRightMotorSpeed;
  180. } XINPUT_VIBRATION;
  181. typedef struct
  182. {
  183. BYTE Type;
  184. BYTE SubType;
  185. WORD Flags;
  186. XINPUT_GAMEPAD Gamepad;
  187. XINPUT_VIBRATION Vibration;
  188. } XINPUT_CAPABILITIES;
  189. #endif // HAVE_XINPUT_H
  190. // This struct is not defined in XInput headers.
  191. typedef struct
  192. {
  193. XINPUT_CAPABILITIES Capabilities;
  194. WORD VendorId;
  195. WORD ProductId;
  196. WORD ProductVersion;
  197. WORD unk1;
  198. DWORD unk2;
  199. } SDL_XINPUT_CAPABILITIES_EX;
  200. // Forward decl's for XInput API's we load dynamically and use if available
  201. typedef DWORD(WINAPI *XInputGetState_t)(
  202. DWORD dwUserIndex, // [in] Index of the gamer associated with the device
  203. XINPUT_STATE *pState // [out] Receives the current state
  204. );
  205. typedef DWORD(WINAPI *XInputSetState_t)(
  206. DWORD dwUserIndex, // [in] Index of the gamer associated with the device
  207. XINPUT_VIBRATION *pVibration // [in, out] The vibration information to send to the controller
  208. );
  209. typedef DWORD(WINAPI *XInputGetCapabilities_t)(
  210. DWORD dwUserIndex, // [in] Index of the gamer associated with the device
  211. DWORD dwFlags, // [in] Input flags that identify the device type
  212. XINPUT_CAPABILITIES *pCapabilities // [out] Receives the capabilities
  213. );
  214. // Only available in XInput 1.4 that is shipped with Windows 8 and newer.
  215. typedef DWORD(WINAPI *XInputGetCapabilitiesEx_t)(
  216. DWORD dwReserved, // [in] Must be 1
  217. DWORD dwUserIndex, // [in] Index of the gamer associated with the device
  218. DWORD dwFlags, // [in] Input flags that identify the device type
  219. SDL_XINPUT_CAPABILITIES_EX *pCapabilitiesEx // [out] Receives the capabilities
  220. );
  221. typedef DWORD(WINAPI *XInputGetBatteryInformation_t)(
  222. DWORD dwUserIndex,
  223. BYTE devType,
  224. XINPUT_BATTERY_INFORMATION_EX *pBatteryInformation);
  225. extern bool WIN_LoadXInputDLL(void);
  226. extern void WIN_UnloadXInputDLL(void);
  227. extern XInputGetState_t SDL_XInputGetState;
  228. extern XInputSetState_t SDL_XInputSetState;
  229. extern XInputGetCapabilities_t SDL_XInputGetCapabilities;
  230. extern XInputGetCapabilitiesEx_t SDL_XInputGetCapabilitiesEx;
  231. extern XInputGetBatteryInformation_t SDL_XInputGetBatteryInformation;
  232. extern DWORD SDL_XInputVersion; // ((major << 16) & 0xFF00) | (minor & 0xFF)
  233. // Ends C function definitions when using C++
  234. #ifdef __cplusplus
  235. }
  236. #endif
  237. #define XINPUTGETSTATE SDL_XInputGetState
  238. #define XINPUTSETSTATE SDL_XInputSetState
  239. #define XINPUTGETCAPABILITIES SDL_XInputGetCapabilities
  240. #define XINPUTGETCAPABILITIESEX SDL_XInputGetCapabilitiesEx
  241. #define XINPUTGETBATTERYINFORMATION SDL_XInputGetBatteryInformation
  242. #endif // SDL_xinput_h_