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