blob: eb4e36c8653edc666304d43fbd1910d354bf46c6 [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 Behr92d9e312016-05-04 20:10:48 -070040#define FLAG_SCALE 'S'
Dominik Behrfd9fdda2016-03-28 17:16:45 -070041#define FLAG_SPLASH_ONLY 's'
David Sodmanbbcb0522014-09-19 10:34:07 -070042
43static struct option command_options[] = {
44 { "clear", required_argument, NULL, FLAG_CLEAR },
45 { "daemon", no_argument, NULL, FLAG_DAEMON },
Dominik Behr46c567f2016-03-08 15:11:48 -080046 { "dev-mode", no_argument, NULL, FLAG_ENABLE_VTS },
47 { "enable-vts", no_argument, NULL, FLAG_ENABLE_VTS },
David Sodmanbbcb0522014-09-19 10:34:07 -070048 { "frame-interval", required_argument, NULL, FLAG_FRAME_INTERVAL },
49 { "gamma", required_argument, NULL, FLAG_GAMMA },
Dominik Behr32a7c892015-10-09 15:47:53 -070050 { "image", required_argument, NULL, FLAG_IMAGE },
51 { "image-hires", required_argument, NULL, FLAG_IMAGE_HIRES },
Dominik Behrfd9fdda2016-03-28 17:16:45 -070052 { "loop-count", required_argument, NULL, FLAG_LOOP_COUNT },
David Sodman8ef20062015-01-06 09:23:40 -080053 { "loop-start", required_argument, NULL, FLAG_LOOP_START },
54 { "loop-interval", required_argument, NULL, FLAG_LOOP_INTERVAL },
55 { "loop-offset", required_argument, NULL, FLAG_LOOP_OFFSET },
56 { "offset", required_argument, NULL, FLAG_OFFSET },
David Sodmanbbcb0522014-09-19 10:34:07 -070057 { "print-resolution", no_argument, NULL, FLAG_PRINT_RESOLUTION },
Dominik Behr92d9e312016-05-04 20:10:48 -070058 { "scale", required_argument, NULL, FLAG_SCALE },
Dominik Behrfd9fdda2016-03-28 17:16:45 -070059 { "splash-only", no_argument, NULL, FLAG_SPLASH_ONLY },
David Sodmanbbcb0522014-09-19 10:34:07 -070060 { NULL, 0, NULL, 0 }
61};
62
Dominik Behr46c567f2016-03-08 15:11:48 -080063commandflags_t command_flags = { 0 };
Dominik Behr580462b2014-11-20 13:50:05 -080064
Stéphane Marchesin8fc13522015-12-14 17:02:28 -080065static void parse_offset(char* param, int32_t* x, int32_t* y)
David Sodman8ef20062015-01-06 09:23:40 -080066{
67 char* token;
68 char* saveptr;
69
70 token = strtok_r(param, ",", &saveptr);
71 if (token)
72 *x = strtol(token, NULL, 0);
73
74 token = strtok_r(NULL, ",", &saveptr);
75 if (token)
76 *y = strtol(token, NULL, 0);
77}
David Sodmanbbcb0522014-09-19 10:34:07 -070078
Dominik Behr44e07e62016-01-13 19:43:57 -080079int main_process_events(uint32_t usec)
80{
81 terminal_t* terminal;
82 terminal_t* new_terminal;
83 fd_set read_set, exception_set;
Dominik Behrd7112672016-01-20 16:59:34 -080084 int maxfd = -1;
Dominik Behr44e07e62016-01-13 19:43:57 -080085 int sstat;
86 struct timeval tm;
87 struct timeval* ptm;
88
89 terminal = term_get_current_terminal();
90
91 FD_ZERO(&read_set);
92 FD_ZERO(&exception_set);
93
Dominik Behrd7112672016-01-20 16:59:34 -080094 dbus_add_fds(&read_set, &exception_set, &maxfd);
Dominik Behrd7112672016-01-20 16:59:34 -080095 input_add_fds(&read_set, &exception_set, &maxfd);
Dominik Behr5239cca2016-01-21 18:22:04 -080096 dev_add_fds(&read_set, &exception_set, &maxfd);
Dominik Behr44e07e62016-01-13 19:43:57 -080097
98 for (int i = 0; i < MAX_TERMINALS; i++) {
99 if (term_is_valid(term_get_terminal(i))) {
100 terminal_t* current_term = term_get_terminal(i);
Dominik Behrd7112672016-01-20 16:59:34 -0800101 term_add_fds(current_term, &read_set, &exception_set, &maxfd);
Dominik Behr44e07e62016-01-13 19:43:57 -0800102 }
103 }
104
105 if (usec) {
106 ptm = &tm;
107 tm.tv_sec = 0;
108 tm.tv_usec = usec;
109 } else
110 ptm = NULL;
111
Dominik Behrd7112672016-01-20 16:59:34 -0800112 sstat = select(maxfd + 1, &read_set, NULL, &exception_set, ptm);
Dominik Behr44e07e62016-01-13 19:43:57 -0800113 if (sstat == 0)
114 return 0;
115
116 dbus_dispatch_io();
117
118 if (term_exception(terminal, &exception_set))
119 return -1;
120
Dominik Behr5239cca2016-01-21 18:22:04 -0800121 dev_dispatch_io(&read_set, &exception_set);
Dominik Behr44e07e62016-01-13 19:43:57 -0800122 input_dispatch_io(&read_set, &exception_set);
123
124 for (int i = 0; i < MAX_TERMINALS; i++) {
125 if (term_is_valid(term_get_terminal(i))) {
126 terminal_t* current_term = term_get_terminal(i);
127 term_dispatch_io(current_term, &read_set);
128 }
129 }
130
131 if (term_is_valid(terminal)) {
132 if (term_is_child_done(terminal)) {
133 if (terminal == term_get_terminal(SPLASH_TERMINAL)) {
134 /*
135 * Note: reference is not lost because it is still referenced
136 * by the splash_t structure which will ultimately destroy
Dominik Behrb1abcba2016-04-14 14:57:21 -0700137 * it, once it's safe to do so.
Dominik Behr44e07e62016-01-13 19:43:57 -0800138 */
139 term_set_terminal(SPLASH_TERMINAL, NULL);
140 return -1;
141 }
Dominik Behr83010f82016-03-18 18:43:08 -0700142 term_set_current_terminal(term_init(true));
Dominik Behr44e07e62016-01-13 19:43:57 -0800143 new_terminal = term_get_current_terminal();
144 if (!term_is_valid(new_terminal)) {
145 return -1;
146 }
147 term_activate(new_terminal);
148 term_close(terminal);
149 }
150 }
151
152 return 0;
153}
154
Dominik Behr46c567f2016-03-08 15:11:48 -0800155int main_loop(void)
Dominik Behr44e07e62016-01-13 19:43:57 -0800156{
Dominik Behr44e07e62016-01-13 19:43:57 -0800157 int status;
158
Dominik Behr44e07e62016-01-13 19:43:57 -0800159 while (1) {
160 status = main_process_events(0);
161 if (status != 0) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700162 LOG(ERROR, "Input process returned %d.", status);
Dominik Behr44e07e62016-01-13 19:43:57 -0800163 break;
164 }
165 }
166
167 return 0;
168}
169
Dominik Behr46c567f2016-03-08 15:11:48 -0800170bool set_drm_master_relax(void)
171{
172 int fd;
173 int num_written;
174
175 /*
176 * Setting drm_master_relax flag in kernel allows us to transfer DRM master
Dominik Behrb1abcba2016-04-14 14:57:21 -0700177 * between Chrome and frecon.
Dominik Behr46c567f2016-03-08 15:11:48 -0800178 */
179 fd = open("/sys/kernel/debug/dri/drm_master_relax", O_WRONLY);
180 if (fd != -1) {
181 num_written = write(fd, "Y", 1);
182 close(fd);
183 if (num_written != 1) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700184 LOG(ERROR, "Unable to set drm_master_relax.");
Dominik Behr46c567f2016-03-08 15:11:48 -0800185 return false;
186 }
187 } else {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700188 LOG(ERROR, "Unable to open drm_master_relax.");
Dominik Behr46c567f2016-03-08 15:11:48 -0800189 return false;
190 }
191 return true;
192}
193
194static void main_on_login_prompt_visible(void* ptr)
195{
196 if (command_flags.daemon && !command_flags.enable_vts) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700197 LOG(INFO, "Chrome started, our work is done, exiting.");
Dominik Behr46c567f2016-03-08 15:11:48 -0800198 exit(EXIT_SUCCESS);
199 } else
200 if (ptr) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700201 LOG(INFO, "Chrome started, splash screen is not needed anymore.");
Dominik Behr46c567f2016-03-08 15:11:48 -0800202 splash_destroy((splash_t*)ptr);
203 }
204}
Dominik Behr44e07e62016-01-13 19:43:57 -0800205
David Sodmanbbcb0522014-09-19 10:34:07 -0700206int main(int argc, char* argv[])
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700207{
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700208 int ret;
David Sodmanbbcb0522014-09-19 10:34:07 -0700209 int c;
David Sodman8ef20062015-01-06 09:23:40 -0800210 int32_t x, y;
Stéphane Marchesin00ff1872015-12-14 13:40:09 -0800211 splash_t* splash;
Dominik Behrb1abcba2016-04-14 14:57:21 -0700212 drm_t* drm;
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700213
Dominik Behrb1abcba2016-04-14 14:57:21 -0700214 /* Find out if we are going to be a daemon .*/
215 optind = 1;
216 for (;;) {
217 c = getopt_long(argc, argv, "", command_options, NULL);
218 if (c == -1) {
219 break;
220 } else if (c == FLAG_DAEMON) {
221 command_flags.daemon = true;
222 }
223 }
224
225 /* Handle resolution special before splash init. */
226 optind = 1;
Douglas Anderson4da95cd2015-10-05 15:04:30 -0700227 for (;;) {
228 c = getopt_long(argc, argv, "", command_options, NULL);
229 if (c == -1) {
230 break;
231 } else if (c == FLAG_PRINT_RESOLUTION) {
Dominik Behr83010f82016-03-18 18:43:08 -0700232 drm_t *drm = drm_scan();
233 if (!drm)
Douglas Anderson4da95cd2015-10-05 15:04:30 -0700234 return EXIT_FAILURE;
235
Dominik Behr83010f82016-03-18 18:43:08 -0700236 printf("%d %d", drm_gethres(drm),
237 drm_getvres(drm));
238 drm_delref(drm);
Douglas Anderson4da95cd2015-10-05 15:04:30 -0700239 return EXIT_SUCCESS;
240 }
241 }
242
Dominik Behrb1abcba2016-04-14 14:57:21 -0700243 splash = splash_init();
244 if (splash == NULL) {
245 LOG(ERROR, "Splash init failed.");
246 return EXIT_FAILURE;
247 }
248
249 if (command_flags.daemon) {
250 splash_present_term_file(splash);
251 daemonize();
252 }
Douglas Anderson4da95cd2015-10-05 15:04:30 -0700253
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700254 ret = input_init();
255 if (ret) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700256 LOG(ERROR, "Input init failed.");
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700257 return EXIT_FAILURE;
258 }
259
Dominik Behr5239cca2016-01-21 18:22:04 -0800260 ret = dev_init();
261 if (ret) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700262 LOG(ERROR, "Device management init failed.");
Dominik Behr5239cca2016-01-21 18:22:04 -0800263 return EXIT_FAILURE;
264 }
265
Dominik Behrb1abcba2016-04-14 14:57:21 -0700266 drm_set(drm = drm_scan());
267 /* Update DRM object in splash term and set video mode. */
268 splash_redrm(splash);
David Sodmanbbcb0522014-09-19 10:34:07 -0700269
Dominik Behrb1abcba2016-04-14 14:57:21 -0700270 optind = 1;
David Sodmanbbcb0522014-09-19 10:34:07 -0700271 for (;;) {
272 c = getopt_long(argc, argv, "", command_options, NULL);
273
274 if (c == -1)
275 break;
276
277 switch (c) {
278 case FLAG_CLEAR:
279 splash_set_clear(splash, strtoul(optarg, NULL, 0));
280 break;
281
David Sodman8ef20062015-01-06 09:23:40 -0800282 case FLAG_FRAME_INTERVAL:
283 splash_set_default_duration(splash, strtoul(optarg, NULL, 0));
284 break;
285
Dominik Behr46c567f2016-03-08 15:11:48 -0800286 case FLAG_ENABLE_VTS:
287 command_flags.enable_vts = true;
David Sodmanbbcb0522014-09-19 10:34:07 -0700288 break;
289
Dominik Behr32a7c892015-10-09 15:47:53 -0700290 case FLAG_IMAGE:
291 if (!splash_is_hires(splash))
292 splash_add_image(splash, optarg);
293 break;
294
295 case FLAG_IMAGE_HIRES:
296 if (splash_is_hires(splash))
297 splash_add_image(splash, optarg);
298 break;
299
Dominik Behrfd9fdda2016-03-28 17:16:45 -0700300 case FLAG_LOOP_COUNT:
301 splash_set_loop_count(splash, strtoul(optarg, NULL, 0));
302 break;
303
David Sodman8ef20062015-01-06 09:23:40 -0800304 case FLAG_LOOP_START:
305 splash_set_loop_start(splash, strtoul(optarg, NULL, 0));
306 break;
307
308 case FLAG_LOOP_INTERVAL:
309 splash_set_loop_duration(splash, strtoul(optarg, NULL, 0));
310 break;
311
312 case FLAG_LOOP_OFFSET:
313 parse_offset(optarg, &x, &y);
314 splash_set_loop_offset(splash, x, y);
315 break;
316
317 case FLAG_OFFSET:
318 parse_offset(optarg, &x, &y);
319 splash_set_offset(splash, x, y);
320 break;
Dominik Behrfd9fdda2016-03-28 17:16:45 -0700321
Dominik Behr92d9e312016-05-04 20:10:48 -0700322 case FLAG_SCALE:
323 splash_set_scale(splash, strtoul(optarg, NULL, 0));
324 break;
325
Dominik Behrfd9fdda2016-03-28 17:16:45 -0700326 case FLAG_SPLASH_ONLY:
327 command_flags.splash_only = true;
328 break;
David Sodmanbbcb0522014-09-19 10:34:07 -0700329 }
330 }
331
Stéphane Marchesinac14d292015-12-14 15:27:18 -0800332 for (int i = optind; i < argc; i++)
David Sodman8ef20062015-01-06 09:23:40 -0800333 splash_add_image(splash, argv[i]);
334
David Sodmanf0a925a2015-05-04 11:19:19 -0700335 if (splash_num_images(splash) > 0) {
Dominik Behr797a3832016-01-11 15:53:11 -0800336 ret = splash_run(splash);
David Sodmanbbcb0522014-09-19 10:34:07 -0700337 if (ret) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700338 LOG(ERROR, "Splash_run failed: %d.", ret);
Yuly Novikov945871e2015-05-23 00:00:41 -0400339 return EXIT_FAILURE;
David Sodmanbbcb0522014-09-19 10:34:07 -0700340 }
341 }
342
Dominik Behrfd9fdda2016-03-28 17:16:45 -0700343 if (command_flags.splash_only)
344 goto main_done;
345
David Sodmanbbcb0522014-09-19 10:34:07 -0700346 /*
Dominik Behr46c567f2016-03-08 15:11:48 -0800347 * The DBUS service launches later than the boot-splash service, and
348 * as a result, when splash_run starts DBUS is not yet up, but, by
349 * the time splash_run completes, it is running.
Dominik Behrb1abcba2016-04-14 14:57:21 -0700350 * We really need DBUS now, so we can interact with Chrome.
David Sodmanbbcb0522014-09-19 10:34:07 -0700351 */
Dominik Behr5f6742f2016-03-10 18:03:54 -0800352 dbus_init_wait();
David Sodman8ef20062015-01-06 09:23:40 -0800353
Dominik Behr46c567f2016-03-08 15:11:48 -0800354 /*
355 * Ask DBUS to call us back so we can destroy splash (or quit) when login
Dominik Behrb1abcba2016-04-14 14:57:21 -0700356 * prompt is visible.
Dominik Behr46c567f2016-03-08 15:11:48 -0800357 */
358 dbus_set_login_prompt_visible_callback(main_on_login_prompt_visible,
359 (void*)splash);
360
Dominik Behr1883c042016-04-27 12:31:02 -0700361 /*
362 * Ask DBUS to notify us when suspend has finished so monitors can be reprobed
363 * in case they changed during suspend.
364 */
365 dbus_set_suspend_done_callback(term_suspend_done, NULL);
366
Dominik Behr46c567f2016-03-08 15:11:48 -0800367 if (command_flags.daemon) {
368 if (command_flags.enable_vts)
Dominik Behr83864df2016-04-21 12:35:08 -0700369 set_drm_master_relax(); /* TODO(dbehr) Remove when Chrome is fixed to actually release master. */
Dominik Behrb1abcba2016-04-14 14:57:21 -0700370 drm_dropmaster(drm);
Dominik Behr83864df2016-04-21 12:35:08 -0700371 term_background();
Dominik Behr46c567f2016-03-08 15:11:48 -0800372 } else {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700373 /* Create and switch to first term in interactve mode. */
Dominik Behr46c567f2016-03-08 15:11:48 -0800374 terminal_t* terminal;
Dominik Behr83864df2016-04-21 12:35:08 -0700375 set_drm_master_relax(); /* TODO(dbehr) Remove when Chrome is fixed to actually release master. */
376 term_foreground();
Dominik Behr83010f82016-03-18 18:43:08 -0700377 term_set_current_terminal(term_init(true));
Dominik Behr46c567f2016-03-08 15:11:48 -0800378 terminal = term_get_current_terminal();
379 term_activate(terminal);
380 }
381
382 ret = main_loop();
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700383
Dominik Behrfd9fdda2016-03-28 17:16:45 -0700384main_done:
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700385 input_close();
Dominik Behr5239cca2016-01-21 18:22:04 -0800386 dev_close();
Dominik Behr44e07e62016-01-13 19:43:57 -0800387 dbus_destroy();
Dominik Behr83010f82016-03-18 18:43:08 -0700388 drm_close();
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700389
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700390 return ret;
391}