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 | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 55 | rtl_locale(false), |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 56 | progressBarType(EMPTY), |
| 57 | progressScopeStart(0), |
| 58 | progressScopeSize(0), |
| 59 | progress(0), |
| 60 | pagesIdentical(false), |
| 61 | text_cols(0), |
| 62 | text_rows(0), |
| 63 | text_col(0), |
| 64 | text_row(0), |
| 65 | text_top(0), |
| 66 | show_text(false), |
| 67 | show_text_ever(false), |
| 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 | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame^] | 73 | installing_frames(-1) { |
yetta_wu | 5b468fc | 2013-06-25 15:03:11 +0800 | [diff] [blame] | 74 | for (int i = 0; i < 5; i++) |
| 75 | backgroundIcon[i] = NULL; |
| 76 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 77 | pthread_mutex_init(&updateMutex, NULL); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 78 | self = this; |
| 79 | } |
| 80 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 81 | // Clear the screen and draw the currently selected background icon (if any). |
| 82 | // Should only be called with updateMutex locked. |
| 83 | void ScreenRecoveryUI::draw_background_locked(Icon icon) |
| 84 | { |
| 85 | pagesIdentical = false; |
| 86 | gr_color(0, 0, 0, 255); |
| 87 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 88 | |
| 89 | if (icon) { |
| 90 | gr_surface surface = backgroundIcon[icon]; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame^] | 91 | if (icon == INSTALLING_UPDATE || icon == ERASING) { |
| 92 | surface = installation[installingFrame]; |
| 93 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 94 | gr_surface text_surface = backgroundText[icon]; |
| 95 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 96 | int iconWidth = gr_get_width(surface); |
| 97 | int iconHeight = gr_get_height(surface); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 98 | int textWidth = gr_get_width(text_surface); |
| 99 | int textHeight = gr_get_height(text_surface); |
| 100 | |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame^] | 101 | iconX = (gr_fb_width() - iconWidth) / 2; |
| 102 | iconY = (gr_fb_height() - (iconHeight+textHeight+40)) / 2; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 103 | |
| 104 | int textX = (gr_fb_width() - textWidth) / 2; |
| 105 | int textY = ((gr_fb_height() - (iconHeight+textHeight+40)) / 2) + iconHeight + 40; |
| 106 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 107 | gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 108 | |
| 109 | gr_color(255, 255, 255, 255); |
| 110 | gr_texticon(textX, textY, text_surface); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | // Draw the progress bar (if any) on the screen. Does not flip pages. |
| 115 | // Should only be called with updateMutex locked. |
| 116 | void ScreenRecoveryUI::draw_progress_locked() |
| 117 | { |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 118 | if (currentIcon == ERROR) return; |
| 119 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 120 | if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) { |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame^] | 121 | gr_surface icon = installation[installingFrame]; |
| 122 | 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] | 123 | } |
| 124 | |
| 125 | if (progressBarType != EMPTY) { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 126 | int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 127 | int width = gr_get_width(progressBarEmpty); |
| 128 | int height = gr_get_height(progressBarEmpty); |
| 129 | |
| 130 | int dx = (gr_fb_width() - width)/2; |
| 131 | int dy = (3*gr_fb_height() + iconHeight - 2*height)/4; |
| 132 | |
| 133 | // Erase behind the progress bar (in case this was a progress-only update) |
| 134 | gr_color(0, 0, 0, 255); |
| 135 | gr_fill(dx, dy, width, height); |
| 136 | |
| 137 | if (progressBarType == DETERMINATE) { |
| 138 | float p = progressScopeStart + progress * progressScopeSize; |
| 139 | int pos = (int) (p * width); |
| 140 | |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 141 | if (rtl_locale) { |
| 142 | // Fill the progress bar from right to left. |
| 143 | if (pos > 0) { |
| 144 | gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy); |
| 145 | } |
| 146 | if (pos < width-1) { |
| 147 | gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy); |
| 148 | } |
| 149 | } else { |
| 150 | // Fill the progress bar from left to right. |
| 151 | if (pos > 0) { |
| 152 | gr_blit(progressBarFill, 0, 0, pos, height, dx, dy); |
| 153 | } |
| 154 | if (pos < width-1) { |
| 155 | gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy); |
| 156 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 162 | void ScreenRecoveryUI::SetColor(UIElement e) { |
| 163 | switch (e) { |
| 164 | case HEADER: |
| 165 | gr_color(247, 0, 6, 255); |
| 166 | break; |
| 167 | case MENU: |
| 168 | case MENU_SEL_BG: |
| 169 | gr_color(0, 106, 157, 255); |
| 170 | break; |
| 171 | case MENU_SEL_FG: |
| 172 | gr_color(255, 255, 255, 255); |
| 173 | break; |
| 174 | case LOG: |
| 175 | gr_color(249, 194, 0, 255); |
| 176 | break; |
| 177 | case TEXT_FILL: |
| 178 | gr_color(0, 0, 0, 160); |
| 179 | break; |
| 180 | default: |
| 181 | gr_color(255, 255, 255, 255); |
| 182 | break; |
| 183 | } |
| 184 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 185 | |
| 186 | // Redraw everything on the screen. Does not flip pages. |
| 187 | // Should only be called with updateMutex locked. |
| 188 | void ScreenRecoveryUI::draw_screen_locked() |
| 189 | { |
| 190 | draw_background_locked(currentIcon); |
| 191 | draw_progress_locked(); |
| 192 | |
| 193 | if (show_text) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 194 | SetColor(TEXT_FILL); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 195 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 196 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 197 | int y = 0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 198 | int i = 0; |
| 199 | if (show_menu) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 200 | SetColor(HEADER); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 201 | |
| 202 | for (; i < menu_top + menu_items; ++i) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 203 | if (i == menu_top) SetColor(MENU); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 204 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 205 | if (i == menu_top + menu_sel) { |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 206 | // draw the highlight bar |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 207 | SetColor(MENU_SEL_BG); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 208 | gr_fill(0, y-2, gr_fb_width(), y+char_height+2); |
| 209 | // white text of selected item |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 210 | SetColor(MENU_SEL_FG); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 211 | if (menu[i][0]) gr_text(4, y, menu[i], 1); |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 212 | SetColor(MENU); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 213 | } else { |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 214 | 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] | 215 | } |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 216 | y += char_height+4; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 217 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 218 | SetColor(MENU); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 219 | y += 4; |
| 220 | gr_fill(0, y, gr_fb_width(), y+2); |
| 221 | y += 4; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 222 | ++i; |
| 223 | } |
| 224 | |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 225 | SetColor(LOG); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 226 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 227 | // display from the bottom up, until we hit the top of the |
| 228 | // screen, the bottom of the menu, or we've displayed the |
| 229 | // entire text buffer. |
| 230 | int ty; |
| 231 | int row = (text_top+text_rows-1) % text_rows; |
| 232 | for (int ty = gr_fb_height() - char_height, count = 0; |
| 233 | ty > y+2 && count < text_rows; |
| 234 | ty -= char_height, ++count) { |
| 235 | gr_text(4, ty, text[row], 0); |
| 236 | --row; |
| 237 | if (row < 0) row = text_rows-1; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // Redraw everything on the screen and flip the screen (make it visible). |
| 243 | // Should only be called with updateMutex locked. |
| 244 | void ScreenRecoveryUI::update_screen_locked() |
| 245 | { |
| 246 | draw_screen_locked(); |
| 247 | gr_flip(); |
| 248 | } |
| 249 | |
| 250 | // Updates only the progress bar, if possible, otherwise redraws the screen. |
| 251 | // Should only be called with updateMutex locked. |
| 252 | void ScreenRecoveryUI::update_progress_locked() |
| 253 | { |
| 254 | if (show_text || !pagesIdentical) { |
| 255 | draw_screen_locked(); // Must redraw the whole screen |
| 256 | pagesIdentical = true; |
| 257 | } else { |
| 258 | draw_progress_locked(); // Draw only the progress bar and overlays |
| 259 | } |
| 260 | gr_flip(); |
| 261 | } |
| 262 | |
| 263 | // Keeps the progress bar updated, even when the process is otherwise busy. |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 264 | void* ScreenRecoveryUI::progress_thread(void *cookie) { |
| 265 | self->progress_loop(); |
| 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | void ScreenRecoveryUI::progress_loop() { |
| 270 | double interval = 1.0 / animation_fps; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 271 | for (;;) { |
| 272 | double start = now(); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 273 | pthread_mutex_lock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 274 | |
| 275 | int redraw = 0; |
| 276 | |
| 277 | // update the installation animation, if active |
| 278 | // skip this if we have a text overlay (too expensive to update) |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 279 | if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && |
| 280 | installing_frames > 0 && !show_text) { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 281 | installingFrame = (installingFrame + 1) % installing_frames; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 282 | redraw = 1; |
| 283 | } |
| 284 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 285 | // move the progress bar forward on timed intervals, if configured |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 286 | int duration = progressScopeDuration; |
| 287 | if (progressBarType == DETERMINATE && duration > 0) { |
| 288 | double elapsed = now() - progressScopeTime; |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 289 | float p = 1.0 * elapsed / duration; |
| 290 | if (p > 1.0) p = 1.0; |
| 291 | if (p > progress) { |
| 292 | progress = p; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 293 | redraw = 1; |
| 294 | } |
| 295 | } |
| 296 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 297 | if (redraw) update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 298 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 299 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 300 | double end = now(); |
| 301 | // minimum of 20ms delay between frames |
| 302 | double delay = interval - (end-start); |
| 303 | if (delay < 0.02) delay = 0.02; |
| 304 | usleep((long)(delay * 1000000)); |
| 305 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) { |
| 309 | int result = res_create_surface(filename, surface); |
| 310 | if (result < 0) { |
| 311 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 312 | } |
| 313 | } |
| 314 | |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame^] | 315 | void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) { |
| 316 | int result = res_create_multi_surface(filename, frames, surface); |
| 317 | if (result < 0) { |
| 318 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 319 | } |
| 320 | } |
| 321 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 322 | void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) { |
| 323 | int result = res_create_localized_surface(filename, surface); |
| 324 | if (result < 0) { |
| 325 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 326 | } |
| 327 | } |
| 328 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 329 | void ScreenRecoveryUI::Init() |
| 330 | { |
| 331 | gr_init(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 332 | |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 333 | gr_font_size(&char_width, &char_height); |
| 334 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 335 | text_col = text_row = 0; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 336 | text_rows = gr_fb_height() / char_height; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 337 | if (text_rows > kMaxRows) text_rows = kMaxRows; |
| 338 | text_top = 1; |
| 339 | |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 340 | text_cols = gr_fb_width() / char_width; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 341 | if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1; |
| 342 | |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame^] | 343 | backgroundIcon[NONE] = NULL; |
| 344 | LoadBitmapArray("icon_installing", &installing_frames, &installation); |
| 345 | backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : NULL; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 346 | backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE]; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 347 | LoadBitmap("icon_error", &backgroundIcon[ERROR]); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 348 | backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; |
| 349 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 350 | LoadBitmap("progress_empty", &progressBarEmpty); |
| 351 | LoadBitmap("progress_fill", &progressBarFill); |
| 352 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 353 | LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]); |
| 354 | LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]); |
| 355 | LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]); |
| 356 | LoadLocalizedBitmap("error_text", &backgroundText[ERROR]); |
| 357 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 358 | pthread_create(&progress_t, NULL, progress_thread, NULL); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 359 | |
| 360 | RecoveryUI::Init(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 363 | void ScreenRecoveryUI::SetLocale(const char* locale) { |
| 364 | if (locale) { |
| 365 | char* lang = strdup(locale); |
| 366 | for (char* p = lang; *p; ++p) { |
| 367 | if (*p == '_') { |
| 368 | *p = '\0'; |
| 369 | break; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | // A bit cheesy: keep an explicit list of supported languages |
| 374 | // that are RTL. |
| 375 | if (strcmp(lang, "ar") == 0 || // Arabic |
| 376 | strcmp(lang, "fa") == 0 || // Persian (Farsi) |
| 377 | strcmp(lang, "he") == 0 || // Hebrew (new language code) |
Doug Zongker | b66cb69 | 2012-09-18 14:52:18 -0700 | [diff] [blame] | 378 | strcmp(lang, "iw") == 0 || // Hebrew (old language code) |
| 379 | strcmp(lang, "ur") == 0) { // Urdu |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 380 | rtl_locale = true; |
| 381 | } |
| 382 | free(lang); |
| 383 | } |
| 384 | } |
| 385 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 386 | void ScreenRecoveryUI::SetBackground(Icon icon) |
| 387 | { |
| 388 | pthread_mutex_lock(&updateMutex); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 389 | |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 390 | currentIcon = icon; |
| 391 | update_screen_locked(); |
| 392 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 393 | pthread_mutex_unlock(&updateMutex); |
| 394 | } |
| 395 | |
| 396 | void ScreenRecoveryUI::SetProgressType(ProgressType type) |
| 397 | { |
| 398 | pthread_mutex_lock(&updateMutex); |
| 399 | if (progressBarType != type) { |
| 400 | progressBarType = type; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 401 | } |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 402 | progressScopeStart = 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 403 | progressScopeSize = 0; |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 404 | progress = 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 405 | update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 406 | pthread_mutex_unlock(&updateMutex); |
| 407 | } |
| 408 | |
| 409 | void ScreenRecoveryUI::ShowProgress(float portion, float seconds) |
| 410 | { |
| 411 | pthread_mutex_lock(&updateMutex); |
| 412 | progressBarType = DETERMINATE; |
| 413 | progressScopeStart += progressScopeSize; |
| 414 | progressScopeSize = portion; |
| 415 | progressScopeTime = now(); |
| 416 | progressScopeDuration = seconds; |
| 417 | progress = 0; |
| 418 | update_progress_locked(); |
| 419 | pthread_mutex_unlock(&updateMutex); |
| 420 | } |
| 421 | |
| 422 | void ScreenRecoveryUI::SetProgress(float fraction) |
| 423 | { |
| 424 | pthread_mutex_lock(&updateMutex); |
| 425 | if (fraction < 0.0) fraction = 0.0; |
| 426 | if (fraction > 1.0) fraction = 1.0; |
| 427 | if (progressBarType == DETERMINATE && fraction > progress) { |
| 428 | // Skip updates that aren't visibly different. |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame^] | 429 | int width = gr_get_width(progressBarEmpty); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 430 | float scale = width * progressScopeSize; |
| 431 | if ((int) (progress * scale) != (int) (fraction * scale)) { |
| 432 | progress = fraction; |
| 433 | update_progress_locked(); |
| 434 | } |
| 435 | } |
| 436 | pthread_mutex_unlock(&updateMutex); |
| 437 | } |
| 438 | |
| 439 | void ScreenRecoveryUI::Print(const char *fmt, ...) |
| 440 | { |
| 441 | char buf[256]; |
| 442 | va_list ap; |
| 443 | va_start(ap, fmt); |
| 444 | vsnprintf(buf, 256, fmt, ap); |
| 445 | va_end(ap); |
| 446 | |
| 447 | fputs(buf, stdout); |
| 448 | |
| 449 | // This can get called before ui_init(), so be careful. |
| 450 | pthread_mutex_lock(&updateMutex); |
| 451 | if (text_rows > 0 && text_cols > 0) { |
| 452 | char *ptr; |
| 453 | for (ptr = buf; *ptr != '\0'; ++ptr) { |
| 454 | if (*ptr == '\n' || text_col >= text_cols) { |
| 455 | text[text_row][text_col] = '\0'; |
| 456 | text_col = 0; |
| 457 | text_row = (text_row + 1) % text_rows; |
| 458 | if (text_row == text_top) text_top = (text_top + 1) % text_rows; |
| 459 | } |
| 460 | if (*ptr != '\n') text[text_row][text_col++] = *ptr; |
| 461 | } |
| 462 | text[text_row][text_col] = '\0'; |
| 463 | update_screen_locked(); |
| 464 | } |
| 465 | pthread_mutex_unlock(&updateMutex); |
| 466 | } |
| 467 | |
| 468 | void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items, |
| 469 | int initial_selection) { |
| 470 | int i; |
| 471 | pthread_mutex_lock(&updateMutex); |
| 472 | if (text_rows > 0 && text_cols > 0) { |
| 473 | for (i = 0; i < text_rows; ++i) { |
| 474 | if (headers[i] == NULL) break; |
| 475 | strncpy(menu[i], headers[i], text_cols-1); |
| 476 | menu[i][text_cols-1] = '\0'; |
| 477 | } |
| 478 | menu_top = i; |
| 479 | for (; i < text_rows; ++i) { |
| 480 | if (items[i-menu_top] == NULL) break; |
| 481 | strncpy(menu[i], items[i-menu_top], text_cols-1); |
| 482 | menu[i][text_cols-1] = '\0'; |
| 483 | } |
| 484 | menu_items = i - menu_top; |
| 485 | show_menu = 1; |
| 486 | menu_sel = initial_selection; |
| 487 | update_screen_locked(); |
| 488 | } |
| 489 | pthread_mutex_unlock(&updateMutex); |
| 490 | } |
| 491 | |
| 492 | int ScreenRecoveryUI::SelectMenu(int sel) { |
| 493 | int old_sel; |
| 494 | pthread_mutex_lock(&updateMutex); |
| 495 | if (show_menu > 0) { |
| 496 | old_sel = menu_sel; |
| 497 | menu_sel = sel; |
| 498 | if (menu_sel < 0) menu_sel = 0; |
| 499 | if (menu_sel >= menu_items) menu_sel = menu_items-1; |
| 500 | sel = menu_sel; |
| 501 | if (menu_sel != old_sel) update_screen_locked(); |
| 502 | } |
| 503 | pthread_mutex_unlock(&updateMutex); |
| 504 | return sel; |
| 505 | } |
| 506 | |
| 507 | void ScreenRecoveryUI::EndMenu() { |
| 508 | int i; |
| 509 | pthread_mutex_lock(&updateMutex); |
| 510 | if (show_menu > 0 && text_rows > 0 && text_cols > 0) { |
| 511 | show_menu = 0; |
| 512 | update_screen_locked(); |
| 513 | } |
| 514 | pthread_mutex_unlock(&updateMutex); |
| 515 | } |
| 516 | |
| 517 | bool ScreenRecoveryUI::IsTextVisible() |
| 518 | { |
| 519 | pthread_mutex_lock(&updateMutex); |
| 520 | int visible = show_text; |
| 521 | pthread_mutex_unlock(&updateMutex); |
| 522 | return visible; |
| 523 | } |
| 524 | |
| 525 | bool ScreenRecoveryUI::WasTextEverVisible() |
| 526 | { |
| 527 | pthread_mutex_lock(&updateMutex); |
| 528 | int ever_visible = show_text_ever; |
| 529 | pthread_mutex_unlock(&updateMutex); |
| 530 | return ever_visible; |
| 531 | } |
| 532 | |
| 533 | void ScreenRecoveryUI::ShowText(bool visible) |
| 534 | { |
| 535 | pthread_mutex_lock(&updateMutex); |
| 536 | show_text = visible; |
| 537 | if (show_text) show_text_ever = 1; |
| 538 | update_screen_locked(); |
| 539 | pthread_mutex_unlock(&updateMutex); |
| 540 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 541 | |
| 542 | void ScreenRecoveryUI::Redraw() |
| 543 | { |
| 544 | pthread_mutex_lock(&updateMutex); |
| 545 | update_screen_locked(); |
| 546 | pthread_mutex_unlock(&updateMutex); |
| 547 | } |