blob: d973a50f4e2349c3d5734d2d74c2386f8ee237a3 [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 Duttad90ad5d2010-11-27 14:41:07 -080032#ifdef BOARD_HAS_NO_SELECT_BUTTON
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -070033static int gShowBackButton = 1;
34#else
35static int gShowBackButton = 0;
36#endif
37
Doug Zongker8a8e6cc2010-07-07 13:55:25 -070038#define MAX_COLS 96
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039#define MAX_ROWS 32
40
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +010041#define MENU_MAX_COLS 64
42#define MENU_MAX_ROWS 250
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043
Ricardo Cerqueira9acb0222010-11-08 23:54:30 +000044#ifndef BOARD_LDPI_RECOVERY
45 #define CHAR_WIDTH 10
46 #define CHAR_HEIGHT 18
47#else
48 #define CHAR_WIDTH 7
49 #define CHAR_HEIGHT 16
50#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051
52#define PROGRESSBAR_INDETERMINATE_STATES 6
53#define PROGRESSBAR_INDETERMINATE_FPS 15
54
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER;
56static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS];
57static gr_surface gProgressBarIndeterminate[PROGRESSBAR_INDETERMINATE_STATES];
Doug Zongkerd93a2542009-10-08 16:32:58 -070058static gr_surface gProgressBarEmpty;
59static gr_surface gProgressBarFill;
Koushik Duttad3cc60b2010-07-03 13:56:45 -070060static int ui_has_initialized = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061
62static const struct { gr_surface* surface; const char *name; } BITMAPS[] = {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080063 { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" },
64 { &gBackgroundIcon[BACKGROUND_ICON_ERROR], "icon_error" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080065 { &gProgressBarIndeterminate[0], "indeterminate1" },
66 { &gProgressBarIndeterminate[1], "indeterminate2" },
67 { &gProgressBarIndeterminate[2], "indeterminate3" },
68 { &gProgressBarIndeterminate[3], "indeterminate4" },
69 { &gProgressBarIndeterminate[4], "indeterminate5" },
70 { &gProgressBarIndeterminate[5], "indeterminate6" },
Doug Zongkerd93a2542009-10-08 16:32:58 -070071 { &gProgressBarEmpty, "progress_empty" },
72 { &gProgressBarFill, "progress_fill" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073 { NULL, NULL },
74};
75
76static gr_surface gCurrentIcon = NULL;
77
78static enum ProgressBarType {
79 PROGRESSBAR_TYPE_NONE,
80 PROGRESSBAR_TYPE_INDETERMINATE,
81 PROGRESSBAR_TYPE_NORMAL,
82} gProgressBarType = PROGRESSBAR_TYPE_NONE;
83
84// Progress bar scope of current operation
85static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
86static time_t gProgressScopeTime, gProgressScopeDuration;
87
88// Set to 1 when both graphics pages are the same (except for the progress bar)
89static int gPagesIdentical = 0;
90
91// Log text overlay, displayed when a magic key is pressed
92static char text[MAX_ROWS][MAX_COLS];
93static int text_cols = 0, text_rows = 0;
94static int text_col = 0, text_row = 0, text_top = 0;
Ying Wang532c8602010-09-01 14:52:22 -070095static int show_text = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080096
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +010097static char menu[MENU_MAX_ROWS][MENU_MAX_COLS];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080098static int show_menu = 0;
99static int menu_top = 0, menu_items = 0, menu_sel = 0;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100100static int menu_show_start = 0; // this is line which menu display is starting at
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800101
102// Key event input queue
103static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
104static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER;
105static int key_queue[256], key_queue_len = 0;
106static volatile char key_pressed[KEY_MAX + 1];
107
108// Clear the screen and draw the currently selected background icon (if any).
109// Should only be called with gUpdateMutex locked.
110static void draw_background_locked(gr_surface icon)
111{
112 gPagesIdentical = 0;
113 gr_color(0, 0, 0, 255);
114 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
115
116 if (icon) {
117 int iconWidth = gr_get_width(icon);
118 int iconHeight = gr_get_height(icon);
119 int iconX = (gr_fb_width() - iconWidth) / 2;
120 int iconY = (gr_fb_height() - iconHeight) / 2;
121 gr_blit(icon, 0, 0, iconWidth, iconHeight, iconX, iconY);
122 }
123}
124
125// Draw the progress bar (if any) on the screen. Does not flip pages.
126// Should only be called with gUpdateMutex locked.
127static void draw_progress_locked()
128{
129 if (gProgressBarType == PROGRESSBAR_TYPE_NONE) return;
130
131 int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]);
Doug Zongkerd93a2542009-10-08 16:32:58 -0700132 int width = gr_get_width(gProgressBarEmpty);
133 int height = gr_get_height(gProgressBarEmpty);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800134
135 int dx = (gr_fb_width() - width)/2;
136 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
137
138 // Erase behind the progress bar (in case this was a progress-only update)
139 gr_color(0, 0, 0, 255);
140 gr_fill(dx, dy, width, height);
141
142 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) {
143 float progress = gProgressScopeStart + gProgress * gProgressScopeSize;
144 int pos = (int) (progress * width);
145
Doug Zongkerd93a2542009-10-08 16:32:58 -0700146 if (pos > 0) {
147 gr_blit(gProgressBarFill, 0, 0, pos, height, dx, dy);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800148 }
Doug Zongkerd93a2542009-10-08 16:32:58 -0700149 if (pos < width-1) {
150 gr_blit(gProgressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
151 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800152 }
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
161static 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
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800167#define MENU_TEXT_COLOR 7, 133, 74, 255
168#define NORMAL_TEXT_COLOR 200, 200, 200, 255
169#define HEADER_TEXT_COLOR NORMAL_TEXT_COLOR
170
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800171// Redraw everything on the screen. Does not flip pages.
172// Should only be called with gUpdateMutex locked.
173static void draw_screen_locked(void)
174{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700175 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800176 draw_background_locked(gCurrentIcon);
177 draw_progress_locked();
178
179 if (show_text) {
180 gr_color(0, 0, 0, 160);
181 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
182
183 int i = 0;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100184 int j = 0;
185 int row = 0; // current row that we are drawing on
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800186 if (show_menu) {
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800187 gr_color(MENU_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100188 gr_fill(0, (menu_top + menu_sel - menu_show_start) * CHAR_HEIGHT,
189 gr_fb_width(), (menu_top + menu_sel - menu_show_start + 1)*CHAR_HEIGHT+1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800190
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800191 gr_color(HEADER_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100192 for (i = 0; i < menu_top; ++i) {
193 draw_text_line(i, menu[i]);
194 row++;
195 }
196
197 if (menu_items - menu_show_start + menu_top >= MAX_ROWS)
198 j = MAX_ROWS - menu_top;
199 else
200 j = menu_items - menu_show_start;
201
202 gr_color(MENU_TEXT_COLOR);
203 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 -0800204 if (i == menu_top + menu_sel) {
205 gr_color(255, 255, 255, 255);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100206 draw_text_line(i - menu_show_start , menu[i]);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800207 gr_color(MENU_TEXT_COLOR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800208 } else {
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100209 gr_color(MENU_TEXT_COLOR);
210 draw_text_line(i - menu_show_start, menu[i]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800211 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100212 row++;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800213 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100214 gr_fill(0, row*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
215 gr_fb_width(), row*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800216 }
217
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800218 gr_color(NORMAL_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100219 for (; row < text_rows; ++row) {
220 draw_text_line(row, text[(row+text_top) % text_rows]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800221 }
222 }
223}
224
225// Redraw everything on the screen and flip the screen (make it visible).
226// Should only be called with gUpdateMutex locked.
227static void update_screen_locked(void)
228{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700229 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800230 draw_screen_locked();
231 gr_flip();
232}
233
234// Updates only the progress bar, if possible, otherwise redraws the screen.
235// Should only be called with gUpdateMutex locked.
236static void update_progress_locked(void)
237{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700238 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800239 if (show_text || !gPagesIdentical) {
240 draw_screen_locked(); // Must redraw the whole screen
241 gPagesIdentical = 1;
242 } else {
243 draw_progress_locked(); // Draw only the progress bar
244 }
245 gr_flip();
246}
247
248// Keeps the progress bar updated, even when the process is otherwise busy.
249static void *progress_thread(void *cookie)
250{
251 for (;;) {
252 usleep(1000000 / PROGRESSBAR_INDETERMINATE_FPS);
253 pthread_mutex_lock(&gUpdateMutex);
254
255 // update the progress bar animation, if active
256 // skip this if we have a text overlay (too expensive to update)
257 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) {
258 update_progress_locked();
259 }
260
261 // move the progress bar forward on timed intervals, if configured
262 int duration = gProgressScopeDuration;
263 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) {
264 int elapsed = time(NULL) - gProgressScopeTime;
265 float progress = 1.0 * elapsed / duration;
266 if (progress > 1.0) progress = 1.0;
267 if (progress > gProgress) {
268 gProgress = progress;
269 update_progress_locked();
270 }
271 }
272
273 pthread_mutex_unlock(&gUpdateMutex);
274 }
275 return NULL;
276}
277
278// Reads input events, handles special hot keys, and adds to the key queue.
279static void *input_thread(void *cookie)
280{
281 int rel_sum = 0;
282 int fake_key = 0;
283 for (;;) {
284 // wait for the next key event
285 struct input_event ev;
286 do {
287 ev_get(&ev, 0);
288
289 if (ev.type == EV_SYN) {
290 continue;
291 } else if (ev.type == EV_REL) {
292 if (ev.code == REL_Y) {
293 // accumulate the up or down motion reported by
294 // the trackball. When it exceeds a threshold
295 // (positive or negative), fake an up/down
296 // key event.
297 rel_sum += ev.value;
298 if (rel_sum > 3) {
299 fake_key = 1;
300 ev.type = EV_KEY;
301 ev.code = KEY_DOWN;
302 ev.value = 1;
303 rel_sum = 0;
304 } else if (rel_sum < -3) {
305 fake_key = 1;
306 ev.type = EV_KEY;
307 ev.code = KEY_UP;
308 ev.value = 1;
309 rel_sum = 0;
310 }
311 }
312 } else {
313 rel_sum = 0;
314 }
315 } while (ev.type != EV_KEY || ev.code > KEY_MAX);
316
317 pthread_mutex_lock(&key_queue_mutex);
318 if (!fake_key) {
319 // our "fake" keys only report a key-down event (no
320 // key-up), so don't record them in the key_pressed
321 // table.
322 key_pressed[ev.code] = ev.value;
323 }
324 fake_key = 0;
325 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
326 if (ev.value > 0 && key_queue_len < queue_max) {
327 key_queue[key_queue_len++] = ev.code;
328 pthread_cond_signal(&key_queue_cond);
329 }
330 pthread_mutex_unlock(&key_queue_mutex);
331
Doug Zongkerddd6a282009-06-09 12:22:33 -0700332 if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800333 pthread_mutex_lock(&gUpdateMutex);
334 show_text = !show_text;
335 update_screen_locked();
336 pthread_mutex_unlock(&gUpdateMutex);
337 }
338
Doug Zongkerddd6a282009-06-09 12:22:33 -0700339 if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800340 reboot(RB_AUTOBOOT);
341 }
342 }
343 return NULL;
344}
345
346void ui_init(void)
347{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700348 ui_has_initialized = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800349 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
Koushik Dutta107629b2010-06-30 23:17:53 -0700378char *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
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800395void 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
Doug Zongker56c51052010-07-01 09:18:44 -0700462 fputs(buf, stdout);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800463
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
Koushik Duttadf1e4062010-12-18 17:42:31 -0800493int ui_start_menu(char** headers, char** items, int initial_selection) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800494 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 }
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700509
510 if (gShowBackButton) {
511 strcpy(menu[i], " - +++++Go Back+++++");
512 ++i;
513 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100514
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800515 menu_items = i - menu_top;
516 show_menu = 1;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800517 menu_sel = menu_show_start = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800518 update_screen_locked();
519 }
520 pthread_mutex_unlock(&gUpdateMutex);
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700521 if (gShowBackButton) {
522 return menu_items - 1;
523 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700524 return menu_items;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800525}
526
527int ui_menu_select(int sel) {
528 int old_sel;
529 pthread_mutex_lock(&gUpdateMutex);
530 if (show_menu > 0) {
531 old_sel = menu_sel;
532 menu_sel = sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100533
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700534 if (menu_sel < 0) menu_sel = menu_items + menu_sel;
535 if (menu_sel >= menu_items) menu_sel = menu_sel - menu_items;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100536
537
538 if (menu_sel < menu_show_start && menu_show_start > 0) {
Koushik Dutta917c38a2010-05-24 16:43:39 -0700539 menu_show_start = menu_sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100540 }
541
542 if (menu_sel - menu_show_start + menu_top >= text_rows) {
Koushik Dutta917c38a2010-05-24 16:43:39 -0700543 menu_show_start = menu_sel + menu_top - text_rows + 1;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100544 }
545
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800546 sel = menu_sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100547
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800548 if (menu_sel != old_sel) update_screen_locked();
549 }
550 pthread_mutex_unlock(&gUpdateMutex);
551 return sel;
552}
553
554void ui_end_menu() {
555 int i;
556 pthread_mutex_lock(&gUpdateMutex);
557 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
558 show_menu = 0;
559 update_screen_locked();
560 }
561 pthread_mutex_unlock(&gUpdateMutex);
562}
563
564int ui_text_visible()
565{
566 pthread_mutex_lock(&gUpdateMutex);
567 int visible = show_text;
568 pthread_mutex_unlock(&gUpdateMutex);
569 return visible;
570}
571
Doug Zongker4bc98062010-09-03 11:00:13 -0700572void ui_show_text(int visible)
573{
574 pthread_mutex_lock(&gUpdateMutex);
575 show_text = visible;
576 update_screen_locked();
577 pthread_mutex_unlock(&gUpdateMutex);
578}
579
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800580int ui_wait_key()
581{
582 pthread_mutex_lock(&key_queue_mutex);
583 while (key_queue_len == 0) {
584 pthread_cond_wait(&key_queue_cond, &key_queue_mutex);
585 }
586
587 int key = key_queue[0];
588 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
589 pthread_mutex_unlock(&key_queue_mutex);
590 return key;
591}
592
593int ui_key_pressed(int key)
594{
595 // This is a volatile static array, don't bother locking
596 return key_pressed[key];
597}
598
599void ui_clear_key_queue() {
600 pthread_mutex_lock(&key_queue_mutex);
601 key_queue_len = 0;
602 pthread_mutex_unlock(&key_queue_mutex);
603}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800604
Koushik K. Dutta03173782010-02-26 14:14:23 -0800605void ui_set_show_text(int value) {
606 show_text = value;
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800607}
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700608
609void ui_set_showing_back_button(int showBackButton) {
610 gShowBackButton = showBackButton;
611}
612
613int ui_get_showing_back_button() {
614 return gShowBackButton;
615}