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