1 /* 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" 30 #include "recovery_ui.h" 31 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 41 static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER; 42 static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS]; 43 static gr_surface gProgressBarIndeterminate[PROGRESSBAR_INDETERMINATE_STATES]; 44 static gr_surface gProgressBarEmpty; 45 static gr_surface gProgressBarFill; 46 47 static const struct { gr_surface* surface; const char *name; } BITMAPS[] = { 48 { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" }, 49 { &gBackgroundIcon[BACKGROUND_ICON_ERROR], "icon_error" }, 50 { &gProgressBarIndeterminate[0], "indeterminate1" }, 51 { &gProgressBarIndeterminate[1], "indeterminate2" }, 52 { &gProgressBarIndeterminate[2], "indeterminate3" }, 53 { &gProgressBarIndeterminate[3], "indeterminate4" }, 54 { &gProgressBarIndeterminate[4], "indeterminate5" }, 55 { &gProgressBarIndeterminate[5], "indeterminate6" }, 56 { &gProgressBarEmpty, "progress_empty" }, 57 { &gProgressBarFill, "progress_fill" }, 58 { NULL, NULL }, 59 }; 60 61 static gr_surface gCurrentIcon = NULL; 62 63 static 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 70 static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0; 71 static time_t gProgressScopeTime, gProgressScopeDuration; 72 73 // Set to 1 when both graphics pages are the same (except for the progress bar) 74 static int gPagesIdentical = 0; 75 76 // Log text overlay, displayed when a magic key is pressed 77 static char text[MAX_ROWS][MAX_COLS]; 78 static int text_cols = 0, text_rows = 0; 79 static int text_col = 0, text_row = 0, text_top = 0; 80 static int show_text = 0; 81 82 static char menu[MAX_ROWS][MAX_COLS]; 83 static int show_menu = 0; 84 static int menu_top = 0, menu_items = 0, menu_sel = 0; 85 86 // Key event input queue 87 static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER; 88 static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER; 89 static int key_queue[256], key_queue_len = 0; 90 static 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. 94 static 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. 111 static void draw_progress_locked() 112 { 113 if (gProgressBarType == PROGRESSBAR_TYPE_NONE) return; 114 115 int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]); 116 int width = gr_get_width(gProgressBarEmpty); 117 int height = gr_get_height(gProgressBarEmpty); 118 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 130 if (pos > 0) { 131 gr_blit(gProgressBarFill, 0, 0, pos, height, dx, dy); 132 } 133 if (pos < width-1) { 134 gr_blit(gProgressBarEmpty, pos, 0, width-pos, height, dx+pos, dy); 135 } 136 } 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 145 static 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. 153 static 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. 192 static 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. 200 static 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. 212 static 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. 242 static 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 295 if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) { 296 pthread_mutex_lock(&gUpdateMutex); 297 show_text = !show_text; 298 update_screen_locked(); 299 pthread_mutex_unlock(&gUpdateMutex); 300 } 301 302 if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) { 303 reboot(RB_AUTOBOOT); 304 } 305 } 306 return NULL; 307 } 308 309 void 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) { 326 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 } 331 *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 340 void 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 348 void 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 358 void 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 371 void 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 388 void 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 399 void 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 407 fputs(buf, stderr); 408 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 428 void ui_start_menu(char** headers, char** items) { 429 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; 445 menu_sel = 0; 446 update_screen_locked(); 447 } 448 pthread_mutex_unlock(&gUpdateMutex); 449 } 450 451 int 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 466 void 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 476 int ui_text_visible() 477 { 478 pthread_mutex_lock(&gUpdateMutex); 479 int visible = show_text; 480 pthread_mutex_unlock(&gUpdateMutex); 481 return visible; 482 } 483 484 int 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 497 int ui_key_pressed(int key) 498 { 499 // This is a volatile static array, don't bother locking 500 return key_pressed[key]; 501 } 502 503 void ui_clear_key_queue() { 504 pthread_mutex_lock(&key_queue_mutex); 505 key_queue_len = 0; 506 pthread_mutex_unlock(&key_queue_mutex); 507 } 508