blob: 5d1bf67103da7c9bc486eeba8d93478b8dbf3bdf [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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"
Doug Zongker23412e62009-07-23 10:16:07 -070030#include "recovery_ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080031
32#define MAX_COLS 64
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
41enum { LEFT_SIDE, CENTER_TILE, RIGHT_SIDE, NUM_SIDES };
42
43static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER;
44static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS];
45static gr_surface gProgressBarIndeterminate[PROGRESSBAR_INDETERMINATE_STATES];
46static gr_surface gProgressBarEmpty[NUM_SIDES];
47static gr_surface gProgressBarFill[NUM_SIDES];
48
49static const struct { gr_surface* surface; const char *name; } BITMAPS[] = {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050 { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" },
51 { &gBackgroundIcon[BACKGROUND_ICON_ERROR], "icon_error" },
52 { &gBackgroundIcon[BACKGROUND_ICON_FIRMWARE_INSTALLING],
53 "icon_firmware_install" },
54 { &gBackgroundIcon[BACKGROUND_ICON_FIRMWARE_ERROR],
55 "icon_firmware_error" },
56 { &gProgressBarIndeterminate[0], "indeterminate1" },
57 { &gProgressBarIndeterminate[1], "indeterminate2" },
58 { &gProgressBarIndeterminate[2], "indeterminate3" },
59 { &gProgressBarIndeterminate[3], "indeterminate4" },
60 { &gProgressBarIndeterminate[4], "indeterminate5" },
61 { &gProgressBarIndeterminate[5], "indeterminate6" },
62 { &gProgressBarEmpty[LEFT_SIDE], "progress_bar_empty_left_round" },
63 { &gProgressBarEmpty[CENTER_TILE], "progress_bar_empty" },
64 { &gProgressBarEmpty[RIGHT_SIDE], "progress_bar_empty_right_round" },
65 { &gProgressBarFill[LEFT_SIDE], "progress_bar_left_round" },
66 { &gProgressBarFill[CENTER_TILE], "progress_bar_fill" },
67 { &gProgressBarFill[RIGHT_SIDE], "progress_bar_right_round" },
68 { NULL, NULL },
69};
70
71static gr_surface gCurrentIcon = NULL;
72
73static enum ProgressBarType {
74 PROGRESSBAR_TYPE_NONE,
75 PROGRESSBAR_TYPE_INDETERMINATE,
76 PROGRESSBAR_TYPE_NORMAL,
77} gProgressBarType = PROGRESSBAR_TYPE_NONE;
78
79// Progress bar scope of current operation
80static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
81static time_t gProgressScopeTime, gProgressScopeDuration;
82
83// Set to 1 when both graphics pages are the same (except for the progress bar)
84static int gPagesIdentical = 0;
85
86// Log text overlay, displayed when a magic key is pressed
87static char text[MAX_ROWS][MAX_COLS];
88static int text_cols = 0, text_rows = 0;
89static int text_col = 0, text_row = 0, text_top = 0;
90static int show_text = 0;
91
92static char menu[MAX_ROWS][MAX_COLS];
93static int show_menu = 0;
94static int menu_top = 0, menu_items = 0, menu_sel = 0;
95
96// Key event input queue
97static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
98static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER;
99static int key_queue[256], key_queue_len = 0;
100static volatile char key_pressed[KEY_MAX + 1];
101
102// Clear the screen and draw the currently selected background icon (if any).
103// Should only be called with gUpdateMutex locked.
104static void draw_background_locked(gr_surface icon)
105{
106 gPagesIdentical = 0;
107 gr_color(0, 0, 0, 255);
108 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
109
110 if (icon) {
111 int iconWidth = gr_get_width(icon);
112 int iconHeight = gr_get_height(icon);
113 int iconX = (gr_fb_width() - iconWidth) / 2;
114 int iconY = (gr_fb_height() - iconHeight) / 2;
115 gr_blit(icon, 0, 0, iconWidth, iconHeight, iconX, iconY);
116 }
117}
118
119// Draw the progress bar (if any) on the screen. Does not flip pages.
120// Should only be called with gUpdateMutex locked.
121static void draw_progress_locked()
122{
123 if (gProgressBarType == PROGRESSBAR_TYPE_NONE) return;
124
125 int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]);
126 int width = gr_get_width(gProgressBarIndeterminate[0]);
127 int height = gr_get_height(gProgressBarIndeterminate[0]);
128
129 int dx = (gr_fb_width() - width)/2;
130 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
131
132 // Erase behind the progress bar (in case this was a progress-only update)
133 gr_color(0, 0, 0, 255);
134 gr_fill(dx, dy, width, height);
135
136 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) {
137 float progress = gProgressScopeStart + gProgress * gProgressScopeSize;
138 int pos = (int) (progress * width);
139
140 gr_surface s = (pos ? gProgressBarFill : gProgressBarEmpty)[LEFT_SIDE];
141 gr_blit(s, 0, 0, gr_get_width(s), gr_get_height(s), dx, dy);
142
143 int x = gr_get_width(s);
144 while (x + (int) gr_get_width(gProgressBarEmpty[RIGHT_SIDE]) < width) {
145 s = (pos > x ? gProgressBarFill : gProgressBarEmpty)[CENTER_TILE];
146 gr_blit(s, 0, 0, gr_get_width(s), gr_get_height(s), dx + x, dy);
147 x += gr_get_width(s);
148 }
149
150 s = (pos > x ? gProgressBarFill : gProgressBarEmpty)[RIGHT_SIDE];
151 gr_blit(s, 0, 0, gr_get_width(s), gr_get_height(s), dx + x, dy);
152 }
153
154 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) {
155 static int frame = 0;
156 gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
157 frame = (frame + 1) % PROGRESSBAR_INDETERMINATE_STATES;
158 }
159}
160
161static void draw_text_line(int row, const char* t) {
162 if (t[0] != '\0') {
163 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
164 }
165}
166
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800167#define MENU_TEXT_COLOR 7, 133, 74, 255
168#define NORMAL_TEXT_COLOR 200, 200, 200, 255
169#define HEADER_TEXT_COLOR NORMAL_TEXT_COLOR
170
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800171// Redraw everything on the screen. Does not flip pages.
172// Should only be called with gUpdateMutex locked.
173static void draw_screen_locked(void)
174{
175 draw_background_locked(gCurrentIcon);
176 draw_progress_locked();
177
178 if (show_text) {
179 gr_color(0, 0, 0, 160);
180 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
181
182 int i = 0;
183 if (show_menu) {
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800184 gr_color(MENU_TEXT_COLOR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800185 gr_fill(0, (menu_top+menu_sel) * CHAR_HEIGHT,
186 gr_fb_width(), (menu_top+menu_sel+1)*CHAR_HEIGHT+1);
187
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800188 gr_color(HEADER_TEXT_COLOR);
189 int is_drawing_header = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800190 for (; i < menu_top + menu_items; ++i) {
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800191 if (is_drawing_header && strcmp(menu[i], "") == 0) {
192 gr_color(MENU_TEXT_COLOR);
193 is_drawing_header = 0;
194 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800195 if (i == menu_top + menu_sel) {
196 gr_color(255, 255, 255, 255);
197 draw_text_line(i, menu[i]);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800198 gr_color(MENU_TEXT_COLOR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800199 } else {
200 draw_text_line(i, menu[i]);
201 }
202 }
203 gr_fill(0, i*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
204 gr_fb_width(), i*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
205 ++i;
206 }
207
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800208 gr_color(NORMAL_TEXT_COLOR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800209
210 for (; i < text_rows; ++i) {
211 draw_text_line(i, text[(i+text_top) % text_rows]);
212 }
213 }
214}
215
216// Redraw everything on the screen and flip the screen (make it visible).
217// Should only be called with gUpdateMutex locked.
218static void update_screen_locked(void)
219{
220 draw_screen_locked();
221 gr_flip();
222}
223
224// Updates only the progress bar, if possible, otherwise redraws the screen.
225// Should only be called with gUpdateMutex locked.
226static void update_progress_locked(void)
227{
228 if (show_text || !gPagesIdentical) {
229 draw_screen_locked(); // Must redraw the whole screen
230 gPagesIdentical = 1;
231 } else {
232 draw_progress_locked(); // Draw only the progress bar
233 }
234 gr_flip();
235}
236
237// Keeps the progress bar updated, even when the process is otherwise busy.
238static void *progress_thread(void *cookie)
239{
240 for (;;) {
241 usleep(1000000 / PROGRESSBAR_INDETERMINATE_FPS);
242 pthread_mutex_lock(&gUpdateMutex);
243
244 // update the progress bar animation, if active
245 // skip this if we have a text overlay (too expensive to update)
246 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) {
247 update_progress_locked();
248 }
249
250 // move the progress bar forward on timed intervals, if configured
251 int duration = gProgressScopeDuration;
252 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) {
253 int elapsed = time(NULL) - gProgressScopeTime;
254 float progress = 1.0 * elapsed / duration;
255 if (progress > 1.0) progress = 1.0;
256 if (progress > gProgress) {
257 gProgress = progress;
258 update_progress_locked();
259 }
260 }
261
262 pthread_mutex_unlock(&gUpdateMutex);
263 }
264 return NULL;
265}
266
267// Reads input events, handles special hot keys, and adds to the key queue.
268static void *input_thread(void *cookie)
269{
270 int rel_sum = 0;
271 int fake_key = 0;
272 for (;;) {
273 // wait for the next key event
274 struct input_event ev;
275 do {
276 ev_get(&ev, 0);
277
278 if (ev.type == EV_SYN) {
279 continue;
280 } else if (ev.type == EV_REL) {
281 if (ev.code == REL_Y) {
282 // accumulate the up or down motion reported by
283 // the trackball. When it exceeds a threshold
284 // (positive or negative), fake an up/down
285 // key event.
286 rel_sum += ev.value;
287 if (rel_sum > 3) {
288 fake_key = 1;
289 ev.type = EV_KEY;
290 ev.code = KEY_DOWN;
291 ev.value = 1;
292 rel_sum = 0;
293 } else if (rel_sum < -3) {
294 fake_key = 1;
295 ev.type = EV_KEY;
296 ev.code = KEY_UP;
297 ev.value = 1;
298 rel_sum = 0;
299 }
300 }
301 } else {
302 rel_sum = 0;
303 }
304 } while (ev.type != EV_KEY || ev.code > KEY_MAX);
305
306 pthread_mutex_lock(&key_queue_mutex);
307 if (!fake_key) {
308 // our "fake" keys only report a key-down event (no
309 // key-up), so don't record them in the key_pressed
310 // table.
311 key_pressed[ev.code] = ev.value;
312 }
313 fake_key = 0;
314 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
315 if (ev.value > 0 && key_queue_len < queue_max) {
316 key_queue[key_queue_len++] = ev.code;
317 pthread_cond_signal(&key_queue_cond);
318 }
319 pthread_mutex_unlock(&key_queue_mutex);
320
Doug Zongkerddd6a282009-06-09 12:22:33 -0700321 if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800322 pthread_mutex_lock(&gUpdateMutex);
323 show_text = !show_text;
324 update_screen_locked();
325 pthread_mutex_unlock(&gUpdateMutex);
326 }
327
Doug Zongkerddd6a282009-06-09 12:22:33 -0700328 if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800329 reboot(RB_AUTOBOOT);
330 }
331 }
332 return NULL;
333}
334
335void ui_init(void)
336{
337 gr_init();
338 ev_init();
339
340 text_col = text_row = 0;
341 text_rows = gr_fb_height() / CHAR_HEIGHT;
342 if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
343 text_top = 1;
344
345 text_cols = gr_fb_width() / CHAR_WIDTH;
346 if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
347
348 int i;
349 for (i = 0; BITMAPS[i].name != NULL; ++i) {
350 int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface);
351 if (result < 0) {
Doug Zongker196c25c2009-09-15 08:50:04 -0700352 if (result == -2) {
353 LOGI("Bitmap %s missing header\n", BITMAPS[i].name);
354 } else {
355 LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result);
356 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800357 *BITMAPS[i].surface = NULL;
358 }
359 }
360
361 pthread_t t;
362 pthread_create(&t, NULL, progress_thread, NULL);
363 pthread_create(&t, NULL, input_thread, NULL);
364}
365
366char *ui_copy_image(int icon, int *width, int *height, int *bpp) {
367 pthread_mutex_lock(&gUpdateMutex);
368 draw_background_locked(gBackgroundIcon[icon]);
369 *width = gr_fb_width();
370 *height = gr_fb_height();
371 *bpp = sizeof(gr_pixel) * 8;
372 int size = *width * *height * sizeof(gr_pixel);
373 char *ret = malloc(size);
374 if (ret == NULL) {
375 LOGE("Can't allocate %d bytes for image\n", size);
376 } else {
377 memcpy(ret, gr_fb_data(), size);
378 }
379 pthread_mutex_unlock(&gUpdateMutex);
380 return ret;
381}
382
383void ui_set_background(int icon)
384{
385 pthread_mutex_lock(&gUpdateMutex);
386 gCurrentIcon = gBackgroundIcon[icon];
387 update_screen_locked();
388 pthread_mutex_unlock(&gUpdateMutex);
389}
390
391void ui_show_indeterminate_progress()
392{
393 pthread_mutex_lock(&gUpdateMutex);
394 if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
395 gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
396 update_progress_locked();
397 }
398 pthread_mutex_unlock(&gUpdateMutex);
399}
400
401void ui_show_progress(float portion, int seconds)
402{
403 pthread_mutex_lock(&gUpdateMutex);
404 gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
405 gProgressScopeStart += gProgressScopeSize;
406 gProgressScopeSize = portion;
407 gProgressScopeTime = time(NULL);
408 gProgressScopeDuration = seconds;
409 gProgress = 0;
410 update_progress_locked();
411 pthread_mutex_unlock(&gUpdateMutex);
412}
413
414void ui_set_progress(float fraction)
415{
416 pthread_mutex_lock(&gUpdateMutex);
417 if (fraction < 0.0) fraction = 0.0;
418 if (fraction > 1.0) fraction = 1.0;
419 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
420 // Skip updates that aren't visibly different.
421 int width = gr_get_width(gProgressBarIndeterminate[0]);
422 float scale = width * gProgressScopeSize;
423 if ((int) (gProgress * scale) != (int) (fraction * scale)) {
424 gProgress = fraction;
425 update_progress_locked();
426 }
427 }
428 pthread_mutex_unlock(&gUpdateMutex);
429}
430
431void ui_reset_progress()
432{
433 pthread_mutex_lock(&gUpdateMutex);
434 gProgressBarType = PROGRESSBAR_TYPE_NONE;
435 gProgressScopeStart = gProgressScopeSize = 0;
436 gProgressScopeTime = gProgressScopeDuration = 0;
437 gProgress = 0;
438 update_screen_locked();
439 pthread_mutex_unlock(&gUpdateMutex);
440}
441
442void ui_print(const char *fmt, ...)
443{
444 char buf[256];
445 va_list ap;
446 va_start(ap, fmt);
447 vsnprintf(buf, 256, fmt, ap);
448 va_end(ap);
449
450 fputs(buf, stderr);
451
452 // This can get called before ui_init(), so be careful.
453 pthread_mutex_lock(&gUpdateMutex);
454 if (text_rows > 0 && text_cols > 0) {
455 char *ptr;
456 for (ptr = buf; *ptr != '\0'; ++ptr) {
457 if (*ptr == '\n' || text_col >= text_cols) {
458 text[text_row][text_col] = '\0';
459 text_col = 0;
460 text_row = (text_row + 1) % text_rows;
461 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
462 }
463 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
464 }
465 text[text_row][text_col] = '\0';
466 update_screen_locked();
467 }
468 pthread_mutex_unlock(&gUpdateMutex);
469}
470
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800471#define MENU_ITEM_HEADER " - "
472#define MENU_ITEM_HEADER_LENGTH strlen(MENU_ITEM_HEADER)
473
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800474void ui_start_menu(char** headers, char** items) {
475 int i;
476 pthread_mutex_lock(&gUpdateMutex);
477 if (text_rows > 0 && text_cols > 0) {
478 for (i = 0; i < text_rows; ++i) {
479 if (headers[i] == NULL) break;
480 strncpy(menu[i], headers[i], text_cols-1);
481 menu[i][text_cols-1] = '\0';
482 }
483 menu_top = i;
484 for (; i < text_rows; ++i) {
485 if (items[i-menu_top] == NULL) break;
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800486 strcpy(menu[i], MENU_ITEM_HEADER);
487 strncpy(menu[i] + MENU_ITEM_HEADER_LENGTH, items[i-menu_top], text_cols-1 - MENU_ITEM_HEADER_LENGTH);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800488 menu[i][text_cols-1] = '\0';
489 }
490 menu_items = i - menu_top;
491 show_menu = 1;
492 menu_sel = 0;
493 update_screen_locked();
494 }
495 pthread_mutex_unlock(&gUpdateMutex);
496}
497
498int ui_menu_select(int sel) {
499 int old_sel;
500 pthread_mutex_lock(&gUpdateMutex);
501 if (show_menu > 0) {
502 old_sel = menu_sel;
503 menu_sel = sel;
504 if (menu_sel < 0) menu_sel = 0;
505 if (menu_sel >= menu_items) menu_sel = menu_items-1;
506 sel = menu_sel;
507 if (menu_sel != old_sel) update_screen_locked();
508 }
509 pthread_mutex_unlock(&gUpdateMutex);
510 return sel;
511}
512
513void ui_end_menu() {
514 int i;
515 pthread_mutex_lock(&gUpdateMutex);
516 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
517 show_menu = 0;
518 update_screen_locked();
519 }
520 pthread_mutex_unlock(&gUpdateMutex);
521}
522
523int ui_text_visible()
524{
525 pthread_mutex_lock(&gUpdateMutex);
526 int visible = show_text;
527 pthread_mutex_unlock(&gUpdateMutex);
528 return visible;
529}
530
531int ui_wait_key()
532{
533 pthread_mutex_lock(&key_queue_mutex);
534 while (key_queue_len == 0) {
535 pthread_cond_wait(&key_queue_cond, &key_queue_mutex);
536 }
537
538 int key = key_queue[0];
539 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
540 pthread_mutex_unlock(&key_queue_mutex);
541 return key;
542}
543
544int ui_key_pressed(int key)
545{
546 // This is a volatile static array, don't bother locking
547 return key_pressed[key];
548}
549
550void ui_clear_key_queue() {
551 pthread_mutex_lock(&key_queue_mutex);
552 key_queue_len = 0;
553 pthread_mutex_unlock(&key_queue_mutex);
554}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800555
Koushik K. Dutta03173782010-02-26 14:14:23 -0800556void ui_set_show_text(int value) {
557 show_text = value;
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800558}