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