forked from CalcProgrammer1/OpenRGB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
400 lines (350 loc) · 14.3 KB
/
main.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
/******************************************************************************************\
* *
* main.cpp *
* *
* Main function for OpenAuraSDK GUI project *
* *
\******************************************************************************************/
#include "ResourceManager.h"
#include "NetworkClient.h"
#include "NetworkServer.h"
#include "ProfileManager.h"
#include "RGBController.h"
#include "i2c_smbus.h"
#include "LogManager.h"
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <thread>
#ifdef _MACOSX_X86_X64
#include "macUSPCIOAccess.h"
io_connect_t macUSPCIO_driver_connection;
#endif
#include "OpenRGBDialog2.h"
#ifdef __APPLE__
#include "macutils.h"
#endif
using namespace std::chrono_literals;
/*-------------------------------------------------------------*\
| Command line functionality and return flags |
\*-------------------------------------------------------------*/
extern unsigned int cli_pre_detection(int argc, char *argv[]);
extern unsigned int cli_post_detection(int argc, char *argv[]);
enum
{
RET_FLAG_PRINT_HELP = 1,
RET_FLAG_START_GUI = 2,
RET_FLAG_I2C_TOOLS = 4,
RET_FLAG_START_MINIMIZED = 8,
RET_FLAG_NO_DETECT = 16,
RET_FLAG_CLI_POST_DETECTION = 32,
RET_FLAG_START_SERVER = 64,
RET_FLAG_NO_AUTO_CONNECT = 128,
};
/******************************************************************************************\
* *
* InitializeTimerResolution (Win32) *
* *
* On Windows, the default timer resolution is 15.6ms. For higher accuracy delays, *
* the timer resolution should be set to a shorter interval. The shortest interval *
* that can be set is 0.5ms. *
* *
\******************************************************************************************/
#ifdef _WIN32
typedef unsigned int NTSTATUS;
typedef NTSTATUS (*NTSETTIMERRESOLUTION)(ULONG DesiredResolution, BOOLEAN SetResolution, PULONG CurrentResolution);
void InitializeTimerResolution()
{
NTSETTIMERRESOLUTION NtSetTimerResolution;
HMODULE NtDllHandle;
ULONG CurrentResolution;
NtDllHandle = LoadLibrary("ntdll.dll");
NtSetTimerResolution = (NTSETTIMERRESOLUTION)GetProcAddress(NtDllHandle, "NtSetTimerResolution");
NtSetTimerResolution(5000, TRUE, &CurrentResolution);
}
void InitializeTimerResolutionThreadFunction()
{
while(1)
{
InitializeTimerResolution();
std::this_thread::sleep_for(500ms);
}
}
#endif
void WaitWhileServerOnline(NetworkServer* srv)
{
while (srv->GetOnline())
{
std::this_thread::sleep_for(1s);
};
}
/******************************************************************************************\
* *
* AttemptLocalConnection *
* *
* Attempts an SDK connection to the local server. Returns true if success *
* *
\******************************************************************************************/
bool AttemptLocalConnection()
{
bool success = false;
NetworkClient * client = new NetworkClient(ResourceManager::get()->GetRGBControllers());
std::string titleString = "OpenRGB ";
titleString.append(VERSION_STRING);
client->SetName(titleString.c_str());
client->StartClient();
for(int timeout = 0; timeout < 10; timeout++)
{
if(client->GetConnected())
{
break;
}
std::this_thread::sleep_for(5ms);
}
if(!client->GetConnected())
{
client->StopClient();
delete client;
client = NULL;
}
else
{
ResourceManager::get()->RegisterNetworkClient(client);
success = true;
/*-----------------------------------------------------*\
| Wait up to 5 seconds for the client connection to |
| retrieve all controllers |
\*-----------------------------------------------------*/
for(int timeout = 0; timeout < 1000; timeout++)
{
if(client->GetOnline())
{
break;
}
std::this_thread::sleep_for(5ms);
}
}
return success;
}
/******************************************************************************************\
* *
* Install SMBus Driver WinRing0, If not already installed (Win32) *
* *
\******************************************************************************************/
#ifdef _WIN32
void InstallWinRing0()
{
TCHAR winring0_install_location[MAX_PATH]; // driver final location usually C:\windows\system32\drivers\WinRing0x64.sys
uint system_path_length = GetSystemDirectory(winring0_install_location, MAX_PATH);
std::string winring0_filename = "WinRing0.sys";
BOOL bIsWow64 = false;
#if _WIN64
winring0_filename = "WinRing0x64.sys";
#else
BOOL (*fnIsWow64Process)(HANDLE, PBOOL) = (BOOL (__cdecl *)(HANDLE, PBOOL))GetProcAddress(GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
if (fnIsWow64Process)
{
fnIsWow64Process(GetCurrentProcess(),&bIsWow64);
}
if(bIsWow64)
{
winring0_filename = "WinRing0x64.sys";
}
#endif
std::strncat(winring0_install_location, "\\drivers\\", MAX_PATH - system_path_length - 1);
std::strncat(winring0_install_location, winring0_filename.c_str(), MAX_PATH - system_path_length - 10);
std::string driver_name = winring0_filename.substr(0, winring0_filename.size() - 4); // driver name: WinRing0 or WinRing0x64
SC_HANDLE manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (manager)
{
PVOID wow64_fsredirection_OldValue = NULL;
if(bIsWow64)
{
Wow64DisableWow64FsRedirection(&wow64_fsredirection_OldValue);
}
if(INVALID_FILE_ATTRIBUTES == GetFileAttributes(winring0_install_location) && GetLastError()==ERROR_FILE_NOT_FOUND)
{
char module_path_buffer[MAX_PATH];
GetModuleFileNameA(NULL, module_path_buffer, MAX_PATH);
std::string::size_type exe_loc = std::string(module_path_buffer).find_last_of("\\/");
std::string driver_source_path = std::string(module_path_buffer).substr(0, exe_loc + 1) + winring0_filename;
CopyFile(driver_source_path.c_str(), winring0_install_location, true);
}
if(bIsWow64)
{
Wow64RevertWow64FsRedirection(wow64_fsredirection_OldValue);
}
SC_HANDLE service = OpenService(manager, driver_name.c_str(), SERVICE_ALL_ACCESS);
if(!service)
{
std::string service_sys_path = "System32\\Drivers\\" + winring0_filename;
service = CreateService(manager,
driver_name.c_str(),
driver_name.c_str(),
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
service_sys_path.c_str(),
NULL,
NULL,
NULL,
NULL,
NULL);
}
CloseServiceHandle(service);
CloseServiceHandle(manager);
}
}
#endif
/******************************************************************************************\
* *
* main *
* *
* Main function. Detects busses and Aura controllers, then opens the main window *
* *
\******************************************************************************************/
int main(int argc, char* argv[])
{
#ifdef _WIN32
/*---------------------------------------------------------*\
| Windows only - Attach console output |
\*---------------------------------------------------------*/
if (AttachConsole(ATTACH_PARENT_PROCESS))
{
/*---------------------------------------------------------*\
| We are running under some terminal context; otherwise |
| leave the GUI and CRT alone |
\*---------------------------------------------------------*/
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
}
/*---------------------------------------------------------*\
| Windows only - Start timer resolution correction thread |
\*---------------------------------------------------------*/
std::thread * InitializeTimerResolutionThread;
InitializeTimerResolutionThread = new std::thread(InitializeTimerResolutionThreadFunction);
InitializeTimerResolutionThread->detach();
/*---------------------------------------------------------*\
| Windows only - Install SMBus Driver WinRing0 |
\*---------------------------------------------------------*/
InstallWinRing0();
#endif
/*---------------------------------------------------------*\
| Mac x86/x64 only - Install SMBus Driver macUSPCIO |
\*---------------------------------------------------------*/
#ifdef _MACOSX_X86_X64
InitMacUSPCIODriver();
#endif
/*---------------------------------------------------------*\
| Process command line arguments before detection |
\*---------------------------------------------------------*/
unsigned int ret_flags = cli_pre_detection(argc, argv);
/*---------------------------------------------------------*\
| Perform local connection and/or hardware detection if not |
| disabled from CLI |
\*---------------------------------------------------------*/
if(!(ret_flags & RET_FLAG_NO_AUTO_CONNECT))
{
printf("Attempting to connect to local OpenRGB server.\r\n");
if(!AttemptLocalConnection())
{
printf("Local OpenRGB server unavailable.\r\n");
}
else
{
printf("Local OpenRGB server connected, running in client mode\r\n");
ResourceManager::get()->DisableDetection();
}
}
/*---------------------------------------------------------*\
| Perform hardware detection if not disabled from CLI |
\*---------------------------------------------------------*/
if(!(ret_flags & RET_FLAG_NO_DETECT))
{
if(ResourceManager::get()->GetDetectionEnabled())
{
printf("Running standalone.\r\n");
}
ResourceManager::get()->DetectDevices();
}
/*---------------------------------------------------------*\
| Start the server if requested from CLI (this must be done |
| after attempting local connection!) |
\*---------------------------------------------------------*/
if(ret_flags & RET_FLAG_START_SERVER)
{
ResourceManager::get()->GetServer()->StartServer();
if(!ResourceManager::get()->GetServer()->GetOnline())
{
printf("Server failed to start\r\n");
}
}
/*---------------------------------------------------------*\
| Process command line arguments after detection only if the|
| pre-detection parsing indicated it should be run |
\*---------------------------------------------------------*/
if(ret_flags & RET_FLAG_CLI_POST_DETECTION)
{
ret_flags |= cli_post_detection(argc, argv);
}
/*---------------------------------------------------------*\
| If the command line parser indicates that the GUI should |
| run, or if there were no command line arguments, start the|
| GUI. |
\*---------------------------------------------------------*/
if(ret_flags & RET_FLAG_START_GUI)
{
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication a(argc, argv);
/*---------------------------------------------------------*\
| Main UI widget |
\*---------------------------------------------------------*/
Ui::OpenRGBDialog2 dlg;
if(ret_flags & RET_FLAG_I2C_TOOLS)
{
dlg.AddI2CToolsPage();
}
dlg.AddClientTab();
if(ret_flags & RET_FLAG_START_MINIMIZED)
{
#ifdef __APPLE__
MacUtils::ToggleApplicationDocklessState(false);
#endif
dlg.hide();
}
else
{
dlg.show();
}
return a.exec();
}
else
{
if(ret_flags & RET_FLAG_START_SERVER)
{
if(!ResourceManager::get()->GetServer()->GetOnline())
{
#ifdef _MACOSX_X86_X64
CloseMacUSPCIODriver();
#endif
return 1;
}
else
{
WaitWhileServerOnline(ResourceManager::get()->GetServer());
#ifdef _MACOSX_X86_X64
CloseMacUSPCIODriver();
#endif
}
}
else
{
#ifdef _MACOSX_X86_X64
CloseMacUSPCIODriver();
#endif
return 0;
}
}
}