isteamcontroller.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // This file is provided under The MIT License as part of Steamworks.NET.
  2. // Copyright (c) 2013-2019 Riley Labrecque
  3. // Please see the included LICENSE.txt for additional information.
  4. // This file is automatically generated.
  5. // Changes to this file will be reverted when you update Steamworks.NET
  6. #if UNITY_ANDROID || UNITY_IOS || UNITY_TIZEN || UNITY_TVOS || UNITY_WEBGL || UNITY_WSA || UNITY_PS4 || UNITY_WII || UNITY_XBOXONE || UNITY_SWITCH
  7. #define DISABLESTEAMWORKS
  8. #endif
  9. #if !DISABLESTEAMWORKS
  10. using System.Runtime.InteropServices;
  11. using IntPtr = System.IntPtr;
  12. namespace Steamworks {
  13. public static class SteamController {
  14. /// <summary>
  15. /// <para> Init and Shutdown must be called when starting/ending use of this interface</para>
  16. /// </summary>
  17. public static bool Init() {
  18. InteropHelp.TestIfAvailableClient();
  19. return NativeMethods.ISteamController_Init(CSteamAPIContext.GetSteamController());
  20. }
  21. public static bool Shutdown() {
  22. InteropHelp.TestIfAvailableClient();
  23. return NativeMethods.ISteamController_Shutdown(CSteamAPIContext.GetSteamController());
  24. }
  25. /// <summary>
  26. /// <para> Synchronize API state with the latest Steam Controller inputs available. This</para>
  27. /// <para> is performed automatically by SteamAPI_RunCallbacks, but for the absolute lowest</para>
  28. /// <para> possible latency, you call this directly before reading controller state. This must</para>
  29. /// <para> be called from somewhere before GetConnectedControllers will return any handles</para>
  30. /// </summary>
  31. public static void RunFrame() {
  32. InteropHelp.TestIfAvailableClient();
  33. NativeMethods.ISteamController_RunFrame(CSteamAPIContext.GetSteamController());
  34. }
  35. /// <summary>
  36. /// <para> Enumerate currently connected controllers</para>
  37. /// <para> handlesOut should point to a STEAM_CONTROLLER_MAX_COUNT sized array of ControllerHandle_t handles</para>
  38. /// <para> Returns the number of handles written to handlesOut</para>
  39. /// </summary>
  40. public static int GetConnectedControllers(ControllerHandle_t[] handlesOut) {
  41. InteropHelp.TestIfAvailableClient();
  42. if (handlesOut != null && handlesOut.Length != Constants.STEAM_CONTROLLER_MAX_COUNT) {
  43. throw new System.ArgumentException("handlesOut must be the same size as Constants.STEAM_CONTROLLER_MAX_COUNT!");
  44. }
  45. return NativeMethods.ISteamController_GetConnectedControllers(CSteamAPIContext.GetSteamController(), handlesOut);
  46. }
  47. /// <summary>
  48. /// <para>-----------------------------------------------------------------------------</para>
  49. /// <para> ACTION SETS</para>
  50. /// <para>-----------------------------------------------------------------------------</para>
  51. /// <para> Lookup the handle for an Action Set. Best to do this once on startup, and store the handles for all future API calls.</para>
  52. /// </summary>
  53. public static ControllerActionSetHandle_t GetActionSetHandle(string pszActionSetName) {
  54. InteropHelp.TestIfAvailableClient();
  55. using (var pszActionSetName2 = new InteropHelp.UTF8StringHandle(pszActionSetName)) {
  56. return (ControllerActionSetHandle_t)NativeMethods.ISteamController_GetActionSetHandle(CSteamAPIContext.GetSteamController(), pszActionSetName2);
  57. }
  58. }
  59. /// <summary>
  60. /// <para> Reconfigure the controller to use the specified action set (ie 'Menu', 'Walk' or 'Drive')</para>
  61. /// <para> This is cheap, and can be safely called repeatedly. It's often easier to repeatedly call it in</para>
  62. /// <para> your state loops, instead of trying to place it in all of your state transitions.</para>
  63. /// </summary>
  64. public static void ActivateActionSet(ControllerHandle_t controllerHandle, ControllerActionSetHandle_t actionSetHandle) {
  65. InteropHelp.TestIfAvailableClient();
  66. NativeMethods.ISteamController_ActivateActionSet(CSteamAPIContext.GetSteamController(), controllerHandle, actionSetHandle);
  67. }
  68. public static ControllerActionSetHandle_t GetCurrentActionSet(ControllerHandle_t controllerHandle) {
  69. InteropHelp.TestIfAvailableClient();
  70. return (ControllerActionSetHandle_t)NativeMethods.ISteamController_GetCurrentActionSet(CSteamAPIContext.GetSteamController(), controllerHandle);
  71. }
  72. /// <summary>
  73. /// <para> ACTION SET LAYERS</para>
  74. /// </summary>
  75. public static void ActivateActionSetLayer(ControllerHandle_t controllerHandle, ControllerActionSetHandle_t actionSetLayerHandle) {
  76. InteropHelp.TestIfAvailableClient();
  77. NativeMethods.ISteamController_ActivateActionSetLayer(CSteamAPIContext.GetSteamController(), controllerHandle, actionSetLayerHandle);
  78. }
  79. public static void DeactivateActionSetLayer(ControllerHandle_t controllerHandle, ControllerActionSetHandle_t actionSetLayerHandle) {
  80. InteropHelp.TestIfAvailableClient();
  81. NativeMethods.ISteamController_DeactivateActionSetLayer(CSteamAPIContext.GetSteamController(), controllerHandle, actionSetLayerHandle);
  82. }
  83. public static void DeactivateAllActionSetLayers(ControllerHandle_t controllerHandle) {
  84. InteropHelp.TestIfAvailableClient();
  85. NativeMethods.ISteamController_DeactivateAllActionSetLayers(CSteamAPIContext.GetSteamController(), controllerHandle);
  86. }
  87. /// <summary>
  88. /// <para> Enumerate currently active layers</para>
  89. /// <para> handlesOut should point to a STEAM_CONTROLLER_MAX_ACTIVE_LAYERS sized array of ControllerActionSetHandle_t handles.</para>
  90. /// <para> Returns the number of handles written to handlesOut</para>
  91. /// </summary>
  92. public static int GetActiveActionSetLayers(ControllerHandle_t controllerHandle, ControllerActionSetHandle_t[] handlesOut) {
  93. InteropHelp.TestIfAvailableClient();
  94. if (handlesOut != null && handlesOut.Length != Constants.STEAM_CONTROLLER_MAX_ACTIVE_LAYERS) {
  95. throw new System.ArgumentException("handlesOut must be the same size as Constants.STEAM_CONTROLLER_MAX_ACTIVE_LAYERS!");
  96. }
  97. return NativeMethods.ISteamController_GetActiveActionSetLayers(CSteamAPIContext.GetSteamController(), controllerHandle, handlesOut);
  98. }
  99. /// <summary>
  100. /// <para>-----------------------------------------------------------------------------</para>
  101. /// <para> ACTIONS</para>
  102. /// <para>-----------------------------------------------------------------------------</para>
  103. /// <para> Lookup the handle for a digital action. Best to do this once on startup, and store the handles for all future API calls.</para>
  104. /// </summary>
  105. public static ControllerDigitalActionHandle_t GetDigitalActionHandle(string pszActionName) {
  106. InteropHelp.TestIfAvailableClient();
  107. using (var pszActionName2 = new InteropHelp.UTF8StringHandle(pszActionName)) {
  108. return (ControllerDigitalActionHandle_t)NativeMethods.ISteamController_GetDigitalActionHandle(CSteamAPIContext.GetSteamController(), pszActionName2);
  109. }
  110. }
  111. /// <summary>
  112. /// <para> Returns the current state of the supplied digital game action</para>
  113. /// </summary>
  114. public static ControllerDigitalActionData_t GetDigitalActionData(ControllerHandle_t controllerHandle, ControllerDigitalActionHandle_t digitalActionHandle) {
  115. InteropHelp.TestIfAvailableClient();
  116. return NativeMethods.ISteamController_GetDigitalActionData(CSteamAPIContext.GetSteamController(), controllerHandle, digitalActionHandle);
  117. }
  118. /// <summary>
  119. /// <para> Get the origin(s) for a digital action within an action set. Returns the number of origins supplied in originsOut. Use this to display the appropriate on-screen prompt for the action.</para>
  120. /// <para> originsOut should point to a STEAM_CONTROLLER_MAX_ORIGINS sized array of EControllerActionOrigin handles. The EControllerActionOrigin enum will get extended as support for new controller controllers gets added to</para>
  121. /// <para> the Steam client and will exceed the values from this header, please check bounds if you are using a look up table.</para>
  122. /// </summary>
  123. public static int GetDigitalActionOrigins(ControllerHandle_t controllerHandle, ControllerActionSetHandle_t actionSetHandle, ControllerDigitalActionHandle_t digitalActionHandle, EControllerActionOrigin[] originsOut) {
  124. InteropHelp.TestIfAvailableClient();
  125. if (originsOut != null && originsOut.Length != Constants.STEAM_CONTROLLER_MAX_ORIGINS) {
  126. throw new System.ArgumentException("originsOut must be the same size as Constants.STEAM_CONTROLLER_MAX_ORIGINS!");
  127. }
  128. return NativeMethods.ISteamController_GetDigitalActionOrigins(CSteamAPIContext.GetSteamController(), controllerHandle, actionSetHandle, digitalActionHandle, originsOut);
  129. }
  130. /// <summary>
  131. /// <para> Lookup the handle for an analog action. Best to do this once on startup, and store the handles for all future API calls.</para>
  132. /// </summary>
  133. public static ControllerAnalogActionHandle_t GetAnalogActionHandle(string pszActionName) {
  134. InteropHelp.TestIfAvailableClient();
  135. using (var pszActionName2 = new InteropHelp.UTF8StringHandle(pszActionName)) {
  136. return (ControllerAnalogActionHandle_t)NativeMethods.ISteamController_GetAnalogActionHandle(CSteamAPIContext.GetSteamController(), pszActionName2);
  137. }
  138. }
  139. /// <summary>
  140. /// <para> Returns the current state of these supplied analog game action</para>
  141. /// </summary>
  142. public static ControllerAnalogActionData_t GetAnalogActionData(ControllerHandle_t controllerHandle, ControllerAnalogActionHandle_t analogActionHandle) {
  143. InteropHelp.TestIfAvailableClient();
  144. return NativeMethods.ISteamController_GetAnalogActionData(CSteamAPIContext.GetSteamController(), controllerHandle, analogActionHandle);
  145. }
  146. /// <summary>
  147. /// <para> Get the origin(s) for an analog action within an action set. Returns the number of origins supplied in originsOut. Use this to display the appropriate on-screen prompt for the action.</para>
  148. /// <para> originsOut should point to a STEAM_CONTROLLER_MAX_ORIGINS sized array of EControllerActionOrigin handles. The EControllerActionOrigin enum will get extended as support for new controller controllers gets added to</para>
  149. /// <para> the Steam client and will exceed the values from this header, please check bounds if you are using a look up table.</para>
  150. /// </summary>
  151. public static int GetAnalogActionOrigins(ControllerHandle_t controllerHandle, ControllerActionSetHandle_t actionSetHandle, ControllerAnalogActionHandle_t analogActionHandle, EControllerActionOrigin[] originsOut) {
  152. InteropHelp.TestIfAvailableClient();
  153. if (originsOut != null && originsOut.Length != Constants.STEAM_CONTROLLER_MAX_ORIGINS) {
  154. throw new System.ArgumentException("originsOut must be the same size as Constants.STEAM_CONTROLLER_MAX_ORIGINS!");
  155. }
  156. return NativeMethods.ISteamController_GetAnalogActionOrigins(CSteamAPIContext.GetSteamController(), controllerHandle, actionSetHandle, analogActionHandle, originsOut);
  157. }
  158. /// <summary>
  159. /// <para> Get a local path to art for on-screen glyph for a particular origin - this call is cheap</para>
  160. /// </summary>
  161. public static string GetGlyphForActionOrigin(EControllerActionOrigin eOrigin) {
  162. InteropHelp.TestIfAvailableClient();
  163. return InteropHelp.PtrToStringUTF8(NativeMethods.ISteamController_GetGlyphForActionOrigin(CSteamAPIContext.GetSteamController(), eOrigin));
  164. }
  165. /// <summary>
  166. /// <para> Returns a localized string (from Steam's language setting) for the specified origin - this call is serialized</para>
  167. /// </summary>
  168. public static string GetStringForActionOrigin(EControllerActionOrigin eOrigin) {
  169. InteropHelp.TestIfAvailableClient();
  170. return InteropHelp.PtrToStringUTF8(NativeMethods.ISteamController_GetStringForActionOrigin(CSteamAPIContext.GetSteamController(), eOrigin));
  171. }
  172. public static void StopAnalogActionMomentum(ControllerHandle_t controllerHandle, ControllerAnalogActionHandle_t eAction) {
  173. InteropHelp.TestIfAvailableClient();
  174. NativeMethods.ISteamController_StopAnalogActionMomentum(CSteamAPIContext.GetSteamController(), controllerHandle, eAction);
  175. }
  176. /// <summary>
  177. /// <para> Returns raw motion data from the specified controller</para>
  178. /// </summary>
  179. public static ControllerMotionData_t GetMotionData(ControllerHandle_t controllerHandle) {
  180. InteropHelp.TestIfAvailableClient();
  181. return NativeMethods.ISteamController_GetMotionData(CSteamAPIContext.GetSteamController(), controllerHandle);
  182. }
  183. /// <summary>
  184. /// <para>-----------------------------------------------------------------------------</para>
  185. /// <para> OUTPUTS</para>
  186. /// <para>-----------------------------------------------------------------------------</para>
  187. /// <para> Trigger a haptic pulse on a controller</para>
  188. /// </summary>
  189. public static void TriggerHapticPulse(ControllerHandle_t controllerHandle, ESteamControllerPad eTargetPad, ushort usDurationMicroSec) {
  190. InteropHelp.TestIfAvailableClient();
  191. NativeMethods.ISteamController_TriggerHapticPulse(CSteamAPIContext.GetSteamController(), controllerHandle, eTargetPad, usDurationMicroSec);
  192. }
  193. /// <summary>
  194. /// <para> Trigger a pulse with a duty cycle of usDurationMicroSec / usOffMicroSec, unRepeat times.</para>
  195. /// <para> nFlags is currently unused and reserved for future use.</para>
  196. /// </summary>
  197. public static void TriggerRepeatedHapticPulse(ControllerHandle_t controllerHandle, ESteamControllerPad eTargetPad, ushort usDurationMicroSec, ushort usOffMicroSec, ushort unRepeat, uint nFlags) {
  198. InteropHelp.TestIfAvailableClient();
  199. NativeMethods.ISteamController_TriggerRepeatedHapticPulse(CSteamAPIContext.GetSteamController(), controllerHandle, eTargetPad, usDurationMicroSec, usOffMicroSec, unRepeat, nFlags);
  200. }
  201. /// <summary>
  202. /// <para> Trigger a vibration event on supported controllers.</para>
  203. /// </summary>
  204. public static void TriggerVibration(ControllerHandle_t controllerHandle, ushort usLeftSpeed, ushort usRightSpeed) {
  205. InteropHelp.TestIfAvailableClient();
  206. NativeMethods.ISteamController_TriggerVibration(CSteamAPIContext.GetSteamController(), controllerHandle, usLeftSpeed, usRightSpeed);
  207. }
  208. /// <summary>
  209. /// <para> Set the controller LED color on supported controllers.</para>
  210. /// </summary>
  211. public static void SetLEDColor(ControllerHandle_t controllerHandle, byte nColorR, byte nColorG, byte nColorB, uint nFlags) {
  212. InteropHelp.TestIfAvailableClient();
  213. NativeMethods.ISteamController_SetLEDColor(CSteamAPIContext.GetSteamController(), controllerHandle, nColorR, nColorG, nColorB, nFlags);
  214. }
  215. /// <summary>
  216. /// <para>-----------------------------------------------------------------------------</para>
  217. /// <para> Utility functions availible without using the rest of Steam Input API</para>
  218. /// <para>-----------------------------------------------------------------------------</para>
  219. /// <para> Invokes the Steam overlay and brings up the binding screen if the user is using Big Picture Mode</para>
  220. /// <para> If the user is not in Big Picture Mode it will open up the binding in a new window</para>
  221. /// </summary>
  222. public static bool ShowBindingPanel(ControllerHandle_t controllerHandle) {
  223. InteropHelp.TestIfAvailableClient();
  224. return NativeMethods.ISteamController_ShowBindingPanel(CSteamAPIContext.GetSteamController(), controllerHandle);
  225. }
  226. /// <summary>
  227. /// <para> Returns the input type for a particular handle</para>
  228. /// </summary>
  229. public static ESteamInputType GetInputTypeForHandle(ControllerHandle_t controllerHandle) {
  230. InteropHelp.TestIfAvailableClient();
  231. return NativeMethods.ISteamController_GetInputTypeForHandle(CSteamAPIContext.GetSteamController(), controllerHandle);
  232. }
  233. /// <summary>
  234. /// <para> Returns the associated controller handle for the specified emulated gamepad - can be used with the above 2 functions</para>
  235. /// <para> to identify controllers presented to your game over Xinput. Returns 0 if the Xinput index isn't associated with Steam Input</para>
  236. /// </summary>
  237. public static ControllerHandle_t GetControllerForGamepadIndex(int nIndex) {
  238. InteropHelp.TestIfAvailableClient();
  239. return (ControllerHandle_t)NativeMethods.ISteamController_GetControllerForGamepadIndex(CSteamAPIContext.GetSteamController(), nIndex);
  240. }
  241. /// <summary>
  242. /// <para> Returns the associated gamepad index for the specified controller, if emulating a gamepad or -1 if not associated with an Xinput index</para>
  243. /// </summary>
  244. public static int GetGamepadIndexForController(ControllerHandle_t ulControllerHandle) {
  245. InteropHelp.TestIfAvailableClient();
  246. return NativeMethods.ISteamController_GetGamepadIndexForController(CSteamAPIContext.GetSteamController(), ulControllerHandle);
  247. }
  248. /// <summary>
  249. /// <para> Returns a localized string (from Steam's language setting) for the specified Xbox controller origin.</para>
  250. /// </summary>
  251. public static string GetStringForXboxOrigin(EXboxOrigin eOrigin) {
  252. InteropHelp.TestIfAvailableClient();
  253. return InteropHelp.PtrToStringUTF8(NativeMethods.ISteamController_GetStringForXboxOrigin(CSteamAPIContext.GetSteamController(), eOrigin));
  254. }
  255. /// <summary>
  256. /// <para> Get a local path to art for on-screen glyph for a particular Xbox controller origin.</para>
  257. /// </summary>
  258. public static string GetGlyphForXboxOrigin(EXboxOrigin eOrigin) {
  259. InteropHelp.TestIfAvailableClient();
  260. return InteropHelp.PtrToStringUTF8(NativeMethods.ISteamController_GetGlyphForXboxOrigin(CSteamAPIContext.GetSteamController(), eOrigin));
  261. }
  262. /// <summary>
  263. /// <para> Get the equivalent ActionOrigin for a given Xbox controller origin this can be chained with GetGlyphForActionOrigin to provide future proof glyphs for</para>
  264. /// <para> non-Steam Input API action games. Note - this only translates the buttons directly and doesn't take into account any remapping a user has made in their configuration</para>
  265. /// </summary>
  266. public static EControllerActionOrigin GetActionOriginFromXboxOrigin(ControllerHandle_t controllerHandle, EXboxOrigin eOrigin) {
  267. InteropHelp.TestIfAvailableClient();
  268. return NativeMethods.ISteamController_GetActionOriginFromXboxOrigin(CSteamAPIContext.GetSteamController(), controllerHandle, eOrigin);
  269. }
  270. /// <summary>
  271. /// <para> Convert an origin to another controller type - for inputs not present on the other controller type this will return k_EControllerActionOrigin_None</para>
  272. /// </summary>
  273. public static EControllerActionOrigin TranslateActionOrigin(ESteamInputType eDestinationInputType, EControllerActionOrigin eSourceOrigin) {
  274. InteropHelp.TestIfAvailableClient();
  275. return NativeMethods.ISteamController_TranslateActionOrigin(CSteamAPIContext.GetSteamController(), eDestinationInputType, eSourceOrigin);
  276. }
  277. /// <summary>
  278. /// <para> Get the binding revision for a given device. Returns false if the handle was not valid or if a mapping is not yet loaded for the device</para>
  279. /// </summary>
  280. public static bool GetControllerBindingRevision(ControllerHandle_t controllerHandle, out int pMajor, out int pMinor) {
  281. InteropHelp.TestIfAvailableClient();
  282. return NativeMethods.ISteamController_GetControllerBindingRevision(CSteamAPIContext.GetSteamController(), controllerHandle, out pMajor, out pMinor);
  283. }
  284. }
  285. }
  286. #endif // !DISABLESTEAMWORKS