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