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