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; |
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; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 132 | image_t* image; |
| 133 | uint32_t duration; |
Dominik Behr | fd9fdda | 2016-03-28 17:16:45 -0700 | [diff] [blame] | 134 | int32_t c, loop_start, loop_count; |
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; |
Dominik Behr | fd9fdda | 2016-03-28 17:16:45 -0700 | [diff] [blame] | 142 | loop_count = (splash->loop_start >= 0 && splash->loop_start < splash->num_images) ? splash->loop_count : 1; |
| 143 | loop_start = (splash->loop_start >= 0 && splash->loop_start < splash->num_images) ? splash->loop_start : 0; |
| 144 | |
| 145 | for (c = 0; ((loop_count < 0) ? true : (c < loop_count)); c++) |
| 146 | for (i = (c > 0) ? loop_start : 0; i < splash->num_images; i++) { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 147 | image = splash->image_frames[i].image; |
| 148 | status = image_load_image_from_file(image); |
| 149 | if (status != 0) { |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 150 | LOG(WARNING, "image_load_image_from_file %s failed: %d:%s.", |
| 151 | image_get_filename(image), status, strerror(status)); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 152 | break; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 153 | } |
| 154 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 155 | now_ms = get_monotonic_time_ms(); |
| 156 | if (last_show_ms > 0) { |
| 157 | if (splash->loop_start >= 0 && i >= splash->loop_start) |
| 158 | duration = splash->loop_duration; |
| 159 | else |
| 160 | duration = splash->image_frames[i].duration; |
| 161 | sleep_ms = duration - (now_ms - last_show_ms); |
| 162 | if (sleep_ms > 0) { |
| 163 | sleep_spec.tv_sec = sleep_ms / MS_PER_SEC; |
| 164 | sleep_spec.tv_nsec = (sleep_ms % MS_PER_SEC) * NS_PER_MS; |
| 165 | nanosleep(&sleep_spec, NULL); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | now_ms = get_monotonic_time_ms(); |
| 170 | |
| 171 | if (i >= splash->loop_start) { |
| 172 | image_set_offset(image, |
| 173 | splash->loop_offset_x, |
| 174 | splash->loop_offset_y); |
| 175 | } |
| 176 | |
| 177 | status = term_show_image(splash->terminal, image); |
| 178 | if (status != 0) { |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 179 | LOG(WARNING, "term_show_image failed: %d:%s.", status, strerror(status)); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 180 | break; |
| 181 | } |
Dominik Behr | 44e07e6 | 2016-01-13 19:43:57 -0800 | [diff] [blame] | 182 | status = main_process_events(1); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 183 | if (status != 0) { |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 184 | LOG(WARNING, "input_process failed: %d:%s.", status, strerror(status)); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 185 | break; |
| 186 | } |
| 187 | last_show_ms = now_ms; |
| 188 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 189 | image_release(image); |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 190 | /* see if we can initialize DBUS */ |
| 191 | if (!dbus_is_initialized()) |
| 192 | dbus_init(); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | for (i = 0; i < splash->num_images; i++) { |
| 196 | image_destroy(splash->image_frames[i].image); |
| 197 | } |
| 198 | |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 199 | term_set_current_to(NULL); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 200 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 201 | return status; |
| 202 | } |
| 203 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 204 | void splash_set_offset(splash_t* splash, int32_t x, int32_t y) |
| 205 | { |
| 206 | if (splash) { |
| 207 | splash->offset_x = x; |
| 208 | splash->offset_y = y; |
| 209 | } |
| 210 | } |
| 211 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 212 | int splash_num_images(splash_t* splash) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 213 | { |
| 214 | if (splash) |
| 215 | return splash->num_images; |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
Dominik Behr | fd9fdda | 2016-03-28 17:16:45 -0700 | [diff] [blame] | 220 | void splash_set_loop_count(splash_t* splash, int32_t count) |
| 221 | { |
| 222 | if (splash) |
| 223 | splash->loop_count = count; |
| 224 | } |
| 225 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 226 | void splash_set_default_duration(splash_t* splash, uint32_t duration) |
| 227 | { |
| 228 | if (splash) |
| 229 | splash->default_duration = duration; |
| 230 | } |
| 231 | |
| 232 | void splash_set_loop_start(splash_t* splash, int32_t loop_start) |
| 233 | { |
| 234 | if (splash) |
| 235 | splash->loop_start = loop_start; |
| 236 | } |
| 237 | |
| 238 | void splash_set_loop_duration(splash_t* splash, uint32_t duration) |
| 239 | { |
| 240 | if (splash) |
| 241 | splash->loop_duration = duration; |
| 242 | } |
| 243 | |
| 244 | void splash_set_loop_offset(splash_t* splash, int32_t x, int32_t y) |
| 245 | { |
| 246 | if (splash) { |
| 247 | splash->loop_offset_x = x; |
| 248 | splash->loop_offset_y = y; |
| 249 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 250 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 251 | |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 252 | void splash_set_scale(splash_t* splash, uint32_t scale) |
| 253 | { |
| 254 | if (scale > MAX_SCALE_FACTOR) |
| 255 | scale = MAX_SCALE_FACTOR; |
| 256 | if (splash) |
| 257 | splash->scale = scale; |
| 258 | } |
| 259 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 260 | void splash_present_term_file(splash_t* splash) |
| 261 | { |
| 262 | fprintf(stdout, "%s\n", term_get_ptsname(splash->terminal)); |
| 263 | } |
Dominik Behr | 32a7c89 | 2015-10-09 15:47:53 -0700 | [diff] [blame] | 264 | |
| 265 | int splash_is_hires(splash_t* splash) |
| 266 | { |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 267 | if (splash && splash->terminal && term_getfb(splash->terminal)) |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 268 | return fb_getwidth(term_getfb(splash->terminal)) > HIRES_THRESHOLD_HR; |
Dominik Behr | 32a7c89 | 2015-10-09 15:47:53 -0700 | [diff] [blame] | 269 | return 0; |
| 270 | } |
Dominik Behr | b1abcba | 2016-04-14 14:57:21 -0700 | [diff] [blame] | 271 | |
| 272 | void splash_redrm(splash_t* splash) |
| 273 | { |
| 274 | term_redrm(splash->terminal); |
| 275 | } |