This repository has been archived by the owner on Mar 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 644
/
Controller.cs
192 lines (178 loc) · 7.31 KB
/
Controller.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
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Runtime.InteropServices;
using SharpDX.Win32;
namespace SharpDX.XInput
{
/// <summary>
/// A XInput controller.
/// </summary>
public class Controller
{
private readonly UserIndex userIndex;
private static readonly IXInput xinput;
/// <summary>
/// Initializes a new instance of the <see cref="Controller"/> class.
/// </summary>
/// <param name="userIndex">Index of the user.</param>
public Controller(UserIndex userIndex = UserIndex.Any)
{
if(xinput == null)
{
throw new NotSupportedException("XInput 1.4 or 1.3 or 9.1.0 is not installed");
}
this.userIndex = userIndex;
}
/// <summary>
/// Gets the <see cref="UserIndex"/> associated with this controller.
/// </summary>
/// <value>The index of the user.</value>
public UserIndex UserIndex { get { return this.userIndex; } }
/// <summary>
/// Gets the battery information.
/// </summary>
/// <param name="batteryDeviceType">Type of the battery device.</param>
/// <returns></returns>
/// <unmanaged>unsigned int XInputGetBatteryInformation([In] XUSER_INDEX dwUserIndex,[In] BATTERY_DEVTYPE devType,[Out] XINPUT_BATTERY_INFORMATION* pBatteryInformation)</unmanaged>
public BatteryInformation GetBatteryInformation(BatteryDeviceType batteryDeviceType)
{
BatteryInformation temp;
var result = ErrorCodeHelper.ToResult(xinput.XInputGetBatteryInformation((int)userIndex, batteryDeviceType, out temp));
result.CheckError();
return temp;
}
/// <summary>
/// Gets the capabilities.
/// </summary>
/// <param name="deviceQueryType">Type of the device query.</param>
/// <returns></returns>
/// <unmanaged>unsigned int XInputGetCapabilities([In] XUSER_INDEX dwUserIndex,[In] XINPUT_DEVQUERYTYPE dwFlags,[Out] XINPUT_CAPABILITIES* pCapabilities)</unmanaged>
public Capabilities GetCapabilities(DeviceQueryType deviceQueryType)
{
Capabilities temp;
var result = ErrorCodeHelper.ToResult(xinput.XInputGetCapabilities((int)userIndex, deviceQueryType, out temp));
result.CheckError();
return temp;
}
/// <summary>
/// Gets the capabilities.
/// </summary>
/// <param name="deviceQueryType">Type of the device query.</param>
/// <param name="capabilities">The capabilities of this controller.</param>
/// <returns><c>true</c> if the controller is connected, <c>false</c> otherwise.</returns>
public bool GetCapabilities(DeviceQueryType deviceQueryType, out Capabilities capabilities)
{
return xinput.XInputGetCapabilities((int)userIndex, deviceQueryType, out capabilities) == 0;
}
/// <summary>
/// Gets the keystroke.
/// </summary>
/// <param name="deviceQueryType">The flag.</param>
/// <param name="keystroke">The keystroke.</param>
/// <returns></returns>
/// <unmanaged>unsigned int XInputGetKeystroke([In] XUSER_INDEX dwUserIndex,[In] unsigned int dwReserved,[Out] XINPUT_KEYSTROKE* pKeystroke)</unmanaged>
public Result GetKeystroke(DeviceQueryType deviceQueryType, out Keystroke keystroke)
{
var result = ErrorCodeHelper.ToResult(xinput.XInputGetKeystroke((int)userIndex, (int)deviceQueryType, out keystroke));
return result;
}
/// <summary>
/// Gets the state.
/// </summary>
/// <returns>The state of this controller.</returns>
public State GetState()
{
State temp;
var result = ErrorCodeHelper.ToResult(xinput.XInputGetState((int)userIndex, out temp));
result.CheckError();
return temp;
}
/// <summary>
/// Gets the state.
/// </summary>
/// <param name="state">The state of this controller.</param>
/// <returns><c>true</c> if the controller is connected, <c>false</c> otherwise.</returns>
public bool GetState(out State state)
{
return xinput.XInputGetState((int)userIndex, out state) == 0;
}
/// <summary>
/// Sets the reporting.
/// </summary>
/// <param name="enableReporting">if set to <c>true</c> [enable reporting].</param>
public static void SetReporting(bool enableReporting)
{
if(xinput != null)
{
xinput.XInputEnable(enableReporting);
}
}
/// <summary>
/// Sets the vibration.
/// </summary>
/// <param name="vibration">The vibration.</param>
/// <returns></returns>
public Result SetVibration(Vibration vibration)
{
var result = ErrorCodeHelper.ToResult(xinput.XInputSetState((int)userIndex, vibration));
result.CheckError();
return result;
}
/// <summary>
/// Gets a value indicating whether this instance is connected.
/// </summary>
/// <value>
/// <c>true</c> if this instance is connected; otherwise, <c>false</c>.
/// </value>
public bool IsConnected
{
get
{
State temp;
return xinput.XInputGetState((int)userIndex, out temp) == 0;
}
}
#if !WINDOWS_UWP
static Controller()
{
if(LoadLibrary("xinput1_4.dll") != IntPtr.Zero)
{
xinput = new XInput14();
}
else if (LoadLibrary("xinput1_3.dll") != IntPtr.Zero)
{
xinput = new XInput13();
}
else if (LoadLibrary("xinput9_1_0.dll") != IntPtr.Zero)
{
xinput = new XInput910();
}
}
[DllImport("kernel32", CharSet = CharSet.Unicode, EntryPoint = "LoadLibrary", SetLastError = true)]
private static extern IntPtr LoadLibrary(string lpFileName);
#else
static Controller()
{
xinput = new XInput14();
}
#endif
}
}