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