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 | |
| 31 | #include "common.h" |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 32 | #include "device.h" |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 33 | #include "minui/minui.h" |
| 34 | #include "screen_ui.h" |
| 35 | #include "ui.h" |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 36 | |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 37 | static int char_width; |
| 38 | static int char_height; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 39 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 40 | // There's only (at most) one of these objects, and global callbacks |
| 41 | // (for pthread_create, and the input event system) need to find it, |
| 42 | // so use a global variable. |
| 43 | static ScreenRecoveryUI* self = NULL; |
| 44 | |
| 45 | // Return the current time as a double (including fractions of a second). |
| 46 | static double now() { |
| 47 | struct timeval tv; |
| 48 | gettimeofday(&tv, NULL); |
| 49 | return tv.tv_sec + tv.tv_usec / 1000000.0; |
| 50 | } |
| 51 | |
| 52 | ScreenRecoveryUI::ScreenRecoveryUI() : |
| 53 | currentIcon(NONE), |
| 54 | installingFrame(0), |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 55 | locale(NULL), |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 56 | rtl_locale(false), |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 57 | progressBarType(EMPTY), |
| 58 | progressScopeStart(0), |
| 59 | progressScopeSize(0), |
| 60 | progress(0), |
| 61 | pagesIdentical(false), |
| 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), |
| 69 | show_menu(false), |
| 70 | menu_top(0), |
| 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 | |
| 78 | for (int i = 0; i < 5; i++) |
| 79 | backgroundIcon[i] = NULL; |
| 80 | |
Bjorn Andersson | 80a7a46 | 2013-08-30 16:59:06 -0700 | [diff] [blame] | 81 | memset(text, 0, sizeof(text)); |
| 82 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 83 | pthread_mutex_init(&updateMutex, NULL); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 84 | self = this; |
| 85 | } |
| 86 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 87 | // Clear the screen and draw the currently selected background icon (if any). |
| 88 | // Should only be called with updateMutex locked. |
| 89 | void ScreenRecoveryUI::draw_background_locked(Icon icon) |
| 90 | { |
| 91 | pagesIdentical = false; |
Doug Zongker | 5b5f6c2 | 2014-06-03 10:50:13 -0700 | [diff] [blame] | 92 | gr_color(0, 0, 0, 255); |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 93 | gr_clear(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 94 | |
| 95 | if (icon) { |
| 96 | gr_surface surface = backgroundIcon[icon]; |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 97 | if (icon == INSTALLING_UPDATE || icon == ERASING) { |
| 98 | surface = installation[installingFrame]; |
| 99 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 100 | gr_surface text_surface = backgroundText[icon]; |
| 101 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 102 | int iconWidth = gr_get_width(surface); |
| 103 | int iconHeight = gr_get_height(surface); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 104 | int textWidth = gr_get_width(text_surface); |
| 105 | int textHeight = gr_get_height(text_surface); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 106 | int stageHeight = gr_get_height(stageMarkerEmpty); |
| 107 | |
| 108 | int sh = (max_stage >= 0) ? stageHeight : 0; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 109 | |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 110 | iconX = (gr_fb_width() - iconWidth) / 2; |
| 111 | iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 112 | |
| 113 | int textX = (gr_fb_width() - textWidth) / 2; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 114 | int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 115 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 116 | gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 117 | if (stageHeight > 0) { |
| 118 | int sw = gr_get_width(stageMarkerEmpty); |
| 119 | int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2; |
| 120 | int y = iconY + iconHeight + 20; |
| 121 | for (int i = 0; i < max_stage; ++i) { |
| 122 | gr_blit((i < stage) ? stageMarkerFill : stageMarkerEmpty, |
| 123 | 0, 0, sw, stageHeight, x, y); |
| 124 | x += sw; |
| 125 | } |
| 126 | } |
| 127 | |
Doug Zongker | 5b5f6c2 | 2014-06-03 10:50:13 -0700 | [diff] [blame] | 128 | gr_color(255, 255, 255, 255); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 129 | gr_texticon(textX, textY, text_surface); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | // Draw the progress bar (if any) on the screen. Does not flip pages. |
| 134 | // Should only be called with updateMutex locked. |
| 135 | void ScreenRecoveryUI::draw_progress_locked() |
| 136 | { |
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; |
| 190 | case MENU_SEL_FG: |
| 191 | gr_color(255, 255, 255, 255); |
| 192 | break; |
| 193 | case LOG: |
Doug Zongker | 5b5f6c2 | 2014-06-03 10:50:13 -0700 | [diff] [blame] | 194 | gr_color(249, 194, 0, 255); |
| 195 | break; |
| 196 | case TEXT_FILL: |
| 197 | gr_color(0, 0, 0, 160); |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 198 | break; |
| 199 | default: |
| 200 | gr_color(255, 255, 255, 255); |
| 201 | break; |
| 202 | } |
| 203 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 204 | |
| 205 | // Redraw everything on the screen. Does not flip pages. |
| 206 | // Should only be called with updateMutex locked. |
| 207 | void ScreenRecoveryUI::draw_screen_locked() |
| 208 | { |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 209 | if (!show_text) { |
| 210 | draw_background_locked(currentIcon); |
| 211 | draw_progress_locked(); |
| 212 | } else { |
Doug Zongker | 5b5f6c2 | 2014-06-03 10:50:13 -0700 | [diff] [blame] | 213 | gr_color(0, 0, 0, 255); |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 214 | gr_clear(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 215 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 216 | int y = 0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 217 | int i = 0; |
| 218 | if (show_menu) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 219 | SetColor(HEADER); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 220 | |
| 221 | for (; i < menu_top + menu_items; ++i) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 222 | if (i == menu_top) SetColor(MENU); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 223 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 224 | if (i == menu_top + menu_sel) { |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 225 | // draw the highlight bar |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 226 | SetColor(MENU_SEL_BG); |
Doug Zongker | 5b5f6c2 | 2014-06-03 10:50:13 -0700 | [diff] [blame] | 227 | gr_fill(0, y-2, gr_fb_width(), y+char_height+2); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 228 | // white text of selected item |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 229 | SetColor(MENU_SEL_FG); |
Doug Zongker | 5b5f6c2 | 2014-06-03 10:50:13 -0700 | [diff] [blame] | 230 | if (menu[i][0]) gr_text(4, y, menu[i], 1); |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 231 | SetColor(MENU); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 232 | } else { |
Doug Zongker | 5b5f6c2 | 2014-06-03 10:50:13 -0700 | [diff] [blame] | 233 | 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] | 234 | } |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 235 | y += char_height+4; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 236 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 237 | SetColor(MENU); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 238 | y += 4; |
| 239 | gr_fill(0, y, gr_fb_width(), y+2); |
| 240 | y += 4; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 241 | ++i; |
| 242 | } |
| 243 | |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 244 | SetColor(LOG); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 245 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 246 | // display from the bottom up, until we hit the top of the |
| 247 | // screen, the bottom of the menu, or we've displayed the |
| 248 | // entire text buffer. |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 249 | int row = (text_top+text_rows-1) % text_rows; |
| 250 | for (int ty = gr_fb_height() - char_height, count = 0; |
| 251 | ty > y+2 && count < text_rows; |
| 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. |
| 262 | void ScreenRecoveryUI::update_screen_locked() |
| 263 | { |
| 264 | draw_screen_locked(); |
| 265 | gr_flip(); |
| 266 | } |
| 267 | |
| 268 | // Updates only the progress bar, if possible, otherwise redraws the screen. |
| 269 | // Should only be called with updateMutex locked. |
| 270 | void ScreenRecoveryUI::update_progress_locked() |
| 271 | { |
| 272 | if (show_text || !pagesIdentical) { |
| 273 | draw_screen_locked(); // Must redraw the whole screen |
| 274 | pagesIdentical = true; |
| 275 | } else { |
| 276 | draw_progress_locked(); // Draw only the progress bar and overlays |
| 277 | } |
| 278 | gr_flip(); |
| 279 | } |
| 280 | |
| 281 | // Keeps the progress bar updated, even when the process is otherwise busy. |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 282 | void* ScreenRecoveryUI::progress_thread(void *cookie) { |
| 283 | self->progress_loop(); |
| 284 | return NULL; |
| 285 | } |
| 286 | |
| 287 | void ScreenRecoveryUI::progress_loop() { |
| 288 | double interval = 1.0 / animation_fps; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 289 | for (;;) { |
| 290 | double start = now(); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 291 | pthread_mutex_lock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 292 | |
| 293 | int redraw = 0; |
| 294 | |
| 295 | // update the installation animation, if active |
| 296 | // skip this if we have a text overlay (too expensive to update) |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 297 | if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && |
| 298 | installing_frames > 0 && !show_text) { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 299 | installingFrame = (installingFrame + 1) % installing_frames; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 300 | redraw = 1; |
| 301 | } |
| 302 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 303 | // move the progress bar forward on timed intervals, if configured |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 304 | int duration = progressScopeDuration; |
| 305 | if (progressBarType == DETERMINATE && duration > 0) { |
| 306 | double elapsed = now() - progressScopeTime; |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 307 | float p = 1.0 * elapsed / duration; |
| 308 | if (p > 1.0) p = 1.0; |
| 309 | if (p > progress) { |
| 310 | progress = p; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 311 | redraw = 1; |
| 312 | } |
| 313 | } |
| 314 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 315 | if (redraw) update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 316 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 317 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 318 | double end = now(); |
| 319 | // minimum of 20ms delay between frames |
| 320 | double delay = interval - (end-start); |
| 321 | if (delay < 0.02) delay = 0.02; |
| 322 | usleep((long)(delay * 1000000)); |
| 323 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) { |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 327 | int result = res_create_display_surface(filename, surface); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 328 | if (result < 0) { |
| 329 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 330 | } |
| 331 | } |
| 332 | |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 333 | void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) { |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 334 | int result = res_create_multi_display_surface(filename, frames, surface); |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 335 | if (result < 0) { |
| 336 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 337 | } |
| 338 | } |
| 339 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 340 | void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) { |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 341 | int result = res_create_localized_alpha_surface(filename, locale, surface); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 342 | if (result < 0) { |
| 343 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 344 | } |
| 345 | } |
| 346 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 347 | void ScreenRecoveryUI::Init() |
| 348 | { |
| 349 | gr_init(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 350 | |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 351 | gr_font_size(&char_width, &char_height); |
| 352 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 353 | text_col = text_row = 0; |
Doug Zongker | 5b5f6c2 | 2014-06-03 10:50:13 -0700 | [diff] [blame] | 354 | text_rows = gr_fb_height() / char_height; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 355 | if (text_rows > kMaxRows) text_rows = kMaxRows; |
| 356 | text_top = 1; |
| 357 | |
Doug Zongker | 5b5f6c2 | 2014-06-03 10:50:13 -0700 | [diff] [blame] | 358 | text_cols = gr_fb_width() / char_width; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 359 | if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1; |
| 360 | |
Alistair Strachan | 9b8ae80 | 2013-07-17 10:34:36 -0700 | [diff] [blame] | 361 | backgroundIcon[NONE] = NULL; |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 362 | LoadBitmapArray("icon_installing", &installing_frames, &installation); |
| 363 | backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : NULL; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 364 | backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE]; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 365 | LoadBitmap("icon_error", &backgroundIcon[ERROR]); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 366 | backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; |
| 367 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 368 | LoadBitmap("progress_empty", &progressBarEmpty); |
| 369 | LoadBitmap("progress_fill", &progressBarFill); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 370 | LoadBitmap("stage_empty", &stageMarkerEmpty); |
| 371 | LoadBitmap("stage_fill", &stageMarkerFill); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 372 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 373 | LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]); |
| 374 | LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]); |
| 375 | LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]); |
| 376 | LoadLocalizedBitmap("error_text", &backgroundText[ERROR]); |
| 377 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 378 | pthread_create(&progress_t, NULL, progress_thread, NULL); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 379 | |
| 380 | RecoveryUI::Init(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 383 | void ScreenRecoveryUI::SetLocale(const char* new_locale) { |
| 384 | if (new_locale) { |
| 385 | this->locale = new_locale; |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 386 | char* lang = strdup(locale); |
| 387 | for (char* p = lang; *p; ++p) { |
| 388 | if (*p == '_') { |
| 389 | *p = '\0'; |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // A bit cheesy: keep an explicit list of supported languages |
| 395 | // that are RTL. |
| 396 | if (strcmp(lang, "ar") == 0 || // Arabic |
| 397 | strcmp(lang, "fa") == 0 || // Persian (Farsi) |
| 398 | strcmp(lang, "he") == 0 || // Hebrew (new language code) |
Doug Zongker | b66cb69 | 2012-09-18 14:52:18 -0700 | [diff] [blame] | 399 | strcmp(lang, "iw") == 0 || // Hebrew (old language code) |
| 400 | strcmp(lang, "ur") == 0) { // Urdu |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 401 | rtl_locale = true; |
| 402 | } |
| 403 | free(lang); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 404 | } else { |
| 405 | new_locale = NULL; |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 409 | void ScreenRecoveryUI::SetBackground(Icon icon) |
| 410 | { |
| 411 | pthread_mutex_lock(&updateMutex); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 412 | |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 413 | currentIcon = icon; |
| 414 | update_screen_locked(); |
| 415 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 416 | pthread_mutex_unlock(&updateMutex); |
| 417 | } |
| 418 | |
| 419 | void ScreenRecoveryUI::SetProgressType(ProgressType type) |
| 420 | { |
| 421 | pthread_mutex_lock(&updateMutex); |
| 422 | if (progressBarType != type) { |
| 423 | progressBarType = type; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 424 | } |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 425 | progressScopeStart = 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 426 | progressScopeSize = 0; |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 427 | progress = 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 428 | update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 429 | pthread_mutex_unlock(&updateMutex); |
| 430 | } |
| 431 | |
| 432 | void ScreenRecoveryUI::ShowProgress(float portion, float seconds) |
| 433 | { |
| 434 | pthread_mutex_lock(&updateMutex); |
| 435 | progressBarType = DETERMINATE; |
| 436 | progressScopeStart += progressScopeSize; |
| 437 | progressScopeSize = portion; |
| 438 | progressScopeTime = now(); |
| 439 | progressScopeDuration = seconds; |
| 440 | progress = 0; |
| 441 | update_progress_locked(); |
| 442 | pthread_mutex_unlock(&updateMutex); |
| 443 | } |
| 444 | |
| 445 | void ScreenRecoveryUI::SetProgress(float fraction) |
| 446 | { |
| 447 | pthread_mutex_lock(&updateMutex); |
| 448 | if (fraction < 0.0) fraction = 0.0; |
| 449 | if (fraction > 1.0) fraction = 1.0; |
| 450 | if (progressBarType == DETERMINATE && fraction > progress) { |
| 451 | // Skip updates that aren't visibly different. |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 452 | int width = gr_get_width(progressBarEmpty); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 453 | float scale = width * progressScopeSize; |
| 454 | if ((int) (progress * scale) != (int) (fraction * scale)) { |
| 455 | progress = fraction; |
| 456 | update_progress_locked(); |
| 457 | } |
| 458 | } |
| 459 | pthread_mutex_unlock(&updateMutex); |
| 460 | } |
| 461 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 462 | void ScreenRecoveryUI::SetStage(int current, int max) { |
| 463 | pthread_mutex_lock(&updateMutex); |
| 464 | stage = current; |
| 465 | max_stage = max; |
| 466 | pthread_mutex_unlock(&updateMutex); |
| 467 | } |
| 468 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 469 | void ScreenRecoveryUI::Print(const char *fmt, ...) |
| 470 | { |
| 471 | char buf[256]; |
| 472 | va_list ap; |
| 473 | va_start(ap, fmt); |
| 474 | vsnprintf(buf, 256, fmt, ap); |
| 475 | va_end(ap); |
| 476 | |
| 477 | fputs(buf, stdout); |
| 478 | |
| 479 | // This can get called before ui_init(), so be careful. |
| 480 | pthread_mutex_lock(&updateMutex); |
| 481 | if (text_rows > 0 && text_cols > 0) { |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 482 | for (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 | |
| 497 | void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items, |
| 498 | int initial_selection) { |
| 499 | int i; |
| 500 | pthread_mutex_lock(&updateMutex); |
| 501 | if (text_rows > 0 && text_cols > 0) { |
| 502 | for (i = 0; i < text_rows; ++i) { |
| 503 | if (headers[i] == NULL) break; |
| 504 | strncpy(menu[i], headers[i], text_cols-1); |
| 505 | menu[i][text_cols-1] = '\0'; |
| 506 | } |
| 507 | menu_top = i; |
| 508 | for (; i < text_rows; ++i) { |
| 509 | if (items[i-menu_top] == NULL) break; |
| 510 | strncpy(menu[i], items[i-menu_top], text_cols-1); |
| 511 | menu[i][text_cols-1] = '\0'; |
| 512 | } |
| 513 | menu_items = i - menu_top; |
| 514 | show_menu = 1; |
| 515 | menu_sel = initial_selection; |
| 516 | update_screen_locked(); |
| 517 | } |
| 518 | pthread_mutex_unlock(&updateMutex); |
| 519 | } |
| 520 | |
| 521 | int ScreenRecoveryUI::SelectMenu(int sel) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 522 | pthread_mutex_lock(&updateMutex); |
| 523 | if (show_menu > 0) { |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 524 | int old_sel = menu_sel; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 525 | menu_sel = sel; |
Elliott Hughes | fc06f87 | 2015-03-23 13:45:31 -0700 | [diff] [blame] | 526 | |
| 527 | // Wrap at top and bottom. |
| 528 | if (menu_sel < 0) menu_sel = menu_items - 1; |
| 529 | if (menu_sel >= menu_items) menu_sel = 0; |
| 530 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 531 | sel = menu_sel; |
| 532 | if (menu_sel != old_sel) update_screen_locked(); |
| 533 | } |
| 534 | pthread_mutex_unlock(&updateMutex); |
| 535 | return sel; |
| 536 | } |
| 537 | |
| 538 | void ScreenRecoveryUI::EndMenu() { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 539 | pthread_mutex_lock(&updateMutex); |
| 540 | if (show_menu > 0 && text_rows > 0 && text_cols > 0) { |
| 541 | show_menu = 0; |
| 542 | update_screen_locked(); |
| 543 | } |
| 544 | pthread_mutex_unlock(&updateMutex); |
| 545 | } |
| 546 | |
| 547 | bool ScreenRecoveryUI::IsTextVisible() |
| 548 | { |
| 549 | pthread_mutex_lock(&updateMutex); |
| 550 | int visible = show_text; |
| 551 | pthread_mutex_unlock(&updateMutex); |
| 552 | return visible; |
| 553 | } |
| 554 | |
| 555 | bool ScreenRecoveryUI::WasTextEverVisible() |
| 556 | { |
| 557 | pthread_mutex_lock(&updateMutex); |
| 558 | int ever_visible = show_text_ever; |
| 559 | pthread_mutex_unlock(&updateMutex); |
| 560 | return ever_visible; |
| 561 | } |
| 562 | |
| 563 | void ScreenRecoveryUI::ShowText(bool visible) |
| 564 | { |
| 565 | pthread_mutex_lock(&updateMutex); |
| 566 | show_text = visible; |
| 567 | if (show_text) show_text_ever = 1; |
| 568 | update_screen_locked(); |
| 569 | pthread_mutex_unlock(&updateMutex); |
| 570 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 571 | |
| 572 | void ScreenRecoveryUI::Redraw() |
| 573 | { |
| 574 | pthread_mutex_lock(&updateMutex); |
| 575 | update_screen_locked(); |
| 576 | pthread_mutex_unlock(&updateMutex); |
| 577 | } |