blob: 82004f031d42fc71a538497d303840c4c7c35c93 [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>
Doug Zongker5cae4452011-01-25 13:15:30 -080027#include <errno.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028
29#include "common.h"
30#include "minui/minui.h"
Doug Zongker23412e62009-07-23 10:16:07 -070031#include "recovery_ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032
Doug Zongkerf291d852010-07-07 13:55:25 -070033#define MAX_COLS 96
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034#define MAX_ROWS 32
35
36#define CHAR_WIDTH 10
37#define CHAR_HEIGHT 18
38
39#define PROGRESSBAR_INDETERMINATE_STATES 6
40#define PROGRESSBAR_INDETERMINATE_FPS 15
41
Doug Zongker5cae4452011-01-25 13:15:30 -080042#define UI_WAIT_KEY_TIMEOUT_SEC 120
43
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER;
45static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS];
46static gr_surface gProgressBarIndeterminate[PROGRESSBAR_INDETERMINATE_STATES];
Doug Zongkerd93a2542009-10-08 16:32:58 -070047static gr_surface gProgressBarEmpty;
48static gr_surface gProgressBarFill;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049
50static const struct { gr_surface* surface; const char *name; } BITMAPS[] = {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051 { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" },
52 { &gBackgroundIcon[BACKGROUND_ICON_ERROR], "icon_error" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080053 { &gProgressBarIndeterminate[0], "indeterminate1" },
54 { &gProgressBarIndeterminate[1], "indeterminate2" },
55 { &gProgressBarIndeterminate[2], "indeterminate3" },
56 { &gProgressBarIndeterminate[3], "indeterminate4" },
57 { &gProgressBarIndeterminate[4], "indeterminate5" },
58 { &gProgressBarIndeterminate[5], "indeterminate6" },
Doug Zongkerd93a2542009-10-08 16:32:58 -070059 { &gProgressBarEmpty, "progress_empty" },
60 { &gProgressBarFill, "progress_fill" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061 { NULL, NULL },
62};
63
64static gr_surface gCurrentIcon = NULL;
65
66static enum ProgressBarType {
67 PROGRESSBAR_TYPE_NONE,
68 PROGRESSBAR_TYPE_INDETERMINATE,
69 PROGRESSBAR_TYPE_NORMAL,
70} gProgressBarType = PROGRESSBAR_TYPE_NONE;
71
72// Progress bar scope of current operation
73static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
74static time_t gProgressScopeTime, gProgressScopeDuration;
75
76// Set to 1 when both graphics pages are the same (except for the progress bar)
77static int gPagesIdentical = 0;
78
79// Log text overlay, displayed when a magic key is pressed
80static char text[MAX_ROWS][MAX_COLS];
81static int text_cols = 0, text_rows = 0;
82static int text_col = 0, text_row = 0, text_top = 0;
83static int show_text = 0;
Doug Zongker5cae4452011-01-25 13:15:30 -080084static int show_text_ever = 0; // has show_text ever been 1?
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080085
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;
Doug Zongker5cae4452011-01-25 13:15:30 -0800302 if (show_text) show_text_ever = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800303 update_screen_locked();
304 pthread_mutex_unlock(&gUpdateMutex);
305 }
306
Doug Zongkerddd6a282009-06-09 12:22:33 -0700307 if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800308 reboot(RB_AUTOBOOT);
309 }
310 }
311 return NULL;
312}
313
314void ui_init(void)
315{
316 gr_init();
317 ev_init();
318
319 text_col = text_row = 0;
320 text_rows = gr_fb_height() / CHAR_HEIGHT;
321 if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
322 text_top = 1;
323
324 text_cols = gr_fb_width() / CHAR_WIDTH;
325 if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
326
327 int i;
328 for (i = 0; BITMAPS[i].name != NULL; ++i) {
329 int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface);
330 if (result < 0) {
Doug Zongker196c25c2009-09-15 08:50:04 -0700331 if (result == -2) {
332 LOGI("Bitmap %s missing header\n", BITMAPS[i].name);
333 } else {
334 LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result);
335 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800336 *BITMAPS[i].surface = NULL;
337 }
338 }
339
340 pthread_t t;
341 pthread_create(&t, NULL, progress_thread, NULL);
342 pthread_create(&t, NULL, input_thread, NULL);
343}
344
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800345void ui_set_background(int icon)
346{
347 pthread_mutex_lock(&gUpdateMutex);
348 gCurrentIcon = gBackgroundIcon[icon];
349 update_screen_locked();
350 pthread_mutex_unlock(&gUpdateMutex);
351}
352
353void ui_show_indeterminate_progress()
354{
355 pthread_mutex_lock(&gUpdateMutex);
356 if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
357 gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
358 update_progress_locked();
359 }
360 pthread_mutex_unlock(&gUpdateMutex);
361}
362
363void ui_show_progress(float portion, int seconds)
364{
365 pthread_mutex_lock(&gUpdateMutex);
366 gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
367 gProgressScopeStart += gProgressScopeSize;
368 gProgressScopeSize = portion;
369 gProgressScopeTime = time(NULL);
370 gProgressScopeDuration = seconds;
371 gProgress = 0;
372 update_progress_locked();
373 pthread_mutex_unlock(&gUpdateMutex);
374}
375
376void ui_set_progress(float fraction)
377{
378 pthread_mutex_lock(&gUpdateMutex);
379 if (fraction < 0.0) fraction = 0.0;
380 if (fraction > 1.0) fraction = 1.0;
381 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
382 // Skip updates that aren't visibly different.
383 int width = gr_get_width(gProgressBarIndeterminate[0]);
384 float scale = width * gProgressScopeSize;
385 if ((int) (gProgress * scale) != (int) (fraction * scale)) {
386 gProgress = fraction;
387 update_progress_locked();
388 }
389 }
390 pthread_mutex_unlock(&gUpdateMutex);
391}
392
393void ui_reset_progress()
394{
395 pthread_mutex_lock(&gUpdateMutex);
396 gProgressBarType = PROGRESSBAR_TYPE_NONE;
397 gProgressScopeStart = gProgressScopeSize = 0;
398 gProgressScopeTime = gProgressScopeDuration = 0;
399 gProgress = 0;
400 update_screen_locked();
401 pthread_mutex_unlock(&gUpdateMutex);
402}
403
404void ui_print(const char *fmt, ...)
405{
406 char buf[256];
407 va_list ap;
408 va_start(ap, fmt);
409 vsnprintf(buf, 256, fmt, ap);
410 va_end(ap);
411
Doug Zongker3d177d02010-07-01 09:18:44 -0700412 fputs(buf, stdout);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800413
414 // This can get called before ui_init(), so be careful.
415 pthread_mutex_lock(&gUpdateMutex);
416 if (text_rows > 0 && text_cols > 0) {
417 char *ptr;
418 for (ptr = buf; *ptr != '\0'; ++ptr) {
419 if (*ptr == '\n' || text_col >= text_cols) {
420 text[text_row][text_col] = '\0';
421 text_col = 0;
422 text_row = (text_row + 1) % text_rows;
423 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
424 }
425 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
426 }
427 text[text_row][text_col] = '\0';
428 update_screen_locked();
429 }
430 pthread_mutex_unlock(&gUpdateMutex);
431}
432
Doug Zongkerbe598882010-04-08 14:36:55 -0700433void ui_start_menu(char** headers, char** items, int initial_selection) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800434 int i;
435 pthread_mutex_lock(&gUpdateMutex);
436 if (text_rows > 0 && text_cols > 0) {
437 for (i = 0; i < text_rows; ++i) {
438 if (headers[i] == NULL) break;
439 strncpy(menu[i], headers[i], text_cols-1);
440 menu[i][text_cols-1] = '\0';
441 }
442 menu_top = i;
443 for (; i < text_rows; ++i) {
444 if (items[i-menu_top] == NULL) break;
445 strncpy(menu[i], items[i-menu_top], text_cols-1);
446 menu[i][text_cols-1] = '\0';
447 }
448 menu_items = i - menu_top;
449 show_menu = 1;
Doug Zongkerbe598882010-04-08 14:36:55 -0700450 menu_sel = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800451 update_screen_locked();
452 }
453 pthread_mutex_unlock(&gUpdateMutex);
454}
455
456int ui_menu_select(int sel) {
457 int old_sel;
458 pthread_mutex_lock(&gUpdateMutex);
459 if (show_menu > 0) {
460 old_sel = menu_sel;
461 menu_sel = sel;
462 if (menu_sel < 0) menu_sel = 0;
463 if (menu_sel >= menu_items) menu_sel = menu_items-1;
464 sel = menu_sel;
465 if (menu_sel != old_sel) update_screen_locked();
466 }
467 pthread_mutex_unlock(&gUpdateMutex);
468 return sel;
469}
470
471void ui_end_menu() {
472 int i;
473 pthread_mutex_lock(&gUpdateMutex);
474 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
475 show_menu = 0;
476 update_screen_locked();
477 }
478 pthread_mutex_unlock(&gUpdateMutex);
479}
480
481int ui_text_visible()
482{
483 pthread_mutex_lock(&gUpdateMutex);
484 int visible = show_text;
485 pthread_mutex_unlock(&gUpdateMutex);
486 return visible;
487}
488
Doug Zongker5cae4452011-01-25 13:15:30 -0800489int ui_text_ever_visible()
490{
491 pthread_mutex_lock(&gUpdateMutex);
492 int ever_visible = show_text_ever;
493 pthread_mutex_unlock(&gUpdateMutex);
494 return ever_visible;
495}
496
Doug Zongker4bc98062010-09-03 11:00:13 -0700497void ui_show_text(int visible)
498{
499 pthread_mutex_lock(&gUpdateMutex);
500 show_text = visible;
Doug Zongker5cae4452011-01-25 13:15:30 -0800501 if (show_text) show_text_ever = 1;
Doug Zongker4bc98062010-09-03 11:00:13 -0700502 update_screen_locked();
503 pthread_mutex_unlock(&gUpdateMutex);
504}
505
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800506int ui_wait_key()
507{
508 pthread_mutex_lock(&key_queue_mutex);
Doug Zongker5cae4452011-01-25 13:15:30 -0800509
510 struct timeval now;
511 struct timespec timeout;
512 gettimeofday(&now, NULL);
513 timeout.tv_sec = now.tv_sec;
514 timeout.tv_nsec = now.tv_usec * 1000;
515 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
516
517 int rc = 0;
518 while (key_queue_len == 0 && rc != ETIMEDOUT) {
519 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex,
520 &timeout);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800521 }
522
Doug Zongker5cae4452011-01-25 13:15:30 -0800523 int key = -1;
524 if (key_queue_len > 0) {
525 key = key_queue[0];
526 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
527 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800528 pthread_mutex_unlock(&key_queue_mutex);
529 return key;
530}
531
532int ui_key_pressed(int key)
533{
534 // This is a volatile static array, don't bother locking
535 return key_pressed[key];
536}
537
538void ui_clear_key_queue() {
539 pthread_mutex_lock(&key_queue_mutex);
540 key_queue_len = 0;
541 pthread_mutex_unlock(&key_queue_mutex);
542}