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 | 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 | terminal_t* terminal; |
| 35 | int num_images; |
| 36 | uint32_t clear; |
| 37 | splash_frame_t image_frames[MAX_SPLASH_IMAGES]; |
| 38 | bool terminated; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 39 | int32_t loop_start; |
Dominik Behr | fd9fdda | 2016-03-28 17:16:45 -0700 | [diff] [blame] | 40 | int32_t loop_count; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 41 | uint32_t loop_duration; |
| 42 | uint32_t default_duration; |
| 43 | int32_t offset_x; |
| 44 | int32_t offset_y; |
| 45 | int32_t loop_offset_x; |
| 46 | int32_t loop_offset_y; |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 47 | uint32_t scale; |
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 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 59 | splash->terminal = term_create_splash_term(); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 60 | splash->loop_start = -1; |
Dominik Behr | fd9fdda | 2016-03-28 17:16:45 -0700 | [diff] [blame] | 61 | splash->loop_count = -1; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 62 | splash->default_duration = 25; |
| 63 | splash->loop_duration = 25; |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 64 | splash->scale = 1; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 65 | |
| 66 | return splash; |
| 67 | } |
| 68 | |
| 69 | int splash_destroy(splash_t* splash) |
| 70 | { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 71 | if (splash->terminal) { |
| 72 | term_close(splash->terminal); |
| 73 | splash->terminal = NULL; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 74 | } |
| 75 | free(splash); |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 76 | term_destroy_splash_term(); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 80 | int splash_set_clear(splash_t* splash, uint32_t clear_color) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 81 | { |
| 82 | splash->clear = clear_color; |
| 83 | return 0; |
| 84 | } |
| 85 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 86 | int splash_add_image(splash_t* splash, char* filespec) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 87 | { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 88 | image_t* image; |
| 89 | int32_t offset_x, offset_y; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 90 | char* filename; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 91 | uint32_t duration; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 92 | if (splash->num_images >= MAX_SPLASH_IMAGES) |
| 93 | return 1; |
| 94 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 95 | filename = (char*)malloc(strlen(filespec) + 1); |
| 96 | parse_filespec(filespec, |
| 97 | filename, |
| 98 | &offset_x, &offset_y, &duration, |
| 99 | splash->default_duration, |
| 100 | splash->offset_x, |
| 101 | splash->offset_y); |
| 102 | |
| 103 | image = image_create(); |
| 104 | image_set_filename(image, filename); |
| 105 | image_set_offset(image, offset_x, offset_y); |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 106 | if (splash->scale == 0) |
| 107 | image_set_scale(image, splash_is_hires(splash) ? 2 : 1); |
| 108 | else |
| 109 | image_set_scale(image, splash->scale); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 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); |
Dominik Behr | b1abcba | 2016-04-14 14:57:21 -0700 | [diff] [blame] | 121 | term_clear(splash->terminal); |
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; |
Dominik Behr | fd9fdda | 2016-03-28 17:16:45 -0700 | [diff] [blame] | 127 | int status = 0; |
Dominik Behr | 607e4dc | 2016-06-14 21:21:28 -0700 | [diff] [blame^] | 128 | /* |
| 129 | * Counters for throttling error messages. Only at most MAX_SPLASH_IMAGES |
| 130 | * of each type of error are logged so every frame of animation could log |
| 131 | * error message but it wouldn't spam the log. |
| 132 | */ |
| 133 | int ec_li = 0, ec_ts = 0, ec_ip = 0; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 134 | int64_t last_show_ms; |
| 135 | int64_t now_ms; |
| 136 | int64_t sleep_ms; |
| 137 | struct timespec sleep_spec; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 138 | image_t* image; |
| 139 | uint32_t duration; |
Dominik Behr | fd9fdda | 2016-03-28 17:16:45 -0700 | [diff] [blame] | 140 | int32_t c, loop_start, loop_count; |
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; |
Dominik Behr | fd9fdda | 2016-03-28 17:16:45 -0700 | [diff] [blame] | 148 | loop_count = (splash->loop_start >= 0 && splash->loop_start < splash->num_images) ? splash->loop_count : 1; |
| 149 | loop_start = (splash->loop_start >= 0 && splash->loop_start < splash->num_images) ? splash->loop_start : 0; |
| 150 | |
| 151 | for (c = 0; ((loop_count < 0) ? true : (c < loop_count)); c++) |
| 152 | for (i = (c > 0) ? loop_start : 0; i < splash->num_images; i++) { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 153 | image = splash->image_frames[i].image; |
| 154 | status = image_load_image_from_file(image); |
Dominik Behr | 607e4dc | 2016-06-14 21:21:28 -0700 | [diff] [blame^] | 155 | if (status != 0 && ec_li < MAX_SPLASH_IMAGES) { |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 156 | LOG(WARNING, "image_load_image_from_file %s failed: %d:%s.", |
| 157 | image_get_filename(image), status, strerror(status)); |
Dominik Behr | 607e4dc | 2016-06-14 21:21:28 -0700 | [diff] [blame^] | 158 | ec_li++; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 159 | } |
Dominik Behr | 607e4dc | 2016-06-14 21:21:28 -0700 | [diff] [blame^] | 160 | /* |
| 161 | * Check status again after timing code so we preserve animation |
| 162 | * frame timings and dont's monopolize CPU time. |
| 163 | */ |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 164 | now_ms = get_monotonic_time_ms(); |
| 165 | if (last_show_ms > 0) { |
| 166 | if (splash->loop_start >= 0 && i >= splash->loop_start) |
| 167 | duration = splash->loop_duration; |
| 168 | else |
| 169 | duration = splash->image_frames[i].duration; |
| 170 | sleep_ms = duration - (now_ms - last_show_ms); |
| 171 | if (sleep_ms > 0) { |
| 172 | sleep_spec.tv_sec = sleep_ms / MS_PER_SEC; |
| 173 | sleep_spec.tv_nsec = (sleep_ms % MS_PER_SEC) * NS_PER_MS; |
| 174 | nanosleep(&sleep_spec, NULL); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | now_ms = get_monotonic_time_ms(); |
Dominik Behr | 607e4dc | 2016-06-14 21:21:28 -0700 | [diff] [blame^] | 179 | if (status != 0) { |
| 180 | goto img_error; |
| 181 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 182 | |
| 183 | if (i >= splash->loop_start) { |
| 184 | image_set_offset(image, |
| 185 | splash->loop_offset_x, |
| 186 | splash->loop_offset_y); |
| 187 | } |
| 188 | |
| 189 | status = term_show_image(splash->terminal, image); |
Dominik Behr | 607e4dc | 2016-06-14 21:21:28 -0700 | [diff] [blame^] | 190 | if (status != 0 && ec_ts < MAX_SPLASH_IMAGES) { |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 191 | LOG(WARNING, "term_show_image failed: %d:%s.", status, strerror(status)); |
Dominik Behr | 607e4dc | 2016-06-14 21:21:28 -0700 | [diff] [blame^] | 192 | ec_ts++; |
| 193 | goto img_error; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 194 | } |
Dominik Behr | 44e07e6 | 2016-01-13 19:43:57 -0800 | [diff] [blame] | 195 | status = main_process_events(1); |
Dominik Behr | 607e4dc | 2016-06-14 21:21:28 -0700 | [diff] [blame^] | 196 | if (status != 0 && ec_ip < MAX_SPLASH_IMAGES) { |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 197 | LOG(WARNING, "input_process failed: %d:%s.", status, strerror(status)); |
Dominik Behr | 607e4dc | 2016-06-14 21:21:28 -0700 | [diff] [blame^] | 198 | ec_ip++; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 199 | } |
Dominik Behr | 607e4dc | 2016-06-14 21:21:28 -0700 | [diff] [blame^] | 200 | img_error: |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 201 | last_show_ms = now_ms; |
| 202 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 203 | image_release(image); |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 204 | /* see if we can initialize DBUS */ |
| 205 | if (!dbus_is_initialized()) |
| 206 | dbus_init(); |
Dominik Behr | 607e4dc | 2016-06-14 21:21:28 -0700 | [diff] [blame^] | 207 | if (status != 0) { |
| 208 | break; |
| 209 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | for (i = 0; i < splash->num_images; i++) { |
| 213 | image_destroy(splash->image_frames[i].image); |
| 214 | } |
| 215 | |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 216 | term_set_current_to(NULL); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 217 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 218 | return status; |
| 219 | } |
| 220 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 221 | void splash_set_offset(splash_t* splash, int32_t x, int32_t y) |
| 222 | { |
| 223 | if (splash) { |
| 224 | splash->offset_x = x; |
| 225 | splash->offset_y = y; |
| 226 | } |
| 227 | } |
| 228 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 229 | int splash_num_images(splash_t* splash) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 230 | { |
| 231 | if (splash) |
| 232 | return splash->num_images; |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
Dominik Behr | fd9fdda | 2016-03-28 17:16:45 -0700 | [diff] [blame] | 237 | void splash_set_loop_count(splash_t* splash, int32_t count) |
| 238 | { |
| 239 | if (splash) |
| 240 | splash->loop_count = count; |
| 241 | } |
| 242 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 243 | void splash_set_default_duration(splash_t* splash, uint32_t duration) |
| 244 | { |
| 245 | if (splash) |
| 246 | splash->default_duration = duration; |
| 247 | } |
| 248 | |
| 249 | void splash_set_loop_start(splash_t* splash, int32_t loop_start) |
| 250 | { |
| 251 | if (splash) |
| 252 | splash->loop_start = loop_start; |
| 253 | } |
| 254 | |
| 255 | void splash_set_loop_duration(splash_t* splash, uint32_t duration) |
| 256 | { |
| 257 | if (splash) |
| 258 | splash->loop_duration = duration; |
| 259 | } |
| 260 | |
| 261 | void splash_set_loop_offset(splash_t* splash, int32_t x, int32_t y) |
| 262 | { |
| 263 | if (splash) { |
| 264 | splash->loop_offset_x = x; |
| 265 | splash->loop_offset_y = y; |
| 266 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 267 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 268 | |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 269 | void splash_set_scale(splash_t* splash, uint32_t scale) |
| 270 | { |
| 271 | if (scale > MAX_SCALE_FACTOR) |
| 272 | scale = MAX_SCALE_FACTOR; |
| 273 | if (splash) |
| 274 | splash->scale = scale; |
| 275 | } |
| 276 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 277 | void splash_present_term_file(splash_t* splash) |
| 278 | { |
| 279 | fprintf(stdout, "%s\n", term_get_ptsname(splash->terminal)); |
| 280 | } |
Dominik Behr | 32a7c89 | 2015-10-09 15:47:53 -0700 | [diff] [blame] | 281 | |
| 282 | int splash_is_hires(splash_t* splash) |
| 283 | { |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 284 | if (splash && splash->terminal && term_getfb(splash->terminal)) |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 285 | return fb_getwidth(term_getfb(splash->terminal)) > HIRES_THRESHOLD_HR; |
Dominik Behr | 32a7c89 | 2015-10-09 15:47:53 -0700 | [diff] [blame] | 286 | return 0; |
| 287 | } |
Dominik Behr | b1abcba | 2016-04-14 14:57:21 -0700 | [diff] [blame] | 288 | |
| 289 | void splash_redrm(splash_t* splash) |
| 290 | { |
| 291 | term_redrm(splash->terminal); |
| 292 | } |