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