Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 <errno.h> |
| 18 | #include <fcntl.h> |
| 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> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <time.h> |
| 29 | #include <unistd.h> |
| 30 | |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 33 | #include "base/strings.h" |
| 34 | #include "cutils/properties.h" |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 35 | #include "common.h" |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 36 | #include "device.h" |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 37 | #include "minui/minui.h" |
| 38 | #include "screen_ui.h" |
| 39 | #include "ui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | extern "C" { |
| 41 | #include "minuitwrp/minui.h" |
| 42 | int twgr_text(int x, int y, const char *s); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 43 | #include "gui/gui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 44 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 45 | #include "data.hpp" |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 46 | |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 47 | static int char_width; |
| 48 | static int char_height; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 49 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 50 | // Return the current time as a double (including fractions of a second). |
| 51 | static double now() { |
| 52 | struct timeval tv; |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 53 | gettimeofday(&tv, nullptr); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 54 | return tv.tv_sec + tv.tv_usec / 1000000.0; |
| 55 | } |
| 56 | |
| 57 | ScreenRecoveryUI::ScreenRecoveryUI() : |
| 58 | currentIcon(NONE), |
| 59 | installingFrame(0), |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 60 | locale(nullptr), |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 61 | rtl_locale(false), |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 62 | progressBarType(EMPTY), |
| 63 | progressScopeStart(0), |
| 64 | progressScopeSize(0), |
| 65 | progress(0), |
| 66 | pagesIdentical(false), |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 67 | text_cols_(0), |
| 68 | text_rows_(0), |
| 69 | text_(nullptr), |
| 70 | text_col_(0), |
| 71 | text_row_(0), |
| 72 | text_top_(0), |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 73 | show_text(false), |
| 74 | show_text_ever(false), |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 75 | menu_(nullptr), |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 76 | show_menu(false), |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 77 | menu_items(0), |
| 78 | menu_sel(0), |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 79 | file_viewer_text_(nullptr), |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 80 | animation_fps(20), |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 81 | installing_frames(-1), |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 82 | stage(-1), |
| 83 | max_stage(-1) { |
yetta_wu | 2f6877a | 2013-06-25 15:03:11 +0800 | [diff] [blame] | 84 | |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 85 | for (int i = 0; i < 5; i++) { |
| 86 | backgroundIcon[i] = nullptr; |
| 87 | } |
| 88 | pthread_mutex_init(&updateMutex, nullptr); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 91 | // Clear the screen and draw the currently selected background icon (if any). |
| 92 | // Should only be called with updateMutex locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 93 | void ScreenRecoveryUI::draw_background_locked(Icon icon) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 94 | pagesIdentical = false; |
| 95 | gr_color(0, 0, 0, 255); |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 96 | gr_clear(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 97 | |
| 98 | if (icon) { |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 99 | GRSurface* surface = backgroundIcon[icon]; |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 100 | if (icon == INSTALLING_UPDATE || icon == ERASING) { |
| 101 | surface = installation[installingFrame]; |
| 102 | } |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 103 | GRSurface* text_surface = backgroundText[icon]; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 104 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 105 | int iconWidth = gr_get_width(surface); |
| 106 | int iconHeight = gr_get_height(surface); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 107 | int textWidth = gr_get_width(text_surface); |
| 108 | int textHeight = gr_get_height(text_surface); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 109 | int stageHeight = gr_get_height(stageMarkerEmpty); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 110 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 111 | int sh = (max_stage >= 0) ? stageHeight : 0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 112 | |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 113 | iconX = (gr_fb_width() - iconWidth) / 2; |
| 114 | iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 115 | |
| 116 | int textX = (gr_fb_width() - textWidth) / 2; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 117 | int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 118 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 119 | gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 120 | if (stageHeight > 0) { |
| 121 | int sw = gr_get_width(stageMarkerEmpty); |
| 122 | int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2; |
| 123 | int y = iconY + iconHeight + 20; |
| 124 | for (int i = 0; i < max_stage; ++i) { |
| 125 | gr_blit((i < stage) ? stageMarkerFill : stageMarkerEmpty, |
| 126 | 0, 0, sw, stageHeight, x, y); |
| 127 | x += sw; |
| 128 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 129 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 130 | |
| 131 | gr_color(255, 255, 255, 255); |
| 132 | gr_texticon(textX, textY, text_surface); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | // Draw the progress bar (if any) on the screen. Does not flip pages. |
| 137 | // Should only be called with updateMutex locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 138 | void ScreenRecoveryUI::draw_progress_locked() { |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 139 | if (currentIcon == ERROR) return; |
| 140 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 141 | if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) { |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 142 | GRSurface* icon = installation[installingFrame]; |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 143 | gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | if (progressBarType != EMPTY) { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 147 | int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 148 | int width = gr_get_width(progressBarEmpty); |
| 149 | int height = gr_get_height(progressBarEmpty); |
| 150 | |
| 151 | int dx = (gr_fb_width() - width)/2; |
| 152 | int dy = (3*gr_fb_height() + iconHeight - 2*height)/4; |
| 153 | |
| 154 | // Erase behind the progress bar (in case this was a progress-only update) |
| 155 | gr_color(0, 0, 0, 255); |
| 156 | gr_fill(dx, dy, width, height); |
| 157 | |
| 158 | if (progressBarType == DETERMINATE) { |
| 159 | float p = progressScopeStart + progress * progressScopeSize; |
| 160 | int pos = (int) (p * width); |
| 161 | |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 162 | if (rtl_locale) { |
| 163 | // Fill the progress bar from right to left. |
| 164 | if (pos > 0) { |
| 165 | gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy); |
| 166 | } |
| 167 | if (pos < width-1) { |
| 168 | gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy); |
| 169 | } |
| 170 | } else { |
| 171 | // Fill the progress bar from left to right. |
| 172 | if (pos > 0) { |
| 173 | gr_blit(progressBarFill, 0, 0, pos, height, dx, dy); |
| 174 | } |
| 175 | if (pos < width-1) { |
| 176 | gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy); |
| 177 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 183 | void ScreenRecoveryUI::SetColor(UIElement e) { |
| 184 | switch (e) { |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 185 | case INFO: |
| 186 | gr_color(249, 194, 0, 255); |
| 187 | break; |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 188 | case HEADER: |
| 189 | gr_color(247, 0, 6, 255); |
| 190 | break; |
| 191 | case MENU: |
| 192 | case MENU_SEL_BG: |
| 193 | gr_color(0, 106, 157, 255); |
| 194 | break; |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 195 | case MENU_SEL_BG_ACTIVE: |
| 196 | gr_color(0, 156, 100, 255); |
| 197 | break; |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 198 | case MENU_SEL_FG: |
| 199 | gr_color(255, 255, 255, 255); |
| 200 | break; |
| 201 | case LOG: |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 202 | gr_color(196, 196, 196, 255); |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 203 | break; |
| 204 | case TEXT_FILL: |
| 205 | gr_color(0, 0, 0, 160); |
| 206 | break; |
| 207 | default: |
| 208 | gr_color(255, 255, 255, 255); |
| 209 | break; |
| 210 | } |
| 211 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 212 | |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 213 | void ScreenRecoveryUI::DrawHorizontalRule(int* y) { |
| 214 | SetColor(MENU); |
| 215 | *y += 4; |
| 216 | gr_fill(0, *y, gr_fb_width(), *y + 2); |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 217 | *y += 4; |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 220 | void ScreenRecoveryUI::DrawTextLine(int* y, const char* line, bool bold) { |
| 221 | gr_text(4, *y, line, bold); |
| 222 | *y += char_height + 4; |
| 223 | } |
| 224 | |
| 225 | void ScreenRecoveryUI::DrawTextLines(int* y, const char* const* lines) { |
| 226 | for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) { |
| 227 | DrawTextLine(y, lines[i], false); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | static const char* REGULAR_HELP[] = { |
| 232 | "Use volume up/down and power.", |
| 233 | NULL |
| 234 | }; |
| 235 | |
| 236 | static const char* LONG_PRESS_HELP[] = { |
| 237 | "Any button cycles highlight.", |
| 238 | "Long-press activates.", |
| 239 | NULL |
| 240 | }; |
| 241 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 242 | // Redraw everything on the screen. Does not flip pages. |
| 243 | // Should only be called with updateMutex locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 244 | void ScreenRecoveryUI::draw_screen_locked() { |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 245 | if (!show_text) { |
| 246 | draw_background_locked(currentIcon); |
| 247 | draw_progress_locked(); |
| 248 | } else { |
Doug Zongker | 5b5f6c2 | 2014-06-03 10:50:13 -0700 | [diff] [blame] | 249 | gr_color(0, 0, 0, 255); |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 250 | gr_clear(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 251 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 252 | int y = 0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 253 | if (show_menu) { |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 254 | char recovery_fingerprint[PROPERTY_VALUE_MAX]; |
| 255 | property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, ""); |
| 256 | |
| 257 | SetColor(INFO); |
| 258 | DrawTextLine(&y, "Android Recovery", true); |
| 259 | for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) { |
| 260 | DrawTextLine(&y, chunk.c_str(), false); |
| 261 | } |
| 262 | DrawTextLines(&y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP); |
| 263 | |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 264 | SetColor(HEADER); |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 265 | DrawTextLines(&y, menu_headers_); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 266 | |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 267 | SetColor(MENU); |
| 268 | DrawHorizontalRule(&y); |
| 269 | y += 4; |
| 270 | for (int i = 0; i < menu_items; ++i) { |
| 271 | if (i == menu_sel) { |
| 272 | // Draw the highlight bar. |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 273 | SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG); |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 274 | gr_fill(0, y - 2, gr_fb_width(), y + char_height + 2); |
| 275 | // Bold white text for the selected item. |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 276 | SetColor(MENU_SEL_FG); |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 277 | gr_text(4, y, menu_[i], true); |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 278 | SetColor(MENU); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 279 | } else { |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 280 | gr_text(4, y, menu_[i], false); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 281 | } |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 282 | y += char_height + 4; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 283 | } |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 284 | DrawHorizontalRule(&y); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 287 | // display from the bottom up, until we hit the top of the |
| 288 | // screen, the bottom of the menu, or we've displayed the |
| 289 | // entire text buffer. |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 290 | SetColor(LOG); |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 291 | int row = (text_top_ + text_rows_ - 1) % text_rows_; |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 292 | size_t count = 0; |
| 293 | for (int ty = gr_fb_height() - char_height; |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 294 | ty >= y && count < text_rows_; |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 295 | ty -= char_height, ++count) { |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 296 | gr_text(0, ty, text_[row], false); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 297 | --row; |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 298 | if (row < 0) row = text_rows_ - 1; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | // Redraw everything on the screen and flip the screen (make it visible). |
| 304 | // Should only be called with updateMutex locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 305 | void ScreenRecoveryUI::update_screen_locked() { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 306 | draw_screen_locked(); |
| 307 | gr_flip(); |
| 308 | } |
| 309 | |
| 310 | // Updates only the progress bar, if possible, otherwise redraws the screen. |
| 311 | // Should only be called with updateMutex locked. |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 312 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 313 | void ScreenRecoveryUI::update_progress_locked() { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 314 | if (show_text || !pagesIdentical) { |
| 315 | draw_screen_locked(); // Must redraw the whole screen |
| 316 | pagesIdentical = true; |
| 317 | } else { |
| 318 | draw_progress_locked(); // Draw only the progress bar and overlays |
| 319 | } |
| 320 | gr_flip(); |
| 321 | } |
| 322 | |
| 323 | // Keeps the progress bar updated, even when the process is otherwise busy. |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 324 | void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) { |
| 325 | reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop(); |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 326 | return nullptr; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 329 | void ScreenRecoveryUI::ProgressThreadLoop() { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 330 | double interval = 1.0 / animation_fps; |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 331 | while (true) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 332 | double start = now(); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 333 | pthread_mutex_lock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 334 | |
| 335 | int redraw = 0; |
| 336 | |
| 337 | // update the installation animation, if active |
| 338 | // skip this if we have a text overlay (too expensive to update) |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 339 | if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && |
| 340 | installing_frames > 0 && !show_text) { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 341 | installingFrame = (installingFrame + 1) % installing_frames; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 342 | redraw = 1; |
| 343 | } |
| 344 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 345 | // move the progress bar forward on timed intervals, if configured |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 346 | int duration = progressScopeDuration; |
| 347 | if (progressBarType == DETERMINATE && duration > 0) { |
| 348 | double elapsed = now() - progressScopeTime; |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 349 | float p = 1.0 * elapsed / duration; |
| 350 | if (p > 1.0) p = 1.0; |
| 351 | if (p > progress) { |
| 352 | progress = p; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 353 | redraw = 1; |
| 354 | } |
| 355 | } |
| 356 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 357 | if (redraw) update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 358 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 359 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 360 | double end = now(); |
| 361 | // minimum of 20ms delay between frames |
| 362 | double delay = interval - (end-start); |
| 363 | if (delay < 0.02) delay = 0.02; |
| 364 | usleep((long)(delay * 1000000)); |
| 365 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 368 | void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) { |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 369 | int result = res_create_display_surface(filename, surface); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 370 | if (result < 0) { |
| 371 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 372 | } |
| 373 | } |
| 374 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 375 | void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, GRSurface*** surface) { |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 376 | int result = res_create_multi_display_surface(filename, frames, surface); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 377 | if (result < 0) { |
| 378 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 379 | } |
| 380 | } |
| 381 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 382 | void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) { |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 383 | int result = res_create_localized_alpha_surface(filename, locale, surface); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 384 | if (result < 0) { |
| 385 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 386 | } |
| 387 | } |
| 388 | |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 389 | static char** Alloc2d(size_t rows, size_t cols) { |
| 390 | char** result = new char*[rows]; |
| 391 | for (size_t i = 0; i < rows; ++i) { |
| 392 | result[i] = new char[cols]; |
| 393 | memset(result[i], 0, cols); |
| 394 | } |
| 395 | return result; |
| 396 | } |
| 397 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 398 | void ScreenRecoveryUI::Init() { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 399 | gr_init(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 400 | |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 401 | gr_font_size(&char_width, &char_height); |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 402 | text_rows_ = gr_fb_height() / char_height; |
| 403 | text_cols_ = gr_fb_width() / char_width; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 404 | |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 405 | text_ = Alloc2d(text_rows_, text_cols_ + 1); |
| 406 | file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1); |
| 407 | menu_ = Alloc2d(text_rows_, text_cols_ + 1); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 408 | |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 409 | text_col_ = text_row_ = 0; |
| 410 | text_top_ = 1; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 411 | |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 412 | backgroundIcon[NONE] = nullptr; |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 413 | LoadBitmapArray("icon_installing", &installing_frames, &installation); |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 414 | backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : nullptr; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 415 | backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE]; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 416 | LoadBitmap("icon_error", &backgroundIcon[ERROR]); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 417 | backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; |
| 418 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 419 | LoadBitmap("progress_empty", &progressBarEmpty); |
| 420 | LoadBitmap("progress_fill", &progressBarFill); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 421 | LoadBitmap("stage_empty", &stageMarkerEmpty); |
| 422 | LoadBitmap("stage_fill", &stageMarkerFill); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 423 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 424 | LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]); |
| 425 | LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]); |
| 426 | LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]); |
| 427 | LoadLocalizedBitmap("error_text", &backgroundText[ERROR]); |
| 428 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 429 | pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 430 | |
| 431 | RecoveryUI::Init(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 434 | void ScreenRecoveryUI::SetLocale(const char* new_locale) { |
| 435 | if (new_locale) { |
| 436 | this->locale = new_locale; |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 437 | char* lang = strdup(locale); |
| 438 | for (char* p = lang; *p; ++p) { |
| 439 | if (*p == '_') { |
| 440 | *p = '\0'; |
| 441 | break; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // A bit cheesy: keep an explicit list of supported languages |
| 446 | // that are RTL. |
| 447 | if (strcmp(lang, "ar") == 0 || // Arabic |
| 448 | strcmp(lang, "fa") == 0 || // Persian (Farsi) |
| 449 | strcmp(lang, "he") == 0 || // Hebrew (new language code) |
Doug Zongker | b66cb69 | 2012-09-18 14:52:18 -0700 | [diff] [blame] | 450 | strcmp(lang, "iw") == 0 || // Hebrew (old language code) |
| 451 | strcmp(lang, "ur") == 0) { // Urdu |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 452 | rtl_locale = true; |
| 453 | } |
| 454 | free(lang); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 455 | } else { |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 456 | new_locale = nullptr; |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 460 | void ScreenRecoveryUI::SetBackground(Icon icon) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 461 | pthread_mutex_lock(&updateMutex); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 462 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 463 | currentIcon = icon; |
| 464 | update_screen_locked(); |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 465 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 466 | pthread_mutex_unlock(&updateMutex); |
| 467 | } |
| 468 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 469 | void ScreenRecoveryUI::SetProgressType(ProgressType type) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 470 | pthread_mutex_lock(&updateMutex); |
| 471 | if (progressBarType != type) { |
| 472 | progressBarType = type; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 473 | } |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 474 | progressScopeStart = 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 475 | progressScopeSize = 0; |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 476 | progress = 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 477 | update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 478 | pthread_mutex_unlock(&updateMutex); |
| 479 | } |
| 480 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 481 | void ScreenRecoveryUI::ShowProgress(float portion, float seconds) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 482 | pthread_mutex_lock(&updateMutex); |
| 483 | progressBarType = DETERMINATE; |
| 484 | progressScopeStart += progressScopeSize; |
| 485 | progressScopeSize = portion; |
| 486 | progressScopeTime = now(); |
| 487 | progressScopeDuration = seconds; |
| 488 | progress = 0; |
| 489 | update_progress_locked(); |
| 490 | pthread_mutex_unlock(&updateMutex); |
| 491 | } |
| 492 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 493 | void ScreenRecoveryUI::SetProgress(float fraction) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 494 | pthread_mutex_lock(&updateMutex); |
| 495 | if (fraction < 0.0) fraction = 0.0; |
| 496 | if (fraction > 1.0) fraction = 1.0; |
| 497 | if (progressBarType == DETERMINATE && fraction > progress) { |
| 498 | // Skip updates that aren't visibly different. |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 499 | int width = gr_get_width(progressBarEmpty); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 500 | float scale = width * progressScopeSize; |
| 501 | if ((int) (progress * scale) != (int) (fraction * scale)) { |
| 502 | progress = fraction; |
| 503 | update_progress_locked(); |
| 504 | } |
| 505 | } |
| 506 | pthread_mutex_unlock(&updateMutex); |
| 507 | } |
| 508 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 509 | void ScreenRecoveryUI::SetStage(int current, int max) { |
| 510 | pthread_mutex_lock(&updateMutex); |
| 511 | stage = current; |
| 512 | max_stage = max; |
| 513 | pthread_mutex_unlock(&updateMutex); |
| 514 | } |
| 515 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 516 | void ScreenRecoveryUI::Print(const char *fmt, ...) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 517 | char buf[256]; |
| 518 | va_list ap; |
| 519 | va_start(ap, fmt); |
| 520 | vsnprintf(buf, 256, fmt, ap); |
| 521 | va_end(ap); |
| 522 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 523 | gui_print("%s", buf); |
| 524 | return; |
| 525 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 526 | fputs(buf, stdout); |
| 527 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 528 | pthread_mutex_lock(&updateMutex); |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 529 | if (text_rows_ > 0 && text_cols_ > 0) { |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 530 | for (const char* ptr = buf; *ptr != '\0'; ++ptr) { |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 531 | if (*ptr == '\n' || text_col_ >= text_cols_) { |
| 532 | text_[text_row_][text_col_] = '\0'; |
| 533 | text_col_ = 0; |
| 534 | text_row_ = (text_row_ + 1) % text_rows_; |
| 535 | if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 536 | } |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 537 | if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 538 | } |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 539 | text_[text_row_][text_col_] = '\0'; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 540 | update_screen_locked(); |
| 541 | } |
| 542 | pthread_mutex_unlock(&updateMutex); |
| 543 | } |
| 544 | |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 545 | void ScreenRecoveryUI::PutChar(char ch) { |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 546 | pthread_mutex_lock(&updateMutex); |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 547 | if (ch != '\n') text_[text_row_][text_col_++] = ch; |
| 548 | if (ch == '\n' || text_col_ >= text_cols_) { |
| 549 | text_col_ = 0; |
| 550 | ++text_row_; |
| 551 | |
| 552 | if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_; |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 553 | } |
| 554 | pthread_mutex_unlock(&updateMutex); |
| 555 | } |
| 556 | |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 557 | void ScreenRecoveryUI::ClearText() { |
| 558 | pthread_mutex_lock(&updateMutex); |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 559 | text_col_ = 0; |
| 560 | text_row_ = 0; |
| 561 | text_top_ = 1; |
| 562 | for (size_t i = 0; i < text_rows_; ++i) { |
| 563 | memset(text_[i], 0, text_cols_ + 1); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 564 | } |
| 565 | pthread_mutex_unlock(&updateMutex); |
| 566 | } |
| 567 | |
| 568 | void ScreenRecoveryUI::ShowFile(FILE* fp) { |
| 569 | std::vector<long> offsets; |
| 570 | offsets.push_back(ftell(fp)); |
| 571 | ClearText(); |
| 572 | |
| 573 | struct stat sb; |
| 574 | fstat(fileno(fp), &sb); |
| 575 | |
| 576 | bool show_prompt = false; |
| 577 | while (true) { |
| 578 | if (show_prompt) { |
| 579 | Print("--(%d%% of %d bytes)--", |
| 580 | static_cast<int>(100 * (double(ftell(fp)) / double(sb.st_size))), |
| 581 | static_cast<int>(sb.st_size)); |
| 582 | Redraw(); |
| 583 | while (show_prompt) { |
| 584 | show_prompt = false; |
| 585 | int key = WaitKey(); |
Elliott Hughes | 300ed08 | 2015-04-13 10:47:59 -0700 | [diff] [blame] | 586 | if (key == KEY_POWER || key == KEY_ENTER) { |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 587 | return; |
| 588 | } else if (key == KEY_UP || key == KEY_VOLUMEUP) { |
| 589 | if (offsets.size() <= 1) { |
| 590 | show_prompt = true; |
| 591 | } else { |
| 592 | offsets.pop_back(); |
| 593 | fseek(fp, offsets.back(), SEEK_SET); |
| 594 | } |
| 595 | } else { |
| 596 | if (feof(fp)) { |
| 597 | return; |
| 598 | } |
| 599 | offsets.push_back(ftell(fp)); |
| 600 | } |
| 601 | } |
| 602 | ClearText(); |
| 603 | } |
| 604 | |
| 605 | int ch = getc(fp); |
| 606 | if (ch == EOF) { |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 607 | while (text_row_ < text_rows_ - 1) PutChar('\n'); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 608 | show_prompt = true; |
| 609 | } else { |
| 610 | PutChar(ch); |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 611 | if (text_col_ == 0 && text_row_ >= text_rows_ - 1) { |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 612 | show_prompt = true; |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 618 | void ScreenRecoveryUI::ShowFile(const char* filename) { |
| 619 | FILE* fp = fopen_path(filename, "re"); |
| 620 | if (fp == nullptr) { |
| 621 | Print(" Unable to open %s: %s\n", filename, strerror(errno)); |
| 622 | return; |
| 623 | } |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 624 | |
| 625 | char** old_text = text_; |
| 626 | size_t old_text_col = text_col_; |
| 627 | size_t old_text_row = text_row_; |
| 628 | size_t old_text_top = text_top_; |
| 629 | |
| 630 | // Swap in the alternate screen and clear it. |
| 631 | text_ = file_viewer_text_; |
| 632 | ClearText(); |
| 633 | |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 634 | ShowFile(fp); |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 635 | fclose(fp); |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 636 | |
| 637 | text_ = old_text; |
| 638 | text_col_ = old_text_col; |
| 639 | text_row_ = old_text_row; |
| 640 | text_top_ = old_text_top; |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 643 | void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items, |
| 644 | int initial_selection) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 645 | pthread_mutex_lock(&updateMutex); |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 646 | if (text_rows_ > 0 && text_cols_ > 0) { |
| 647 | menu_headers_ = headers; |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 648 | size_t i = 0; |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 649 | for (; i < text_rows_ && items[i] != nullptr; ++i) { |
| 650 | strncpy(menu_[i], items[i], text_cols_ - 1); |
| 651 | menu_[i][text_cols_ - 1] = '\0'; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 652 | } |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 653 | menu_items = i; |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 654 | show_menu = true; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 655 | menu_sel = initial_selection; |
| 656 | update_screen_locked(); |
| 657 | } |
| 658 | pthread_mutex_unlock(&updateMutex); |
| 659 | } |
| 660 | |
| 661 | int ScreenRecoveryUI::SelectMenu(int sel) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 662 | pthread_mutex_lock(&updateMutex); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 663 | if (show_menu) { |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 664 | int old_sel = menu_sel; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 665 | menu_sel = sel; |
Elliott Hughes | fc06f87 | 2015-03-23 13:45:31 -0700 | [diff] [blame] | 666 | |
| 667 | // Wrap at top and bottom. |
| 668 | if (menu_sel < 0) menu_sel = menu_items - 1; |
| 669 | if (menu_sel >= menu_items) menu_sel = 0; |
| 670 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 671 | sel = menu_sel; |
| 672 | if (menu_sel != old_sel) update_screen_locked(); |
| 673 | } |
| 674 | pthread_mutex_unlock(&updateMutex); |
| 675 | return sel; |
| 676 | } |
| 677 | |
| 678 | void ScreenRecoveryUI::EndMenu() { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 679 | pthread_mutex_lock(&updateMutex); |
Elliott Hughes | df52e1e | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 680 | if (show_menu && text_rows_ > 0 && text_cols_ > 0) { |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 681 | show_menu = false; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 682 | update_screen_locked(); |
| 683 | } |
| 684 | pthread_mutex_unlock(&updateMutex); |
| 685 | } |
| 686 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 687 | bool ScreenRecoveryUI::IsTextVisible() { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 688 | pthread_mutex_lock(&updateMutex); |
| 689 | int visible = show_text; |
| 690 | pthread_mutex_unlock(&updateMutex); |
| 691 | return visible; |
| 692 | } |
| 693 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 694 | bool ScreenRecoveryUI::WasTextEverVisible() { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 695 | pthread_mutex_lock(&updateMutex); |
| 696 | int ever_visible = show_text_ever; |
| 697 | pthread_mutex_unlock(&updateMutex); |
| 698 | return ever_visible; |
| 699 | } |
| 700 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 701 | void ScreenRecoveryUI::ShowText(bool visible) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 702 | pthread_mutex_lock(&updateMutex); |
| 703 | show_text = visible; |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 704 | if (show_text) show_text_ever = true; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 705 | update_screen_locked(); |
| 706 | pthread_mutex_unlock(&updateMutex); |
| 707 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 708 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 709 | void ScreenRecoveryUI::Redraw() { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 710 | pthread_mutex_lock(&updateMutex); |
| 711 | update_screen_locked(); |
| 712 | pthread_mutex_unlock(&updateMutex); |
| 713 | } |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 714 | |
| 715 | void ScreenRecoveryUI::KeyLongPress(int) { |
| 716 | // Redraw so that if we're in the menu, the highlight |
| 717 | // will change color to indicate a successful long press. |
| 718 | Redraw(); |
| 719 | } |