blob: 2b9fb07f8b543a59e4d91081630e8f1aaa1361f4 [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
Tanguy Pruvot67d358c2011-06-10 20:26:44 +020032extern int __system(const char *command);
33
Koushik Duttad90ad5d2010-11-27 14:41:07 -080034#ifdef BOARD_HAS_NO_SELECT_BUTTON
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -070035static int gShowBackButton = 1;
36#else
37static int gShowBackButton = 0;
38#endif
39
Doug Zongker8a8e6cc2010-07-07 13:55:25 -070040#define MAX_COLS 96
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041#define MAX_ROWS 32
42
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +010043#define MENU_MAX_COLS 64
44#define MENU_MAX_ROWS 250
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045
Ricardo Cerqueira9acb0222010-11-08 23:54:30 +000046#ifndef BOARD_LDPI_RECOVERY
47 #define CHAR_WIDTH 10
48 #define CHAR_HEIGHT 18
49#else
50 #define CHAR_WIDTH 7
51 #define CHAR_HEIGHT 16
52#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080053
54#define PROGRESSBAR_INDETERMINATE_STATES 6
55#define PROGRESSBAR_INDETERMINATE_FPS 15
56
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080057static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER;
58static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS];
59static gr_surface gProgressBarIndeterminate[PROGRESSBAR_INDETERMINATE_STATES];
Doug Zongkerd93a2542009-10-08 16:32:58 -070060static gr_surface gProgressBarEmpty;
61static gr_surface gProgressBarFill;
Koushik Duttad3cc60b2010-07-03 13:56:45 -070062static int ui_has_initialized = 0;
Tanguy Pruvot67d358c2011-06-10 20:26:44 +020063static int ui_log_stdout = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080064
65static const struct { gr_surface* surface; const char *name; } BITMAPS[] = {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080066 { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" },
67 { &gBackgroundIcon[BACKGROUND_ICON_ERROR], "icon_error" },
Koushik Dutta5d808172010-12-18 22:29:27 -080068 { &gBackgroundIcon[BACKGROUND_ICON_CLOCKWORK], "icon_clockwork" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080069 { &gProgressBarIndeterminate[0], "indeterminate1" },
70 { &gProgressBarIndeterminate[1], "indeterminate2" },
71 { &gProgressBarIndeterminate[2], "indeterminate3" },
72 { &gProgressBarIndeterminate[3], "indeterminate4" },
73 { &gProgressBarIndeterminate[4], "indeterminate5" },
74 { &gProgressBarIndeterminate[5], "indeterminate6" },
Doug Zongkerd93a2542009-10-08 16:32:58 -070075 { &gProgressBarEmpty, "progress_empty" },
76 { &gProgressBarFill, "progress_fill" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077 { NULL, NULL },
78};
79
80static gr_surface gCurrentIcon = NULL;
81
82static enum ProgressBarType {
83 PROGRESSBAR_TYPE_NONE,
84 PROGRESSBAR_TYPE_INDETERMINATE,
85 PROGRESSBAR_TYPE_NORMAL,
86} gProgressBarType = PROGRESSBAR_TYPE_NONE;
87
88// Progress bar scope of current operation
89static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
90static time_t gProgressScopeTime, gProgressScopeDuration;
91
92// Set to 1 when both graphics pages are the same (except for the progress bar)
93static int gPagesIdentical = 0;
94
95// Log text overlay, displayed when a magic key is pressed
96static char text[MAX_ROWS][MAX_COLS];
97static int text_cols = 0, text_rows = 0;
98static int text_col = 0, text_row = 0, text_top = 0;
Ying Wang532c8602010-09-01 14:52:22 -070099static int show_text = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800100
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100101static char menu[MENU_MAX_ROWS][MENU_MAX_COLS];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800102static int show_menu = 0;
103static int menu_top = 0, menu_items = 0, menu_sel = 0;
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200104static int menu_show_start = 0; // this is line which menu display is starting at
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800105
106// Key event input queue
107static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
108static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER;
109static int key_queue[256], key_queue_len = 0;
110static volatile char key_pressed[KEY_MAX + 1];
111
112// Clear the screen and draw the currently selected background icon (if any).
113// Should only be called with gUpdateMutex locked.
114static void draw_background_locked(gr_surface icon)
115{
116 gPagesIdentical = 0;
117 gr_color(0, 0, 0, 255);
118 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
119
120 if (icon) {
121 int iconWidth = gr_get_width(icon);
122 int iconHeight = gr_get_height(icon);
123 int iconX = (gr_fb_width() - iconWidth) / 2;
124 int iconY = (gr_fb_height() - iconHeight) / 2;
125 gr_blit(icon, 0, 0, iconWidth, iconHeight, iconX, iconY);
126 }
127}
128
129// Draw the progress bar (if any) on the screen. Does not flip pages.
130// Should only be called with gUpdateMutex locked.
131static void draw_progress_locked()
132{
133 if (gProgressBarType == PROGRESSBAR_TYPE_NONE) return;
134
135 int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]);
Doug Zongkerd93a2542009-10-08 16:32:58 -0700136 int width = gr_get_width(gProgressBarEmpty);
137 int height = gr_get_height(gProgressBarEmpty);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800138
139 int dx = (gr_fb_width() - width)/2;
140 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
141
142 // Erase behind the progress bar (in case this was a progress-only update)
143 gr_color(0, 0, 0, 255);
144 gr_fill(dx, dy, width, height);
145
146 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) {
147 float progress = gProgressScopeStart + gProgress * gProgressScopeSize;
148 int pos = (int) (progress * width);
149
Doug Zongkerd93a2542009-10-08 16:32:58 -0700150 if (pos > 0) {
151 gr_blit(gProgressBarFill, 0, 0, pos, height, dx, dy);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800152 }
Doug Zongkerd93a2542009-10-08 16:32:58 -0700153 if (pos < width-1) {
154 gr_blit(gProgressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
155 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800156 }
157
158 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) {
159 static int frame = 0;
160 gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
161 frame = (frame + 1) % PROGRESSBAR_INDETERMINATE_STATES;
162 }
163}
164
165static void draw_text_line(int row, const char* t) {
166 if (t[0] != '\0') {
167 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
168 }
169}
170
Koushik Dutta30a937a2011-09-05 21:14:06 -0700171//#define MENU_TEXT_COLOR 255, 160, 49, 255
172#define MENU_TEXT_COLOR 0, 191, 255, 255
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800173#define NORMAL_TEXT_COLOR 200, 200, 200, 255
174#define HEADER_TEXT_COLOR NORMAL_TEXT_COLOR
175
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800176// Redraw everything on the screen. Does not flip pages.
177// Should only be called with gUpdateMutex locked.
178static void draw_screen_locked(void)
179{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700180 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800181 draw_background_locked(gCurrentIcon);
182 draw_progress_locked();
183
184 if (show_text) {
185 gr_color(0, 0, 0, 160);
186 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
187
188 int i = 0;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100189 int j = 0;
190 int row = 0; // current row that we are drawing on
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800191 if (show_menu) {
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800192 gr_color(MENU_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100193 gr_fill(0, (menu_top + menu_sel - menu_show_start) * CHAR_HEIGHT,
194 gr_fb_width(), (menu_top + menu_sel - menu_show_start + 1)*CHAR_HEIGHT+1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800195
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800196 gr_color(HEADER_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100197 for (i = 0; i < menu_top; ++i) {
198 draw_text_line(i, menu[i]);
199 row++;
200 }
201
202 if (menu_items - menu_show_start + menu_top >= MAX_ROWS)
203 j = MAX_ROWS - menu_top;
204 else
205 j = menu_items - menu_show_start;
206
207 gr_color(MENU_TEXT_COLOR);
208 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 -0800209 if (i == menu_top + menu_sel) {
210 gr_color(255, 255, 255, 255);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100211 draw_text_line(i - menu_show_start , menu[i]);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800212 gr_color(MENU_TEXT_COLOR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800213 } else {
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100214 gr_color(MENU_TEXT_COLOR);
215 draw_text_line(i - menu_show_start, menu[i]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800216 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100217 row++;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800218 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100219 gr_fill(0, row*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
220 gr_fb_width(), row*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800221 }
222
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800223 gr_color(NORMAL_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100224 for (; row < text_rows; ++row) {
225 draw_text_line(row, text[(row+text_top) % text_rows]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800226 }
227 }
228}
229
230// Redraw everything on the screen and flip the screen (make it visible).
231// Should only be called with gUpdateMutex locked.
232static void update_screen_locked(void)
233{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700234 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800235 draw_screen_locked();
236 gr_flip();
237}
238
239// Updates only the progress bar, if possible, otherwise redraws the screen.
240// Should only be called with gUpdateMutex locked.
241static void update_progress_locked(void)
242{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700243 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800244 if (show_text || !gPagesIdentical) {
245 draw_screen_locked(); // Must redraw the whole screen
246 gPagesIdentical = 1;
247 } else {
248 draw_progress_locked(); // Draw only the progress bar
249 }
250 gr_flip();
251}
252
253// Keeps the progress bar updated, even when the process is otherwise busy.
254static void *progress_thread(void *cookie)
255{
256 for (;;) {
257 usleep(1000000 / PROGRESSBAR_INDETERMINATE_FPS);
258 pthread_mutex_lock(&gUpdateMutex);
259
260 // update the progress bar animation, if active
261 // skip this if we have a text overlay (too expensive to update)
262 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) {
263 update_progress_locked();
264 }
265
266 // move the progress bar forward on timed intervals, if configured
267 int duration = gProgressScopeDuration;
268 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) {
269 int elapsed = time(NULL) - gProgressScopeTime;
270 float progress = 1.0 * elapsed / duration;
271 if (progress > 1.0) progress = 1.0;
272 if (progress > gProgress) {
273 gProgress = progress;
274 update_progress_locked();
275 }
276 }
277
278 pthread_mutex_unlock(&gUpdateMutex);
279 }
280 return NULL;
281}
282
283// Reads input events, handles special hot keys, and adds to the key queue.
284static void *input_thread(void *cookie)
285{
286 int rel_sum = 0;
287 int fake_key = 0;
288 for (;;) {
289 // wait for the next key event
290 struct input_event ev;
291 do {
292 ev_get(&ev, 0);
293
294 if (ev.type == EV_SYN) {
295 continue;
296 } else if (ev.type == EV_REL) {
297 if (ev.code == REL_Y) {
298 // accumulate the up or down motion reported by
299 // the trackball. When it exceeds a threshold
300 // (positive or negative), fake an up/down
301 // key event.
302 rel_sum += ev.value;
303 if (rel_sum > 3) {
304 fake_key = 1;
305 ev.type = EV_KEY;
306 ev.code = KEY_DOWN;
307 ev.value = 1;
308 rel_sum = 0;
309 } else if (rel_sum < -3) {
310 fake_key = 1;
311 ev.type = EV_KEY;
312 ev.code = KEY_UP;
313 ev.value = 1;
314 rel_sum = 0;
315 }
316 }
317 } else {
318 rel_sum = 0;
319 }
320 } while (ev.type != EV_KEY || ev.code > KEY_MAX);
321
322 pthread_mutex_lock(&key_queue_mutex);
323 if (!fake_key) {
324 // our "fake" keys only report a key-down event (no
325 // key-up), so don't record them in the key_pressed
326 // table.
327 key_pressed[ev.code] = ev.value;
328 }
329 fake_key = 0;
330 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
331 if (ev.value > 0 && key_queue_len < queue_max) {
332 key_queue[key_queue_len++] = ev.code;
333 pthread_cond_signal(&key_queue_cond);
334 }
335 pthread_mutex_unlock(&key_queue_mutex);
336
Doug Zongkerddd6a282009-06-09 12:22:33 -0700337 if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800338 pthread_mutex_lock(&gUpdateMutex);
339 show_text = !show_text;
340 update_screen_locked();
341 pthread_mutex_unlock(&gUpdateMutex);
342 }
343
Doug Zongkerddd6a282009-06-09 12:22:33 -0700344 if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800345 reboot(RB_AUTOBOOT);
346 }
347 }
348 return NULL;
349}
350
351void ui_init(void)
352{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700353 ui_has_initialized = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800354 gr_init();
355 ev_init();
356
357 text_col = text_row = 0;
358 text_rows = gr_fb_height() / CHAR_HEIGHT;
359 if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
360 text_top = 1;
361
362 text_cols = gr_fb_width() / CHAR_WIDTH;
363 if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
364
365 int i;
366 for (i = 0; BITMAPS[i].name != NULL; ++i) {
367 int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface);
368 if (result < 0) {
Doug Zongker196c25c2009-09-15 08:50:04 -0700369 if (result == -2) {
370 LOGI("Bitmap %s missing header\n", BITMAPS[i].name);
371 } else {
372 LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result);
373 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800374 *BITMAPS[i].surface = NULL;
375 }
376 }
377
378 pthread_t t;
379 pthread_create(&t, NULL, progress_thread, NULL);
380 pthread_create(&t, NULL, input_thread, NULL);
381}
382
Koushik Dutta107629b2010-06-30 23:17:53 -0700383char *ui_copy_image(int icon, int *width, int *height, int *bpp) {
384 pthread_mutex_lock(&gUpdateMutex);
385 draw_background_locked(gBackgroundIcon[icon]);
386 *width = gr_fb_width();
387 *height = gr_fb_height();
388 *bpp = sizeof(gr_pixel) * 8;
389 int size = *width * *height * sizeof(gr_pixel);
390 char *ret = malloc(size);
391 if (ret == NULL) {
392 LOGE("Can't allocate %d bytes for image\n", size);
393 } else {
394 memcpy(ret, gr_fb_data(), size);
395 }
396 pthread_mutex_unlock(&gUpdateMutex);
397 return ret;
398}
399
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800400void ui_set_background(int icon)
401{
402 pthread_mutex_lock(&gUpdateMutex);
403 gCurrentIcon = gBackgroundIcon[icon];
404 update_screen_locked();
405 pthread_mutex_unlock(&gUpdateMutex);
406}
407
408void ui_show_indeterminate_progress()
409{
410 pthread_mutex_lock(&gUpdateMutex);
411 if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
412 gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
413 update_progress_locked();
414 }
415 pthread_mutex_unlock(&gUpdateMutex);
416}
417
418void ui_show_progress(float portion, int seconds)
419{
420 pthread_mutex_lock(&gUpdateMutex);
421 gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
422 gProgressScopeStart += gProgressScopeSize;
423 gProgressScopeSize = portion;
424 gProgressScopeTime = time(NULL);
425 gProgressScopeDuration = seconds;
426 gProgress = 0;
427 update_progress_locked();
428 pthread_mutex_unlock(&gUpdateMutex);
429}
430
431void ui_set_progress(float fraction)
432{
433 pthread_mutex_lock(&gUpdateMutex);
434 if (fraction < 0.0) fraction = 0.0;
435 if (fraction > 1.0) fraction = 1.0;
436 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
437 // Skip updates that aren't visibly different.
438 int width = gr_get_width(gProgressBarIndeterminate[0]);
439 float scale = width * gProgressScopeSize;
440 if ((int) (gProgress * scale) != (int) (fraction * scale)) {
441 gProgress = fraction;
442 update_progress_locked();
443 }
444 }
445 pthread_mutex_unlock(&gUpdateMutex);
446}
447
448void ui_reset_progress()
449{
450 pthread_mutex_lock(&gUpdateMutex);
451 gProgressBarType = PROGRESSBAR_TYPE_NONE;
452 gProgressScopeStart = gProgressScopeSize = 0;
453 gProgressScopeTime = gProgressScopeDuration = 0;
454 gProgress = 0;
455 update_screen_locked();
456 pthread_mutex_unlock(&gUpdateMutex);
457}
458
459void ui_print(const char *fmt, ...)
460{
461 char buf[256];
462 va_list ap;
463 va_start(ap, fmt);
464 vsnprintf(buf, 256, fmt, ap);
465 va_end(ap);
466
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200467 if (ui_log_stdout)
468 fputs(buf, stdout);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800469
470 // This can get called before ui_init(), so be careful.
471 pthread_mutex_lock(&gUpdateMutex);
472 if (text_rows > 0 && text_cols > 0) {
473 char *ptr;
474 for (ptr = buf; *ptr != '\0'; ++ptr) {
475 if (*ptr == '\n' || text_col >= text_cols) {
476 text[text_row][text_col] = '\0';
477 text_col = 0;
478 text_row = (text_row + 1) % text_rows;
479 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
480 }
481 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
482 }
483 text[text_row][text_col] = '\0';
484 update_screen_locked();
485 }
486 pthread_mutex_unlock(&gUpdateMutex);
487}
488
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200489void ui_printlogtail(int nb_lines) {
490 char * log_data;
491 char tmp[PATH_MAX];
492 FILE * f;
493 int line=0;
494 //don't log output to recovery.log
495 ui_log_stdout=0;
496 sprintf(tmp, "tail -n %d /tmp/recovery.log > /tmp/tail.log", nb_lines);
497 __system(tmp);
498 f = fopen("/tmp/tail.log", "rb");
499 if (f != NULL) {
500 while (line < nb_lines) {
501 log_data = fgets(tmp, PATH_MAX, f);
502 if (log_data == NULL) break;
503 ui_print("%s", tmp);
504 line++;
505 }
506 fclose(f);
507 }
508 ui_log_stdout=1;
509}
510
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800511void ui_reset_text_col()
512{
513 pthread_mutex_lock(&gUpdateMutex);
514 text_col = 0;
515 pthread_mutex_unlock(&gUpdateMutex);
516}
517
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800518#define MENU_ITEM_HEADER " - "
519#define MENU_ITEM_HEADER_LENGTH strlen(MENU_ITEM_HEADER)
520
Koushik Duttadf1e4062010-12-18 17:42:31 -0800521int ui_start_menu(char** headers, char** items, int initial_selection) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800522 int i;
523 pthread_mutex_lock(&gUpdateMutex);
524 if (text_rows > 0 && text_cols > 0) {
525 for (i = 0; i < text_rows; ++i) {
526 if (headers[i] == NULL) break;
527 strncpy(menu[i], headers[i], text_cols-1);
528 menu[i][text_cols-1] = '\0';
529 }
530 menu_top = i;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100531 for (; i < MENU_MAX_ROWS; ++i) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800532 if (items[i-menu_top] == NULL) break;
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800533 strcpy(menu[i], MENU_ITEM_HEADER);
534 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 -0800535 menu[i][text_cols-1] = '\0';
536 }
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700537
538 if (gShowBackButton) {
539 strcpy(menu[i], " - +++++Go Back+++++");
540 ++i;
541 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100542
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800543 menu_items = i - menu_top;
544 show_menu = 1;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800545 menu_sel = menu_show_start = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800546 update_screen_locked();
547 }
548 pthread_mutex_unlock(&gUpdateMutex);
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700549 if (gShowBackButton) {
550 return menu_items - 1;
551 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700552 return menu_items;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800553}
554
555int ui_menu_select(int sel) {
556 int old_sel;
557 pthread_mutex_lock(&gUpdateMutex);
558 if (show_menu > 0) {
559 old_sel = menu_sel;
560 menu_sel = sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100561
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700562 if (menu_sel < 0) menu_sel = menu_items + menu_sel;
563 if (menu_sel >= menu_items) menu_sel = menu_sel - menu_items;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100564
565
566 if (menu_sel < menu_show_start && menu_show_start > 0) {
Koushik Dutta917c38a2010-05-24 16:43:39 -0700567 menu_show_start = menu_sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100568 }
569
570 if (menu_sel - menu_show_start + menu_top >= text_rows) {
Koushik Dutta917c38a2010-05-24 16:43:39 -0700571 menu_show_start = menu_sel + menu_top - text_rows + 1;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100572 }
573
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800574 sel = menu_sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100575
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800576 if (menu_sel != old_sel) update_screen_locked();
577 }
578 pthread_mutex_unlock(&gUpdateMutex);
579 return sel;
580}
581
582void ui_end_menu() {
583 int i;
584 pthread_mutex_lock(&gUpdateMutex);
585 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
586 show_menu = 0;
587 update_screen_locked();
588 }
589 pthread_mutex_unlock(&gUpdateMutex);
590}
591
592int ui_text_visible()
593{
594 pthread_mutex_lock(&gUpdateMutex);
595 int visible = show_text;
596 pthread_mutex_unlock(&gUpdateMutex);
597 return visible;
598}
599
Doug Zongker4bc98062010-09-03 11:00:13 -0700600void ui_show_text(int visible)
601{
602 pthread_mutex_lock(&gUpdateMutex);
603 show_text = visible;
604 update_screen_locked();
605 pthread_mutex_unlock(&gUpdateMutex);
606}
607
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800608int ui_wait_key()
609{
610 pthread_mutex_lock(&key_queue_mutex);
611 while (key_queue_len == 0) {
612 pthread_cond_wait(&key_queue_cond, &key_queue_mutex);
613 }
614
615 int key = key_queue[0];
616 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
617 pthread_mutex_unlock(&key_queue_mutex);
618 return key;
619}
620
621int ui_key_pressed(int key)
622{
623 // This is a volatile static array, don't bother locking
624 return key_pressed[key];
625}
626
627void ui_clear_key_queue() {
628 pthread_mutex_lock(&key_queue_mutex);
629 key_queue_len = 0;
630 pthread_mutex_unlock(&key_queue_mutex);
631}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800632
Koushik K. Dutta03173782010-02-26 14:14:23 -0800633void ui_set_show_text(int value) {
634 show_text = value;
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800635}
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700636
637void ui_set_showing_back_button(int showBackButton) {
638 gShowBackButton = showBackButton;
639}
640
641int ui_get_showing_back_button() {
642 return gShowBackButton;
643}