blob: 133f4da4a6b867f7c581d2c9f9b7e79407bc8e1a [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" },
Koushik Dutta5d808172010-12-18 22:29:27 -080065 { &gBackgroundIcon[BACKGROUND_ICON_CLOCKWORK], "icon_clockwork" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080066 { &gProgressBarIndeterminate[0], "indeterminate1" },
67 { &gProgressBarIndeterminate[1], "indeterminate2" },
68 { &gProgressBarIndeterminate[2], "indeterminate3" },
69 { &gProgressBarIndeterminate[3], "indeterminate4" },
70 { &gProgressBarIndeterminate[4], "indeterminate5" },
71 { &gProgressBarIndeterminate[5], "indeterminate6" },
Doug Zongkerd93a2542009-10-08 16:32:58 -070072 { &gProgressBarEmpty, "progress_empty" },
73 { &gProgressBarFill, "progress_fill" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080074 { NULL, NULL },
75};
76
77static gr_surface gCurrentIcon = NULL;
78
79static enum ProgressBarType {
80 PROGRESSBAR_TYPE_NONE,
81 PROGRESSBAR_TYPE_INDETERMINATE,
82 PROGRESSBAR_TYPE_NORMAL,
83} gProgressBarType = PROGRESSBAR_TYPE_NONE;
84
85// Progress bar scope of current operation
86static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
87static time_t gProgressScopeTime, gProgressScopeDuration;
88
89// Set to 1 when both graphics pages are the same (except for the progress bar)
90static int gPagesIdentical = 0;
91
92// Log text overlay, displayed when a magic key is pressed
93static char text[MAX_ROWS][MAX_COLS];
94static int text_cols = 0, text_rows = 0;
95static int text_col = 0, text_row = 0, text_top = 0;
Ying Wang532c8602010-09-01 14:52:22 -070096static int show_text = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080097
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +010098static char menu[MENU_MAX_ROWS][MENU_MAX_COLS];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080099static int show_menu = 0;
100static int menu_top = 0, menu_items = 0, menu_sel = 0;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100101static int menu_show_start = 0; // this is line which menu display is starting at
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800102
103// Key event input queue
104static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
105static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER;
106static int key_queue[256], key_queue_len = 0;
107static volatile char key_pressed[KEY_MAX + 1];
108
109// Clear the screen and draw the currently selected background icon (if any).
110// Should only be called with gUpdateMutex locked.
111static void draw_background_locked(gr_surface icon)
112{
113 gPagesIdentical = 0;
114 gr_color(0, 0, 0, 255);
115 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
116
117 if (icon) {
118 int iconWidth = gr_get_width(icon);
119 int iconHeight = gr_get_height(icon);
120 int iconX = (gr_fb_width() - iconWidth) / 2;
121 int iconY = (gr_fb_height() - iconHeight) / 2;
122 gr_blit(icon, 0, 0, iconWidth, iconHeight, iconX, iconY);
123 }
124}
125
126// Draw the progress bar (if any) on the screen. Does not flip pages.
127// Should only be called with gUpdateMutex locked.
128static void draw_progress_locked()
129{
130 if (gProgressBarType == PROGRESSBAR_TYPE_NONE) return;
131
132 int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]);
Doug Zongkerd93a2542009-10-08 16:32:58 -0700133 int width = gr_get_width(gProgressBarEmpty);
134 int height = gr_get_height(gProgressBarEmpty);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800135
136 int dx = (gr_fb_width() - width)/2;
137 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
138
139 // Erase behind the progress bar (in case this was a progress-only update)
140 gr_color(0, 0, 0, 255);
141 gr_fill(dx, dy, width, height);
142
143 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) {
144 float progress = gProgressScopeStart + gProgress * gProgressScopeSize;
145 int pos = (int) (progress * width);
146
Doug Zongkerd93a2542009-10-08 16:32:58 -0700147 if (pos > 0) {
148 gr_blit(gProgressBarFill, 0, 0, pos, height, dx, dy);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800149 }
Doug Zongkerd93a2542009-10-08 16:32:58 -0700150 if (pos < width-1) {
151 gr_blit(gProgressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
152 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800153 }
154
155 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) {
156 static int frame = 0;
157 gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
158 frame = (frame + 1) % PROGRESSBAR_INDETERMINATE_STATES;
159 }
160}
161
162static void draw_text_line(int row, const char* t) {
163 if (t[0] != '\0') {
164 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
165 }
166}
167
Koushik Dutta5d808172010-12-18 22:29:27 -0800168#define MENU_TEXT_COLOR 255, 160, 49, 255
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800169#define NORMAL_TEXT_COLOR 200, 200, 200, 255
170#define HEADER_TEXT_COLOR NORMAL_TEXT_COLOR
171
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800172// Redraw everything on the screen. Does not flip pages.
173// Should only be called with gUpdateMutex locked.
174static void draw_screen_locked(void)
175{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700176 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800177 draw_background_locked(gCurrentIcon);
178 draw_progress_locked();
179
180 if (show_text) {
181 gr_color(0, 0, 0, 160);
182 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
183
184 int i = 0;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100185 int j = 0;
186 int row = 0; // current row that we are drawing on
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800187 if (show_menu) {
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800188 gr_color(MENU_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100189 gr_fill(0, (menu_top + menu_sel - menu_show_start) * CHAR_HEIGHT,
190 gr_fb_width(), (menu_top + menu_sel - menu_show_start + 1)*CHAR_HEIGHT+1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800191
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800192 gr_color(HEADER_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100193 for (i = 0; i < menu_top; ++i) {
194 draw_text_line(i, menu[i]);
195 row++;
196 }
197
198 if (menu_items - menu_show_start + menu_top >= MAX_ROWS)
199 j = MAX_ROWS - menu_top;
200 else
201 j = menu_items - menu_show_start;
202
203 gr_color(MENU_TEXT_COLOR);
204 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 -0800205 if (i == menu_top + menu_sel) {
206 gr_color(255, 255, 255, 255);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100207 draw_text_line(i - menu_show_start , menu[i]);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800208 gr_color(MENU_TEXT_COLOR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800209 } else {
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100210 gr_color(MENU_TEXT_COLOR);
211 draw_text_line(i - menu_show_start, menu[i]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800212 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100213 row++;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800214 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100215 gr_fill(0, row*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
216 gr_fb_width(), row*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800217 }
218
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800219 gr_color(NORMAL_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100220 for (; row < text_rows; ++row) {
221 draw_text_line(row, text[(row+text_top) % text_rows]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800222 }
223 }
224}
225
226// Redraw everything on the screen and flip the screen (make it visible).
227// Should only be called with gUpdateMutex locked.
228static void update_screen_locked(void)
229{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700230 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800231 draw_screen_locked();
232 gr_flip();
233}
234
235// Updates only the progress bar, if possible, otherwise redraws the screen.
236// Should only be called with gUpdateMutex locked.
237static void update_progress_locked(void)
238{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700239 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800240 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{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700349 ui_has_initialized = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800350 gr_init();
351 ev_init();
352
353 text_col = text_row = 0;
354 text_rows = gr_fb_height() / CHAR_HEIGHT;
355 if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
356 text_top = 1;
357
358 text_cols = gr_fb_width() / CHAR_WIDTH;
359 if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
360
361 int i;
362 for (i = 0; BITMAPS[i].name != NULL; ++i) {
363 int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface);
364 if (result < 0) {
Doug Zongker196c25c2009-09-15 08:50:04 -0700365 if (result == -2) {
366 LOGI("Bitmap %s missing header\n", BITMAPS[i].name);
367 } else {
368 LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result);
369 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800370 *BITMAPS[i].surface = NULL;
371 }
372 }
373
374 pthread_t t;
375 pthread_create(&t, NULL, progress_thread, NULL);
376 pthread_create(&t, NULL, input_thread, NULL);
377}
378
Koushik Dutta107629b2010-06-30 23:17:53 -0700379char *ui_copy_image(int icon, int *width, int *height, int *bpp) {
380 pthread_mutex_lock(&gUpdateMutex);
381 draw_background_locked(gBackgroundIcon[icon]);
382 *width = gr_fb_width();
383 *height = gr_fb_height();
384 *bpp = sizeof(gr_pixel) * 8;
385 int size = *width * *height * sizeof(gr_pixel);
386 char *ret = malloc(size);
387 if (ret == NULL) {
388 LOGE("Can't allocate %d bytes for image\n", size);
389 } else {
390 memcpy(ret, gr_fb_data(), size);
391 }
392 pthread_mutex_unlock(&gUpdateMutex);
393 return ret;
394}
395
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800396void ui_set_background(int icon)
397{
398 pthread_mutex_lock(&gUpdateMutex);
399 gCurrentIcon = gBackgroundIcon[icon];
400 update_screen_locked();
401 pthread_mutex_unlock(&gUpdateMutex);
402}
403
404void ui_show_indeterminate_progress()
405{
406 pthread_mutex_lock(&gUpdateMutex);
407 if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
408 gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
409 update_progress_locked();
410 }
411 pthread_mutex_unlock(&gUpdateMutex);
412}
413
414void ui_show_progress(float portion, int seconds)
415{
416 pthread_mutex_lock(&gUpdateMutex);
417 gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
418 gProgressScopeStart += gProgressScopeSize;
419 gProgressScopeSize = portion;
420 gProgressScopeTime = time(NULL);
421 gProgressScopeDuration = seconds;
422 gProgress = 0;
423 update_progress_locked();
424 pthread_mutex_unlock(&gUpdateMutex);
425}
426
427void ui_set_progress(float fraction)
428{
429 pthread_mutex_lock(&gUpdateMutex);
430 if (fraction < 0.0) fraction = 0.0;
431 if (fraction > 1.0) fraction = 1.0;
432 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
433 // Skip updates that aren't visibly different.
434 int width = gr_get_width(gProgressBarIndeterminate[0]);
435 float scale = width * gProgressScopeSize;
436 if ((int) (gProgress * scale) != (int) (fraction * scale)) {
437 gProgress = fraction;
438 update_progress_locked();
439 }
440 }
441 pthread_mutex_unlock(&gUpdateMutex);
442}
443
444void ui_reset_progress()
445{
446 pthread_mutex_lock(&gUpdateMutex);
447 gProgressBarType = PROGRESSBAR_TYPE_NONE;
448 gProgressScopeStart = gProgressScopeSize = 0;
449 gProgressScopeTime = gProgressScopeDuration = 0;
450 gProgress = 0;
451 update_screen_locked();
452 pthread_mutex_unlock(&gUpdateMutex);
453}
454
455void ui_print(const char *fmt, ...)
456{
457 char buf[256];
458 va_list ap;
459 va_start(ap, fmt);
460 vsnprintf(buf, 256, fmt, ap);
461 va_end(ap);
462
Doug Zongker56c51052010-07-01 09:18:44 -0700463 fputs(buf, stdout);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800464
465 // This can get called before ui_init(), so be careful.
466 pthread_mutex_lock(&gUpdateMutex);
467 if (text_rows > 0 && text_cols > 0) {
468 char *ptr;
469 for (ptr = buf; *ptr != '\0'; ++ptr) {
470 if (*ptr == '\n' || text_col >= text_cols) {
471 text[text_row][text_col] = '\0';
472 text_col = 0;
473 text_row = (text_row + 1) % text_rows;
474 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
475 }
476 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
477 }
478 text[text_row][text_col] = '\0';
479 update_screen_locked();
480 }
481 pthread_mutex_unlock(&gUpdateMutex);
482}
483
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800484void ui_reset_text_col()
485{
486 pthread_mutex_lock(&gUpdateMutex);
487 text_col = 0;
488 pthread_mutex_unlock(&gUpdateMutex);
489}
490
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800491#define MENU_ITEM_HEADER " - "
492#define MENU_ITEM_HEADER_LENGTH strlen(MENU_ITEM_HEADER)
493
Koushik Duttadf1e4062010-12-18 17:42:31 -0800494int ui_start_menu(char** headers, char** items, int initial_selection) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800495 int i;
496 pthread_mutex_lock(&gUpdateMutex);
497 if (text_rows > 0 && text_cols > 0) {
498 for (i = 0; i < text_rows; ++i) {
499 if (headers[i] == NULL) break;
500 strncpy(menu[i], headers[i], text_cols-1);
501 menu[i][text_cols-1] = '\0';
502 }
503 menu_top = i;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100504 for (; i < MENU_MAX_ROWS; ++i) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800505 if (items[i-menu_top] == NULL) break;
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800506 strcpy(menu[i], MENU_ITEM_HEADER);
507 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 -0800508 menu[i][text_cols-1] = '\0';
509 }
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700510
511 if (gShowBackButton) {
512 strcpy(menu[i], " - +++++Go Back+++++");
513 ++i;
514 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100515
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800516 menu_items = i - menu_top;
517 show_menu = 1;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800518 menu_sel = menu_show_start = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800519 update_screen_locked();
520 }
521 pthread_mutex_unlock(&gUpdateMutex);
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700522 if (gShowBackButton) {
523 return menu_items - 1;
524 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700525 return menu_items;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800526}
527
528int ui_menu_select(int sel) {
529 int old_sel;
530 pthread_mutex_lock(&gUpdateMutex);
531 if (show_menu > 0) {
532 old_sel = menu_sel;
533 menu_sel = sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100534
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700535 if (menu_sel < 0) menu_sel = menu_items + menu_sel;
536 if (menu_sel >= menu_items) menu_sel = menu_sel - menu_items;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100537
538
539 if (menu_sel < menu_show_start && menu_show_start > 0) {
Koushik Dutta917c38a2010-05-24 16:43:39 -0700540 menu_show_start = menu_sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100541 }
542
543 if (menu_sel - menu_show_start + menu_top >= text_rows) {
Koushik Dutta917c38a2010-05-24 16:43:39 -0700544 menu_show_start = menu_sel + menu_top - text_rows + 1;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100545 }
546
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800547 sel = menu_sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100548
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800549 if (menu_sel != old_sel) update_screen_locked();
550 }
551 pthread_mutex_unlock(&gUpdateMutex);
552 return sel;
553}
554
555void ui_end_menu() {
556 int i;
557 pthread_mutex_lock(&gUpdateMutex);
558 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
559 show_menu = 0;
560 update_screen_locked();
561 }
562 pthread_mutex_unlock(&gUpdateMutex);
563}
564
565int ui_text_visible()
566{
567 pthread_mutex_lock(&gUpdateMutex);
568 int visible = show_text;
569 pthread_mutex_unlock(&gUpdateMutex);
570 return visible;
571}
572
Doug Zongker4bc98062010-09-03 11:00:13 -0700573void ui_show_text(int visible)
574{
575 pthread_mutex_lock(&gUpdateMutex);
576 show_text = visible;
577 update_screen_locked();
578 pthread_mutex_unlock(&gUpdateMutex);
579}
580
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800581int ui_wait_key()
582{
583 pthread_mutex_lock(&key_queue_mutex);
584 while (key_queue_len == 0) {
585 pthread_cond_wait(&key_queue_cond, &key_queue_mutex);
586 }
587
588 int key = key_queue[0];
589 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
590 pthread_mutex_unlock(&key_queue_mutex);
591 return key;
592}
593
594int ui_key_pressed(int key)
595{
596 // This is a volatile static array, don't bother locking
597 return key_pressed[key];
598}
599
600void ui_clear_key_queue() {
601 pthread_mutex_lock(&key_queue_mutex);
602 key_queue_len = 0;
603 pthread_mutex_unlock(&key_queue_mutex);
604}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800605
Koushik K. Dutta03173782010-02-26 14:14:23 -0800606void ui_set_show_text(int value) {
607 show_text = value;
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800608}
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700609
610void ui_set_showing_back_button(int showBackButton) {
611 gShowBackButton = showBackButton;
612}
613
614int ui_get_showing_back_button() {
615 return gShowBackButton;
616}