blob: c06e425e5b636653bb1076b131d19549a2a7998d [file] [log] [blame]
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -07001/*
David Sodman8ef20062015-01-06 09:23:40 -08002 * Copyright 2014 The Chromium OS Authors. All rights reserved.
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -07003 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
Dominik Behr46c567f2016-03-08 15:11:48 -08007#include <fcntl.h>
Dominik Behr580462b2014-11-20 13:50:05 -08008#include <getopt.h>
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -07009#include <libtsm.h>
Dominik Behr580462b2014-11-20 13:50:05 -080010#include <memory.h>
11#include <stdbool.h>
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070012#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
Dominik Behr46c567f2016-03-08 15:11:48 -080015#include <sys/stat.h>
16#include <sys/types.h>
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070017
Dominik Behr580462b2014-11-20 13:50:05 -080018#include "dbus.h"
Dominik Behr44e07e62016-01-13 19:43:57 -080019#include "dbus_interface.h"
Dominik Behr5239cca2016-01-21 18:22:04 -080020#include "dev.h"
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070021#include "input.h"
Dominik Behr44e07e62016-01-13 19:43:57 -080022#include "main.h"
Dominik Behr580462b2014-11-20 13:50:05 -080023#include "splash.h"
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070024#include "term.h"
David Sodmanbbcb0522014-09-19 10:34:07 -070025#include "util.h"
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070026
David Sodmanbbcb0522014-09-19 10:34:07 -070027#define FLAG_CLEAR 'c'
28#define FLAG_DAEMON 'd'
Dominik Behr46c567f2016-03-08 15:11:48 -080029#define FLAG_ENABLE_VTS 'e'
David Sodmanbbcb0522014-09-19 10:34:07 -070030#define FLAG_FRAME_INTERVAL 'f'
31#define FLAG_GAMMA 'g'
Dominik Behr32a7c892015-10-09 15:47:53 -070032#define FLAG_IMAGE 'i'
33#define FLAG_IMAGE_HIRES 'I'
Dominik Behrfd9fdda2016-03-28 17:16:45 -070034#define FLAG_LOOP_COUNT 'C'
David Sodman8ef20062015-01-06 09:23:40 -080035#define FLAG_LOOP_START 'l'
36#define FLAG_LOOP_INTERVAL 'L'
37#define FLAG_LOOP_OFFSET 'o'
38#define FLAG_OFFSET 'O'
David Sodmanbbcb0522014-09-19 10:34:07 -070039#define FLAG_PRINT_RESOLUTION 'p'
Dominik Behrfd9fdda2016-03-28 17:16:45 -070040#define FLAG_SPLASH_ONLY 's'
David Sodmanbbcb0522014-09-19 10:34:07 -070041
42static struct option command_options[] = {
43 { "clear", required_argument, NULL, FLAG_CLEAR },
44 { "daemon", no_argument, NULL, FLAG_DAEMON },
Dominik Behr46c567f2016-03-08 15:11:48 -080045 { "dev-mode", no_argument, NULL, FLAG_ENABLE_VTS },
46 { "enable-vts", no_argument, NULL, FLAG_ENABLE_VTS },
David Sodmanbbcb0522014-09-19 10:34:07 -070047 { "frame-interval", required_argument, NULL, FLAG_FRAME_INTERVAL },
48 { "gamma", required_argument, NULL, FLAG_GAMMA },
Dominik Behr32a7c892015-10-09 15:47:53 -070049 { "image", required_argument, NULL, FLAG_IMAGE },
50 { "image-hires", required_argument, NULL, FLAG_IMAGE_HIRES },
Dominik Behrfd9fdda2016-03-28 17:16:45 -070051 { "loop-count", required_argument, NULL, FLAG_LOOP_COUNT },
David Sodman8ef20062015-01-06 09:23:40 -080052 { "loop-start", required_argument, NULL, FLAG_LOOP_START },
53 { "loop-interval", required_argument, NULL, FLAG_LOOP_INTERVAL },
54 { "loop-offset", required_argument, NULL, FLAG_LOOP_OFFSET },
55 { "offset", required_argument, NULL, FLAG_OFFSET },
David Sodmanbbcb0522014-09-19 10:34:07 -070056 { "print-resolution", no_argument, NULL, FLAG_PRINT_RESOLUTION },
Dominik Behrfd9fdda2016-03-28 17:16:45 -070057 { "splash-only", no_argument, NULL, FLAG_SPLASH_ONLY },
David Sodmanbbcb0522014-09-19 10:34:07 -070058 { NULL, 0, NULL, 0 }
59};
60
Dominik Behr46c567f2016-03-08 15:11:48 -080061commandflags_t command_flags = { 0 };
Dominik Behr580462b2014-11-20 13:50:05 -080062
Stéphane Marchesin8fc13522015-12-14 17:02:28 -080063static void parse_offset(char* param, int32_t* x, int32_t* y)
David Sodman8ef20062015-01-06 09:23:40 -080064{
65 char* token;
66 char* saveptr;
67
68 token = strtok_r(param, ",", &saveptr);
69 if (token)
70 *x = strtol(token, NULL, 0);
71
72 token = strtok_r(NULL, ",", &saveptr);
73 if (token)
74 *y = strtol(token, NULL, 0);
75}
David Sodmanbbcb0522014-09-19 10:34:07 -070076
Dominik Behr44e07e62016-01-13 19:43:57 -080077int main_process_events(uint32_t usec)
78{
79 terminal_t* terminal;
80 terminal_t* new_terminal;
81 fd_set read_set, exception_set;
Dominik Behrd7112672016-01-20 16:59:34 -080082 int maxfd = -1;
Dominik Behr44e07e62016-01-13 19:43:57 -080083 int sstat;
84 struct timeval tm;
85 struct timeval* ptm;
86
87 terminal = term_get_current_terminal();
88
89 FD_ZERO(&read_set);
90 FD_ZERO(&exception_set);
91
Dominik Behrd7112672016-01-20 16:59:34 -080092 dbus_add_fds(&read_set, &exception_set, &maxfd);
Dominik Behrd7112672016-01-20 16:59:34 -080093 input_add_fds(&read_set, &exception_set, &maxfd);
Dominik Behr5239cca2016-01-21 18:22:04 -080094 dev_add_fds(&read_set, &exception_set, &maxfd);
Dominik Behr44e07e62016-01-13 19:43:57 -080095
96 for (int i = 0; i < MAX_TERMINALS; i++) {
97 if (term_is_valid(term_get_terminal(i))) {
98 terminal_t* current_term = term_get_terminal(i);
Dominik Behrd7112672016-01-20 16:59:34 -080099 term_add_fds(current_term, &read_set, &exception_set, &maxfd);
Dominik Behr44e07e62016-01-13 19:43:57 -0800100 }
101 }
102
103 if (usec) {
104 ptm = &tm;
105 tm.tv_sec = 0;
106 tm.tv_usec = usec;
107 } else
108 ptm = NULL;
109
Dominik Behrd7112672016-01-20 16:59:34 -0800110 sstat = select(maxfd + 1, &read_set, NULL, &exception_set, ptm);
Dominik Behr44e07e62016-01-13 19:43:57 -0800111 if (sstat == 0)
112 return 0;
113
114 dbus_dispatch_io();
115
116 if (term_exception(terminal, &exception_set))
117 return -1;
118
Dominik Behr5239cca2016-01-21 18:22:04 -0800119 dev_dispatch_io(&read_set, &exception_set);
Dominik Behr44e07e62016-01-13 19:43:57 -0800120 input_dispatch_io(&read_set, &exception_set);
121
122 for (int i = 0; i < MAX_TERMINALS; i++) {
123 if (term_is_valid(term_get_terminal(i))) {
124 terminal_t* current_term = term_get_terminal(i);
125 term_dispatch_io(current_term, &read_set);
126 }
127 }
128
129 if (term_is_valid(terminal)) {
130 if (term_is_child_done(terminal)) {
131 if (terminal == term_get_terminal(SPLASH_TERMINAL)) {
132 /*
133 * Note: reference is not lost because it is still referenced
134 * by the splash_t structure which will ultimately destroy
Dominik Behrb1abcba2016-04-14 14:57:21 -0700135 * it, once it's safe to do so.
Dominik Behr44e07e62016-01-13 19:43:57 -0800136 */
137 term_set_terminal(SPLASH_TERMINAL, NULL);
138 return -1;
139 }
Dominik Behr83010f82016-03-18 18:43:08 -0700140 term_set_current_terminal(term_init(true));
Dominik Behr44e07e62016-01-13 19:43:57 -0800141 new_terminal = term_get_current_terminal();
142 if (!term_is_valid(new_terminal)) {
143 return -1;
144 }
145 term_activate(new_terminal);
146 term_close(terminal);
147 }
148 }
149
150 return 0;
151}
152
Dominik Behr46c567f2016-03-08 15:11:48 -0800153int main_loop(void)
Dominik Behr44e07e62016-01-13 19:43:57 -0800154{
Dominik Behr44e07e62016-01-13 19:43:57 -0800155 int status;
156
Dominik Behr44e07e62016-01-13 19:43:57 -0800157 while (1) {
158 status = main_process_events(0);
159 if (status != 0) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700160 LOG(ERROR, "Input process returned %d.", status);
Dominik Behr44e07e62016-01-13 19:43:57 -0800161 break;
162 }
163 }
164
165 return 0;
166}
167
Dominik Behr46c567f2016-03-08 15:11:48 -0800168bool set_drm_master_relax(void)
169{
170 int fd;
171 int num_written;
172
173 /*
174 * Setting drm_master_relax flag in kernel allows us to transfer DRM master
Dominik Behrb1abcba2016-04-14 14:57:21 -0700175 * between Chrome and frecon.
Dominik Behr46c567f2016-03-08 15:11:48 -0800176 */
177 fd = open("/sys/kernel/debug/dri/drm_master_relax", O_WRONLY);
178 if (fd != -1) {
179 num_written = write(fd, "Y", 1);
180 close(fd);
181 if (num_written != 1) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700182 LOG(ERROR, "Unable to set drm_master_relax.");
Dominik Behr46c567f2016-03-08 15:11:48 -0800183 return false;
184 }
185 } else {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700186 LOG(ERROR, "Unable to open drm_master_relax.");
Dominik Behr46c567f2016-03-08 15:11:48 -0800187 return false;
188 }
189 return true;
190}
191
192static void main_on_login_prompt_visible(void* ptr)
193{
194 if (command_flags.daemon && !command_flags.enable_vts) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700195 LOG(INFO, "Chrome started, our work is done, exiting.");
Dominik Behr46c567f2016-03-08 15:11:48 -0800196 exit(EXIT_SUCCESS);
197 } else
198 if (ptr) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700199 LOG(INFO, "Chrome started, splash screen is not needed anymore.");
Dominik Behr46c567f2016-03-08 15:11:48 -0800200 splash_destroy((splash_t*)ptr);
201 }
202}
Dominik Behr44e07e62016-01-13 19:43:57 -0800203
David Sodmanbbcb0522014-09-19 10:34:07 -0700204int main(int argc, char* argv[])
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700205{
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700206 int ret;
David Sodmanbbcb0522014-09-19 10:34:07 -0700207 int c;
David Sodman8ef20062015-01-06 09:23:40 -0800208 int32_t x, y;
Stéphane Marchesin00ff1872015-12-14 13:40:09 -0800209 splash_t* splash;
Dominik Behrb1abcba2016-04-14 14:57:21 -0700210 drm_t* drm;
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700211
Dominik Behrb1abcba2016-04-14 14:57:21 -0700212 /* Find out if we are going to be a daemon .*/
213 optind = 1;
214 for (;;) {
215 c = getopt_long(argc, argv, "", command_options, NULL);
216 if (c == -1) {
217 break;
218 } else if (c == FLAG_DAEMON) {
219 command_flags.daemon = true;
220 }
221 }
222
223 /* Handle resolution special before splash init. */
224 optind = 1;
Douglas Anderson4da95cd2015-10-05 15:04:30 -0700225 for (;;) {
226 c = getopt_long(argc, argv, "", command_options, NULL);
227 if (c == -1) {
228 break;
229 } else if (c == FLAG_PRINT_RESOLUTION) {
Dominik Behr83010f82016-03-18 18:43:08 -0700230 drm_t *drm = drm_scan();
231 if (!drm)
Douglas Anderson4da95cd2015-10-05 15:04:30 -0700232 return EXIT_FAILURE;
233
Dominik Behr83010f82016-03-18 18:43:08 -0700234 printf("%d %d", drm_gethres(drm),
235 drm_getvres(drm));
236 drm_delref(drm);
Douglas Anderson4da95cd2015-10-05 15:04:30 -0700237 return EXIT_SUCCESS;
238 }
239 }
240
Dominik Behrb1abcba2016-04-14 14:57:21 -0700241 splash = splash_init();
242 if (splash == NULL) {
243 LOG(ERROR, "Splash init failed.");
244 return EXIT_FAILURE;
245 }
246
247 if (command_flags.daemon) {
248 splash_present_term_file(splash);
249 daemonize();
250 }
Douglas Anderson4da95cd2015-10-05 15:04:30 -0700251
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700252 ret = input_init();
253 if (ret) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700254 LOG(ERROR, "Input init failed.");
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700255 return EXIT_FAILURE;
256 }
257
Dominik Behr5239cca2016-01-21 18:22:04 -0800258 ret = dev_init();
259 if (ret) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700260 LOG(ERROR, "Device management init failed.");
Dominik Behr5239cca2016-01-21 18:22:04 -0800261 return EXIT_FAILURE;
262 }
263
Dominik Behrb1abcba2016-04-14 14:57:21 -0700264 drm_set(drm = drm_scan());
265 /* Update DRM object in splash term and set video mode. */
266 splash_redrm(splash);
David Sodmanbbcb0522014-09-19 10:34:07 -0700267
Dominik Behrb1abcba2016-04-14 14:57:21 -0700268 optind = 1;
David Sodmanbbcb0522014-09-19 10:34:07 -0700269 for (;;) {
270 c = getopt_long(argc, argv, "", command_options, NULL);
271
272 if (c == -1)
273 break;
274
275 switch (c) {
276 case FLAG_CLEAR:
277 splash_set_clear(splash, strtoul(optarg, NULL, 0));
278 break;
279
David Sodman8ef20062015-01-06 09:23:40 -0800280 case FLAG_FRAME_INTERVAL:
281 splash_set_default_duration(splash, strtoul(optarg, NULL, 0));
282 break;
283
Dominik Behr46c567f2016-03-08 15:11:48 -0800284 case FLAG_ENABLE_VTS:
285 command_flags.enable_vts = true;
David Sodmanbbcb0522014-09-19 10:34:07 -0700286 break;
287
Dominik Behr32a7c892015-10-09 15:47:53 -0700288 case FLAG_IMAGE:
289 if (!splash_is_hires(splash))
290 splash_add_image(splash, optarg);
291 break;
292
293 case FLAG_IMAGE_HIRES:
294 if (splash_is_hires(splash))
295 splash_add_image(splash, optarg);
296 break;
297
Dominik Behrfd9fdda2016-03-28 17:16:45 -0700298 case FLAG_LOOP_COUNT:
299 splash_set_loop_count(splash, strtoul(optarg, NULL, 0));
300 break;
301
David Sodman8ef20062015-01-06 09:23:40 -0800302 case FLAG_LOOP_START:
303 splash_set_loop_start(splash, strtoul(optarg, NULL, 0));
304 break;
305
306 case FLAG_LOOP_INTERVAL:
307 splash_set_loop_duration(splash, strtoul(optarg, NULL, 0));
308 break;
309
310 case FLAG_LOOP_OFFSET:
311 parse_offset(optarg, &x, &y);
312 splash_set_loop_offset(splash, x, y);
313 break;
314
315 case FLAG_OFFSET:
316 parse_offset(optarg, &x, &y);
317 splash_set_offset(splash, x, y);
318 break;
Dominik Behrfd9fdda2016-03-28 17:16:45 -0700319
320 case FLAG_SPLASH_ONLY:
321 command_flags.splash_only = true;
322 break;
David Sodmanbbcb0522014-09-19 10:34:07 -0700323 }
324 }
325
Stéphane Marchesinac14d292015-12-14 15:27:18 -0800326 for (int i = optind; i < argc; i++)
David Sodman8ef20062015-01-06 09:23:40 -0800327 splash_add_image(splash, argv[i]);
328
David Sodmanf0a925a2015-05-04 11:19:19 -0700329 if (splash_num_images(splash) > 0) {
Dominik Behr797a3832016-01-11 15:53:11 -0800330 ret = splash_run(splash);
David Sodmanbbcb0522014-09-19 10:34:07 -0700331 if (ret) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700332 LOG(ERROR, "Splash_run failed: %d.", ret);
Yuly Novikov945871e2015-05-23 00:00:41 -0400333 return EXIT_FAILURE;
David Sodmanbbcb0522014-09-19 10:34:07 -0700334 }
335 }
336
Dominik Behrfd9fdda2016-03-28 17:16:45 -0700337 if (command_flags.splash_only)
338 goto main_done;
339
David Sodmanbbcb0522014-09-19 10:34:07 -0700340 /*
Dominik Behr46c567f2016-03-08 15:11:48 -0800341 * The DBUS service launches later than the boot-splash service, and
342 * as a result, when splash_run starts DBUS is not yet up, but, by
343 * the time splash_run completes, it is running.
Dominik Behrb1abcba2016-04-14 14:57:21 -0700344 * We really need DBUS now, so we can interact with Chrome.
David Sodmanbbcb0522014-09-19 10:34:07 -0700345 */
Dominik Behr5f6742f2016-03-10 18:03:54 -0800346 dbus_init_wait();
David Sodman8ef20062015-01-06 09:23:40 -0800347
Dominik Behr46c567f2016-03-08 15:11:48 -0800348 /*
349 * Ask DBUS to call us back so we can destroy splash (or quit) when login
Dominik Behrb1abcba2016-04-14 14:57:21 -0700350 * prompt is visible.
Dominik Behr46c567f2016-03-08 15:11:48 -0800351 */
352 dbus_set_login_prompt_visible_callback(main_on_login_prompt_visible,
353 (void*)splash);
354
Dominik Behr1883c042016-04-27 12:31:02 -0700355 /*
356 * Ask DBUS to notify us when suspend has finished so monitors can be reprobed
357 * in case they changed during suspend.
358 */
359 dbus_set_suspend_done_callback(term_suspend_done, NULL);
360
Dominik Behr46c567f2016-03-08 15:11:48 -0800361 if (command_flags.daemon) {
362 if (command_flags.enable_vts)
Dominik Behr83864df2016-04-21 12:35:08 -0700363 set_drm_master_relax(); /* TODO(dbehr) Remove when Chrome is fixed to actually release master. */
Dominik Behrb1abcba2016-04-14 14:57:21 -0700364 drm_dropmaster(drm);
Dominik Behr83864df2016-04-21 12:35:08 -0700365 term_background();
Dominik Behr46c567f2016-03-08 15:11:48 -0800366 } else {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700367 /* Create and switch to first term in interactve mode. */
Dominik Behr46c567f2016-03-08 15:11:48 -0800368 terminal_t* terminal;
Dominik Behr83864df2016-04-21 12:35:08 -0700369 set_drm_master_relax(); /* TODO(dbehr) Remove when Chrome is fixed to actually release master. */
370 term_foreground();
Dominik Behr83010f82016-03-18 18:43:08 -0700371 term_set_current_terminal(term_init(true));
Dominik Behr46c567f2016-03-08 15:11:48 -0800372 terminal = term_get_current_terminal();
373 term_activate(terminal);
374 }
375
376 ret = main_loop();
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700377
Dominik Behrfd9fdda2016-03-28 17:16:45 -0700378main_done:
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700379 input_close();
Dominik Behr5239cca2016-01-21 18:22:04 -0800380 dev_close();
Dominik Behr44e07e62016-01-13 19:43:57 -0800381 dbus_destroy();
Dominik Behr83010f82016-03-18 18:43:08 -0700382 drm_close();
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700383
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700384 return ret;
385}