forked from hanzelpeter/dispmanx_vnc
-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.cpp
314 lines (271 loc) · 8.88 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
// VNC server using dispmanx
// TODO: mouse support with inconsistency between fbdev and dispmanx
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <errno.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdint.h>
#include <assert.h>
#include <getopt.h>
#include <libconfig.h++>
#include "Exception.hpp"
#include "UFile.hpp"
#include "DMXResource.hpp"
#include "DMXDisplay.hpp"
#include "DMXVNCServer.hpp"
#include "Logger.hpp"
extern bool terminate;
struct ConfigData
{
uint32_t screen = 0;
bool relative = false;
std::string password;
int port = 0;
bool downscale = false;
bool unsafe = false;
bool fullscreen = false;
bool multiThreaded = false;
bool localhost = false;
bool inetd = false;
bool once = false;
int frameRate = 15;
std::string configFile;
std::string vncParams;
};
void GetConfigData(int argc, char *argv[], ConfigData& configData);
void GetCommandLineConfigData(int argc, char *argv[], ConfigData& configData);
bool ReadConfigFile(const char *programName, const std::string& configFile, ConfigData& configData);
bool TryReadConfigFile(libconfig::Config& config, const std::string& file);
void usage(const char *programName);
void sig_handler(int signo)
{
terminate = true;
}
int main(int argc, char *argv[])
{
int ret = EXIT_SUCCESS;
try
{
Logger::SetDefaultModule("dmxvnc");
if (signal(SIGINT, sig_handler) == SIG_ERR) {
throw Exception( "error setting sighandler");
}
if (signal(SIGTERM, sig_handler) == SIG_ERR) {
throw Exception("error setting sighandler");
}
ConfigData configData;
GetConfigData(argc, argv, configData);
Logger::Get() <<
"Running vnc server with the following settings\n"
"\tframe-rate = " << configData.frameRate << "\n"
"\tdownscale = " << (configData.downscale ? "true" : "false") << "\n"
"\tfullscreen = " << (configData.fullscreen ? "true" : "false") << "\n"
"\tlocalhost = " << (configData.localhost ? "true" : "false") << "\n"
"\tinetd = " << (configData.inetd ? "true" : "false") << "\n"
"\tmulti-threaded = " << (configData.multiThreaded ? "true" : "false") << "\n"
"\tonce = " << (configData.once ? "true" : "false") << "\n"
"\tpassword = " << (configData.password.length() ? "***" : "") << "\n"
"\tport = " << configData.port << "\n"
"\trelative = " << (configData.relative ? "true" : "false") << "\n"
"\tscreen = " << configData.screen << "\n"
"\tunsafe = " << (configData.unsafe ? "true" : "false") << "\n"
"\tvnc-params = " << configData.vncParams;
DMXVNCServer vncServer(configData.frameRate);
vncServer.Run(configData.port, configData.password, configData.screen,
configData.relative, !configData.unsafe, !configData.fullscreen,
configData.multiThreaded, configData.downscale,
configData.localhost,
configData.inetd,
configData.once,
configData.vncParams);
}
catch (HelpException&) {
usage(argv[0]);
}
catch (ParamException&) {
Logger::Get() << "Try '" << argv[0] << "' --help for more information";
ret = EXIT_FAILURE;
}
catch (Exception& e) {
Logger::Get() << "Exception: " << e.what();
ret = EXIT_FAILURE;
}
return ret;
}
void GetConfigData(int argc, char *argv[], ConfigData& configData)
{
ConfigData configDataTemp;
GetCommandLineConfigData(argc, argv, configDataTemp);
if (ReadConfigFile(argv[0], configDataTemp.configFile, configData))
GetCommandLineConfigData(argc, argv, configData);
else
configData = configDataTemp;
}
void GetCommandLineConfigData(int argc, char *argv[], ConfigData& configData)
{
static struct option long_options[] = {
{ "relative", no_argument, nullptr, 'r' },
{ "absolute", no_argument, nullptr, 'a' },
{ "config-file", required_argument, nullptr, 'c' },
{ "downscale", no_argument, nullptr, 'd' },
{ "unsafe", no_argument, nullptr, 'u' },
{ "fullscreen", no_argument, nullptr, 'f' },
{ "localhost", no_argument, nullptr, 'l' },
{ "inetd", no_argument, nullptr, 'i' },
{ "multi-threaded", no_argument, nullptr, 'm' },
{ "once", no_argument, nullptr, 'o' },
{ "password", required_argument, nullptr, 'P' },
{ "port", required_argument, nullptr, 'p' },
{ "screen", required_argument, nullptr, 's' },
{ "frame-rate", required_argument, nullptr, 't' },
{ "vnc-params", required_argument, nullptr, 'v' },
{ "help", no_argument, nullptr, CHAR_MIN - 2 },
{ nullptr, 0, nullptr, 0 }
};
int c;
optind = 1;
while (-1 != (c = getopt_long(argc, argv, "abc:dfilmP:op:rs:t:uv:", long_options, nullptr))) {
switch (c) {
case 'a':
configData.relative = false;
break;
case 'r':
configData.relative = true;
break;
case 'c':
configData.configFile = optarg;
break;
case 'd':
configData.downscale = true;
break;
case 'u':
configData.unsafe = true;
break;
case 'f':
configData.fullscreen = true;
break;
case 'l':
configData.localhost = true;
break;
case 'i':
configData.inetd = true;
break;
case 'm':
configData.multiThreaded = true;
break;
case 'o':
configData.once = true;
break;
case 'P':
configData.password = optarg;
break;
case 'p':
configData.port = atoi(optarg);
break;
case 's':
configData.screen = atoi(optarg);
break;
case 't':
configData.frameRate = atoi(optarg);
break;
case 'v':
configData.vncParams = optarg;
break;
case CHAR_MIN - 2:
throw HelpException();
default:
throw ParamException();
}
}
if (optind < argc) {
Logger::Get() << "Unknown parameter: " << argv[optind];
throw ParamException();
}
}
bool ReadConfigFile(const char *programName, const std::string& configFile, ConfigData& configData)
{
std::string configFileTemp;
libconfig::Config config;
bool readConfig = false;
if (configFile.length()) {
configFileTemp = configFile;
readConfig = TryReadConfigFile(config, configFileTemp);
if (!readConfig)
throw Exception("Unable to read the specified configuration file");
}
else {
configFileTemp = programName;
configFileTemp += ".conf";
readConfig = TryReadConfigFile(config, configFileTemp);
if (!readConfig) {
const char *baseName = basename(programName);
configFileTemp = "/etc/";
configFileTemp += baseName;
configFileTemp += ".conf";
Logger::Get() << "Trying to read configuration file: " << configFileTemp;
//configFileTemp = "/etc/dispmanx_vncserver.conf";
readConfig = TryReadConfigFile(config, configFileTemp);
}
}
if (readConfig) {
Logger::Get() << "Read config file: " << configFileTemp;
config.lookupValue("relative", configData.relative);
config.lookupValue("unsafe", configData.unsafe);
config.lookupValue("downscale", configData.downscale);
config.lookupValue("fullscreen", configData.fullscreen);
config.lookupValue("localhost", configData.localhost);
config.lookupValue("inetd", configData.inetd);
config.lookupValue("multi-threaded", configData.multiThreaded);
config.lookupValue("once", configData.once);
config.lookupValue("password", configData.password);
config.lookupValue("port", configData.port);
config.lookupValue("screen", configData.screen);
config.lookupValue("frame-rate", configData.frameRate);
config.lookupValue("vnc-params", configData.vncParams);
}
else
Logger::Get() << "No config file found";
return readConfig;
}
bool TryReadConfigFile(libconfig::Config& config, const std::string& file)
{
try{
config.readFile(file.c_str());
return true;
}
catch (libconfig::FileIOException&) {
return false;;
}
catch (libconfig::ParseException &e) {
std::stringstream ss;
ss << "Error: " << e.getError() << " on line " << e.getLine() << " while reading configuration file: " << file;
throw Exception(ss.str());
}
}
void usage(const char *programName)
{
std::cerr <<
"Usage: " << programName << " [OPTION]...\n"
"\n"
" -a, --absolute absolute mouse movements\n"
" -c, --config-file=FILE use the specified configuration file\n"
" -d, --downscale downscales the screen to a quarter in vnc\n"
" -f, --fullscreen always runs fullscreen mode\n"
" -i, --inetd stdio instead of listening socket\n"
" -l, --localhost only listens to local ports\n"
" -m, --multi-threaded runs vnc in a separate thread\n"
" -o, --once connect once, then terminate\n"
" -p, --port=PORT makes vnc available on the speficied port\n"
" -P, --password=PASSWORD protects the session with PASSWORD\n"
" -r, --relative relative mouse movements\n"
" -s, --screen=SCREEN opens the specified screen number\n"
" -t, --frame-rate=RATE sets the target frame rate, default is 15\n"
" -u, --unsafe disables more robust handling of resolution\n"
" change at a small performance gain\n"
" -v, --vnc-params parameters to send to libvncserver\n"
" --help displays this help and exit\n";
}