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 | |
| 32 | #define MAX_COLS 64 |
| 33 | #define MAX_ROWS 32 |
| 34 | |
| 35 | #define CHAR_WIDTH 10 |
| 36 | #define CHAR_HEIGHT 18 |
| 37 | |
| 38 | #define PROGRESSBAR_INDETERMINATE_STATES 6 |
| 39 | #define PROGRESSBAR_INDETERMINATE_FPS 15 |
| 40 | |
| 41 | enum { LEFT_SIDE, CENTER_TILE, RIGHT_SIDE, NUM_SIDES }; |
| 42 | |
| 43 | static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER; |
| 44 | static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS]; |
| 45 | static gr_surface gProgressBarIndeterminate[PROGRESSBAR_INDETERMINATE_STATES]; |
| 46 | static gr_surface gProgressBarEmpty[NUM_SIDES]; |
| 47 | static gr_surface gProgressBarFill[NUM_SIDES]; |
| 48 | |
| 49 | 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] | 50 | { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" }, |
| 51 | { &gBackgroundIcon[BACKGROUND_ICON_ERROR], "icon_error" }, |
| 52 | { &gBackgroundIcon[BACKGROUND_ICON_FIRMWARE_INSTALLING], |
| 53 | "icon_firmware_install" }, |
| 54 | { &gBackgroundIcon[BACKGROUND_ICON_FIRMWARE_ERROR], |
| 55 | "icon_firmware_error" }, |
| 56 | { &gProgressBarIndeterminate[0], "indeterminate1" }, |
| 57 | { &gProgressBarIndeterminate[1], "indeterminate2" }, |
| 58 | { &gProgressBarIndeterminate[2], "indeterminate3" }, |
| 59 | { &gProgressBarIndeterminate[3], "indeterminate4" }, |
| 60 | { &gProgressBarIndeterminate[4], "indeterminate5" }, |
| 61 | { &gProgressBarIndeterminate[5], "indeterminate6" }, |
| 62 | { &gProgressBarEmpty[LEFT_SIDE], "progress_bar_empty_left_round" }, |
| 63 | { &gProgressBarEmpty[CENTER_TILE], "progress_bar_empty" }, |
| 64 | { &gProgressBarEmpty[RIGHT_SIDE], "progress_bar_empty_right_round" }, |
| 65 | { &gProgressBarFill[LEFT_SIDE], "progress_bar_left_round" }, |
| 66 | { &gProgressBarFill[CENTER_TILE], "progress_bar_fill" }, |
| 67 | { &gProgressBarFill[RIGHT_SIDE], "progress_bar_right_round" }, |
| 68 | { NULL, NULL }, |
| 69 | }; |
| 70 | |
| 71 | static gr_surface gCurrentIcon = NULL; |
| 72 | |
| 73 | static enum ProgressBarType { |
| 74 | PROGRESSBAR_TYPE_NONE, |
| 75 | PROGRESSBAR_TYPE_INDETERMINATE, |
| 76 | PROGRESSBAR_TYPE_NORMAL, |
| 77 | } gProgressBarType = PROGRESSBAR_TYPE_NONE; |
| 78 | |
| 79 | // Progress bar scope of current operation |
| 80 | static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0; |
| 81 | static time_t gProgressScopeTime, gProgressScopeDuration; |
| 82 | |
| 83 | // Set to 1 when both graphics pages are the same (except for the progress bar) |
| 84 | static int gPagesIdentical = 0; |
| 85 | |
| 86 | // Log text overlay, displayed when a magic key is pressed |
| 87 | static char text[MAX_ROWS][MAX_COLS]; |
| 88 | static int text_cols = 0, text_rows = 0; |
| 89 | static int text_col = 0, text_row = 0, text_top = 0; |
| 90 | static int show_text = 0; |
| 91 | |
| 92 | static char menu[MAX_ROWS][MAX_COLS]; |
| 93 | static int show_menu = 0; |
| 94 | static int menu_top = 0, menu_items = 0, menu_sel = 0; |
| 95 | |
| 96 | // Key event input queue |
| 97 | static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 98 | static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER; |
| 99 | static int key_queue[256], key_queue_len = 0; |
| 100 | static volatile char key_pressed[KEY_MAX + 1]; |
| 101 | |
| 102 | // Clear the screen and draw the currently selected background icon (if any). |
| 103 | // Should only be called with gUpdateMutex locked. |
| 104 | static void draw_background_locked(gr_surface icon) |
| 105 | { |
| 106 | gPagesIdentical = 0; |
| 107 | gr_color(0, 0, 0, 255); |
| 108 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 109 | |
| 110 | if (icon) { |
| 111 | int iconWidth = gr_get_width(icon); |
| 112 | int iconHeight = gr_get_height(icon); |
| 113 | int iconX = (gr_fb_width() - iconWidth) / 2; |
| 114 | int iconY = (gr_fb_height() - iconHeight) / 2; |
| 115 | gr_blit(icon, 0, 0, iconWidth, iconHeight, iconX, iconY); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Draw the progress bar (if any) on the screen. Does not flip pages. |
| 120 | // Should only be called with gUpdateMutex locked. |
| 121 | static void draw_progress_locked() |
| 122 | { |
| 123 | if (gProgressBarType == PROGRESSBAR_TYPE_NONE) return; |
| 124 | |
| 125 | int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]); |
| 126 | int width = gr_get_width(gProgressBarIndeterminate[0]); |
| 127 | int height = gr_get_height(gProgressBarIndeterminate[0]); |
| 128 | |
| 129 | int dx = (gr_fb_width() - width)/2; |
| 130 | int dy = (3*gr_fb_height() + iconHeight - 2*height)/4; |
| 131 | |
| 132 | // Erase behind the progress bar (in case this was a progress-only update) |
| 133 | gr_color(0, 0, 0, 255); |
| 134 | gr_fill(dx, dy, width, height); |
| 135 | |
| 136 | if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) { |
| 137 | float progress = gProgressScopeStart + gProgress * gProgressScopeSize; |
| 138 | int pos = (int) (progress * width); |
| 139 | |
| 140 | gr_surface s = (pos ? gProgressBarFill : gProgressBarEmpty)[LEFT_SIDE]; |
| 141 | gr_blit(s, 0, 0, gr_get_width(s), gr_get_height(s), dx, dy); |
| 142 | |
| 143 | int x = gr_get_width(s); |
| 144 | while (x + (int) gr_get_width(gProgressBarEmpty[RIGHT_SIDE]) < width) { |
| 145 | s = (pos > x ? gProgressBarFill : gProgressBarEmpty)[CENTER_TILE]; |
| 146 | gr_blit(s, 0, 0, gr_get_width(s), gr_get_height(s), dx + x, dy); |
| 147 | x += gr_get_width(s); |
| 148 | } |
| 149 | |
| 150 | s = (pos > x ? gProgressBarFill : gProgressBarEmpty)[RIGHT_SIDE]; |
| 151 | gr_blit(s, 0, 0, gr_get_width(s), gr_get_height(s), dx + x, dy); |
| 152 | } |
| 153 | |
| 154 | if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) { |
| 155 | static int frame = 0; |
| 156 | gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy); |
| 157 | frame = (frame + 1) % PROGRESSBAR_INDETERMINATE_STATES; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | static void draw_text_line(int row, const char* t) { |
| 162 | if (t[0] != '\0') { |
| 163 | gr_text(0, (row+1)*CHAR_HEIGHT-1, t); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Redraw everything on the screen. Does not flip pages. |
| 168 | // Should only be called with gUpdateMutex locked. |
| 169 | static void draw_screen_locked(void) |
| 170 | { |
| 171 | draw_background_locked(gCurrentIcon); |
| 172 | draw_progress_locked(); |
| 173 | |
| 174 | if (show_text) { |
| 175 | gr_color(0, 0, 0, 160); |
| 176 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 177 | |
| 178 | int i = 0; |
| 179 | if (show_menu) { |
| 180 | gr_color(64, 96, 255, 255); |
| 181 | gr_fill(0, (menu_top+menu_sel) * CHAR_HEIGHT, |
| 182 | gr_fb_width(), (menu_top+menu_sel+1)*CHAR_HEIGHT+1); |
| 183 | |
| 184 | for (; i < menu_top + menu_items; ++i) { |
| 185 | if (i == menu_top + menu_sel) { |
| 186 | gr_color(255, 255, 255, 255); |
| 187 | draw_text_line(i, menu[i]); |
| 188 | gr_color(64, 96, 255, 255); |
| 189 | } else { |
| 190 | draw_text_line(i, menu[i]); |
| 191 | } |
| 192 | } |
| 193 | gr_fill(0, i*CHAR_HEIGHT+CHAR_HEIGHT/2-1, |
| 194 | gr_fb_width(), i*CHAR_HEIGHT+CHAR_HEIGHT/2+1); |
| 195 | ++i; |
| 196 | } |
| 197 | |
| 198 | gr_color(255, 255, 0, 255); |
| 199 | |
| 200 | for (; i < text_rows; ++i) { |
| 201 | draw_text_line(i, text[(i+text_top) % text_rows]); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Redraw everything on the screen and flip the screen (make it visible). |
| 207 | // Should only be called with gUpdateMutex locked. |
| 208 | static void update_screen_locked(void) |
| 209 | { |
| 210 | draw_screen_locked(); |
| 211 | gr_flip(); |
| 212 | } |
| 213 | |
| 214 | // Updates only the progress bar, if possible, otherwise redraws the screen. |
| 215 | // Should only be called with gUpdateMutex locked. |
| 216 | static void update_progress_locked(void) |
| 217 | { |
| 218 | if (show_text || !gPagesIdentical) { |
| 219 | draw_screen_locked(); // Must redraw the whole screen |
| 220 | gPagesIdentical = 1; |
| 221 | } else { |
| 222 | draw_progress_locked(); // Draw only the progress bar |
| 223 | } |
| 224 | gr_flip(); |
| 225 | } |
| 226 | |
| 227 | // Keeps the progress bar updated, even when the process is otherwise busy. |
| 228 | static void *progress_thread(void *cookie) |
| 229 | { |
| 230 | for (;;) { |
| 231 | usleep(1000000 / PROGRESSBAR_INDETERMINATE_FPS); |
| 232 | pthread_mutex_lock(&gUpdateMutex); |
| 233 | |
| 234 | // update the progress bar animation, if active |
| 235 | // skip this if we have a text overlay (too expensive to update) |
| 236 | if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) { |
| 237 | update_progress_locked(); |
| 238 | } |
| 239 | |
| 240 | // move the progress bar forward on timed intervals, if configured |
| 241 | int duration = gProgressScopeDuration; |
| 242 | if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) { |
| 243 | int elapsed = time(NULL) - gProgressScopeTime; |
| 244 | float progress = 1.0 * elapsed / duration; |
| 245 | if (progress > 1.0) progress = 1.0; |
| 246 | if (progress > gProgress) { |
| 247 | gProgress = progress; |
| 248 | update_progress_locked(); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | pthread_mutex_unlock(&gUpdateMutex); |
| 253 | } |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | // Reads input events, handles special hot keys, and adds to the key queue. |
| 258 | static void *input_thread(void *cookie) |
| 259 | { |
| 260 | int rel_sum = 0; |
| 261 | int fake_key = 0; |
| 262 | for (;;) { |
| 263 | // wait for the next key event |
| 264 | struct input_event ev; |
| 265 | do { |
| 266 | ev_get(&ev, 0); |
| 267 | |
| 268 | if (ev.type == EV_SYN) { |
| 269 | continue; |
| 270 | } else if (ev.type == EV_REL) { |
| 271 | if (ev.code == REL_Y) { |
| 272 | // accumulate the up or down motion reported by |
| 273 | // the trackball. When it exceeds a threshold |
| 274 | // (positive or negative), fake an up/down |
| 275 | // key event. |
| 276 | rel_sum += ev.value; |
| 277 | if (rel_sum > 3) { |
| 278 | fake_key = 1; |
| 279 | ev.type = EV_KEY; |
| 280 | ev.code = KEY_DOWN; |
| 281 | ev.value = 1; |
| 282 | rel_sum = 0; |
| 283 | } else if (rel_sum < -3) { |
| 284 | fake_key = 1; |
| 285 | ev.type = EV_KEY; |
| 286 | ev.code = KEY_UP; |
| 287 | ev.value = 1; |
| 288 | rel_sum = 0; |
| 289 | } |
| 290 | } |
| 291 | } else { |
| 292 | rel_sum = 0; |
| 293 | } |
| 294 | } while (ev.type != EV_KEY || ev.code > KEY_MAX); |
| 295 | |
| 296 | pthread_mutex_lock(&key_queue_mutex); |
| 297 | if (!fake_key) { |
| 298 | // our "fake" keys only report a key-down event (no |
| 299 | // key-up), so don't record them in the key_pressed |
| 300 | // table. |
| 301 | key_pressed[ev.code] = ev.value; |
| 302 | } |
| 303 | fake_key = 0; |
| 304 | const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]); |
| 305 | if (ev.value > 0 && key_queue_len < queue_max) { |
| 306 | key_queue[key_queue_len++] = ev.code; |
| 307 | pthread_cond_signal(&key_queue_cond); |
| 308 | } |
| 309 | pthread_mutex_unlock(&key_queue_mutex); |
| 310 | |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 311 | 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] | 312 | pthread_mutex_lock(&gUpdateMutex); |
| 313 | show_text = !show_text; |
| 314 | update_screen_locked(); |
| 315 | pthread_mutex_unlock(&gUpdateMutex); |
| 316 | } |
| 317 | |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 318 | 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] | 319 | reboot(RB_AUTOBOOT); |
| 320 | } |
| 321 | } |
| 322 | return NULL; |
| 323 | } |
| 324 | |
| 325 | void ui_init(void) |
| 326 | { |
| 327 | gr_init(); |
| 328 | ev_init(); |
| 329 | |
| 330 | text_col = text_row = 0; |
| 331 | text_rows = gr_fb_height() / CHAR_HEIGHT; |
| 332 | if (text_rows > MAX_ROWS) text_rows = MAX_ROWS; |
| 333 | text_top = 1; |
| 334 | |
| 335 | text_cols = gr_fb_width() / CHAR_WIDTH; |
| 336 | if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1; |
| 337 | |
| 338 | int i; |
| 339 | for (i = 0; BITMAPS[i].name != NULL; ++i) { |
| 340 | int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface); |
| 341 | if (result < 0) { |
| 342 | LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result); |
| 343 | *BITMAPS[i].surface = NULL; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | pthread_t t; |
| 348 | pthread_create(&t, NULL, progress_thread, NULL); |
| 349 | pthread_create(&t, NULL, input_thread, NULL); |
| 350 | } |
| 351 | |
| 352 | char *ui_copy_image(int icon, int *width, int *height, int *bpp) { |
| 353 | pthread_mutex_lock(&gUpdateMutex); |
| 354 | draw_background_locked(gBackgroundIcon[icon]); |
| 355 | *width = gr_fb_width(); |
| 356 | *height = gr_fb_height(); |
| 357 | *bpp = sizeof(gr_pixel) * 8; |
| 358 | int size = *width * *height * sizeof(gr_pixel); |
| 359 | char *ret = malloc(size); |
| 360 | if (ret == NULL) { |
| 361 | LOGE("Can't allocate %d bytes for image\n", size); |
| 362 | } else { |
| 363 | memcpy(ret, gr_fb_data(), size); |
| 364 | } |
| 365 | pthread_mutex_unlock(&gUpdateMutex); |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | void ui_set_background(int icon) |
| 370 | { |
| 371 | pthread_mutex_lock(&gUpdateMutex); |
| 372 | gCurrentIcon = gBackgroundIcon[icon]; |
| 373 | update_screen_locked(); |
| 374 | pthread_mutex_unlock(&gUpdateMutex); |
| 375 | } |
| 376 | |
| 377 | void ui_show_indeterminate_progress() |
| 378 | { |
| 379 | pthread_mutex_lock(&gUpdateMutex); |
| 380 | if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) { |
| 381 | gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE; |
| 382 | update_progress_locked(); |
| 383 | } |
| 384 | pthread_mutex_unlock(&gUpdateMutex); |
| 385 | } |
| 386 | |
| 387 | void ui_show_progress(float portion, int seconds) |
| 388 | { |
| 389 | pthread_mutex_lock(&gUpdateMutex); |
| 390 | gProgressBarType = PROGRESSBAR_TYPE_NORMAL; |
| 391 | gProgressScopeStart += gProgressScopeSize; |
| 392 | gProgressScopeSize = portion; |
| 393 | gProgressScopeTime = time(NULL); |
| 394 | gProgressScopeDuration = seconds; |
| 395 | gProgress = 0; |
| 396 | update_progress_locked(); |
| 397 | pthread_mutex_unlock(&gUpdateMutex); |
| 398 | } |
| 399 | |
| 400 | void ui_set_progress(float fraction) |
| 401 | { |
| 402 | pthread_mutex_lock(&gUpdateMutex); |
| 403 | if (fraction < 0.0) fraction = 0.0; |
| 404 | if (fraction > 1.0) fraction = 1.0; |
| 405 | if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) { |
| 406 | // Skip updates that aren't visibly different. |
| 407 | int width = gr_get_width(gProgressBarIndeterminate[0]); |
| 408 | float scale = width * gProgressScopeSize; |
| 409 | if ((int) (gProgress * scale) != (int) (fraction * scale)) { |
| 410 | gProgress = fraction; |
| 411 | update_progress_locked(); |
| 412 | } |
| 413 | } |
| 414 | pthread_mutex_unlock(&gUpdateMutex); |
| 415 | } |
| 416 | |
| 417 | void ui_reset_progress() |
| 418 | { |
| 419 | pthread_mutex_lock(&gUpdateMutex); |
| 420 | gProgressBarType = PROGRESSBAR_TYPE_NONE; |
| 421 | gProgressScopeStart = gProgressScopeSize = 0; |
| 422 | gProgressScopeTime = gProgressScopeDuration = 0; |
| 423 | gProgress = 0; |
| 424 | update_screen_locked(); |
| 425 | pthread_mutex_unlock(&gUpdateMutex); |
| 426 | } |
| 427 | |
| 428 | void ui_print(const char *fmt, ...) |
| 429 | { |
| 430 | char buf[256]; |
| 431 | va_list ap; |
| 432 | va_start(ap, fmt); |
| 433 | vsnprintf(buf, 256, fmt, ap); |
| 434 | va_end(ap); |
| 435 | |
| 436 | fputs(buf, stderr); |
| 437 | |
| 438 | // This can get called before ui_init(), so be careful. |
| 439 | pthread_mutex_lock(&gUpdateMutex); |
| 440 | if (text_rows > 0 && text_cols > 0) { |
| 441 | char *ptr; |
| 442 | for (ptr = buf; *ptr != '\0'; ++ptr) { |
| 443 | if (*ptr == '\n' || text_col >= text_cols) { |
| 444 | text[text_row][text_col] = '\0'; |
| 445 | text_col = 0; |
| 446 | text_row = (text_row + 1) % text_rows; |
| 447 | if (text_row == text_top) text_top = (text_top + 1) % text_rows; |
| 448 | } |
| 449 | if (*ptr != '\n') text[text_row][text_col++] = *ptr; |
| 450 | } |
| 451 | text[text_row][text_col] = '\0'; |
| 452 | update_screen_locked(); |
| 453 | } |
| 454 | pthread_mutex_unlock(&gUpdateMutex); |
| 455 | } |
| 456 | |
| 457 | void ui_start_menu(char** headers, char** items) { |
| 458 | int i; |
| 459 | pthread_mutex_lock(&gUpdateMutex); |
| 460 | if (text_rows > 0 && text_cols > 0) { |
| 461 | for (i = 0; i < text_rows; ++i) { |
| 462 | if (headers[i] == NULL) break; |
| 463 | strncpy(menu[i], headers[i], text_cols-1); |
| 464 | menu[i][text_cols-1] = '\0'; |
| 465 | } |
| 466 | menu_top = i; |
| 467 | for (; i < text_rows; ++i) { |
| 468 | if (items[i-menu_top] == NULL) break; |
| 469 | strncpy(menu[i], items[i-menu_top], text_cols-1); |
| 470 | menu[i][text_cols-1] = '\0'; |
| 471 | } |
| 472 | menu_items = i - menu_top; |
| 473 | show_menu = 1; |
| 474 | menu_sel = 0; |
| 475 | update_screen_locked(); |
| 476 | } |
| 477 | pthread_mutex_unlock(&gUpdateMutex); |
| 478 | } |
| 479 | |
| 480 | int ui_menu_select(int sel) { |
| 481 | int old_sel; |
| 482 | pthread_mutex_lock(&gUpdateMutex); |
| 483 | if (show_menu > 0) { |
| 484 | old_sel = menu_sel; |
| 485 | menu_sel = sel; |
| 486 | if (menu_sel < 0) menu_sel = 0; |
| 487 | if (menu_sel >= menu_items) menu_sel = menu_items-1; |
| 488 | sel = menu_sel; |
| 489 | if (menu_sel != old_sel) update_screen_locked(); |
| 490 | } |
| 491 | pthread_mutex_unlock(&gUpdateMutex); |
| 492 | return sel; |
| 493 | } |
| 494 | |
| 495 | void ui_end_menu() { |
| 496 | int i; |
| 497 | pthread_mutex_lock(&gUpdateMutex); |
| 498 | if (show_menu > 0 && text_rows > 0 && text_cols > 0) { |
| 499 | show_menu = 0; |
| 500 | update_screen_locked(); |
| 501 | } |
| 502 | pthread_mutex_unlock(&gUpdateMutex); |
| 503 | } |
| 504 | |
| 505 | int ui_text_visible() |
| 506 | { |
| 507 | pthread_mutex_lock(&gUpdateMutex); |
| 508 | int visible = show_text; |
| 509 | pthread_mutex_unlock(&gUpdateMutex); |
| 510 | return visible; |
| 511 | } |
| 512 | |
| 513 | int ui_wait_key() |
| 514 | { |
| 515 | pthread_mutex_lock(&key_queue_mutex); |
| 516 | while (key_queue_len == 0) { |
| 517 | pthread_cond_wait(&key_queue_cond, &key_queue_mutex); |
| 518 | } |
| 519 | |
| 520 | int key = key_queue[0]; |
| 521 | memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len); |
| 522 | pthread_mutex_unlock(&key_queue_mutex); |
| 523 | return key; |
| 524 | } |
| 525 | |
| 526 | int ui_key_pressed(int key) |
| 527 | { |
| 528 | // This is a volatile static array, don't bother locking |
| 529 | return key_pressed[key]; |
| 530 | } |
| 531 | |
| 532 | void ui_clear_key_queue() { |
| 533 | pthread_mutex_lock(&key_queue_mutex); |
| 534 | key_queue_len = 0; |
| 535 | pthread_mutex_unlock(&key_queue_mutex); |
| 536 | } |