forked from FoxCouncil/Steamworks.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SteamController.cs
369 lines (268 loc) · 16.8 KB
/
SteamController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// !! // Steamworks.Core - SteamController.cs
// *.-". // Created: 2016-10-18 [7:51 PM]
// | | // Copyright 2016 // MIT License // The Fox Council
// Modified by: Fox Diller on 2016-10-22 @ 3:03 PM
#region Usings
using System;
using System.Runtime.InteropServices;
#endregion
namespace Steamworks.Core
{
public class SteamController : ISteamController
{
private readonly IntPtr m_instancePtr;
public SteamController(IntPtr c_instancePtr)
{
m_instancePtr = c_instancePtr;
}
private void CheckIfUsable()
{
if (m_instancePtr == IntPtr.Zero)
{
throw new InvalidOperationException("Steam Controller Not Initialized!");
}
}
#region Constants
public const int STEAM_CONTROLLER_MAX_COUNT = 16;
public const int STEAM_CONTROLLER_MAX_ANALOG_ACTIONS = 16;
public const int STEAM_CONTROLLER_MAX_DIGITAL_ACTIONS = 128;
public const int STEAM_CONTROLLER_MAX_ORIGINS = 8;
public const ulong STEAM_CONTROLLER_HANDLE_ALL_CONTROLLERS = ulong.MaxValue;
public const float STEAM_CONTROLLER_MIN_ANALOG_ACTION_DATA = -1.0f;
public const float STEAM_CONTROLLER_MAX_ANALOG_ACTION_DATA = 1.0f;
#endregion
#region Native Methods
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_Init")]
private static extern bool SteamAPI_ISteamController_Init(IntPtr c_instancePtr);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_Shutdown")]
private static extern bool SteamAPI_ISteamController_Shutdown(IntPtr c_instancePtr);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_RunFrame")]
private static extern void SteamAPI_ISteamController_RunFrame(IntPtr c_instancePtr);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_GetConnectedControllers")]
private static extern int SteamAPI_ISteamController_GetConnectedControllers(IntPtr c_instancePtr, ulong[] c_handlesOut);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_ShowBindingPanel")]
private static extern bool SteamAPI_ISteamController_ShowBindingPanel(IntPtr c_instancePtr, ulong c_controllerHandle);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_GetActionSetHandle")]
private static extern ulong SteamAPI_ISteamController_GetActionSetHandle(IntPtr c_instancePtr, SafeUtf8String c_pszActionSetName);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_ActivateActionSet")]
private static extern void SteamAPI_ISteamController_ActivateActionSet(IntPtr c_instancePtr, ulong c_controllerHandle, ulong c_actionSetHandle);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_GetCurrentActionSet")]
private static extern ulong SteamAPI_ISteamController_GetCurrentActionSet(IntPtr c_instancePtr, ulong c_controllerHandle);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_GetDigitalActionHandle")]
private static extern ulong SteamAPI_ISteamController_GetDigitalActionHandle(IntPtr c_instancePtr, SafeUtf8String c_pszActionName);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_GetDigitalActionData")]
private static extern ControllerDigitalActionData SteamAPI_ISteamController_GetDigitalActionData(IntPtr c_instancePtr, ulong c_controllerHandle, ulong c_digitalActionHandle);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_GetDigitalActionOrigins")]
private static extern int SteamAPI_ISteamController_GetDigitalActionOrigins(IntPtr c_instancePtr, ulong c_controllerHandle, ulong c_actionSetHandle, ulong c_digitalActionHandle, ref uint c_originsOut);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_GetAnalogActionHandle")]
private static extern ulong SteamAPI_ISteamController_GetAnalogActionHandle(IntPtr c_instancePtr, SafeUtf8String c_pszActionName);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_GetAnalogActionData")]
private static extern ControllerAnalogActionData SteamAPI_ISteamController_GetAnalogActionData(IntPtr c_instancePtr, ulong c_controllerHandle, ulong c_analogActionHandle);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_GetAnalogActionOrigins")]
private static extern int SteamAPI_ISteamController_GetAnalogActionOrigins(IntPtr c_instancePtr, ulong c_controllerHandle, ulong c_actionSetHandle, ulong c_analogActionHandle, ref uint c_originsOut);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_StopAnalogActionMomentum")]
private static extern void SteamAPI_ISteamController_StopAnalogActionMomentum(IntPtr c_instancePtr, ulong c_controllerHandle, ulong c_eAction);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_TriggerHapticPulse")]
private static extern void SteamAPI_ISteamController_TriggerHapticPulse(IntPtr c_instancePtr, ulong c_controllerHandle, uint c_eTargetPad, char c_usDurationMicroSec);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_TriggerRepeatedHapticPulse")]
private static extern void SteamAPI_ISteamController_TriggerRepeatedHapticPulse(IntPtr c_instancePtr, ulong c_controllerHandle, uint c_eTargetPad, char c_usDurationMicroSec, char c_usOffMicroSec, char c_unRepeat, uint c_nFlags);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_GetGamepadIndexForController")]
private static extern int SteamAPI_ISteamController_GetGamepadIndexForController(IntPtr c_instancePtr, ulong c_ulControllerHandle);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_GetControllerForGamepadIndex")]
private static extern ulong SteamAPI_ISteamController_GetControllerForGamepadIndex(IntPtr c_instancePtr, int c_nIndex);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_GetMotionData")]
private static extern ControllerMotionData SteamAPI_ISteamController_GetMotionData(IntPtr c_instancePtr, ulong c_controllerHandle);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_ShowDigitalActionOrigins")]
private static extern bool SteamAPI_ISteamController_ShowDigitalActionOrigins(IntPtr c_instancePtr, ulong c_controllerHandle, ulong c_digitalActionHandle, float c_flScale, float c_flXPosition, float c_flYPosition);
[DllImport(SteamApi.STEAMWORKS_MODULE_NAME, EntryPoint = "SteamAPI_ISteamController_ShowAnalogActionOrigins")]
private static extern bool SteamAPI_ISteamController_ShowAnalogActionOrigins(IntPtr c_instancePtr, ulong c_controllerHandle, ulong c_analogActionHandle, float c_flScale, float c_flXPosition, float c_flYPosition);
#endregion
#region Overrides of ISteamController
public override IntPtr GetIntPtr()
{
return m_instancePtr;
}
public override bool Init()
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_Init(m_instancePtr);
return a_result;
}
public override bool Shutdown()
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_Shutdown(m_instancePtr);
return a_result;
}
public override void RunFrame()
{
CheckIfUsable();
SteamAPI_ISteamController_RunFrame(m_instancePtr);
}
public override int GetConnectedControllers(ref ulong[] c_handlesOut)
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_GetConnectedControllers(m_instancePtr, c_handlesOut);
return a_result;
}
public override bool ShowBindingPanel(ulong c_controllerHandle)
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_ShowBindingPanel(m_instancePtr, c_controllerHandle);
return a_result;
}
public override ulong GetActionSetHandle(string c_pszActionSetName)
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_GetActionSetHandle(m_instancePtr, new SafeUtf8String(c_pszActionSetName));
return a_result;
}
public override void ActivateActionSet(ulong c_controllerHandle, ulong c_actionSetHandle)
{
CheckIfUsable();
SteamAPI_ISteamController_ActivateActionSet(m_instancePtr, c_controllerHandle, c_actionSetHandle);
}
public override ulong GetCurrentActionSet(ulong c_controllerHandle)
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_GetCurrentActionSet(m_instancePtr, c_controllerHandle);
return a_result;
}
public override ulong GetDigitalActionHandle(string c_pszActionName)
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_GetDigitalActionHandle(m_instancePtr, new SafeUtf8String(c_pszActionName));
return a_result;
}
public override ControllerDigitalActionData GetDigitalActionData(ulong c_controllerHandle, ulong c_digitalActionHandle)
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_GetDigitalActionData(m_instancePtr, c_controllerHandle, c_digitalActionHandle);
return a_result;
}
public override int GetDigitalActionOrigins(ulong c_controllerHandle, ulong c_actionSetHandle, ulong c_digitalActionHandle, ref uint c_originsOut)
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_GetDigitalActionOrigins(m_instancePtr, c_controllerHandle, c_actionSetHandle, c_digitalActionHandle, ref c_originsOut);
return a_result;
}
public override ulong GetAnalogActionHandle(string c_pszActionName)
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_GetAnalogActionHandle(m_instancePtr, new SafeUtf8String(c_pszActionName));
return a_result;
}
public override ControllerAnalogActionData GetAnalogActionData(ulong c_controllerHandle, ulong c_analogActionHandle)
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_GetAnalogActionData(m_instancePtr, c_controllerHandle, c_analogActionHandle);
return a_result;
}
public override int GetAnalogActionOrigins(ulong c_controllerHandle, ulong c_actionSetHandle, ulong c_analogActionHandle, ref uint c_originsOut)
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_GetAnalogActionOrigins(m_instancePtr, c_controllerHandle, c_actionSetHandle, c_analogActionHandle, ref c_originsOut);
return a_result;
}
public override void StopAnalogActionMomentum(ulong c_controllerHandle, ulong c_eAction)
{
CheckIfUsable();
SteamAPI_ISteamController_StopAnalogActionMomentum(m_instancePtr, c_controllerHandle, c_eAction);
}
public override void TriggerHapticPulse(ulong c_controllerHandle, uint c_eTargetPad, char c_usDurationMicroSec)
{
CheckIfUsable();
SteamAPI_ISteamController_TriggerHapticPulse(m_instancePtr, c_controllerHandle, c_eTargetPad, c_usDurationMicroSec);
}
public override void TriggerRepeatedHapticPulse(ulong c_controllerHandle, uint c_eTargetPad, char c_usDurationMicroSec, char c_usOffMicroSec, char c_unRepeat, uint c_nFlags)
{
CheckIfUsable();
SteamAPI_ISteamController_TriggerRepeatedHapticPulse(m_instancePtr, c_controllerHandle, c_eTargetPad, c_usDurationMicroSec, c_usOffMicroSec, c_unRepeat, c_nFlags);
}
public override int GetGamepadIndexForController(ulong c_ulControllerHandle)
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_GetGamepadIndexForController(m_instancePtr, c_ulControllerHandle);
return a_result;
}
public override ulong GetControllerForGamepadIndex(int c_nIndex)
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_GetControllerForGamepadIndex(m_instancePtr, c_nIndex);
return a_result;
}
public override ControllerMotionData GetMotionData(ulong c_controllerHandle)
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_GetMotionData(m_instancePtr, c_controllerHandle);
return a_result;
}
public override bool ShowDigitalActionOrigins(ulong c_controllerHandle, ulong c_digitalActionHandle, float c_flScale, float c_flXPosition, float c_flYPosition)
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_ShowDigitalActionOrigins(m_instancePtr, c_controllerHandle, c_digitalActionHandle, c_flScale, c_flXPosition, c_flYPosition);
return a_result;
}
public override bool ShowAnalogActionOrigins(ulong c_controllerHandle, ulong c_analogActionHandle, float c_flScale, float c_flXPosition, float c_flYPosition)
{
CheckIfUsable();
var a_result = SteamAPI_ISteamController_ShowAnalogActionOrigins(m_instancePtr, c_controllerHandle, c_analogActionHandle, c_flScale, c_flXPosition, c_flYPosition);
return a_result;
}
#endregion
}
public abstract class ISteamController
{
public abstract IntPtr GetIntPtr();
public abstract bool Init();
public abstract bool Shutdown();
public abstract void RunFrame();
public abstract int GetConnectedControllers(ref ulong[] c_handlesOut);
public abstract bool ShowBindingPanel(ulong c_controllerHandle);
public abstract ulong GetActionSetHandle(string c_pszActionSetName);
public abstract void ActivateActionSet(ulong c_controllerHandle, ulong c_actionSetHandle);
public abstract ulong GetCurrentActionSet(ulong c_controllerHandle);
public abstract ulong GetDigitalActionHandle(string c_pszActionName);
public abstract ControllerDigitalActionData GetDigitalActionData(ulong c_controllerHandle, ulong c_digitalActionHandle);
public abstract int GetDigitalActionOrigins(ulong c_controllerHandle, ulong c_actionSetHandle, ulong c_digitalActionHandle, ref uint c_originsOut);
public abstract ulong GetAnalogActionHandle(string c_pszActionName);
public abstract ControllerAnalogActionData GetAnalogActionData(ulong c_controllerHandle, ulong c_analogActionHandle);
public abstract int GetAnalogActionOrigins(ulong c_controllerHandle, ulong c_actionSetHandle, ulong c_analogActionHandle, ref uint c_originsOut);
public abstract void StopAnalogActionMomentum(ulong c_controllerHandle, ulong c_eAction);
public abstract void TriggerHapticPulse(ulong c_controllerHandle, uint c_eTargetPad, char c_usDurationMicroSec);
public abstract void TriggerRepeatedHapticPulse(ulong c_controllerHandle, uint c_eTargetPad, char c_usDurationMicroSec, char c_usOffMicroSec, char c_unRepeat, uint c_nFlags);
public abstract int GetGamepadIndexForController(ulong c_ulControllerHandle);
public abstract ulong GetControllerForGamepadIndex(int c_nIndex);
public abstract ControllerMotionData GetMotionData(ulong c_controllerHandle);
public abstract bool ShowDigitalActionOrigins(ulong c_controllerHandle, ulong c_digitalActionHandle, float c_flScale, float c_flXPosition, float c_flYPosition);
public abstract bool ShowAnalogActionOrigins(ulong c_controllerHandle, ulong c_analogActionHandle, float c_flScale, float c_flXPosition, float c_flYPosition);
}
[StructLayout(LayoutKind.Sequential)]
public struct ControllerDigitalActionData
{
public byte State;
public byte Active;
}
[StructLayout(LayoutKind.Sequential)]
public struct ControllerAnalogActionData
{
public EControllerSourceMode Mode;
public float X;
public float Y;
public byte Active;
}
[StructLayout(LayoutKind.Sequential)]
public struct ControllerMotionData
{
public float RotQuatX;
public float RotQuatY;
public float RotQuatZ;
public float RotQuatW;
public float PosAccelX;
public float PosAccelY;
public float PosAccelZ;
public float RotVelX;
public float RotVelY;
public float RotVelZ;
}
}