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