| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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" | 
| Doug Zongker | 23412e6 | 2009-07-23 10:16:07 -0700 | [diff] [blame] | 30 | #include "recovery_ui.h" | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 31 |  | 
| Tanguy Pruvot | 67d358c | 2011-06-10 20:26:44 +0200 | [diff] [blame] | 32 | extern int __system(const char *command); | 
|  | 33 |  | 
| Koushik Dutta | d90ad5d | 2010-11-27 14:41:07 -0800 | [diff] [blame] | 34 | #ifdef BOARD_HAS_NO_SELECT_BUTTON | 
| Koushik Dutta | 4ca9b4c | 2010-07-14 18:37:33 -0700 | [diff] [blame] | 35 | static int gShowBackButton = 1; | 
|  | 36 | #else | 
|  | 37 | static int gShowBackButton = 0; | 
|  | 38 | #endif | 
|  | 39 |  | 
| Doug Zongker | 8a8e6cc | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 40 | #define MAX_COLS 96 | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 41 | #define MAX_ROWS 32 | 
|  | 42 |  | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 43 | #define MENU_MAX_COLS 64 | 
|  | 44 | #define MENU_MAX_ROWS 250 | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 45 |  | 
| Ricardo Cerqueira | 9acb022 | 2010-11-08 23:54:30 +0000 | [diff] [blame] | 46 | #ifndef BOARD_LDPI_RECOVERY | 
|  | 47 | #define CHAR_WIDTH 10 | 
|  | 48 | #define CHAR_HEIGHT 18 | 
|  | 49 | #else | 
|  | 50 | #define CHAR_WIDTH 7 | 
|  | 51 | #define CHAR_HEIGHT 16 | 
|  | 52 | #endif | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 53 |  | 
|  | 54 | #define PROGRESSBAR_INDETERMINATE_STATES 6 | 
|  | 55 | #define PROGRESSBAR_INDETERMINATE_FPS 15 | 
|  | 56 |  | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 57 | static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER; | 
|  | 58 | static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS]; | 
|  | 59 | static gr_surface gProgressBarIndeterminate[PROGRESSBAR_INDETERMINATE_STATES]; | 
| Doug Zongker | d93a254 | 2009-10-08 16:32:58 -0700 | [diff] [blame] | 60 | static gr_surface gProgressBarEmpty; | 
|  | 61 | static gr_surface gProgressBarFill; | 
| Koushik Dutta | d3cc60b | 2010-07-03 13:56:45 -0700 | [diff] [blame] | 62 | static int ui_has_initialized = 0; | 
| Tanguy Pruvot | 67d358c | 2011-06-10 20:26:44 +0200 | [diff] [blame] | 63 | static int ui_log_stdout = 1; | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 64 |  | 
|  | 65 | static const struct { gr_surface* surface; const char *name; } BITMAPS[] = { | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 66 | { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" }, | 
|  | 67 | { &gBackgroundIcon[BACKGROUND_ICON_ERROR],      "icon_error" }, | 
| Koushik Dutta | 5d80817 | 2010-12-18 22:29:27 -0800 | [diff] [blame] | 68 | { &gBackgroundIcon[BACKGROUND_ICON_CLOCKWORK],  "icon_clockwork" }, | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 69 | { &gProgressBarIndeterminate[0],    "indeterminate1" }, | 
|  | 70 | { &gProgressBarIndeterminate[1],    "indeterminate2" }, | 
|  | 71 | { &gProgressBarIndeterminate[2],    "indeterminate3" }, | 
|  | 72 | { &gProgressBarIndeterminate[3],    "indeterminate4" }, | 
|  | 73 | { &gProgressBarIndeterminate[4],    "indeterminate5" }, | 
|  | 74 | { &gProgressBarIndeterminate[5],    "indeterminate6" }, | 
| Doug Zongker | d93a254 | 2009-10-08 16:32:58 -0700 | [diff] [blame] | 75 | { &gProgressBarEmpty,               "progress_empty" }, | 
|  | 76 | { &gProgressBarFill,                "progress_fill" }, | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 77 | { NULL,                             NULL }, | 
|  | 78 | }; | 
|  | 79 |  | 
|  | 80 | static gr_surface gCurrentIcon = NULL; | 
|  | 81 |  | 
|  | 82 | static enum ProgressBarType { | 
|  | 83 | PROGRESSBAR_TYPE_NONE, | 
|  | 84 | PROGRESSBAR_TYPE_INDETERMINATE, | 
|  | 85 | PROGRESSBAR_TYPE_NORMAL, | 
|  | 86 | } gProgressBarType = PROGRESSBAR_TYPE_NONE; | 
|  | 87 |  | 
|  | 88 | // Progress bar scope of current operation | 
|  | 89 | static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0; | 
|  | 90 | static time_t gProgressScopeTime, gProgressScopeDuration; | 
|  | 91 |  | 
|  | 92 | // Set to 1 when both graphics pages are the same (except for the progress bar) | 
|  | 93 | static int gPagesIdentical = 0; | 
|  | 94 |  | 
|  | 95 | // Log text overlay, displayed when a magic key is pressed | 
|  | 96 | static char text[MAX_ROWS][MAX_COLS]; | 
|  | 97 | static int text_cols = 0, text_rows = 0; | 
|  | 98 | static int text_col = 0, text_row = 0, text_top = 0; | 
| Ying Wang | 532c860 | 2010-09-01 14:52:22 -0700 | [diff] [blame] | 99 | static int show_text = 0; | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 100 |  | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 101 | static char menu[MENU_MAX_ROWS][MENU_MAX_COLS]; | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 102 | static int show_menu = 0; | 
|  | 103 | static int menu_top = 0, menu_items = 0, menu_sel = 0; | 
| Tanguy Pruvot | 67d358c | 2011-06-10 20:26:44 +0200 | [diff] [blame] | 104 | static int menu_show_start = 0;             // this is line which menu display is starting at | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 105 |  | 
|  | 106 | // Key event input queue | 
|  | 107 | static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER; | 
|  | 108 | static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER; | 
|  | 109 | static int key_queue[256], key_queue_len = 0; | 
|  | 110 | static volatile char key_pressed[KEY_MAX + 1]; | 
|  | 111 |  | 
|  | 112 | // Clear the screen and draw the currently selected background icon (if any). | 
|  | 113 | // Should only be called with gUpdateMutex locked. | 
|  | 114 | static void draw_background_locked(gr_surface icon) | 
|  | 115 | { | 
|  | 116 | gPagesIdentical = 0; | 
|  | 117 | gr_color(0, 0, 0, 255); | 
|  | 118 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); | 
|  | 119 |  | 
|  | 120 | if (icon) { | 
|  | 121 | int iconWidth = gr_get_width(icon); | 
|  | 122 | int iconHeight = gr_get_height(icon); | 
|  | 123 | int iconX = (gr_fb_width() - iconWidth) / 2; | 
|  | 124 | int iconY = (gr_fb_height() - iconHeight) / 2; | 
|  | 125 | gr_blit(icon, 0, 0, iconWidth, iconHeight, iconX, iconY); | 
|  | 126 | } | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | // Draw the progress bar (if any) on the screen.  Does not flip pages. | 
|  | 130 | // Should only be called with gUpdateMutex locked. | 
|  | 131 | static void draw_progress_locked() | 
|  | 132 | { | 
|  | 133 | if (gProgressBarType == PROGRESSBAR_TYPE_NONE) return; | 
|  | 134 |  | 
|  | 135 | int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]); | 
| Doug Zongker | d93a254 | 2009-10-08 16:32:58 -0700 | [diff] [blame] | 136 | int width = gr_get_width(gProgressBarEmpty); | 
|  | 137 | int height = gr_get_height(gProgressBarEmpty); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 138 |  | 
|  | 139 | int dx = (gr_fb_width() - width)/2; | 
|  | 140 | int dy = (3*gr_fb_height() + iconHeight - 2*height)/4; | 
|  | 141 |  | 
|  | 142 | // Erase behind the progress bar (in case this was a progress-only update) | 
|  | 143 | gr_color(0, 0, 0, 255); | 
|  | 144 | gr_fill(dx, dy, width, height); | 
|  | 145 |  | 
|  | 146 | if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) { | 
|  | 147 | float progress = gProgressScopeStart + gProgress * gProgressScopeSize; | 
|  | 148 | int pos = (int) (progress * width); | 
|  | 149 |  | 
| Doug Zongker | d93a254 | 2009-10-08 16:32:58 -0700 | [diff] [blame] | 150 | if (pos > 0) { | 
|  | 151 | gr_blit(gProgressBarFill, 0, 0, pos, height, dx, dy); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 152 | } | 
| Doug Zongker | d93a254 | 2009-10-08 16:32:58 -0700 | [diff] [blame] | 153 | if (pos < width-1) { | 
|  | 154 | gr_blit(gProgressBarEmpty, pos, 0, width-pos, height, dx+pos, dy); | 
|  | 155 | } | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
|  | 158 | if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) { | 
|  | 159 | static int frame = 0; | 
|  | 160 | gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy); | 
|  | 161 | frame = (frame + 1) % PROGRESSBAR_INDETERMINATE_STATES; | 
|  | 162 | } | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | static void 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 |  | 
| Koushik Dutta | 5d80817 | 2010-12-18 22:29:27 -0800 | [diff] [blame] | 171 | #define MENU_TEXT_COLOR 255, 160, 49, 255 | 
| Koushik K. Dutta | 261dde9 | 2010-02-25 16:51:45 -0800 | [diff] [blame] | 172 | #define NORMAL_TEXT_COLOR 200, 200, 200, 255 | 
|  | 173 | #define HEADER_TEXT_COLOR NORMAL_TEXT_COLOR | 
|  | 174 |  | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 175 | // Redraw everything on the screen.  Does not flip pages. | 
|  | 176 | // Should only be called with gUpdateMutex locked. | 
|  | 177 | static void draw_screen_locked(void) | 
|  | 178 | { | 
| Koushik Dutta | d3cc60b | 2010-07-03 13:56:45 -0700 | [diff] [blame] | 179 | if (!ui_has_initialized) return; | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 180 | draw_background_locked(gCurrentIcon); | 
|  | 181 | draw_progress_locked(); | 
|  | 182 |  | 
|  | 183 | if (show_text) { | 
|  | 184 | gr_color(0, 0, 0, 160); | 
|  | 185 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); | 
|  | 186 |  | 
|  | 187 | int i = 0; | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 188 | int j = 0; | 
|  | 189 | int row = 0;            // current row that we are drawing on | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 190 | if (show_menu) { | 
| Koushik K. Dutta | 261dde9 | 2010-02-25 16:51:45 -0800 | [diff] [blame] | 191 | gr_color(MENU_TEXT_COLOR); | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 192 | gr_fill(0, (menu_top + menu_sel - menu_show_start) * CHAR_HEIGHT, | 
|  | 193 | gr_fb_width(), (menu_top + menu_sel - menu_show_start + 1)*CHAR_HEIGHT+1); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 194 |  | 
| Koushik K. Dutta | 261dde9 | 2010-02-25 16:51:45 -0800 | [diff] [blame] | 195 | gr_color(HEADER_TEXT_COLOR); | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 196 | for (i = 0; i < menu_top; ++i) { | 
|  | 197 | draw_text_line(i, menu[i]); | 
|  | 198 | row++; | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | if (menu_items - menu_show_start + menu_top >= MAX_ROWS) | 
|  | 202 | j = MAX_ROWS - menu_top; | 
|  | 203 | else | 
|  | 204 | j = menu_items - menu_show_start; | 
|  | 205 |  | 
|  | 206 | gr_color(MENU_TEXT_COLOR); | 
|  | 207 | for (i = menu_show_start + menu_top; i < (menu_show_start + menu_top + j); ++i) { | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 208 | if (i == menu_top + menu_sel) { | 
|  | 209 | gr_color(255, 255, 255, 255); | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 210 | draw_text_line(i - menu_show_start , menu[i]); | 
| Koushik K. Dutta | 261dde9 | 2010-02-25 16:51:45 -0800 | [diff] [blame] | 211 | gr_color(MENU_TEXT_COLOR); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 212 | } else { | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 213 | gr_color(MENU_TEXT_COLOR); | 
|  | 214 | draw_text_line(i - menu_show_start, menu[i]); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 215 | } | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 216 | row++; | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 217 | } | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 218 | gr_fill(0, row*CHAR_HEIGHT+CHAR_HEIGHT/2-1, | 
|  | 219 | gr_fb_width(), row*CHAR_HEIGHT+CHAR_HEIGHT/2+1); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 220 | } | 
|  | 221 |  | 
| Koushik K. Dutta | 261dde9 | 2010-02-25 16:51:45 -0800 | [diff] [blame] | 222 | gr_color(NORMAL_TEXT_COLOR); | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 223 | for (; row < text_rows; ++row) { | 
|  | 224 | draw_text_line(row, text[(row+text_top) % text_rows]); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 225 | } | 
|  | 226 | } | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | // Redraw everything on the screen and flip the screen (make it visible). | 
|  | 230 | // Should only be called with gUpdateMutex locked. | 
|  | 231 | static void update_screen_locked(void) | 
|  | 232 | { | 
| Koushik Dutta | d3cc60b | 2010-07-03 13:56:45 -0700 | [diff] [blame] | 233 | if (!ui_has_initialized) return; | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 234 | draw_screen_locked(); | 
|  | 235 | gr_flip(); | 
|  | 236 | } | 
|  | 237 |  | 
|  | 238 | // Updates only the progress bar, if possible, otherwise redraws the screen. | 
|  | 239 | // Should only be called with gUpdateMutex locked. | 
|  | 240 | static void update_progress_locked(void) | 
|  | 241 | { | 
| Koushik Dutta | d3cc60b | 2010-07-03 13:56:45 -0700 | [diff] [blame] | 242 | if (!ui_has_initialized) return; | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 243 | if (show_text || !gPagesIdentical) { | 
|  | 244 | draw_screen_locked();    // Must redraw the whole screen | 
|  | 245 | gPagesIdentical = 1; | 
|  | 246 | } else { | 
|  | 247 | draw_progress_locked();  // Draw only the progress bar | 
|  | 248 | } | 
|  | 249 | gr_flip(); | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | // Keeps the progress bar updated, even when the process is otherwise busy. | 
|  | 253 | static void *progress_thread(void *cookie) | 
|  | 254 | { | 
|  | 255 | for (;;) { | 
|  | 256 | usleep(1000000 / PROGRESSBAR_INDETERMINATE_FPS); | 
|  | 257 | pthread_mutex_lock(&gUpdateMutex); | 
|  | 258 |  | 
|  | 259 | // update the progress bar animation, if active | 
|  | 260 | // skip this if we have a text overlay (too expensive to update) | 
|  | 261 | if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) { | 
|  | 262 | update_progress_locked(); | 
|  | 263 | } | 
|  | 264 |  | 
|  | 265 | // move the progress bar forward on timed intervals, if configured | 
|  | 266 | int duration = gProgressScopeDuration; | 
|  | 267 | if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) { | 
|  | 268 | int elapsed = time(NULL) - gProgressScopeTime; | 
|  | 269 | float progress = 1.0 * elapsed / duration; | 
|  | 270 | if (progress > 1.0) progress = 1.0; | 
|  | 271 | if (progress > gProgress) { | 
|  | 272 | gProgress = progress; | 
|  | 273 | update_progress_locked(); | 
|  | 274 | } | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | pthread_mutex_unlock(&gUpdateMutex); | 
|  | 278 | } | 
|  | 279 | return NULL; | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | // Reads input events, handles special hot keys, and adds to the key queue. | 
|  | 283 | static void *input_thread(void *cookie) | 
|  | 284 | { | 
|  | 285 | int rel_sum = 0; | 
|  | 286 | int fake_key = 0; | 
|  | 287 | for (;;) { | 
|  | 288 | // wait for the next key event | 
|  | 289 | struct input_event ev; | 
|  | 290 | do { | 
|  | 291 | ev_get(&ev, 0); | 
|  | 292 |  | 
|  | 293 | if (ev.type == EV_SYN) { | 
|  | 294 | continue; | 
|  | 295 | } else if (ev.type == EV_REL) { | 
|  | 296 | if (ev.code == REL_Y) { | 
|  | 297 | // accumulate the up or down motion reported by | 
|  | 298 | // the trackball.  When it exceeds a threshold | 
|  | 299 | // (positive or negative), fake an up/down | 
|  | 300 | // key event. | 
|  | 301 | rel_sum += ev.value; | 
|  | 302 | if (rel_sum > 3) { | 
|  | 303 | fake_key = 1; | 
|  | 304 | ev.type = EV_KEY; | 
|  | 305 | ev.code = KEY_DOWN; | 
|  | 306 | ev.value = 1; | 
|  | 307 | rel_sum = 0; | 
|  | 308 | } else if (rel_sum < -3) { | 
|  | 309 | fake_key = 1; | 
|  | 310 | ev.type = EV_KEY; | 
|  | 311 | ev.code = KEY_UP; | 
|  | 312 | ev.value = 1; | 
|  | 313 | rel_sum = 0; | 
|  | 314 | } | 
|  | 315 | } | 
|  | 316 | } else { | 
|  | 317 | rel_sum = 0; | 
|  | 318 | } | 
|  | 319 | } while (ev.type != EV_KEY || ev.code > KEY_MAX); | 
|  | 320 |  | 
|  | 321 | pthread_mutex_lock(&key_queue_mutex); | 
|  | 322 | if (!fake_key) { | 
|  | 323 | // our "fake" keys only report a key-down event (no | 
|  | 324 | // key-up), so don't record them in the key_pressed | 
|  | 325 | // table. | 
|  | 326 | key_pressed[ev.code] = ev.value; | 
|  | 327 | } | 
|  | 328 | fake_key = 0; | 
|  | 329 | const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]); | 
|  | 330 | if (ev.value > 0 && key_queue_len < queue_max) { | 
|  | 331 | key_queue[key_queue_len++] = ev.code; | 
|  | 332 | pthread_cond_signal(&key_queue_cond); | 
|  | 333 | } | 
|  | 334 | pthread_mutex_unlock(&key_queue_mutex); | 
|  | 335 |  | 
| Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 336 | if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) { | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 337 | pthread_mutex_lock(&gUpdateMutex); | 
|  | 338 | show_text = !show_text; | 
|  | 339 | update_screen_locked(); | 
|  | 340 | pthread_mutex_unlock(&gUpdateMutex); | 
|  | 341 | } | 
|  | 342 |  | 
| Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 343 | if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) { | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 344 | reboot(RB_AUTOBOOT); | 
|  | 345 | } | 
|  | 346 | } | 
|  | 347 | return NULL; | 
|  | 348 | } | 
|  | 349 |  | 
|  | 350 | void ui_init(void) | 
|  | 351 | { | 
| Koushik Dutta | d3cc60b | 2010-07-03 13:56:45 -0700 | [diff] [blame] | 352 | ui_has_initialized = 1; | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 353 | gr_init(); | 
|  | 354 | ev_init(); | 
|  | 355 |  | 
|  | 356 | text_col = text_row = 0; | 
|  | 357 | text_rows = gr_fb_height() / CHAR_HEIGHT; | 
|  | 358 | if (text_rows > MAX_ROWS) text_rows = MAX_ROWS; | 
|  | 359 | text_top = 1; | 
|  | 360 |  | 
|  | 361 | text_cols = gr_fb_width() / CHAR_WIDTH; | 
|  | 362 | if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1; | 
|  | 363 |  | 
|  | 364 | int i; | 
|  | 365 | for (i = 0; BITMAPS[i].name != NULL; ++i) { | 
|  | 366 | int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface); | 
|  | 367 | if (result < 0) { | 
| Doug Zongker | 196c25c | 2009-09-15 08:50:04 -0700 | [diff] [blame] | 368 | if (result == -2) { | 
|  | 369 | LOGI("Bitmap %s missing header\n", BITMAPS[i].name); | 
|  | 370 | } else { | 
|  | 371 | LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result); | 
|  | 372 | } | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 373 | *BITMAPS[i].surface = NULL; | 
|  | 374 | } | 
|  | 375 | } | 
|  | 376 |  | 
|  | 377 | pthread_t t; | 
|  | 378 | pthread_create(&t, NULL, progress_thread, NULL); | 
|  | 379 | pthread_create(&t, NULL, input_thread, NULL); | 
|  | 380 | } | 
|  | 381 |  | 
| Koushik Dutta | 107629b | 2010-06-30 23:17:53 -0700 | [diff] [blame] | 382 | char *ui_copy_image(int icon, int *width, int *height, int *bpp) { | 
|  | 383 | pthread_mutex_lock(&gUpdateMutex); | 
|  | 384 | draw_background_locked(gBackgroundIcon[icon]); | 
|  | 385 | *width = gr_fb_width(); | 
|  | 386 | *height = gr_fb_height(); | 
|  | 387 | *bpp = sizeof(gr_pixel) * 8; | 
|  | 388 | int size = *width * *height * sizeof(gr_pixel); | 
|  | 389 | char *ret = malloc(size); | 
|  | 390 | if (ret == NULL) { | 
|  | 391 | LOGE("Can't allocate %d bytes for image\n", size); | 
|  | 392 | } else { | 
|  | 393 | memcpy(ret, gr_fb_data(), size); | 
|  | 394 | } | 
|  | 395 | pthread_mutex_unlock(&gUpdateMutex); | 
|  | 396 | return ret; | 
|  | 397 | } | 
|  | 398 |  | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 399 | void ui_set_background(int icon) | 
|  | 400 | { | 
|  | 401 | pthread_mutex_lock(&gUpdateMutex); | 
|  | 402 | gCurrentIcon = gBackgroundIcon[icon]; | 
|  | 403 | update_screen_locked(); | 
|  | 404 | pthread_mutex_unlock(&gUpdateMutex); | 
|  | 405 | } | 
|  | 406 |  | 
|  | 407 | void ui_show_indeterminate_progress() | 
|  | 408 | { | 
|  | 409 | pthread_mutex_lock(&gUpdateMutex); | 
|  | 410 | if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) { | 
|  | 411 | gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE; | 
|  | 412 | update_progress_locked(); | 
|  | 413 | } | 
|  | 414 | pthread_mutex_unlock(&gUpdateMutex); | 
|  | 415 | } | 
|  | 416 |  | 
|  | 417 | void ui_show_progress(float portion, int seconds) | 
|  | 418 | { | 
|  | 419 | pthread_mutex_lock(&gUpdateMutex); | 
|  | 420 | gProgressBarType = PROGRESSBAR_TYPE_NORMAL; | 
|  | 421 | gProgressScopeStart += gProgressScopeSize; | 
|  | 422 | gProgressScopeSize = portion; | 
|  | 423 | gProgressScopeTime = time(NULL); | 
|  | 424 | gProgressScopeDuration = seconds; | 
|  | 425 | gProgress = 0; | 
|  | 426 | update_progress_locked(); | 
|  | 427 | pthread_mutex_unlock(&gUpdateMutex); | 
|  | 428 | } | 
|  | 429 |  | 
|  | 430 | void ui_set_progress(float fraction) | 
|  | 431 | { | 
|  | 432 | pthread_mutex_lock(&gUpdateMutex); | 
|  | 433 | if (fraction < 0.0) fraction = 0.0; | 
|  | 434 | if (fraction > 1.0) fraction = 1.0; | 
|  | 435 | if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) { | 
|  | 436 | // Skip updates that aren't visibly different. | 
|  | 437 | int width = gr_get_width(gProgressBarIndeterminate[0]); | 
|  | 438 | float scale = width * gProgressScopeSize; | 
|  | 439 | if ((int) (gProgress * scale) != (int) (fraction * scale)) { | 
|  | 440 | gProgress = fraction; | 
|  | 441 | update_progress_locked(); | 
|  | 442 | } | 
|  | 443 | } | 
|  | 444 | pthread_mutex_unlock(&gUpdateMutex); | 
|  | 445 | } | 
|  | 446 |  | 
|  | 447 | void ui_reset_progress() | 
|  | 448 | { | 
|  | 449 | pthread_mutex_lock(&gUpdateMutex); | 
|  | 450 | gProgressBarType = PROGRESSBAR_TYPE_NONE; | 
|  | 451 | gProgressScopeStart = gProgressScopeSize = 0; | 
|  | 452 | gProgressScopeTime = gProgressScopeDuration = 0; | 
|  | 453 | gProgress = 0; | 
|  | 454 | update_screen_locked(); | 
|  | 455 | pthread_mutex_unlock(&gUpdateMutex); | 
|  | 456 | } | 
|  | 457 |  | 
|  | 458 | void ui_print(const char *fmt, ...) | 
|  | 459 | { | 
|  | 460 | char buf[256]; | 
|  | 461 | va_list ap; | 
|  | 462 | va_start(ap, fmt); | 
|  | 463 | vsnprintf(buf, 256, fmt, ap); | 
|  | 464 | va_end(ap); | 
|  | 465 |  | 
| Tanguy Pruvot | 67d358c | 2011-06-10 20:26:44 +0200 | [diff] [blame] | 466 | if (ui_log_stdout) | 
|  | 467 | fputs(buf, stdout); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 468 |  | 
|  | 469 | // This can get called before ui_init(), so be careful. | 
|  | 470 | pthread_mutex_lock(&gUpdateMutex); | 
|  | 471 | if (text_rows > 0 && text_cols > 0) { | 
|  | 472 | char *ptr; | 
|  | 473 | for (ptr = buf; *ptr != '\0'; ++ptr) { | 
|  | 474 | if (*ptr == '\n' || text_col >= text_cols) { | 
|  | 475 | text[text_row][text_col] = '\0'; | 
|  | 476 | text_col = 0; | 
|  | 477 | text_row = (text_row + 1) % text_rows; | 
|  | 478 | if (text_row == text_top) text_top = (text_top + 1) % text_rows; | 
|  | 479 | } | 
|  | 480 | if (*ptr != '\n') text[text_row][text_col++] = *ptr; | 
|  | 481 | } | 
|  | 482 | text[text_row][text_col] = '\0'; | 
|  | 483 | update_screen_locked(); | 
|  | 484 | } | 
|  | 485 | pthread_mutex_unlock(&gUpdateMutex); | 
|  | 486 | } | 
|  | 487 |  | 
| Tanguy Pruvot | 67d358c | 2011-06-10 20:26:44 +0200 | [diff] [blame] | 488 | void ui_printlogtail(int nb_lines) { | 
|  | 489 | char * log_data; | 
|  | 490 | char tmp[PATH_MAX]; | 
|  | 491 | FILE * f; | 
|  | 492 | int line=0; | 
|  | 493 | //don't log output to recovery.log | 
|  | 494 | ui_log_stdout=0; | 
|  | 495 | sprintf(tmp, "tail -n %d /tmp/recovery.log > /tmp/tail.log", nb_lines); | 
|  | 496 | __system(tmp); | 
|  | 497 | f = fopen("/tmp/tail.log", "rb"); | 
|  | 498 | if (f != NULL) { | 
|  | 499 | while (line < nb_lines) { | 
|  | 500 | log_data = fgets(tmp, PATH_MAX, f); | 
|  | 501 | if (log_data == NULL) break; | 
|  | 502 | ui_print("%s", tmp); | 
|  | 503 | line++; | 
|  | 504 | } | 
|  | 505 | fclose(f); | 
|  | 506 | } | 
|  | 507 | ui_log_stdout=1; | 
|  | 508 | } | 
|  | 509 |  | 
| Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 510 | void ui_reset_text_col() | 
|  | 511 | { | 
|  | 512 | pthread_mutex_lock(&gUpdateMutex); | 
|  | 513 | text_col = 0; | 
|  | 514 | pthread_mutex_unlock(&gUpdateMutex); | 
|  | 515 | } | 
|  | 516 |  | 
| Koushik K. Dutta | 261dde9 | 2010-02-25 16:51:45 -0800 | [diff] [blame] | 517 | #define MENU_ITEM_HEADER " - " | 
|  | 518 | #define MENU_ITEM_HEADER_LENGTH strlen(MENU_ITEM_HEADER) | 
|  | 519 |  | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 520 | int ui_start_menu(char** headers, char** items, int initial_selection) { | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 521 | int i; | 
|  | 522 | pthread_mutex_lock(&gUpdateMutex); | 
|  | 523 | if (text_rows > 0 && text_cols > 0) { | 
|  | 524 | for (i = 0; i < text_rows; ++i) { | 
|  | 525 | if (headers[i] == NULL) break; | 
|  | 526 | strncpy(menu[i], headers[i], text_cols-1); | 
|  | 527 | menu[i][text_cols-1] = '\0'; | 
|  | 528 | } | 
|  | 529 | menu_top = i; | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 530 | for (; i < MENU_MAX_ROWS; ++i) { | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 531 | if (items[i-menu_top] == NULL) break; | 
| Koushik K. Dutta | 261dde9 | 2010-02-25 16:51:45 -0800 | [diff] [blame] | 532 | strcpy(menu[i], MENU_ITEM_HEADER); | 
|  | 533 | strncpy(menu[i] + MENU_ITEM_HEADER_LENGTH, items[i-menu_top], text_cols-1 - MENU_ITEM_HEADER_LENGTH); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 534 | menu[i][text_cols-1] = '\0'; | 
|  | 535 | } | 
| Koushik Dutta | 4ca9b4c | 2010-07-14 18:37:33 -0700 | [diff] [blame] | 536 |  | 
|  | 537 | if (gShowBackButton) { | 
|  | 538 | strcpy(menu[i], " - +++++Go Back+++++"); | 
|  | 539 | ++i; | 
|  | 540 | } | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 541 |  | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 542 | menu_items = i - menu_top; | 
|  | 543 | show_menu = 1; | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 544 | menu_sel = menu_show_start = initial_selection; | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 545 | update_screen_locked(); | 
|  | 546 | } | 
|  | 547 | pthread_mutex_unlock(&gUpdateMutex); | 
| Koushik Dutta | 4ca9b4c | 2010-07-14 18:37:33 -0700 | [diff] [blame] | 548 | if (gShowBackButton) { | 
|  | 549 | return menu_items - 1; | 
|  | 550 | } | 
| Koushik Dutta | f4e3a67 | 2010-06-09 12:19:41 -0700 | [diff] [blame] | 551 | return menu_items; | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 552 | } | 
|  | 553 |  | 
|  | 554 | int ui_menu_select(int sel) { | 
|  | 555 | int old_sel; | 
|  | 556 | pthread_mutex_lock(&gUpdateMutex); | 
|  | 557 | if (show_menu > 0) { | 
|  | 558 | old_sel = menu_sel; | 
|  | 559 | menu_sel = sel; | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 560 |  | 
| Koushik K. Dutta | 707fa6d | 2010-03-23 11:44:33 -0700 | [diff] [blame] | 561 | if (menu_sel < 0) menu_sel = menu_items + menu_sel; | 
|  | 562 | if (menu_sel >= menu_items) menu_sel = menu_sel - menu_items; | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 563 |  | 
|  | 564 |  | 
|  | 565 | if (menu_sel < menu_show_start && menu_show_start > 0) { | 
| Koushik Dutta | 917c38a | 2010-05-24 16:43:39 -0700 | [diff] [blame] | 566 | menu_show_start = menu_sel; | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 567 | } | 
|  | 568 |  | 
|  | 569 | if (menu_sel - menu_show_start + menu_top >= text_rows) { | 
| Koushik Dutta | 917c38a | 2010-05-24 16:43:39 -0700 | [diff] [blame] | 570 | menu_show_start = menu_sel + menu_top - text_rows + 1; | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 571 | } | 
|  | 572 |  | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 573 | sel = menu_sel; | 
| Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 574 |  | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 575 | if (menu_sel != old_sel) update_screen_locked(); | 
|  | 576 | } | 
|  | 577 | pthread_mutex_unlock(&gUpdateMutex); | 
|  | 578 | return sel; | 
|  | 579 | } | 
|  | 580 |  | 
|  | 581 | void ui_end_menu() { | 
|  | 582 | int i; | 
|  | 583 | pthread_mutex_lock(&gUpdateMutex); | 
|  | 584 | if (show_menu > 0 && text_rows > 0 && text_cols > 0) { | 
|  | 585 | show_menu = 0; | 
|  | 586 | update_screen_locked(); | 
|  | 587 | } | 
|  | 588 | pthread_mutex_unlock(&gUpdateMutex); | 
|  | 589 | } | 
|  | 590 |  | 
|  | 591 | int ui_text_visible() | 
|  | 592 | { | 
|  | 593 | pthread_mutex_lock(&gUpdateMutex); | 
|  | 594 | int visible = show_text; | 
|  | 595 | pthread_mutex_unlock(&gUpdateMutex); | 
|  | 596 | return visible; | 
|  | 597 | } | 
|  | 598 |  | 
| Doug Zongker | 4bc9806 | 2010-09-03 11:00:13 -0700 | [diff] [blame] | 599 | void ui_show_text(int visible) | 
|  | 600 | { | 
|  | 601 | pthread_mutex_lock(&gUpdateMutex); | 
|  | 602 | show_text = visible; | 
|  | 603 | update_screen_locked(); | 
|  | 604 | pthread_mutex_unlock(&gUpdateMutex); | 
|  | 605 | } | 
|  | 606 |  | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 607 | int ui_wait_key() | 
|  | 608 | { | 
|  | 609 | pthread_mutex_lock(&key_queue_mutex); | 
|  | 610 | while (key_queue_len == 0) { | 
|  | 611 | pthread_cond_wait(&key_queue_cond, &key_queue_mutex); | 
|  | 612 | } | 
|  | 613 |  | 
|  | 614 | int key = key_queue[0]; | 
|  | 615 | memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len); | 
|  | 616 | pthread_mutex_unlock(&key_queue_mutex); | 
|  | 617 | return key; | 
|  | 618 | } | 
|  | 619 |  | 
|  | 620 | int ui_key_pressed(int key) | 
|  | 621 | { | 
|  | 622 | // This is a volatile static array, don't bother locking | 
|  | 623 | return key_pressed[key]; | 
|  | 624 | } | 
|  | 625 |  | 
|  | 626 | void ui_clear_key_queue() { | 
|  | 627 | pthread_mutex_lock(&key_queue_mutex); | 
|  | 628 | key_queue_len = 0; | 
|  | 629 | pthread_mutex_unlock(&key_queue_mutex); | 
|  | 630 | } | 
| Koushik K. Dutta | a3c2f73 | 2010-02-19 14:17:22 -0800 | [diff] [blame] | 631 |  | 
| Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 632 | void ui_set_show_text(int value) { | 
|  | 633 | show_text = value; | 
| Koushik K. Dutta | a3c2f73 | 2010-02-19 14:17:22 -0800 | [diff] [blame] | 634 | } | 
| Koushik Dutta | 4ca9b4c | 2010-07-14 18:37:33 -0700 | [diff] [blame] | 635 |  | 
|  | 636 | void ui_set_showing_back_button(int showBackButton) { | 
|  | 637 | gShowBackButton = showBackButton; | 
|  | 638 | } | 
|  | 639 |  | 
|  | 640 | int ui_get_showing_back_button() { | 
|  | 641 | return gShowBackButton; | 
|  | 642 | } |