blob: 1736319260074856ffd83923d37213048f883eba [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"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035
Doug Zongkerf291d852010-07-07 13:55:25 -070036#define MAX_COLS 96
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037#define MAX_ROWS 32
38
39#define CHAR_WIDTH 10
40#define CHAR_HEIGHT 18
41
Doug Zongker5cae4452011-01-25 13:15:30 -080042#define UI_WAIT_KEY_TIMEOUT_SEC 120
43
Doug Zongker6809c512011-03-01 14:04:34 -080044UIParameters ui_parameters = {
Doug Zongkerbe6d4d12011-03-02 10:38:02 -080045 6, // indeterminate progress bar frames
46 20, // fps
47 7, // installation icon frames (0 == static image)
48 23, 83, // installation icon overlay offset
Doug Zongker6809c512011-03-01 14:04:34 -080049};
50
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER;
52static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS];
Doug Zongker6809c512011-03-01 14:04:34 -080053static gr_surface *gInstallationOverlay;
54static gr_surface *gProgressBarIndeterminate;
Doug Zongkerd93a2542009-10-08 16:32:58 -070055static gr_surface gProgressBarEmpty;
56static gr_surface gProgressBarFill;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080057
58static const struct { gr_surface* surface; const char *name; } BITMAPS[] = {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080059 { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" },
60 { &gBackgroundIcon[BACKGROUND_ICON_ERROR], "icon_error" },
Doug Zongkerd93a2542009-10-08 16:32:58 -070061 { &gProgressBarEmpty, "progress_empty" },
62 { &gProgressBarFill, "progress_fill" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080063 { NULL, NULL },
64};
65
Doug Zongker6809c512011-03-01 14:04:34 -080066static int gCurrentIcon = 0;
67static int gInstallingFrame = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080068
69static enum ProgressBarType {
70 PROGRESSBAR_TYPE_NONE,
71 PROGRESSBAR_TYPE_INDETERMINATE,
72 PROGRESSBAR_TYPE_NORMAL,
73} gProgressBarType = PROGRESSBAR_TYPE_NONE;
74
75// Progress bar scope of current operation
76static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
Doug Zongker6809c512011-03-01 14:04:34 -080077static double gProgressScopeTime, gProgressScopeDuration;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080078
79// Set to 1 when both graphics pages are the same (except for the progress bar)
80static int gPagesIdentical = 0;
81
82// Log text overlay, displayed when a magic key is pressed
83static char text[MAX_ROWS][MAX_COLS];
84static int text_cols = 0, text_rows = 0;
85static int text_col = 0, text_row = 0, text_top = 0;
86static int show_text = 0;
Doug Zongker5cae4452011-01-25 13:15:30 -080087static int show_text_ever = 0; // has show_text ever been 1?
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080088
89static char menu[MAX_ROWS][MAX_COLS];
90static int show_menu = 0;
91static int menu_top = 0, menu_items = 0, menu_sel = 0;
92
93// Key event input queue
94static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
95static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER;
96static int key_queue[256], key_queue_len = 0;
97static volatile char key_pressed[KEY_MAX + 1];
98
Doug Zongker6809c512011-03-01 14:04:34 -080099// Return the current time as a double (including fractions of a second).
100static double now() {
101 struct timeval tv;
102 gettimeofday(&tv, NULL);
103 return tv.tv_sec + tv.tv_usec / 1000000.0;
104}
105
106// Draw the given frame over the installation overlay animation. The
107// background is not cleared or draw with the base icon first; we
108// assume that the frame already contains some other frame of the
109// animation. Does nothing if no overlay animation is defined.
110// Should only be called with gUpdateMutex locked.
111static void draw_install_overlay_locked(int frame) {
112 if (gInstallationOverlay == NULL) return;
113 gr_surface surface = gInstallationOverlay[frame];
114 int iconWidth = gr_get_width(surface);
115 int iconHeight = gr_get_height(surface);
116 gr_blit(surface, 0, 0, iconWidth, iconHeight,
117 ui_parameters.install_overlay_offset_x,
118 ui_parameters.install_overlay_offset_y);
119}
120
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800121// Clear the screen and draw the currently selected background icon (if any).
122// Should only be called with gUpdateMutex locked.
Doug Zongker6809c512011-03-01 14:04:34 -0800123static void draw_background_locked(int icon)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800124{
125 gPagesIdentical = 0;
126 gr_color(0, 0, 0, 255);
127 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
128
129 if (icon) {
Doug Zongker6809c512011-03-01 14:04:34 -0800130 gr_surface surface = gBackgroundIcon[icon];
131 int iconWidth = gr_get_width(surface);
132 int iconHeight = gr_get_height(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800133 int iconX = (gr_fb_width() - iconWidth) / 2;
134 int iconY = (gr_fb_height() - iconHeight) / 2;
Doug Zongker6809c512011-03-01 14:04:34 -0800135 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
136 if (icon == BACKGROUND_ICON_INSTALLING) {
137 draw_install_overlay_locked(gInstallingFrame);
138 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800139 }
140}
141
142// Draw the progress bar (if any) on the screen. Does not flip pages.
143// Should only be called with gUpdateMutex locked.
144static void draw_progress_locked()
145{
Doug Zongker6809c512011-03-01 14:04:34 -0800146 if (gCurrentIcon == BACKGROUND_ICON_INSTALLING) {
147 draw_install_overlay_locked(gInstallingFrame);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800148 }
149
Doug Zongker6809c512011-03-01 14:04:34 -0800150 if (gProgressBarType != PROGRESSBAR_TYPE_NONE) {
151 int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]);
152 int width = gr_get_width(gProgressBarEmpty);
153 int height = gr_get_height(gProgressBarEmpty);
154
155 int dx = (gr_fb_width() - width)/2;
156 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
157
158 // Erase behind the progress bar (in case this was a progress-only update)
159 gr_color(0, 0, 0, 255);
160 gr_fill(dx, dy, width, height);
161
162 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) {
163 float progress = gProgressScopeStart + gProgress * gProgressScopeSize;
164 int pos = (int) (progress * width);
165
166 if (pos > 0) {
167 gr_blit(gProgressBarFill, 0, 0, pos, height, dx, dy);
168 }
169 if (pos < width-1) {
170 gr_blit(gProgressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
171 }
172 }
173
174 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) {
175 static int frame = 0;
176 gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
177 frame = (frame + 1) % ui_parameters.indeterminate_frames;
178 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800179 }
180}
181
182static void draw_text_line(int row, const char* t) {
183 if (t[0] != '\0') {
184 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
185 }
186}
187
188// Redraw everything on the screen. Does not flip pages.
189// Should only be called with gUpdateMutex locked.
190static void draw_screen_locked(void)
191{
192 draw_background_locked(gCurrentIcon);
193 draw_progress_locked();
194
195 if (show_text) {
196 gr_color(0, 0, 0, 160);
197 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
198
199 int i = 0;
200 if (show_menu) {
201 gr_color(64, 96, 255, 255);
202 gr_fill(0, (menu_top+menu_sel) * CHAR_HEIGHT,
203 gr_fb_width(), (menu_top+menu_sel+1)*CHAR_HEIGHT+1);
204
205 for (; i < menu_top + menu_items; ++i) {
206 if (i == menu_top + menu_sel) {
207 gr_color(255, 255, 255, 255);
208 draw_text_line(i, menu[i]);
209 gr_color(64, 96, 255, 255);
210 } else {
211 draw_text_line(i, menu[i]);
212 }
213 }
214 gr_fill(0, i*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
215 gr_fb_width(), i*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
216 ++i;
217 }
218
219 gr_color(255, 255, 0, 255);
220
221 for (; i < text_rows; ++i) {
222 draw_text_line(i, text[(i+text_top) % text_rows]);
223 }
224 }
225}
226
227// Redraw everything on the screen and flip the screen (make it visible).
228// Should only be called with gUpdateMutex locked.
229static void update_screen_locked(void)
230{
231 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{
239 if (show_text || !gPagesIdentical) {
240 draw_screen_locked(); // Must redraw the whole screen
241 gPagesIdentical = 1;
242 } else {
Doug Zongker6809c512011-03-01 14:04:34 -0800243 draw_progress_locked(); // Draw only the progress bar and overlays
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800244 }
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{
Doug Zongker6809c512011-03-01 14:04:34 -0800251 double interval = 1.0 / ui_parameters.update_fps;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800252 for (;;) {
Doug Zongker6809c512011-03-01 14:04:34 -0800253 double start = now();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800254 pthread_mutex_lock(&gUpdateMutex);
255
Doug Zongker6809c512011-03-01 14:04:34 -0800256 int redraw = 0;
257
258 // update the installation animation, if active
259 // skip this if we have a text overlay (too expensive to update)
260 if (gCurrentIcon == BACKGROUND_ICON_INSTALLING &&
261 ui_parameters.installing_frames > 0 &&
262 !show_text) {
263 gInstallingFrame =
264 (gInstallingFrame + 1) % ui_parameters.installing_frames;
265 redraw = 1;
266 }
267
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800268 // update the progress bar animation, if active
269 // skip this if we have a text overlay (too expensive to update)
270 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) {
Doug Zongker6809c512011-03-01 14:04:34 -0800271 redraw = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800272 }
273
274 // move the progress bar forward on timed intervals, if configured
275 int duration = gProgressScopeDuration;
276 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) {
Doug Zongker6809c512011-03-01 14:04:34 -0800277 double elapsed = now() - gProgressScopeTime;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800278 float progress = 1.0 * elapsed / duration;
279 if (progress > 1.0) progress = 1.0;
280 if (progress > gProgress) {
281 gProgress = progress;
Doug Zongker6809c512011-03-01 14:04:34 -0800282 redraw = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800283 }
284 }
285
Doug Zongker6809c512011-03-01 14:04:34 -0800286 if (redraw) update_progress_locked();
287
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800288 pthread_mutex_unlock(&gUpdateMutex);
Doug Zongker6809c512011-03-01 14:04:34 -0800289 double end = now();
290 // minimum of 20ms delay between frames
291 double delay = interval - (end-start);
292 if (delay < 0.02) delay = 0.02;
293 usleep((long)(delay * 1000000));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800294 }
295 return NULL;
296}
297
298// Reads input events, handles special hot keys, and adds to the key queue.
299static void *input_thread(void *cookie)
300{
301 int rel_sum = 0;
302 int fake_key = 0;
303 for (;;) {
304 // wait for the next key event
305 struct input_event ev;
306 do {
307 ev_get(&ev, 0);
308
309 if (ev.type == EV_SYN) {
310 continue;
311 } else if (ev.type == EV_REL) {
312 if (ev.code == REL_Y) {
313 // accumulate the up or down motion reported by
314 // the trackball. When it exceeds a threshold
315 // (positive or negative), fake an up/down
316 // key event.
317 rel_sum += ev.value;
318 if (rel_sum > 3) {
319 fake_key = 1;
320 ev.type = EV_KEY;
321 ev.code = KEY_DOWN;
322 ev.value = 1;
323 rel_sum = 0;
324 } else if (rel_sum < -3) {
325 fake_key = 1;
326 ev.type = EV_KEY;
327 ev.code = KEY_UP;
328 ev.value = 1;
329 rel_sum = 0;
330 }
331 }
332 } else {
333 rel_sum = 0;
334 }
335 } while (ev.type != EV_KEY || ev.code > KEY_MAX);
336
337 pthread_mutex_lock(&key_queue_mutex);
338 if (!fake_key) {
339 // our "fake" keys only report a key-down event (no
340 // key-up), so don't record them in the key_pressed
341 // table.
342 key_pressed[ev.code] = ev.value;
343 }
344 fake_key = 0;
345 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
346 if (ev.value > 0 && key_queue_len < queue_max) {
347 key_queue[key_queue_len++] = ev.code;
348 pthread_cond_signal(&key_queue_cond);
349 }
350 pthread_mutex_unlock(&key_queue_mutex);
351
Doug Zongkerddd6a282009-06-09 12:22:33 -0700352 if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800353 pthread_mutex_lock(&gUpdateMutex);
354 show_text = !show_text;
Doug Zongker5cae4452011-01-25 13:15:30 -0800355 if (show_text) show_text_ever = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800356 update_screen_locked();
357 pthread_mutex_unlock(&gUpdateMutex);
358 }
359
Doug Zongkerddd6a282009-06-09 12:22:33 -0700360 if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) {
Ken Sumrall6e4472a2011-03-07 23:37:27 -0800361 android_reboot(ANDROID_RB_RESTART, 0, 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800362 }
363 }
364 return NULL;
365}
366
367void ui_init(void)
368{
369 gr_init();
370 ev_init();
371
372 text_col = text_row = 0;
373 text_rows = gr_fb_height() / CHAR_HEIGHT;
374 if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
375 text_top = 1;
376
377 text_cols = gr_fb_width() / CHAR_WIDTH;
378 if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
379
380 int i;
381 for (i = 0; BITMAPS[i].name != NULL; ++i) {
382 int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface);
383 if (result < 0) {
Doug Zongker6809c512011-03-01 14:04:34 -0800384 LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800385 }
386 }
387
Doug Zongker6809c512011-03-01 14:04:34 -0800388 gProgressBarIndeterminate = malloc(ui_parameters.indeterminate_frames *
389 sizeof(gr_surface));
390 for (i = 0; i < ui_parameters.indeterminate_frames; ++i) {
391 char filename[40];
Doug Zongkerbe6d4d12011-03-02 10:38:02 -0800392 // "indeterminate01.png", "indeterminate02.png", ...
393 sprintf(filename, "indeterminate%02d", i+1);
Doug Zongker6809c512011-03-01 14:04:34 -0800394 int result = res_create_surface(filename, gProgressBarIndeterminate+i);
395 if (result < 0) {
396 LOGE("Missing bitmap %s\n(Code %d)\n", filename, result);
397 }
398 }
399
400 if (ui_parameters.installing_frames > 0) {
401 gInstallationOverlay = malloc(ui_parameters.installing_frames *
402 sizeof(gr_surface));
403 for (i = 0; i < ui_parameters.installing_frames; ++i) {
404 char filename[40];
Doug Zongkerbe6d4d12011-03-02 10:38:02 -0800405 // "icon_installing_overlay01.png",
406 // "icon_installing_overlay02.png", ...
407 sprintf(filename, "icon_installing_overlay%02d", i+1);
Doug Zongker6809c512011-03-01 14:04:34 -0800408 int result = res_create_surface(filename, gInstallationOverlay+i);
409 if (result < 0) {
410 LOGE("Missing bitmap %s\n(Code %d)\n", filename, result);
411 }
412 }
413
414 // Adjust the offset to account for the positioning of the
415 // base image on the screen.
416 if (gBackgroundIcon[BACKGROUND_ICON_INSTALLING] != NULL) {
417 gr_surface bg = gBackgroundIcon[BACKGROUND_ICON_INSTALLING];
418 ui_parameters.install_overlay_offset_x +=
419 (gr_fb_width() - gr_get_width(bg)) / 2;
420 ui_parameters.install_overlay_offset_y +=
421 (gr_fb_height() - gr_get_height(bg)) / 2;
422 }
423 } else {
424 gInstallationOverlay = NULL;
425 }
426
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800427 pthread_t t;
428 pthread_create(&t, NULL, progress_thread, NULL);
429 pthread_create(&t, NULL, input_thread, NULL);
430}
431
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800432void ui_set_background(int icon)
433{
434 pthread_mutex_lock(&gUpdateMutex);
Doug Zongker6809c512011-03-01 14:04:34 -0800435 gCurrentIcon = icon;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800436 update_screen_locked();
437 pthread_mutex_unlock(&gUpdateMutex);
438}
439
440void ui_show_indeterminate_progress()
441{
442 pthread_mutex_lock(&gUpdateMutex);
443 if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
444 gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
445 update_progress_locked();
446 }
447 pthread_mutex_unlock(&gUpdateMutex);
448}
449
450void ui_show_progress(float portion, int seconds)
451{
452 pthread_mutex_lock(&gUpdateMutex);
453 gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
454 gProgressScopeStart += gProgressScopeSize;
455 gProgressScopeSize = portion;
Doug Zongker6809c512011-03-01 14:04:34 -0800456 gProgressScopeTime = now();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800457 gProgressScopeDuration = seconds;
458 gProgress = 0;
459 update_progress_locked();
460 pthread_mutex_unlock(&gUpdateMutex);
461}
462
463void ui_set_progress(float fraction)
464{
465 pthread_mutex_lock(&gUpdateMutex);
466 if (fraction < 0.0) fraction = 0.0;
467 if (fraction > 1.0) fraction = 1.0;
468 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
469 // Skip updates that aren't visibly different.
470 int width = gr_get_width(gProgressBarIndeterminate[0]);
471 float scale = width * gProgressScopeSize;
472 if ((int) (gProgress * scale) != (int) (fraction * scale)) {
473 gProgress = fraction;
474 update_progress_locked();
475 }
476 }
477 pthread_mutex_unlock(&gUpdateMutex);
478}
479
480void ui_reset_progress()
481{
482 pthread_mutex_lock(&gUpdateMutex);
483 gProgressBarType = PROGRESSBAR_TYPE_NONE;
484 gProgressScopeStart = gProgressScopeSize = 0;
485 gProgressScopeTime = gProgressScopeDuration = 0;
486 gProgress = 0;
487 update_screen_locked();
488 pthread_mutex_unlock(&gUpdateMutex);
489}
490
491void ui_print(const char *fmt, ...)
492{
493 char buf[256];
494 va_list ap;
495 va_start(ap, fmt);
496 vsnprintf(buf, 256, fmt, ap);
497 va_end(ap);
498
Doug Zongker3d177d02010-07-01 09:18:44 -0700499 fputs(buf, stdout);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800500
501 // This can get called before ui_init(), so be careful.
502 pthread_mutex_lock(&gUpdateMutex);
503 if (text_rows > 0 && text_cols > 0) {
504 char *ptr;
505 for (ptr = buf; *ptr != '\0'; ++ptr) {
506 if (*ptr == '\n' || text_col >= text_cols) {
507 text[text_row][text_col] = '\0';
508 text_col = 0;
509 text_row = (text_row + 1) % text_rows;
510 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
511 }
512 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
513 }
514 text[text_row][text_col] = '\0';
515 update_screen_locked();
516 }
517 pthread_mutex_unlock(&gUpdateMutex);
518}
519
Doug Zongkerbe598882010-04-08 14:36:55 -0700520void ui_start_menu(char** headers, char** items, int initial_selection) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800521 int i;
522 pthread_mutex_lock(&gUpdateMutex);
523 if (text_rows > 0 && text_cols > 0) {
524 for (i = 0; i < text_rows; ++i) {
525 if (headers[i] == NULL) break;
526 strncpy(menu[i], headers[i], text_cols-1);
527 menu[i][text_cols-1] = '\0';
528 }
529 menu_top = i;
530 for (; i < text_rows; ++i) {
531 if (items[i-menu_top] == NULL) break;
532 strncpy(menu[i], items[i-menu_top], text_cols-1);
533 menu[i][text_cols-1] = '\0';
534 }
535 menu_items = i - menu_top;
536 show_menu = 1;
Doug Zongkerbe598882010-04-08 14:36:55 -0700537 menu_sel = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800538 update_screen_locked();
539 }
540 pthread_mutex_unlock(&gUpdateMutex);
541}
542
543int ui_menu_select(int sel) {
544 int old_sel;
545 pthread_mutex_lock(&gUpdateMutex);
546 if (show_menu > 0) {
547 old_sel = menu_sel;
548 menu_sel = sel;
549 if (menu_sel < 0) menu_sel = 0;
550 if (menu_sel >= menu_items) menu_sel = menu_items-1;
551 sel = menu_sel;
552 if (menu_sel != old_sel) update_screen_locked();
553 }
554 pthread_mutex_unlock(&gUpdateMutex);
555 return sel;
556}
557
558void ui_end_menu() {
559 int i;
560 pthread_mutex_lock(&gUpdateMutex);
561 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
562 show_menu = 0;
563 update_screen_locked();
564 }
565 pthread_mutex_unlock(&gUpdateMutex);
566}
567
568int ui_text_visible()
569{
570 pthread_mutex_lock(&gUpdateMutex);
571 int visible = show_text;
572 pthread_mutex_unlock(&gUpdateMutex);
573 return visible;
574}
575
Doug Zongker5cae4452011-01-25 13:15:30 -0800576int ui_text_ever_visible()
577{
578 pthread_mutex_lock(&gUpdateMutex);
579 int ever_visible = show_text_ever;
580 pthread_mutex_unlock(&gUpdateMutex);
581 return ever_visible;
582}
583
Doug Zongker4bc98062010-09-03 11:00:13 -0700584void ui_show_text(int visible)
585{
586 pthread_mutex_lock(&gUpdateMutex);
587 show_text = visible;
Doug Zongker5cae4452011-01-25 13:15:30 -0800588 if (show_text) show_text_ever = 1;
Doug Zongker4bc98062010-09-03 11:00:13 -0700589 update_screen_locked();
590 pthread_mutex_unlock(&gUpdateMutex);
591}
592
Doug Zongker6ce4a322011-03-08 12:25:05 -0800593// Return true if USB is connected.
594static int usb_connected() {
Benoit Goby7e6067e2011-06-24 17:54:19 -0700595 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
Doug Zongker6ce4a322011-03-08 12:25:05 -0800596 if (fd < 0) {
Benoit Goby7e6067e2011-06-24 17:54:19 -0700597 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
Doug Zongker6ce4a322011-03-08 12:25:05 -0800598 strerror(errno));
599 return 0;
600 }
601
602 char buf;
Benoit Goby7e6067e2011-06-24 17:54:19 -0700603 /* USB is connected if android_usb state is CONNECTED or CONFIGURED */
604 int connected = (read(fd, &buf, 1) == 1) && (buf == 'C');
Doug Zongker6ce4a322011-03-08 12:25:05 -0800605 if (close(fd) < 0) {
Benoit Goby7e6067e2011-06-24 17:54:19 -0700606 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
Doug Zongker6ce4a322011-03-08 12:25:05 -0800607 strerror(errno));
608 }
609 return connected;
610}
611
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800612int ui_wait_key()
613{
614 pthread_mutex_lock(&key_queue_mutex);
Doug Zongker5cae4452011-01-25 13:15:30 -0800615
Doug Zongker6ce4a322011-03-08 12:25:05 -0800616 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
617 // plugged in.
618 do {
619 struct timeval now;
620 struct timespec timeout;
621 gettimeofday(&now, NULL);
622 timeout.tv_sec = now.tv_sec;
623 timeout.tv_nsec = now.tv_usec * 1000;
624 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
Doug Zongker5cae4452011-01-25 13:15:30 -0800625
Doug Zongker6ce4a322011-03-08 12:25:05 -0800626 int rc = 0;
627 while (key_queue_len == 0 && rc != ETIMEDOUT) {
628 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex,
629 &timeout);
630 }
631 } while (usb_connected() && key_queue_len == 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800632
Doug Zongker5cae4452011-01-25 13:15:30 -0800633 int key = -1;
634 if (key_queue_len > 0) {
635 key = key_queue[0];
636 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
637 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800638 pthread_mutex_unlock(&key_queue_mutex);
639 return key;
640}
641
642int ui_key_pressed(int key)
643{
644 // This is a volatile static array, don't bother locking
645 return key_pressed[key];
646}
647
648void ui_clear_key_queue() {
649 pthread_mutex_lock(&key_queue_mutex);
650 key_queue_len = 0;
651 pthread_mutex_unlock(&key_queue_mutex);
652}