forked from DarthAffe/CUE.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CueSDK.cs
265 lines (221 loc) · 11.1 KB
/
CueSDK.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
// ReSharper disable MemberCanBePrivate.Global
using System.Collections.Generic;
using System.Runtime.InteropServices;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Headset;
using CUE.NET.Devices.Keyboard;
using CUE.NET.Devices.Mouse;
using CUE.NET.Exceptions;
using CUE.NET.Native;
namespace CUE.NET
{
public static class CueSDK
{
#region Properties & Fields
// ReSharper disable UnusedAutoPropertyAccessor.Global
/// <summary>
/// Indicates if the SDK is initialized and ready to use.
/// </summary>
public static bool IsInitialized { get; private set; }
/// <summary>
/// Gets the loaded architecture (x64/x86).
/// </summary>
public static string LoadedArchitecture => _CUESDK.LoadedArchitecture;
/// <summary>
/// Gets the protocol details for the current SDK-connection.
/// </summary>
public static CorsairProtocolDetails ProtocolDetails { get; private set; }
/// <summary>
/// Gets whether the application has exclusive access to the SDK or not.
/// </summary>
public static bool HasExclusiveAccess { get; private set; }
/// <summary>
/// Gets the last error documented by CUE.
/// </summary>
public static CorsairError LastError => _CUESDK.CorsairGetLastError();
/// <summary>
/// Gets the managed representation of a keyboard managed by the CUE-SDK.
/// Note that currently only one connected keyboard is supported.
/// </summary>
public static CorsairKeyboard KeyboardSDK { get; private set; }
/// <summary>
/// Gets the managed representation of a mouse managed by the CUE-SDK.
/// Note that currently only one connected mouse is supported.
/// </summary>
public static CorsairMouse MouseSDK { get; private set; }
/// <summary>
/// Gets the managed representation of a headset managed by the CUE-SDK.
/// Note that currently only one connected headset is supported.
/// </summary>
public static CorsairHeadset HeadsetSDK { get; private set; }
// ReSharper restore UnusedAutoPropertyAccessor.Global
#endregion
#region Methods
/// <summary>
/// Checks if the SDK for the provided <see cref="CorsairDeviceType"/> is available or checks if CUE is installed and SDK supported enabled if null is provided.
/// </summary>
/// <param name="sdkType">The <see cref="CorsairDeviceType"/> to check or null to check for generall SDK availability.</param>
/// <returns>The availability of the provided <see cref="CorsairDeviceType"/>.</returns>
public static bool IsSDKAvailable(CorsairDeviceType? sdkType = null)
{
try
{
if (IsInitialized)
{
switch (sdkType)
{
case CorsairDeviceType.Keyboard:
return KeyboardSDK != null;
case CorsairDeviceType.Mouse:
return MouseSDK != null;
case CorsairDeviceType.Headset:
return HeadsetSDK != null;
default:
return true;
}
}
else
{
_CUESDK.CorsairPerformProtocolHandshake();
if (sdkType == null || sdkType == CorsairDeviceType.Unknown)
return LastError == CorsairError.Success;
int deviceCount = _CUESDK.CorsairGetDeviceCount();
for (int i = 0; i < deviceCount; i++)
{
GenericDeviceInfo info = new GenericDeviceInfo((_CorsairDeviceInfo)Marshal.PtrToStructure(_CUESDK.CorsairGetDeviceInfo(i), typeof(_CorsairDeviceInfo)));
if (info.CapsMask.HasFlag(CorsairDeviceCaps.Lighting) && info.Type == sdkType.Value)
return true;
}
}
}
catch
{
return false;
}
return false;
}
// ReSharper disable once ExceptionNotThrown
/// <summary>
/// Initializes the CUE-SDK. This method should be called exactly ONE time, before anything else is done.
/// </summary>
/// <param name="exclusiveAccess">Specifies whether the application should request exclusive access or not.</param>
/// <exception cref="WrapperException">Thrown if the SDK is already initialized, the SDK is not compatible to CUE or if CUE returns unknown devices.</exception>
/// <exception cref="CUEException">Thrown if the CUE-SDK provides an error.</exception>
public static void Initialize(bool exclusiveAccess = false)
{
if (IsInitialized)
throw new WrapperException("CueSDK is already initialized.");
ProtocolDetails = new CorsairProtocolDetails(_CUESDK.CorsairPerformProtocolHandshake());
CorsairError error = LastError;
if (error != CorsairError.Success)
Throw(error);
if (ProtocolDetails.BreakingChanges)
throw new WrapperException("The SDK currently used isn't compatible with the installed version of CUE.\r\n"
+ $"CUE-Version: {ProtocolDetails.ServerVersion} (Protocol {ProtocolDetails.ServerProtocolVersion})\r\n"
+ $"SDK-Version: {ProtocolDetails.SdkVersion} (Protocol {ProtocolDetails.SdkProtocolVersion})");
if (exclusiveAccess)
{
if (!_CUESDK.CorsairRequestControl(CorsairAccessMode.ExclusiveLightingControl))
Throw(LastError);
HasExclusiveAccess = true;
}
int deviceCount = _CUESDK.CorsairGetDeviceCount();
for (int i = 0; i < deviceCount; i++)
{
_CorsairDeviceInfo nativeDeviceInfo = (_CorsairDeviceInfo)Marshal.PtrToStructure(_CUESDK.CorsairGetDeviceInfo(i), typeof(_CorsairDeviceInfo));
GenericDeviceInfo info = new GenericDeviceInfo(nativeDeviceInfo);
if (!info.CapsMask.HasFlag(CorsairDeviceCaps.Lighting))
continue; // Everything that doesn't support lighting control is useless
switch (info.Type)
{
case CorsairDeviceType.Keyboard:
KeyboardSDK = new CorsairKeyboard(new CorsairKeyboardDeviceInfo(nativeDeviceInfo));
break;
case CorsairDeviceType.Mouse:
MouseSDK = new CorsairMouse(new CorsairMouseDeviceInfo(nativeDeviceInfo));
break;
case CorsairDeviceType.Headset:
HeadsetSDK = new CorsairHeadset(new CorsairHeadsetDeviceInfo(nativeDeviceInfo));
break;
// ReSharper disable once RedundantCaseLabel
case CorsairDeviceType.Unknown:
default:
throw new WrapperException("Unknown Device-Type");
}
error = LastError;
if (error != CorsairError.Success)
Throw(error);
}
IsInitialized = true;
}
/// <summary>
/// Reinitialize the CUE-SDK and temporarily hand back full control to CUE.
/// </summary>
public static void Reinitialize()
{
Reinitialize(HasExclusiveAccess);
}
/// <summary>
/// Reinitialize the CUE-SDK and temporarily hand back full control to CUE.
/// </summary>
/// <param name="exclusiveAccess">Specifies whether the application should request exclusive access or not.</param>
public static void Reinitialize(bool exclusiveAccess)
{
if (!IsInitialized)
throw new WrapperException("CueSDK isn't initialized.");
KeyboardSDK?.ResetLeds();
MouseSDK?.ResetLeds();
HeadsetSDK?.ResetLeds();
_CUESDK.Reload();
_CUESDK.CorsairPerformProtocolHandshake();
CorsairError error = LastError;
if (error != CorsairError.Success)
Throw(error);
if (ProtocolDetails.BreakingChanges)
throw new WrapperException("The SDK currently used isn't compatible with the installed version of CUE.\r\n"
+ $"CUE-Version: {ProtocolDetails.ServerVersion} (Protocol {ProtocolDetails.ServerProtocolVersion})\r\n"
+ $"SDK-Version: {ProtocolDetails.SdkVersion} (Protocol {ProtocolDetails.SdkProtocolVersion})");
if (exclusiveAccess)
if (!_CUESDK.CorsairRequestControl(CorsairAccessMode.ExclusiveLightingControl))
Throw(LastError);
HasExclusiveAccess = exclusiveAccess;
int deviceCount = _CUESDK.CorsairGetDeviceCount();
Dictionary<CorsairDeviceType, GenericDeviceInfo> reloadedDevices = new Dictionary<CorsairDeviceType, GenericDeviceInfo>();
for (int i = 0; i < deviceCount; i++)
{
GenericDeviceInfo info = new GenericDeviceInfo((_CorsairDeviceInfo)Marshal.PtrToStructure(_CUESDK.CorsairGetDeviceInfo(i), typeof(_CorsairDeviceInfo)));
if (!info.CapsMask.HasFlag(CorsairDeviceCaps.Lighting))
continue; // Everything that doesn't support lighting control is useless
reloadedDevices.Add(info.Type, info);
error = LastError;
if (error != CorsairError.Success)
Throw(error);
}
if (KeyboardSDK != null)
if (!reloadedDevices.ContainsKey(CorsairDeviceType.Keyboard)
|| KeyboardSDK.KeyboardDeviceInfo.Model != reloadedDevices[CorsairDeviceType.Keyboard].Model)
throw new WrapperException("The previously loaded Keyboard got disconnected.");
if (MouseSDK != null)
if (!reloadedDevices.ContainsKey(CorsairDeviceType.Mouse)
|| MouseSDK.MouseDeviceInfo.Model != reloadedDevices[CorsairDeviceType.Mouse].Model)
throw new WrapperException("The previously loaded Mouse got disconnected.");
if (HeadsetSDK != null)
if (!reloadedDevices.ContainsKey(CorsairDeviceType.Headset)
|| HeadsetSDK.HeadsetDeviceInfo.Model != reloadedDevices[CorsairDeviceType.Headset].Model)
throw new WrapperException("The previously loaded Headset got disconnected.");
IsInitialized = true;
}
private static void Throw(CorsairError error)
{
ProtocolDetails = null;
HasExclusiveAccess = false;
KeyboardSDK = null;
MouseSDK = null;
HeadsetSDK = null;
IsInitialized = false;
throw new CUEException(error);
}
#endregion
}
}