blob: a60b04682362ded3991326ea6882d824463d6cb5 [file] [log] [blame]
Doug Zongker211aebc2011-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"
Doug Zongker211aebc2011-10-28 15:13:10 -070034#include "ui.h"
35#include "screen_ui.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070036#include "device.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070037
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),
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070081 key_queue_len(0),
82 key_last_down(-1) {
Doug Zongker211aebc2011-10-28 15:13:10 -070083 pthread_mutex_init(&updateMutex, NULL);
84 pthread_mutex_init(&key_queue_mutex, NULL);
85 pthread_cond_init(&key_queue_cond, NULL);
86 self = this;
87}
88
89// Draw the given frame over the installation overlay animation. The
90// background is not cleared or draw with the base icon first; we
91// assume that the frame already contains some other frame of the
92// animation. Does nothing if no overlay animation is defined.
93// Should only be called with updateMutex locked.
94void ScreenRecoveryUI::draw_install_overlay_locked(int frame) {
95 if (installationOverlay == NULL) return;
96 gr_surface surface = installationOverlay[frame];
97 int iconWidth = gr_get_width(surface);
98 int iconHeight = gr_get_height(surface);
99 gr_blit(surface, 0, 0, iconWidth, iconHeight,
100 ui_parameters.install_overlay_offset_x,
101 ui_parameters.install_overlay_offset_y);
102}
103
104// Clear the screen and draw the currently selected background icon (if any).
105// Should only be called with updateMutex locked.
106void ScreenRecoveryUI::draw_background_locked(Icon icon)
107{
108 pagesIdentical = false;
109 gr_color(0, 0, 0, 255);
110 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
111
112 if (icon) {
113 gr_surface surface = backgroundIcon[icon];
114 int iconWidth = gr_get_width(surface);
115 int iconHeight = gr_get_height(surface);
116 int iconX = (gr_fb_width() - iconWidth) / 2;
117 int iconY = (gr_fb_height() - iconHeight) / 2;
118 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
119 if (icon == INSTALLING) {
120 draw_install_overlay_locked(installingFrame);
121 }
122 }
123}
124
125// Draw the progress bar (if any) on the screen. Does not flip pages.
126// Should only be called with updateMutex locked.
127void ScreenRecoveryUI::draw_progress_locked()
128{
129 if (currentIcon == INSTALLING) {
130 draw_install_overlay_locked(installingFrame);
131 }
132
133 if (progressBarType != EMPTY) {
134 int iconHeight = gr_get_height(backgroundIcon[INSTALLING]);
135 int width = gr_get_width(progressBarEmpty);
136 int height = gr_get_height(progressBarEmpty);
137
138 int dx = (gr_fb_width() - width)/2;
139 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
140
141 // Erase behind the progress bar (in case this was a progress-only update)
142 gr_color(0, 0, 0, 255);
143 gr_fill(dx, dy, width, height);
144
145 if (progressBarType == DETERMINATE) {
146 float p = progressScopeStart + progress * progressScopeSize;
147 int pos = (int) (p * width);
148
149 if (pos > 0) {
150 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
151 }
152 if (pos < width-1) {
153 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
154 }
155 }
156
157 if (progressBarType == INDETERMINATE) {
158 static int frame = 0;
159 gr_blit(progressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
160 frame = (frame + 1) % ui_parameters.indeterminate_frames;
161 }
162 }
163}
164
165void ScreenRecoveryUI::draw_text_line(int row, const char* t) {
166 if (t[0] != '\0') {
167 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
168 }
169}
170
171// Redraw everything on the screen. Does not flip pages.
172// Should only be called with updateMutex locked.
173void ScreenRecoveryUI::draw_screen_locked()
174{
175 draw_background_locked(currentIcon);
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) {
184 gr_color(64, 96, 255, 255);
185 gr_fill(0, (menu_top+menu_sel) * CHAR_HEIGHT,
186 gr_fb_width(), (menu_top+menu_sel+1)*CHAR_HEIGHT+1);
187
188 for (; i < menu_top + menu_items; ++i) {
189 if (i == menu_top + menu_sel) {
190 gr_color(255, 255, 255, 255);
191 draw_text_line(i, menu[i]);
192 gr_color(64, 96, 255, 255);
193 } else {
194 draw_text_line(i, menu[i]);
195 }
196 }
197 gr_fill(0, i*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
198 gr_fb_width(), i*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
199 ++i;
200 }
201
202 gr_color(255, 255, 0, 255);
203
204 for (; i < text_rows; ++i) {
205 draw_text_line(i, text[(i+text_top) % text_rows]);
206 }
207 }
208}
209
210// Redraw everything on the screen and flip the screen (make it visible).
211// Should only be called with updateMutex locked.
212void ScreenRecoveryUI::update_screen_locked()
213{
214 draw_screen_locked();
215 gr_flip();
216}
217
218// Updates only the progress bar, if possible, otherwise redraws the screen.
219// Should only be called with updateMutex locked.
220void ScreenRecoveryUI::update_progress_locked()
221{
222 if (show_text || !pagesIdentical) {
223 draw_screen_locked(); // Must redraw the whole screen
224 pagesIdentical = true;
225 } else {
226 draw_progress_locked(); // Draw only the progress bar and overlays
227 }
228 gr_flip();
229}
230
231// Keeps the progress bar updated, even when the process is otherwise busy.
232void* ScreenRecoveryUI::progress_thread(void *cookie)
233{
234 double interval = 1.0 / ui_parameters.update_fps;
235 for (;;) {
236 double start = now();
237 pthread_mutex_lock(&self->updateMutex);
238
239 int redraw = 0;
240
241 // update the installation animation, if active
242 // skip this if we have a text overlay (too expensive to update)
243 if (self->currentIcon == INSTALLING &&
244 ui_parameters.installing_frames > 0 &&
245 !self->show_text) {
246 self->installingFrame =
247 (self->installingFrame + 1) % ui_parameters.installing_frames;
248 redraw = 1;
249 }
250
251 // update the progress bar animation, if active
252 // skip this if we have a text overlay (too expensive to update)
253 if (self->progressBarType == INDETERMINATE && !self->show_text) {
254 redraw = 1;
255 }
256
257 // move the progress bar forward on timed intervals, if configured
258 int duration = self->progressScopeDuration;
259 if (self->progressBarType == DETERMINATE && duration > 0) {
260 double elapsed = now() - self->progressScopeTime;
261 float progress = 1.0 * elapsed / duration;
262 if (progress > 1.0) progress = 1.0;
263 if (progress > progress) {
264 progress = progress;
265 redraw = 1;
266 }
267 }
268
269 if (redraw) self->update_progress_locked();
270
271 pthread_mutex_unlock(&self->updateMutex);
272 double end = now();
273 // minimum of 20ms delay between frames
274 double delay = interval - (end-start);
275 if (delay < 0.02) delay = 0.02;
276 usleep((long)(delay * 1000000));
277 }
278 return NULL;
279}
280
281int ScreenRecoveryUI::input_callback(int fd, short revents, void* data)
282{
283 struct input_event ev;
284 int ret;
Doug Zongker211aebc2011-10-28 15:13:10 -0700285
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) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700300 self->process_key(KEY_DOWN, 1); // press down key
301 self->process_key(KEY_DOWN, 0); // and release it
Doug Zongker211aebc2011-10-28 15:13:10 -0700302 self->rel_sum = 0;
303 } else if (self->rel_sum < -3) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700304 self->process_key(KEY_UP, 1); // press up key
305 self->process_key(KEY_UP, 0); // and release it
Doug Zongker211aebc2011-10-28 15:13:10 -0700306 self->rel_sum = 0;
307 }
308 }
309 } else {
310 self->rel_sum = 0;
311 }
312
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700313 if (ev.type == EV_KEY && ev.code <= KEY_MAX)
314 self->process_key(ev.code, ev.value);
Doug Zongker211aebc2011-10-28 15:13:10 -0700315
316 return 0;
317}
318
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700319// Process a key-up or -down event. A key is "registered" when it is
320// pressed and then released, with no other keypresses or releases in
321// between. Registered keys are passed to CheckKey() to see if it
322// should trigger a visibility toggle, an immediate reboot, or be
323// queued to be processed next time the foreground thread wants a key
324// (eg, for the menu).
325//
326// We also keep track of which keys are currently down so that
327// CheckKey can call IsKeyPressed to see what other keys are held when
328// a key is registered.
329//
330// updown == 1 for key down events; 0 for key up events
331void ScreenRecoveryUI::process_key(int key_code, int updown) {
332 bool register_key = false;
333
334 pthread_mutex_lock(&key_queue_mutex);
335 key_pressed[key_code] = updown;
336 if (updown) {
337 key_last_down = key_code;
338 } else {
339 if (key_last_down == key_code)
340 register_key = true;
341 key_last_down = -1;
342 }
343 pthread_mutex_unlock(&key_queue_mutex);
344
345 if (register_key) {
346 switch (CheckKey(key_code)) {
347 case RecoveryUI::TOGGLE:
348 pthread_mutex_lock(&updateMutex);
349 show_text = !show_text;
350 if (show_text) show_text_ever = true;
351 update_screen_locked();
352 pthread_mutex_unlock(&updateMutex);
353 break;
354
355 case RecoveryUI::REBOOT:
356 android_reboot(ANDROID_RB_RESTART, 0, 0);
357 break;
358
359 case RecoveryUI::ENQUEUE:
360 pthread_mutex_lock(&key_queue_mutex);
361 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
362 if (key_queue_len < queue_max) {
363 key_queue[key_queue_len++] = key_code;
364 pthread_cond_signal(&key_queue_cond);
365 }
366 pthread_mutex_unlock(&key_queue_mutex);
367 break;
368 }
369 }
370}
371
Doug Zongker211aebc2011-10-28 15:13:10 -0700372// Reads input events, handles special hot keys, and adds to the key queue.
373void* ScreenRecoveryUI::input_thread(void *cookie)
374{
375 for (;;) {
376 if (!ev_wait(-1))
377 ev_dispatch();
378 }
379 return NULL;
380}
381
382void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
383 int result = res_create_surface(filename, surface);
384 if (result < 0) {
385 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
386 }
387}
388
389void ScreenRecoveryUI::Init()
390{
391 gr_init();
392 ev_init(input_callback, NULL);
393
394 text_col = text_row = 0;
395 text_rows = gr_fb_height() / CHAR_HEIGHT;
396 if (text_rows > kMaxRows) text_rows = kMaxRows;
397 text_top = 1;
398
399 text_cols = gr_fb_width() / CHAR_WIDTH;
400 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
401
402 LoadBitmap("icon_installing", &backgroundIcon[INSTALLING]);
403 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
404 LoadBitmap("progress_empty", &progressBarEmpty);
405 LoadBitmap("progress_fill", &progressBarFill);
406
407 int i;
408
409 progressBarIndeterminate = (gr_surface*)malloc(ui_parameters.indeterminate_frames *
410 sizeof(gr_surface));
411 for (i = 0; i < ui_parameters.indeterminate_frames; ++i) {
412 char filename[40];
413 // "indeterminate01.png", "indeterminate02.png", ...
414 sprintf(filename, "indeterminate%02d", i+1);
415 LoadBitmap(filename, progressBarIndeterminate+i);
416 }
417
418 if (ui_parameters.installing_frames > 0) {
419 installationOverlay = (gr_surface*)malloc(ui_parameters.installing_frames *
420 sizeof(gr_surface));
421 for (i = 0; i < ui_parameters.installing_frames; ++i) {
422 char filename[40];
423 // "icon_installing_overlay01.png",
424 // "icon_installing_overlay02.png", ...
425 sprintf(filename, "icon_installing_overlay%02d", i+1);
426 LoadBitmap(filename, installationOverlay+i);
427 }
428
429 // Adjust the offset to account for the positioning of the
430 // base image on the screen.
431 if (backgroundIcon[INSTALLING] != NULL) {
432 gr_surface bg = backgroundIcon[INSTALLING];
433 ui_parameters.install_overlay_offset_x +=
434 (gr_fb_width() - gr_get_width(bg)) / 2;
435 ui_parameters.install_overlay_offset_y +=
436 (gr_fb_height() - gr_get_height(bg)) / 2;
437 }
438 } else {
439 installationOverlay = NULL;
440 }
441
442 pthread_create(&progress_t, NULL, progress_thread, NULL);
443 pthread_create(&input_t, NULL, input_thread, NULL);
444}
445
446void ScreenRecoveryUI::SetBackground(Icon icon)
447{
448 pthread_mutex_lock(&updateMutex);
449 currentIcon = icon;
450 update_screen_locked();
451 pthread_mutex_unlock(&updateMutex);
452}
453
454void ScreenRecoveryUI::SetProgressType(ProgressType type)
455{
456 pthread_mutex_lock(&updateMutex);
457 if (progressBarType != type) {
458 progressBarType = type;
459 update_progress_locked();
460 }
461 pthread_mutex_unlock(&updateMutex);
462}
463
464void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
465{
466 pthread_mutex_lock(&updateMutex);
467 progressBarType = DETERMINATE;
468 progressScopeStart += progressScopeSize;
469 progressScopeSize = portion;
470 progressScopeTime = now();
471 progressScopeDuration = seconds;
472 progress = 0;
473 update_progress_locked();
474 pthread_mutex_unlock(&updateMutex);
475}
476
477void ScreenRecoveryUI::SetProgress(float fraction)
478{
479 pthread_mutex_lock(&updateMutex);
480 if (fraction < 0.0) fraction = 0.0;
481 if (fraction > 1.0) fraction = 1.0;
482 if (progressBarType == DETERMINATE && fraction > progress) {
483 // Skip updates that aren't visibly different.
484 int width = gr_get_width(progressBarIndeterminate[0]);
485 float scale = width * progressScopeSize;
486 if ((int) (progress * scale) != (int) (fraction * scale)) {
487 progress = fraction;
488 update_progress_locked();
489 }
490 }
491 pthread_mutex_unlock(&updateMutex);
492}
493
494void ScreenRecoveryUI::Print(const char *fmt, ...)
495{
496 char buf[256];
497 va_list ap;
498 va_start(ap, fmt);
499 vsnprintf(buf, 256, fmt, ap);
500 va_end(ap);
501
502 fputs(buf, stdout);
503
504 // This can get called before ui_init(), so be careful.
505 pthread_mutex_lock(&updateMutex);
506 if (text_rows > 0 && text_cols > 0) {
507 char *ptr;
508 for (ptr = buf; *ptr != '\0'; ++ptr) {
509 if (*ptr == '\n' || text_col >= text_cols) {
510 text[text_row][text_col] = '\0';
511 text_col = 0;
512 text_row = (text_row + 1) % text_rows;
513 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
514 }
515 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
516 }
517 text[text_row][text_col] = '\0';
518 update_screen_locked();
519 }
520 pthread_mutex_unlock(&updateMutex);
521}
522
523void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
524 int initial_selection) {
525 int i;
526 pthread_mutex_lock(&updateMutex);
527 if (text_rows > 0 && text_cols > 0) {
528 for (i = 0; i < text_rows; ++i) {
529 if (headers[i] == NULL) break;
530 strncpy(menu[i], headers[i], text_cols-1);
531 menu[i][text_cols-1] = '\0';
532 }
533 menu_top = i;
534 for (; i < text_rows; ++i) {
535 if (items[i-menu_top] == NULL) break;
536 strncpy(menu[i], items[i-menu_top], text_cols-1);
537 menu[i][text_cols-1] = '\0';
538 }
539 menu_items = i - menu_top;
540 show_menu = 1;
541 menu_sel = initial_selection;
542 update_screen_locked();
543 }
544 pthread_mutex_unlock(&updateMutex);
545}
546
547int ScreenRecoveryUI::SelectMenu(int sel) {
548 int old_sel;
549 pthread_mutex_lock(&updateMutex);
550 if (show_menu > 0) {
551 old_sel = menu_sel;
552 menu_sel = sel;
553 if (menu_sel < 0) menu_sel = 0;
554 if (menu_sel >= menu_items) menu_sel = menu_items-1;
555 sel = menu_sel;
556 if (menu_sel != old_sel) update_screen_locked();
557 }
558 pthread_mutex_unlock(&updateMutex);
559 return sel;
560}
561
562void ScreenRecoveryUI::EndMenu() {
563 int i;
564 pthread_mutex_lock(&updateMutex);
565 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
566 show_menu = 0;
567 update_screen_locked();
568 }
569 pthread_mutex_unlock(&updateMutex);
570}
571
572bool ScreenRecoveryUI::IsTextVisible()
573{
574 pthread_mutex_lock(&updateMutex);
575 int visible = show_text;
576 pthread_mutex_unlock(&updateMutex);
577 return visible;
578}
579
580bool ScreenRecoveryUI::WasTextEverVisible()
581{
582 pthread_mutex_lock(&updateMutex);
583 int ever_visible = show_text_ever;
584 pthread_mutex_unlock(&updateMutex);
585 return ever_visible;
586}
587
588void ScreenRecoveryUI::ShowText(bool visible)
589{
590 pthread_mutex_lock(&updateMutex);
591 show_text = visible;
592 if (show_text) show_text_ever = 1;
593 update_screen_locked();
594 pthread_mutex_unlock(&updateMutex);
595}
596
597// Return true if USB is connected.
598bool ScreenRecoveryUI::usb_connected() {
599 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
600 if (fd < 0) {
601 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
602 strerror(errno));
603 return 0;
604 }
605
606 char buf;
607 /* USB is connected if android_usb state is CONNECTED or CONFIGURED */
608 int connected = (read(fd, &buf, 1) == 1) && (buf == 'C');
609 if (close(fd) < 0) {
610 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
611 strerror(errno));
612 }
613 return connected;
614}
615
616int ScreenRecoveryUI::WaitKey()
617{
618 pthread_mutex_lock(&key_queue_mutex);
619
620 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
621 // plugged in.
622 do {
623 struct timeval now;
624 struct timespec timeout;
625 gettimeofday(&now, NULL);
626 timeout.tv_sec = now.tv_sec;
627 timeout.tv_nsec = now.tv_usec * 1000;
628 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
629
630 int rc = 0;
631 while (key_queue_len == 0 && rc != ETIMEDOUT) {
632 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex,
633 &timeout);
634 }
635 } while (usb_connected() && key_queue_len == 0);
636
637 int key = -1;
638 if (key_queue_len > 0) {
639 key = key_queue[0];
640 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
641 }
642 pthread_mutex_unlock(&key_queue_mutex);
643 return key;
644}
645
646bool ScreenRecoveryUI::IsKeyPressed(int key)
647{
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700648 pthread_mutex_lock(&key_queue_mutex);
649 int pressed = key_pressed[key];
650 pthread_mutex_unlock(&key_queue_mutex);
651 return pressed;
Doug Zongker211aebc2011-10-28 15:13:10 -0700652}
653
654void ScreenRecoveryUI::FlushKeys() {
655 pthread_mutex_lock(&key_queue_mutex);
656 key_queue_len = 0;
657 pthread_mutex_unlock(&key_queue_mutex);
658}
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700659
660RecoveryUI::KeyAction ScreenRecoveryUI::CheckKey(int key) {
661 return RecoveryUI::ENQUEUE;
662}