blob: 4e5977c013ef72c791d6f8b935daffaeaa7717e5 [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'
David Sodman8ef20062015-01-06 09:23:40 -080034#define FLAG_LOOP_START 'l'
35#define FLAG_LOOP_INTERVAL 'L'
36#define FLAG_LOOP_OFFSET 'o'
37#define FLAG_OFFSET 'O'
David Sodmanbbcb0522014-09-19 10:34:07 -070038#define FLAG_PRINT_RESOLUTION 'p'
39
40static struct option command_options[] = {
41 { "clear", required_argument, NULL, FLAG_CLEAR },
42 { "daemon", no_argument, NULL, FLAG_DAEMON },
Dominik Behr46c567f2016-03-08 15:11:48 -080043 { "dev-mode", no_argument, NULL, FLAG_ENABLE_VTS },
44 { "enable-vts", no_argument, NULL, FLAG_ENABLE_VTS },
David Sodmanbbcb0522014-09-19 10:34:07 -070045 { "frame-interval", required_argument, NULL, FLAG_FRAME_INTERVAL },
46 { "gamma", required_argument, NULL, FLAG_GAMMA },
Dominik Behr32a7c892015-10-09 15:47:53 -070047 { "image", required_argument, NULL, FLAG_IMAGE },
48 { "image-hires", required_argument, NULL, FLAG_IMAGE_HIRES },
David Sodman8ef20062015-01-06 09:23:40 -080049 { "loop-start", required_argument, NULL, FLAG_LOOP_START },
50 { "loop-interval", required_argument, NULL, FLAG_LOOP_INTERVAL },
51 { "loop-offset", required_argument, NULL, FLAG_LOOP_OFFSET },
52 { "offset", required_argument, NULL, FLAG_OFFSET },
David Sodmanbbcb0522014-09-19 10:34:07 -070053 { "print-resolution", no_argument, NULL, FLAG_PRINT_RESOLUTION },
54 { NULL, 0, NULL, 0 }
55};
56
Dominik Behr46c567f2016-03-08 15:11:48 -080057commandflags_t command_flags = { 0 };
Dominik Behr580462b2014-11-20 13:50:05 -080058
Stéphane Marchesin8fc13522015-12-14 17:02:28 -080059static void parse_offset(char* param, int32_t* x, int32_t* y)
David Sodman8ef20062015-01-06 09:23:40 -080060{
61 char* token;
62 char* saveptr;
63
64 token = strtok_r(param, ",", &saveptr);
65 if (token)
66 *x = strtol(token, NULL, 0);
67
68 token = strtok_r(NULL, ",", &saveptr);
69 if (token)
70 *y = strtol(token, NULL, 0);
71}
David Sodmanbbcb0522014-09-19 10:34:07 -070072
Dominik Behr44e07e62016-01-13 19:43:57 -080073int main_process_events(uint32_t usec)
74{
75 terminal_t* terminal;
76 terminal_t* new_terminal;
77 fd_set read_set, exception_set;
Dominik Behrd7112672016-01-20 16:59:34 -080078 int maxfd = -1;
Dominik Behr44e07e62016-01-13 19:43:57 -080079 int sstat;
80 struct timeval tm;
81 struct timeval* ptm;
82
83 terminal = term_get_current_terminal();
84
85 FD_ZERO(&read_set);
86 FD_ZERO(&exception_set);
87
Dominik Behrd7112672016-01-20 16:59:34 -080088 dbus_add_fds(&read_set, &exception_set, &maxfd);
Dominik Behrd7112672016-01-20 16:59:34 -080089 input_add_fds(&read_set, &exception_set, &maxfd);
Dominik Behr5239cca2016-01-21 18:22:04 -080090 dev_add_fds(&read_set, &exception_set, &maxfd);
Dominik Behr44e07e62016-01-13 19:43:57 -080091
92 for (int i = 0; i < MAX_TERMINALS; i++) {
93 if (term_is_valid(term_get_terminal(i))) {
94 terminal_t* current_term = term_get_terminal(i);
Dominik Behrd7112672016-01-20 16:59:34 -080095 term_add_fds(current_term, &read_set, &exception_set, &maxfd);
Dominik Behr44e07e62016-01-13 19:43:57 -080096 }
97 }
98
99 if (usec) {
100 ptm = &tm;
101 tm.tv_sec = 0;
102 tm.tv_usec = usec;
103 } else
104 ptm = NULL;
105
Dominik Behrd7112672016-01-20 16:59:34 -0800106 sstat = select(maxfd + 1, &read_set, NULL, &exception_set, ptm);
Dominik Behr44e07e62016-01-13 19:43:57 -0800107 if (sstat == 0)
108 return 0;
109
110 dbus_dispatch_io();
111
112 if (term_exception(terminal, &exception_set))
113 return -1;
114
Dominik Behr5239cca2016-01-21 18:22:04 -0800115 dev_dispatch_io(&read_set, &exception_set);
Dominik Behr44e07e62016-01-13 19:43:57 -0800116 input_dispatch_io(&read_set, &exception_set);
117
118 for (int i = 0; i < MAX_TERMINALS; i++) {
119 if (term_is_valid(term_get_terminal(i))) {
120 terminal_t* current_term = term_get_terminal(i);
121 term_dispatch_io(current_term, &read_set);
122 }
123 }
124
125 if (term_is_valid(terminal)) {
126 if (term_is_child_done(terminal)) {
127 if (terminal == term_get_terminal(SPLASH_TERMINAL)) {
128 /*
129 * Note: reference is not lost because it is still referenced
130 * by the splash_t structure which will ultimately destroy
131 * it, once it's safe to do so
132 */
133 term_set_terminal(SPLASH_TERMINAL, NULL);
134 return -1;
135 }
Dominik Behr83010f82016-03-18 18:43:08 -0700136 term_set_current_terminal(term_init(true));
Dominik Behr44e07e62016-01-13 19:43:57 -0800137 new_terminal = term_get_current_terminal();
138 if (!term_is_valid(new_terminal)) {
139 return -1;
140 }
141 term_activate(new_terminal);
142 term_close(terminal);
143 }
144 }
145
146 return 0;
147}
148
Dominik Behr46c567f2016-03-08 15:11:48 -0800149int main_loop(void)
Dominik Behr44e07e62016-01-13 19:43:57 -0800150{
Dominik Behr44e07e62016-01-13 19:43:57 -0800151 int status;
152
Dominik Behr44e07e62016-01-13 19:43:57 -0800153 while (1) {
154 status = main_process_events(0);
155 if (status != 0) {
156 LOG(ERROR, "input process returned %d", status);
157 break;
158 }
159 }
160
161 return 0;
162}
163
Dominik Behr46c567f2016-03-08 15:11:48 -0800164bool set_drm_master_relax(void)
165{
166 int fd;
167 int num_written;
168
169 /*
170 * Setting drm_master_relax flag in kernel allows us to transfer DRM master
171 * between Chrome and frecon
172 */
173 fd = open("/sys/kernel/debug/dri/drm_master_relax", O_WRONLY);
174 if (fd != -1) {
175 num_written = write(fd, "Y", 1);
176 close(fd);
177 if (num_written != 1) {
178 LOG(ERROR, "Unable to set drm_master_relax");
179 return false;
180 }
181 } else {
182 LOG(ERROR, "unable to open drm_master_relax");
183 return false;
184 }
185 return true;
186}
187
188static void main_on_login_prompt_visible(void* ptr)
189{
190 if (command_flags.daemon && !command_flags.enable_vts) {
191 exit(EXIT_SUCCESS);
192 } else
193 if (ptr) {
194 splash_destroy((splash_t*)ptr);
195 }
196}
Dominik Behr44e07e62016-01-13 19:43:57 -0800197
David Sodmanbbcb0522014-09-19 10:34:07 -0700198int main(int argc, char* argv[])
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700199{
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700200 int ret;
David Sodmanbbcb0522014-09-19 10:34:07 -0700201 int c;
David Sodman8ef20062015-01-06 09:23:40 -0800202 int32_t x, y;
Stéphane Marchesin00ff1872015-12-14 13:40:09 -0800203 splash_t* splash;
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700204
Douglas Anderson4da95cd2015-10-05 15:04:30 -0700205 /* Handle resolution special before splash init */
206 for (;;) {
207 c = getopt_long(argc, argv, "", command_options, NULL);
208 if (c == -1) {
209 break;
210 } else if (c == FLAG_PRINT_RESOLUTION) {
Dominik Behr83010f82016-03-18 18:43:08 -0700211 drm_t *drm = drm_scan();
212 if (!drm)
Douglas Anderson4da95cd2015-10-05 15:04:30 -0700213 return EXIT_FAILURE;
214
Dominik Behr83010f82016-03-18 18:43:08 -0700215 printf("%d %d", drm_gethres(drm),
216 drm_getvres(drm));
217 drm_delref(drm);
Douglas Anderson4da95cd2015-10-05 15:04:30 -0700218 return EXIT_SUCCESS;
219 }
220 }
221
222 /* Reset option parsing */
223 optind = 1;
224
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700225 ret = input_init();
226 if (ret) {
David Sodmanbbcb0522014-09-19 10:34:07 -0700227 LOG(ERROR, "Input init failed");
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700228 return EXIT_FAILURE;
229 }
230
Dominik Behr5239cca2016-01-21 18:22:04 -0800231 ret = dev_init();
232 if (ret) {
233 LOG(ERROR, "Device management init failed");
234 return EXIT_FAILURE;
235 }
236
Dominik Behr83010f82016-03-18 18:43:08 -0700237 drm_set(drm_scan());
David Sodmanbf3f2842014-11-12 08:26:58 -0800238 splash = splash_init();
David Sodmanbbcb0522014-09-19 10:34:07 -0700239 if (splash == NULL) {
240 LOG(ERROR, "splash init failed");
241 return EXIT_FAILURE;
242 }
243
244 for (;;) {
245 c = getopt_long(argc, argv, "", command_options, NULL);
246
247 if (c == -1)
248 break;
249
250 switch (c) {
251 case FLAG_CLEAR:
252 splash_set_clear(splash, strtoul(optarg, NULL, 0));
253 break;
254
255 case FLAG_DAEMON:
Dominik Behr46c567f2016-03-08 15:11:48 -0800256 command_flags.daemon = true;
David Sodmanbbcb0522014-09-19 10:34:07 -0700257 break;
258
David Sodman8ef20062015-01-06 09:23:40 -0800259 case FLAG_FRAME_INTERVAL:
260 splash_set_default_duration(splash, strtoul(optarg, NULL, 0));
261 break;
262
Dominik Behr46c567f2016-03-08 15:11:48 -0800263 case FLAG_ENABLE_VTS:
264 command_flags.enable_vts = true;
David Sodmanbbcb0522014-09-19 10:34:07 -0700265 break;
266
Dominik Behr32a7c892015-10-09 15:47:53 -0700267 case FLAG_IMAGE:
268 if (!splash_is_hires(splash))
269 splash_add_image(splash, optarg);
270 break;
271
272 case FLAG_IMAGE_HIRES:
273 if (splash_is_hires(splash))
274 splash_add_image(splash, optarg);
275 break;
276
David Sodman8ef20062015-01-06 09:23:40 -0800277 case FLAG_LOOP_START:
278 splash_set_loop_start(splash, strtoul(optarg, NULL, 0));
279 break;
280
281 case FLAG_LOOP_INTERVAL:
282 splash_set_loop_duration(splash, strtoul(optarg, NULL, 0));
283 break;
284
285 case FLAG_LOOP_OFFSET:
286 parse_offset(optarg, &x, &y);
287 splash_set_loop_offset(splash, x, y);
288 break;
289
290 case FLAG_OFFSET:
291 parse_offset(optarg, &x, &y);
292 splash_set_offset(splash, x, y);
293 break;
David Sodmanbbcb0522014-09-19 10:34:07 -0700294 }
295 }
296
Stéphane Marchesinac14d292015-12-14 15:27:18 -0800297 for (int i = optind; i < argc; i++)
David Sodman8ef20062015-01-06 09:23:40 -0800298 splash_add_image(splash, argv[i]);
299
Dominik Behr46c567f2016-03-08 15:11:48 -0800300 if (command_flags.daemon) {
David Sodmanf0a925a2015-05-04 11:19:19 -0700301 splash_present_term_file(splash);
302 daemonize();
303 }
Dominik Behr46c567f2016-03-08 15:11:48 -0800304
David Sodmanf0a925a2015-05-04 11:19:19 -0700305 if (splash_num_images(splash) > 0) {
Dominik Behr797a3832016-01-11 15:53:11 -0800306 ret = splash_run(splash);
David Sodmanbbcb0522014-09-19 10:34:07 -0700307 if (ret) {
Yuly Novikov945871e2015-05-23 00:00:41 -0400308 LOG(ERROR, "splash_run failed: %d", ret);
309 return EXIT_FAILURE;
David Sodmanbbcb0522014-09-19 10:34:07 -0700310 }
311 }
312
313 /*
Dominik Behr46c567f2016-03-08 15:11:48 -0800314 * The DBUS service launches later than the boot-splash service, and
315 * as a result, when splash_run starts DBUS is not yet up, but, by
316 * the time splash_run completes, it is running.
317 * We really need DBUS now, so we can interact with Chrome
David Sodmanbbcb0522014-09-19 10:34:07 -0700318 */
Dominik Behr5f6742f2016-03-10 18:03:54 -0800319 dbus_init_wait();
David Sodman8ef20062015-01-06 09:23:40 -0800320
Dominik Behr46c567f2016-03-08 15:11:48 -0800321 /*
322 * Ask DBUS to call us back so we can destroy splash (or quit) when login
323 * prompt is visible;
324 */
325 dbus_set_login_prompt_visible_callback(main_on_login_prompt_visible,
326 (void*)splash);
327
328 if (command_flags.daemon) {
329 if (command_flags.enable_vts)
330 set_drm_master_relax();
331 dbus_take_display_ownership();
332 } else {
Dominik Behr46c567f2016-03-08 15:11:48 -0800333 /* create and switch to first term in interactve mode */
334 terminal_t* terminal;
Dominik Behr5f6742f2016-03-10 18:03:54 -0800335 set_drm_master_relax();
Dominik Behr46c567f2016-03-08 15:11:48 -0800336 dbus_release_display_ownership();
Dominik Behr83010f82016-03-18 18:43:08 -0700337 term_set_current_terminal(term_init(true));
Dominik Behr46c567f2016-03-08 15:11:48 -0800338 terminal = term_get_current_terminal();
339 term_activate(terminal);
340 }
341
342 ret = main_loop();
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700343
344 input_close();
Dominik Behr5239cca2016-01-21 18:22:04 -0800345 dev_close();
Dominik Behr44e07e62016-01-13 19:43:57 -0800346 dbus_destroy();
Dominik Behr83010f82016-03-18 18:43:08 -0700347 drm_close();
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700348
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700349 return ret;
350}