blob: fdc09911b4ec8350370fef0ea29efe01b99a5606 [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
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER;
42static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS];
43static gr_surface gProgressBarIndeterminate[PROGRESSBAR_INDETERMINATE_STATES];
Doug Zongkerd93a2542009-10-08 16:32:58 -070044static gr_surface gProgressBarEmpty;
45static gr_surface gProgressBarFill;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080046
47static const struct { gr_surface* surface; const char *name; } BITMAPS[] = {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048 { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" },
49 { &gBackgroundIcon[BACKGROUND_ICON_ERROR], "icon_error" },
50 { &gBackgroundIcon[BACKGROUND_ICON_FIRMWARE_INSTALLING],
51 "icon_firmware_install" },
52 { &gBackgroundIcon[BACKGROUND_ICON_FIRMWARE_ERROR],
53 "icon_firmware_error" },
54 { &gProgressBarIndeterminate[0], "indeterminate1" },
55 { &gProgressBarIndeterminate[1], "indeterminate2" },
56 { &gProgressBarIndeterminate[2], "indeterminate3" },
57 { &gProgressBarIndeterminate[3], "indeterminate4" },
58 { &gProgressBarIndeterminate[4], "indeterminate5" },
59 { &gProgressBarIndeterminate[5], "indeterminate6" },
Doug Zongkerd93a2542009-10-08 16:32:58 -070060 { &gProgressBarEmpty, "progress_empty" },
61 { &gProgressBarFill, "progress_fill" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080062 { NULL, NULL },
63};
64
65static gr_surface gCurrentIcon = NULL;
66
67static enum ProgressBarType {
68 PROGRESSBAR_TYPE_NONE,
69 PROGRESSBAR_TYPE_INDETERMINATE,
70 PROGRESSBAR_TYPE_NORMAL,
71} gProgressBarType = PROGRESSBAR_TYPE_NONE;
72
73// Progress bar scope of current operation
74static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
75static time_t gProgressScopeTime, gProgressScopeDuration;
76
77// Set to 1 when both graphics pages are the same (except for the progress bar)
78static int gPagesIdentical = 0;
79
80// Log text overlay, displayed when a magic key is pressed
81static char text[MAX_ROWS][MAX_COLS];
82static int text_cols = 0, text_rows = 0;
83static int text_col = 0, text_row = 0, text_top = 0;
84static int show_text = 0;
85
86static char menu[MAX_ROWS][MAX_COLS];
87static int show_menu = 0;
88static int menu_top = 0, menu_items = 0, menu_sel = 0;
89
90// Key event input queue
91static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
92static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER;
93static int key_queue[256], key_queue_len = 0;
94static volatile char key_pressed[KEY_MAX + 1];
95
96// Clear the screen and draw the currently selected background icon (if any).
97// Should only be called with gUpdateMutex locked.
98static void draw_background_locked(gr_surface icon)
99{
100 gPagesIdentical = 0;
101 gr_color(0, 0, 0, 255);
102 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
103
104 if (icon) {
105 int iconWidth = gr_get_width(icon);
106 int iconHeight = gr_get_height(icon);
107 int iconX = (gr_fb_width() - iconWidth) / 2;
108 int iconY = (gr_fb_height() - iconHeight) / 2;
109 gr_blit(icon, 0, 0, iconWidth, iconHeight, iconX, iconY);
110 }
111}
112
113// Draw the progress bar (if any) on the screen. Does not flip pages.
114// Should only be called with gUpdateMutex locked.
115static void draw_progress_locked()
116{
117 if (gProgressBarType == PROGRESSBAR_TYPE_NONE) return;
118
119 int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]);
Doug Zongkerd93a2542009-10-08 16:32:58 -0700120 int width = gr_get_width(gProgressBarEmpty);
121 int height = gr_get_height(gProgressBarEmpty);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800122
123 int dx = (gr_fb_width() - width)/2;
124 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
125
126 // Erase behind the progress bar (in case this was a progress-only update)
127 gr_color(0, 0, 0, 255);
128 gr_fill(dx, dy, width, height);
129
130 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) {
131 float progress = gProgressScopeStart + gProgress * gProgressScopeSize;
132 int pos = (int) (progress * width);
133
Doug Zongkerd93a2542009-10-08 16:32:58 -0700134 if (pos > 0) {
135 gr_blit(gProgressBarFill, 0, 0, pos, height, dx, dy);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800136 }
Doug Zongkerd93a2542009-10-08 16:32:58 -0700137 if (pos < width-1) {
138 gr_blit(gProgressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
139 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800140 }
141
142 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) {
143 static int frame = 0;
144 gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
145 frame = (frame + 1) % PROGRESSBAR_INDETERMINATE_STATES;
146 }
147}
148
149static void draw_text_line(int row, const char* t) {
150 if (t[0] != '\0') {
151 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
152 }
153}
154
155// Redraw everything on the screen. Does not flip pages.
156// Should only be called with gUpdateMutex locked.
157static void draw_screen_locked(void)
158{
159 draw_background_locked(gCurrentIcon);
160 draw_progress_locked();
161
162 if (show_text) {
163 gr_color(0, 0, 0, 160);
164 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
165
166 int i = 0;
167 if (show_menu) {
168 gr_color(64, 96, 255, 255);
169 gr_fill(0, (menu_top+menu_sel) * CHAR_HEIGHT,
170 gr_fb_width(), (menu_top+menu_sel+1)*CHAR_HEIGHT+1);
171
172 for (; i < menu_top + menu_items; ++i) {
173 if (i == menu_top + menu_sel) {
174 gr_color(255, 255, 255, 255);
175 draw_text_line(i, menu[i]);
176 gr_color(64, 96, 255, 255);
177 } else {
178 draw_text_line(i, menu[i]);
179 }
180 }
181 gr_fill(0, i*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
182 gr_fb_width(), i*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
183 ++i;
184 }
185
186 gr_color(255, 255, 0, 255);
187
188 for (; i < text_rows; ++i) {
189 draw_text_line(i, text[(i+text_top) % text_rows]);
190 }
191 }
192}
193
194// Redraw everything on the screen and flip the screen (make it visible).
195// Should only be called with gUpdateMutex locked.
196static void update_screen_locked(void)
197{
198 draw_screen_locked();
199 gr_flip();
200}
201
202// Updates only the progress bar, if possible, otherwise redraws the screen.
203// Should only be called with gUpdateMutex locked.
204static void update_progress_locked(void)
205{
206 if (show_text || !gPagesIdentical) {
207 draw_screen_locked(); // Must redraw the whole screen
208 gPagesIdentical = 1;
209 } else {
210 draw_progress_locked(); // Draw only the progress bar
211 }
212 gr_flip();
213}
214
215// Keeps the progress bar updated, even when the process is otherwise busy.
216static void *progress_thread(void *cookie)
217{
218 for (;;) {
219 usleep(1000000 / PROGRESSBAR_INDETERMINATE_FPS);
220 pthread_mutex_lock(&gUpdateMutex);
221
222 // update the progress bar animation, if active
223 // skip this if we have a text overlay (too expensive to update)
224 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) {
225 update_progress_locked();
226 }
227
228 // move the progress bar forward on timed intervals, if configured
229 int duration = gProgressScopeDuration;
230 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) {
231 int elapsed = time(NULL) - gProgressScopeTime;
232 float progress = 1.0 * elapsed / duration;
233 if (progress > 1.0) progress = 1.0;
234 if (progress > gProgress) {
235 gProgress = progress;
236 update_progress_locked();
237 }
238 }
239
240 pthread_mutex_unlock(&gUpdateMutex);
241 }
242 return NULL;
243}
244
245// Reads input events, handles special hot keys, and adds to the key queue.
246static void *input_thread(void *cookie)
247{
248 int rel_sum = 0;
249 int fake_key = 0;
250 for (;;) {
251 // wait for the next key event
252 struct input_event ev;
253 do {
254 ev_get(&ev, 0);
255
256 if (ev.type == EV_SYN) {
257 continue;
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 } while (ev.type != EV_KEY || ev.code > KEY_MAX);
283
284 pthread_mutex_lock(&key_queue_mutex);
285 if (!fake_key) {
286 // our "fake" keys only report a key-down event (no
287 // key-up), so don't record them in the key_pressed
288 // table.
289 key_pressed[ev.code] = ev.value;
290 }
291 fake_key = 0;
292 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
293 if (ev.value > 0 && key_queue_len < queue_max) {
294 key_queue[key_queue_len++] = ev.code;
295 pthread_cond_signal(&key_queue_cond);
296 }
297 pthread_mutex_unlock(&key_queue_mutex);
298
Doug Zongkerddd6a282009-06-09 12:22:33 -0700299 if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800300 pthread_mutex_lock(&gUpdateMutex);
301 show_text = !show_text;
302 update_screen_locked();
303 pthread_mutex_unlock(&gUpdateMutex);
304 }
305
Doug Zongkerddd6a282009-06-09 12:22:33 -0700306 if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800307 reboot(RB_AUTOBOOT);
308 }
309 }
310 return NULL;
311}
312
313void ui_init(void)
314{
315 gr_init();
316 ev_init();
317
318 text_col = text_row = 0;
319 text_rows = gr_fb_height() / CHAR_HEIGHT;
320 if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
321 text_top = 1;
322
323 text_cols = gr_fb_width() / CHAR_WIDTH;
324 if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
325
326 int i;
327 for (i = 0; BITMAPS[i].name != NULL; ++i) {
328 int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface);
329 if (result < 0) {
Doug Zongker196c25c2009-09-15 08:50:04 -0700330 if (result == -2) {
331 LOGI("Bitmap %s missing header\n", BITMAPS[i].name);
332 } else {
333 LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result);
334 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800335 *BITMAPS[i].surface = NULL;
336 }
337 }
338
339 pthread_t t;
340 pthread_create(&t, NULL, progress_thread, NULL);
341 pthread_create(&t, NULL, input_thread, NULL);
342}
343
344char *ui_copy_image(int icon, int *width, int *height, int *bpp) {
345 pthread_mutex_lock(&gUpdateMutex);
346 draw_background_locked(gBackgroundIcon[icon]);
347 *width = gr_fb_width();
348 *height = gr_fb_height();
349 *bpp = sizeof(gr_pixel) * 8;
350 int size = *width * *height * sizeof(gr_pixel);
351 char *ret = malloc(size);
352 if (ret == NULL) {
353 LOGE("Can't allocate %d bytes for image\n", size);
354 } else {
355 memcpy(ret, gr_fb_data(), size);
356 }
357 pthread_mutex_unlock(&gUpdateMutex);
358 return ret;
359}
360
361void ui_set_background(int icon)
362{
363 pthread_mutex_lock(&gUpdateMutex);
364 gCurrentIcon = gBackgroundIcon[icon];
365 update_screen_locked();
366 pthread_mutex_unlock(&gUpdateMutex);
367}
368
369void ui_show_indeterminate_progress()
370{
371 pthread_mutex_lock(&gUpdateMutex);
372 if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
373 gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
374 update_progress_locked();
375 }
376 pthread_mutex_unlock(&gUpdateMutex);
377}
378
379void ui_show_progress(float portion, int seconds)
380{
381 pthread_mutex_lock(&gUpdateMutex);
382 gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
383 gProgressScopeStart += gProgressScopeSize;
384 gProgressScopeSize = portion;
385 gProgressScopeTime = time(NULL);
386 gProgressScopeDuration = seconds;
387 gProgress = 0;
388 update_progress_locked();
389 pthread_mutex_unlock(&gUpdateMutex);
390}
391
392void ui_set_progress(float fraction)
393{
394 pthread_mutex_lock(&gUpdateMutex);
395 if (fraction < 0.0) fraction = 0.0;
396 if (fraction > 1.0) fraction = 1.0;
397 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
398 // Skip updates that aren't visibly different.
399 int width = gr_get_width(gProgressBarIndeterminate[0]);
400 float scale = width * gProgressScopeSize;
401 if ((int) (gProgress * scale) != (int) (fraction * scale)) {
402 gProgress = fraction;
403 update_progress_locked();
404 }
405 }
406 pthread_mutex_unlock(&gUpdateMutex);
407}
408
409void ui_reset_progress()
410{
411 pthread_mutex_lock(&gUpdateMutex);
412 gProgressBarType = PROGRESSBAR_TYPE_NONE;
413 gProgressScopeStart = gProgressScopeSize = 0;
414 gProgressScopeTime = gProgressScopeDuration = 0;
415 gProgress = 0;
416 update_screen_locked();
417 pthread_mutex_unlock(&gUpdateMutex);
418}
419
420void ui_print(const char *fmt, ...)
421{
422 char buf[256];
423 va_list ap;
424 va_start(ap, fmt);
425 vsnprintf(buf, 256, fmt, ap);
426 va_end(ap);
427
428 fputs(buf, stderr);
429
430 // This can get called before ui_init(), so be careful.
431 pthread_mutex_lock(&gUpdateMutex);
432 if (text_rows > 0 && text_cols > 0) {
433 char *ptr;
434 for (ptr = buf; *ptr != '\0'; ++ptr) {
435 if (*ptr == '\n' || text_col >= text_cols) {
436 text[text_row][text_col] = '\0';
437 text_col = 0;
438 text_row = (text_row + 1) % text_rows;
439 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
440 }
441 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
442 }
443 text[text_row][text_col] = '\0';
444 update_screen_locked();
445 }
446 pthread_mutex_unlock(&gUpdateMutex);
447}
448
449void ui_start_menu(char** headers, char** items) {
450 int i;
451 pthread_mutex_lock(&gUpdateMutex);
452 if (text_rows > 0 && text_cols > 0) {
453 for (i = 0; i < text_rows; ++i) {
454 if (headers[i] == NULL) break;
455 strncpy(menu[i], headers[i], text_cols-1);
456 menu[i][text_cols-1] = '\0';
457 }
458 menu_top = i;
459 for (; i < text_rows; ++i) {
460 if (items[i-menu_top] == NULL) break;
461 strncpy(menu[i], items[i-menu_top], text_cols-1);
462 menu[i][text_cols-1] = '\0';
463 }
464 menu_items = i - menu_top;
465 show_menu = 1;
466 menu_sel = 0;
467 update_screen_locked();
468 }
469 pthread_mutex_unlock(&gUpdateMutex);
470}
471
472int ui_menu_select(int sel) {
473 int old_sel;
474 pthread_mutex_lock(&gUpdateMutex);
475 if (show_menu > 0) {
476 old_sel = menu_sel;
477 menu_sel = sel;
478 if (menu_sel < 0) menu_sel = 0;
479 if (menu_sel >= menu_items) menu_sel = menu_items-1;
480 sel = menu_sel;
481 if (menu_sel != old_sel) update_screen_locked();
482 }
483 pthread_mutex_unlock(&gUpdateMutex);
484 return sel;
485}
486
487void ui_end_menu() {
488 int i;
489 pthread_mutex_lock(&gUpdateMutex);
490 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
491 show_menu = 0;
492 update_screen_locked();
493 }
494 pthread_mutex_unlock(&gUpdateMutex);
495}
496
497int ui_text_visible()
498{
499 pthread_mutex_lock(&gUpdateMutex);
500 int visible = show_text;
501 pthread_mutex_unlock(&gUpdateMutex);
502 return visible;
503}
504
505int ui_wait_key()
506{
507 pthread_mutex_lock(&key_queue_mutex);
508 while (key_queue_len == 0) {
509 pthread_cond_wait(&key_queue_cond, &key_queue_mutex);
510 }
511
512 int key = key_queue[0];
513 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
514 pthread_mutex_unlock(&key_queue_mutex);
515 return key;
516}
517
518int ui_key_pressed(int key)
519{
520 // This is a volatile static array, don't bother locking
521 return key_pressed[key];
522}
523
524void ui_clear_key_queue() {
525 pthread_mutex_lock(&key_queue_mutex);
526 key_queue_len = 0;
527 pthread_mutex_unlock(&key_queue_mutex);
528}