blob: 054e53f60be3817f2a639f08a6eb102adc1d102b [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>
Doug Zongker5cae4452011-01-25 13:15:30 -080027#include <errno.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028
29#include "common.h"
30#include "minui/minui.h"
Doug Zongker23412e62009-07-23 10:16:07 -070031#include "recovery_ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032
Doug Zongkerf291d852010-07-07 13:55:25 -070033#define MAX_COLS 96
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034#define MAX_ROWS 32
35
36#define CHAR_WIDTH 10
37#define CHAR_HEIGHT 18
38
Doug Zongker5cae4452011-01-25 13:15:30 -080039#define UI_WAIT_KEY_TIMEOUT_SEC 120
40
Doug Zongker6809c512011-03-01 14:04:34 -080041UIParameters ui_parameters = {
42 6, // indeterminate progress bar frames
43 15, // fps
44 0, // installation icon frames (0 == static image)
45 0, 0, // installation icon overlay offset
46};
47
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER;
49static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS];
Doug Zongker6809c512011-03-01 14:04:34 -080050static gr_surface *gInstallationOverlay;
51static gr_surface *gProgressBarIndeterminate;
Doug Zongkerd93a2542009-10-08 16:32:58 -070052static gr_surface gProgressBarEmpty;
53static gr_surface gProgressBarFill;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080054
55static const struct { gr_surface* surface; const char *name; } BITMAPS[] = {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056 { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" },
57 { &gBackgroundIcon[BACKGROUND_ICON_ERROR], "icon_error" },
Doug Zongkerd93a2542009-10-08 16:32:58 -070058 { &gProgressBarEmpty, "progress_empty" },
59 { &gProgressBarFill, "progress_fill" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060 { NULL, NULL },
61};
62
Doug Zongker6809c512011-03-01 14:04:34 -080063static int gCurrentIcon = 0;
64static int gInstallingFrame = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080065
66static enum ProgressBarType {
67 PROGRESSBAR_TYPE_NONE,
68 PROGRESSBAR_TYPE_INDETERMINATE,
69 PROGRESSBAR_TYPE_NORMAL,
70} gProgressBarType = PROGRESSBAR_TYPE_NONE;
71
72// Progress bar scope of current operation
73static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
Doug Zongker6809c512011-03-01 14:04:34 -080074static double gProgressScopeTime, gProgressScopeDuration;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080075
76// Set to 1 when both graphics pages are the same (except for the progress bar)
77static int gPagesIdentical = 0;
78
79// Log text overlay, displayed when a magic key is pressed
80static char text[MAX_ROWS][MAX_COLS];
81static int text_cols = 0, text_rows = 0;
82static int text_col = 0, text_row = 0, text_top = 0;
83static int show_text = 0;
Doug Zongker5cae4452011-01-25 13:15:30 -080084static int show_text_ever = 0; // has show_text ever been 1?
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080085
86static char menu[MAX_ROWS][MAX_COLS];
87static int show_menu = 0;
88static int menu_top = 0, menu_items = 0, menu_sel = 0;
89
90// Key event input queue
91static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
92static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER;
93static int key_queue[256], key_queue_len = 0;
94static volatile char key_pressed[KEY_MAX + 1];
95
Doug Zongker6809c512011-03-01 14:04:34 -080096// Return the current time as a double (including fractions of a second).
97static double now() {
98 struct timeval tv;
99 gettimeofday(&tv, NULL);
100 return tv.tv_sec + tv.tv_usec / 1000000.0;
101}
102
103// Draw the given frame over the installation overlay animation. The
104// background is not cleared or draw with the base icon first; we
105// assume that the frame already contains some other frame of the
106// animation. Does nothing if no overlay animation is defined.
107// Should only be called with gUpdateMutex locked.
108static void draw_install_overlay_locked(int frame) {
109 if (gInstallationOverlay == NULL) return;
110 gr_surface surface = gInstallationOverlay[frame];
111 int iconWidth = gr_get_width(surface);
112 int iconHeight = gr_get_height(surface);
113 gr_blit(surface, 0, 0, iconWidth, iconHeight,
114 ui_parameters.install_overlay_offset_x,
115 ui_parameters.install_overlay_offset_y);
116}
117
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800118// Clear the screen and draw the currently selected background icon (if any).
119// Should only be called with gUpdateMutex locked.
Doug Zongker6809c512011-03-01 14:04:34 -0800120static void draw_background_locked(int icon)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800121{
122 gPagesIdentical = 0;
123 gr_color(0, 0, 0, 255);
124 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
125
126 if (icon) {
Doug Zongker6809c512011-03-01 14:04:34 -0800127 gr_surface surface = gBackgroundIcon[icon];
128 int iconWidth = gr_get_width(surface);
129 int iconHeight = gr_get_height(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800130 int iconX = (gr_fb_width() - iconWidth) / 2;
131 int iconY = (gr_fb_height() - iconHeight) / 2;
Doug Zongker6809c512011-03-01 14:04:34 -0800132 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
133 if (icon == BACKGROUND_ICON_INSTALLING) {
134 draw_install_overlay_locked(gInstallingFrame);
135 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800136 }
137}
138
139// Draw the progress bar (if any) on the screen. Does not flip pages.
140// Should only be called with gUpdateMutex locked.
141static void draw_progress_locked()
142{
Doug Zongker6809c512011-03-01 14:04:34 -0800143 if (gCurrentIcon == BACKGROUND_ICON_INSTALLING) {
144 draw_install_overlay_locked(gInstallingFrame);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800145 }
146
Doug Zongker6809c512011-03-01 14:04:34 -0800147 if (gProgressBarType != PROGRESSBAR_TYPE_NONE) {
148 int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]);
149 int width = gr_get_width(gProgressBarEmpty);
150 int height = gr_get_height(gProgressBarEmpty);
151
152 int dx = (gr_fb_width() - width)/2;
153 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
154
155 // Erase behind the progress bar (in case this was a progress-only update)
156 gr_color(0, 0, 0, 255);
157 gr_fill(dx, dy, width, height);
158
159 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) {
160 float progress = gProgressScopeStart + gProgress * gProgressScopeSize;
161 int pos = (int) (progress * width);
162
163 if (pos > 0) {
164 gr_blit(gProgressBarFill, 0, 0, pos, height, dx, dy);
165 }
166 if (pos < width-1) {
167 gr_blit(gProgressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
168 }
169 }
170
171 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) {
172 static int frame = 0;
173 gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
174 frame = (frame + 1) % ui_parameters.indeterminate_frames;
175 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800176 }
177}
178
179static void draw_text_line(int row, const char* t) {
180 if (t[0] != '\0') {
181 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
182 }
183}
184
185// Redraw everything on the screen. Does not flip pages.
186// Should only be called with gUpdateMutex locked.
187static void draw_screen_locked(void)
188{
189 draw_background_locked(gCurrentIcon);
190 draw_progress_locked();
191
192 if (show_text) {
193 gr_color(0, 0, 0, 160);
194 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
195
196 int i = 0;
197 if (show_menu) {
198 gr_color(64, 96, 255, 255);
199 gr_fill(0, (menu_top+menu_sel) * CHAR_HEIGHT,
200 gr_fb_width(), (menu_top+menu_sel+1)*CHAR_HEIGHT+1);
201
202 for (; i < menu_top + menu_items; ++i) {
203 if (i == menu_top + menu_sel) {
204 gr_color(255, 255, 255, 255);
205 draw_text_line(i, menu[i]);
206 gr_color(64, 96, 255, 255);
207 } else {
208 draw_text_line(i, menu[i]);
209 }
210 }
211 gr_fill(0, i*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
212 gr_fb_width(), i*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
213 ++i;
214 }
215
216 gr_color(255, 255, 0, 255);
217
218 for (; i < text_rows; ++i) {
219 draw_text_line(i, text[(i+text_top) % text_rows]);
220 }
221 }
222}
223
224// Redraw everything on the screen and flip the screen (make it visible).
225// Should only be called with gUpdateMutex locked.
226static void update_screen_locked(void)
227{
228 draw_screen_locked();
229 gr_flip();
230}
231
232// Updates only the progress bar, if possible, otherwise redraws the screen.
233// Should only be called with gUpdateMutex locked.
234static void update_progress_locked(void)
235{
236 if (show_text || !gPagesIdentical) {
237 draw_screen_locked(); // Must redraw the whole screen
238 gPagesIdentical = 1;
239 } else {
Doug Zongker6809c512011-03-01 14:04:34 -0800240 draw_progress_locked(); // Draw only the progress bar and overlays
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800241 }
242 gr_flip();
243}
244
245// Keeps the progress bar updated, even when the process is otherwise busy.
246static void *progress_thread(void *cookie)
247{
Doug Zongker6809c512011-03-01 14:04:34 -0800248 double interval = 1.0 / ui_parameters.update_fps;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800249 for (;;) {
Doug Zongker6809c512011-03-01 14:04:34 -0800250 double start = now();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800251 pthread_mutex_lock(&gUpdateMutex);
252
Doug Zongker6809c512011-03-01 14:04:34 -0800253 int redraw = 0;
254
255 // update the installation animation, if active
256 // skip this if we have a text overlay (too expensive to update)
257 if (gCurrentIcon == BACKGROUND_ICON_INSTALLING &&
258 ui_parameters.installing_frames > 0 &&
259 !show_text) {
260 gInstallingFrame =
261 (gInstallingFrame + 1) % ui_parameters.installing_frames;
262 redraw = 1;
263 }
264
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800265 // update the progress bar animation, if active
266 // skip this if we have a text overlay (too expensive to update)
267 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) {
Doug Zongker6809c512011-03-01 14:04:34 -0800268 redraw = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800269 }
270
271 // move the progress bar forward on timed intervals, if configured
272 int duration = gProgressScopeDuration;
273 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) {
Doug Zongker6809c512011-03-01 14:04:34 -0800274 double elapsed = now() - gProgressScopeTime;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800275 float progress = 1.0 * elapsed / duration;
276 if (progress > 1.0) progress = 1.0;
277 if (progress > gProgress) {
278 gProgress = progress;
Doug Zongker6809c512011-03-01 14:04:34 -0800279 redraw = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800280 }
281 }
282
Doug Zongker6809c512011-03-01 14:04:34 -0800283 if (redraw) update_progress_locked();
284
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800285 pthread_mutex_unlock(&gUpdateMutex);
Doug Zongker6809c512011-03-01 14:04:34 -0800286 double end = now();
287 // minimum of 20ms delay between frames
288 double delay = interval - (end-start);
289 if (delay < 0.02) delay = 0.02;
290 usleep((long)(delay * 1000000));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800291 }
292 return NULL;
293}
294
295// Reads input events, handles special hot keys, and adds to the key queue.
296static void *input_thread(void *cookie)
297{
298 int rel_sum = 0;
299 int fake_key = 0;
300 for (;;) {
301 // wait for the next key event
302 struct input_event ev;
303 do {
304 ev_get(&ev, 0);
305
306 if (ev.type == EV_SYN) {
307 continue;
308 } else if (ev.type == EV_REL) {
309 if (ev.code == REL_Y) {
310 // accumulate the up or down motion reported by
311 // the trackball. When it exceeds a threshold
312 // (positive or negative), fake an up/down
313 // key event.
314 rel_sum += ev.value;
315 if (rel_sum > 3) {
316 fake_key = 1;
317 ev.type = EV_KEY;
318 ev.code = KEY_DOWN;
319 ev.value = 1;
320 rel_sum = 0;
321 } else if (rel_sum < -3) {
322 fake_key = 1;
323 ev.type = EV_KEY;
324 ev.code = KEY_UP;
325 ev.value = 1;
326 rel_sum = 0;
327 }
328 }
329 } else {
330 rel_sum = 0;
331 }
332 } while (ev.type != EV_KEY || ev.code > KEY_MAX);
333
334 pthread_mutex_lock(&key_queue_mutex);
335 if (!fake_key) {
336 // our "fake" keys only report a key-down event (no
337 // key-up), so don't record them in the key_pressed
338 // table.
339 key_pressed[ev.code] = ev.value;
340 }
341 fake_key = 0;
342 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
343 if (ev.value > 0 && key_queue_len < queue_max) {
344 key_queue[key_queue_len++] = ev.code;
345 pthread_cond_signal(&key_queue_cond);
346 }
347 pthread_mutex_unlock(&key_queue_mutex);
348
Doug Zongkerddd6a282009-06-09 12:22:33 -0700349 if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800350 pthread_mutex_lock(&gUpdateMutex);
351 show_text = !show_text;
Doug Zongker5cae4452011-01-25 13:15:30 -0800352 if (show_text) show_text_ever = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800353 update_screen_locked();
354 pthread_mutex_unlock(&gUpdateMutex);
355 }
356
Doug Zongkerddd6a282009-06-09 12:22:33 -0700357 if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800358 reboot(RB_AUTOBOOT);
359 }
360 }
361 return NULL;
362}
363
364void ui_init(void)
365{
366 gr_init();
367 ev_init();
368
369 text_col = text_row = 0;
370 text_rows = gr_fb_height() / CHAR_HEIGHT;
371 if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
372 text_top = 1;
373
374 text_cols = gr_fb_width() / CHAR_WIDTH;
375 if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
376
377 int i;
378 for (i = 0; BITMAPS[i].name != NULL; ++i) {
379 int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface);
380 if (result < 0) {
Doug Zongker6809c512011-03-01 14:04:34 -0800381 LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800382 }
383 }
384
Doug Zongker6809c512011-03-01 14:04:34 -0800385 gProgressBarIndeterminate = malloc(ui_parameters.indeterminate_frames *
386 sizeof(gr_surface));
387 for (i = 0; i < ui_parameters.indeterminate_frames; ++i) {
388 char filename[40];
389 // "indeterminateN" if fewer than 10 frames, else "indeterminateNN".
390 sprintf(filename, "indeterminate%0*d",
391 ui_parameters.indeterminate_frames < 10 ? 1 : 2,
392 i+1);
393 int result = res_create_surface(filename, gProgressBarIndeterminate+i);
394 if (result < 0) {
395 LOGE("Missing bitmap %s\n(Code %d)\n", filename, result);
396 }
397 }
398
399 if (ui_parameters.installing_frames > 0) {
400 gInstallationOverlay = malloc(ui_parameters.installing_frames *
401 sizeof(gr_surface));
402 for (i = 0; i < ui_parameters.installing_frames; ++i) {
403 char filename[40];
404 sprintf(filename, "icon_installing_overlay%0*d",
405 ui_parameters.installing_frames < 10 ? 1 : 2,
406 i+1);
407 int result = res_create_surface(filename, gInstallationOverlay+i);
408 if (result < 0) {
409 LOGE("Missing bitmap %s\n(Code %d)\n", filename, result);
410 }
411 }
412
413 // Adjust the offset to account for the positioning of the
414 // base image on the screen.
415 if (gBackgroundIcon[BACKGROUND_ICON_INSTALLING] != NULL) {
416 gr_surface bg = gBackgroundIcon[BACKGROUND_ICON_INSTALLING];
417 ui_parameters.install_overlay_offset_x +=
418 (gr_fb_width() - gr_get_width(bg)) / 2;
419 ui_parameters.install_overlay_offset_y +=
420 (gr_fb_height() - gr_get_height(bg)) / 2;
421 }
422 } else {
423 gInstallationOverlay = NULL;
424 }
425
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800426 pthread_t t;
427 pthread_create(&t, NULL, progress_thread, NULL);
428 pthread_create(&t, NULL, input_thread, NULL);
429}
430
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800431void ui_set_background(int icon)
432{
433 pthread_mutex_lock(&gUpdateMutex);
Doug Zongker6809c512011-03-01 14:04:34 -0800434 gCurrentIcon = icon;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800435 update_screen_locked();
436 pthread_mutex_unlock(&gUpdateMutex);
437}
438
439void ui_show_indeterminate_progress()
440{
441 pthread_mutex_lock(&gUpdateMutex);
442 if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
443 gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
444 update_progress_locked();
445 }
446 pthread_mutex_unlock(&gUpdateMutex);
447}
448
449void ui_show_progress(float portion, int seconds)
450{
451 pthread_mutex_lock(&gUpdateMutex);
452 gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
453 gProgressScopeStart += gProgressScopeSize;
454 gProgressScopeSize = portion;
Doug Zongker6809c512011-03-01 14:04:34 -0800455 gProgressScopeTime = now();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800456 gProgressScopeDuration = seconds;
457 gProgress = 0;
458 update_progress_locked();
459 pthread_mutex_unlock(&gUpdateMutex);
460}
461
462void ui_set_progress(float fraction)
463{
464 pthread_mutex_lock(&gUpdateMutex);
465 if (fraction < 0.0) fraction = 0.0;
466 if (fraction > 1.0) fraction = 1.0;
467 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
468 // Skip updates that aren't visibly different.
469 int width = gr_get_width(gProgressBarIndeterminate[0]);
470 float scale = width * gProgressScopeSize;
471 if ((int) (gProgress * scale) != (int) (fraction * scale)) {
472 gProgress = fraction;
473 update_progress_locked();
474 }
475 }
476 pthread_mutex_unlock(&gUpdateMutex);
477}
478
479void ui_reset_progress()
480{
481 pthread_mutex_lock(&gUpdateMutex);
482 gProgressBarType = PROGRESSBAR_TYPE_NONE;
483 gProgressScopeStart = gProgressScopeSize = 0;
484 gProgressScopeTime = gProgressScopeDuration = 0;
485 gProgress = 0;
486 update_screen_locked();
487 pthread_mutex_unlock(&gUpdateMutex);
488}
489
490void ui_print(const char *fmt, ...)
491{
492 char buf[256];
493 va_list ap;
494 va_start(ap, fmt);
495 vsnprintf(buf, 256, fmt, ap);
496 va_end(ap);
497
Doug Zongker3d177d02010-07-01 09:18:44 -0700498 fputs(buf, stdout);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800499
500 // This can get called before ui_init(), so be careful.
501 pthread_mutex_lock(&gUpdateMutex);
502 if (text_rows > 0 && text_cols > 0) {
503 char *ptr;
504 for (ptr = buf; *ptr != '\0'; ++ptr) {
505 if (*ptr == '\n' || text_col >= text_cols) {
506 text[text_row][text_col] = '\0';
507 text_col = 0;
508 text_row = (text_row + 1) % text_rows;
509 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
510 }
511 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
512 }
513 text[text_row][text_col] = '\0';
514 update_screen_locked();
515 }
516 pthread_mutex_unlock(&gUpdateMutex);
517}
518
Doug Zongkerbe598882010-04-08 14:36:55 -0700519void ui_start_menu(char** headers, char** items, int initial_selection) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800520 int i;
521 pthread_mutex_lock(&gUpdateMutex);
522 if (text_rows > 0 && text_cols > 0) {
523 for (i = 0; i < text_rows; ++i) {
524 if (headers[i] == NULL) break;
525 strncpy(menu[i], headers[i], text_cols-1);
526 menu[i][text_cols-1] = '\0';
527 }
528 menu_top = i;
529 for (; i < text_rows; ++i) {
530 if (items[i-menu_top] == NULL) break;
531 strncpy(menu[i], items[i-menu_top], text_cols-1);
532 menu[i][text_cols-1] = '\0';
533 }
534 menu_items = i - menu_top;
535 show_menu = 1;
Doug Zongkerbe598882010-04-08 14:36:55 -0700536 menu_sel = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800537 update_screen_locked();
538 }
539 pthread_mutex_unlock(&gUpdateMutex);
540}
541
542int ui_menu_select(int sel) {
543 int old_sel;
544 pthread_mutex_lock(&gUpdateMutex);
545 if (show_menu > 0) {
546 old_sel = menu_sel;
547 menu_sel = sel;
548 if (menu_sel < 0) menu_sel = 0;
549 if (menu_sel >= menu_items) menu_sel = menu_items-1;
550 sel = menu_sel;
551 if (menu_sel != old_sel) update_screen_locked();
552 }
553 pthread_mutex_unlock(&gUpdateMutex);
554 return sel;
555}
556
557void ui_end_menu() {
558 int i;
559 pthread_mutex_lock(&gUpdateMutex);
560 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
561 show_menu = 0;
562 update_screen_locked();
563 }
564 pthread_mutex_unlock(&gUpdateMutex);
565}
566
567int ui_text_visible()
568{
569 pthread_mutex_lock(&gUpdateMutex);
570 int visible = show_text;
571 pthread_mutex_unlock(&gUpdateMutex);
572 return visible;
573}
574
Doug Zongker5cae4452011-01-25 13:15:30 -0800575int ui_text_ever_visible()
576{
577 pthread_mutex_lock(&gUpdateMutex);
578 int ever_visible = show_text_ever;
579 pthread_mutex_unlock(&gUpdateMutex);
580 return ever_visible;
581}
582
Doug Zongker4bc98062010-09-03 11:00:13 -0700583void ui_show_text(int visible)
584{
585 pthread_mutex_lock(&gUpdateMutex);
586 show_text = visible;
Doug Zongker5cae4452011-01-25 13:15:30 -0800587 if (show_text) show_text_ever = 1;
Doug Zongker4bc98062010-09-03 11:00:13 -0700588 update_screen_locked();
589 pthread_mutex_unlock(&gUpdateMutex);
590}
591
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800592int ui_wait_key()
593{
594 pthread_mutex_lock(&key_queue_mutex);
Doug Zongker5cae4452011-01-25 13:15:30 -0800595
596 struct timeval now;
597 struct timespec timeout;
598 gettimeofday(&now, NULL);
599 timeout.tv_sec = now.tv_sec;
600 timeout.tv_nsec = now.tv_usec * 1000;
601 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
602
603 int rc = 0;
604 while (key_queue_len == 0 && rc != ETIMEDOUT) {
605 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex,
606 &timeout);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800607 }
608
Doug Zongker5cae4452011-01-25 13:15:30 -0800609 int key = -1;
610 if (key_queue_len > 0) {
611 key = key_queue[0];
612 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
613 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800614 pthread_mutex_unlock(&key_queue_mutex);
615 return key;
616}
617
618int ui_key_pressed(int key)
619{
620 // This is a volatile static array, don't bother locking
621 return key_pressed[key];
622}
623
624void ui_clear_key_queue() {
625 pthread_mutex_lock(&key_queue_mutex);
626 key_queue_len = 0;
627 pthread_mutex_unlock(&key_queue_mutex);
628}