blob: df52c7ea9ddc447a69670a95b65f9aa5c4db4404 [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
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -070032#ifdef KEY_POWER_IS_SELECT_ITEM
33static int gShowBackButton = 1;
34#else
35static int gShowBackButton = 0;
36#endif
37
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038#define MAX_COLS 64
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +010039#define MAX_ROWS 32
40
41#define MENU_MAX_COLS 64
42#define MENU_MAX_ROWS 250
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043
44#define CHAR_WIDTH 10
45#define CHAR_HEIGHT 18
46
47#define PROGRESSBAR_INDETERMINATE_STATES 6
48#define PROGRESSBAR_INDETERMINATE_FPS 15
49
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER;
51static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS];
52static gr_surface gProgressBarIndeterminate[PROGRESSBAR_INDETERMINATE_STATES];
Doug Zongkerd93a2542009-10-08 16:32:58 -070053static gr_surface gProgressBarEmpty;
54static gr_surface gProgressBarFill;
Koushik Duttad3cc60b2010-07-03 13:56:45 -070055static int ui_has_initialized = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056
57static const struct { gr_surface* surface; const char *name; } BITMAPS[] = {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058 { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" },
59 { &gBackgroundIcon[BACKGROUND_ICON_ERROR], "icon_error" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060 { &gProgressBarIndeterminate[0], "indeterminate1" },
61 { &gProgressBarIndeterminate[1], "indeterminate2" },
62 { &gProgressBarIndeterminate[2], "indeterminate3" },
63 { &gProgressBarIndeterminate[3], "indeterminate4" },
64 { &gProgressBarIndeterminate[4], "indeterminate5" },
65 { &gProgressBarIndeterminate[5], "indeterminate6" },
Doug Zongkerd93a2542009-10-08 16:32:58 -070066 { &gProgressBarEmpty, "progress_empty" },
67 { &gProgressBarFill, "progress_fill" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080068 { NULL, NULL },
69};
70
71static gr_surface gCurrentIcon = NULL;
72
73static 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
80static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
81static time_t gProgressScopeTime, gProgressScopeDuration;
82
83// Set to 1 when both graphics pages are the same (except for the progress bar)
84static int gPagesIdentical = 0;
85
86// Log text overlay, displayed when a magic key is pressed
87static char text[MAX_ROWS][MAX_COLS];
88static int text_cols = 0, text_rows = 0;
89static int text_col = 0, text_row = 0, text_top = 0;
90static int show_text = 0;
91
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +010092static char menu[MENU_MAX_ROWS][MENU_MAX_COLS];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080093static int show_menu = 0;
94static int menu_top = 0, menu_items = 0, menu_sel = 0;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +010095static int menu_show_start = 0; // this is line which menu display is starting at
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080096
97// Key event input queue
98static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
99static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER;
100static int key_queue[256], key_queue_len = 0;
101static volatile char key_pressed[KEY_MAX + 1];
102
103// Clear the screen and draw the currently selected background icon (if any).
104// Should only be called with gUpdateMutex locked.
105static void draw_background_locked(gr_surface icon)
106{
107 gPagesIdentical = 0;
108 gr_color(0, 0, 0, 255);
109 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
110
111 if (icon) {
112 int iconWidth = gr_get_width(icon);
113 int iconHeight = gr_get_height(icon);
114 int iconX = (gr_fb_width() - iconWidth) / 2;
115 int iconY = (gr_fb_height() - iconHeight) / 2;
116 gr_blit(icon, 0, 0, iconWidth, iconHeight, iconX, iconY);
117 }
118}
119
120// Draw the progress bar (if any) on the screen. Does not flip pages.
121// Should only be called with gUpdateMutex locked.
122static void draw_progress_locked()
123{
124 if (gProgressBarType == PROGRESSBAR_TYPE_NONE) return;
125
126 int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]);
Doug Zongkerd93a2542009-10-08 16:32:58 -0700127 int width = gr_get_width(gProgressBarEmpty);
128 int height = gr_get_height(gProgressBarEmpty);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800129
130 int dx = (gr_fb_width() - width)/2;
131 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
132
133 // Erase behind the progress bar (in case this was a progress-only update)
134 gr_color(0, 0, 0, 255);
135 gr_fill(dx, dy, width, height);
136
137 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) {
138 float progress = gProgressScopeStart + gProgress * gProgressScopeSize;
139 int pos = (int) (progress * width);
140
Doug Zongkerd93a2542009-10-08 16:32:58 -0700141 if (pos > 0) {
142 gr_blit(gProgressBarFill, 0, 0, pos, height, dx, dy);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800143 }
Doug Zongkerd93a2542009-10-08 16:32:58 -0700144 if (pos < width-1) {
145 gr_blit(gProgressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
146 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800147 }
148
149 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) {
150 static int frame = 0;
151 gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
152 frame = (frame + 1) % PROGRESSBAR_INDETERMINATE_STATES;
153 }
154}
155
156static void draw_text_line(int row, const char* t) {
157 if (t[0] != '\0') {
158 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
159 }
160}
161
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800162#define MENU_TEXT_COLOR 7, 133, 74, 255
163#define NORMAL_TEXT_COLOR 200, 200, 200, 255
164#define HEADER_TEXT_COLOR NORMAL_TEXT_COLOR
165
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800166// Redraw everything on the screen. Does not flip pages.
167// Should only be called with gUpdateMutex locked.
168static void draw_screen_locked(void)
169{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700170 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800171 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;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100179 int j = 0;
180 int row = 0; // current row that we are drawing on
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800181 if (show_menu) {
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800182 gr_color(MENU_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100183 gr_fill(0, (menu_top + menu_sel - menu_show_start) * CHAR_HEIGHT,
184 gr_fb_width(), (menu_top + menu_sel - menu_show_start + 1)*CHAR_HEIGHT+1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800185
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800186 gr_color(HEADER_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100187 for (i = 0; i < menu_top; ++i) {
188 draw_text_line(i, menu[i]);
189 row++;
190 }
191
192 if (menu_items - menu_show_start + menu_top >= MAX_ROWS)
193 j = MAX_ROWS - menu_top;
194 else
195 j = menu_items - menu_show_start;
196
197 gr_color(MENU_TEXT_COLOR);
198 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 -0800199 if (i == menu_top + menu_sel) {
200 gr_color(255, 255, 255, 255);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100201 draw_text_line(i - menu_show_start , menu[i]);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800202 gr_color(MENU_TEXT_COLOR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800203 } else {
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100204 gr_color(MENU_TEXT_COLOR);
205 draw_text_line(i - menu_show_start, menu[i]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800206 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100207 row++;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800208 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100209 gr_fill(0, row*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
210 gr_fb_width(), row*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800211 }
212
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800213 gr_color(NORMAL_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100214 for (; row < text_rows; ++row) {
215 draw_text_line(row, text[(row+text_top) % text_rows]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800216 }
217 }
218}
219
220// Redraw everything on the screen and flip the screen (make it visible).
221// Should only be called with gUpdateMutex locked.
222static void update_screen_locked(void)
223{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700224 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800225 draw_screen_locked();
226 gr_flip();
227}
228
229// Updates only the progress bar, if possible, otherwise redraws the screen.
230// Should only be called with gUpdateMutex locked.
231static void update_progress_locked(void)
232{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700233 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800234 if (show_text || !gPagesIdentical) {
235 draw_screen_locked(); // Must redraw the whole screen
236 gPagesIdentical = 1;
237 } else {
238 draw_progress_locked(); // Draw only the progress bar
239 }
240 gr_flip();
241}
242
243// Keeps the progress bar updated, even when the process is otherwise busy.
244static void *progress_thread(void *cookie)
245{
246 for (;;) {
247 usleep(1000000 / PROGRESSBAR_INDETERMINATE_FPS);
248 pthread_mutex_lock(&gUpdateMutex);
249
250 // update the progress bar animation, if active
251 // skip this if we have a text overlay (too expensive to update)
252 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) {
253 update_progress_locked();
254 }
255
256 // move the progress bar forward on timed intervals, if configured
257 int duration = gProgressScopeDuration;
258 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) {
259 int elapsed = time(NULL) - gProgressScopeTime;
260 float progress = 1.0 * elapsed / duration;
261 if (progress > 1.0) progress = 1.0;
262 if (progress > gProgress) {
263 gProgress = progress;
264 update_progress_locked();
265 }
266 }
267
268 pthread_mutex_unlock(&gUpdateMutex);
269 }
270 return NULL;
271}
272
273// Reads input events, handles special hot keys, and adds to the key queue.
274static void *input_thread(void *cookie)
275{
276 int rel_sum = 0;
277 int fake_key = 0;
278 for (;;) {
279 // wait for the next key event
280 struct input_event ev;
281 do {
282 ev_get(&ev, 0);
283
284 if (ev.type == EV_SYN) {
285 continue;
286 } else if (ev.type == EV_REL) {
287 if (ev.code == REL_Y) {
288 // accumulate the up or down motion reported by
289 // the trackball. When it exceeds a threshold
290 // (positive or negative), fake an up/down
291 // key event.
292 rel_sum += ev.value;
293 if (rel_sum > 3) {
294 fake_key = 1;
295 ev.type = EV_KEY;
296 ev.code = KEY_DOWN;
297 ev.value = 1;
298 rel_sum = 0;
299 } else if (rel_sum < -3) {
300 fake_key = 1;
301 ev.type = EV_KEY;
302 ev.code = KEY_UP;
303 ev.value = 1;
304 rel_sum = 0;
305 }
306 }
307 } else {
308 rel_sum = 0;
309 }
310 } while (ev.type != EV_KEY || ev.code > KEY_MAX);
311
312 pthread_mutex_lock(&key_queue_mutex);
313 if (!fake_key) {
314 // our "fake" keys only report a key-down event (no
315 // key-up), so don't record them in the key_pressed
316 // table.
317 key_pressed[ev.code] = ev.value;
318 }
319 fake_key = 0;
320 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
321 if (ev.value > 0 && key_queue_len < queue_max) {
322 key_queue[key_queue_len++] = ev.code;
323 pthread_cond_signal(&key_queue_cond);
324 }
325 pthread_mutex_unlock(&key_queue_mutex);
326
Doug Zongkerddd6a282009-06-09 12:22:33 -0700327 if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800328 pthread_mutex_lock(&gUpdateMutex);
329 show_text = !show_text;
330 update_screen_locked();
331 pthread_mutex_unlock(&gUpdateMutex);
332 }
333
Doug Zongkerddd6a282009-06-09 12:22:33 -0700334 if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800335 reboot(RB_AUTOBOOT);
336 }
337 }
338 return NULL;
339}
340
341void ui_init(void)
342{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700343 ui_has_initialized = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800344 gr_init();
345 ev_init();
346
347 text_col = text_row = 0;
348 text_rows = gr_fb_height() / CHAR_HEIGHT;
349 if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
350 text_top = 1;
351
352 text_cols = gr_fb_width() / CHAR_WIDTH;
353 if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
354
355 int i;
356 for (i = 0; BITMAPS[i].name != NULL; ++i) {
357 int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface);
358 if (result < 0) {
Doug Zongker196c25c2009-09-15 08:50:04 -0700359 if (result == -2) {
360 LOGI("Bitmap %s missing header\n", BITMAPS[i].name);
361 } else {
362 LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result);
363 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800364 *BITMAPS[i].surface = NULL;
365 }
366 }
367
368 pthread_t t;
369 pthread_create(&t, NULL, progress_thread, NULL);
370 pthread_create(&t, NULL, input_thread, NULL);
371}
372
Koushik Dutta107629b2010-06-30 23:17:53 -0700373char *ui_copy_image(int icon, int *width, int *height, int *bpp) {
374 pthread_mutex_lock(&gUpdateMutex);
375 draw_background_locked(gBackgroundIcon[icon]);
376 *width = gr_fb_width();
377 *height = gr_fb_height();
378 *bpp = sizeof(gr_pixel) * 8;
379 int size = *width * *height * sizeof(gr_pixel);
380 char *ret = malloc(size);
381 if (ret == NULL) {
382 LOGE("Can't allocate %d bytes for image\n", size);
383 } else {
384 memcpy(ret, gr_fb_data(), size);
385 }
386 pthread_mutex_unlock(&gUpdateMutex);
387 return ret;
388}
389
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800390void ui_set_background(int icon)
391{
392 pthread_mutex_lock(&gUpdateMutex);
393 gCurrentIcon = gBackgroundIcon[icon];
394 update_screen_locked();
395 pthread_mutex_unlock(&gUpdateMutex);
396}
397
398void ui_show_indeterminate_progress()
399{
400 pthread_mutex_lock(&gUpdateMutex);
401 if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
402 gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
403 update_progress_locked();
404 }
405 pthread_mutex_unlock(&gUpdateMutex);
406}
407
408void ui_show_progress(float portion, int seconds)
409{
410 pthread_mutex_lock(&gUpdateMutex);
411 gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
412 gProgressScopeStart += gProgressScopeSize;
413 gProgressScopeSize = portion;
414 gProgressScopeTime = time(NULL);
415 gProgressScopeDuration = seconds;
416 gProgress = 0;
417 update_progress_locked();
418 pthread_mutex_unlock(&gUpdateMutex);
419}
420
421void ui_set_progress(float fraction)
422{
423 pthread_mutex_lock(&gUpdateMutex);
424 if (fraction < 0.0) fraction = 0.0;
425 if (fraction > 1.0) fraction = 1.0;
426 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
427 // Skip updates that aren't visibly different.
428 int width = gr_get_width(gProgressBarIndeterminate[0]);
429 float scale = width * gProgressScopeSize;
430 if ((int) (gProgress * scale) != (int) (fraction * scale)) {
431 gProgress = fraction;
432 update_progress_locked();
433 }
434 }
435 pthread_mutex_unlock(&gUpdateMutex);
436}
437
438void ui_reset_progress()
439{
440 pthread_mutex_lock(&gUpdateMutex);
441 gProgressBarType = PROGRESSBAR_TYPE_NONE;
442 gProgressScopeStart = gProgressScopeSize = 0;
443 gProgressScopeTime = gProgressScopeDuration = 0;
444 gProgress = 0;
445 update_screen_locked();
446 pthread_mutex_unlock(&gUpdateMutex);
447}
448
449void ui_print(const char *fmt, ...)
450{
451 char buf[256];
452 va_list ap;
453 va_start(ap, fmt);
454 vsnprintf(buf, 256, fmt, ap);
455 va_end(ap);
456
457 fputs(buf, stderr);
458
459 // This can get called before ui_init(), so be careful.
460 pthread_mutex_lock(&gUpdateMutex);
461 if (text_rows > 0 && text_cols > 0) {
462 char *ptr;
463 for (ptr = buf; *ptr != '\0'; ++ptr) {
464 if (*ptr == '\n' || text_col >= text_cols) {
465 text[text_row][text_col] = '\0';
466 text_col = 0;
467 text_row = (text_row + 1) % text_rows;
468 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
469 }
470 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
471 }
472 text[text_row][text_col] = '\0';
473 update_screen_locked();
474 }
475 pthread_mutex_unlock(&gUpdateMutex);
476}
477
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800478void ui_reset_text_col()
479{
480 pthread_mutex_lock(&gUpdateMutex);
481 text_col = 0;
482 pthread_mutex_unlock(&gUpdateMutex);
483}
484
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800485#define MENU_ITEM_HEADER " - "
486#define MENU_ITEM_HEADER_LENGTH strlen(MENU_ITEM_HEADER)
487
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700488int ui_start_menu(char** headers, char** items) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800489 int i;
490 pthread_mutex_lock(&gUpdateMutex);
491 if (text_rows > 0 && text_cols > 0) {
492 for (i = 0; i < text_rows; ++i) {
493 if (headers[i] == NULL) break;
494 strncpy(menu[i], headers[i], text_cols-1);
495 menu[i][text_cols-1] = '\0';
496 }
497 menu_top = i;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100498 for (; i < MENU_MAX_ROWS; ++i) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800499 if (items[i-menu_top] == NULL) break;
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800500 strcpy(menu[i], MENU_ITEM_HEADER);
501 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 -0800502 menu[i][text_cols-1] = '\0';
503 }
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700504
505 if (gShowBackButton) {
506 strcpy(menu[i], " - +++++Go Back+++++");
507 ++i;
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);
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700516 if (gShowBackButton) {
517 return menu_items - 1;
518 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700519 return menu_items;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800520}
521
522int ui_menu_select(int sel) {
523 int old_sel;
524 pthread_mutex_lock(&gUpdateMutex);
525 if (show_menu > 0) {
526 old_sel = menu_sel;
527 menu_sel = sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100528
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700529 if (menu_sel < 0) menu_sel = menu_items + menu_sel;
530 if (menu_sel >= menu_items) menu_sel = menu_sel - menu_items;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100531
532
533 if (menu_sel < menu_show_start && menu_show_start > 0) {
Koushik Dutta917c38a2010-05-24 16:43:39 -0700534 menu_show_start = menu_sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100535 }
536
537 if (menu_sel - menu_show_start + menu_top >= text_rows) {
Koushik Dutta917c38a2010-05-24 16:43:39 -0700538 menu_show_start = menu_sel + menu_top - text_rows + 1;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100539 }
540
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800541 sel = menu_sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100542
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800543 if (menu_sel != old_sel) update_screen_locked();
544 }
545 pthread_mutex_unlock(&gUpdateMutex);
546 return sel;
547}
548
549void ui_end_menu() {
550 int i;
551 pthread_mutex_lock(&gUpdateMutex);
552 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
553 show_menu = 0;
554 update_screen_locked();
555 }
556 pthread_mutex_unlock(&gUpdateMutex);
557}
558
559int ui_text_visible()
560{
561 pthread_mutex_lock(&gUpdateMutex);
562 int visible = show_text;
563 pthread_mutex_unlock(&gUpdateMutex);
564 return visible;
565}
566
567int ui_wait_key()
568{
569 pthread_mutex_lock(&key_queue_mutex);
570 while (key_queue_len == 0) {
571 pthread_cond_wait(&key_queue_cond, &key_queue_mutex);
572 }
573
574 int key = key_queue[0];
575 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
576 pthread_mutex_unlock(&key_queue_mutex);
577 return key;
578}
579
580int ui_key_pressed(int key)
581{
582 // This is a volatile static array, don't bother locking
583 return key_pressed[key];
584}
585
586void ui_clear_key_queue() {
587 pthread_mutex_lock(&key_queue_mutex);
588 key_queue_len = 0;
589 pthread_mutex_unlock(&key_queue_mutex);
590}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800591
Koushik K. Dutta03173782010-02-26 14:14:23 -0800592void ui_set_show_text(int value) {
593 show_text = value;
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800594}
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700595
596void ui_set_showing_back_button(int showBackButton) {
597 gShowBackButton = showBackButton;
598}
599
600int ui_get_showing_back_button() {
601 return gShowBackButton;
602}