-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplash.c
309 lines (267 loc) · 6.86 KB
/
splash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Copyright 2014 The ChromiumOS Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include "dbus.h"
#include "dbus_interface.h"
#include "image.h"
#include "input.h"
#include "main.h"
#include "splash.h"
#include "term.h"
#include "util.h"
#define MAX_SPLASH_IMAGES (100)
#define MAX_SPLASH_WAITTIME (8)
typedef struct {
image_t* image;
uint32_t duration;
} splash_frame_t;
struct _splash_t {
int num_images;
uint32_t clear;
splash_frame_t image_frames[MAX_SPLASH_IMAGES];
bool terminated;
int32_t loop_start;
int32_t loop_count;
uint32_t loop_duration;
uint32_t default_duration;
int32_t offset_x;
int32_t offset_y;
int32_t loop_offset_x;
int32_t loop_offset_y;
uint32_t scale;
};
splash_t* splash_init(int pts_fd)
{
splash_t* splash;
splash = (splash_t*)calloc(1, sizeof(splash_t));
if (!splash)
return NULL;
term_create_splash_term(pts_fd);
splash->loop_start = -1;
splash->loop_count = -1;
splash->default_duration = 25;
splash->loop_duration = 25;
splash->scale = 1;
return splash;
}
int splash_destroy(splash_t* splash)
{
free(splash);
term_destroy_splash_term();
return 0;
}
int splash_set_clear(splash_t* splash, uint32_t clear_color)
{
splash->clear = clear_color;
return 0;
}
int splash_add_image(splash_t* splash, char* filespec)
{
image_t* image;
int32_t offset_x, offset_y;
char* filename;
uint32_t duration;
if (splash->num_images >= MAX_SPLASH_IMAGES)
return 1;
filename = (char*)malloc(strlen(filespec) + 1);
parse_filespec(filespec,
filename,
&offset_x, &offset_y, &duration,
splash->default_duration,
splash->offset_x,
splash->offset_y);
image = image_create();
image_set_filename(image, filename);
image_set_offset(image, offset_x, offset_y);
if (splash->scale == 0)
image_set_scale(image, splash_is_hires(splash) ? 2 : 1);
else
image_set_scale(image, splash->scale);
splash->image_frames[splash->num_images].image = image;
splash->image_frames[splash->num_images].duration = duration;
splash->num_images++;
free(filename);
return 0;
}
int splash_run(splash_t* splash)
{
int i;
int status = 0;
/*
* Counters for throttling error messages. Only at most MAX_SPLASH_IMAGES
* of each type of error are logged so every frame of animation could log
* error message but it wouldn't spam the log.
*/
int ec_li = 0, ec_ts = 0, ec_ip = 0;
int64_t last_show_ms;
int64_t now_ms;
int64_t sleep_ms;
struct timespec sleep_spec;
image_t* image;
uint32_t duration;
int32_t c, loop_start, loop_count;
bool active = false;
terminal_t *terminal = term_get_terminal(TERM_SPLASH_TERMINAL);
if (!terminal)
return -ENOENT;
/* Update the bootstat metrics once the first image is shown */
errno = 0;
status = system("/usr/sbin/bootstat splash-screen-visible");
if (status) {
LOG(ERROR, "Failed to execute 'bootstat splash-screen-visible': "
"status = %d, errno = %d (%s)",
status, errno, strerror(errno));
}
/*
* First draw the actual splash screen
*/
term_set_background(terminal, splash->clear);
term_clear(terminal);
term_set_current_to(terminal);
term_update_current_link();
last_show_ms = -1;
loop_count = (splash->loop_start >= 0 && splash->loop_start < splash->num_images) ? splash->loop_count : 1;
loop_start = (splash->loop_start >= 0 && splash->loop_start < splash->num_images) ? splash->loop_start : 0;
for (c = 0; ((loop_count < 0) ? true : (c < loop_count)); c++)
for (i = (c > 0) ? loop_start : 0; i < splash->num_images; i++) {
image = splash->image_frames[i].image;
status = image_load_image_from_file(image);
if (status != 0 && ec_li < MAX_SPLASH_IMAGES) {
LOG(WARNING, "image_load_image_from_file %s failed: %d:%s.",
image_get_filename(image), status, strerror(status));
ec_li++;
}
/*
* Check status again after timing code so we preserve animation
* frame timings and dont's monopolize CPU time.
*/
now_ms = get_monotonic_time_ms();
if (last_show_ms > 0) {
if (splash->loop_start >= 0 && i >= splash->loop_start)
duration = splash->loop_duration;
else
duration = splash->image_frames[i].duration;
sleep_ms = duration - (now_ms - last_show_ms);
if (sleep_ms > 0) {
sleep_spec.tv_sec = sleep_ms / MS_PER_SEC;
sleep_spec.tv_nsec = (sleep_ms % MS_PER_SEC) * NS_PER_MS;
nanosleep(&sleep_spec, NULL);
}
}
now_ms = get_monotonic_time_ms();
if (status != 0) {
goto img_error;
}
if (i >= splash->loop_start) {
image_set_offset(image,
splash->loop_offset_x,
splash->loop_offset_y);
}
status = term_show_image(terminal, image);
if (status != 0 && ec_ts < MAX_SPLASH_IMAGES) {
LOG(WARNING, "term_show_image failed: %d:%s.", status, strerror(status));
ec_ts++;
goto img_error;
}
if (!active) {
/*
* Set video mode on first frame so user does not see
* us drawing first frame.
*/
term_activate(terminal);
active = true;
}
status = main_process_events(1);
if (status != 0 && ec_ip < MAX_SPLASH_IMAGES) {
LOG(WARNING, "input_process failed: %d:%s.", status, strerror(status));
ec_ip++;
}
img_error:
last_show_ms = now_ms;
image_release(image);
/* see if we can initialize DBUS */
if (!dbus_is_initialized())
dbus_init();
if (status != 0) {
break;
}
}
for (i = 0; i < splash->num_images; i++) {
image_destroy(splash->image_frames[i].image);
}
return status;
}
void splash_set_offset(splash_t* splash, int32_t x, int32_t y)
{
if (splash) {
splash->offset_x = x;
splash->offset_y = y;
}
}
int splash_num_images(splash_t* splash)
{
if (splash)
return splash->num_images;
return 0;
}
void splash_set_loop_count(splash_t* splash, int32_t count)
{
if (splash)
splash->loop_count = count;
}
void splash_set_default_duration(splash_t* splash, uint32_t duration)
{
if (splash)
splash->default_duration = duration;
}
void splash_set_loop_start(splash_t* splash, int32_t loop_start)
{
if (splash)
splash->loop_start = loop_start;
}
void splash_set_loop_duration(splash_t* splash, uint32_t duration)
{
if (splash)
splash->loop_duration = duration;
}
void splash_set_loop_offset(splash_t* splash, int32_t x, int32_t y)
{
if (splash) {
splash->loop_offset_x = x;
splash->loop_offset_y = y;
}
}
void splash_set_scale(splash_t* splash, uint32_t scale)
{
if (scale > MAX_SCALE_FACTOR)
scale = MAX_SCALE_FACTOR;
if (splash)
splash->scale = scale;
}
int splash_is_hires(splash_t* splash)
{
terminal_t *terminal = term_get_terminal(TERM_SPLASH_TERMINAL);
if (!terminal)
return 0;
if (term_getfb(terminal))
return image_is_hires(term_getfb(terminal));
return 0;
}
void splash_redrm(splash_t* splash)
{
terminal_t *terminal = term_get_terminal(TERM_SPLASH_TERMINAL);
if (!terminal)
return;
term_redrm(terminal);
}