blob: ee6a0c5caa7c067d722f3a17926443f9fe5368ff [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
Doug Zongkerf291d852010-07-07 13:55:25 -070032#define MAX_COLS 96
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033#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" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050 { &gProgressBarIndeterminate[0], "indeterminate1" },
51 { &gProgressBarIndeterminate[1], "indeterminate2" },
52 { &gProgressBarIndeterminate[2], "indeterminate3" },
53 { &gProgressBarIndeterminate[3], "indeterminate4" },
54 { &gProgressBarIndeterminate[4], "indeterminate5" },
55 { &gProgressBarIndeterminate[5], "indeterminate6" },
Doug Zongkerd93a2542009-10-08 16:32:58 -070056 { &gProgressBarEmpty, "progress_empty" },
57 { &gProgressBarFill, "progress_fill" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058 { NULL, NULL },
59};
60
61static gr_surface gCurrentIcon = NULL;
62
63static 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
70static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
71static time_t gProgressScopeTime, gProgressScopeDuration;
72
73// Set to 1 when both graphics pages are the same (except for the progress bar)
74static int gPagesIdentical = 0;
75
76// Log text overlay, displayed when a magic key is pressed
77static char text[MAX_ROWS][MAX_COLS];
78static int text_cols = 0, text_rows = 0;
79static int text_col = 0, text_row = 0, text_top = 0;
80static int show_text = 0;
81
82static char menu[MAX_ROWS][MAX_COLS];
83static int show_menu = 0;
84static int menu_top = 0, menu_items = 0, menu_sel = 0;
85
86// Key event input queue
87static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
88static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER;
89static int key_queue[256], key_queue_len = 0;
90static volatile char key_pressed[KEY_MAX + 1];
91
92// Clear the screen and draw the currently selected background icon (if any).
93// Should only be called with gUpdateMutex locked.
94static void draw_background_locked(gr_surface icon)
95{
96 gPagesIdentical = 0;
97 gr_color(0, 0, 0, 255);
98 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
99
100 if (icon) {
101 int iconWidth = gr_get_width(icon);
102 int iconHeight = gr_get_height(icon);
103 int iconX = (gr_fb_width() - iconWidth) / 2;
104 int iconY = (gr_fb_height() - iconHeight) / 2;
105 gr_blit(icon, 0, 0, iconWidth, iconHeight, iconX, iconY);
106 }
107}
108
109// Draw the progress bar (if any) on the screen. Does not flip pages.
110// Should only be called with gUpdateMutex locked.
111static void draw_progress_locked()
112{
113 if (gProgressBarType == PROGRESSBAR_TYPE_NONE) return;
114
115 int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]);
Doug Zongkerd93a2542009-10-08 16:32:58 -0700116 int width = gr_get_width(gProgressBarEmpty);
117 int height = gr_get_height(gProgressBarEmpty);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800118
119 int dx = (gr_fb_width() - width)/2;
120 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
121
122 // Erase behind the progress bar (in case this was a progress-only update)
123 gr_color(0, 0, 0, 255);
124 gr_fill(dx, dy, width, height);
125
126 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) {
127 float progress = gProgressScopeStart + gProgress * gProgressScopeSize;
128 int pos = (int) (progress * width);
129
Doug Zongkerd93a2542009-10-08 16:32:58 -0700130 if (pos > 0) {
131 gr_blit(gProgressBarFill, 0, 0, pos, height, dx, dy);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800132 }
Doug Zongkerd93a2542009-10-08 16:32:58 -0700133 if (pos < width-1) {
134 gr_blit(gProgressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
135 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800136 }
137
138 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) {
139 static int frame = 0;
140 gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
141 frame = (frame + 1) % PROGRESSBAR_INDETERMINATE_STATES;
142 }
143}
144
145static void draw_text_line(int row, const char* t) {
146 if (t[0] != '\0') {
147 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
148 }
149}
150
151// Redraw everything on the screen. Does not flip pages.
152// Should only be called with gUpdateMutex locked.
153static void draw_screen_locked(void)
154{
155 draw_background_locked(gCurrentIcon);
156 draw_progress_locked();
157
158 if (show_text) {
159 gr_color(0, 0, 0, 160);
160 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
161
162 int i = 0;
163 if (show_menu) {
164 gr_color(64, 96, 255, 255);
165 gr_fill(0, (menu_top+menu_sel) * CHAR_HEIGHT,
166 gr_fb_width(), (menu_top+menu_sel+1)*CHAR_HEIGHT+1);
167
168 for (; i < menu_top + menu_items; ++i) {
169 if (i == menu_top + menu_sel) {
170 gr_color(255, 255, 255, 255);
171 draw_text_line(i, menu[i]);
172 gr_color(64, 96, 255, 255);
173 } else {
174 draw_text_line(i, menu[i]);
175 }
176 }
177 gr_fill(0, i*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
178 gr_fb_width(), i*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
179 ++i;
180 }
181
182 gr_color(255, 255, 0, 255);
183
184 for (; i < text_rows; ++i) {
185 draw_text_line(i, text[(i+text_top) % text_rows]);
186 }
187 }
188}
189
190// Redraw everything on the screen and flip the screen (make it visible).
191// Should only be called with gUpdateMutex locked.
192static void update_screen_locked(void)
193{
194 draw_screen_locked();
195 gr_flip();
196}
197
198// Updates only the progress bar, if possible, otherwise redraws the screen.
199// Should only be called with gUpdateMutex locked.
200static void update_progress_locked(void)
201{
202 if (show_text || !gPagesIdentical) {
203 draw_screen_locked(); // Must redraw the whole screen
204 gPagesIdentical = 1;
205 } else {
206 draw_progress_locked(); // Draw only the progress bar
207 }
208 gr_flip();
209}
210
211// Keeps the progress bar updated, even when the process is otherwise busy.
212static void *progress_thread(void *cookie)
213{
214 for (;;) {
215 usleep(1000000 / PROGRESSBAR_INDETERMINATE_FPS);
216 pthread_mutex_lock(&gUpdateMutex);
217
218 // update the progress bar animation, if active
219 // skip this if we have a text overlay (too expensive to update)
220 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) {
221 update_progress_locked();
222 }
223
224 // move the progress bar forward on timed intervals, if configured
225 int duration = gProgressScopeDuration;
226 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) {
227 int elapsed = time(NULL) - gProgressScopeTime;
228 float progress = 1.0 * elapsed / duration;
229 if (progress > 1.0) progress = 1.0;
230 if (progress > gProgress) {
231 gProgress = progress;
232 update_progress_locked();
233 }
234 }
235
236 pthread_mutex_unlock(&gUpdateMutex);
237 }
238 return NULL;
239}
240
241// Reads input events, handles special hot keys, and adds to the key queue.
242static void *input_thread(void *cookie)
243{
244 int rel_sum = 0;
245 int fake_key = 0;
246 for (;;) {
247 // wait for the next key event
248 struct input_event ev;
249 do {
250 ev_get(&ev, 0);
251
252 if (ev.type == EV_SYN) {
253 continue;
254 } else if (ev.type == EV_REL) {
255 if (ev.code == REL_Y) {
256 // accumulate the up or down motion reported by
257 // the trackball. When it exceeds a threshold
258 // (positive or negative), fake an up/down
259 // key event.
260 rel_sum += ev.value;
261 if (rel_sum > 3) {
262 fake_key = 1;
263 ev.type = EV_KEY;
264 ev.code = KEY_DOWN;
265 ev.value = 1;
266 rel_sum = 0;
267 } else if (rel_sum < -3) {
268 fake_key = 1;
269 ev.type = EV_KEY;
270 ev.code = KEY_UP;
271 ev.value = 1;
272 rel_sum = 0;
273 }
274 }
275 } else {
276 rel_sum = 0;
277 }
278 } while (ev.type != EV_KEY || ev.code > KEY_MAX);
279
280 pthread_mutex_lock(&key_queue_mutex);
281 if (!fake_key) {
282 // our "fake" keys only report a key-down event (no
283 // key-up), so don't record them in the key_pressed
284 // table.
285 key_pressed[ev.code] = ev.value;
286 }
287 fake_key = 0;
288 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
289 if (ev.value > 0 && key_queue_len < queue_max) {
290 key_queue[key_queue_len++] = ev.code;
291 pthread_cond_signal(&key_queue_cond);
292 }
293 pthread_mutex_unlock(&key_queue_mutex);
294
Doug Zongkerddd6a282009-06-09 12:22:33 -0700295 if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800296 pthread_mutex_lock(&gUpdateMutex);
297 show_text = !show_text;
298 update_screen_locked();
299 pthread_mutex_unlock(&gUpdateMutex);
300 }
301
Doug Zongkerddd6a282009-06-09 12:22:33 -0700302 if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800303 reboot(RB_AUTOBOOT);
304 }
305 }
306 return NULL;
307}
308
309void ui_init(void)
310{
311 gr_init();
312 ev_init();
313
314 text_col = text_row = 0;
315 text_rows = gr_fb_height() / CHAR_HEIGHT;
316 if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
317 text_top = 1;
318
319 text_cols = gr_fb_width() / CHAR_WIDTH;
320 if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
321
322 int i;
323 for (i = 0; BITMAPS[i].name != NULL; ++i) {
324 int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface);
325 if (result < 0) {
Doug Zongker196c25c2009-09-15 08:50:04 -0700326 if (result == -2) {
327 LOGI("Bitmap %s missing header\n", BITMAPS[i].name);
328 } else {
329 LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result);
330 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800331 *BITMAPS[i].surface = NULL;
332 }
333 }
334
335 pthread_t t;
336 pthread_create(&t, NULL, progress_thread, NULL);
337 pthread_create(&t, NULL, input_thread, NULL);
338}
339
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800340void ui_set_background(int icon)
341{
342 pthread_mutex_lock(&gUpdateMutex);
343 gCurrentIcon = gBackgroundIcon[icon];
344 update_screen_locked();
345 pthread_mutex_unlock(&gUpdateMutex);
346}
347
348void ui_show_indeterminate_progress()
349{
350 pthread_mutex_lock(&gUpdateMutex);
351 if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
352 gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
353 update_progress_locked();
354 }
355 pthread_mutex_unlock(&gUpdateMutex);
356}
357
358void ui_show_progress(float portion, int seconds)
359{
360 pthread_mutex_lock(&gUpdateMutex);
361 gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
362 gProgressScopeStart += gProgressScopeSize;
363 gProgressScopeSize = portion;
364 gProgressScopeTime = time(NULL);
365 gProgressScopeDuration = seconds;
366 gProgress = 0;
367 update_progress_locked();
368 pthread_mutex_unlock(&gUpdateMutex);
369}
370
371void ui_set_progress(float fraction)
372{
373 pthread_mutex_lock(&gUpdateMutex);
374 if (fraction < 0.0) fraction = 0.0;
375 if (fraction > 1.0) fraction = 1.0;
376 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
377 // Skip updates that aren't visibly different.
378 int width = gr_get_width(gProgressBarIndeterminate[0]);
379 float scale = width * gProgressScopeSize;
380 if ((int) (gProgress * scale) != (int) (fraction * scale)) {
381 gProgress = fraction;
382 update_progress_locked();
383 }
384 }
385 pthread_mutex_unlock(&gUpdateMutex);
386}
387
388void ui_reset_progress()
389{
390 pthread_mutex_lock(&gUpdateMutex);
391 gProgressBarType = PROGRESSBAR_TYPE_NONE;
392 gProgressScopeStart = gProgressScopeSize = 0;
393 gProgressScopeTime = gProgressScopeDuration = 0;
394 gProgress = 0;
395 update_screen_locked();
396 pthread_mutex_unlock(&gUpdateMutex);
397}
398
399void ui_print(const char *fmt, ...)
400{
401 char buf[256];
402 va_list ap;
403 va_start(ap, fmt);
404 vsnprintf(buf, 256, fmt, ap);
405 va_end(ap);
406
Doug Zongker3d177d02010-07-01 09:18:44 -0700407 fputs(buf, stdout);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800408
409 // This can get called before ui_init(), so be careful.
410 pthread_mutex_lock(&gUpdateMutex);
411 if (text_rows > 0 && text_cols > 0) {
412 char *ptr;
413 for (ptr = buf; *ptr != '\0'; ++ptr) {
414 if (*ptr == '\n' || text_col >= text_cols) {
415 text[text_row][text_col] = '\0';
416 text_col = 0;
417 text_row = (text_row + 1) % text_rows;
418 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
419 }
420 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
421 }
422 text[text_row][text_col] = '\0';
423 update_screen_locked();
424 }
425 pthread_mutex_unlock(&gUpdateMutex);
426}
427
Doug Zongkerbe598882010-04-08 14:36:55 -0700428void ui_start_menu(char** headers, char** items, int initial_selection) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800429 int i;
430 pthread_mutex_lock(&gUpdateMutex);
431 if (text_rows > 0 && text_cols > 0) {
432 for (i = 0; i < text_rows; ++i) {
433 if (headers[i] == NULL) break;
434 strncpy(menu[i], headers[i], text_cols-1);
435 menu[i][text_cols-1] = '\0';
436 }
437 menu_top = i;
438 for (; i < text_rows; ++i) {
439 if (items[i-menu_top] == NULL) break;
440 strncpy(menu[i], items[i-menu_top], text_cols-1);
441 menu[i][text_cols-1] = '\0';
442 }
443 menu_items = i - menu_top;
444 show_menu = 1;
Doug Zongkerbe598882010-04-08 14:36:55 -0700445 menu_sel = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800446 update_screen_locked();
447 }
448 pthread_mutex_unlock(&gUpdateMutex);
449}
450
451int ui_menu_select(int sel) {
452 int old_sel;
453 pthread_mutex_lock(&gUpdateMutex);
454 if (show_menu > 0) {
455 old_sel = menu_sel;
456 menu_sel = sel;
457 if (menu_sel < 0) menu_sel = 0;
458 if (menu_sel >= menu_items) menu_sel = menu_items-1;
459 sel = menu_sel;
460 if (menu_sel != old_sel) update_screen_locked();
461 }
462 pthread_mutex_unlock(&gUpdateMutex);
463 return sel;
464}
465
466void ui_end_menu() {
467 int i;
468 pthread_mutex_lock(&gUpdateMutex);
469 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
470 show_menu = 0;
471 update_screen_locked();
472 }
473 pthread_mutex_unlock(&gUpdateMutex);
474}
475
476int ui_text_visible()
477{
478 pthread_mutex_lock(&gUpdateMutex);
479 int visible = show_text;
480 pthread_mutex_unlock(&gUpdateMutex);
481 return visible;
482}
483
484int ui_wait_key()
485{
486 pthread_mutex_lock(&key_queue_mutex);
487 while (key_queue_len == 0) {
488 pthread_cond_wait(&key_queue_cond, &key_queue_mutex);
489 }
490
491 int key = key_queue[0];
492 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
493 pthread_mutex_unlock(&key_queue_mutex);
494 return key;
495}
496
497int ui_key_pressed(int key)
498{
499 // This is a volatile static array, don't bother locking
500 return key_pressed[key];
501}
502
503void ui_clear_key_queue() {
504 pthread_mutex_lock(&key_queue_mutex);
505 key_queue_len = 0;
506 pthread_mutex_unlock(&key_queue_mutex);
507}