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