-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
583 lines (491 loc) · 14.5 KB
/
main.c
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
* Copyright 2014 The ChromiumOS Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <libtsm.h>
#include <memory.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "dbus.h"
#include "dbus_interface.h"
#include "dev.h"
#include "input.h"
#include "main.h"
#include "splash.h"
#include "term.h"
#include "util.h"
#define DBUS_WAIT_DELAY_US 50000
/* Splash screen */
splash_t* splash;
#define FLAG_CLEAR 'c'
#define FLAG_DAEMON 'd'
#define FLAG_ENABLE_OSC 'G'
#define FLAG_ENABLE_VT1 '1'
#define FLAG_ENABLE_VTS 'e'
#define FLAG_FRAME_INTERVAL 'f'
#define FLAG_HELP 'h'
#define FLAG_IMAGE 'i'
#define FLAG_IMAGE_HIRES 'I'
#define FLAG_LOOP_COUNT 'C'
#define FLAG_LOOP_START 'l'
#define FLAG_LOOP_INTERVAL 'L'
#define FLAG_LOOP_OFFSET 'o'
#define FLAG_NUM_VTS 'N'
#define FLAG_NO_LOGIN 'n'
#define FLAG_OFFSET 'O'
#define FLAG_PRE_CREATE_VTS 'P'
#define FLAG_PRINT_RESOLUTION 'p'
#define FLAG_SCALE 'S'
#define FLAG_SPLASH_ONLY 's'
#define FLAG_WAIT_DROP_MASTER 'W'
static const struct option command_options[] = {
{ "clear", required_argument, NULL, FLAG_CLEAR },
{ "daemon", no_argument, NULL, FLAG_DAEMON },
{ "dev-mode", no_argument, NULL, FLAG_ENABLE_VTS },
{ "enable-gfx", no_argument, NULL, FLAG_ENABLE_OSC },
{ "enable-osc", no_argument, NULL, FLAG_ENABLE_OSC },
{ "enable-vt1", no_argument, NULL, FLAG_ENABLE_VT1 },
{ "enable-vts", no_argument, NULL, FLAG_ENABLE_VTS },
{ "frame-interval", required_argument, NULL, FLAG_FRAME_INTERVAL },
{ "help", no_argument, NULL, FLAG_HELP },
{ "image", required_argument, NULL, FLAG_IMAGE },
{ "image-hires", required_argument, NULL, FLAG_IMAGE_HIRES },
{ "loop-count", required_argument, NULL, FLAG_LOOP_COUNT },
{ "loop-start", required_argument, NULL, FLAG_LOOP_START },
{ "loop-interval", required_argument, NULL, FLAG_LOOP_INTERVAL },
{ "loop-offset", required_argument, NULL, FLAG_LOOP_OFFSET },
{ "num-vts", required_argument, NULL, FLAG_NUM_VTS },
{ "no-login", no_argument, NULL, FLAG_NO_LOGIN },
{ "offset", required_argument, NULL, FLAG_OFFSET },
{ "print-resolution", no_argument, NULL, FLAG_PRINT_RESOLUTION },
{ "pre-create-vts", no_argument, NULL, FLAG_PRE_CREATE_VTS },
{ "scale", required_argument, NULL, FLAG_SCALE },
{ "splash-only", no_argument, NULL, FLAG_SPLASH_ONLY },
{ "wait-drop-master", no_argument, NULL, FLAG_WAIT_DROP_MASTER },
{ NULL, 0, NULL, 0 }
};
static const char * const command_help[] = {
"Splash screen clear color.",
"Daemonize frecon.",
"Force dev mode behavior (deprecated, use --enable-vts).",
"Enable image and box drawing OSC escape codes (deprecated, use --enable-osc).",
"Enable OSC escape codes for graphics and input control.",
"Enable switching to VT1 and keep a terminal on it.",
"Enable additional terminals beyond VT1.",
"Default time (in msecs) between splash animation frames.",
"This help screen!",
"Image (low res) to use for splash animation.",
"Image (hi res) to use for splash animation.",
"Number of times to loop splash animations (unset = forever).",
"First frame to start the splash animation loop (and enable looping).",
"Pause time (in msecs) between splash animation frames.",
"Offset (as x,y) for centering looped image.",
"Number of enabled VTs. The default is 4, the maximum is 12.",
"Do not display login prompt on additional VTs.",
"Absolute location of the splash image on screen (as x,y).",
"(Deprecated) Print detected screen resolution and exit.",
"Create all VTs immediately instead of on-demand.",
"Default scale for splash screen images.",
"Exit immediately after finishing splash animation.",
"Wait to drop DRM master until the escape code is received.",
};
static void usage(int status)
{
FILE *out = status ? stderr : stdout;
static_assert(ARRAY_SIZE(command_help) == ARRAY_SIZE(command_options) - 1,
"The help & option arrays need resyncing");
fprintf(out,
"Frecon: The Freon based console daemon.\n"
"\n"
"Usage: frecon [options] [splash images]\n"
"\n"
"Options:\n"
);
/* Output all the options & help text, and auto-align them. */
int len;
for (int i = 0; command_options[i].name; ++i) {
len = fprintf(out, " -%c, --%s ",
command_options[i].val, command_options[i].name);
if (command_options[i].has_arg == required_argument)
len += fprintf(out, "<arg> ");
fprintf(out, "%*s %s\n", (30 - len), "", command_help[i]);
}
fprintf(out, "\nFor more detailed documentation, visit:\n"
"https://chromium.googlesource.com/chromiumos/platform/frecon/+/master\n");
exit(status);
}
commandflags_t command_flags = { 0 };
static void parse_offset(char* param, int32_t* x, int32_t* y)
{
char* token;
char* saveptr;
token = strtok_r(param, ",", &saveptr);
if (token)
*x = strtol(token, NULL, 0);
token = strtok_r(NULL, ",", &saveptr);
if (token)
*y = strtol(token, NULL, 0);
}
static void main_on_login_prompt_visible(void)
{
if (command_flags.daemon && !command_flags.enable_vts) {
LOG(INFO, "Chrome started, our work is done, exiting.");
exit(EXIT_SUCCESS);
} else {
if (splash) {
splash_destroy(splash);
splash = NULL;
}
if (command_flags.enable_vt1)
LOG(WARNING, "VT1 enabled and Chrome is active!");
}
}
int main_process_events(uint32_t usec)
{
terminal_t* terminal;
terminal_t* new_terminal;
fd_set read_set, exception_set;
int maxfd = -1;
int sstat;
struct timeval tm;
struct timeval* ptm;
terminal = term_get_current_terminal();
FD_ZERO(&read_set);
FD_ZERO(&exception_set);
dbus_add_fds(&read_set, &exception_set, &maxfd);
input_add_fds(&read_set, &exception_set, &maxfd);
dev_add_fds(&read_set, &exception_set, &maxfd);
for (unsigned i = 0; i < term_num_terminals; i++) {
terminal_t* current_term = term_get_terminal(i);
if (term_is_valid(current_term))
term_add_fds(current_term, &read_set, &exception_set, &maxfd);
}
if (usec) {
ptm = &tm;
tm.tv_sec = 0;
tm.tv_usec = usec;
} else
ptm = NULL;
sstat = select(maxfd + 1, &read_set, NULL, &exception_set, ptm);
if (sstat == 0)
return 0;
dbus_dispatch_io();
if (term_exception(terminal, &exception_set))
return -1;
dev_dispatch_io(&read_set, &exception_set);
input_dispatch_io(&read_set, &exception_set);
for (unsigned i = 0; i < term_num_terminals; i++) {
terminal_t* current_term = term_get_terminal(i);
if (term_is_valid(current_term))
term_dispatch_io(current_term, &read_set);
}
/* Could have changed in input dispatch. */
terminal = term_get_current_terminal();
/* Restart terminal on which child has exited. We don't want possible garbage settings from previous session to remain. */
if (term_is_valid(terminal)) {
if (term_is_child_done(terminal)) {
if (terminal == term_get_terminal(TERM_SPLASH_TERMINAL) && !command_flags.enable_vt1) {
/* Let the old term be, splash_destroy will clean it up. */
return 0;
}
term_set_current_terminal(term_init(term_get_current(), -1));
new_terminal = term_get_current_terminal();
if (!term_is_valid(new_terminal)) {
return -1;
}
term_activate(new_terminal);
term_close(terminal);
}
}
return 0;
}
uint32_t get_process_events_timeout()
{
uint32_t usec = 0;
if (!dbus_is_initialized()) {
/*
* The DBUS service launches later than the boot-splash service, and
* as a result, when splash_run starts DBUS is not yet up. Keep
* trying to initialize it while we're waiting.
*/
if (dbus_init()) {
LOG(INFO, "DBUS initialized.");
/*
* Ask DBUS to call us back so we can quit when login prompt is
* visible.
*/
dbus_set_login_prompt_visible_callback(
main_on_login_prompt_visible);
/*
* Ask DBUS to notify us when suspend has finished so monitors
* can be reprobed in case they changed during suspend.
*/
dbus_set_suspend_done_callback(term_suspend_done, NULL);
} else {
/*
* Wait a bit for DBus to come up, so we don't flood the system
* with requests.
*/
usec = DBUS_WAIT_DELAY_US;
}
}
return usec;
}
int main_loop(void)
{
int status;
while (1) {
uint32_t usec = get_process_events_timeout();
status = main_process_events(usec);
if (status != 0) {
LOG(ERROR, "Input process returned %d.", status);
break;
}
}
return 0;
}
bool set_drm_master_relax(void)
{
int fd;
int num_written;
/*
* Setting drm_master_relax flag in kernel allows us to transfer DRM master
* between Chrome and frecon.
*/
fd = open("/sys/kernel/debug/dri/drm_master_relax", O_WRONLY);
if (fd != -1) {
num_written = write(fd, "Y", 1);
close(fd);
if (num_written != 1) {
LOG(ERROR, "Unable to set drm_master_relax.");
return false;
}
} else {
LOG(ERROR, "Unable to open drm_master_relax.");
return false;
}
return true;
}
static void legacy_print_resolution(int argc, char* argv[])
{
int c;
optind = 1;
opterr = 0;
for (;;) {
c = getopt_long(argc, argv, "", command_options, NULL);
if (c == -1) {
break;
} else if (c == FLAG_PRINT_RESOLUTION) {
drm_t *drm = drm_scan();
if (!drm)
exit(EXIT_FAILURE);
printf("%d %d", drm_gethres(drm),
drm_getvres(drm));
drm_delref(drm);
exit(EXIT_SUCCESS);
}
}
}
int main(int argc, char* argv[])
{
int ret;
int c;
int pts_fd;
unsigned vt;
int32_t x, y;
drm_t* drm;
legacy_print_resolution(argc, argv);
fix_stdio();
pts_fd = posix_openpt(O_RDWR | O_NOCTTY | O_CLOEXEC | O_NONBLOCK);
optind = 1;
opterr = 1;
for (;;) {
c = getopt_long(argc, argv, "", command_options, NULL);
if (c == -1)
break;
switch (c) {
case FLAG_DAEMON:
command_flags.daemon = true;
break;
case FLAG_ENABLE_OSC:
command_flags.enable_osc = true;
break;
case FLAG_ENABLE_VT1:
command_flags.enable_vt1 = true;
break;
case FLAG_ENABLE_VTS:
command_flags.enable_vts = true;;
break;
case FLAG_NO_LOGIN:
command_flags.no_login = true;
break;
case FLAG_NUM_VTS:
term_set_num_terminals(strtoul(optarg, NULL, 0));
break;
case FLAG_PRE_CREATE_VTS:
command_flags.pre_create_vts = true;
break;
case FLAG_SPLASH_ONLY:
command_flags.splash_only = true;
break;
case FLAG_WAIT_DROP_MASTER:
command_flags.wait_drop_master = true;
break;
case FLAG_HELP:
usage(0);
break;
case '?':
usage(1);
break;
}
}
/* Remove all stale VT links. */
for (vt = 0; vt < TERM_MAX_TERMINALS; vt++) {
char path[32];
snprintf(path, sizeof(path), FRECON_VT_PATH, vt);
unlink(path);
}
/* And PID file. */
unlink(FRECON_PID_FILE);
/* And hi-res file. */
unlink(FRECON_HI_RES_FILE);
/* And current terminal. */
unlink(FRECON_CURRENT_VT);
if (command_flags.daemon) {
int status;
daemonize(command_flags.pre_create_vts);
status = mkdir(FRECON_RUN_DIR, S_IRWXU | S_IRWXG);
if (status == 0 || (status < 0 && errno == EEXIST)) {
char pids[32];
sprintf(pids, "%u", getpid());
write_string_to_file(FRECON_PID_FILE, pids);
}
}
ret = input_init();
if (ret) {
LOG(ERROR, "Input init failed.");
return EXIT_FAILURE;
}
ret = dev_init();
if (ret) {
LOG(ERROR, "Device management init failed.");
return EXIT_FAILURE;
}
drm_set(drm = drm_scan());
splash = splash_init(pts_fd);
if (splash == NULL) {
LOG(ERROR, "Splash init failed.");
return EXIT_FAILURE;
} else {
int status;
status = mkdir(FRECON_RUN_DIR, S_IRWXU | S_IRWXG);
if (status == 0 || (status < 0 && errno == EEXIST)) {
char hires[32];
sprintf(hires, "%u", splash_is_hires(splash));
write_string_to_file(FRECON_HI_RES_FILE, hires);
}
}
if (command_flags.pre_create_vts) {
for (unsigned vt = command_flags.enable_vt1 ? TERM_SPLASH_TERMINAL : 1;
vt < (command_flags.enable_vts ? term_num_terminals : 1); vt++) {
terminal_t *terminal = term_get_terminal(vt);
if (!terminal) {
terminal = term_init(vt, -1);
term_set_terminal(vt, terminal);
}
}
}
if (command_flags.daemon && command_flags.pre_create_vts)
daemon_exit_code(EXIT_SUCCESS);
/* These flags can be only processed after splash object has been created. */
optind = 1;
for (;;) {
c = getopt_long(argc, argv, "", command_options, NULL);
if (c == -1)
break;
switch (c) {
case FLAG_CLEAR:
splash_set_clear(splash, strtoul(optarg, NULL, 0));
break;
case FLAG_FRAME_INTERVAL:
splash_set_default_duration(splash, strtoul(optarg, NULL, 0));
break;
case FLAG_IMAGE:
if (!splash_is_hires(splash))
splash_add_image(splash, optarg);
break;
case FLAG_IMAGE_HIRES:
if (splash_is_hires(splash))
splash_add_image(splash, optarg);
break;
case FLAG_LOOP_COUNT:
splash_set_loop_count(splash, strtoul(optarg, NULL, 0));
break;
case FLAG_LOOP_START:
splash_set_loop_start(splash, strtoul(optarg, NULL, 0));
break;
case FLAG_LOOP_INTERVAL:
splash_set_loop_duration(splash, strtoul(optarg, NULL, 0));
break;
case FLAG_LOOP_OFFSET:
parse_offset(optarg, &x, &y);
splash_set_loop_offset(splash, x, y);
break;
case FLAG_OFFSET:
parse_offset(optarg, &x, &y);
splash_set_offset(splash, x, y);
break;
case FLAG_SCALE:
splash_set_scale(splash, strtoul(optarg, NULL, 0));
break;
}
}
for (int i = optind; i < argc; i++)
splash_add_image(splash, argv[i]);
if (drm && splash_num_images(splash) > 0) {
ret = splash_run(splash);
if (ret) {
LOG(ERROR, "Splash_run failed: %d.", ret);
if (!command_flags.daemon)
return EXIT_FAILURE;
}
}
if (!command_flags.daemon) {
splash_destroy(splash);
splash = NULL;
}
if (command_flags.splash_only)
goto main_done;
if (command_flags.daemon) {
if (command_flags.enable_vts)
set_drm_master_relax();
if (command_flags.enable_vt1)
term_switch_to(TERM_SPLASH_TERMINAL);
else if (!command_flags.wait_drop_master)
term_background(true);
} else {
/* Create and switch to first term in interactve mode. */
set_drm_master_relax();
term_switch_to(command_flags.enable_vt1 ? TERM_SPLASH_TERMINAL : 1);
}
ret = main_loop();
main_done:
input_close();
dev_close();
dbus_destroy();
drm_close();
if (command_flags.daemon)
unlink(FRECON_PID_FILE);
unlink(FRECON_HI_RES_FILE);
unlink(FRECON_CURRENT_VT);
return ret;
}