Repository navigation
Expand file tree
/
Copy pathmain_FreeBSD_Linux_MacOS.cpp
More file actions
145 lines (125 loc) · 5.24 KB
/
Copy pathmain_FreeBSD_Linux_MacOS.cpp
File metadata and controls
145 lines (125 loc) · 5.24 KB
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
/*---------------------------------------------------------*\
| main_FreeBSD_Linux_MacOS.cpp |
| |
| Entry point for the OpenRGB application |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include "cli.h"
#include "DetectionManager.h"
#include "ResourceManager.h"
#include "NetworkServer.h"
#include "LogManager.h"
#include "startup.h"
#ifdef _MACOSX_X86_X64
#include "macUSPCIOAccess.h"
io_connect_t macUSPCIO_driver_connection;
#endif
#ifdef __APPLE__
#include "macutils.h"
#endif
#include <condition_variable>
#include <mutex>
using namespace std::chrono_literals;
#include <csignal>
static volatile bool service_stop_requested = false;
static std::mutex service_stop_mutex;
static std::condition_variable service_stop_cv;
static void sigHandler(int /*s*/)
{
service_stop_requested = true;
service_stop_cv.notify_one();
}
/*---------------------------------------------------------*\
| WaitWhileServerOnline |
| |
| Wait while NetworkServer is online and return only when |
| it has shut down |
\*---------------------------------------------------------*/
void WaitWhileServerOnline(NetworkServer* srv)
{
std::unique_lock<std::mutex> lock(service_stop_mutex);
while(srv->GetOnline())
{
if(service_stop_requested)
{
srv->StopServer();
break;
}
service_stop_cv.wait_for(lock, 1s);
};
}
/*---------------------------------------------------------*\
| main |
| |
| Entry point, calls the startup processing |
\*---------------------------------------------------------*/
int main(int argc, char* argv[])
{
/*-----------------------------------------------------*\
| Mac x86/x64 only - Install SMBus Driver macUSPCIO |
\*-----------------------------------------------------*/
#ifdef _MACOSX_X86_X64
InitMacUSPCIODriver();
#endif
/*-----------------------------------------------------*\
| Perform CLI pre-detection processing to get return |
| flags |
\*-----------------------------------------------------*/
unsigned int ret_flags = cli_pre_detection(argc, argv);
/*-----------------------------------------------------*\
| Initialize ResourceManager |
\*-----------------------------------------------------*/
ResourceManager::get()->Initialize(
!(ret_flags & RET_FLAG_NO_AUTO_CONNECT),
!(ret_flags & RET_FLAG_NO_DETECT),
ret_flags & RET_FLAG_START_SERVER,
ret_flags & RET_FLAG_CLI_POST_DETECTION,
ret_flags & RET_FLAG_START_GUI);
/*-----------------------------------------------------*\
| If running as a headless server, register signal |
| handler for stopping server |
\*-----------------------------------------------------*/
if((ret_flags & RET_FLAG_START_SERVER) && !(ret_flags & RET_FLAG_START_GUI))
{
std::signal(SIGINT, sigHandler);
std::signal(SIGTERM, sigHandler);
}
/*-----------------------------------------------------*\
| Perform application startup and run the application. |
| This call returns only when the GUI application is |
| closing or if not running the GUI. |
\*-----------------------------------------------------*/
int exitval = startup(argc, argv, ret_flags);
/*-----------------------------------------------------*\
| If started in headless server mode, wait until server |
| shuts down before closing application. |
\*-----------------------------------------------------*/
if((ret_flags & RET_FLAG_START_SERVER) && !(ret_flags & RET_FLAG_START_GUI))
{
NetworkServer* server = ResourceManager::get()->GetServer();
if(server)
{
WaitWhileServerOnline(server);
}
}
/*-----------------------------------------------------*\
| Call ServiceShutdown to allow operations before |
| controllers are closed and deleted. Only runs when |
| running as a background service (headless server). |
\*-----------------------------------------------------*/
ResourceManager::get()->ServiceShutdown();
/*-----------------------------------------------------*\
| Clean up detected devices so destructors can run. |
\*-----------------------------------------------------*/
DetectionManager::get()->Cleanup();
LOG_TRACE("OpenRGB finishing with exit code %d", exitval);
/*-----------------------------------------------------*\
| Mac x86/x64 only - Uninstall SMBus Driver macUSPCIO |
\*-----------------------------------------------------*/
#ifdef _MACOSX_X86_X64
CloseMacUSPCIODriver();
#endif
return exitval;
}