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