blob: 657a01ece7e1fb34fd56ea77dc34bb892803a9e3 [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
Doug Zongker6ce4a322011-03-08 12:25:05 -080017#include <errno.h>
18#include <fcntl.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080019#include <linux/input.h>
20#include <pthread.h>
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Doug Zongker6ce4a322011-03-08 12:25:05 -080025#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080026#include <sys/time.h>
Doug Zongker6ce4a322011-03-08 12:25:05 -080027#include <sys/types.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028#include <time.h>
29#include <unistd.h>
30
31#include "common.h"
Ken Sumrall6e4472a2011-03-07 23:37:27 -080032#include <cutils/android_reboot.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033#include "minui/minui.h"
Doug Zongker23412e62009-07-23 10:16:07 -070034#include "recovery_ui.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070035#include "ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036
Doug Zongkerf291d852010-07-07 13:55:25 -070037#define MAX_COLS 96
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038#define MAX_ROWS 32
39
40#define CHAR_WIDTH 10
41#define CHAR_HEIGHT 18
42
Doug Zongker5cae4452011-01-25 13:15:30 -080043#define UI_WAIT_KEY_TIMEOUT_SEC 120
44
Doug Zongker6809c512011-03-01 14:04:34 -080045UIParameters ui_parameters = {
Doug Zongkerbe6d4d12011-03-02 10:38:02 -080046 6, // indeterminate progress bar frames
47 20, // fps
48 7, // installation icon frames (0 == static image)
Doug Zongkerfdfb6362011-09-20 14:16:46 -070049 13, 190, // installation icon overlay offset
Doug Zongker6809c512011-03-01 14:04:34 -080050};
51
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080052static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER;
53static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS];
Doug Zongker6809c512011-03-01 14:04:34 -080054static gr_surface *gInstallationOverlay;
55static gr_surface *gProgressBarIndeterminate;
Doug Zongkerd93a2542009-10-08 16:32:58 -070056static gr_surface gProgressBarEmpty;
57static gr_surface gProgressBarFill;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058
59static const struct { gr_surface* surface; const char *name; } BITMAPS[] = {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060 { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" },
61 { &gBackgroundIcon[BACKGROUND_ICON_ERROR], "icon_error" },
Doug Zongkerd93a2542009-10-08 16:32:58 -070062 { &gProgressBarEmpty, "progress_empty" },
63 { &gProgressBarFill, "progress_fill" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080064 { NULL, NULL },
65};
66
Doug Zongker6809c512011-03-01 14:04:34 -080067static int gCurrentIcon = 0;
68static int gInstallingFrame = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080069
70static enum ProgressBarType {
71 PROGRESSBAR_TYPE_NONE,
72 PROGRESSBAR_TYPE_INDETERMINATE,
73 PROGRESSBAR_TYPE_NORMAL,
74} gProgressBarType = PROGRESSBAR_TYPE_NONE;
75
76// Progress bar scope of current operation
77static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
Doug Zongker6809c512011-03-01 14:04:34 -080078static double gProgressScopeTime, gProgressScopeDuration;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080079
80// Set to 1 when both graphics pages are the same (except for the progress bar)
81static int gPagesIdentical = 0;
82
83// Log text overlay, displayed when a magic key is pressed
84static char text[MAX_ROWS][MAX_COLS];
85static int text_cols = 0, text_rows = 0;
86static int text_col = 0, text_row = 0, text_top = 0;
87static int show_text = 0;
Doug Zongker5cae4452011-01-25 13:15:30 -080088static int show_text_ever = 0; // has show_text ever been 1?
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080089
90static char menu[MAX_ROWS][MAX_COLS];
91static int show_menu = 0;
92static int menu_top = 0, menu_items = 0, menu_sel = 0;
93
94// Key event input queue
95static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
96static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER;
97static int key_queue[256], key_queue_len = 0;
98static volatile char key_pressed[KEY_MAX + 1];
99
Doug Zongker6809c512011-03-01 14:04:34 -0800100// Return the current time as a double (including fractions of a second).
101static double now() {
102 struct timeval tv;
103 gettimeofday(&tv, NULL);
104 return tv.tv_sec + tv.tv_usec / 1000000.0;
105}
106
107// Draw the given frame over the installation overlay animation. The
108// background is not cleared or draw with the base icon first; we
109// assume that the frame already contains some other frame of the
110// animation. Does nothing if no overlay animation is defined.
111// Should only be called with gUpdateMutex locked.
112static void draw_install_overlay_locked(int frame) {
113 if (gInstallationOverlay == NULL) return;
114 gr_surface surface = gInstallationOverlay[frame];
115 int iconWidth = gr_get_width(surface);
116 int iconHeight = gr_get_height(surface);
117 gr_blit(surface, 0, 0, iconWidth, iconHeight,
118 ui_parameters.install_overlay_offset_x,
119 ui_parameters.install_overlay_offset_y);
120}
121
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800122// Clear the screen and draw the currently selected background icon (if any).
123// Should only be called with gUpdateMutex locked.
Doug Zongker6809c512011-03-01 14:04:34 -0800124static void draw_background_locked(int icon)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800125{
126 gPagesIdentical = 0;
127 gr_color(0, 0, 0, 255);
128 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
129
130 if (icon) {
Doug Zongker6809c512011-03-01 14:04:34 -0800131 gr_surface surface = gBackgroundIcon[icon];
132 int iconWidth = gr_get_width(surface);
133 int iconHeight = gr_get_height(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800134 int iconX = (gr_fb_width() - iconWidth) / 2;
135 int iconY = (gr_fb_height() - iconHeight) / 2;
Doug Zongker6809c512011-03-01 14:04:34 -0800136 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
137 if (icon == BACKGROUND_ICON_INSTALLING) {
138 draw_install_overlay_locked(gInstallingFrame);
139 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800140 }
141}
142
143// Draw the progress bar (if any) on the screen. Does not flip pages.
144// Should only be called with gUpdateMutex locked.
145static void draw_progress_locked()
146{
Doug Zongker6809c512011-03-01 14:04:34 -0800147 if (gCurrentIcon == BACKGROUND_ICON_INSTALLING) {
148 draw_install_overlay_locked(gInstallingFrame);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800149 }
150
Doug Zongker6809c512011-03-01 14:04:34 -0800151 if (gProgressBarType != PROGRESSBAR_TYPE_NONE) {
152 int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]);
153 int width = gr_get_width(gProgressBarEmpty);
154 int height = gr_get_height(gProgressBarEmpty);
155
156 int dx = (gr_fb_width() - width)/2;
157 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
158
159 // Erase behind the progress bar (in case this was a progress-only update)
160 gr_color(0, 0, 0, 255);
161 gr_fill(dx, dy, width, height);
162
163 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) {
164 float progress = gProgressScopeStart + gProgress * gProgressScopeSize;
165 int pos = (int) (progress * width);
166
167 if (pos > 0) {
168 gr_blit(gProgressBarFill, 0, 0, pos, height, dx, dy);
169 }
170 if (pos < width-1) {
171 gr_blit(gProgressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
172 }
173 }
174
175 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) {
176 static int frame = 0;
177 gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
178 frame = (frame + 1) % ui_parameters.indeterminate_frames;
179 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800180 }
181}
182
183static void draw_text_line(int row, const char* t) {
184 if (t[0] != '\0') {
185 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
186 }
187}
188
189// Redraw everything on the screen. Does not flip pages.
190// Should only be called with gUpdateMutex locked.
191static void draw_screen_locked(void)
192{
193 draw_background_locked(gCurrentIcon);
194 draw_progress_locked();
195
196 if (show_text) {
197 gr_color(0, 0, 0, 160);
198 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
199
200 int i = 0;
201 if (show_menu) {
202 gr_color(64, 96, 255, 255);
203 gr_fill(0, (menu_top+menu_sel) * CHAR_HEIGHT,
204 gr_fb_width(), (menu_top+menu_sel+1)*CHAR_HEIGHT+1);
205
206 for (; i < menu_top + menu_items; ++i) {
207 if (i == menu_top + menu_sel) {
208 gr_color(255, 255, 255, 255);
209 draw_text_line(i, menu[i]);
210 gr_color(64, 96, 255, 255);
211 } else {
212 draw_text_line(i, menu[i]);
213 }
214 }
215 gr_fill(0, i*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
216 gr_fb_width(), i*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
217 ++i;
218 }
219
220 gr_color(255, 255, 0, 255);
221
222 for (; i < text_rows; ++i) {
223 draw_text_line(i, text[(i+text_top) % text_rows]);
224 }
225 }
226}
227
228// Redraw everything on the screen and flip the screen (make it visible).
229// Should only be called with gUpdateMutex locked.
230static void update_screen_locked(void)
231{
232 draw_screen_locked();
233 gr_flip();
234}
235
236// Updates only the progress bar, if possible, otherwise redraws the screen.
237// Should only be called with gUpdateMutex locked.
238static void update_progress_locked(void)
239{
240 if (show_text || !gPagesIdentical) {
241 draw_screen_locked(); // Must redraw the whole screen
242 gPagesIdentical = 1;
243 } else {
Doug Zongker6809c512011-03-01 14:04:34 -0800244 draw_progress_locked(); // Draw only the progress bar and overlays
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800245 }
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{
Doug Zongker6809c512011-03-01 14:04:34 -0800252 double interval = 1.0 / ui_parameters.update_fps;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800253 for (;;) {
Doug Zongker6809c512011-03-01 14:04:34 -0800254 double start = now();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800255 pthread_mutex_lock(&gUpdateMutex);
256
Doug Zongker6809c512011-03-01 14:04:34 -0800257 int redraw = 0;
258
259 // update the installation animation, if active
260 // skip this if we have a text overlay (too expensive to update)
261 if (gCurrentIcon == BACKGROUND_ICON_INSTALLING &&
262 ui_parameters.installing_frames > 0 &&
263 !show_text) {
264 gInstallingFrame =
265 (gInstallingFrame + 1) % ui_parameters.installing_frames;
266 redraw = 1;
267 }
268
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800269 // update the progress bar animation, if active
270 // skip this if we have a text overlay (too expensive to update)
271 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) {
Doug Zongker6809c512011-03-01 14:04:34 -0800272 redraw = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800273 }
274
275 // move the progress bar forward on timed intervals, if configured
276 int duration = gProgressScopeDuration;
277 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) {
Doug Zongker6809c512011-03-01 14:04:34 -0800278 double elapsed = now() - gProgressScopeTime;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800279 float progress = 1.0 * elapsed / duration;
280 if (progress > 1.0) progress = 1.0;
281 if (progress > gProgress) {
282 gProgress = progress;
Doug Zongker6809c512011-03-01 14:04:34 -0800283 redraw = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800284 }
285 }
286
Doug Zongker6809c512011-03-01 14:04:34 -0800287 if (redraw) update_progress_locked();
288
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800289 pthread_mutex_unlock(&gUpdateMutex);
Doug Zongker6809c512011-03-01 14:04:34 -0800290 double end = now();
291 // minimum of 20ms delay between frames
292 double delay = interval - (end-start);
293 if (delay < 0.02) delay = 0.02;
294 usleep((long)(delay * 1000000));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800295 }
296 return NULL;
297}
298
Dima Zavinbc290632011-08-30 11:59:45 -0700299static int rel_sum = 0;
300
301static int input_callback(int fd, short revents, void *data)
302{
303 struct input_event ev;
304 int ret;
305 int fake_key = 0;
306
307 ret = ev_get_input(fd, revents, &ev);
308 if (ret)
309 return -1;
310
311 if (ev.type == EV_SYN) {
312 return 0;
313 } else if (ev.type == EV_REL) {
314 if (ev.code == REL_Y) {
315 // accumulate the up or down motion reported by
316 // the trackball. When it exceeds a threshold
317 // (positive or negative), fake an up/down
318 // key event.
319 rel_sum += ev.value;
320 if (rel_sum > 3) {
321 fake_key = 1;
322 ev.type = EV_KEY;
323 ev.code = KEY_DOWN;
324 ev.value = 1;
325 rel_sum = 0;
326 } else if (rel_sum < -3) {
327 fake_key = 1;
328 ev.type = EV_KEY;
329 ev.code = KEY_UP;
330 ev.value = 1;
331 rel_sum = 0;
332 }
333 }
334 } else {
335 rel_sum = 0;
336 }
337
338 if (ev.type != EV_KEY || ev.code > KEY_MAX)
339 return 0;
340
341 pthread_mutex_lock(&key_queue_mutex);
342 if (!fake_key) {
343 // our "fake" keys only report a key-down event (no
344 // key-up), so don't record them in the key_pressed
345 // table.
346 key_pressed[ev.code] = ev.value;
347 }
348 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
349 if (ev.value > 0 && key_queue_len < queue_max) {
350 key_queue[key_queue_len++] = ev.code;
351 pthread_cond_signal(&key_queue_cond);
352 }
353 pthread_mutex_unlock(&key_queue_mutex);
354
355 if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) {
356 pthread_mutex_lock(&gUpdateMutex);
357 show_text = !show_text;
358 if (show_text) show_text_ever = 1;
359 update_screen_locked();
360 pthread_mutex_unlock(&gUpdateMutex);
361 }
362
363 if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) {
364 android_reboot(ANDROID_RB_RESTART, 0, 0);
365 }
366
367 return 0;
368}
369
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800370// Reads input events, handles special hot keys, and adds to the key queue.
371static void *input_thread(void *cookie)
372{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800373 for (;;) {
Dima Zavinbc290632011-08-30 11:59:45 -0700374 if (!ev_wait(-1))
375 ev_dispatch();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800376 }
377 return NULL;
378}
379
380void ui_init(void)
381{
382 gr_init();
Dima Zavinbc290632011-08-30 11:59:45 -0700383 ev_init(input_callback, NULL);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800384
385 text_col = text_row = 0;
386 text_rows = gr_fb_height() / CHAR_HEIGHT;
387 if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
388 text_top = 1;
389
390 text_cols = gr_fb_width() / CHAR_WIDTH;
391 if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
392
393 int i;
394 for (i = 0; BITMAPS[i].name != NULL; ++i) {
395 int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface);
396 if (result < 0) {
Doug Zongker6809c512011-03-01 14:04:34 -0800397 LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800398 }
399 }
400
Doug Zongker10e418d2011-10-28 10:33:05 -0700401 gProgressBarIndeterminate = (gr_surface*)malloc(ui_parameters.indeterminate_frames *
402 sizeof(gr_surface));
Doug Zongker6809c512011-03-01 14:04:34 -0800403 for (i = 0; i < ui_parameters.indeterminate_frames; ++i) {
404 char filename[40];
Doug Zongkerbe6d4d12011-03-02 10:38:02 -0800405 // "indeterminate01.png", "indeterminate02.png", ...
406 sprintf(filename, "indeterminate%02d", i+1);
Doug Zongker6809c512011-03-01 14:04:34 -0800407 int result = res_create_surface(filename, gProgressBarIndeterminate+i);
408 if (result < 0) {
409 LOGE("Missing bitmap %s\n(Code %d)\n", filename, result);
410 }
411 }
412
413 if (ui_parameters.installing_frames > 0) {
Doug Zongker10e418d2011-10-28 10:33:05 -0700414 gInstallationOverlay = (gr_surface*)malloc(ui_parameters.installing_frames *
415 sizeof(gr_surface));
Doug Zongker6809c512011-03-01 14:04:34 -0800416 for (i = 0; i < ui_parameters.installing_frames; ++i) {
417 char filename[40];
Doug Zongkerbe6d4d12011-03-02 10:38:02 -0800418 // "icon_installing_overlay01.png",
419 // "icon_installing_overlay02.png", ...
420 sprintf(filename, "icon_installing_overlay%02d", i+1);
Doug Zongker6809c512011-03-01 14:04:34 -0800421 int result = res_create_surface(filename, gInstallationOverlay+i);
422 if (result < 0) {
423 LOGE("Missing bitmap %s\n(Code %d)\n", filename, result);
424 }
425 }
426
427 // Adjust the offset to account for the positioning of the
428 // base image on the screen.
429 if (gBackgroundIcon[BACKGROUND_ICON_INSTALLING] != NULL) {
430 gr_surface bg = gBackgroundIcon[BACKGROUND_ICON_INSTALLING];
431 ui_parameters.install_overlay_offset_x +=
432 (gr_fb_width() - gr_get_width(bg)) / 2;
433 ui_parameters.install_overlay_offset_y +=
434 (gr_fb_height() - gr_get_height(bg)) / 2;
435 }
436 } else {
437 gInstallationOverlay = NULL;
438 }
439
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800440 pthread_t t;
441 pthread_create(&t, NULL, progress_thread, NULL);
442 pthread_create(&t, NULL, input_thread, NULL);
443}
444
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800445void ui_set_background(int icon)
446{
447 pthread_mutex_lock(&gUpdateMutex);
Doug Zongker6809c512011-03-01 14:04:34 -0800448 gCurrentIcon = icon;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800449 update_screen_locked();
450 pthread_mutex_unlock(&gUpdateMutex);
451}
452
453void ui_show_indeterminate_progress()
454{
455 pthread_mutex_lock(&gUpdateMutex);
456 if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
457 gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
458 update_progress_locked();
459 }
460 pthread_mutex_unlock(&gUpdateMutex);
461}
462
463void ui_show_progress(float portion, int seconds)
464{
465 pthread_mutex_lock(&gUpdateMutex);
466 gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
467 gProgressScopeStart += gProgressScopeSize;
468 gProgressScopeSize = portion;
Doug Zongker6809c512011-03-01 14:04:34 -0800469 gProgressScopeTime = now();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800470 gProgressScopeDuration = seconds;
471 gProgress = 0;
472 update_progress_locked();
473 pthread_mutex_unlock(&gUpdateMutex);
474}
475
476void ui_set_progress(float fraction)
477{
478 pthread_mutex_lock(&gUpdateMutex);
479 if (fraction < 0.0) fraction = 0.0;
480 if (fraction > 1.0) fraction = 1.0;
481 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
482 // Skip updates that aren't visibly different.
483 int width = gr_get_width(gProgressBarIndeterminate[0]);
484 float scale = width * gProgressScopeSize;
485 if ((int) (gProgress * scale) != (int) (fraction * scale)) {
486 gProgress = fraction;
487 update_progress_locked();
488 }
489 }
490 pthread_mutex_unlock(&gUpdateMutex);
491}
492
493void ui_reset_progress()
494{
495 pthread_mutex_lock(&gUpdateMutex);
496 gProgressBarType = PROGRESSBAR_TYPE_NONE;
497 gProgressScopeStart = gProgressScopeSize = 0;
498 gProgressScopeTime = gProgressScopeDuration = 0;
499 gProgress = 0;
500 update_screen_locked();
501 pthread_mutex_unlock(&gUpdateMutex);
502}
503
504void ui_print(const char *fmt, ...)
505{
506 char buf[256];
507 va_list ap;
508 va_start(ap, fmt);
509 vsnprintf(buf, 256, fmt, ap);
510 va_end(ap);
511
Doug Zongker3d177d02010-07-01 09:18:44 -0700512 fputs(buf, stdout);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800513
514 // This can get called before ui_init(), so be careful.
515 pthread_mutex_lock(&gUpdateMutex);
516 if (text_rows > 0 && text_cols > 0) {
517 char *ptr;
518 for (ptr = buf; *ptr != '\0'; ++ptr) {
519 if (*ptr == '\n' || text_col >= text_cols) {
520 text[text_row][text_col] = '\0';
521 text_col = 0;
522 text_row = (text_row + 1) % text_rows;
523 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
524 }
525 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
526 }
527 text[text_row][text_col] = '\0';
528 update_screen_locked();
529 }
530 pthread_mutex_unlock(&gUpdateMutex);
531}
532
Doug Zongker10e418d2011-10-28 10:33:05 -0700533void ui_start_menu(const char* const * headers, const char* const * items,
534 int initial_selection) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800535 int i;
536 pthread_mutex_lock(&gUpdateMutex);
537 if (text_rows > 0 && text_cols > 0) {
538 for (i = 0; i < text_rows; ++i) {
539 if (headers[i] == NULL) break;
540 strncpy(menu[i], headers[i], text_cols-1);
541 menu[i][text_cols-1] = '\0';
542 }
543 menu_top = i;
544 for (; i < text_rows; ++i) {
545 if (items[i-menu_top] == NULL) break;
546 strncpy(menu[i], items[i-menu_top], text_cols-1);
547 menu[i][text_cols-1] = '\0';
548 }
549 menu_items = i - menu_top;
550 show_menu = 1;
Doug Zongkerbe598882010-04-08 14:36:55 -0700551 menu_sel = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800552 update_screen_locked();
553 }
554 pthread_mutex_unlock(&gUpdateMutex);
555}
556
557int ui_menu_select(int sel) {
558 int old_sel;
559 pthread_mutex_lock(&gUpdateMutex);
560 if (show_menu > 0) {
561 old_sel = menu_sel;
562 menu_sel = sel;
563 if (menu_sel < 0) menu_sel = 0;
564 if (menu_sel >= menu_items) menu_sel = menu_items-1;
565 sel = menu_sel;
566 if (menu_sel != old_sel) update_screen_locked();
567 }
568 pthread_mutex_unlock(&gUpdateMutex);
569 return sel;
570}
571
572void ui_end_menu() {
573 int i;
574 pthread_mutex_lock(&gUpdateMutex);
575 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
576 show_menu = 0;
577 update_screen_locked();
578 }
579 pthread_mutex_unlock(&gUpdateMutex);
580}
581
582int ui_text_visible()
583{
584 pthread_mutex_lock(&gUpdateMutex);
585 int visible = show_text;
586 pthread_mutex_unlock(&gUpdateMutex);
587 return visible;
588}
589
Doug Zongker5cae4452011-01-25 13:15:30 -0800590int ui_text_ever_visible()
591{
592 pthread_mutex_lock(&gUpdateMutex);
593 int ever_visible = show_text_ever;
594 pthread_mutex_unlock(&gUpdateMutex);
595 return ever_visible;
596}
597
Doug Zongker4bc98062010-09-03 11:00:13 -0700598void ui_show_text(int visible)
599{
600 pthread_mutex_lock(&gUpdateMutex);
601 show_text = visible;
Doug Zongker5cae4452011-01-25 13:15:30 -0800602 if (show_text) show_text_ever = 1;
Doug Zongker4bc98062010-09-03 11:00:13 -0700603 update_screen_locked();
604 pthread_mutex_unlock(&gUpdateMutex);
605}
606
Doug Zongker6ce4a322011-03-08 12:25:05 -0800607// Return true if USB is connected.
608static int usb_connected() {
Benoit Goby7e6067e2011-06-24 17:54:19 -0700609 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
Doug Zongker6ce4a322011-03-08 12:25:05 -0800610 if (fd < 0) {
Benoit Goby7e6067e2011-06-24 17:54:19 -0700611 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
Doug Zongker6ce4a322011-03-08 12:25:05 -0800612 strerror(errno));
613 return 0;
614 }
615
616 char buf;
Benoit Goby7e6067e2011-06-24 17:54:19 -0700617 /* USB is connected if android_usb state is CONNECTED or CONFIGURED */
618 int connected = (read(fd, &buf, 1) == 1) && (buf == 'C');
Doug Zongker6ce4a322011-03-08 12:25:05 -0800619 if (close(fd) < 0) {
Benoit Goby7e6067e2011-06-24 17:54:19 -0700620 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
Doug Zongker6ce4a322011-03-08 12:25:05 -0800621 strerror(errno));
622 }
623 return connected;
624}
625
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800626int ui_wait_key()
627{
628 pthread_mutex_lock(&key_queue_mutex);
Doug Zongker5cae4452011-01-25 13:15:30 -0800629
Doug Zongker6ce4a322011-03-08 12:25:05 -0800630 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
631 // plugged in.
632 do {
633 struct timeval now;
634 struct timespec timeout;
635 gettimeofday(&now, NULL);
636 timeout.tv_sec = now.tv_sec;
637 timeout.tv_nsec = now.tv_usec * 1000;
638 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
Doug Zongker5cae4452011-01-25 13:15:30 -0800639
Doug Zongker6ce4a322011-03-08 12:25:05 -0800640 int rc = 0;
641 while (key_queue_len == 0 && rc != ETIMEDOUT) {
642 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex,
643 &timeout);
644 }
645 } while (usb_connected() && key_queue_len == 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800646
Doug Zongker5cae4452011-01-25 13:15:30 -0800647 int key = -1;
648 if (key_queue_len > 0) {
649 key = key_queue[0];
650 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
651 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800652 pthread_mutex_unlock(&key_queue_mutex);
653 return key;
654}
655
656int ui_key_pressed(int key)
657{
658 // This is a volatile static array, don't bother locking
659 return key_pressed[key];
660}
661
662void ui_clear_key_queue() {
663 pthread_mutex_lock(&key_queue_mutex);
664 key_queue_len = 0;
665 pthread_mutex_unlock(&key_queue_mutex);
666}