This repository has been archived by the owner on Aug 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathCueSDK.cs
397 lines (334 loc) · 17.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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using CUE.NET.Devices;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Headset;
using CUE.NET.Devices.HeadsetStand;
using CUE.NET.Devices.Keyboard;
using CUE.NET.Devices.Mouse;
using CUE.NET.Devices.Mousemat;
using CUE.NET.EventArgs;
using CUE.NET.Exceptions;
using CUE.NET.Native;
namespace CUE.NET
{
/// <summary>
/// Static entry point to work with the Corsair-SDK.
/// </summary>
public static partial class CueSDK
{
#region Properties & Fields
// ReSharper disable UnusedAutoPropertyAccessor.Global
/// <summary>
/// Gets a modifiable list of paths used to find the native SDK-dlls for x86 applications.
/// The first match will be used.
/// </summary>
public static List<string> PossibleX86NativePaths { get; } = new List<string> { "x86/CUESDK_2015.dll", "x86/CUESDK.dll" };
/// <summary>
/// Gets a modifiable list of paths used to find the native SDK-dlls for x64 applications.
/// The first match will be used.
/// </summary>
public static List<string> PossibleX64NativePaths { get; } = new List<string> { "x64/CUESDK_2015.dll", "x64/CUESDK.dll" };
/// <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 all initialized devices managed by the CUE-SDK.
/// </summary>
public static IEnumerable<ICueDevice> InitializedDevices { get; private set; }
/// <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; }
/// <summary>
/// Gets the managed representation of a mousemat managed by the CUE-SDK.
/// Note that currently only one connected mousemat is supported.
/// </summary>
public static CorsairMousemat MousematSDK { get; private set; }
/// <summary>
/// Gets the managed representation of a headset stand managed by the CUE-SDK.
/// Note that currently only one connected headset stand is supported.
/// </summary>
public static CorsairHeadsetStand HeadsetStandSDK { get; private set; }
// ReSharper restore UnusedAutoPropertyAccessor.Global
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void OnKeyPressedDelegate(IntPtr context, CorsairKeyId keyId, [MarshalAs(UnmanagedType.I1)] bool pressed);
private static OnKeyPressedDelegate _onKeyPressedDelegate;
#endregion
#region Events
/// <summary>
/// Occurs when the SDK reports that a key is pressed.
/// Notice that right now only G- (keyboard) and M- (mouse) keys are supported.
///
/// To enable this event <see cref="EnableKeypressCallback"/> needs to be called.
/// </summary>
public static event EventHandler<KeyPressedEventArgs> KeyPressed;
#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 general SDK availability.</param>
/// <returns>The availability of the provided <see cref="CorsairDeviceType"/>.</returns>
public static bool IsSDKAvailable(CorsairDeviceType? sdkType = null)
{
try
{
// ReSharper disable once RedundantIfElseBlock
if (IsInitialized)
{
// ReSharper disable once SwitchStatementMissingSomeCases - everything else is true
switch (sdkType)
{
case CorsairDeviceType.Keyboard:
return KeyboardSDK != null;
case CorsairDeviceType.Mouse:
return MouseSDK != null;
case CorsairDeviceType.Headset:
return HeadsetSDK != null;
case CorsairDeviceType.Mousemat:
return MousematSDK != null;
case CorsairDeviceType.HeadsetStand:
return HeadsetStandSDK != null;
default:
return true;
}
}
else
{
_CUESDK.Reload();
_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.");
_CUESDK.Reload();
ProtocolDetails = new CorsairProtocolDetails(_CUESDK.CorsairPerformProtocolHandshake());
CorsairError error = LastError;
if (error != CorsairError.Success)
Throw(error, true);
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, true);
HasExclusiveAccess = true;
}
IList<ICueDevice> devices = new List<ICueDevice>();
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
ICueDevice device;
switch (info.Type)
{
case CorsairDeviceType.Keyboard:
device = KeyboardSDK = new CorsairKeyboard(new CorsairKeyboardDeviceInfo(nativeDeviceInfo));
break;
case CorsairDeviceType.Mouse:
device = MouseSDK = new CorsairMouse(new CorsairMouseDeviceInfo(nativeDeviceInfo));
break;
case CorsairDeviceType.Headset:
device = HeadsetSDK = new CorsairHeadset(new CorsairHeadsetDeviceInfo(nativeDeviceInfo));
break;
case CorsairDeviceType.Mousemat:
device = MousematSDK = new CorsairMousemat(new CorsairMousematDeviceInfo(nativeDeviceInfo));
break;
case CorsairDeviceType.HeadsetStand:
device = HeadsetStandSDK = new CorsairHeadsetStand(new CorsairHeadsetStandDeviceInfo(nativeDeviceInfo));
break;
// ReSharper disable once RedundantCaseLabel
case CorsairDeviceType.Unknown:
default:
throw new WrapperException("Unknown Device-Type");
}
device.Initialize();
devices.Add(device);
error = LastError;
if (error != CorsairError.Success)
Throw(error, true);
}
error = LastError;
if (error != CorsairError.Success)
Throw(error, false);
InitializedDevices = new ReadOnlyCollection<ICueDevice>(devices);
IsInitialized = true;
}
/// <summary>
/// Enables the keypress-callback.
/// This method needs to be called to enable the <see cref="KeyPressed"/>-event.
///
/// WARNING: AFTER THIS METHOD IS CALLED IT'S NO LONGER POSSIBLE TO REINITIALIZE THE SDK!
/// </summary>
public static void EnableKeypressCallback()
{
if (!IsInitialized)
throw new WrapperException("CueSDK isn't initialized.");
if (_onKeyPressedDelegate != null)
return;
_onKeyPressedDelegate = OnKeyPressed;
_CUESDK.CorsairRegisterKeypressCallback(Marshal.GetFunctionPointerForDelegate(_onKeyPressedDelegate), IntPtr.Zero);
}
/// <summary>
/// Resets the colors of all devices back to the last saved color-data. (If there wasn't a manual save, that's the data from the time the SDK was initialized.)
/// </summary>
public static void Reset()
{
foreach (ICueDevice device in InitializedDevices)
device.RestoreColors();
}
/// <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.");
if (_onKeyPressedDelegate != null)
throw new WrapperException("Keypress-Callback is enabled.");
KeyboardSDK?.ResetLeds();
MouseSDK?.ResetLeds();
HeadsetSDK?.ResetLeds();
MousematSDK?.ResetLeds();
HeadsetStandSDK?.ResetLeds();
_CUESDK.Reload();
_CUESDK.CorsairPerformProtocolHandshake();
CorsairError error = LastError;
if (error != CorsairError.Success)
Throw(error, false);
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, false);
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, false);
}
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.");
if (MousematSDK != null)
if (!reloadedDevices.ContainsKey(CorsairDeviceType.Mousemat)
|| MousematSDK.MousematDeviceInfo.Model != reloadedDevices[CorsairDeviceType.Mousemat].Model)
throw new WrapperException("The previously loaded Mousemat got disconnected.");
if (HeadsetStandSDK != null)
if (!reloadedDevices.ContainsKey(CorsairDeviceType.HeadsetStand)
|| HeadsetStandSDK.HeadsetStandDeviceInfo.Model != reloadedDevices[CorsairDeviceType.HeadsetStand].Model)
throw new WrapperException("The previously loaded Headset Stand got disconnected.");
error = LastError;
if (error != CorsairError.Success)
Throw(error, false);
IsInitialized = true;
}
private static void Throw(CorsairError error, bool reset)
{
if (reset)
{
ProtocolDetails = null;
HasExclusiveAccess = false;
KeyboardSDK = null;
MouseSDK = null;
HeadsetSDK = null;
MousematSDK = null;
HeadsetStandSDK = null;
IsInitialized = false;
}
throw new CUEException(error);
}
private static void OnKeyPressed(IntPtr context, CorsairKeyId keyId, bool pressed)
=> KeyPressed?.Invoke(null, new KeyPressedEventArgs(keyId, pressed));
#endregion
}
}