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 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 16 | #include "dbus.h" |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 17 | #include "dbus_interface.h" |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 18 | #include "image.h" |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 19 | #include "input.h" |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 20 | #include "splash.h" |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 21 | #include "term.h" |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 22 | #include "util.h" |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 23 | |
| 24 | #define MAX_SPLASH_IMAGES (30) |
David Sodman | dd5b23e | 2015-02-18 21:36:38 -0800 | [diff] [blame] | 25 | #define MAX_SPLASH_WAITTIME (8) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 26 | #define DBUS_WAIT_DELAY (50000) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 27 | |
| 28 | typedef struct { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 29 | image_t* image; |
| 30 | uint32_t duration; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 31 | } splash_frame_t; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 32 | |
| 33 | struct _splash_t { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 34 | video_t* video; |
| 35 | terminal_t* terminal; |
| 36 | int num_images; |
| 37 | uint32_t clear; |
| 38 | splash_frame_t image_frames[MAX_SPLASH_IMAGES]; |
| 39 | bool terminated; |
| 40 | bool devmode; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -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; |
| 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 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 123 | int splash_run(splash_t* splash) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 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 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 205 | while (!dbus_is_initialized()) { |
| 206 | dbus_init(); |
| 207 | usleep(DBUS_WAIT_DELAY); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | if (splash->devmode) { |
David Sodman | 5c9afa7 | 2015-02-14 09:32:53 -0800 | [diff] [blame] | 211 | /* |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 212 | * Now set drm_master_relax so that we can transfer drm_master between |
| 213 | * chrome and frecon |
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 | fd = open("/sys/kernel/debug/dri/drm_master_relax", O_WRONLY); |
| 216 | if (fd != -1) { |
| 217 | num_written = write(fd, "Y", 1); |
| 218 | close(fd); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 219 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 220 | /* |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 221 | * If we can't set drm_master relax, then transitions between chrome |
| 222 | * 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] | 223 | */ |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 224 | if (num_written != 1) { |
| 225 | LOG(ERROR, "Unable to set drm_master_relax"); |
| 226 | splash->devmode = false; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 227 | } |
David Sodman | dd5b23e | 2015-02-18 21:36:38 -0800 | [diff] [blame] | 228 | } else { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 229 | LOG(ERROR, "unable to open drm_master_relax"); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 230 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 231 | } else { |
| 232 | /* |
| 233 | * Below, we will wait for Chrome to appear above the splash |
| 234 | * image. If we are not in dev mode, wait and then exit |
| 235 | */ |
| 236 | sleep(MAX_SPLASH_WAITTIME); |
| 237 | exit(EXIT_SUCCESS); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 238 | } |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 239 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 240 | dbus_take_display_ownership(); |
David Sodman | f348b0d | 2015-02-10 08:34:57 -0800 | [diff] [blame] | 241 | |
| 242 | /* |
| 243 | * Finally, wait until chrome has drawn on top of the splash. In dev mode, |
| 244 | * wait a few seconds for chrome to show up. |
| 245 | */ |
| 246 | sleep(MAX_SPLASH_WAITTIME); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 247 | return status; |
| 248 | } |
| 249 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 250 | void splash_set_offset(splash_t* splash, int32_t x, int32_t y) |
| 251 | { |
| 252 | if (splash) { |
| 253 | splash->offset_x = x; |
| 254 | splash->offset_y = y; |
| 255 | } |
| 256 | } |
| 257 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 258 | void splash_set_devmode(splash_t* splash) |
| 259 | { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 260 | if (splash) |
| 261 | splash->devmode = true; |
| 262 | } |
| 263 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 264 | int splash_num_images(splash_t* splash) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 265 | { |
| 266 | if (splash) |
| 267 | return splash->num_images; |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | void splash_set_default_duration(splash_t* splash, uint32_t duration) |
| 273 | { |
| 274 | if (splash) |
| 275 | splash->default_duration = duration; |
| 276 | } |
| 277 | |
| 278 | void splash_set_loop_start(splash_t* splash, int32_t loop_start) |
| 279 | { |
| 280 | if (splash) |
| 281 | splash->loop_start = loop_start; |
| 282 | } |
| 283 | |
| 284 | void splash_set_loop_duration(splash_t* splash, uint32_t duration) |
| 285 | { |
| 286 | if (splash) |
| 287 | splash->loop_duration = duration; |
| 288 | } |
| 289 | |
| 290 | void splash_set_loop_offset(splash_t* splash, int32_t x, int32_t y) |
| 291 | { |
| 292 | if (splash) { |
| 293 | splash->loop_offset_x = x; |
| 294 | splash->loop_offset_y = y; |
| 295 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 296 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 297 | |
| 298 | void splash_present_term_file(splash_t* splash) |
| 299 | { |
| 300 | fprintf(stdout, "%s\n", term_get_ptsname(splash->terminal)); |
| 301 | } |
Dominik Behr | 32a7c89 | 2015-10-09 15:47:53 -0700 | [diff] [blame] | 302 | |
| 303 | int splash_is_hires(splash_t* splash) |
| 304 | { |
| 305 | if (splash && splash->video) |
| 306 | return video_getwidth(splash->video) > 1920; |
| 307 | return 0; |
| 308 | } |