1 /*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <linux/input.h>
18 #include <pthread.h>
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/reboot.h>
24 #include <sys/time.h>
25 #include <time.h>
26 #include <unistd.h>
27
28 #include "common.h"
29 #include "minui/minui.h"
30 #include "recovery_ui.h"
31
32 #define MAX_COLS 96
33 #define MAX_ROWS 32
34
35 #define CHAR_WIDTH 10
36 #define CHAR_HEIGHT 18
37
38 #define PROGRESSBAR_INDETERMINATE_STATES 6
39 #define PROGRESSBAR_INDETERMINATE_FPS 15
40
41 static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER;
42 static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS];
43 static gr_surface gProgressBarIndeterminate[PROGRESSBAR_INDETERMINATE_STATES];
44 static gr_surface gProgressBarEmpty;
45 static gr_surface gProgressBarFill;
46
47 static const struct { gr_surface* surface; const char *name; } BITMAPS[] = {
48 { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" },
49 { &gBackgroundIcon[BACKGROUND_ICON_ERROR], "icon_error" },
50 { &gProgressBarIndeterminate[0], "indeterminate1" },
51 { &gProgressBarIndeterminate[1], "indeterminate2" },
52 { &gProgressBarIndeterminate[2], "indeterminate3" },
53 { &gProgressBarIndeterminate[3], "indeterminate4" },
54 { &gProgressBarIndeterminate[4], "indeterminate5" },
55 { &gProgressBarIndeterminate[5], "indeterminate6" },
56 { &gProgressBarEmpty, "progress_empty" },
57 { &gProgressBarFill, "progress_fill" },
58 { NULL, NULL },
59 };
60
61 static gr_surface gCurrentIcon = NULL;
62
63 static enum ProgressBarType {
64 PROGRESSBAR_TYPE_NONE,
65 PROGRESSBAR_TYPE_INDETERMINATE,
66 PROGRESSBAR_TYPE_NORMAL,
67 } gProgressBarType = PROGRESSBAR_TYPE_NONE;
68
69 // Progress bar scope of current operation
70 static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
71 static time_t gProgressScopeTime, gProgressScopeDuration;
72
73 // Set to 1 when both graphics pages are the same (except for the progress bar)
74 static int gPagesIdentical = 0;
75
76 // Log text overlay, displayed when a magic key is pressed
77 static char text[MAX_ROWS][MAX_COLS];
78 static int text_cols = 0, text_rows = 0;
79 static int text_col = 0, text_row = 0, text_top = 0;
80 static int show_text = 1;
81
82 static char menu[MAX_ROWS][MAX_COLS];
83 static int show_menu = 0;
84 static int menu_top = 0, menu_items = 0, menu_sel = 0;
85
86 // Key event input queue
87 static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
88 static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER;
89 static int key_queue[256], key_queue_len = 0;
90 static volatile char key_pressed[KEY_MAX + 1];
91
92 extern GRSurface* gr_draw;
93
94 // Clear the screen and draw the currently selected background icon (if any).
95 // Should only be called with gUpdateMutex locked.
draw_background_locked(gr_surface icon)96 static void draw_background_locked(gr_surface icon)
97 {
98 gPagesIdentical = 0;
99 gr_color(0, 0, 0, 255);
100 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
101
102 if (icon) {
103 int iconWidth = gr_get_width(icon);
104 int iconHeight = gr_get_height(icon);
105 int iconX = (gr_fb_width() - iconWidth) / 2;
106 int iconY = (gr_fb_height() - iconHeight) / 2;
107 gr_blit(icon, 0, 0, iconWidth, iconHeight, iconX, iconY);
108 }
109 }
110
111 // Draw the progress bar (if any) on the screen. Does not flip pages.
112 // Should only be called with gUpdateMutex locked.
draw_progress_locked()113 static void draw_progress_locked()
114 {
115 if (gProgressBarType == PROGRESSBAR_TYPE_NONE) return;
116
117 int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]);
118 int width = gr_get_width(gProgressBarEmpty);
119 int height = gr_get_height(gProgressBarEmpty);
120
121 int dx = (gr_fb_width() - width) / 2;
122 int dy = (3 * gr_fb_height() + iconHeight - 2 * height) / 4;
123
124 // Erase behind the progress bar (in case this was a progress-only update)
125 gr_color(0, 0, 0, 255);
126 gr_fill(dx, dy, width, height);
127
128 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) {
129 float progress = gProgressScopeStart + gProgress * gProgressScopeSize;
130 int pos = (int) (progress * width);
131
132 if (pos > 0) {
133 gr_blit(gProgressBarFill, 0, 0, pos, height, dx, dy);
134 }
135 if (pos < width - 1) {
136 gr_blit(gProgressBarEmpty, pos, 0, width - pos, height, dx + pos, dy);
137 }
138 }
139
140 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) {
141 static int frame = 0;
142 gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
143 frame = (frame + 1) % PROGRESSBAR_INDETERMINATE_STATES;
144 }
145 }
146
draw_text_line(int row,const char * t)147 static void draw_text_line(int row, const char* t)
148 {
149 if (t[0] != '\0') {
150 gr_text(0, (row + 1)*CHAR_HEIGHT - 1, t);
151 }
152 }
153
154 // Redraw everything on the screen. Does not flip pages.
155 // Should only be called with gUpdateMutex locked.
draw_screen_locked(void)156 static void draw_screen_locked(void)
157 {
158 draw_background_locked(gCurrentIcon);
159 draw_progress_locked();
160
161 if (show_text) {
162 gr_color(0, 0, 0, 160);
163 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
164
165 int i = 0;
166 if (show_menu) {
167 gr_color(64, 96, 255, 255);
168 gr_fill(0, (menu_top + 1 + menu_sel) * CHAR_HEIGHT,
169 gr_fb_width(), (menu_top + menu_sel + 2)*CHAR_HEIGHT + 1);
170
171 for (; i < menu_top + menu_items; ++i) {
172 if (i == menu_top + menu_sel) {
173 gr_color(255, 255, 255, 255);
174 draw_text_line(i, menu[i]);
175 gr_color(64, 96, 255, 255);
176 } else {
177 draw_text_line(i, menu[i]);
178 }
179 }
180 ++i;
181 gr_fill(0, i * CHAR_HEIGHT + CHAR_HEIGHT / 2 - 1,
182 gr_fb_width(), i * CHAR_HEIGHT + CHAR_HEIGHT / 2 + 1);
183 }
184
185 gr_color(255, 255, 0, 255);
186
187 for (; i < text_rows; ++i) {
188 draw_text_line(i, text[(i + text_top) % text_rows]);
189 }
190 }
191 }
192
193 // Redraw everything on the screen and flip the screen (make it visible).
194 // Should only be called with gUpdateMutex locked.
update_screen_locked(void)195 static void update_screen_locked(void)
196 {
197 draw_screen_locked();
198 gr_flip();
199 }
200
201 // Updates only the progress bar, if possible, otherwise redraws the screen.
202 // Should only be called with gUpdateMutex locked.
update_progress_locked(void)203 static void update_progress_locked(void)
204 {
205 if (show_text || !gPagesIdentical) {
206 draw_screen_locked(); // Must redraw the whole screen
207 gPagesIdentical = 1;
208 } else {
209 draw_progress_locked(); // Draw only the progress bar
210 }
211 gr_flip();
212 }
213
214 // Keeps the progress bar updated, even when the process is otherwise busy.
progress_thread(void * cookie)215 static void *progress_thread(void *cookie)
216 {
217 for (;;) {
218 usleep(1000000 / PROGRESSBAR_INDETERMINATE_FPS);
219 pthread_mutex_lock(&gUpdateMutex);
220
221 // update the progress bar animation, if active
222 // skip this if we have a text overlay (too expensive to update)
223 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) {
224 update_progress_locked();
225 }
226
227 // move the progress bar forward on timed intervals, if configured
228 int duration = gProgressScopeDuration;
229 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) {
230 int elapsed = time(NULL) - gProgressScopeTime;
231 float progress = 1.0 * elapsed / duration;
232 if (progress > 1.0) progress = 1.0;
233 if (progress > gProgress) {
234 gProgress = progress;
235 update_progress_locked();
236 }
237 }
238
239 pthread_mutex_unlock(&gUpdateMutex);
240 }
241 return NULL;
242 }
243
244 static int rel_sum = 0;
245
input_callback(int fd,short revents,void * data)246 static int input_callback(int fd, short revents, void *data)
247 {
248 struct input_event ev;
249 int ret;
250 int fake_key = 0;
251
252 ret = ev_get_input(fd, revents, &ev);
253 if (ret)
254 return -1;
255
256 if (ev.type == EV_SYN) {
257 return 0;
258 } else if (ev.type == EV_REL) {
259 if (ev.code == REL_Y) {
260 // accumulate the up or down motion reported by
261 // the trackball. When it exceeds a threshold
262 // (positive or negative), fake an up/down
263 // key event.
264 rel_sum += ev.value;
265 if (rel_sum > 3) {
266 fake_key = 1;
267 ev.type = EV_KEY;
268 ev.code = KEY_DOWN;
269 ev.value = 1;
270 rel_sum = 0;
271 } else if (rel_sum < -3) {
272 fake_key = 1;
273 ev.type = EV_KEY;
274 ev.code = KEY_UP;
275 ev.value = 1;
276 rel_sum = 0;
277 }
278 }
279 } else {
280 rel_sum = 0;
281 }
282
283 if (ev.type != EV_KEY || ev.code > KEY_MAX)
284 return 0;
285
286 pthread_mutex_lock(&key_queue_mutex);
287 if (!fake_key) {
288 // our "fake" keys only report a key-down event (no
289 // key-up), so don't record them in the key_pressed
290 // table.
291 key_pressed[ev.code] = ev.value;
292 }
293 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
294 if (ev.value > 0 && key_queue_len < queue_max) {
295 key_queue[key_queue_len++] = ev.code;
296 pthread_cond_signal(&key_queue_cond);
297 }
298 pthread_mutex_unlock(&key_queue_mutex);
299
300 if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) {
301 pthread_mutex_lock(&gUpdateMutex);
302 show_text = !show_text;
303 update_screen_locked();
304 pthread_mutex_unlock(&gUpdateMutex);
305 }
306
307 if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) {
308 reboot(RB_AUTOBOOT);
309 }
310
311 return 0;
312 }
313
314 // Reads input events, handles special hot keys, and adds to the key queue.
input_thread(void * cookie)315 static void *input_thread(void *cookie)
316 {
317 for (;;) {
318 if (!ev_wait(-1))
319 ev_dispatch();
320 }
321 return NULL;
322 }
323
ui_init(void)324 void ui_init(void)
325 {
326 int ret = 0;
327 ret = gr_init();
328 if (ret)
329 return;
330
331 ev_init((ev_callback)input_callback, NULL);
332
333 text_col = text_row = 0;
334 text_rows = gr_fb_height() / CHAR_HEIGHT;
335 if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
336 text_top = 1;
337
338 text_cols = gr_fb_width() / CHAR_WIDTH;
339 if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
340
341 int i;
342 for (i = 0; BITMAPS[i].name != NULL; ++i) {
343 int result = res_create_display_surface(BITMAPS[i].name, BITMAPS[i].surface);
344 if (result < 0) {
345 if (result == -2) {
346 LOGI("Bitmap %s missing header\n", BITMAPS[i].name);
347 } else {
348 LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result);
349 }
350 *BITMAPS[i].surface = NULL;
351 }
352 }
353
354 pthread_t t;
355 pthread_create(&t, NULL, progress_thread, NULL);
356 pthread_create(&t, NULL, input_thread, NULL);
357 }
358
ui_set_background(int icon)359 void ui_set_background(int icon)
360 {
361 if (!gr_draw)
362 return;
363
364 pthread_mutex_lock(&gUpdateMutex);
365 gCurrentIcon = gBackgroundIcon[icon];
366 update_screen_locked();
367 pthread_mutex_unlock(&gUpdateMutex);
368 }
369
ui_show_indeterminate_progress()370 void ui_show_indeterminate_progress()
371 {
372 if (!gr_draw)
373 return;
374
375 pthread_mutex_lock(&gUpdateMutex);
376 if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
377 gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
378 update_progress_locked();
379 }
380 pthread_mutex_unlock(&gUpdateMutex);
381 }
382
ui_show_progress(float portion,int seconds)383 void ui_show_progress(float portion, int seconds)
384 {
385 if (!gr_draw)
386 return;
387
388 pthread_mutex_lock(&gUpdateMutex);
389 gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
390 gProgressScopeStart += gProgressScopeSize;
391 gProgressScopeSize = portion;
392 gProgressScopeTime = time(NULL);
393 gProgressScopeDuration = seconds;
394 gProgress = 0;
395 update_progress_locked();
396 pthread_mutex_unlock(&gUpdateMutex);
397 }
398
ui_set_progress(float fraction)399 void ui_set_progress(float fraction)
400 {
401 if (!gr_draw)
402 return;
403
404 pthread_mutex_lock(&gUpdateMutex);
405 if (fraction < 0.0) fraction = 0.0;
406 if (fraction > 1.0) fraction = 1.0;
407 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
408 // Skip updates that aren't visibly different.
409 int width = gr_get_width(gProgressBarIndeterminate[0]);
410 float scale = width * gProgressScopeSize;
411 if ((int) (gProgress * scale) != (int) (fraction * scale)) {
412 gProgress = fraction;
413 update_progress_locked();
414 }
415 }
416 pthread_mutex_unlock(&gUpdateMutex);
417 }
418
ui_reset_progress()419 void ui_reset_progress()
420 {
421 if (!gr_draw)
422 return;
423 pthread_mutex_lock(&gUpdateMutex);
424 gProgressBarType = PROGRESSBAR_TYPE_NONE;
425 gProgressScopeStart = gProgressScopeSize = 0;
426 gProgressScopeTime = gProgressScopeDuration = 0;
427 gProgress = 0;
428 update_screen_locked();
429 pthread_mutex_unlock(&gUpdateMutex);
430 }
431
ui_print(const char * fmt,...)432 void ui_print(const char *fmt, ...)
433 {
434 char buf[256];
435 va_list ap;
436 va_start(ap, fmt);
437 vsnprintf(buf, 256, fmt, ap);
438 va_end(ap);
439
440 //fputs(buf, stdout);
441
442 if (!gr_draw)
443 return;
444
445 // This can get called before ui_init(), so be careful.
446 pthread_mutex_lock(&gUpdateMutex);
447 if (text_rows > 0 && text_cols > 0) {
448 char *ptr;
449 for (ptr = buf; *ptr != '\0'; ++ptr) {
450 if (*ptr == '\n' || text_col >= text_cols) {
451 text[text_row][text_col] = '\0';
452 text_col = 0;
453 text_row = (text_row + 1) % text_rows;
454 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
455 }
456 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
457 }
458 text[text_row][text_col] = '\0';
459 update_screen_locked();
460 }
461 pthread_mutex_unlock(&gUpdateMutex);
462 }
463
ui_start_menu(char ** headers,char ** items,int initial_selection)464 void ui_start_menu(char** headers, char** items, int initial_selection)
465 {
466 int i;
467
468 if (!gr_draw)
469 return;
470
471 pthread_mutex_lock(&gUpdateMutex);
472 if (text_rows > 0 && text_cols > 0) {
473 for (i = 0; i < text_rows; ++i) {
474 if (headers[i] == NULL) break;
475 strncpy(menu[i], headers[i], text_cols - 1);
476 menu[i][text_cols - 1] = '\0';
477 }
478 menu_top = i;
479 for (; i < text_rows; ++i) {
480 if (items[i - menu_top] == NULL) break;
481 strncpy(menu[i], items[i - menu_top], text_cols - 1);
482 menu[i][text_cols - 1] = '\0';
483 }
484 menu_items = i - menu_top;
485 show_menu = 1;
486 menu_sel = initial_selection;
487 update_screen_locked();
488 }
489 pthread_mutex_unlock(&gUpdateMutex);
490 }
491
ui_menu_select(int sel)492 int ui_menu_select(int sel)
493 {
494 int old_sel;
495
496 if (!gr_draw)
497 return 0;
498
499 pthread_mutex_lock(&gUpdateMutex);
500 if (show_menu > 0) {
501 old_sel = menu_sel;
502 menu_sel = sel;
503 if (menu_sel < 0) menu_sel = 0;
504 if (menu_sel >= menu_items) menu_sel = menu_items - 1;
505 sel = menu_sel;
506 if (menu_sel != old_sel) update_screen_locked();
507 }
508 pthread_mutex_unlock(&gUpdateMutex);
509 return sel;
510 }
511
ui_end_menu()512 void ui_end_menu()
513 {
514 int i;
515 if (!gr_draw)
516 return;
517
518 pthread_mutex_lock(&gUpdateMutex);
519 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
520 show_menu = 0;
521 update_screen_locked();
522 }
523 pthread_mutex_unlock(&gUpdateMutex);
524 }
525
ui_text_visible()526 int ui_text_visible()
527 {
528 if (!gr_draw)
529 return 0;
530
531 pthread_mutex_lock(&gUpdateMutex);
532 int visible = show_text;
533 pthread_mutex_unlock(&gUpdateMutex);
534 return visible;
535 }
536
ui_show_text(int visible)537 void ui_show_text(int visible)
538 {
539 if (!gr_draw)
540 return;
541
542 pthread_mutex_lock(&gUpdateMutex);
543 show_text = visible;
544 update_screen_locked();
545 pthread_mutex_unlock(&gUpdateMutex);
546 }
547
ui_wait_key()548 int ui_wait_key()
549 {
550 if (!gr_draw)
551 return 0;
552
553 pthread_mutex_lock(&key_queue_mutex);
554 while (key_queue_len == 0) {
555 pthread_cond_wait(&key_queue_cond, &key_queue_mutex);
556 }
557
558 int key = key_queue[0];
559 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
560 pthread_mutex_unlock(&key_queue_mutex);
561 return key;
562 }
563
ui_key_pressed(int key)564 int ui_key_pressed(int key)
565 {
566 // This is a volatile static array, don't bother locking
567 return key_pressed[key];
568 }
569
ui_clear_key_queue()570 void ui_clear_key_queue()
571 {
572 pthread_mutex_lock(&key_queue_mutex);
573 key_queue_len = 0;
574 pthread_mutex_unlock(&key_queue_mutex);
575 }
576