blob: 5ea4e42e072c2ed38ae68c7b45488f2193605e03 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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 Zongker23412e62009-07-23 10:16:07 -070030#include "recovery_ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080031
32#define MAX_COLS 64
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +010033#define MAX_ROWS 32
34
35#define MENU_MAX_COLS 64
36#define MENU_MAX_ROWS 250
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037
38#define CHAR_WIDTH 10
39#define CHAR_HEIGHT 18
40
41#define PROGRESSBAR_INDETERMINATE_STATES 6
42#define PROGRESSBAR_INDETERMINATE_FPS 15
43
44enum { LEFT_SIDE, CENTER_TILE, RIGHT_SIDE, NUM_SIDES };
45
46static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER;
47static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS];
48static gr_surface gProgressBarIndeterminate[PROGRESSBAR_INDETERMINATE_STATES];
49static gr_surface gProgressBarEmpty[NUM_SIDES];
50static gr_surface gProgressBarFill[NUM_SIDES];
51
52static const struct { gr_surface* surface; const char *name; } BITMAPS[] = {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080053 { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" },
54 { &gBackgroundIcon[BACKGROUND_ICON_ERROR], "icon_error" },
55 { &gBackgroundIcon[BACKGROUND_ICON_FIRMWARE_INSTALLING],
56 "icon_firmware_install" },
57 { &gBackgroundIcon[BACKGROUND_ICON_FIRMWARE_ERROR],
58 "icon_firmware_error" },
59 { &gProgressBarIndeterminate[0], "indeterminate1" },
60 { &gProgressBarIndeterminate[1], "indeterminate2" },
61 { &gProgressBarIndeterminate[2], "indeterminate3" },
62 { &gProgressBarIndeterminate[3], "indeterminate4" },
63 { &gProgressBarIndeterminate[4], "indeterminate5" },
64 { &gProgressBarIndeterminate[5], "indeterminate6" },
65 { &gProgressBarEmpty[LEFT_SIDE], "progress_bar_empty_left_round" },
66 { &gProgressBarEmpty[CENTER_TILE], "progress_bar_empty" },
67 { &gProgressBarEmpty[RIGHT_SIDE], "progress_bar_empty_right_round" },
68 { &gProgressBarFill[LEFT_SIDE], "progress_bar_left_round" },
69 { &gProgressBarFill[CENTER_TILE], "progress_bar_fill" },
70 { &gProgressBarFill[RIGHT_SIDE], "progress_bar_right_round" },
71 { NULL, NULL },
72};
73
74static gr_surface gCurrentIcon = NULL;
75
76static enum ProgressBarType {
77 PROGRESSBAR_TYPE_NONE,
78 PROGRESSBAR_TYPE_INDETERMINATE,
79 PROGRESSBAR_TYPE_NORMAL,
80} gProgressBarType = PROGRESSBAR_TYPE_NONE;
81
82// Progress bar scope of current operation
83static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
84static time_t gProgressScopeTime, gProgressScopeDuration;
85
86// Set to 1 when both graphics pages are the same (except for the progress bar)
87static int gPagesIdentical = 0;
88
89// Log text overlay, displayed when a magic key is pressed
90static char text[MAX_ROWS][MAX_COLS];
91static int text_cols = 0, text_rows = 0;
92static int text_col = 0, text_row = 0, text_top = 0;
93static int show_text = 0;
94
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +010095static char menu[MENU_MAX_ROWS][MENU_MAX_COLS];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080096static int show_menu = 0;
97static int menu_top = 0, menu_items = 0, menu_sel = 0;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +010098static int menu_show_start = 0; // this is line which menu display is starting at
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080099
100// Key event input queue
101static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
102static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER;
103static int key_queue[256], key_queue_len = 0;
104static volatile char key_pressed[KEY_MAX + 1];
105
106// Clear the screen and draw the currently selected background icon (if any).
107// Should only be called with gUpdateMutex locked.
108static void draw_background_locked(gr_surface icon)
109{
110 gPagesIdentical = 0;
111 gr_color(0, 0, 0, 255);
112 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
113
114 if (icon) {
115 int iconWidth = gr_get_width(icon);
116 int iconHeight = gr_get_height(icon);
117 int iconX = (gr_fb_width() - iconWidth) / 2;
118 int iconY = (gr_fb_height() - iconHeight) / 2;
119 gr_blit(icon, 0, 0, iconWidth, iconHeight, iconX, iconY);
120 }
121}
122
123// Draw the progress bar (if any) on the screen. Does not flip pages.
124// Should only be called with gUpdateMutex locked.
125static void draw_progress_locked()
126{
127 if (gProgressBarType == PROGRESSBAR_TYPE_NONE) return;
128
129 int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]);
130 int width = gr_get_width(gProgressBarIndeterminate[0]);
131 int height = gr_get_height(gProgressBarIndeterminate[0]);
132
133 int dx = (gr_fb_width() - width)/2;
134 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
135
136 // Erase behind the progress bar (in case this was a progress-only update)
137 gr_color(0, 0, 0, 255);
138 gr_fill(dx, dy, width, height);
139
140 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) {
141 float progress = gProgressScopeStart + gProgress * gProgressScopeSize;
142 int pos = (int) (progress * width);
143
144 gr_surface s = (pos ? gProgressBarFill : gProgressBarEmpty)[LEFT_SIDE];
145 gr_blit(s, 0, 0, gr_get_width(s), gr_get_height(s), dx, dy);
146
147 int x = gr_get_width(s);
148 while (x + (int) gr_get_width(gProgressBarEmpty[RIGHT_SIDE]) < width) {
149 s = (pos > x ? gProgressBarFill : gProgressBarEmpty)[CENTER_TILE];
150 gr_blit(s, 0, 0, gr_get_width(s), gr_get_height(s), dx + x, dy);
151 x += gr_get_width(s);
152 }
153
154 s = (pos > x ? gProgressBarFill : gProgressBarEmpty)[RIGHT_SIDE];
155 gr_blit(s, 0, 0, gr_get_width(s), gr_get_height(s), dx + x, dy);
156 }
157
158 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) {
159 static int frame = 0;
160 gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
161 frame = (frame + 1) % PROGRESSBAR_INDETERMINATE_STATES;
162 }
163}
164
165static void draw_text_line(int row, const char* t) {
166 if (t[0] != '\0') {
167 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
168 }
169}
170
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800171#define MENU_TEXT_COLOR 7, 133, 74, 255
172#define NORMAL_TEXT_COLOR 200, 200, 200, 255
173#define HEADER_TEXT_COLOR NORMAL_TEXT_COLOR
174
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800175// Redraw everything on the screen. Does not flip pages.
176// Should only be called with gUpdateMutex locked.
177static void draw_screen_locked(void)
178{
179 draw_background_locked(gCurrentIcon);
180 draw_progress_locked();
181
182 if (show_text) {
183 gr_color(0, 0, 0, 160);
184 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
185
186 int i = 0;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100187 int j = 0;
188 int row = 0; // current row that we are drawing on
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800189 if (show_menu) {
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800190 gr_color(MENU_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100191 gr_fill(0, (menu_top + menu_sel - menu_show_start) * CHAR_HEIGHT,
192 gr_fb_width(), (menu_top + menu_sel - menu_show_start + 1)*CHAR_HEIGHT+1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800193
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800194 gr_color(HEADER_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100195 for (i = 0; i < menu_top; ++i) {
196 draw_text_line(i, menu[i]);
197 row++;
198 }
199
200 if (menu_items - menu_show_start + menu_top >= MAX_ROWS)
201 j = MAX_ROWS - menu_top;
202 else
203 j = menu_items - menu_show_start;
204
205 gr_color(MENU_TEXT_COLOR);
206 for (i = menu_show_start + menu_top; i < (menu_show_start + menu_top + j); ++i) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800207 if (i == menu_top + menu_sel) {
208 gr_color(255, 255, 255, 255);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100209 draw_text_line(i - menu_show_start , menu[i]);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800210 gr_color(MENU_TEXT_COLOR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800211 } else {
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100212 gr_color(MENU_TEXT_COLOR);
213 draw_text_line(i - menu_show_start, menu[i]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800214 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100215 row++;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800216 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100217 gr_fill(0, row*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
218 gr_fb_width(), row*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800219 }
220
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800221 gr_color(NORMAL_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100222 for (; row < text_rows; ++row) {
223 draw_text_line(row, text[(row+text_top) % text_rows]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800224 }
225 }
226}
227
228// Redraw everything on the screen and flip the screen (make it visible).
229// Should only be called with gUpdateMutex locked.
230static 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.
238static 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 {
244 draw_progress_locked(); // Draw only the progress bar
245 }
246 gr_flip();
247}
248
249// Keeps the progress bar updated, even when the process is otherwise busy.
250static void *progress_thread(void *cookie)
251{
252 for (;;) {
253 usleep(1000000 / PROGRESSBAR_INDETERMINATE_FPS);
254 pthread_mutex_lock(&gUpdateMutex);
255
256 // update the progress bar animation, if active
257 // skip this if we have a text overlay (too expensive to update)
258 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) {
259 update_progress_locked();
260 }
261
262 // move the progress bar forward on timed intervals, if configured
263 int duration = gProgressScopeDuration;
264 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) {
265 int elapsed = time(NULL) - gProgressScopeTime;
266 float progress = 1.0 * elapsed / duration;
267 if (progress > 1.0) progress = 1.0;
268 if (progress > gProgress) {
269 gProgress = progress;
270 update_progress_locked();
271 }
272 }
273
274 pthread_mutex_unlock(&gUpdateMutex);
275 }
276 return NULL;
277}
278
279// Reads input events, handles special hot keys, and adds to the key queue.
280static void *input_thread(void *cookie)
281{
282 int rel_sum = 0;
283 int fake_key = 0;
284 for (;;) {
285 // wait for the next key event
286 struct input_event ev;
287 do {
288 ev_get(&ev, 0);
289
290 if (ev.type == EV_SYN) {
291 continue;
292 } else if (ev.type == EV_REL) {
293 if (ev.code == REL_Y) {
294 // accumulate the up or down motion reported by
295 // the trackball. When it exceeds a threshold
296 // (positive or negative), fake an up/down
297 // key event.
298 rel_sum += ev.value;
299 if (rel_sum > 3) {
300 fake_key = 1;
301 ev.type = EV_KEY;
302 ev.code = KEY_DOWN;
303 ev.value = 1;
304 rel_sum = 0;
305 } else if (rel_sum < -3) {
306 fake_key = 1;
307 ev.type = EV_KEY;
308 ev.code = KEY_UP;
309 ev.value = 1;
310 rel_sum = 0;
311 }
312 }
313 } else {
314 rel_sum = 0;
315 }
316 } while (ev.type != EV_KEY || ev.code > KEY_MAX);
317
318 pthread_mutex_lock(&key_queue_mutex);
319 if (!fake_key) {
320 // our "fake" keys only report a key-down event (no
321 // key-up), so don't record them in the key_pressed
322 // table.
323 key_pressed[ev.code] = ev.value;
324 }
325 fake_key = 0;
326 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
327 if (ev.value > 0 && key_queue_len < queue_max) {
328 key_queue[key_queue_len++] = ev.code;
329 pthread_cond_signal(&key_queue_cond);
330 }
331 pthread_mutex_unlock(&key_queue_mutex);
332
Doug Zongkerddd6a282009-06-09 12:22:33 -0700333 if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800334 pthread_mutex_lock(&gUpdateMutex);
335 show_text = !show_text;
336 update_screen_locked();
337 pthread_mutex_unlock(&gUpdateMutex);
338 }
339
Doug Zongkerddd6a282009-06-09 12:22:33 -0700340 if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800341 reboot(RB_AUTOBOOT);
342 }
343 }
344 return NULL;
345}
346
347void ui_init(void)
348{
349 gr_init();
350 ev_init();
351
352 text_col = text_row = 0;
353 text_rows = gr_fb_height() / CHAR_HEIGHT;
354 if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
355 text_top = 1;
356
357 text_cols = gr_fb_width() / CHAR_WIDTH;
358 if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
359
360 int i;
361 for (i = 0; BITMAPS[i].name != NULL; ++i) {
362 int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface);
363 if (result < 0) {
Doug Zongker196c25c2009-09-15 08:50:04 -0700364 if (result == -2) {
365 LOGI("Bitmap %s missing header\n", BITMAPS[i].name);
366 } else {
367 LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result);
368 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800369 *BITMAPS[i].surface = NULL;
370 }
371 }
372
373 pthread_t t;
374 pthread_create(&t, NULL, progress_thread, NULL);
375 pthread_create(&t, NULL, input_thread, NULL);
376}
377
378char *ui_copy_image(int icon, int *width, int *height, int *bpp) {
379 pthread_mutex_lock(&gUpdateMutex);
380 draw_background_locked(gBackgroundIcon[icon]);
381 *width = gr_fb_width();
382 *height = gr_fb_height();
383 *bpp = sizeof(gr_pixel) * 8;
384 int size = *width * *height * sizeof(gr_pixel);
385 char *ret = malloc(size);
386 if (ret == NULL) {
387 LOGE("Can't allocate %d bytes for image\n", size);
388 } else {
389 memcpy(ret, gr_fb_data(), size);
390 }
391 pthread_mutex_unlock(&gUpdateMutex);
392 return ret;
393}
394
395void ui_set_background(int icon)
396{
397 pthread_mutex_lock(&gUpdateMutex);
398 gCurrentIcon = gBackgroundIcon[icon];
399 update_screen_locked();
400 pthread_mutex_unlock(&gUpdateMutex);
401}
402
403void ui_show_indeterminate_progress()
404{
405 pthread_mutex_lock(&gUpdateMutex);
406 if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
407 gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
408 update_progress_locked();
409 }
410 pthread_mutex_unlock(&gUpdateMutex);
411}
412
413void ui_show_progress(float portion, int seconds)
414{
415 pthread_mutex_lock(&gUpdateMutex);
416 gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
417 gProgressScopeStart += gProgressScopeSize;
418 gProgressScopeSize = portion;
419 gProgressScopeTime = time(NULL);
420 gProgressScopeDuration = seconds;
421 gProgress = 0;
422 update_progress_locked();
423 pthread_mutex_unlock(&gUpdateMutex);
424}
425
426void ui_set_progress(float fraction)
427{
428 pthread_mutex_lock(&gUpdateMutex);
429 if (fraction < 0.0) fraction = 0.0;
430 if (fraction > 1.0) fraction = 1.0;
431 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
432 // Skip updates that aren't visibly different.
433 int width = gr_get_width(gProgressBarIndeterminate[0]);
434 float scale = width * gProgressScopeSize;
435 if ((int) (gProgress * scale) != (int) (fraction * scale)) {
436 gProgress = fraction;
437 update_progress_locked();
438 }
439 }
440 pthread_mutex_unlock(&gUpdateMutex);
441}
442
443void ui_reset_progress()
444{
445 pthread_mutex_lock(&gUpdateMutex);
446 gProgressBarType = PROGRESSBAR_TYPE_NONE;
447 gProgressScopeStart = gProgressScopeSize = 0;
448 gProgressScopeTime = gProgressScopeDuration = 0;
449 gProgress = 0;
450 update_screen_locked();
451 pthread_mutex_unlock(&gUpdateMutex);
452}
453
454void ui_print(const char *fmt, ...)
455{
456 char buf[256];
457 va_list ap;
458 va_start(ap, fmt);
459 vsnprintf(buf, 256, fmt, ap);
460 va_end(ap);
461
462 fputs(buf, stderr);
463
464 // This can get called before ui_init(), so be careful.
465 pthread_mutex_lock(&gUpdateMutex);
466 if (text_rows > 0 && text_cols > 0) {
467 char *ptr;
468 for (ptr = buf; *ptr != '\0'; ++ptr) {
469 if (*ptr == '\n' || text_col >= text_cols) {
470 text[text_row][text_col] = '\0';
471 text_col = 0;
472 text_row = (text_row + 1) % text_rows;
473 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
474 }
475 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
476 }
477 text[text_row][text_col] = '\0';
478 update_screen_locked();
479 }
480 pthread_mutex_unlock(&gUpdateMutex);
481}
482
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800483void ui_reset_text_col()
484{
485 pthread_mutex_lock(&gUpdateMutex);
486 text_col = 0;
487 pthread_mutex_unlock(&gUpdateMutex);
488}
489
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800490#define MENU_ITEM_HEADER " - "
491#define MENU_ITEM_HEADER_LENGTH strlen(MENU_ITEM_HEADER)
492
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800493void ui_start_menu(char** headers, char** items) {
494 int i;
495 pthread_mutex_lock(&gUpdateMutex);
496 if (text_rows > 0 && text_cols > 0) {
497 for (i = 0; i < text_rows; ++i) {
498 if (headers[i] == NULL) break;
499 strncpy(menu[i], headers[i], text_cols-1);
500 menu[i][text_cols-1] = '\0';
501 }
502 menu_top = i;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100503 for (; i < MENU_MAX_ROWS; ++i) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800504 if (items[i-menu_top] == NULL) break;
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800505 strcpy(menu[i], MENU_ITEM_HEADER);
506 strncpy(menu[i] + MENU_ITEM_HEADER_LENGTH, items[i-menu_top], text_cols-1 - MENU_ITEM_HEADER_LENGTH);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800507 menu[i][text_cols-1] = '\0';
508 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100509
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800510 menu_items = i - menu_top;
511 show_menu = 1;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100512 menu_sel = menu_show_start = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800513 update_screen_locked();
514 }
515 pthread_mutex_unlock(&gUpdateMutex);
516}
517
518int ui_menu_select(int sel) {
519 int old_sel;
520 pthread_mutex_lock(&gUpdateMutex);
521 if (show_menu > 0) {
522 old_sel = menu_sel;
523 menu_sel = sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100524
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700525 if (menu_sel < 0) menu_sel = menu_items + menu_sel;
526 if (menu_sel >= menu_items) menu_sel = menu_sel - menu_items;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100527
528
529 if (menu_sel < menu_show_start && menu_show_start > 0) {
530 menu_show_start--;
531 }
532
533 if (menu_sel - menu_show_start + menu_top >= text_rows) {
534 menu_show_start++;
535 }
536
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800537 sel = menu_sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100538
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800539 if (menu_sel != old_sel) update_screen_locked();
540 }
541 pthread_mutex_unlock(&gUpdateMutex);
542 return sel;
543}
544
545void ui_end_menu() {
546 int i;
547 pthread_mutex_lock(&gUpdateMutex);
548 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
549 show_menu = 0;
550 update_screen_locked();
551 }
552 pthread_mutex_unlock(&gUpdateMutex);
553}
554
555int ui_text_visible()
556{
557 pthread_mutex_lock(&gUpdateMutex);
558 int visible = show_text;
559 pthread_mutex_unlock(&gUpdateMutex);
560 return visible;
561}
562
563int ui_wait_key()
564{
565 pthread_mutex_lock(&key_queue_mutex);
566 while (key_queue_len == 0) {
567 pthread_cond_wait(&key_queue_cond, &key_queue_mutex);
568 }
569
570 int key = key_queue[0];
571 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
572 pthread_mutex_unlock(&key_queue_mutex);
573 return key;
574}
575
576int ui_key_pressed(int key)
577{
578 // This is a volatile static array, don't bother locking
579 return key_pressed[key];
580}
581
582void ui_clear_key_queue() {
583 pthread_mutex_lock(&key_queue_mutex);
584 key_queue_len = 0;
585 pthread_mutex_unlock(&key_queue_mutex);
586}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800587
Koushik K. Dutta03173782010-02-26 14:14:23 -0800588void ui_set_show_text(int value) {
589 show_text = value;
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800590}