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[] = { |
| 49 | { &gBackgroundIcon[BACKGROUND_ICON_UNPACKING], "icon_unpacking" }, |
| 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 | |
| 311 | // Alt+L or Home+End: toggle log display |
| 312 | int alt = key_pressed[KEY_LEFTALT] || key_pressed[KEY_RIGHTALT]; |
| 313 | if ((alt && ev.code == KEY_L && ev.value > 0) || |
| 314 | (key_pressed[KEY_HOME] && ev.code == KEY_END && ev.value > 0)) { |
| 315 | pthread_mutex_lock(&gUpdateMutex); |
| 316 | show_text = !show_text; |
| 317 | update_screen_locked(); |
| 318 | pthread_mutex_unlock(&gUpdateMutex); |
| 319 | } |
| 320 | |
| 321 | // Green+Menu+Red: reboot immediately |
| 322 | if (ev.code == KEY_DREAM_RED && |
| 323 | key_pressed[KEY_DREAM_MENU] && |
| 324 | key_pressed[KEY_DREAM_GREEN]) { |
| 325 | reboot(RB_AUTOBOOT); |
| 326 | } |
| 327 | } |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | void ui_init(void) |
| 332 | { |
| 333 | gr_init(); |
| 334 | ev_init(); |
| 335 | |
| 336 | text_col = text_row = 0; |
| 337 | text_rows = gr_fb_height() / CHAR_HEIGHT; |
| 338 | if (text_rows > MAX_ROWS) text_rows = MAX_ROWS; |
| 339 | text_top = 1; |
| 340 | |
| 341 | text_cols = gr_fb_width() / CHAR_WIDTH; |
| 342 | if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1; |
| 343 | |
| 344 | int i; |
| 345 | for (i = 0; BITMAPS[i].name != NULL; ++i) { |
| 346 | int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface); |
| 347 | if (result < 0) { |
| 348 | LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result); |
| 349 | *BITMAPS[i].surface = NULL; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | pthread_t t; |
| 354 | pthread_create(&t, NULL, progress_thread, NULL); |
| 355 | pthread_create(&t, NULL, input_thread, NULL); |
| 356 | } |
| 357 | |
| 358 | char *ui_copy_image(int icon, int *width, int *height, int *bpp) { |
| 359 | pthread_mutex_lock(&gUpdateMutex); |
| 360 | draw_background_locked(gBackgroundIcon[icon]); |
| 361 | *width = gr_fb_width(); |
| 362 | *height = gr_fb_height(); |
| 363 | *bpp = sizeof(gr_pixel) * 8; |
| 364 | int size = *width * *height * sizeof(gr_pixel); |
| 365 | char *ret = malloc(size); |
| 366 | if (ret == NULL) { |
| 367 | LOGE("Can't allocate %d bytes for image\n", size); |
| 368 | } else { |
| 369 | memcpy(ret, gr_fb_data(), size); |
| 370 | } |
| 371 | pthread_mutex_unlock(&gUpdateMutex); |
| 372 | return ret; |
| 373 | } |
| 374 | |
| 375 | void ui_set_background(int icon) |
| 376 | { |
| 377 | pthread_mutex_lock(&gUpdateMutex); |
| 378 | gCurrentIcon = gBackgroundIcon[icon]; |
| 379 | update_screen_locked(); |
| 380 | pthread_mutex_unlock(&gUpdateMutex); |
| 381 | } |
| 382 | |
| 383 | void ui_show_indeterminate_progress() |
| 384 | { |
| 385 | pthread_mutex_lock(&gUpdateMutex); |
| 386 | if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) { |
| 387 | gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE; |
| 388 | update_progress_locked(); |
| 389 | } |
| 390 | pthread_mutex_unlock(&gUpdateMutex); |
| 391 | } |
| 392 | |
| 393 | void ui_show_progress(float portion, int seconds) |
| 394 | { |
| 395 | pthread_mutex_lock(&gUpdateMutex); |
| 396 | gProgressBarType = PROGRESSBAR_TYPE_NORMAL; |
| 397 | gProgressScopeStart += gProgressScopeSize; |
| 398 | gProgressScopeSize = portion; |
| 399 | gProgressScopeTime = time(NULL); |
| 400 | gProgressScopeDuration = seconds; |
| 401 | gProgress = 0; |
| 402 | update_progress_locked(); |
| 403 | pthread_mutex_unlock(&gUpdateMutex); |
| 404 | } |
| 405 | |
| 406 | void ui_set_progress(float fraction) |
| 407 | { |
| 408 | pthread_mutex_lock(&gUpdateMutex); |
| 409 | if (fraction < 0.0) fraction = 0.0; |
| 410 | if (fraction > 1.0) fraction = 1.0; |
| 411 | if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) { |
| 412 | // Skip updates that aren't visibly different. |
| 413 | int width = gr_get_width(gProgressBarIndeterminate[0]); |
| 414 | float scale = width * gProgressScopeSize; |
| 415 | if ((int) (gProgress * scale) != (int) (fraction * scale)) { |
| 416 | gProgress = fraction; |
| 417 | update_progress_locked(); |
| 418 | } |
| 419 | } |
| 420 | pthread_mutex_unlock(&gUpdateMutex); |
| 421 | } |
| 422 | |
| 423 | void ui_reset_progress() |
| 424 | { |
| 425 | pthread_mutex_lock(&gUpdateMutex); |
| 426 | gProgressBarType = PROGRESSBAR_TYPE_NONE; |
| 427 | gProgressScopeStart = gProgressScopeSize = 0; |
| 428 | gProgressScopeTime = gProgressScopeDuration = 0; |
| 429 | gProgress = 0; |
| 430 | update_screen_locked(); |
| 431 | pthread_mutex_unlock(&gUpdateMutex); |
| 432 | } |
| 433 | |
| 434 | void ui_print(const char *fmt, ...) |
| 435 | { |
| 436 | char buf[256]; |
| 437 | va_list ap; |
| 438 | va_start(ap, fmt); |
| 439 | vsnprintf(buf, 256, fmt, ap); |
| 440 | va_end(ap); |
| 441 | |
| 442 | fputs(buf, stderr); |
| 443 | |
| 444 | // This can get called before ui_init(), so be careful. |
| 445 | pthread_mutex_lock(&gUpdateMutex); |
| 446 | if (text_rows > 0 && text_cols > 0) { |
| 447 | char *ptr; |
| 448 | for (ptr = buf; *ptr != '\0'; ++ptr) { |
| 449 | if (*ptr == '\n' || text_col >= text_cols) { |
| 450 | text[text_row][text_col] = '\0'; |
| 451 | text_col = 0; |
| 452 | text_row = (text_row + 1) % text_rows; |
| 453 | if (text_row == text_top) text_top = (text_top + 1) % text_rows; |
| 454 | } |
| 455 | if (*ptr != '\n') text[text_row][text_col++] = *ptr; |
| 456 | } |
| 457 | text[text_row][text_col] = '\0'; |
| 458 | update_screen_locked(); |
| 459 | } |
| 460 | pthread_mutex_unlock(&gUpdateMutex); |
| 461 | } |
| 462 | |
| 463 | void ui_start_menu(char** headers, char** items) { |
| 464 | int i; |
| 465 | pthread_mutex_lock(&gUpdateMutex); |
| 466 | if (text_rows > 0 && text_cols > 0) { |
| 467 | for (i = 0; i < text_rows; ++i) { |
| 468 | if (headers[i] == NULL) break; |
| 469 | strncpy(menu[i], headers[i], text_cols-1); |
| 470 | menu[i][text_cols-1] = '\0'; |
| 471 | } |
| 472 | menu_top = i; |
| 473 | for (; i < text_rows; ++i) { |
| 474 | if (items[i-menu_top] == NULL) break; |
| 475 | strncpy(menu[i], items[i-menu_top], text_cols-1); |
| 476 | menu[i][text_cols-1] = '\0'; |
| 477 | } |
| 478 | menu_items = i - menu_top; |
| 479 | show_menu = 1; |
| 480 | menu_sel = 0; |
| 481 | update_screen_locked(); |
| 482 | } |
| 483 | pthread_mutex_unlock(&gUpdateMutex); |
| 484 | } |
| 485 | |
| 486 | int ui_menu_select(int sel) { |
| 487 | int old_sel; |
| 488 | pthread_mutex_lock(&gUpdateMutex); |
| 489 | if (show_menu > 0) { |
| 490 | old_sel = menu_sel; |
| 491 | menu_sel = sel; |
| 492 | if (menu_sel < 0) menu_sel = 0; |
| 493 | if (menu_sel >= menu_items) menu_sel = menu_items-1; |
| 494 | sel = menu_sel; |
| 495 | if (menu_sel != old_sel) update_screen_locked(); |
| 496 | } |
| 497 | pthread_mutex_unlock(&gUpdateMutex); |
| 498 | return sel; |
| 499 | } |
| 500 | |
| 501 | void ui_end_menu() { |
| 502 | int i; |
| 503 | pthread_mutex_lock(&gUpdateMutex); |
| 504 | if (show_menu > 0 && text_rows > 0 && text_cols > 0) { |
| 505 | show_menu = 0; |
| 506 | update_screen_locked(); |
| 507 | } |
| 508 | pthread_mutex_unlock(&gUpdateMutex); |
| 509 | } |
| 510 | |
| 511 | int ui_text_visible() |
| 512 | { |
| 513 | pthread_mutex_lock(&gUpdateMutex); |
| 514 | int visible = show_text; |
| 515 | pthread_mutex_unlock(&gUpdateMutex); |
| 516 | return visible; |
| 517 | } |
| 518 | |
| 519 | int ui_wait_key() |
| 520 | { |
| 521 | pthread_mutex_lock(&key_queue_mutex); |
| 522 | while (key_queue_len == 0) { |
| 523 | pthread_cond_wait(&key_queue_cond, &key_queue_mutex); |
| 524 | } |
| 525 | |
| 526 | int key = key_queue[0]; |
| 527 | memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len); |
| 528 | pthread_mutex_unlock(&key_queue_mutex); |
| 529 | return key; |
| 530 | } |
| 531 | |
| 532 | int ui_key_pressed(int key) |
| 533 | { |
| 534 | // This is a volatile static array, don't bother locking |
| 535 | return key_pressed[key]; |
| 536 | } |
| 537 | |
| 538 | void ui_clear_key_queue() { |
| 539 | pthread_mutex_lock(&key_queue_mutex); |
| 540 | key_queue_len = 0; |
| 541 | pthread_mutex_unlock(&key_queue_mutex); |
| 542 | } |