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