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