blob: 560753cc804084cb0a32f4319c666e2448e7a045 [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 Behrd2530902016-05-05 14:01:06 -070029#define FLAG_ENABLE_GFX 'G'
Dominik Behr46c567f2016-03-08 15:11:48 -080030#define FLAG_ENABLE_VTS 'e'
David Sodmanbbcb0522014-09-19 10:34:07 -070031#define FLAG_FRAME_INTERVAL 'f'
32#define FLAG_GAMMA 'g'
Dominik Behr32a7c892015-10-09 15:47:53 -070033#define FLAG_IMAGE 'i'
34#define FLAG_IMAGE_HIRES 'I'
Dominik Behrfd9fdda2016-03-28 17:16:45 -070035#define FLAG_LOOP_COUNT 'C'
David Sodman8ef20062015-01-06 09:23:40 -080036#define FLAG_LOOP_START 'l'
37#define FLAG_LOOP_INTERVAL 'L'
38#define FLAG_LOOP_OFFSET 'o'
39#define FLAG_OFFSET 'O'
David Sodmanbbcb0522014-09-19 10:34:07 -070040#define FLAG_PRINT_RESOLUTION 'p'
Dominik Behr92d9e312016-05-04 20:10:48 -070041#define FLAG_SCALE 'S'
Dominik Behrfd9fdda2016-03-28 17:16:45 -070042#define FLAG_SPLASH_ONLY 's'
David Sodmanbbcb0522014-09-19 10:34:07 -070043
44static struct option command_options[] = {
45 { "clear", required_argument, NULL, FLAG_CLEAR },
46 { "daemon", no_argument, NULL, FLAG_DAEMON },
Dominik Behr46c567f2016-03-08 15:11:48 -080047 { "dev-mode", no_argument, NULL, FLAG_ENABLE_VTS },
Dominik Behrd2530902016-05-05 14:01:06 -070048 { "enable-gfx", no_argument, NULL, FLAG_ENABLE_GFX },
Dominik Behr46c567f2016-03-08 15:11:48 -080049 { "enable-vts", no_argument, NULL, FLAG_ENABLE_VTS },
David Sodmanbbcb0522014-09-19 10:34:07 -070050 { "frame-interval", required_argument, NULL, FLAG_FRAME_INTERVAL },
51 { "gamma", required_argument, NULL, FLAG_GAMMA },
Dominik Behr32a7c892015-10-09 15:47:53 -070052 { "image", required_argument, NULL, FLAG_IMAGE },
53 { "image-hires", required_argument, NULL, FLAG_IMAGE_HIRES },
Dominik Behrfd9fdda2016-03-28 17:16:45 -070054 { "loop-count", required_argument, NULL, FLAG_LOOP_COUNT },
David Sodman8ef20062015-01-06 09:23:40 -080055 { "loop-start", required_argument, NULL, FLAG_LOOP_START },
56 { "loop-interval", required_argument, NULL, FLAG_LOOP_INTERVAL },
57 { "loop-offset", required_argument, NULL, FLAG_LOOP_OFFSET },
58 { "offset", required_argument, NULL, FLAG_OFFSET },
David Sodmanbbcb0522014-09-19 10:34:07 -070059 { "print-resolution", no_argument, NULL, FLAG_PRINT_RESOLUTION },
Dominik Behr92d9e312016-05-04 20:10:48 -070060 { "scale", required_argument, NULL, FLAG_SCALE },
Dominik Behrfd9fdda2016-03-28 17:16:45 -070061 { "splash-only", no_argument, NULL, FLAG_SPLASH_ONLY },
David Sodmanbbcb0522014-09-19 10:34:07 -070062 { NULL, 0, NULL, 0 }
63};
64
Dominik Behr46c567f2016-03-08 15:11:48 -080065commandflags_t command_flags = { 0 };
Dominik Behr580462b2014-11-20 13:50:05 -080066
Stéphane Marchesin8fc13522015-12-14 17:02:28 -080067static void parse_offset(char* param, int32_t* x, int32_t* y)
David Sodman8ef20062015-01-06 09:23:40 -080068{
69 char* token;
70 char* saveptr;
71
72 token = strtok_r(param, ",", &saveptr);
73 if (token)
74 *x = strtol(token, NULL, 0);
75
76 token = strtok_r(NULL, ",", &saveptr);
77 if (token)
78 *y = strtol(token, NULL, 0);
79}
David Sodmanbbcb0522014-09-19 10:34:07 -070080
Dominik Behr44e07e62016-01-13 19:43:57 -080081int main_process_events(uint32_t usec)
82{
83 terminal_t* terminal;
84 terminal_t* new_terminal;
85 fd_set read_set, exception_set;
Dominik Behrd7112672016-01-20 16:59:34 -080086 int maxfd = -1;
Dominik Behr44e07e62016-01-13 19:43:57 -080087 int sstat;
88 struct timeval tm;
89 struct timeval* ptm;
90
91 terminal = term_get_current_terminal();
92
93 FD_ZERO(&read_set);
94 FD_ZERO(&exception_set);
95
Dominik Behrd7112672016-01-20 16:59:34 -080096 dbus_add_fds(&read_set, &exception_set, &maxfd);
Dominik Behrd7112672016-01-20 16:59:34 -080097 input_add_fds(&read_set, &exception_set, &maxfd);
Dominik Behr5239cca2016-01-21 18:22:04 -080098 dev_add_fds(&read_set, &exception_set, &maxfd);
Dominik Behr44e07e62016-01-13 19:43:57 -080099
100 for (int i = 0; i < MAX_TERMINALS; i++) {
101 if (term_is_valid(term_get_terminal(i))) {
102 terminal_t* current_term = term_get_terminal(i);
Dominik Behrd7112672016-01-20 16:59:34 -0800103 term_add_fds(current_term, &read_set, &exception_set, &maxfd);
Dominik Behr44e07e62016-01-13 19:43:57 -0800104 }
105 }
106
107 if (usec) {
108 ptm = &tm;
109 tm.tv_sec = 0;
110 tm.tv_usec = usec;
111 } else
112 ptm = NULL;
113
Dominik Behrd7112672016-01-20 16:59:34 -0800114 sstat = select(maxfd + 1, &read_set, NULL, &exception_set, ptm);
Dominik Behr44e07e62016-01-13 19:43:57 -0800115 if (sstat == 0)
116 return 0;
117
118 dbus_dispatch_io();
119
120 if (term_exception(terminal, &exception_set))
121 return -1;
122
Dominik Behr5239cca2016-01-21 18:22:04 -0800123 dev_dispatch_io(&read_set, &exception_set);
Dominik Behr44e07e62016-01-13 19:43:57 -0800124 input_dispatch_io(&read_set, &exception_set);
125
126 for (int i = 0; i < MAX_TERMINALS; i++) {
127 if (term_is_valid(term_get_terminal(i))) {
128 terminal_t* current_term = term_get_terminal(i);
129 term_dispatch_io(current_term, &read_set);
130 }
131 }
132
133 if (term_is_valid(terminal)) {
134 if (term_is_child_done(terminal)) {
135 if (terminal == term_get_terminal(SPLASH_TERMINAL)) {
136 /*
137 * Note: reference is not lost because it is still referenced
138 * by the splash_t structure which will ultimately destroy
Dominik Behrb1abcba2016-04-14 14:57:21 -0700139 * it, once it's safe to do so.
Dominik Behr44e07e62016-01-13 19:43:57 -0800140 */
141 term_set_terminal(SPLASH_TERMINAL, NULL);
142 return -1;
143 }
Dominik Behr83010f82016-03-18 18:43:08 -0700144 term_set_current_terminal(term_init(true));
Dominik Behr44e07e62016-01-13 19:43:57 -0800145 new_terminal = term_get_current_terminal();
146 if (!term_is_valid(new_terminal)) {
147 return -1;
148 }
149 term_activate(new_terminal);
150 term_close(terminal);
151 }
152 }
153
154 return 0;
155}
156
Dominik Behr46c567f2016-03-08 15:11:48 -0800157int main_loop(void)
Dominik Behr44e07e62016-01-13 19:43:57 -0800158{
Dominik Behr44e07e62016-01-13 19:43:57 -0800159 int status;
160
Dominik Behr44e07e62016-01-13 19:43:57 -0800161 while (1) {
162 status = main_process_events(0);
163 if (status != 0) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700164 LOG(ERROR, "Input process returned %d.", status);
Dominik Behr44e07e62016-01-13 19:43:57 -0800165 break;
166 }
167 }
168
169 return 0;
170}
171
Dominik Behr46c567f2016-03-08 15:11:48 -0800172bool set_drm_master_relax(void)
173{
174 int fd;
175 int num_written;
176
177 /*
178 * Setting drm_master_relax flag in kernel allows us to transfer DRM master
Dominik Behrb1abcba2016-04-14 14:57:21 -0700179 * between Chrome and frecon.
Dominik Behr46c567f2016-03-08 15:11:48 -0800180 */
181 fd = open("/sys/kernel/debug/dri/drm_master_relax", O_WRONLY);
182 if (fd != -1) {
183 num_written = write(fd, "Y", 1);
184 close(fd);
185 if (num_written != 1) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700186 LOG(ERROR, "Unable to set drm_master_relax.");
Dominik Behr46c567f2016-03-08 15:11:48 -0800187 return false;
188 }
189 } else {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700190 LOG(ERROR, "Unable to open drm_master_relax.");
Dominik Behr46c567f2016-03-08 15:11:48 -0800191 return false;
192 }
193 return true;
194}
195
196static void main_on_login_prompt_visible(void* ptr)
197{
198 if (command_flags.daemon && !command_flags.enable_vts) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700199 LOG(INFO, "Chrome started, our work is done, exiting.");
Dominik Behr46c567f2016-03-08 15:11:48 -0800200 exit(EXIT_SUCCESS);
201 } else
202 if (ptr) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700203 LOG(INFO, "Chrome started, splash screen is not needed anymore.");
Dominik Behr46c567f2016-03-08 15:11:48 -0800204 splash_destroy((splash_t*)ptr);
205 }
206}
Dominik Behr44e07e62016-01-13 19:43:57 -0800207
David Sodmanbbcb0522014-09-19 10:34:07 -0700208int main(int argc, char* argv[])
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700209{
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700210 int ret;
David Sodmanbbcb0522014-09-19 10:34:07 -0700211 int c;
David Sodman8ef20062015-01-06 09:23:40 -0800212 int32_t x, y;
Stéphane Marchesin00ff1872015-12-14 13:40:09 -0800213 splash_t* splash;
Dominik Behrb1abcba2016-04-14 14:57:21 -0700214 drm_t* drm;
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700215
Dominik Behrb1abcba2016-04-14 14:57:21 -0700216 /* Find out if we are going to be a daemon .*/
217 optind = 1;
218 for (;;) {
219 c = getopt_long(argc, argv, "", command_options, NULL);
220 if (c == -1) {
221 break;
222 } else if (c == FLAG_DAEMON) {
223 command_flags.daemon = true;
224 }
225 }
226
227 /* Handle resolution special before splash init. */
228 optind = 1;
Douglas Anderson4da95cd2015-10-05 15:04:30 -0700229 for (;;) {
230 c = getopt_long(argc, argv, "", command_options, NULL);
231 if (c == -1) {
232 break;
233 } else if (c == FLAG_PRINT_RESOLUTION) {
Dominik Behr83010f82016-03-18 18:43:08 -0700234 drm_t *drm = drm_scan();
235 if (!drm)
Douglas Anderson4da95cd2015-10-05 15:04:30 -0700236 return EXIT_FAILURE;
237
Dominik Behr83010f82016-03-18 18:43:08 -0700238 printf("%d %d", drm_gethres(drm),
239 drm_getvres(drm));
240 drm_delref(drm);
Douglas Anderson4da95cd2015-10-05 15:04:30 -0700241 return EXIT_SUCCESS;
242 }
243 }
244
Dominik Behrb1abcba2016-04-14 14:57:21 -0700245 splash = splash_init();
246 if (splash == NULL) {
247 LOG(ERROR, "Splash init failed.");
248 return EXIT_FAILURE;
249 }
250
251 if (command_flags.daemon) {
252 splash_present_term_file(splash);
253 daemonize();
254 }
Douglas Anderson4da95cd2015-10-05 15:04:30 -0700255
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700256 ret = input_init();
257 if (ret) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700258 LOG(ERROR, "Input init failed.");
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700259 return EXIT_FAILURE;
260 }
261
Dominik Behr5239cca2016-01-21 18:22:04 -0800262 ret = dev_init();
263 if (ret) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700264 LOG(ERROR, "Device management init failed.");
Dominik Behr5239cca2016-01-21 18:22:04 -0800265 return EXIT_FAILURE;
266 }
267
Dominik Behrb1abcba2016-04-14 14:57:21 -0700268 drm_set(drm = drm_scan());
269 /* Update DRM object in splash term and set video mode. */
270 splash_redrm(splash);
David Sodmanbbcb0522014-09-19 10:34:07 -0700271
Dominik Behrb1abcba2016-04-14 14:57:21 -0700272 optind = 1;
David Sodmanbbcb0522014-09-19 10:34:07 -0700273 for (;;) {
274 c = getopt_long(argc, argv, "", command_options, NULL);
275
276 if (c == -1)
277 break;
278
279 switch (c) {
280 case FLAG_CLEAR:
281 splash_set_clear(splash, strtoul(optarg, NULL, 0));
282 break;
283
David Sodman8ef20062015-01-06 09:23:40 -0800284 case FLAG_FRAME_INTERVAL:
285 splash_set_default_duration(splash, strtoul(optarg, NULL, 0));
286 break;
287
Dominik Behrd2530902016-05-05 14:01:06 -0700288 case FLAG_ENABLE_GFX:
289 command_flags.enable_gfx = true;
290 break;
291
Dominik Behr46c567f2016-03-08 15:11:48 -0800292 case FLAG_ENABLE_VTS:
293 command_flags.enable_vts = true;
David Sodmanbbcb0522014-09-19 10:34:07 -0700294 break;
295
Dominik Behr32a7c892015-10-09 15:47:53 -0700296 case FLAG_IMAGE:
297 if (!splash_is_hires(splash))
298 splash_add_image(splash, optarg);
299 break;
300
301 case FLAG_IMAGE_HIRES:
302 if (splash_is_hires(splash))
303 splash_add_image(splash, optarg);
304 break;
305
Dominik Behrfd9fdda2016-03-28 17:16:45 -0700306 case FLAG_LOOP_COUNT:
307 splash_set_loop_count(splash, strtoul(optarg, NULL, 0));
308 break;
309
David Sodman8ef20062015-01-06 09:23:40 -0800310 case FLAG_LOOP_START:
311 splash_set_loop_start(splash, strtoul(optarg, NULL, 0));
312 break;
313
314 case FLAG_LOOP_INTERVAL:
315 splash_set_loop_duration(splash, strtoul(optarg, NULL, 0));
316 break;
317
318 case FLAG_LOOP_OFFSET:
319 parse_offset(optarg, &x, &y);
320 splash_set_loop_offset(splash, x, y);
321 break;
322
323 case FLAG_OFFSET:
324 parse_offset(optarg, &x, &y);
325 splash_set_offset(splash, x, y);
326 break;
Dominik Behrfd9fdda2016-03-28 17:16:45 -0700327
Dominik Behr92d9e312016-05-04 20:10:48 -0700328 case FLAG_SCALE:
329 splash_set_scale(splash, strtoul(optarg, NULL, 0));
330 break;
331
Dominik Behrfd9fdda2016-03-28 17:16:45 -0700332 case FLAG_SPLASH_ONLY:
333 command_flags.splash_only = true;
334 break;
David Sodmanbbcb0522014-09-19 10:34:07 -0700335 }
336 }
337
Stéphane Marchesinac14d292015-12-14 15:27:18 -0800338 for (int i = optind; i < argc; i++)
David Sodman8ef20062015-01-06 09:23:40 -0800339 splash_add_image(splash, argv[i]);
340
David Sodmanf0a925a2015-05-04 11:19:19 -0700341 if (splash_num_images(splash) > 0) {
Dominik Behr797a3832016-01-11 15:53:11 -0800342 ret = splash_run(splash);
David Sodmanbbcb0522014-09-19 10:34:07 -0700343 if (ret) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700344 LOG(ERROR, "Splash_run failed: %d.", ret);
Yuly Novikov945871e2015-05-23 00:00:41 -0400345 return EXIT_FAILURE;
David Sodmanbbcb0522014-09-19 10:34:07 -0700346 }
347 }
348
Dominik Behrfd9fdda2016-03-28 17:16:45 -0700349 if (command_flags.splash_only)
350 goto main_done;
351
David Sodmanbbcb0522014-09-19 10:34:07 -0700352 /*
Dominik Behr46c567f2016-03-08 15:11:48 -0800353 * The DBUS service launches later than the boot-splash service, and
354 * as a result, when splash_run starts DBUS is not yet up, but, by
355 * the time splash_run completes, it is running.
Dominik Behrb1abcba2016-04-14 14:57:21 -0700356 * We really need DBUS now, so we can interact with Chrome.
David Sodmanbbcb0522014-09-19 10:34:07 -0700357 */
Dominik Behr5f6742f2016-03-10 18:03:54 -0800358 dbus_init_wait();
David Sodman8ef20062015-01-06 09:23:40 -0800359
Dominik Behr46c567f2016-03-08 15:11:48 -0800360 /*
361 * Ask DBUS to call us back so we can destroy splash (or quit) when login
Dominik Behrb1abcba2016-04-14 14:57:21 -0700362 * prompt is visible.
Dominik Behr46c567f2016-03-08 15:11:48 -0800363 */
364 dbus_set_login_prompt_visible_callback(main_on_login_prompt_visible,
365 (void*)splash);
366
Dominik Behr1883c042016-04-27 12:31:02 -0700367 /*
368 * Ask DBUS to notify us when suspend has finished so monitors can be reprobed
369 * in case they changed during suspend.
370 */
371 dbus_set_suspend_done_callback(term_suspend_done, NULL);
372
Dominik Behr46c567f2016-03-08 15:11:48 -0800373 if (command_flags.daemon) {
374 if (command_flags.enable_vts)
Dominik Behr83864df2016-04-21 12:35:08 -0700375 set_drm_master_relax(); /* TODO(dbehr) Remove when Chrome is fixed to actually release master. */
Dominik Behrb1abcba2016-04-14 14:57:21 -0700376 drm_dropmaster(drm);
Dominik Behr83864df2016-04-21 12:35:08 -0700377 term_background();
Dominik Behr46c567f2016-03-08 15:11:48 -0800378 } else {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700379 /* Create and switch to first term in interactve mode. */
Dominik Behr46c567f2016-03-08 15:11:48 -0800380 terminal_t* terminal;
Dominik Behr83864df2016-04-21 12:35:08 -0700381 set_drm_master_relax(); /* TODO(dbehr) Remove when Chrome is fixed to actually release master. */
382 term_foreground();
Dominik Behr83010f82016-03-18 18:43:08 -0700383 term_set_current_terminal(term_init(true));
Dominik Behr46c567f2016-03-08 15:11:48 -0800384 terminal = term_get_current_terminal();
385 term_activate(terminal);
386 }
387
388 ret = main_loop();
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700389
Dominik Behrfd9fdda2016-03-28 17:16:45 -0700390main_done:
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700391 input_close();
Dominik Behr5239cca2016-01-21 18:22:04 -0800392 dev_close();
Dominik Behr44e07e62016-01-13 19:43:57 -0800393 dbus_destroy();
Dominik Behr83010f82016-03-18 18:43:08 -0700394 drm_close();
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700395
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700396 return ret;
397}