blob: 6c747c109a6bae36b63bedcbb5ceaa3c1f89e748 [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" },
Koushik Dutta7f13e152011-09-08 16:55:35 -070069 { &gBackgroundIcon[BACKGROUND_ICON_FIRMWARE_INSTALLING], "icon_firmware_install" },
70 { &gBackgroundIcon[BACKGROUND_ICON_FIRMWARE_ERROR], "icon_firmware_error" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080071 { &gProgressBarIndeterminate[0], "indeterminate1" },
72 { &gProgressBarIndeterminate[1], "indeterminate2" },
73 { &gProgressBarIndeterminate[2], "indeterminate3" },
74 { &gProgressBarIndeterminate[3], "indeterminate4" },
75 { &gProgressBarIndeterminate[4], "indeterminate5" },
76 { &gProgressBarIndeterminate[5], "indeterminate6" },
Doug Zongkerd93a2542009-10-08 16:32:58 -070077 { &gProgressBarEmpty, "progress_empty" },
78 { &gProgressBarFill, "progress_fill" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080079 { NULL, NULL },
80};
81
82static gr_surface gCurrentIcon = NULL;
83
84static enum ProgressBarType {
85 PROGRESSBAR_TYPE_NONE,
86 PROGRESSBAR_TYPE_INDETERMINATE,
87 PROGRESSBAR_TYPE_NORMAL,
88} gProgressBarType = PROGRESSBAR_TYPE_NONE;
89
90// Progress bar scope of current operation
91static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
92static time_t gProgressScopeTime, gProgressScopeDuration;
93
94// Set to 1 when both graphics pages are the same (except for the progress bar)
95static int gPagesIdentical = 0;
96
97// Log text overlay, displayed when a magic key is pressed
98static char text[MAX_ROWS][MAX_COLS];
99static int text_cols = 0, text_rows = 0;
100static int text_col = 0, text_row = 0, text_top = 0;
Ying Wang532c8602010-09-01 14:52:22 -0700101static int show_text = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800102
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100103static char menu[MENU_MAX_ROWS][MENU_MAX_COLS];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800104static int show_menu = 0;
105static int menu_top = 0, menu_items = 0, menu_sel = 0;
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200106static int menu_show_start = 0; // this is line which menu display is starting at
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800107
108// Key event input queue
109static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
110static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER;
111static int key_queue[256], key_queue_len = 0;
112static volatile char key_pressed[KEY_MAX + 1];
113
114// Clear the screen and draw the currently selected background icon (if any).
115// Should only be called with gUpdateMutex locked.
116static void draw_background_locked(gr_surface icon)
117{
118 gPagesIdentical = 0;
119 gr_color(0, 0, 0, 255);
120 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
121
122 if (icon) {
123 int iconWidth = gr_get_width(icon);
124 int iconHeight = gr_get_height(icon);
125 int iconX = (gr_fb_width() - iconWidth) / 2;
126 int iconY = (gr_fb_height() - iconHeight) / 2;
127 gr_blit(icon, 0, 0, iconWidth, iconHeight, iconX, iconY);
128 }
129}
130
131// Draw the progress bar (if any) on the screen. Does not flip pages.
132// Should only be called with gUpdateMutex locked.
133static void draw_progress_locked()
134{
135 if (gProgressBarType == PROGRESSBAR_TYPE_NONE) return;
136
137 int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]);
Doug Zongkerd93a2542009-10-08 16:32:58 -0700138 int width = gr_get_width(gProgressBarEmpty);
139 int height = gr_get_height(gProgressBarEmpty);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800140
141 int dx = (gr_fb_width() - width)/2;
142 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
143
144 // Erase behind the progress bar (in case this was a progress-only update)
145 gr_color(0, 0, 0, 255);
146 gr_fill(dx, dy, width, height);
147
148 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) {
149 float progress = gProgressScopeStart + gProgress * gProgressScopeSize;
150 int pos = (int) (progress * width);
151
Doug Zongkerd93a2542009-10-08 16:32:58 -0700152 if (pos > 0) {
153 gr_blit(gProgressBarFill, 0, 0, pos, height, dx, dy);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800154 }
Doug Zongkerd93a2542009-10-08 16:32:58 -0700155 if (pos < width-1) {
156 gr_blit(gProgressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
157 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800158 }
159
160 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) {
161 static int frame = 0;
162 gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
163 frame = (frame + 1) % PROGRESSBAR_INDETERMINATE_STATES;
164 }
165}
166
167static void draw_text_line(int row, const char* t) {
168 if (t[0] != '\0') {
169 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
170 }
171}
172
Koushik Dutta30a937a2011-09-05 21:14:06 -0700173//#define MENU_TEXT_COLOR 255, 160, 49, 255
174#define MENU_TEXT_COLOR 0, 191, 255, 255
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800175#define NORMAL_TEXT_COLOR 200, 200, 200, 255
176#define HEADER_TEXT_COLOR NORMAL_TEXT_COLOR
177
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800178// Redraw everything on the screen. Does not flip pages.
179// Should only be called with gUpdateMutex locked.
180static void draw_screen_locked(void)
181{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700182 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800183 draw_background_locked(gCurrentIcon);
184 draw_progress_locked();
185
186 if (show_text) {
187 gr_color(0, 0, 0, 160);
188 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
189
190 int i = 0;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100191 int j = 0;
192 int row = 0; // current row that we are drawing on
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800193 if (show_menu) {
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800194 gr_color(MENU_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100195 gr_fill(0, (menu_top + menu_sel - menu_show_start) * CHAR_HEIGHT,
196 gr_fb_width(), (menu_top + menu_sel - menu_show_start + 1)*CHAR_HEIGHT+1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800197
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800198 gr_color(HEADER_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100199 for (i = 0; i < menu_top; ++i) {
200 draw_text_line(i, menu[i]);
201 row++;
202 }
203
204 if (menu_items - menu_show_start + menu_top >= MAX_ROWS)
205 j = MAX_ROWS - menu_top;
206 else
207 j = menu_items - menu_show_start;
208
209 gr_color(MENU_TEXT_COLOR);
210 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 -0800211 if (i == menu_top + menu_sel) {
212 gr_color(255, 255, 255, 255);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100213 draw_text_line(i - menu_show_start , menu[i]);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800214 gr_color(MENU_TEXT_COLOR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800215 } else {
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100216 gr_color(MENU_TEXT_COLOR);
217 draw_text_line(i - menu_show_start, menu[i]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800218 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100219 row++;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800220 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100221 gr_fill(0, row*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
222 gr_fb_width(), row*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800223 }
224
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800225 gr_color(NORMAL_TEXT_COLOR);
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100226 for (; row < text_rows; ++row) {
227 draw_text_line(row, text[(row+text_top) % text_rows]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800228 }
229 }
230}
231
232// Redraw everything on the screen and flip the screen (make it visible).
233// Should only be called with gUpdateMutex locked.
234static void update_screen_locked(void)
235{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700236 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800237 draw_screen_locked();
238 gr_flip();
239}
240
241// Updates only the progress bar, if possible, otherwise redraws the screen.
242// Should only be called with gUpdateMutex locked.
243static void update_progress_locked(void)
244{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700245 if (!ui_has_initialized) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800246 if (show_text || !gPagesIdentical) {
247 draw_screen_locked(); // Must redraw the whole screen
248 gPagesIdentical = 1;
249 } else {
250 draw_progress_locked(); // Draw only the progress bar
251 }
252 gr_flip();
253}
254
255// Keeps the progress bar updated, even when the process is otherwise busy.
256static void *progress_thread(void *cookie)
257{
258 for (;;) {
259 usleep(1000000 / PROGRESSBAR_INDETERMINATE_FPS);
260 pthread_mutex_lock(&gUpdateMutex);
261
262 // update the progress bar animation, if active
263 // skip this if we have a text overlay (too expensive to update)
264 if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) {
265 update_progress_locked();
266 }
267
268 // move the progress bar forward on timed intervals, if configured
269 int duration = gProgressScopeDuration;
270 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) {
271 int elapsed = time(NULL) - gProgressScopeTime;
272 float progress = 1.0 * elapsed / duration;
273 if (progress > 1.0) progress = 1.0;
274 if (progress > gProgress) {
275 gProgress = progress;
276 update_progress_locked();
277 }
278 }
279
280 pthread_mutex_unlock(&gUpdateMutex);
281 }
282 return NULL;
283}
284
285// Reads input events, handles special hot keys, and adds to the key queue.
286static void *input_thread(void *cookie)
287{
288 int rel_sum = 0;
289 int fake_key = 0;
290 for (;;) {
291 // wait for the next key event
292 struct input_event ev;
293 do {
294 ev_get(&ev, 0);
295
296 if (ev.type == EV_SYN) {
297 continue;
298 } else if (ev.type == EV_REL) {
299 if (ev.code == REL_Y) {
300 // accumulate the up or down motion reported by
301 // the trackball. When it exceeds a threshold
302 // (positive or negative), fake an up/down
303 // key event.
304 rel_sum += ev.value;
305 if (rel_sum > 3) {
306 fake_key = 1;
307 ev.type = EV_KEY;
308 ev.code = KEY_DOWN;
309 ev.value = 1;
310 rel_sum = 0;
311 } else if (rel_sum < -3) {
312 fake_key = 1;
313 ev.type = EV_KEY;
314 ev.code = KEY_UP;
315 ev.value = 1;
316 rel_sum = 0;
317 }
318 }
319 } else {
320 rel_sum = 0;
321 }
322 } while (ev.type != EV_KEY || ev.code > KEY_MAX);
323
324 pthread_mutex_lock(&key_queue_mutex);
325 if (!fake_key) {
326 // our "fake" keys only report a key-down event (no
327 // key-up), so don't record them in the key_pressed
328 // table.
329 key_pressed[ev.code] = ev.value;
330 }
331 fake_key = 0;
332 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
333 if (ev.value > 0 && key_queue_len < queue_max) {
334 key_queue[key_queue_len++] = ev.code;
335 pthread_cond_signal(&key_queue_cond);
336 }
337 pthread_mutex_unlock(&key_queue_mutex);
338
Doug Zongkerddd6a282009-06-09 12:22:33 -0700339 if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800340 pthread_mutex_lock(&gUpdateMutex);
341 show_text = !show_text;
342 update_screen_locked();
343 pthread_mutex_unlock(&gUpdateMutex);
344 }
345
Doug Zongkerddd6a282009-06-09 12:22:33 -0700346 if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800347 reboot(RB_AUTOBOOT);
348 }
349 }
350 return NULL;
351}
352
353void ui_init(void)
354{
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700355 ui_has_initialized = 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800356 gr_init();
357 ev_init();
358
359 text_col = text_row = 0;
360 text_rows = gr_fb_height() / CHAR_HEIGHT;
361 if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
362 text_top = 1;
363
364 text_cols = gr_fb_width() / CHAR_WIDTH;
365 if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
366
367 int i;
368 for (i = 0; BITMAPS[i].name != NULL; ++i) {
369 int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface);
370 if (result < 0) {
Doug Zongker196c25c2009-09-15 08:50:04 -0700371 if (result == -2) {
372 LOGI("Bitmap %s missing header\n", BITMAPS[i].name);
373 } else {
374 LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result);
375 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800376 *BITMAPS[i].surface = NULL;
377 }
378 }
379
380 pthread_t t;
381 pthread_create(&t, NULL, progress_thread, NULL);
382 pthread_create(&t, NULL, input_thread, NULL);
383}
384
Koushik Dutta107629b2010-06-30 23:17:53 -0700385char *ui_copy_image(int icon, int *width, int *height, int *bpp) {
386 pthread_mutex_lock(&gUpdateMutex);
387 draw_background_locked(gBackgroundIcon[icon]);
388 *width = gr_fb_width();
389 *height = gr_fb_height();
390 *bpp = sizeof(gr_pixel) * 8;
391 int size = *width * *height * sizeof(gr_pixel);
392 char *ret = malloc(size);
393 if (ret == NULL) {
394 LOGE("Can't allocate %d bytes for image\n", size);
395 } else {
396 memcpy(ret, gr_fb_data(), size);
397 }
398 pthread_mutex_unlock(&gUpdateMutex);
399 return ret;
400}
401
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800402void ui_set_background(int icon)
403{
404 pthread_mutex_lock(&gUpdateMutex);
405 gCurrentIcon = gBackgroundIcon[icon];
406 update_screen_locked();
407 pthread_mutex_unlock(&gUpdateMutex);
408}
409
410void ui_show_indeterminate_progress()
411{
412 pthread_mutex_lock(&gUpdateMutex);
413 if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
414 gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
415 update_progress_locked();
416 }
417 pthread_mutex_unlock(&gUpdateMutex);
418}
419
420void ui_show_progress(float portion, int seconds)
421{
422 pthread_mutex_lock(&gUpdateMutex);
423 gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
424 gProgressScopeStart += gProgressScopeSize;
425 gProgressScopeSize = portion;
426 gProgressScopeTime = time(NULL);
427 gProgressScopeDuration = seconds;
428 gProgress = 0;
429 update_progress_locked();
430 pthread_mutex_unlock(&gUpdateMutex);
431}
432
433void ui_set_progress(float fraction)
434{
435 pthread_mutex_lock(&gUpdateMutex);
436 if (fraction < 0.0) fraction = 0.0;
437 if (fraction > 1.0) fraction = 1.0;
438 if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
439 // Skip updates that aren't visibly different.
440 int width = gr_get_width(gProgressBarIndeterminate[0]);
441 float scale = width * gProgressScopeSize;
442 if ((int) (gProgress * scale) != (int) (fraction * scale)) {
443 gProgress = fraction;
444 update_progress_locked();
445 }
446 }
447 pthread_mutex_unlock(&gUpdateMutex);
448}
449
450void ui_reset_progress()
451{
452 pthread_mutex_lock(&gUpdateMutex);
453 gProgressBarType = PROGRESSBAR_TYPE_NONE;
454 gProgressScopeStart = gProgressScopeSize = 0;
455 gProgressScopeTime = gProgressScopeDuration = 0;
456 gProgress = 0;
457 update_screen_locked();
458 pthread_mutex_unlock(&gUpdateMutex);
459}
460
461void ui_print(const char *fmt, ...)
462{
463 char buf[256];
464 va_list ap;
465 va_start(ap, fmt);
466 vsnprintf(buf, 256, fmt, ap);
467 va_end(ap);
468
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200469 if (ui_log_stdout)
470 fputs(buf, stdout);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800471
472 // This can get called before ui_init(), so be careful.
473 pthread_mutex_lock(&gUpdateMutex);
474 if (text_rows > 0 && text_cols > 0) {
475 char *ptr;
476 for (ptr = buf; *ptr != '\0'; ++ptr) {
477 if (*ptr == '\n' || text_col >= text_cols) {
478 text[text_row][text_col] = '\0';
479 text_col = 0;
480 text_row = (text_row + 1) % text_rows;
481 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
482 }
483 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
484 }
485 text[text_row][text_col] = '\0';
486 update_screen_locked();
487 }
488 pthread_mutex_unlock(&gUpdateMutex);
489}
490
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200491void ui_printlogtail(int nb_lines) {
492 char * log_data;
493 char tmp[PATH_MAX];
494 FILE * f;
495 int line=0;
496 //don't log output to recovery.log
497 ui_log_stdout=0;
498 sprintf(tmp, "tail -n %d /tmp/recovery.log > /tmp/tail.log", nb_lines);
499 __system(tmp);
500 f = fopen("/tmp/tail.log", "rb");
501 if (f != NULL) {
502 while (line < nb_lines) {
503 log_data = fgets(tmp, PATH_MAX, f);
504 if (log_data == NULL) break;
505 ui_print("%s", tmp);
506 line++;
507 }
508 fclose(f);
509 }
510 ui_log_stdout=1;
511}
512
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800513void ui_reset_text_col()
514{
515 pthread_mutex_lock(&gUpdateMutex);
516 text_col = 0;
517 pthread_mutex_unlock(&gUpdateMutex);
518}
519
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800520#define MENU_ITEM_HEADER " - "
521#define MENU_ITEM_HEADER_LENGTH strlen(MENU_ITEM_HEADER)
522
Koushik Duttadf1e4062010-12-18 17:42:31 -0800523int ui_start_menu(char** headers, char** items, int initial_selection) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800524 int i;
525 pthread_mutex_lock(&gUpdateMutex);
526 if (text_rows > 0 && text_cols > 0) {
527 for (i = 0; i < text_rows; ++i) {
528 if (headers[i] == NULL) break;
529 strncpy(menu[i], headers[i], text_cols-1);
530 menu[i][text_cols-1] = '\0';
531 }
532 menu_top = i;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100533 for (; i < MENU_MAX_ROWS; ++i) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800534 if (items[i-menu_top] == NULL) break;
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800535 strcpy(menu[i], MENU_ITEM_HEADER);
536 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 -0800537 menu[i][text_cols-1] = '\0';
538 }
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700539
540 if (gShowBackButton) {
541 strcpy(menu[i], " - +++++Go Back+++++");
542 ++i;
543 }
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100544
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800545 menu_items = i - menu_top;
546 show_menu = 1;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800547 menu_sel = menu_show_start = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800548 update_screen_locked();
549 }
550 pthread_mutex_unlock(&gUpdateMutex);
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700551 if (gShowBackButton) {
552 return menu_items - 1;
553 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700554 return menu_items;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800555}
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;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100563
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700564 if (menu_sel < 0) menu_sel = menu_items + menu_sel;
565 if (menu_sel >= menu_items) menu_sel = menu_sel - menu_items;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100566
567
568 if (menu_sel < menu_show_start && menu_show_start > 0) {
Koushik Dutta917c38a2010-05-24 16:43:39 -0700569 menu_show_start = menu_sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100570 }
571
572 if (menu_sel - menu_show_start + menu_top >= text_rows) {
Koushik Dutta917c38a2010-05-24 16:43:39 -0700573 menu_show_start = menu_sel + menu_top - text_rows + 1;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100574 }
575
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800576 sel = menu_sel;
Magnus Eriksson8bbbbd42010-03-08 16:09:49 +0100577
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800578 if (menu_sel != old_sel) update_screen_locked();
579 }
580 pthread_mutex_unlock(&gUpdateMutex);
581 return sel;
582}
583
584void ui_end_menu() {
585 int i;
586 pthread_mutex_lock(&gUpdateMutex);
587 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
588 show_menu = 0;
589 update_screen_locked();
590 }
591 pthread_mutex_unlock(&gUpdateMutex);
592}
593
594int ui_text_visible()
595{
596 pthread_mutex_lock(&gUpdateMutex);
597 int visible = show_text;
598 pthread_mutex_unlock(&gUpdateMutex);
599 return visible;
600}
601
Doug Zongker4bc98062010-09-03 11:00:13 -0700602void ui_show_text(int visible)
603{
604 pthread_mutex_lock(&gUpdateMutex);
605 show_text = visible;
606 update_screen_locked();
607 pthread_mutex_unlock(&gUpdateMutex);
608}
609
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800610int ui_wait_key()
611{
612 pthread_mutex_lock(&key_queue_mutex);
613 while (key_queue_len == 0) {
614 pthread_cond_wait(&key_queue_cond, &key_queue_mutex);
615 }
616
617 int key = key_queue[0];
618 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
619 pthread_mutex_unlock(&key_queue_mutex);
620 return key;
621}
622
623int ui_key_pressed(int key)
624{
625 // This is a volatile static array, don't bother locking
626 return key_pressed[key];
627}
628
629void ui_clear_key_queue() {
630 pthread_mutex_lock(&key_queue_mutex);
631 key_queue_len = 0;
632 pthread_mutex_unlock(&key_queue_mutex);
633}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800634
Koushik K. Dutta03173782010-02-26 14:14:23 -0800635void ui_set_show_text(int value) {
636 show_text = value;
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800637}
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700638
639void ui_set_showing_back_button(int showBackButton) {
640 gShowBackButton = showBackButton;
641}
642
643int ui_get_showing_back_button() {
644 return gShowBackButton;
645}