The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Zongker | 6ce4a32 | 2011-03-08 12:25:05 -0800 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 19 | #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 Zongker | 6ce4a32 | 2011-03-08 12:25:05 -0800 | [diff] [blame] | 25 | #include <sys/stat.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 26 | #include <sys/time.h> |
Doug Zongker | 6ce4a32 | 2011-03-08 12:25:05 -0800 | [diff] [blame] | 27 | #include <sys/types.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 28 | #include <time.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #include "common.h" |
Ken Sumrall | 6e4472a | 2011-03-07 23:37:27 -0800 | [diff] [blame] | 32 | #include <cutils/android_reboot.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 33 | #include "minui/minui.h" |
Doug Zongker | 23412e6 | 2009-07-23 10:16:07 -0700 | [diff] [blame] | 34 | #include "recovery_ui.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 35 | |
Tanguy Pruvot | 67d358c | 2011-06-10 20:26:44 +0200 | [diff] [blame] | 36 | extern int __system(const char *command); |
| 37 | |
Koushik Dutta | d90ad5d | 2010-11-27 14:41:07 -0800 | [diff] [blame] | 38 | #ifdef BOARD_HAS_NO_SELECT_BUTTON |
Koushik Dutta | 4ca9b4c | 2010-07-14 18:37:33 -0700 | [diff] [blame] | 39 | static int gShowBackButton = 1; |
| 40 | #else |
| 41 | static int gShowBackButton = 0; |
| 42 | #endif |
| 43 | |
Doug Zongker | 8a8e6cc | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 44 | #define MAX_COLS 96 |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 45 | #define MAX_ROWS 32 |
| 46 | |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 47 | #define MENU_MAX_COLS 64 |
| 48 | #define MENU_MAX_ROWS 250 |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 49 | |
Koushik Dutta | 3cdfd13 | 2012-01-26 22:56:00 -0800 | [diff] [blame] | 50 | #define MIN_LOG_ROWS 3 |
| 51 | |
Koushik Dutta | 18e3f62 | 2012-01-26 16:49:33 -0800 | [diff] [blame] | 52 | #define CHAR_WIDTH BOARD_RECOVERY_CHAR_WIDTH |
| 53 | #define CHAR_HEIGHT BOARD_RECOVERY_CHAR_HEIGHT |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 54 | |
Koushik Dutta | 0e7d886 | 2011-11-16 16:47:53 -0800 | [diff] [blame] | 55 | #define UI_WAIT_KEY_TIMEOUT_SEC 3600 |
Doug Zongker | 5cae445 | 2011-01-25 13:15:30 -0800 | [diff] [blame] | 56 | |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 57 | UIParameters ui_parameters = { |
Doug Zongker | be6d4d1 | 2011-03-02 10:38:02 -0800 | [diff] [blame] | 58 | 6, // indeterminate progress bar frames |
| 59 | 20, // fps |
| 60 | 7, // installation icon frames (0 == static image) |
Doug Zongker | fdfb636 | 2011-09-20 14:16:46 -0700 | [diff] [blame] | 61 | 13, 190, // installation icon overlay offset |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 62 | }; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 63 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 64 | static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER; |
| 65 | static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS]; |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 66 | static gr_surface *gInstallationOverlay; |
| 67 | static gr_surface *gProgressBarIndeterminate; |
Doug Zongker | d93a254 | 2009-10-08 16:32:58 -0700 | [diff] [blame] | 68 | static gr_surface gProgressBarEmpty; |
| 69 | static gr_surface gProgressBarFill; |
Koushik Dutta | d3cc60b | 2010-07-03 13:56:45 -0700 | [diff] [blame] | 70 | static int ui_has_initialized = 0; |
Tanguy Pruvot | 67d358c | 2011-06-10 20:26:44 +0200 | [diff] [blame] | 71 | static int ui_log_stdout = 1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 72 | |
| 73 | static const struct { gr_surface* surface; const char *name; } BITMAPS[] = { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 74 | { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" }, |
| 75 | { &gBackgroundIcon[BACKGROUND_ICON_ERROR], "icon_error" }, |
Koushik Dutta | 5d80817 | 2010-12-18 22:29:27 -0800 | [diff] [blame] | 76 | { &gBackgroundIcon[BACKGROUND_ICON_CLOCKWORK], "icon_clockwork" }, |
Koushik Dutta | 7f13e15 | 2011-09-08 16:55:35 -0700 | [diff] [blame] | 77 | { &gBackgroundIcon[BACKGROUND_ICON_FIRMWARE_INSTALLING], "icon_firmware_install" }, |
| 78 | { &gBackgroundIcon[BACKGROUND_ICON_FIRMWARE_ERROR], "icon_firmware_error" }, |
Doug Zongker | d93a254 | 2009-10-08 16:32:58 -0700 | [diff] [blame] | 79 | { &gProgressBarEmpty, "progress_empty" }, |
| 80 | { &gProgressBarFill, "progress_fill" }, |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 81 | { NULL, NULL }, |
| 82 | }; |
| 83 | |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 84 | static int gCurrentIcon = 0; |
| 85 | static int gInstallingFrame = 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 86 | |
| 87 | static enum ProgressBarType { |
| 88 | PROGRESSBAR_TYPE_NONE, |
| 89 | PROGRESSBAR_TYPE_INDETERMINATE, |
| 90 | PROGRESSBAR_TYPE_NORMAL, |
| 91 | } gProgressBarType = PROGRESSBAR_TYPE_NONE; |
| 92 | |
| 93 | // Progress bar scope of current operation |
| 94 | static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0; |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 95 | static double gProgressScopeTime, gProgressScopeDuration; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 96 | |
| 97 | // Set to 1 when both graphics pages are the same (except for the progress bar) |
| 98 | static int gPagesIdentical = 0; |
| 99 | |
| 100 | // Log text overlay, displayed when a magic key is pressed |
| 101 | static char text[MAX_ROWS][MAX_COLS]; |
| 102 | static int text_cols = 0, text_rows = 0; |
| 103 | static int text_col = 0, text_row = 0, text_top = 0; |
Ying Wang | 532c860 | 2010-09-01 14:52:22 -0700 | [diff] [blame] | 104 | static int show_text = 0; |
Doug Zongker | 5cae445 | 2011-01-25 13:15:30 -0800 | [diff] [blame] | 105 | static int show_text_ever = 0; // has show_text ever been 1? |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 106 | |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 107 | static char menu[MENU_MAX_ROWS][MENU_MAX_COLS]; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 108 | static int show_menu = 0; |
| 109 | static int menu_top = 0, menu_items = 0, menu_sel = 0; |
Tanguy Pruvot | 67d358c | 2011-06-10 20:26:44 +0200 | [diff] [blame] | 110 | static int menu_show_start = 0; // this is line which menu display is starting at |
Koushik Dutta | 3cdfd13 | 2012-01-26 22:56:00 -0800 | [diff] [blame] | 111 | static int max_menu_rows; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 112 | |
| 113 | // Key event input queue |
| 114 | static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 115 | static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER; |
| 116 | static int key_queue[256], key_queue_len = 0; |
| 117 | static volatile char key_pressed[KEY_MAX + 1]; |
| 118 | |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 119 | // Return the current time as a double (including fractions of a second). |
| 120 | static double now() { |
| 121 | struct timeval tv; |
| 122 | gettimeofday(&tv, NULL); |
| 123 | return tv.tv_sec + tv.tv_usec / 1000000.0; |
| 124 | } |
| 125 | |
| 126 | // Draw the given frame over the installation overlay animation. The |
| 127 | // background is not cleared or draw with the base icon first; we |
| 128 | // assume that the frame already contains some other frame of the |
| 129 | // animation. Does nothing if no overlay animation is defined. |
| 130 | // Should only be called with gUpdateMutex locked. |
| 131 | static void draw_install_overlay_locked(int frame) { |
| 132 | if (gInstallationOverlay == NULL) return; |
| 133 | gr_surface surface = gInstallationOverlay[frame]; |
| 134 | int iconWidth = gr_get_width(surface); |
| 135 | int iconHeight = gr_get_height(surface); |
| 136 | gr_blit(surface, 0, 0, iconWidth, iconHeight, |
| 137 | ui_parameters.install_overlay_offset_x, |
| 138 | ui_parameters.install_overlay_offset_y); |
| 139 | } |
| 140 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 141 | // Clear the screen and draw the currently selected background icon (if any). |
| 142 | // Should only be called with gUpdateMutex locked. |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 143 | static void draw_background_locked(int icon) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 144 | { |
| 145 | gPagesIdentical = 0; |
| 146 | gr_color(0, 0, 0, 255); |
| 147 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 148 | |
| 149 | if (icon) { |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 150 | gr_surface surface = gBackgroundIcon[icon]; |
| 151 | int iconWidth = gr_get_width(surface); |
| 152 | int iconHeight = gr_get_height(surface); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 153 | int iconX = (gr_fb_width() - iconWidth) / 2; |
| 154 | int iconY = (gr_fb_height() - iconHeight) / 2; |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 155 | gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY); |
| 156 | if (icon == BACKGROUND_ICON_INSTALLING) { |
| 157 | draw_install_overlay_locked(gInstallingFrame); |
| 158 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | // Draw the progress bar (if any) on the screen. Does not flip pages. |
| 163 | // Should only be called with gUpdateMutex locked. |
| 164 | static void draw_progress_locked() |
| 165 | { |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 166 | if (gCurrentIcon == BACKGROUND_ICON_INSTALLING) { |
| 167 | draw_install_overlay_locked(gInstallingFrame); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 170 | if (gProgressBarType != PROGRESSBAR_TYPE_NONE) { |
| 171 | int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]); |
| 172 | int width = gr_get_width(gProgressBarEmpty); |
| 173 | int height = gr_get_height(gProgressBarEmpty); |
| 174 | |
| 175 | int dx = (gr_fb_width() - width)/2; |
| 176 | int dy = (3*gr_fb_height() + iconHeight - 2*height)/4; |
| 177 | |
| 178 | // Erase behind the progress bar (in case this was a progress-only update) |
| 179 | gr_color(0, 0, 0, 255); |
| 180 | gr_fill(dx, dy, width, height); |
| 181 | |
| 182 | if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) { |
| 183 | float progress = gProgressScopeStart + gProgress * gProgressScopeSize; |
| 184 | int pos = (int) (progress * width); |
| 185 | |
| 186 | if (pos > 0) { |
| 187 | gr_blit(gProgressBarFill, 0, 0, pos, height, dx, dy); |
| 188 | } |
| 189 | if (pos < width-1) { |
| 190 | gr_blit(gProgressBarEmpty, pos, 0, width-pos, height, dx+pos, dy); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) { |
| 195 | static int frame = 0; |
| 196 | gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy); |
| 197 | frame = (frame + 1) % ui_parameters.indeterminate_frames; |
| 198 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | static void draw_text_line(int row, const char* t) { |
| 203 | if (t[0] != '\0') { |
| 204 | gr_text(0, (row+1)*CHAR_HEIGHT-1, t); |
| 205 | } |
| 206 | } |
| 207 | |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 208 | //#define MENU_TEXT_COLOR 255, 160, 49, 255 |
| 209 | #define MENU_TEXT_COLOR 0, 191, 255, 255 |
Koushik K. Dutta | 261dde9 | 2010-02-25 16:51:45 -0800 | [diff] [blame] | 210 | #define NORMAL_TEXT_COLOR 200, 200, 200, 255 |
| 211 | #define HEADER_TEXT_COLOR NORMAL_TEXT_COLOR |
| 212 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 213 | // Redraw everything on the screen. Does not flip pages. |
| 214 | // Should only be called with gUpdateMutex locked. |
| 215 | static void draw_screen_locked(void) |
| 216 | { |
Koushik Dutta | d3cc60b | 2010-07-03 13:56:45 -0700 | [diff] [blame] | 217 | if (!ui_has_initialized) return; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 218 | draw_background_locked(gCurrentIcon); |
| 219 | draw_progress_locked(); |
| 220 | |
| 221 | if (show_text) { |
| 222 | gr_color(0, 0, 0, 160); |
| 223 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 224 | |
Koushik Dutta | 3cdfd13 | 2012-01-26 22:56:00 -0800 | [diff] [blame] | 225 | int total_rows = gr_fb_height() / CHAR_HEIGHT; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 226 | int i = 0; |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 227 | int j = 0; |
CEnnis91 | 4f78176 | 2012-01-14 23:51:58 -0500 | [diff] [blame] | 228 | int offset = 0; // offset of separating bar under menus |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 229 | int row = 0; // current row that we are drawing on |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 230 | if (show_menu) { |
Koushik K. Dutta | 261dde9 | 2010-02-25 16:51:45 -0800 | [diff] [blame] | 231 | gr_color(MENU_TEXT_COLOR); |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 232 | gr_fill(0, (menu_top + menu_sel - menu_show_start) * CHAR_HEIGHT, |
| 233 | gr_fb_width(), (menu_top + menu_sel - menu_show_start + 1)*CHAR_HEIGHT+1); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 234 | |
Koushik K. Dutta | 261dde9 | 2010-02-25 16:51:45 -0800 | [diff] [blame] | 235 | gr_color(HEADER_TEXT_COLOR); |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 236 | for (i = 0; i < menu_top; ++i) { |
| 237 | draw_text_line(i, menu[i]); |
| 238 | row++; |
| 239 | } |
| 240 | |
Koushik Dutta | 1c50ff2 | 2012-01-31 20:02:38 -0800 | [diff] [blame] | 241 | if (menu_items - menu_show_start + menu_top >= max_menu_rows) |
| 242 | j = max_menu_rows - menu_top; |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 243 | else |
| 244 | j = menu_items - menu_show_start; |
| 245 | |
| 246 | gr_color(MENU_TEXT_COLOR); |
| 247 | for (i = menu_show_start + menu_top; i < (menu_show_start + menu_top + j); ++i) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 248 | if (i == menu_top + menu_sel) { |
| 249 | gr_color(255, 255, 255, 255); |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 250 | draw_text_line(i - menu_show_start , menu[i]); |
Koushik K. Dutta | 261dde9 | 2010-02-25 16:51:45 -0800 | [diff] [blame] | 251 | gr_color(MENU_TEXT_COLOR); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 252 | } else { |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 253 | gr_color(MENU_TEXT_COLOR); |
| 254 | draw_text_line(i - menu_show_start, menu[i]); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 255 | } |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 256 | row++; |
Koushik Dutta | 1c50ff2 | 2012-01-31 20:02:38 -0800 | [diff] [blame] | 257 | if (row >= max_menu_rows) |
Koushik Dutta | 3cdfd13 | 2012-01-26 22:56:00 -0800 | [diff] [blame] | 258 | break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 259 | } |
CEnnis91 | 4f78176 | 2012-01-14 23:51:58 -0500 | [diff] [blame] | 260 | |
Koushik Dutta | 1c50ff2 | 2012-01-31 20:02:38 -0800 | [diff] [blame] | 261 | if (menu_items <= max_menu_rows) |
CEnnis91 | 4f78176 | 2012-01-14 23:51:58 -0500 | [diff] [blame] | 262 | offset = 1; |
Koushik Dutta | 1c50ff2 | 2012-01-31 20:02:38 -0800 | [diff] [blame] | 263 | |
CEnnis91 | 4f78176 | 2012-01-14 23:51:58 -0500 | [diff] [blame] | 264 | gr_fill(0, (row-offset)*CHAR_HEIGHT+CHAR_HEIGHT/2-1, |
| 265 | gr_fb_width(), (row-offset)*CHAR_HEIGHT+CHAR_HEIGHT/2+1); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Koushik K. Dutta | 261dde9 | 2010-02-25 16:51:45 -0800 | [diff] [blame] | 268 | gr_color(NORMAL_TEXT_COLOR); |
Koushik Dutta | 3cdfd13 | 2012-01-26 22:56:00 -0800 | [diff] [blame] | 269 | int cur_row = text_row; |
| 270 | int available_rows = total_rows - row - 1; |
| 271 | int start_row = row + 1; |
| 272 | if (available_rows < MAX_ROWS) |
| 273 | cur_row = (cur_row + (MAX_ROWS - available_rows)) % MAX_ROWS; |
| 274 | else |
| 275 | start_row = total_rows - MAX_ROWS; |
| 276 | |
| 277 | int r; |
| 278 | for (r = 0; r < (available_rows < MAX_ROWS ? available_rows : MAX_ROWS); r++) { |
| 279 | draw_text_line(start_row + r, text[(cur_row + r) % MAX_ROWS]); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // Redraw everything on the screen and flip the screen (make it visible). |
| 285 | // Should only be called with gUpdateMutex locked. |
| 286 | static void update_screen_locked(void) |
| 287 | { |
Koushik Dutta | d3cc60b | 2010-07-03 13:56:45 -0700 | [diff] [blame] | 288 | if (!ui_has_initialized) return; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 289 | draw_screen_locked(); |
| 290 | gr_flip(); |
| 291 | } |
| 292 | |
| 293 | // Updates only the progress bar, if possible, otherwise redraws the screen. |
| 294 | // Should only be called with gUpdateMutex locked. |
| 295 | static void update_progress_locked(void) |
| 296 | { |
Koushik Dutta | d3cc60b | 2010-07-03 13:56:45 -0700 | [diff] [blame] | 297 | if (!ui_has_initialized) return; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 298 | if (show_text || !gPagesIdentical) { |
| 299 | draw_screen_locked(); // Must redraw the whole screen |
| 300 | gPagesIdentical = 1; |
| 301 | } else { |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 302 | draw_progress_locked(); // Draw only the progress bar and overlays |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 303 | } |
| 304 | gr_flip(); |
| 305 | } |
| 306 | |
| 307 | // Keeps the progress bar updated, even when the process is otherwise busy. |
| 308 | static void *progress_thread(void *cookie) |
| 309 | { |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 310 | double interval = 1.0 / ui_parameters.update_fps; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 311 | for (;;) { |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 312 | double start = now(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 313 | pthread_mutex_lock(&gUpdateMutex); |
| 314 | |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 315 | int redraw = 0; |
| 316 | |
| 317 | // update the installation animation, if active |
| 318 | // skip this if we have a text overlay (too expensive to update) |
| 319 | if (gCurrentIcon == BACKGROUND_ICON_INSTALLING && |
| 320 | ui_parameters.installing_frames > 0 && |
| 321 | !show_text) { |
| 322 | gInstallingFrame = |
| 323 | (gInstallingFrame + 1) % ui_parameters.installing_frames; |
| 324 | redraw = 1; |
| 325 | } |
| 326 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 327 | // update the progress bar animation, if active |
| 328 | // skip this if we have a text overlay (too expensive to update) |
| 329 | if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) { |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 330 | redraw = 1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // move the progress bar forward on timed intervals, if configured |
| 334 | int duration = gProgressScopeDuration; |
| 335 | if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) { |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 336 | double elapsed = now() - gProgressScopeTime; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 337 | float progress = 1.0 * elapsed / duration; |
| 338 | if (progress > 1.0) progress = 1.0; |
| 339 | if (progress > gProgress) { |
| 340 | gProgress = progress; |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 341 | redraw = 1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 345 | if (redraw) update_progress_locked(); |
| 346 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 347 | pthread_mutex_unlock(&gUpdateMutex); |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 348 | double end = now(); |
| 349 | // minimum of 20ms delay between frames |
| 350 | double delay = interval - (end-start); |
| 351 | if (delay < 0.02) delay = 0.02; |
| 352 | usleep((long)(delay * 1000000)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 353 | } |
| 354 | return NULL; |
| 355 | } |
| 356 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 357 | static int rel_sum = 0; |
| 358 | |
| 359 | static int input_callback(int fd, short revents, void *data) |
| 360 | { |
| 361 | struct input_event ev; |
| 362 | int ret; |
| 363 | int fake_key = 0; |
| 364 | |
| 365 | ret = ev_get_input(fd, revents, &ev); |
| 366 | if (ret) |
| 367 | return -1; |
| 368 | |
| 369 | if (ev.type == EV_SYN) { |
| 370 | return 0; |
| 371 | } else if (ev.type == EV_REL) { |
| 372 | if (ev.code == REL_Y) { |
| 373 | // accumulate the up or down motion reported by |
| 374 | // the trackball. When it exceeds a threshold |
| 375 | // (positive or negative), fake an up/down |
| 376 | // key event. |
| 377 | rel_sum += ev.value; |
| 378 | if (rel_sum > 3) { |
| 379 | fake_key = 1; |
| 380 | ev.type = EV_KEY; |
| 381 | ev.code = KEY_DOWN; |
| 382 | ev.value = 1; |
| 383 | rel_sum = 0; |
| 384 | } else if (rel_sum < -3) { |
| 385 | fake_key = 1; |
| 386 | ev.type = EV_KEY; |
| 387 | ev.code = KEY_UP; |
| 388 | ev.value = 1; |
| 389 | rel_sum = 0; |
| 390 | } |
| 391 | } |
| 392 | } else { |
| 393 | rel_sum = 0; |
| 394 | } |
| 395 | |
| 396 | if (ev.type != EV_KEY || ev.code > KEY_MAX) |
| 397 | return 0; |
| 398 | |
| 399 | pthread_mutex_lock(&key_queue_mutex); |
| 400 | if (!fake_key) { |
| 401 | // our "fake" keys only report a key-down event (no |
| 402 | // key-up), so don't record them in the key_pressed |
| 403 | // table. |
| 404 | key_pressed[ev.code] = ev.value; |
| 405 | } |
| 406 | const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]); |
| 407 | if (ev.value > 0 && key_queue_len < queue_max) { |
| 408 | key_queue[key_queue_len++] = ev.code; |
| 409 | pthread_cond_signal(&key_queue_cond); |
| 410 | } |
| 411 | pthread_mutex_unlock(&key_queue_mutex); |
| 412 | |
| 413 | if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) { |
| 414 | pthread_mutex_lock(&gUpdateMutex); |
| 415 | show_text = !show_text; |
| 416 | if (show_text) show_text_ever = 1; |
| 417 | update_screen_locked(); |
| 418 | pthread_mutex_unlock(&gUpdateMutex); |
| 419 | } |
| 420 | |
| 421 | if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) { |
| 422 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
| 423 | } |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 428 | // Reads input events, handles special hot keys, and adds to the key queue. |
| 429 | static void *input_thread(void *cookie) |
| 430 | { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 431 | for (;;) { |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 432 | if (!ev_wait(-1)) |
| 433 | ev_dispatch(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 434 | } |
| 435 | return NULL; |
| 436 | } |
| 437 | |
| 438 | void ui_init(void) |
| 439 | { |
Koushik Dutta | d3cc60b | 2010-07-03 13:56:45 -0700 | [diff] [blame] | 440 | ui_has_initialized = 1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 441 | gr_init(); |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 442 | ev_init(input_callback, NULL); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 443 | |
| 444 | text_col = text_row = 0; |
| 445 | text_rows = gr_fb_height() / CHAR_HEIGHT; |
Koushik Dutta | 3cdfd13 | 2012-01-26 22:56:00 -0800 | [diff] [blame] | 446 | max_menu_rows = text_rows - MIN_LOG_ROWS; |
Koushik Dutta | 1c50ff2 | 2012-01-31 20:02:38 -0800 | [diff] [blame] | 447 | if (max_menu_rows > MENU_MAX_ROWS) |
| 448 | max_menu_rows = MENU_MAX_ROWS; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 449 | if (text_rows > MAX_ROWS) text_rows = MAX_ROWS; |
| 450 | text_top = 1; |
| 451 | |
| 452 | text_cols = gr_fb_width() / CHAR_WIDTH; |
| 453 | if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1; |
| 454 | |
| 455 | int i; |
| 456 | for (i = 0; BITMAPS[i].name != NULL; ++i) { |
| 457 | int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface); |
| 458 | if (result < 0) { |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 459 | LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 463 | gProgressBarIndeterminate = malloc(ui_parameters.indeterminate_frames * |
| 464 | sizeof(gr_surface)); |
| 465 | for (i = 0; i < ui_parameters.indeterminate_frames; ++i) { |
| 466 | char filename[40]; |
Doug Zongker | be6d4d1 | 2011-03-02 10:38:02 -0800 | [diff] [blame] | 467 | // "indeterminate01.png", "indeterminate02.png", ... |
| 468 | sprintf(filename, "indeterminate%02d", i+1); |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 469 | int result = res_create_surface(filename, gProgressBarIndeterminate+i); |
| 470 | if (result < 0) { |
| 471 | LOGE("Missing bitmap %s\n(Code %d)\n", filename, result); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | if (ui_parameters.installing_frames > 0) { |
| 476 | gInstallationOverlay = malloc(ui_parameters.installing_frames * |
| 477 | sizeof(gr_surface)); |
| 478 | for (i = 0; i < ui_parameters.installing_frames; ++i) { |
| 479 | char filename[40]; |
Doug Zongker | be6d4d1 | 2011-03-02 10:38:02 -0800 | [diff] [blame] | 480 | // "icon_installing_overlay01.png", |
| 481 | // "icon_installing_overlay02.png", ... |
| 482 | sprintf(filename, "icon_installing_overlay%02d", i+1); |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 483 | int result = res_create_surface(filename, gInstallationOverlay+i); |
| 484 | if (result < 0) { |
| 485 | LOGE("Missing bitmap %s\n(Code %d)\n", filename, result); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | // Adjust the offset to account for the positioning of the |
| 490 | // base image on the screen. |
| 491 | if (gBackgroundIcon[BACKGROUND_ICON_INSTALLING] != NULL) { |
| 492 | gr_surface bg = gBackgroundIcon[BACKGROUND_ICON_INSTALLING]; |
| 493 | ui_parameters.install_overlay_offset_x += |
| 494 | (gr_fb_width() - gr_get_width(bg)) / 2; |
| 495 | ui_parameters.install_overlay_offset_y += |
| 496 | (gr_fb_height() - gr_get_height(bg)) / 2; |
| 497 | } |
| 498 | } else { |
| 499 | gInstallationOverlay = NULL; |
| 500 | } |
| 501 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 502 | pthread_t t; |
| 503 | pthread_create(&t, NULL, progress_thread, NULL); |
| 504 | pthread_create(&t, NULL, input_thread, NULL); |
| 505 | } |
| 506 | |
Koushik Dutta | 107629b | 2010-06-30 23:17:53 -0700 | [diff] [blame] | 507 | char *ui_copy_image(int icon, int *width, int *height, int *bpp) { |
| 508 | pthread_mutex_lock(&gUpdateMutex); |
| 509 | draw_background_locked(gBackgroundIcon[icon]); |
| 510 | *width = gr_fb_width(); |
| 511 | *height = gr_fb_height(); |
| 512 | *bpp = sizeof(gr_pixel) * 8; |
| 513 | int size = *width * *height * sizeof(gr_pixel); |
| 514 | char *ret = malloc(size); |
| 515 | if (ret == NULL) { |
| 516 | LOGE("Can't allocate %d bytes for image\n", size); |
| 517 | } else { |
| 518 | memcpy(ret, gr_fb_data(), size); |
| 519 | } |
| 520 | pthread_mutex_unlock(&gUpdateMutex); |
| 521 | return ret; |
| 522 | } |
| 523 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 524 | void ui_set_background(int icon) |
| 525 | { |
| 526 | pthread_mutex_lock(&gUpdateMutex); |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 527 | gCurrentIcon = icon; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 528 | update_screen_locked(); |
| 529 | pthread_mutex_unlock(&gUpdateMutex); |
| 530 | } |
| 531 | |
| 532 | void ui_show_indeterminate_progress() |
| 533 | { |
| 534 | pthread_mutex_lock(&gUpdateMutex); |
| 535 | if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) { |
| 536 | gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE; |
| 537 | update_progress_locked(); |
| 538 | } |
| 539 | pthread_mutex_unlock(&gUpdateMutex); |
| 540 | } |
| 541 | |
| 542 | void ui_show_progress(float portion, int seconds) |
| 543 | { |
| 544 | pthread_mutex_lock(&gUpdateMutex); |
| 545 | gProgressBarType = PROGRESSBAR_TYPE_NORMAL; |
| 546 | gProgressScopeStart += gProgressScopeSize; |
| 547 | gProgressScopeSize = portion; |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 548 | gProgressScopeTime = now(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 549 | gProgressScopeDuration = seconds; |
| 550 | gProgress = 0; |
| 551 | update_progress_locked(); |
| 552 | pthread_mutex_unlock(&gUpdateMutex); |
| 553 | } |
| 554 | |
| 555 | void ui_set_progress(float fraction) |
| 556 | { |
| 557 | pthread_mutex_lock(&gUpdateMutex); |
| 558 | if (fraction < 0.0) fraction = 0.0; |
| 559 | if (fraction > 1.0) fraction = 1.0; |
| 560 | if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) { |
| 561 | // Skip updates that aren't visibly different. |
| 562 | int width = gr_get_width(gProgressBarIndeterminate[0]); |
| 563 | float scale = width * gProgressScopeSize; |
| 564 | if ((int) (gProgress * scale) != (int) (fraction * scale)) { |
| 565 | gProgress = fraction; |
| 566 | update_progress_locked(); |
| 567 | } |
| 568 | } |
| 569 | pthread_mutex_unlock(&gUpdateMutex); |
| 570 | } |
| 571 | |
| 572 | void ui_reset_progress() |
| 573 | { |
| 574 | pthread_mutex_lock(&gUpdateMutex); |
| 575 | gProgressBarType = PROGRESSBAR_TYPE_NONE; |
| 576 | gProgressScopeStart = gProgressScopeSize = 0; |
| 577 | gProgressScopeTime = gProgressScopeDuration = 0; |
| 578 | gProgress = 0; |
| 579 | update_screen_locked(); |
| 580 | pthread_mutex_unlock(&gUpdateMutex); |
| 581 | } |
| 582 | |
| 583 | void ui_print(const char *fmt, ...) |
| 584 | { |
| 585 | char buf[256]; |
| 586 | va_list ap; |
| 587 | va_start(ap, fmt); |
| 588 | vsnprintf(buf, 256, fmt, ap); |
| 589 | va_end(ap); |
| 590 | |
Tanguy Pruvot | 67d358c | 2011-06-10 20:26:44 +0200 | [diff] [blame] | 591 | if (ui_log_stdout) |
| 592 | fputs(buf, stdout); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 593 | |
| 594 | // This can get called before ui_init(), so be careful. |
| 595 | pthread_mutex_lock(&gUpdateMutex); |
| 596 | if (text_rows > 0 && text_cols > 0) { |
| 597 | char *ptr; |
| 598 | for (ptr = buf; *ptr != '\0'; ++ptr) { |
| 599 | if (*ptr == '\n' || text_col >= text_cols) { |
| 600 | text[text_row][text_col] = '\0'; |
| 601 | text_col = 0; |
| 602 | text_row = (text_row + 1) % text_rows; |
| 603 | if (text_row == text_top) text_top = (text_top + 1) % text_rows; |
| 604 | } |
| 605 | if (*ptr != '\n') text[text_row][text_col++] = *ptr; |
| 606 | } |
| 607 | text[text_row][text_col] = '\0'; |
| 608 | update_screen_locked(); |
| 609 | } |
| 610 | pthread_mutex_unlock(&gUpdateMutex); |
| 611 | } |
| 612 | |
Tanguy Pruvot | 67d358c | 2011-06-10 20:26:44 +0200 | [diff] [blame] | 613 | void ui_printlogtail(int nb_lines) { |
| 614 | char * log_data; |
| 615 | char tmp[PATH_MAX]; |
| 616 | FILE * f; |
| 617 | int line=0; |
| 618 | //don't log output to recovery.log |
| 619 | ui_log_stdout=0; |
| 620 | sprintf(tmp, "tail -n %d /tmp/recovery.log > /tmp/tail.log", nb_lines); |
| 621 | __system(tmp); |
| 622 | f = fopen("/tmp/tail.log", "rb"); |
| 623 | if (f != NULL) { |
| 624 | while (line < nb_lines) { |
| 625 | log_data = fgets(tmp, PATH_MAX, f); |
| 626 | if (log_data == NULL) break; |
| 627 | ui_print("%s", tmp); |
| 628 | line++; |
| 629 | } |
| 630 | fclose(f); |
| 631 | } |
| 632 | ui_log_stdout=1; |
| 633 | } |
| 634 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 635 | void ui_reset_text_col() |
| 636 | { |
| 637 | pthread_mutex_lock(&gUpdateMutex); |
| 638 | text_col = 0; |
| 639 | pthread_mutex_unlock(&gUpdateMutex); |
| 640 | } |
| 641 | |
Koushik K. Dutta | 261dde9 | 2010-02-25 16:51:45 -0800 | [diff] [blame] | 642 | #define MENU_ITEM_HEADER " - " |
| 643 | #define MENU_ITEM_HEADER_LENGTH strlen(MENU_ITEM_HEADER) |
| 644 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 645 | int ui_start_menu(char** headers, char** items, int initial_selection) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 646 | int i; |
| 647 | pthread_mutex_lock(&gUpdateMutex); |
| 648 | if (text_rows > 0 && text_cols > 0) { |
| 649 | for (i = 0; i < text_rows; ++i) { |
| 650 | if (headers[i] == NULL) break; |
| 651 | strncpy(menu[i], headers[i], text_cols-1); |
| 652 | menu[i][text_cols-1] = '\0'; |
| 653 | } |
| 654 | menu_top = i; |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 655 | for (; i < MENU_MAX_ROWS; ++i) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 656 | if (items[i-menu_top] == NULL) break; |
Koushik K. Dutta | 261dde9 | 2010-02-25 16:51:45 -0800 | [diff] [blame] | 657 | strcpy(menu[i], MENU_ITEM_HEADER); |
Koushik Dutta | 3cdfd13 | 2012-01-26 22:56:00 -0800 | [diff] [blame] | 658 | strncpy(menu[i] + MENU_ITEM_HEADER_LENGTH, items[i-menu_top], MENU_MAX_COLS - 1 - MENU_ITEM_HEADER_LENGTH); |
| 659 | menu[i][MENU_MAX_COLS-1] = '\0'; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 660 | } |
Koushik Dutta | 4ca9b4c | 2010-07-14 18:37:33 -0700 | [diff] [blame] | 661 | |
CEnnis91 | 4f78176 | 2012-01-14 23:51:58 -0500 | [diff] [blame] | 662 | if (gShowBackButton && ui_menu_level > 0) { |
Koushik Dutta | 4ca9b4c | 2010-07-14 18:37:33 -0700 | [diff] [blame] | 663 | strcpy(menu[i], " - +++++Go Back+++++"); |
| 664 | ++i; |
| 665 | } |
CEnnis91 | 4f78176 | 2012-01-14 23:51:58 -0500 | [diff] [blame] | 666 | |
| 667 | strcpy(menu[i], " "); |
| 668 | ++i; |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 669 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 670 | menu_items = i - menu_top; |
| 671 | show_menu = 1; |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 672 | menu_sel = menu_show_start = initial_selection; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 673 | update_screen_locked(); |
| 674 | } |
| 675 | pthread_mutex_unlock(&gUpdateMutex); |
CEnnis91 | 4f78176 | 2012-01-14 23:51:58 -0500 | [diff] [blame] | 676 | if (gShowBackButton && ui_menu_level > 0) { |
Koushik Dutta | 4ca9b4c | 2010-07-14 18:37:33 -0700 | [diff] [blame] | 677 | return menu_items - 1; |
| 678 | } |
Koushik Dutta | f4e3a67 | 2010-06-09 12:19:41 -0700 | [diff] [blame] | 679 | return menu_items; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | int ui_menu_select(int sel) { |
| 683 | int old_sel; |
| 684 | pthread_mutex_lock(&gUpdateMutex); |
| 685 | if (show_menu > 0) { |
| 686 | old_sel = menu_sel; |
| 687 | menu_sel = sel; |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 688 | |
CEnnis91 | 4f78176 | 2012-01-14 23:51:58 -0500 | [diff] [blame] | 689 | if (menu_sel < 0) menu_sel = menu_items-1 + menu_sel; |
| 690 | if (menu_sel >= menu_items-1) menu_sel = menu_sel - menu_items+1; |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 691 | |
| 692 | |
| 693 | if (menu_sel < menu_show_start && menu_show_start > 0) { |
Koushik Dutta | 917c38a | 2010-05-24 16:43:39 -0700 | [diff] [blame] | 694 | menu_show_start = menu_sel; |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 695 | } |
| 696 | |
Koushik Dutta | 3cdfd13 | 2012-01-26 22:56:00 -0800 | [diff] [blame] | 697 | if (menu_sel - menu_show_start + menu_top >= max_menu_rows) { |
| 698 | menu_show_start = menu_sel + menu_top - max_menu_rows + 1; |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 699 | } |
| 700 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 701 | sel = menu_sel; |
Magnus Eriksson | 8bbbbd4 | 2010-03-08 16:09:49 +0100 | [diff] [blame] | 702 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 703 | if (menu_sel != old_sel) update_screen_locked(); |
| 704 | } |
| 705 | pthread_mutex_unlock(&gUpdateMutex); |
| 706 | return sel; |
| 707 | } |
| 708 | |
| 709 | void ui_end_menu() { |
| 710 | int i; |
| 711 | pthread_mutex_lock(&gUpdateMutex); |
| 712 | if (show_menu > 0 && text_rows > 0 && text_cols > 0) { |
| 713 | show_menu = 0; |
| 714 | update_screen_locked(); |
| 715 | } |
| 716 | pthread_mutex_unlock(&gUpdateMutex); |
| 717 | } |
| 718 | |
| 719 | int ui_text_visible() |
| 720 | { |
| 721 | pthread_mutex_lock(&gUpdateMutex); |
| 722 | int visible = show_text; |
| 723 | pthread_mutex_unlock(&gUpdateMutex); |
| 724 | return visible; |
| 725 | } |
| 726 | |
Doug Zongker | 5cae445 | 2011-01-25 13:15:30 -0800 | [diff] [blame] | 727 | int ui_text_ever_visible() |
| 728 | { |
| 729 | pthread_mutex_lock(&gUpdateMutex); |
| 730 | int ever_visible = show_text_ever; |
| 731 | pthread_mutex_unlock(&gUpdateMutex); |
| 732 | return ever_visible; |
| 733 | } |
| 734 | |
Doug Zongker | 4bc9806 | 2010-09-03 11:00:13 -0700 | [diff] [blame] | 735 | void ui_show_text(int visible) |
| 736 | { |
| 737 | pthread_mutex_lock(&gUpdateMutex); |
| 738 | show_text = visible; |
Doug Zongker | 5cae445 | 2011-01-25 13:15:30 -0800 | [diff] [blame] | 739 | if (show_text) show_text_ever = 1; |
Doug Zongker | 4bc9806 | 2010-09-03 11:00:13 -0700 | [diff] [blame] | 740 | update_screen_locked(); |
| 741 | pthread_mutex_unlock(&gUpdateMutex); |
| 742 | } |
| 743 | |
Doug Zongker | 6ce4a32 | 2011-03-08 12:25:05 -0800 | [diff] [blame] | 744 | // Return true if USB is connected. |
| 745 | static int usb_connected() { |
Benoit Goby | 7e6067e | 2011-06-24 17:54:19 -0700 | [diff] [blame] | 746 | int fd = open("/sys/class/android_usb/android0/state", O_RDONLY); |
Doug Zongker | 6ce4a32 | 2011-03-08 12:25:05 -0800 | [diff] [blame] | 747 | if (fd < 0) { |
Benoit Goby | 7e6067e | 2011-06-24 17:54:19 -0700 | [diff] [blame] | 748 | printf("failed to open /sys/class/android_usb/android0/state: %s\n", |
Doug Zongker | 6ce4a32 | 2011-03-08 12:25:05 -0800 | [diff] [blame] | 749 | strerror(errno)); |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | char buf; |
Benoit Goby | 7e6067e | 2011-06-24 17:54:19 -0700 | [diff] [blame] | 754 | /* USB is connected if android_usb state is CONNECTED or CONFIGURED */ |
| 755 | int connected = (read(fd, &buf, 1) == 1) && (buf == 'C'); |
Doug Zongker | 6ce4a32 | 2011-03-08 12:25:05 -0800 | [diff] [blame] | 756 | if (close(fd) < 0) { |
Benoit Goby | 7e6067e | 2011-06-24 17:54:19 -0700 | [diff] [blame] | 757 | printf("failed to close /sys/class/android_usb/android0/state: %s\n", |
Doug Zongker | 6ce4a32 | 2011-03-08 12:25:05 -0800 | [diff] [blame] | 758 | strerror(errno)); |
| 759 | } |
| 760 | return connected; |
| 761 | } |
| 762 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 763 | int ui_wait_key() |
| 764 | { |
| 765 | pthread_mutex_lock(&key_queue_mutex); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 766 | |
Doug Zongker | 6ce4a32 | 2011-03-08 12:25:05 -0800 | [diff] [blame] | 767 | // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is |
| 768 | // plugged in. |
| 769 | do { |
| 770 | struct timeval now; |
| 771 | struct timespec timeout; |
| 772 | gettimeofday(&now, NULL); |
| 773 | timeout.tv_sec = now.tv_sec; |
| 774 | timeout.tv_nsec = now.tv_usec * 1000; |
| 775 | timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC; |
Doug Zongker | 5cae445 | 2011-01-25 13:15:30 -0800 | [diff] [blame] | 776 | |
Doug Zongker | 6ce4a32 | 2011-03-08 12:25:05 -0800 | [diff] [blame] | 777 | int rc = 0; |
| 778 | while (key_queue_len == 0 && rc != ETIMEDOUT) { |
| 779 | rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, |
| 780 | &timeout); |
| 781 | } |
| 782 | } while (usb_connected() && key_queue_len == 0); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 783 | |
Doug Zongker | 5cae445 | 2011-01-25 13:15:30 -0800 | [diff] [blame] | 784 | int key = -1; |
| 785 | if (key_queue_len > 0) { |
| 786 | key = key_queue[0]; |
| 787 | memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len); |
| 788 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 789 | pthread_mutex_unlock(&key_queue_mutex); |
| 790 | return key; |
| 791 | } |
| 792 | |
| 793 | int ui_key_pressed(int key) |
| 794 | { |
| 795 | // This is a volatile static array, don't bother locking |
| 796 | return key_pressed[key]; |
| 797 | } |
| 798 | |
| 799 | void ui_clear_key_queue() { |
| 800 | pthread_mutex_lock(&key_queue_mutex); |
| 801 | key_queue_len = 0; |
| 802 | pthread_mutex_unlock(&key_queue_mutex); |
| 803 | } |
Koushik K. Dutta | a3c2f73 | 2010-02-19 14:17:22 -0800 | [diff] [blame] | 804 | |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 805 | void ui_set_show_text(int value) { |
| 806 | show_text = value; |
Koushik K. Dutta | a3c2f73 | 2010-02-19 14:17:22 -0800 | [diff] [blame] | 807 | } |
Koushik Dutta | 4ca9b4c | 2010-07-14 18:37:33 -0700 | [diff] [blame] | 808 | |
| 809 | void ui_set_showing_back_button(int showBackButton) { |
| 810 | gShowBackButton = showBackButton; |
| 811 | } |
| 812 | |
| 813 | int ui_get_showing_back_button() { |
| 814 | return gShowBackButton; |
| 815 | } |