This repository has been archived by the owner on Aug 6, 2021. It is now read-only.
/
_CUESDK.cs
248 lines (198 loc) · 11.5 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
// ReSharper disable UnusedMethodReturnValue.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Exceptions;
namespace CUE.NET.Native
{
// ReSharper disable once InconsistentNaming
internal static class _CUESDK
{
#region Libary Management
private static IntPtr _dllHandle = IntPtr.Zero;
/// <summary>
/// Gets the loaded architecture (x64/x86).
/// </summary>
internal static string LoadedArchitecture { get; private set; }
/// <summary>
/// Reloads the SDK.
/// </summary>
internal static void Reload()
{
UnloadCUESDK();
LoadCUESDK();
}
private static void LoadCUESDK()
{
if (_dllHandle != IntPtr.Zero) return;
LoadedArchitecture = LoadedArchitecture = Environment.Is64BitProcess ? "x64" : "x86";
// HACK: Load library at runtime to support both, x86 and x64 with one managed dll
List<string> possiblePathList = Environment.Is64BitProcess ? CueSDK.PossibleX64NativePaths : CueSDK.PossibleX86NativePaths;
string dllPath = null;
foreach (string path in possiblePathList)
if (File.Exists(path))
{
dllPath = path;
break;
}
if (dllPath == null) throw new WrapperException($"Can't find the CUE-SDK at one of the expected locations:\r\n '{string.Join("\r\n", possiblePathList.Select(Path.GetFullPath))}'");
_dllHandle = LoadLibrary(dllPath);
_corsairSetLedsColorsPointer = (CorsairSetLedsColorsPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairSetLedsColors"), typeof(CorsairSetLedsColorsPointer));
_corsairGetLedsColorsPointer = (CorsairGetLedsColorsPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLedsColors"), typeof(CorsairGetLedsColorsPointer));
_corsairGetDeviceCountPointer = (CorsairGetDeviceCountPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetDeviceCount"), typeof(CorsairGetDeviceCountPointer));
_corsairGetDeviceInfoPointer = (CorsairGetDeviceInfoPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetDeviceInfo"), typeof(CorsairGetDeviceInfoPointer));
_corsairGetLedPositionsPointer = (CorsairGetLedPositionsPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLedPositions"), typeof(CorsairGetLedPositionsPointer));
_corsairGetLedPositionsByDeviceIndexPointer = (CorsairGetLedPositionsByDeviceIndexPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLedPositionsByDeviceIndex"), typeof(CorsairGetLedPositionsByDeviceIndexPointer));
_corsairGetLedIdForKeyNamePointer = (CorsairGetLedIdForKeyNamePointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLedIdForKeyName"), typeof(CorsairGetLedIdForKeyNamePointer));
_corsairRequestControlPointer = (CorsairRequestControlPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairRequestControl"), typeof(CorsairRequestControlPointer));
_corsairReleaseControlPointer = (CorsairReleaseControlPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairReleaseControl"), typeof(CorsairReleaseControlPointer));
_corsairPerformProtocolHandshakePointer = (CorsairPerformProtocolHandshakePointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairPerformProtocolHandshake"), typeof(CorsairPerformProtocolHandshakePointer));
_corsairGetLastErrorPointer = (CorsairGetLastErrorPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLastError"), typeof(CorsairGetLastErrorPointer));
_corsairRegisterKeypressCallbackPointer = (CorsairRegisterKeypressCallbackPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairRegisterKeypressCallback"), typeof(CorsairRegisterKeypressCallbackPointer));
}
private static void UnloadCUESDK()
{
if (_dllHandle == IntPtr.Zero) return;
// ReSharper disable once EmptyEmbeddedStatement - DarthAffe 20.02.2016: We might need to reduce the internal reference counter more than once to set the library free
while (FreeLibrary(_dllHandle)) ;
_dllHandle = IntPtr.Zero;
}
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
private static extern bool FreeLibrary(IntPtr dllHandle);
[DllImport("kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr dllHandle, string name);
#endregion
#region SDK-METHODS
#region Pointers
private static CorsairSetLedsColorsPointer _corsairSetLedsColorsPointer;
private static CorsairGetLedsColorsPointer _corsairGetLedsColorsPointer;
private static CorsairGetDeviceCountPointer _corsairGetDeviceCountPointer;
private static CorsairGetDeviceInfoPointer _corsairGetDeviceInfoPointer;
private static CorsairGetLedPositionsPointer _corsairGetLedPositionsPointer;
private static CorsairGetLedIdForKeyNamePointer _corsairGetLedIdForKeyNamePointer;
private static CorsairGetLedPositionsByDeviceIndexPointer _corsairGetLedPositionsByDeviceIndexPointer;
private static CorsairRequestControlPointer _corsairRequestControlPointer;
private static CorsairReleaseControlPointer _corsairReleaseControlPointer;
private static CorsairPerformProtocolHandshakePointer _corsairPerformProtocolHandshakePointer;
private static CorsairGetLastErrorPointer _corsairGetLastErrorPointer;
private static CorsairRegisterKeypressCallbackPointer _corsairRegisterKeypressCallbackPointer;
#endregion
#region Delegates
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool CorsairSetLedsColorsPointer(int size, IntPtr ledsColors);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool CorsairGetLedsColorsPointer(int size, IntPtr ledsColors);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int CorsairGetDeviceCountPointer();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate IntPtr CorsairGetDeviceInfoPointer(int deviceIndex);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate IntPtr CorsairGetLedPositionsPointer();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate IntPtr CorsairGetLedPositionsByDeviceIndexPointer(int deviceIndex);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate CorsairLedId CorsairGetLedIdForKeyNamePointer(char keyName);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool CorsairRequestControlPointer(CorsairAccessMode accessMode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool CorsairReleaseControlPointer(CorsairAccessMode accessMode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate _CorsairProtocolDetails CorsairPerformProtocolHandshakePointer();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate CorsairError CorsairGetLastErrorPointer();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool CorsairRegisterKeypressCallbackPointer(IntPtr callback, IntPtr context);
#endregion
// ReSharper disable EventExceptionNotDocumented
/// <summary>
/// CUE-SDK: set specified leds to some colors. The color is retained until changed by successive calls. This function does not take logical layout into account.
/// </summary>
internal static bool CorsairSetLedsColors(int size, IntPtr ledsColors)
{
return _corsairSetLedsColorsPointer(size, ledsColors);
}
/// <summary>
/// CUE-SDK: get current color for the list of requested LEDs.
/// </summary>
internal static bool CorsairGetLedsColors(int size, IntPtr ledsColors)
{
return _corsairGetLedsColorsPointer(size, ledsColors);
}
/// <summary>
/// CUE-SDK: returns number of connected Corsair devices that support lighting control.
/// </summary>
internal static int CorsairGetDeviceCount()
{
return _corsairGetDeviceCountPointer();
}
/// <summary>
/// CUE-SDK: returns information about device at provided index.
/// </summary>
internal static IntPtr CorsairGetDeviceInfo(int deviceIndex)
{
return _corsairGetDeviceInfoPointer(deviceIndex);
}
/// <summary>
/// CUE-SDK: provides list of keyboard LEDs with their physical positions.
/// </summary>
internal static IntPtr CorsairGetLedPositions()
{
return _corsairGetLedPositionsPointer();
}
/// <summary>
/// CUE-SDK: provides list of keyboard or mousemat LEDs with their physical positions.
/// </summary>
internal static IntPtr CorsairGetLedPositionsByDeviceIndex(int deviceIndex)
{
return _corsairGetLedPositionsByDeviceIndexPointer(deviceIndex);
}
/// <summary>
/// CUE-SDK: retrieves led id for key name taking logical layout into account.
/// </summary>
internal static CorsairLedId CorsairGetLedIdForKeyName(char keyName)
{
return _corsairGetLedIdForKeyNamePointer(keyName);
}
/// <summary>
/// CUE-SDK: requestes control using specified access mode.
/// By default client has shared control over lighting so there is no need to call CorsairRequestControl unless client requires exclusive control.
/// </summary>
internal static bool CorsairRequestControl(CorsairAccessMode accessMode)
{
return _corsairRequestControlPointer(accessMode);
}
/// <summary>
/// CUE-SDK: releases previously requested control for specified access mode.
/// </summary>
internal static bool CorsairReleaseControl(CorsairAccessMode accessMode)
{
return _corsairReleaseControlPointer(accessMode);
}
/// <summary>
/// CUE-SDK: checks file and protocol version of CUE to understand which of SDK functions can be used with this version of CUE.
/// </summary>
internal static _CorsairProtocolDetails CorsairPerformProtocolHandshake()
{
return _corsairPerformProtocolHandshakePointer();
}
/// <summary>
/// CUE-SDK: returns last error that occured while using any of Corsair* functions.
/// </summary>
internal static CorsairError CorsairGetLastError()
{
return _corsairGetLastErrorPointer();
}
internal static bool CorsairRegisterKeypressCallback(IntPtr callback, IntPtr context)
{
return _corsairRegisterKeypressCallbackPointer(callback, context);
}
// ReSharper restore EventExceptionNotDocumented
#endregion
}
}