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" |
Jean-Baptiste Queru | a43c44f | 2009-11-12 18:45:15 -0800 | [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 | |
Jean-Baptiste Queru | a43c44f | 2009-11-12 18:45:15 -0800 | [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 | |
Jean-Baptiste Queru | a43c44f | 2009-11-12 18:45:15 -0800 | [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) { |
Jean-Baptiste Queru | a43c44f | 2009-11-12 18:45:15 -0800 | [diff] [blame] | 342 | if (result == -2) { |
| 343 | LOGI("Bitmap %s missing header\n", BITMAPS[i].name); |
| 344 | } else { |
| 345 | LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result); |
| 346 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 347 | *BITMAPS[i].surface = NULL; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | pthread_t t; |
| 352 | pthread_create(&t, NULL, progress_thread, NULL); |
| 353 | pthread_create(&t, NULL, input_thread, NULL); |
| 354 | } |
| 355 | |
| 356 | char *ui_copy_image(int icon, int *width, int *height, int *bpp) { |
| 357 | pthread_mutex_lock(&gUpdateMutex); |
| 358 | draw_background_locked(gBackgroundIcon[icon]); |
| 359 | *width = gr_fb_width(); |
| 360 | *height = gr_fb_height(); |
| 361 | *bpp = sizeof(gr_pixel) * 8; |
| 362 | int size = *width * *height * sizeof(gr_pixel); |
| 363 | char *ret = malloc(size); |
| 364 | if (ret == NULL) { |
| 365 | LOGE("Can't allocate %d bytes for image\n", size); |
| 366 | } else { |
| 367 | memcpy(ret, gr_fb_data(), size); |
| 368 | } |
| 369 | pthread_mutex_unlock(&gUpdateMutex); |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | void ui_set_background(int icon) |
| 374 | { |
| 375 | pthread_mutex_lock(&gUpdateMutex); |
| 376 | gCurrentIcon = gBackgroundIcon[icon]; |
| 377 | update_screen_locked(); |
| 378 | pthread_mutex_unlock(&gUpdateMutex); |
| 379 | } |
| 380 | |
| 381 | void ui_show_indeterminate_progress() |
| 382 | { |
| 383 | pthread_mutex_lock(&gUpdateMutex); |
| 384 | if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) { |
| 385 | gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE; |
| 386 | update_progress_locked(); |
| 387 | } |
| 388 | pthread_mutex_unlock(&gUpdateMutex); |
| 389 | } |
| 390 | |
| 391 | void ui_show_progress(float portion, int seconds) |
| 392 | { |
| 393 | pthread_mutex_lock(&gUpdateMutex); |
| 394 | gProgressBarType = PROGRESSBAR_TYPE_NORMAL; |
| 395 | gProgressScopeStart += gProgressScopeSize; |
| 396 | gProgressScopeSize = portion; |
| 397 | gProgressScopeTime = time(NULL); |
| 398 | gProgressScopeDuration = seconds; |
| 399 | gProgress = 0; |
| 400 | update_progress_locked(); |
| 401 | pthread_mutex_unlock(&gUpdateMutex); |
| 402 | } |
| 403 | |
| 404 | void ui_set_progress(float fraction) |
| 405 | { |
| 406 | pthread_mutex_lock(&gUpdateMutex); |
| 407 | if (fraction < 0.0) fraction = 0.0; |
| 408 | if (fraction > 1.0) fraction = 1.0; |
| 409 | if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) { |
| 410 | // Skip updates that aren't visibly different. |
| 411 | int width = gr_get_width(gProgressBarIndeterminate[0]); |
| 412 | float scale = width * gProgressScopeSize; |
| 413 | if ((int) (gProgress * scale) != (int) (fraction * scale)) { |
| 414 | gProgress = fraction; |
| 415 | update_progress_locked(); |
| 416 | } |
| 417 | } |
| 418 | pthread_mutex_unlock(&gUpdateMutex); |
| 419 | } |
| 420 | |
| 421 | void ui_reset_progress() |
| 422 | { |
| 423 | pthread_mutex_lock(&gUpdateMutex); |
| 424 | gProgressBarType = PROGRESSBAR_TYPE_NONE; |
| 425 | gProgressScopeStart = gProgressScopeSize = 0; |
| 426 | gProgressScopeTime = gProgressScopeDuration = 0; |
| 427 | gProgress = 0; |
| 428 | update_screen_locked(); |
| 429 | pthread_mutex_unlock(&gUpdateMutex); |
| 430 | } |
| 431 | |
| 432 | void ui_print(const char *fmt, ...) |
| 433 | { |
| 434 | char buf[256]; |
| 435 | va_list ap; |
| 436 | va_start(ap, fmt); |
| 437 | vsnprintf(buf, 256, fmt, ap); |
| 438 | va_end(ap); |
| 439 | |
| 440 | fputs(buf, stderr); |
| 441 | |
| 442 | // This can get called before ui_init(), so be careful. |
| 443 | pthread_mutex_lock(&gUpdateMutex); |
| 444 | if (text_rows > 0 && text_cols > 0) { |
| 445 | char *ptr; |
| 446 | for (ptr = buf; *ptr != '\0'; ++ptr) { |
| 447 | if (*ptr == '\n' || text_col >= text_cols) { |
| 448 | text[text_row][text_col] = '\0'; |
| 449 | text_col = 0; |
| 450 | text_row = (text_row + 1) % text_rows; |
| 451 | if (text_row == text_top) text_top = (text_top + 1) % text_rows; |
| 452 | } |
| 453 | if (*ptr != '\n') text[text_row][text_col++] = *ptr; |
| 454 | } |
| 455 | text[text_row][text_col] = '\0'; |
| 456 | update_screen_locked(); |
| 457 | } |
| 458 | pthread_mutex_unlock(&gUpdateMutex); |
| 459 | } |
| 460 | |
| 461 | void ui_start_menu(char** headers, char** items) { |
| 462 | int i; |
| 463 | pthread_mutex_lock(&gUpdateMutex); |
| 464 | if (text_rows > 0 && text_cols > 0) { |
| 465 | for (i = 0; i < text_rows; ++i) { |
| 466 | if (headers[i] == NULL) break; |
| 467 | strncpy(menu[i], headers[i], text_cols-1); |
| 468 | menu[i][text_cols-1] = '\0'; |
| 469 | } |
| 470 | menu_top = i; |
| 471 | for (; i < text_rows; ++i) { |
| 472 | if (items[i-menu_top] == NULL) break; |
| 473 | strncpy(menu[i], items[i-menu_top], text_cols-1); |
| 474 | menu[i][text_cols-1] = '\0'; |
| 475 | } |
| 476 | menu_items = i - menu_top; |
| 477 | show_menu = 1; |
| 478 | menu_sel = 0; |
| 479 | update_screen_locked(); |
| 480 | } |
| 481 | pthread_mutex_unlock(&gUpdateMutex); |
| 482 | } |
| 483 | |
| 484 | int ui_menu_select(int sel) { |
| 485 | int old_sel; |
| 486 | pthread_mutex_lock(&gUpdateMutex); |
| 487 | if (show_menu > 0) { |
| 488 | old_sel = menu_sel; |
| 489 | menu_sel = sel; |
| 490 | if (menu_sel < 0) menu_sel = 0; |
| 491 | if (menu_sel >= menu_items) menu_sel = menu_items-1; |
| 492 | sel = menu_sel; |
| 493 | if (menu_sel != old_sel) update_screen_locked(); |
| 494 | } |
| 495 | pthread_mutex_unlock(&gUpdateMutex); |
| 496 | return sel; |
| 497 | } |
| 498 | |
| 499 | void ui_end_menu() { |
| 500 | int i; |
| 501 | pthread_mutex_lock(&gUpdateMutex); |
| 502 | if (show_menu > 0 && text_rows > 0 && text_cols > 0) { |
| 503 | show_menu = 0; |
| 504 | update_screen_locked(); |
| 505 | } |
| 506 | pthread_mutex_unlock(&gUpdateMutex); |
| 507 | } |
| 508 | |
| 509 | int ui_text_visible() |
| 510 | { |
| 511 | pthread_mutex_lock(&gUpdateMutex); |
| 512 | int visible = show_text; |
| 513 | pthread_mutex_unlock(&gUpdateMutex); |
| 514 | return visible; |
| 515 | } |
| 516 | |
| 517 | int ui_wait_key() |
| 518 | { |
| 519 | pthread_mutex_lock(&key_queue_mutex); |
| 520 | while (key_queue_len == 0) { |
| 521 | pthread_cond_wait(&key_queue_cond, &key_queue_mutex); |
| 522 | } |
| 523 | |
| 524 | int key = key_queue[0]; |
| 525 | memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len); |
| 526 | pthread_mutex_unlock(&key_queue_mutex); |
| 527 | return key; |
| 528 | } |
| 529 | |
| 530 | int ui_key_pressed(int key) |
| 531 | { |
| 532 | // This is a volatile static array, don't bother locking |
| 533 | return key_pressed[key]; |
| 534 | } |
| 535 | |
| 536 | void ui_clear_key_queue() { |
| 537 | pthread_mutex_lock(&key_queue_mutex); |
| 538 | key_queue_len = 0; |
| 539 | pthread_mutex_unlock(&key_queue_mutex); |
| 540 | } |