David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 7 | #include <fcntl.h> |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 8 | #include <stdbool.h> |
| 9 | #include <stddef.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <sys/mman.h> |
| 14 | #include <unistd.h> |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 15 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 16 | #include "dbus_interface.h" |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 17 | #include "image.h" |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 18 | #include "input.h" |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 19 | #include "splash.h" |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 20 | #include "term.h" |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 21 | #include "util.h" |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 22 | |
| 23 | #define MAX_SPLASH_IMAGES (30) |
David Sodman | dd5b23e | 2015-02-18 21:36:38 -0800 | [diff] [blame] | 24 | #define MAX_SPLASH_WAITTIME (8) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 25 | #define DBUS_WAIT_DELAY (50000) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 26 | |
| 27 | typedef struct { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 28 | image_t *image; |
| 29 | uint32_t duration; |
| 30 | } splash_frame_t; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 31 | |
| 32 | struct _splash_t { |
| 33 | video_t *video; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 34 | terminal_t *terminal; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 35 | int num_images; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 36 | uint32_t clear; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 37 | splash_frame_t image_frames[MAX_SPLASH_IMAGES]; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 38 | bool terminated; |
| 39 | bool devmode; |
| 40 | dbus_t *dbus; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 41 | int32_t loop_start; |
| 42 | uint32_t loop_duration; |
| 43 | uint32_t default_duration; |
| 44 | int32_t offset_x; |
| 45 | int32_t offset_y; |
| 46 | int32_t loop_offset_x; |
| 47 | int32_t loop_offset_y; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 50 | |
| 51 | splash_t* splash_init() |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 52 | { |
| 53 | splash_t* splash; |
David Sodman | 73e8227 | 2014-11-17 10:11:47 -0800 | [diff] [blame] | 54 | FILE *cookie_fp; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 55 | |
| 56 | splash = (splash_t*)calloc(1, sizeof(splash_t)); |
| 57 | if (splash == NULL) |
| 58 | return NULL; |
| 59 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 60 | splash->video = video_init(); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 61 | splash->terminal = input_create_splash_term(splash->video); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 62 | splash->loop_start = -1; |
| 63 | splash->default_duration = 25; |
| 64 | splash->loop_duration = 25; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 65 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 66 | // Hide the cursor on the splash screen |
| 67 | term_hide_cursor(splash->terminal); |
| 68 | |
David Sodman | 73e8227 | 2014-11-17 10:11:47 -0800 | [diff] [blame] | 69 | cookie_fp = fopen("/tmp/display_info.bin", "wb"); |
| 70 | if (cookie_fp) { |
| 71 | fwrite(&splash->video->internal_panel, sizeof(char), 1, cookie_fp); |
| 72 | fwrite(splash->video->edid, EDID_SIZE, 1, cookie_fp); |
| 73 | fclose(cookie_fp); |
| 74 | } |
| 75 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 76 | return splash; |
| 77 | } |
| 78 | |
| 79 | int splash_destroy(splash_t* splash) |
| 80 | { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 81 | if (splash->terminal) { |
| 82 | term_close(splash->terminal); |
| 83 | splash->terminal = NULL; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 84 | } |
| 85 | free(splash); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 86 | input_destroy_splash_term(); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 90 | int splash_set_clear(splash_t *splash, uint32_t clear_color) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 91 | { |
| 92 | splash->clear = clear_color; |
| 93 | return 0; |
| 94 | } |
| 95 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 96 | int splash_add_image(splash_t* splash, char* filespec) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 97 | { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 98 | image_t* image; |
| 99 | int32_t offset_x, offset_y; |
| 100 | char *filename; |
| 101 | uint32_t duration; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 102 | if (splash->num_images >= MAX_SPLASH_IMAGES) |
| 103 | return 1; |
| 104 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 105 | filename = (char*)malloc(strlen(filespec) + 1); |
| 106 | parse_filespec(filespec, |
| 107 | filename, |
| 108 | &offset_x, &offset_y, &duration, |
| 109 | splash->default_duration, |
| 110 | splash->offset_x, |
| 111 | splash->offset_y); |
| 112 | |
| 113 | image = image_create(); |
| 114 | image_set_filename(image, filename); |
| 115 | image_set_offset(image, offset_x, offset_y); |
| 116 | splash->image_frames[splash->num_images].image = image; |
| 117 | splash->image_frames[splash->num_images].duration = duration; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 118 | splash->num_images++; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 119 | |
| 120 | free(filename); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 124 | static void splash_clear_screen(splash_t *splash) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 125 | { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 126 | term_set_background(splash->terminal, splash->clear); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | int splash_run(splash_t* splash, dbus_t** dbus) |
| 130 | { |
| 131 | int i; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 132 | int status; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 133 | int64_t last_show_ms; |
| 134 | int64_t now_ms; |
| 135 | int64_t sleep_ms; |
| 136 | struct timespec sleep_spec; |
| 137 | int fd; |
| 138 | int num_written; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 139 | image_t* image; |
| 140 | uint32_t duration; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 141 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 142 | /* |
| 143 | * First draw the actual splash screen |
| 144 | */ |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 145 | splash_clear_screen(splash); |
| 146 | term_activate(splash->terminal); |
| 147 | last_show_ms = -1; |
| 148 | for (i = 0; i < splash->num_images; i++) { |
| 149 | image = splash->image_frames[i].image; |
| 150 | status = image_load_image_from_file(image); |
| 151 | if (status != 0) { |
| 152 | LOG(WARNING, "image_load_image_from_file failed: %d", status); |
| 153 | break; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 154 | } |
| 155 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 156 | now_ms = get_monotonic_time_ms(); |
| 157 | if (last_show_ms > 0) { |
| 158 | if (splash->loop_start >= 0 && i >= splash->loop_start) |
| 159 | duration = splash->loop_duration; |
| 160 | else |
| 161 | duration = splash->image_frames[i].duration; |
| 162 | sleep_ms = duration - (now_ms - last_show_ms); |
| 163 | if (sleep_ms > 0) { |
| 164 | sleep_spec.tv_sec = sleep_ms / MS_PER_SEC; |
| 165 | sleep_spec.tv_nsec = (sleep_ms % MS_PER_SEC) * NS_PER_MS; |
| 166 | nanosleep(&sleep_spec, NULL); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | now_ms = get_monotonic_time_ms(); |
| 171 | |
| 172 | if (i >= splash->loop_start) { |
| 173 | image_set_offset(image, |
| 174 | splash->loop_offset_x, |
| 175 | splash->loop_offset_y); |
| 176 | } |
| 177 | |
| 178 | status = term_show_image(splash->terminal, image); |
| 179 | if (status != 0) { |
| 180 | LOG(WARNING, "term_show_image failed: %d", status); |
| 181 | break; |
| 182 | } |
| 183 | status = input_process(splash->terminal, 1); |
| 184 | if (status != 0) { |
| 185 | LOG(WARNING, "input_process failed: %d", status); |
| 186 | break; |
| 187 | } |
| 188 | last_show_ms = now_ms; |
| 189 | |
| 190 | if ((splash->loop_start >= 0) && |
| 191 | (splash->loop_start < splash->num_images)) { |
| 192 | if (i == splash->num_images - 1) |
| 193 | i = splash->loop_start - 1; |
| 194 | } |
| 195 | |
| 196 | image_release(image); |
| 197 | } |
| 198 | |
| 199 | for (i = 0; i < splash->num_images; i++) { |
| 200 | image_destroy(splash->image_frames[i].image); |
| 201 | } |
| 202 | |
| 203 | input_set_current(NULL); |
| 204 | |
| 205 | /* |
| 206 | * Now Chrome can take over |
| 207 | */ |
| 208 | video_release(splash->video); |
| 209 | video_unlock(splash->video); |
| 210 | |
| 211 | if (dbus != NULL) { |
| 212 | do { |
| 213 | *dbus = dbus_init(); |
| 214 | usleep(DBUS_WAIT_DELAY); |
| 215 | } while (*dbus == NULL); |
| 216 | splash_set_dbus(splash, *dbus); |
| 217 | } |
| 218 | |
| 219 | if (splash->devmode) { |
David Sodman | 5c9afa7 | 2015-02-14 09:32:53 -0800 | [diff] [blame] | 220 | /* |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 221 | * Now set drm_master_relax so that we can transfer drm_master between |
| 222 | * chrome and frecon |
David Sodman | 5c9afa7 | 2015-02-14 09:32:53 -0800 | [diff] [blame] | 223 | */ |
| 224 | video_release(splash->video); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 225 | video_unlock(splash->video); |
David Sodman | 5c9afa7 | 2015-02-14 09:32:53 -0800 | [diff] [blame] | 226 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 227 | if (dbus != NULL) { |
| 228 | do { |
| 229 | *dbus = dbus_init(); |
| 230 | usleep(DBUS_WAIT_DELAY); |
| 231 | } while (*dbus == NULL); |
| 232 | splash_set_dbus(splash, *dbus); |
| 233 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 234 | fd = open("/sys/kernel/debug/dri/drm_master_relax", O_WRONLY); |
| 235 | if (fd != -1) { |
| 236 | num_written = write(fd, "Y", 1); |
| 237 | close(fd); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 238 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 239 | /* |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 240 | * If we can't set drm_master relax, then transitions between chrome |
| 241 | * and frecon won't work. No point in having frecon hold any resources |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 242 | */ |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 243 | if (num_written != 1) { |
| 244 | LOG(ERROR, "Unable to set drm_master_relax"); |
| 245 | splash->devmode = false; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 246 | } |
David Sodman | dd5b23e | 2015-02-18 21:36:38 -0800 | [diff] [blame] | 247 | } else { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 248 | LOG(ERROR, "unable to open drm_master_relax"); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 249 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 250 | } else { |
| 251 | /* |
| 252 | * Below, we will wait for Chrome to appear above the splash |
| 253 | * image. If we are not in dev mode, wait and then exit |
| 254 | */ |
| 255 | sleep(MAX_SPLASH_WAITTIME); |
| 256 | exit(EXIT_SUCCESS); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 257 | } |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 258 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 259 | if (splash->dbus) { |
| 260 | (void)dbus_method_call0(splash->dbus, |
| 261 | kLibCrosServiceName, |
| 262 | kLibCrosServicePath, |
| 263 | kLibCrosServiceInterface, |
| 264 | kTakeDisplayOwnership); |
| 265 | } |
David Sodman | f348b0d | 2015-02-10 08:34:57 -0800 | [diff] [blame] | 266 | |
| 267 | /* |
| 268 | * Finally, wait until chrome has drawn on top of the splash. In dev mode, |
| 269 | * wait a few seconds for chrome to show up. |
| 270 | */ |
| 271 | sleep(MAX_SPLASH_WAITTIME); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 272 | return status; |
| 273 | } |
| 274 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 275 | void splash_set_offset(splash_t* splash, int32_t x, int32_t y) |
| 276 | { |
| 277 | if (splash) { |
| 278 | splash->offset_x = x; |
| 279 | splash->offset_y = y; |
| 280 | } |
| 281 | } |
| 282 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 283 | void splash_set_dbus(splash_t* splash, dbus_t* dbus) |
| 284 | { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 285 | if (splash) |
| 286 | splash->dbus = dbus; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | void splash_set_devmode(splash_t* splash) |
| 290 | { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 291 | if (splash) |
| 292 | splash->devmode = true; |
| 293 | } |
| 294 | |
| 295 | int splash_num_images(splash_t *splash) |
| 296 | { |
| 297 | if (splash) |
| 298 | return splash->num_images; |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | void splash_set_default_duration(splash_t* splash, uint32_t duration) |
| 304 | { |
| 305 | if (splash) |
| 306 | splash->default_duration = duration; |
| 307 | } |
| 308 | |
| 309 | void splash_set_loop_start(splash_t* splash, int32_t loop_start) |
| 310 | { |
| 311 | if (splash) |
| 312 | splash->loop_start = loop_start; |
| 313 | } |
| 314 | |
| 315 | void splash_set_loop_duration(splash_t* splash, uint32_t duration) |
| 316 | { |
| 317 | if (splash) |
| 318 | splash->loop_duration = duration; |
| 319 | } |
| 320 | |
| 321 | void splash_set_loop_offset(splash_t* splash, int32_t x, int32_t y) |
| 322 | { |
| 323 | if (splash) { |
| 324 | splash->loop_offset_x = x; |
| 325 | splash->loop_offset_y = y; |
| 326 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 327 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame^] | 328 | |
| 329 | void splash_present_term_file(splash_t* splash) |
| 330 | { |
| 331 | fprintf(stdout, "%s\n", term_get_ptsname(splash->terminal)); |
| 332 | } |