blob: d953562ea77472d615f6e48dd42053f18414bd7e [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 Dutta5d808172010-12-18 22:29:27 -0800171#define MENU_TEXT_COLOR 255, 160, 49, 255
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800172#define NORMAL_TEXT_COLOR 200, 200, 200, 255
173#define HEADER_TEXT_COLOR NORMAL_TEXT_COLOR
174
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800175// Redraw everything on the screen. Does not flip pages.
176// Should only be called with gUpdateMutex locked.
177static void draw_screen_locked(void)
178{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700179 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800180 draw_background_locked(gCurrentIcon);
181 draw_progress_locked();
182
183 if (show_text) {
184 gr_color(0, 0, 0, 160);
185 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
186
187 int i = 0;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100188 int j = 0;
189 int row = 0; // current row that we are drawing on
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800190 if (show_menu) {
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800191 gr_color(MENU_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100192 gr_fill(0, (menu_top + menu_sel - menu_show_start) * CHAR_HEIGHT,
193 gr_fb_width(), (menu_top + menu_sel - menu_show_start + 1)*CHAR_HEIGHT+1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800194
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800195 gr_color(HEADER_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100196 for (i = 0; i < menu_top; ++i) {
197 draw_text_line(i, menu[i]);
198 row++;
199 }
200
201 if (menu_items - menu_show_start + menu_top >= MAX_ROWS)
202 j = MAX_ROWS - menu_top;
203 else
204 j = menu_items - menu_show_start;
205
206 gr_color(MENU_TEXT_COLOR);
207 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 -0800208 if (i == menu_top + menu_sel) {
209 gr_color(255, 255, 255, 255);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100210 draw_text_line(i - menu_show_start , menu[i]);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800211 gr_color(MENU_TEXT_COLOR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800212 } else {
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100213 gr_color(MENU_TEXT_COLOR);
214 draw_text_line(i - menu_show_start, menu[i]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800215 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100216 row++;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800217 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100218 gr_fill(0, row*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
219 gr_fb_width(), row*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800220 }
221
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800222 gr_color(NORMAL_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100223 for (; row < text_rows; ++row) {
224 draw_text_line(row, text[(row+text_top) % text_rows]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800225 }
226 }
227}
228
229// Redraw everything on the screen and flip the screen (make it visible).
230// Should only be called with gUpdateMutex locked.
231static void update_screen_locked(void)
232{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700233 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800234 draw_screen_locked();
235 gr_flip();
236}
237
238// Updates only the progress bar, if possible, otherwise redraws the screen.
239// Should only be called with gUpdateMutex locked.
240static void update_progress_locked(void)
241{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700242 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800243 if (show_text || !gPagesIdentical) {
244 draw_screen_locked(); // Must redraw the whole screen
245 gPagesIdentical = 1;
246 } else {
247 draw_progress_locked(); // Draw only the progress bar
248 }
249 gr_flip();
250}
251
252// Keeps the progress bar updated, even when the process is otherwise busy.
253static void *progress_thread(void *cookie)
254{
255 for (;;) {
256 usleep(1000000 / PROGRESSBAR_INDETERMINATE_FPS);
257 pthread_mutex_lock(&gUpdateMutex);
258
259 // update the progress bar animation, if active
260 // skip this if we have a text overlay (too expensive to update)
261 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) {
262 update_progress_locked();
263 }
264
265 // move the progress bar forward on timed intervals, if configured
266 int duration = gProgressScopeDuration;
267 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) {
268 int elapsed = time(NULL) - gProgressScopeTime;
269 float progress = 1.0 * elapsed / duration;
270 if (progress > 1.0) progress = 1.0;
271 if (progress > gProgress) {
272 gProgress = progress;
273 update_progress_locked();
274 }
275 }
276
277 pthread_mutex_unlock(&gUpdateMutex);
278 }
279 return NULL;
280}
281
282// Reads input events, handles special hot keys, and adds to the key queue.
283static void *input_thread(void *cookie)
284{
285 int rel_sum = 0;
286 int fake_key = 0;
287 for (;;) {
288 // wait for the next key event
289 struct input_event ev;
290 do {
291 ev_get(&ev, 0);
292
293 if (ev.type == EV_SYN) {
294 continue;
295 } else if (ev.type == EV_REL) {
296 if (ev.code == REL_Y) {
297 // accumulate the up or down motion reported by
298 // the trackball. When it exceeds a threshold
299 // (positive or negative), fake an up/down
300 // key event.
301 rel_sum += ev.value;
302 if (rel_sum > 3) {
303 fake_key = 1;
304 ev.type = EV_KEY;
305 ev.code = KEY_DOWN;
306 ev.value = 1;
307 rel_sum = 0;
308 } else if (rel_sum < -3) {
309 fake_key = 1;
310 ev.type = EV_KEY;
311 ev.code = KEY_UP;
312 ev.value = 1;
313 rel_sum = 0;
314 }
315 }
316 } else {
317 rel_sum = 0;
318 }
319 } while (ev.type != EV_KEY || ev.code > KEY_MAX);
320
321 pthread_mutex_lock(&key_queue_mutex);
322 if (!fake_key) {
323 // our "fake" keys only report a key-down event (no
324 // key-up), so don't record them in the key_pressed
325 // table.
326 key_pressed[ev.code] = ev.value;
327 }
328 fake_key = 0;
329 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
330 if (ev.value > 0 && key_queue_len < queue_max) {
331 key_queue[key_queue_len++] = ev.code;
332 pthread_cond_signal(&key_queue_cond);
333 }
334 pthread_mutex_unlock(&key_queue_mutex);
335
Doug Zongkerddd6a282009-06-09 12:22:33 -0700336 if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800337 pthread_mutex_lock(&gUpdateMutex);
338 show_text = !show_text;
339 update_screen_locked();
340 pthread_mutex_unlock(&gUpdateMutex);
341 }
342
Doug Zongkerddd6a282009-06-09 12:22:33 -0700343 if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800344 reboot(RB_AUTOBOOT);
345 }
346 }
347 return NULL;
348}
349
350void ui_init(void)
351{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700352 ui_has_initialized = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800353 gr_init();
354 ev_init();
355
356 text_col = text_row = 0;
357 text_rows = gr_fb_height() / CHAR_HEIGHT;
358 if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
359 text_top = 1;
360
361 text_cols = gr_fb_width() / CHAR_WIDTH;
362 if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
363
364 int i;
365 for (i = 0; BITMAPS[i].name != NULL; ++i) {
366 int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface);
367 if (result < 0) {
Doug Zongker196c25c2009-09-15 08:50:04 -0700368 if (result == -2) {
369 LOGI("Bitmap %s missing header\n", BITMAPS[i].name);
370 } else {
371 LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result);
372 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800373 *BITMAPS[i].surface = NULL;
374 }
375 }
376
377 pthread_t t;
378 pthread_create(&t, NULL, progress_thread, NULL);
379 pthread_create(&t, NULL, input_thread, NULL);
380}
381
Koushik Dutta107629b2010-06-30 23:17:53 -0700382char *ui_copy_image(int icon, int *width, int *height, int *bpp) {
383 pthread_mutex_lock(&gUpdateMutex);
384 draw_background_locked(gBackgroundIcon[icon]);
385 *width = gr_fb_width();
386 *height = gr_fb_height();
387 *bpp = sizeof(gr_pixel) * 8;
388 int size = *width * *height * sizeof(gr_pixel);
389 char *ret = malloc(size);
390 if (ret == NULL) {
391 LOGE("Can't allocate %d bytes for image\n", size);
392 } else {
393 memcpy(ret, gr_fb_data(), size);
394 }
395 pthread_mutex_unlock(&gUpdateMutex);
396 return ret;
397}
398
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800399void ui_set_background(int icon)
400{
401 pthread_mutex_lock(&gUpdateMutex);
402 gCurrentIcon = gBackgroundIcon[icon];
403 update_screen_locked();
404 pthread_mutex_unlock(&gUpdateMutex);
405}
406
407void ui_show_indeterminate_progress()
408{
409 pthread_mutex_lock(&gUpdateMutex);
410 if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
411 gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
412 update_progress_locked();
413 }
414 pthread_mutex_unlock(&gUpdateMutex);
415}
416
417void ui_show_progress(float portion, int seconds)
418{
419 pthread_mutex_lock(&gUpdateMutex);
420 gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
421 gProgressScopeStart += gProgressScopeSize;
422 gProgressScopeSize = portion;
423 gProgressScopeTime = time(NULL);
424 gProgressScopeDuration = seconds;
425 gProgress = 0;
426 update_progress_locked();
427 pthread_mutex_unlock(&gUpdateMutex);
428}
429
430void ui_set_progress(float fraction)
431{
432 pthread_mutex_lock(&gUpdateMutex);
433 if (fraction < 0.0) fraction = 0.0;
434 if (fraction > 1.0) fraction = 1.0;
435 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
436 // Skip updates that aren't visibly different.
437 int width = gr_get_width(gProgressBarIndeterminate[0]);
438 float scale = width * gProgressScopeSize;
439 if ((int) (gProgress * scale) != (int) (fraction * scale)) {
440 gProgress = fraction;
441 update_progress_locked();
442 }
443 }
444 pthread_mutex_unlock(&gUpdateMutex);
445}
446
447void ui_reset_progress()
448{
449 pthread_mutex_lock(&gUpdateMutex);
450 gProgressBarType = PROGRESSBAR_TYPE_NONE;
451 gProgressScopeStart = gProgressScopeSize = 0;
452 gProgressScopeTime = gProgressScopeDuration = 0;
453 gProgress = 0;
454 update_screen_locked();
455 pthread_mutex_unlock(&gUpdateMutex);
456}
457
458void ui_print(const char *fmt, ...)
459{
460 char buf[256];
461 va_list ap;
462 va_start(ap, fmt);
463 vsnprintf(buf, 256, fmt, ap);
464 va_end(ap);
465
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200466 if (ui_log_stdout)
467 fputs(buf, stdout);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800468
469 // This can get called before ui_init(), so be careful.
470 pthread_mutex_lock(&gUpdateMutex);
471 if (text_rows > 0 && text_cols > 0) {
472 char *ptr;
473 for (ptr = buf; *ptr != '\0'; ++ptr) {
474 if (*ptr == '\n' || text_col >= text_cols) {
475 text[text_row][text_col] = '\0';
476 text_col = 0;
477 text_row = (text_row + 1) % text_rows;
478 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
479 }
480 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
481 }
482 text[text_row][text_col] = '\0';
483 update_screen_locked();
484 }
485 pthread_mutex_unlock(&gUpdateMutex);
486}
487
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200488void ui_printlogtail(int nb_lines) {
489 char * log_data;
490 char tmp[PATH_MAX];
491 FILE * f;
492 int line=0;
493 //don't log output to recovery.log
494 ui_log_stdout=0;
495 sprintf(tmp, "tail -n %d /tmp/recovery.log > /tmp/tail.log", nb_lines);
496 __system(tmp);
497 f = fopen("/tmp/tail.log", "rb");
498 if (f != NULL) {
499 while (line < nb_lines) {
500 log_data = fgets(tmp, PATH_MAX, f);
501 if (log_data == NULL) break;
502 ui_print("%s", tmp);
503 line++;
504 }
505 fclose(f);
506 }
507 ui_log_stdout=1;
508}
509
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800510void ui_reset_text_col()
511{
512 pthread_mutex_lock(&gUpdateMutex);
513 text_col = 0;
514 pthread_mutex_unlock(&gUpdateMutex);
515}
516
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800517#define MENU_ITEM_HEADER " - "
518#define MENU_ITEM_HEADER_LENGTH strlen(MENU_ITEM_HEADER)
519
Koushik Duttadf1e4062010-12-18 17:42:31 -0800520int 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;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100530 for (; i < MENU_MAX_ROWS; ++i) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800531 if (items[i-menu_top] == NULL) break;
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800532 strcpy(menu[i], MENU_ITEM_HEADER);
533 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 -0800534 menu[i][text_cols-1] = '\0';
535 }
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700536
537 if (gShowBackButton) {
538 strcpy(menu[i], " - +++++Go Back+++++");
539 ++i;
540 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100541
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800542 menu_items = i - menu_top;
543 show_menu = 1;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800544 menu_sel = menu_show_start = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800545 update_screen_locked();
546 }
547 pthread_mutex_unlock(&gUpdateMutex);
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700548 if (gShowBackButton) {
549 return menu_items - 1;
550 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700551 return menu_items;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800552}
553
554int ui_menu_select(int sel) {
555 int old_sel;
556 pthread_mutex_lock(&gUpdateMutex);
557 if (show_menu > 0) {
558 old_sel = menu_sel;
559 menu_sel = sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100560
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700561 if (menu_sel < 0) menu_sel = menu_items + menu_sel;
562 if (menu_sel >= menu_items) menu_sel = menu_sel - menu_items;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100563
564
565 if (menu_sel < menu_show_start && menu_show_start > 0) {
Koushik Dutta917c38a2010-05-24 16:43:39 -0700566 menu_show_start = menu_sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100567 }
568
569 if (menu_sel - menu_show_start + menu_top >= text_rows) {
Koushik Dutta917c38a2010-05-24 16:43:39 -0700570 menu_show_start = menu_sel + menu_top - text_rows + 1;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100571 }
572
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800573 sel = menu_sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100574
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800575 if (menu_sel != old_sel) update_screen_locked();
576 }
577 pthread_mutex_unlock(&gUpdateMutex);
578 return sel;
579}
580
581void ui_end_menu() {
582 int i;
583 pthread_mutex_lock(&gUpdateMutex);
584 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
585 show_menu = 0;
586 update_screen_locked();
587 }
588 pthread_mutex_unlock(&gUpdateMutex);
589}
590
591int ui_text_visible()
592{
593 pthread_mutex_lock(&gUpdateMutex);
594 int visible = show_text;
595 pthread_mutex_unlock(&gUpdateMutex);
596 return visible;
597}
598
Doug Zongker4bc98062010-09-03 11:00:13 -0700599void ui_show_text(int visible)
600{
601 pthread_mutex_lock(&gUpdateMutex);
602 show_text = visible;
603 update_screen_locked();
604 pthread_mutex_unlock(&gUpdateMutex);
605}
606
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800607int ui_wait_key()
608{
609 pthread_mutex_lock(&key_queue_mutex);
610 while (key_queue_len == 0) {
611 pthread_cond_wait(&key_queue_cond, &key_queue_mutex);
612 }
613
614 int key = key_queue[0];
615 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
616 pthread_mutex_unlock(&key_queue_mutex);
617 return key;
618}
619
620int ui_key_pressed(int key)
621{
622 // This is a volatile static array, don't bother locking
623 return key_pressed[key];
624}
625
626void ui_clear_key_queue() {
627 pthread_mutex_lock(&key_queue_mutex);
628 key_queue_len = 0;
629 pthread_mutex_unlock(&key_queue_mutex);
630}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800631
Koushik K. Dutta03173782010-02-26 14:14:23 -0800632void ui_set_show_text(int value) {
633 show_text = value;
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800634}
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700635
636void ui_set_showing_back_button(int showBackButton) {
637 gShowBackButton = showBackButton;
638}
639
640int ui_get_showing_back_button() {
641 return gShowBackButton;
642}