Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 1 | /* |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 2 | * Copyright 2014 The Chromium OS Authors. All rights reserved. |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
Dominik Behr | 580462b | 2014-11-20 13:50:05 -0800 | [diff] [blame] | 7 | #include <getopt.h> |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 8 | #include <libtsm.h> |
Dominik Behr | 580462b | 2014-11-20 13:50:05 -0800 | [diff] [blame] | 9 | #include <memory.h> |
| 10 | #include <stdbool.h> |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <unistd.h> |
| 14 | |
Dominik Behr | 580462b | 2014-11-20 13:50:05 -0800 | [diff] [blame] | 15 | #include "dbus.h" |
Dominik Behr | 44e07e6 | 2016-01-13 19:43:57 -0800 | [diff] [blame^] | 16 | #include "dbus_interface.h" |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 17 | #include "input.h" |
Dominik Behr | 44e07e6 | 2016-01-13 19:43:57 -0800 | [diff] [blame^] | 18 | #include "main.h" |
Dominik Behr | 580462b | 2014-11-20 13:50:05 -0800 | [diff] [blame] | 19 | #include "splash.h" |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 20 | #include "term.h" |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 21 | #include "util.h" |
Stéphane Marchesin | 62561a1 | 2015-12-11 17:32:37 -0800 | [diff] [blame] | 22 | #include "video.h" |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 23 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 24 | #define FLAG_CLEAR 'c' |
| 25 | #define FLAG_DAEMON 'd' |
| 26 | #define FLAG_DEV_MODE 'e' |
| 27 | #define FLAG_FRAME_INTERVAL 'f' |
| 28 | #define FLAG_GAMMA 'g' |
Dominik Behr | 32a7c89 | 2015-10-09 15:47:53 -0700 | [diff] [blame] | 29 | #define FLAG_IMAGE 'i' |
| 30 | #define FLAG_IMAGE_HIRES 'I' |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 31 | #define FLAG_LOOP_START 'l' |
| 32 | #define FLAG_LOOP_INTERVAL 'L' |
| 33 | #define FLAG_LOOP_OFFSET 'o' |
| 34 | #define FLAG_OFFSET 'O' |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 35 | #define FLAG_PRINT_RESOLUTION 'p' |
| 36 | |
| 37 | static struct option command_options[] = { |
| 38 | { "clear", required_argument, NULL, FLAG_CLEAR }, |
| 39 | { "daemon", no_argument, NULL, FLAG_DAEMON }, |
| 40 | { "dev-mode", no_argument, NULL, FLAG_DEV_MODE }, |
| 41 | { "frame-interval", required_argument, NULL, FLAG_FRAME_INTERVAL }, |
| 42 | { "gamma", required_argument, NULL, FLAG_GAMMA }, |
Dominik Behr | 32a7c89 | 2015-10-09 15:47:53 -0700 | [diff] [blame] | 43 | { "image", required_argument, NULL, FLAG_IMAGE }, |
| 44 | { "image-hires", required_argument, NULL, FLAG_IMAGE_HIRES }, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 45 | { "loop-start", required_argument, NULL, FLAG_LOOP_START }, |
| 46 | { "loop-interval", required_argument, NULL, FLAG_LOOP_INTERVAL }, |
| 47 | { "loop-offset", required_argument, NULL, FLAG_LOOP_OFFSET }, |
| 48 | { "offset", required_argument, NULL, FLAG_OFFSET }, |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 49 | { "print-resolution", no_argument, NULL, FLAG_PRINT_RESOLUTION }, |
| 50 | { NULL, 0, NULL, 0 } |
| 51 | }; |
| 52 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 53 | typedef struct { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 54 | bool standalone; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 55 | } commandflags_t; |
Dominik Behr | 580462b | 2014-11-20 13:50:05 -0800 | [diff] [blame] | 56 | |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 57 | static void parse_offset(char* param, int32_t* x, int32_t* y) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 58 | { |
| 59 | char* token; |
| 60 | char* saveptr; |
| 61 | |
| 62 | token = strtok_r(param, ",", &saveptr); |
| 63 | if (token) |
| 64 | *x = strtol(token, NULL, 0); |
| 65 | |
| 66 | token = strtok_r(NULL, ",", &saveptr); |
| 67 | if (token) |
| 68 | *y = strtol(token, NULL, 0); |
| 69 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 70 | |
Dominik Behr | 44e07e6 | 2016-01-13 19:43:57 -0800 | [diff] [blame^] | 71 | int main_process_events(uint32_t usec) |
| 72 | { |
| 73 | terminal_t* terminal; |
| 74 | terminal_t* new_terminal; |
| 75 | fd_set read_set, exception_set; |
| 76 | int maxfd; |
| 77 | int sstat; |
| 78 | struct timeval tm; |
| 79 | struct timeval* ptm; |
| 80 | |
| 81 | terminal = term_get_current_terminal(); |
| 82 | |
| 83 | FD_ZERO(&read_set); |
| 84 | FD_ZERO(&exception_set); |
| 85 | |
| 86 | maxfd = dbus_add_fds(&read_set, &exception_set) + 1; |
| 87 | |
| 88 | maxfd = MAX(maxfd, input_add_fds(&read_set, &exception_set)) + 1; |
| 89 | |
| 90 | for (int i = 0; i < MAX_TERMINALS; i++) { |
| 91 | if (term_is_valid(term_get_terminal(i))) { |
| 92 | terminal_t* current_term = term_get_terminal(i); |
| 93 | maxfd = MAX(maxfd, term_add_fds(current_term, &read_set, &exception_set)) + 1; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (usec) { |
| 98 | ptm = &tm; |
| 99 | tm.tv_sec = 0; |
| 100 | tm.tv_usec = usec; |
| 101 | } else |
| 102 | ptm = NULL; |
| 103 | |
| 104 | sstat = select(maxfd, &read_set, NULL, &exception_set, ptm); |
| 105 | if (sstat == 0) |
| 106 | return 0; |
| 107 | |
| 108 | dbus_dispatch_io(); |
| 109 | |
| 110 | if (term_exception(terminal, &exception_set)) |
| 111 | return -1; |
| 112 | |
| 113 | input_dispatch_io(&read_set, &exception_set); |
| 114 | |
| 115 | for (int i = 0; i < MAX_TERMINALS; i++) { |
| 116 | if (term_is_valid(term_get_terminal(i))) { |
| 117 | terminal_t* current_term = term_get_terminal(i); |
| 118 | term_dispatch_io(current_term, &read_set); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (term_is_valid(terminal)) { |
| 123 | if (term_is_child_done(terminal)) { |
| 124 | if (terminal == term_get_terminal(SPLASH_TERMINAL)) { |
| 125 | /* |
| 126 | * Note: reference is not lost because it is still referenced |
| 127 | * by the splash_t structure which will ultimately destroy |
| 128 | * it, once it's safe to do so |
| 129 | */ |
| 130 | term_set_terminal(SPLASH_TERMINAL, NULL); |
| 131 | return -1; |
| 132 | } |
| 133 | term_set_current_terminal(term_init(true, term_getvideo(terminal))); |
| 134 | new_terminal = term_get_current_terminal(); |
| 135 | if (!term_is_valid(new_terminal)) { |
| 136 | return -1; |
| 137 | } |
| 138 | term_activate(new_terminal); |
| 139 | term_close(terminal); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | int main_loop(bool standalone) |
| 147 | { |
| 148 | terminal_t* terminal; |
| 149 | int status; |
| 150 | |
| 151 | if (standalone) { |
| 152 | dbus_take_display_ownership(); |
| 153 | term_set_current_terminal(term_init(true, NULL)); |
| 154 | terminal = term_get_current_terminal(); |
| 155 | term_activate(terminal); |
| 156 | } |
| 157 | |
| 158 | while (1) { |
| 159 | status = main_process_events(0); |
| 160 | if (status != 0) { |
| 161 | LOG(ERROR, "input process returned %d", status); |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 170 | int main(int argc, char* argv[]) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 171 | { |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 172 | int ret; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 173 | int c; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 174 | int32_t x, y; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 175 | splash_t* splash; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 176 | commandflags_t command_flags; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 177 | |
Douglas Anderson | 4da95cd | 2015-10-05 15:04:30 -0700 | [diff] [blame] | 178 | /* Handle resolution special before splash init */ |
| 179 | for (;;) { |
| 180 | c = getopt_long(argc, argv, "", command_options, NULL); |
| 181 | if (c == -1) { |
| 182 | break; |
| 183 | } else if (c == FLAG_PRINT_RESOLUTION) { |
| 184 | video_t *video = video_init(); |
| 185 | if (!video) |
| 186 | return EXIT_FAILURE; |
| 187 | |
| 188 | printf("%d %d", video_getwidth(video), |
| 189 | video_getheight(video)); |
| 190 | video_close(video); |
| 191 | return EXIT_SUCCESS; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /* Reset option parsing */ |
| 196 | optind = 1; |
| 197 | |
Dominik Behr | 580462b | 2014-11-20 13:50:05 -0800 | [diff] [blame] | 198 | memset(&command_flags, 0, sizeof(command_flags)); |
| 199 | command_flags.standalone = true; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 200 | |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 201 | ret = input_init(); |
| 202 | if (ret) { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 203 | LOG(ERROR, "Input init failed"); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 204 | return EXIT_FAILURE; |
| 205 | } |
| 206 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 207 | splash = splash_init(); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 208 | if (splash == NULL) { |
| 209 | LOG(ERROR, "splash init failed"); |
| 210 | return EXIT_FAILURE; |
| 211 | } |
| 212 | |
| 213 | for (;;) { |
| 214 | c = getopt_long(argc, argv, "", command_options, NULL); |
| 215 | |
| 216 | if (c == -1) |
| 217 | break; |
| 218 | |
| 219 | switch (c) { |
| 220 | case FLAG_CLEAR: |
| 221 | splash_set_clear(splash, strtoul(optarg, NULL, 0)); |
| 222 | break; |
| 223 | |
| 224 | case FLAG_DAEMON: |
Dominik Behr | 580462b | 2014-11-20 13:50:05 -0800 | [diff] [blame] | 225 | command_flags.standalone = false; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 226 | break; |
| 227 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 228 | case FLAG_FRAME_INTERVAL: |
| 229 | splash_set_default_duration(splash, strtoul(optarg, NULL, 0)); |
| 230 | break; |
| 231 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 232 | case FLAG_DEV_MODE: |
| 233 | splash_set_devmode(splash); |
| 234 | break; |
| 235 | |
Dominik Behr | 32a7c89 | 2015-10-09 15:47:53 -0700 | [diff] [blame] | 236 | case FLAG_IMAGE: |
| 237 | if (!splash_is_hires(splash)) |
| 238 | splash_add_image(splash, optarg); |
| 239 | break; |
| 240 | |
| 241 | case FLAG_IMAGE_HIRES: |
| 242 | if (splash_is_hires(splash)) |
| 243 | splash_add_image(splash, optarg); |
| 244 | break; |
| 245 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 246 | case FLAG_LOOP_START: |
| 247 | splash_set_loop_start(splash, strtoul(optarg, NULL, 0)); |
| 248 | break; |
| 249 | |
| 250 | case FLAG_LOOP_INTERVAL: |
| 251 | splash_set_loop_duration(splash, strtoul(optarg, NULL, 0)); |
| 252 | break; |
| 253 | |
| 254 | case FLAG_LOOP_OFFSET: |
| 255 | parse_offset(optarg, &x, &y); |
| 256 | splash_set_loop_offset(splash, x, y); |
| 257 | break; |
| 258 | |
| 259 | case FLAG_OFFSET: |
| 260 | parse_offset(optarg, &x, &y); |
| 261 | splash_set_offset(splash, x, y); |
| 262 | break; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Stéphane Marchesin | ac14d29 | 2015-12-14 15:27:18 -0800 | [diff] [blame] | 266 | for (int i = optind; i < argc; i++) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 267 | splash_add_image(splash, argv[i]); |
| 268 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 269 | /* |
| 270 | * The DBUS service launches later than the boot-splash service, and |
| 271 | * as a result, when splash_run starts dbus is not yet up, but, by |
| 272 | * the time splash_run completes, it is running. At the same time, |
| 273 | * splash_run needs dbus to determine when chrome is visible. So, |
| 274 | * it creates the dbus object and then passes it back to the caller |
| 275 | * who can then pass it to the other objects that need it |
| 276 | */ |
Douglas Anderson | 4da95cd | 2015-10-05 15:04:30 -0700 | [diff] [blame] | 277 | if (command_flags.standalone == false) { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 278 | splash_present_term_file(splash); |
| 279 | daemonize(); |
| 280 | } |
| 281 | if (splash_num_images(splash) > 0) { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 282 | ret = splash_run(splash); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 283 | if (ret) { |
Yuly Novikov | 945871e | 2015-05-23 00:00:41 -0400 | [diff] [blame] | 284 | LOG(ERROR, "splash_run failed: %d", ret); |
| 285 | return EXIT_FAILURE; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 288 | splash_destroy(splash); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 289 | |
| 290 | /* |
| 291 | * If splash_run didn't create the dbus object (for example, if |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 292 | * there are no splash screen images), then go ahead and create |
| 293 | * it now |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 294 | */ |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 295 | if (!dbus_is_initialized()) { |
| 296 | dbus_init(); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 297 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 298 | |
Dominik Behr | 44e07e6 | 2016-01-13 19:43:57 -0800 | [diff] [blame^] | 299 | ret = main_loop(command_flags.standalone); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 300 | |
| 301 | input_close(); |
Dominik Behr | 44e07e6 | 2016-01-13 19:43:57 -0800 | [diff] [blame^] | 302 | dbus_destroy(); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 303 | |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 304 | return ret; |
| 305 | } |