blob: e6a31db28c62372eefcd6dd90ee348f7849f69c3 [file] [log] [blame]
Doug Zongker74406302011-10-28 15:13:10 -07001/*
2 * Copyright (C) 2011 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 <errno.h>
18#include <fcntl.h>
19#include <linux/input.h>
20#include <pthread.h>
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/time.h>
27#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
30
31#include "common.h"
32#include <cutils/android_reboot.h>
33#include "minui/minui.h"
34#include "recovery_ui.h"
35#include "ui.h"
36#include "screen_ui.h"
37
38#define CHAR_WIDTH 10
39#define CHAR_HEIGHT 18
40
41#define UI_WAIT_KEY_TIMEOUT_SEC 120
42
43UIParameters ui_parameters = {
44 6, // indeterminate progress bar frames
45 20, // fps
46 7, // installation icon frames (0 == static image)
47 13, 190, // installation icon overlay offset
48};
49
50// There's only (at most) one of these objects, and global callbacks
51// (for pthread_create, and the input event system) need to find it,
52// so use a global variable.
53static ScreenRecoveryUI* self = NULL;
54
55// Return the current time as a double (including fractions of a second).
56static double now() {
57 struct timeval tv;
58 gettimeofday(&tv, NULL);
59 return tv.tv_sec + tv.tv_usec / 1000000.0;
60}
61
62ScreenRecoveryUI::ScreenRecoveryUI() :
63 currentIcon(NONE),
64 installingFrame(0),
65 progressBarType(EMPTY),
66 progressScopeStart(0),
67 progressScopeSize(0),
68 progress(0),
69 pagesIdentical(false),
70 text_cols(0),
71 text_rows(0),
72 text_col(0),
73 text_row(0),
74 text_top(0),
75 show_text(false),
76 show_text_ever(false),
77 show_menu(false),
78 menu_top(0),
79 menu_items(0),
80 menu_sel(0),
81 key_queue_len(0) {
82 pthread_mutex_init(&updateMutex, NULL);
83 pthread_mutex_init(&key_queue_mutex, NULL);
84 pthread_cond_init(&key_queue_cond, NULL);
85 self = this;
86}
87
88// Draw the given frame over the installation overlay animation. The
89// background is not cleared or draw with the base icon first; we
90// assume that the frame already contains some other frame of the
91// animation. Does nothing if no overlay animation is defined.
92// Should only be called with updateMutex locked.
93void ScreenRecoveryUI::draw_install_overlay_locked(int frame) {
94 if (installationOverlay == NULL) return;
95 gr_surface surface = installationOverlay[frame];
96 int iconWidth = gr_get_width(surface);
97 int iconHeight = gr_get_height(surface);
98 gr_blit(surface, 0, 0, iconWidth, iconHeight,
99 ui_parameters.install_overlay_offset_x,
100 ui_parameters.install_overlay_offset_y);
101}
102
103// Clear the screen and draw the currently selected background icon (if any).
104// Should only be called with updateMutex locked.
105void ScreenRecoveryUI::draw_background_locked(Icon icon)
106{
107 pagesIdentical = false;
108 gr_color(0, 0, 0, 255);
109 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
110
111 if (icon) {
112 gr_surface surface = backgroundIcon[icon];
113 int iconWidth = gr_get_width(surface);
114 int iconHeight = gr_get_height(surface);
115 int iconX = (gr_fb_width() - iconWidth) / 2;
116 int iconY = (gr_fb_height() - iconHeight) / 2;
117 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
118 if (icon == INSTALLING) {
119 draw_install_overlay_locked(installingFrame);
120 }
121 }
122}
123
124// Draw the progress bar (if any) on the screen. Does not flip pages.
125// Should only be called with updateMutex locked.
126void ScreenRecoveryUI::draw_progress_locked()
127{
128 if (currentIcon == INSTALLING) {
129 draw_install_overlay_locked(installingFrame);
130 }
131
132 if (progressBarType != EMPTY) {
133 int iconHeight = gr_get_height(backgroundIcon[INSTALLING]);
134 int width = gr_get_width(progressBarEmpty);
135 int height = gr_get_height(progressBarEmpty);
136
137 int dx = (gr_fb_width() - width)/2;
138 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
139
140 // Erase behind the progress bar (in case this was a progress-only update)
141 gr_color(0, 0, 0, 255);
142 gr_fill(dx, dy, width, height);
143
144 if (progressBarType == DETERMINATE) {
145 float p = progressScopeStart + progress * progressScopeSize;
146 int pos = (int) (p * width);
147
148 if (pos > 0) {
149 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
150 }
151 if (pos < width-1) {
152 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
153 }
154 }
155
156 if (progressBarType == INDETERMINATE) {
157 static int frame = 0;
158 gr_blit(progressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
159 frame = (frame + 1) % ui_parameters.indeterminate_frames;
160 }
161 }
162}
163
164void ScreenRecoveryUI::draw_text_line(int row, const char* t) {
165 if (t[0] != '\0') {
166 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
167 }
168}
169
170// Redraw everything on the screen. Does not flip pages.
171// Should only be called with updateMutex locked.
172void ScreenRecoveryUI::draw_screen_locked()
173{
174 draw_background_locked(currentIcon);
175 draw_progress_locked();
176
177 if (show_text) {
178 gr_color(0, 0, 0, 160);
179 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
180
181 int i = 0;
182 if (show_menu) {
183 gr_color(64, 96, 255, 255);
184 gr_fill(0, (menu_top+menu_sel) * CHAR_HEIGHT,
185 gr_fb_width(), (menu_top+menu_sel+1)*CHAR_HEIGHT+1);
186
187 for (; i < menu_top + menu_items; ++i) {
188 if (i == menu_top + menu_sel) {
189 gr_color(255, 255, 255, 255);
190 draw_text_line(i, menu[i]);
191 gr_color(64, 96, 255, 255);
192 } else {
193 draw_text_line(i, menu[i]);
194 }
195 }
196 gr_fill(0, i*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
197 gr_fb_width(), i*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
198 ++i;
199 }
200
201 gr_color(255, 255, 0, 255);
202
203 for (; i < text_rows; ++i) {
204 draw_text_line(i, text[(i+text_top) % text_rows]);
205 }
206 }
207}
208
209// Redraw everything on the screen and flip the screen (make it visible).
210// Should only be called with updateMutex locked.
211void ScreenRecoveryUI::update_screen_locked()
212{
213 draw_screen_locked();
214 gr_flip();
215}
216
217// Updates only the progress bar, if possible, otherwise redraws the screen.
218// Should only be called with updateMutex locked.
219void ScreenRecoveryUI::update_progress_locked()
220{
221 if (show_text || !pagesIdentical) {
222 draw_screen_locked(); // Must redraw the whole screen
223 pagesIdentical = true;
224 } else {
225 draw_progress_locked(); // Draw only the progress bar and overlays
226 }
227 gr_flip();
228}
229
230// Keeps the progress bar updated, even when the process is otherwise busy.
231void* ScreenRecoveryUI::progress_thread(void *cookie)
232{
233 double interval = 1.0 / ui_parameters.update_fps;
234 for (;;) {
235 double start = now();
236 pthread_mutex_lock(&self->updateMutex);
237
238 int redraw = 0;
239
240 // update the installation animation, if active
241 // skip this if we have a text overlay (too expensive to update)
242 if (self->currentIcon == INSTALLING &&
243 ui_parameters.installing_frames > 0 &&
244 !self->show_text) {
245 self->installingFrame =
246 (self->installingFrame + 1) % ui_parameters.installing_frames;
247 redraw = 1;
248 }
249
250 // update the progress bar animation, if active
251 // skip this if we have a text overlay (too expensive to update)
252 if (self->progressBarType == INDETERMINATE && !self->show_text) {
253 redraw = 1;
254 }
255
256 // move the progress bar forward on timed intervals, if configured
257 int duration = self->progressScopeDuration;
258 if (self->progressBarType == DETERMINATE && duration > 0) {
259 double elapsed = now() - self->progressScopeTime;
260 float progress = 1.0 * elapsed / duration;
261 if (progress > 1.0) progress = 1.0;
262 if (progress > progress) {
263 progress = progress;
264 redraw = 1;
265 }
266 }
267
268 if (redraw) self->update_progress_locked();
269
270 pthread_mutex_unlock(&self->updateMutex);
271 double end = now();
272 // minimum of 20ms delay between frames
273 double delay = interval - (end-start);
274 if (delay < 0.02) delay = 0.02;
275 usleep((long)(delay * 1000000));
276 }
277 return NULL;
278}
279
280int ScreenRecoveryUI::input_callback(int fd, short revents, void* data)
281{
282 struct input_event ev;
283 int ret;
284 int fake_key = 0;
285
286 ret = ev_get_input(fd, revents, &ev);
287 if (ret)
288 return -1;
289
290 if (ev.type == EV_SYN) {
291 return 0;
292 } else if (ev.type == EV_REL) {
293 if (ev.code == REL_Y) {
294 // accumulate the up or down motion reported by
295 // the trackball. When it exceeds a threshold
296 // (positive or negative), fake an up/down
297 // key event.
298 self->rel_sum += ev.value;
299 if (self->rel_sum > 3) {
300 fake_key = 1;
301 ev.type = EV_KEY;
302 ev.code = KEY_DOWN;
303 ev.value = 1;
304 self->rel_sum = 0;
305 } else if (self->rel_sum < -3) {
306 fake_key = 1;
307 ev.type = EV_KEY;
308 ev.code = KEY_UP;
309 ev.value = 1;
310 self->rel_sum = 0;
311 }
312 }
313 } else {
314 self->rel_sum = 0;
315 }
316
317 if (ev.type != EV_KEY || ev.code > KEY_MAX)
318 return 0;
319
320 pthread_mutex_lock(&self->key_queue_mutex);
321 if (!fake_key) {
322 // our "fake" keys only report a key-down event (no
323 // key-up), so don't record them in the key_pressed
324 // table.
325 self->key_pressed[ev.code] = ev.value;
326 }
327 const int queue_max = sizeof(self->key_queue) / sizeof(self->key_queue[0]);
328 if (ev.value > 0 && self->key_queue_len < queue_max) {
329 self->key_queue[self->key_queue_len++] = ev.code;
330 pthread_cond_signal(&self->key_queue_cond);
331 }
332 pthread_mutex_unlock(&self->key_queue_mutex);
333
334 if (ev.value > 0 && device_toggle_display(self->key_pressed, ev.code)) {
335 pthread_mutex_lock(&self->updateMutex);
336 self->show_text = !self->show_text;
337 if (self->show_text) self->show_text_ever = true;
338 self->update_screen_locked();
339 pthread_mutex_unlock(&self->updateMutex);
340 }
341
342 if (ev.value > 0 && device_reboot_now(self->key_pressed, ev.code)) {
343 android_reboot(ANDROID_RB_RESTART, 0, 0);
344 }
345
346 return 0;
347}
348
349// Reads input events, handles special hot keys, and adds to the key queue.
350void* ScreenRecoveryUI::input_thread(void *cookie)
351{
352 for (;;) {
353 if (!ev_wait(-1))
354 ev_dispatch();
355 }
356 return NULL;
357}
358
359void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
360 int result = res_create_surface(filename, surface);
361 if (result < 0) {
362 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
363 }
364}
365
366void ScreenRecoveryUI::Init()
367{
368 gr_init();
369 ev_init(input_callback, NULL);
370
371 text_col = text_row = 0;
372 text_rows = gr_fb_height() / CHAR_HEIGHT;
373 if (text_rows > kMaxRows) text_rows = kMaxRows;
374 text_top = 1;
375
376 text_cols = gr_fb_width() / CHAR_WIDTH;
377 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
378
379 LoadBitmap("icon_installing", &backgroundIcon[INSTALLING]);
380 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
381 LoadBitmap("progress_empty", &progressBarEmpty);
382 LoadBitmap("progress_fill", &progressBarFill);
383
384 int i;
385
386 progressBarIndeterminate = (gr_surface*)malloc(ui_parameters.indeterminate_frames *
387 sizeof(gr_surface));
388 for (i = 0; i < ui_parameters.indeterminate_frames; ++i) {
389 char filename[40];
390 // "indeterminate01.png", "indeterminate02.png", ...
391 sprintf(filename, "indeterminate%02d", i+1);
392 LoadBitmap(filename, progressBarIndeterminate+i);
393 }
394
395 if (ui_parameters.installing_frames > 0) {
396 installationOverlay = (gr_surface*)malloc(ui_parameters.installing_frames *
397 sizeof(gr_surface));
398 for (i = 0; i < ui_parameters.installing_frames; ++i) {
399 char filename[40];
400 // "icon_installing_overlay01.png",
401 // "icon_installing_overlay02.png", ...
402 sprintf(filename, "icon_installing_overlay%02d", i+1);
403 LoadBitmap(filename, installationOverlay+i);
404 }
405
406 // Adjust the offset to account for the positioning of the
407 // base image on the screen.
408 if (backgroundIcon[INSTALLING] != NULL) {
409 gr_surface bg = backgroundIcon[INSTALLING];
410 ui_parameters.install_overlay_offset_x +=
411 (gr_fb_width() - gr_get_width(bg)) / 2;
412 ui_parameters.install_overlay_offset_y +=
413 (gr_fb_height() - gr_get_height(bg)) / 2;
414 }
415 } else {
416 installationOverlay = NULL;
417 }
418
419 pthread_create(&progress_t, NULL, progress_thread, NULL);
420 pthread_create(&input_t, NULL, input_thread, NULL);
421}
422
423void ScreenRecoveryUI::SetBackground(Icon icon)
424{
425 pthread_mutex_lock(&updateMutex);
426 currentIcon = icon;
427 update_screen_locked();
428 pthread_mutex_unlock(&updateMutex);
429}
430
431void ScreenRecoveryUI::SetProgressType(ProgressType type)
432{
433 pthread_mutex_lock(&updateMutex);
434 if (progressBarType != type) {
435 progressBarType = type;
436 update_progress_locked();
437 }
438 pthread_mutex_unlock(&updateMutex);
439}
440
441void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
442{
443 pthread_mutex_lock(&updateMutex);
444 progressBarType = DETERMINATE;
445 progressScopeStart += progressScopeSize;
446 progressScopeSize = portion;
447 progressScopeTime = now();
448 progressScopeDuration = seconds;
449 progress = 0;
450 update_progress_locked();
451 pthread_mutex_unlock(&updateMutex);
452}
453
454void ScreenRecoveryUI::SetProgress(float fraction)
455{
456 pthread_mutex_lock(&updateMutex);
457 if (fraction < 0.0) fraction = 0.0;
458 if (fraction > 1.0) fraction = 1.0;
459 if (progressBarType == DETERMINATE && fraction > progress) {
460 // Skip updates that aren't visibly different.
461 int width = gr_get_width(progressBarIndeterminate[0]);
462 float scale = width * progressScopeSize;
463 if ((int) (progress * scale) != (int) (fraction * scale)) {
464 progress = fraction;
465 update_progress_locked();
466 }
467 }
468 pthread_mutex_unlock(&updateMutex);
469}
470
471void ScreenRecoveryUI::Print(const char *fmt, ...)
472{
473 char buf[256];
474 va_list ap;
475 va_start(ap, fmt);
476 vsnprintf(buf, 256, fmt, ap);
477 va_end(ap);
478
479 fputs(buf, stdout);
480
481 // This can get called before ui_init(), so be careful.
482 pthread_mutex_lock(&updateMutex);
483 if (text_rows > 0 && text_cols > 0) {
484 char *ptr;
485 for (ptr = buf; *ptr != '\0'; ++ptr) {
486 if (*ptr == '\n' || text_col >= text_cols) {
487 text[text_row][text_col] = '\0';
488 text_col = 0;
489 text_row = (text_row + 1) % text_rows;
490 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
491 }
492 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
493 }
494 text[text_row][text_col] = '\0';
495 update_screen_locked();
496 }
497 pthread_mutex_unlock(&updateMutex);
498}
499
500void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
501 int initial_selection) {
502 int i;
503 pthread_mutex_lock(&updateMutex);
504 if (text_rows > 0 && text_cols > 0) {
505 for (i = 0; i < text_rows; ++i) {
506 if (headers[i] == NULL) break;
507 strncpy(menu[i], headers[i], text_cols-1);
508 menu[i][text_cols-1] = '\0';
509 }
510 menu_top = i;
511 for (; i < text_rows; ++i) {
512 if (items[i-menu_top] == NULL) break;
513 strncpy(menu[i], items[i-menu_top], text_cols-1);
514 menu[i][text_cols-1] = '\0';
515 }
516 menu_items = i - menu_top;
517 show_menu = 1;
518 menu_sel = initial_selection;
519 update_screen_locked();
520 }
521 pthread_mutex_unlock(&updateMutex);
522}
523
524int ScreenRecoveryUI::SelectMenu(int sel) {
525 int old_sel;
526 pthread_mutex_lock(&updateMutex);
527 if (show_menu > 0) {
528 old_sel = menu_sel;
529 menu_sel = sel;
530 if (menu_sel < 0) menu_sel = 0;
531 if (menu_sel >= menu_items) menu_sel = menu_items-1;
532 sel = menu_sel;
533 if (menu_sel != old_sel) update_screen_locked();
534 }
535 pthread_mutex_unlock(&updateMutex);
536 return sel;
537}
538
539void ScreenRecoveryUI::EndMenu() {
540 int i;
541 pthread_mutex_lock(&updateMutex);
542 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
543 show_menu = 0;
544 update_screen_locked();
545 }
546 pthread_mutex_unlock(&updateMutex);
547}
548
549bool ScreenRecoveryUI::IsTextVisible()
550{
551 pthread_mutex_lock(&updateMutex);
552 int visible = show_text;
553 pthread_mutex_unlock(&updateMutex);
554 return visible;
555}
556
557bool ScreenRecoveryUI::WasTextEverVisible()
558{
559 pthread_mutex_lock(&updateMutex);
560 int ever_visible = show_text_ever;
561 pthread_mutex_unlock(&updateMutex);
562 return ever_visible;
563}
564
565void ScreenRecoveryUI::ShowText(bool visible)
566{
567 pthread_mutex_lock(&updateMutex);
568 show_text = visible;
569 if (show_text) show_text_ever = 1;
570 update_screen_locked();
571 pthread_mutex_unlock(&updateMutex);
572}
573
574// Return true if USB is connected.
575bool ScreenRecoveryUI::usb_connected() {
576 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
577 if (fd < 0) {
578 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
579 strerror(errno));
580 return 0;
581 }
582
583 char buf;
584 /* USB is connected if android_usb state is CONNECTED or CONFIGURED */
585 int connected = (read(fd, &buf, 1) == 1) && (buf == 'C');
586 if (close(fd) < 0) {
587 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
588 strerror(errno));
589 }
590 return connected;
591}
592
593int ScreenRecoveryUI::WaitKey()
594{
595 pthread_mutex_lock(&key_queue_mutex);
596
597 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
598 // plugged in.
599 do {
600 struct timeval now;
601 struct timespec timeout;
602 gettimeofday(&now, NULL);
603 timeout.tv_sec = now.tv_sec;
604 timeout.tv_nsec = now.tv_usec * 1000;
605 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
606
607 int rc = 0;
608 while (key_queue_len == 0 && rc != ETIMEDOUT) {
609 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex,
610 &timeout);
611 }
612 } while (usb_connected() && key_queue_len == 0);
613
614 int key = -1;
615 if (key_queue_len > 0) {
616 key = key_queue[0];
617 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
618 }
619 pthread_mutex_unlock(&key_queue_mutex);
620 return key;
621}
622
623bool ScreenRecoveryUI::IsKeyPressed(int key)
624{
625 // This is a volatile static array, don't bother locking
626 return key_pressed[key];
627}
628
629void ScreenRecoveryUI::FlushKeys() {
630 pthread_mutex_lock(&key_queue_mutex);
631 key_queue_len = 0;
632 pthread_mutex_unlock(&key_queue_mutex);
633}