-
Notifications
You must be signed in to change notification settings - Fork 14
/
RzChromaSDK.cpp
431 lines (377 loc) · 12.8 KB
/
RzChromaSDK.cpp
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include "stdafx.h"
#include "RzChromaSDK.h"
#include "ChromaLogger.h"
#include "RzErrors.h"
#include <filesystem>
#include "VerifyLibrarySignature.h"
#include <tchar.h>
#include <psapi.h>
#ifdef _WIN64
#define CHROMASDKDLL L"RzChromatic64.dll"
#else
#define CHROMASDKDLL L"RzChromatic.dll"
#endif
using namespace ChromaSDK;
HMODULE RzChromaSDK::_sLibrary = NULL;
bool RzChromaSDK::_sInvalidSignature = false;
bool RzChromaSDK::_sLoaded = false;
// assign static methods
#undef CHROMASDK_DECLARE_METHOD_IMPL
#define CHROMASDK_DECLARE_METHOD_IMPL(Signature, FieldName) Signature RzChromaSDK::_sMethod ## FieldName = NULL
// CORE API METHODS
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_INIT, Init);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_INIT_SDK, InitSDK);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_UNINIT, UnInit);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_CREATE_EFFECT, CreateEffect);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_CREATE_CHROMA_LINK_EFFECT, CreateChromaLinkEffect);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_CREATE_KEYBOARD_EFFECT, CreateKeyboardEffect);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_CREATE_KEYPAD_EFFECT, CreateKeypadEffect);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_CREATE_HEADSET_EFFECT, CreateHeadsetEffect);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_CREATE_MOUSE_EFFECT, CreateMouseEffect);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_CREATE_MOUSEPAD_EFFECT, CreateMousepadEffect);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_SET_EFFECT, SetEffect);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_DELETE_EFFECT, DeleteEffect);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_REGISTER_EVENT_NOTIFICATION, RegisterEventNotification);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_UNREGISTER_EVENT_NOTIFICATION, UnregisterEventNotification);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_QUERY_DEVICE, QueryDevice);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_IS_ACTIVE, IsActive);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_IS_CONNECTED, IsConnected);
CHROMASDK_DECLARE_METHOD_IMPL(CHROMA_SDK_SET_EVENT_NAME, SetEventName);
// validate static methods
#undef CHROMASDK_VALIDATE_METHOD
#define CHROMASDK_VALIDATE_METHOD(Signature, FieldName) if (_sMethod ## FieldName == nullptr) \
{ \
_sMethod ## FieldName = (Signature) GetProcAddress(_sLibrary, #FieldName); \
if (_sMethod ## FieldName == nullptr) \
{ \
fprintf(stderr, "RzChromaSDK: Method not available! %s\r\n", #FieldName); \
FreeLibrary(_sLibrary); \
_sLibrary = NULL; \
return RZRESULT_FAILED; \
} \
}
RZRESULT RzChromaSDK::Init()
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodInit();
}
RZRESULT RzChromaSDK::InitSDK(ChromaSDK::APPINFOTYPE* AppInfo)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodInitSDK(AppInfo);
}
RZRESULT RzChromaSDK::UnInit()
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodUnInit();
}
RZRESULT RzChromaSDK::CreateEffect(RZDEVICEID DeviceId, ChromaSDK::EFFECT_TYPE Effect, PRZPARAM pParam, RZEFFECTID *pEffectId)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodCreateEffect(DeviceId, Effect, pParam, pEffectId);
}
RZRESULT RzChromaSDK::CreateChromaLinkEffect(ChromaSDK::ChromaLink::EFFECT_TYPE Effect, PRZPARAM pParam, RZEFFECTID *pEffectId)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodCreateChromaLinkEffect(Effect, pParam, pEffectId);
}
RZRESULT RzChromaSDK::CreateKeyboardEffect(ChromaSDK::Keyboard::EFFECT_TYPE Effect, PRZPARAM pParam, RZEFFECTID *pEffectId)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodCreateKeyboardEffect(Effect, pParam, pEffectId);
}
RZRESULT RzChromaSDK::CreateKeypadEffect(ChromaSDK::Keypad::EFFECT_TYPE Effect, PRZPARAM pParam, RZEFFECTID *pEffectId)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodCreateKeypadEffect(Effect, pParam, pEffectId);
}
RZRESULT RzChromaSDK::CreateHeadsetEffect(ChromaSDK::Headset::EFFECT_TYPE Effect, PRZPARAM pParam, RZEFFECTID *pEffectId)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodCreateHeadsetEffect(Effect, pParam, pEffectId);
}
RZRESULT RzChromaSDK::CreateMouseEffect(ChromaSDK::Mouse::EFFECT_TYPE Effect, PRZPARAM pParam, RZEFFECTID *pEffectId)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodCreateMouseEffect(Effect, pParam, pEffectId);
}
RZRESULT RzChromaSDK::CreateMousepadEffect(ChromaSDK::Mousepad::EFFECT_TYPE Effect, PRZPARAM pParam, RZEFFECTID *pEffectId)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodCreateMousepadEffect(Effect, pParam, pEffectId);
}
RZRESULT RzChromaSDK::SetEffect(RZEFFECTID EffectId)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodSetEffect(EffectId);
}
RZRESULT RzChromaSDK::DeleteEffect(RZEFFECTID EffectId)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodDeleteEffect(EffectId);
}
RZRESULT RzChromaSDK::RegisterEventNotification(HWND hWnd)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodRegisterEventNotification(hWnd);
}
RZRESULT RzChromaSDK::UnregisterEventNotification(void)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodUnregisterEventNotification();
}
RZRESULT RzChromaSDK::QueryDevice(RZDEVICEID DeviceId, ChromaSDK::DEVICE_INFO_TYPE &DeviceInfo)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodQueryDevice(DeviceId, DeviceInfo);
}
RZRESULT RzChromaSDK::IsActive(BOOL& Active)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodIsActive(Active);
}
RZRESULT RzChromaSDK::IsConnected(ChromaSDK::DEVICE_INFO_TYPE& DeviceInfo)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodIsConnected(DeviceInfo);
}
RZRESULT RzChromaSDK::SetEventName(LPCTSTR Name)
{
RZRESULT state = GetLibraryLoadedState();
if (state != RZRESULT_SUCCESS)
{
return state;
}
return _sMethodSetEventName(Name);
}
// EXTRA METHODS
RZRESULT RzChromaSDK::GetLibraryLoadedState()
{
// abort load if an invalid signature was detected
if (_sInvalidSignature)
{
return RZRESULT_DLL_INVALID_SIGNATURE;
}
// library has loaded and validation is complete
if (_sLoaded)
{
return RZRESULT_SUCCESS;
}
// load the library if previously not loaded
if (_sLibrary == NULL)
{
wchar_t pathTemp[MAX_PATH];
GetModuleFileNameW(NULL, pathTemp, sizeof(pathTemp));
std::wstring strPathCurrent;
const size_t last_slash_idx = std::wstring(pathTemp).rfind('\\');
if (std::string::npos != last_slash_idx)
{
strPathCurrent = std::wstring(pathTemp).substr(0, last_slash_idx);
}
#ifdef USE_CHROMA_CLOUD
std::wstring path = strPathCurrent;
path += L"\\";
path += CHROMASDKDLL;
// check the library file version
if (!VerifyLibrarySignature::IsFileVersionSameOrNewer(path, 1, 0, 0, 10))
{
ChromaLogger::fprintf(stderr, "Detected old version of Chroma SDK Library!\r\n");
return RZRESULT_DLL_NOT_FOUND;
}
#ifdef CHECK_CHROMA_LIBRARY_SIGNATURE
// verify the library has a valid signature
_sInvalidSignature = !VerifyLibrarySignature::VerifyModule(path);
#endif
if (_sInvalidSignature)
{
ChromaLogger::fprintf(stderr, "Chroma SDK Library has an invalid signature!\r\n");
return RZRESULT_DLL_INVALID_SIGNATURE;
}
#else
// DLL Search Order:
// 1. The directory from which the application loaded.
// 2. The system directory.Use the GetSystemDirectory function to get the path of this directory.
// 3. The 16 - bit system directory.There is no function that obtains the path of this directory, but it is searched.
// 4. The Windows directory.Use the GetWindowsDirectory function to get the path of this directory.
// 5. The current directory.
// 6. The directories that are listed in the PATH environment variable.Note that this does not include the per - application path specified by the App Paths registry key.The App Paths key is not used when computing the DLL search path.
std::vector<std::wstring> searchPaths;
// #1
searchPaths.push_back(strPathCurrent);
// #2
if (GetSystemDirectory(pathTemp, sizeof(pathTemp)))
{
std::wstring strPathSys = pathTemp;
searchPaths.push_back(strPathSys);
}
// #4
if (GetWindowsDirectory(pathTemp, sizeof(pathTemp)))
{
std::wstring strPathWin = pathTemp;
searchPaths.push_back(strPathWin);
}
// #6
DWORD envMaxSize = 65535; //Limit according to http://msdn.microsoft.com/en-us/library/ms683188.aspx
std::wstring strEnvPath;
strEnvPath.resize(envMaxSize);
if (GetEnvironmentVariableW(L"PATH", &strEnvPath[0], envMaxSize))
{
const char delimiter = ';';
size_t previous = 0;
size_t index = strEnvPath.find(delimiter);
while (index != std::wstring::npos)
{
searchPaths.push_back(strEnvPath.substr(previous, index - previous));
previous = index + 1;
index = strEnvPath.find(delimiter, previous);
}
searchPaths.push_back(strEnvPath.substr(previous));
}
// search DLL paths to check version and signature
for (std::vector<std::wstring>::iterator it = searchPaths.begin(); it != searchPaths.end(); ++it)
{
std::wstring strPathSearch = *it;
if (strPathSearch.length() > 0 && strPathSearch.compare(strPathSearch.length() - 1, 1, L"\\") != 0) //not endsWith slash
{
strPathSearch += L"\\";
}
strPathSearch += CHROMASDKDLL;
// check if DLL exists in search path
std::filesystem::path p = strPathSearch.c_str();
std::error_code pathError;
if (!std::filesystem::exists(p, pathError))
{
// not found
continue;
}
// check the library file version
if (!VerifyLibrarySignature::IsFileVersionSameOrNewer(strPathSearch, 1, 0, 0, 6))
{
ChromaLogger::fprintf(stderr, "Detected old version of Chroma SDK Library!\r\n");
return RZRESULT_DLL_NOT_FOUND;
}
#ifdef CHECK_CHROMA_LIBRARY_SIGNATURE
// verify the library has a valid signature
_sInvalidSignature = !VerifyLibrarySignature::VerifyModule(strPathSearch);
#endif
if (_sInvalidSignature)
{
ChromaLogger::fprintf(stderr, "Chroma SDK Library has an invalid signature!\r\n");
return RZRESULT_DLL_INVALID_SIGNATURE;
}
break;
}
std::wstring path = CHROMASDKDLL;
#endif
// load the library
HMODULE library = LoadLibrary(path.c_str());
if (library == NULL)
{
ChromaLogger::fprintf(stderr, "Failed to load Chroma SDK Library!\r\n");
return RZRESULT_DLL_NOT_FOUND;
}
_sLibrary = library;
}
// CORE API METHODS
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_INIT, Init);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_INIT_SDK, InitSDK);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_UNINIT, UnInit);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_CREATE_EFFECT, CreateEffect);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_CREATE_CHROMA_LINK_EFFECT, CreateChromaLinkEffect);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_CREATE_KEYBOARD_EFFECT, CreateKeyboardEffect);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_CREATE_KEYPAD_EFFECT, CreateKeypadEffect);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_CREATE_HEADSET_EFFECT, CreateHeadsetEffect);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_CREATE_MOUSE_EFFECT, CreateMouseEffect);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_CREATE_MOUSEPAD_EFFECT, CreateMousepadEffect);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_SET_EFFECT, SetEffect);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_DELETE_EFFECT, DeleteEffect);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_REGISTER_EVENT_NOTIFICATION, RegisterEventNotification);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_UNREGISTER_EVENT_NOTIFICATION, UnregisterEventNotification);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_QUERY_DEVICE, QueryDevice);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_IS_ACTIVE, IsActive);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_IS_CONNECTED, IsConnected);
CHROMASDK_VALIDATE_METHOD(CHROMA_SDK_SET_EVENT_NAME, SetEventName);
_sLoaded = true;
return RZRESULT_SUCCESS;
}
void RzChromaSDK::Unload()
{
if (!_sLoaded)
{
return;
}
if (_sLibrary != nullptr)
{
FreeLibrary(_sLibrary);
_sLibrary = nullptr;
}
_sLoaded = false;
}