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