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 | |
| 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 | 8347cb2 | 2012-10-08 08:38:16 -0700 | [diff] [blame] | 79 | indeterminate_frames(6), |
| 80 | installing_frames(7), |
| 81 | install_overlay_offset_x(13), |
| 82 | install_overlay_offset_y(190), |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 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 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 195 | #define C_HEADER 247,0,6 |
| 196 | #define C_MENU 0,106,157 |
| 197 | #define C_LOG 249,194,0 |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 198 | |
| 199 | // Redraw everything on the screen. Does not flip pages. |
| 200 | // Should only be called with updateMutex locked. |
| 201 | void ScreenRecoveryUI::draw_screen_locked() |
| 202 | { |
| 203 | draw_background_locked(currentIcon); |
| 204 | draw_progress_locked(); |
| 205 | |
| 206 | if (show_text) { |
| 207 | gr_color(0, 0, 0, 160); |
| 208 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 209 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 210 | int y = 0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 211 | int i = 0; |
| 212 | if (show_menu) { |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 213 | gr_color(C_HEADER, 255); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 214 | |
| 215 | for (; i < menu_top + menu_items; ++i) { |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 216 | if (i == menu_top) gr_color(C_MENU, 255); |
| 217 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 218 | if (i == menu_top + menu_sel) { |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 219 | // draw the highlight bar |
| 220 | gr_fill(0, y-2, gr_fb_width(), y+char_height+2); |
| 221 | // white text of selected item |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 222 | gr_color(255, 255, 255, 255); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 223 | if (menu[i][0]) gr_text(4, y, menu[i], 1); |
| 224 | gr_color(C_MENU, 255); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 225 | } else { |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 226 | 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] | 227 | } |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 228 | y += char_height+4; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 229 | } |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 230 | gr_color(C_MENU, 255); |
| 231 | y += 4; |
| 232 | gr_fill(0, y, gr_fb_width(), y+2); |
| 233 | y += 4; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 234 | ++i; |
| 235 | } |
| 236 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 237 | gr_color(C_LOG, 255); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 238 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 239 | // display from the bottom up, until we hit the top of the |
| 240 | // screen, the bottom of the menu, or we've displayed the |
| 241 | // entire text buffer. |
| 242 | int ty; |
| 243 | int row = (text_top+text_rows-1) % text_rows; |
| 244 | for (int ty = gr_fb_height() - char_height, count = 0; |
| 245 | ty > y+2 && count < text_rows; |
| 246 | ty -= char_height, ++count) { |
| 247 | gr_text(4, ty, text[row], 0); |
| 248 | --row; |
| 249 | if (row < 0) row = text_rows-1; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // Redraw everything on the screen and flip the screen (make it visible). |
| 255 | // Should only be called with updateMutex locked. |
| 256 | void ScreenRecoveryUI::update_screen_locked() |
| 257 | { |
| 258 | draw_screen_locked(); |
| 259 | gr_flip(); |
| 260 | } |
| 261 | |
| 262 | // Updates only the progress bar, if possible, otherwise redraws the screen. |
| 263 | // Should only be called with updateMutex locked. |
| 264 | void ScreenRecoveryUI::update_progress_locked() |
| 265 | { |
| 266 | if (show_text || !pagesIdentical) { |
| 267 | draw_screen_locked(); // Must redraw the whole screen |
| 268 | pagesIdentical = true; |
| 269 | } else { |
| 270 | draw_progress_locked(); // Draw only the progress bar and overlays |
| 271 | } |
| 272 | gr_flip(); |
| 273 | } |
| 274 | |
| 275 | // Keeps the progress bar updated, even when the process is otherwise busy. |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 276 | void* ScreenRecoveryUI::progress_thread(void *cookie) { |
| 277 | self->progress_loop(); |
| 278 | return NULL; |
| 279 | } |
| 280 | |
| 281 | void ScreenRecoveryUI::progress_loop() { |
| 282 | double interval = 1.0 / animation_fps; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 283 | for (;;) { |
| 284 | double start = now(); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 285 | pthread_mutex_lock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 286 | |
| 287 | int redraw = 0; |
| 288 | |
| 289 | // update the installation animation, if active |
| 290 | // skip this if we have a text overlay (too expensive to update) |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 291 | if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && |
| 292 | installing_frames > 0 && !show_text) { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 293 | installingFrame = (installingFrame + 1) % installing_frames; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 294 | redraw = 1; |
| 295 | } |
| 296 | |
| 297 | // update the progress bar animation, if active |
| 298 | // skip this if we have a text overlay (too expensive to update) |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 299 | if (progressBarType == INDETERMINATE && !show_text) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 300 | redraw = 1; |
| 301 | } |
| 302 | |
| 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) { |
| 327 | int result = res_create_surface(filename, surface); |
| 328 | if (result < 0) { |
| 329 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 330 | } |
| 331 | } |
| 332 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 333 | void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) { |
| 334 | int result = res_create_localized_surface(filename, surface); |
| 335 | if (result < 0) { |
| 336 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 337 | } |
| 338 | } |
| 339 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 340 | void ScreenRecoveryUI::Init() |
| 341 | { |
| 342 | gr_init(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 343 | |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 344 | gr_font_size(&char_width, &char_height); |
| 345 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 346 | text_col = text_row = 0; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 347 | text_rows = gr_fb_height() / char_height; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 348 | if (text_rows > kMaxRows) text_rows = kMaxRows; |
| 349 | text_top = 1; |
| 350 | |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 351 | text_cols = gr_fb_width() / char_width; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 352 | if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1; |
| 353 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 354 | LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]); |
| 355 | backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE]; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 356 | LoadBitmap("icon_error", &backgroundIcon[ERROR]); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 357 | backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; |
| 358 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 359 | LoadBitmap("progress_empty", &progressBarEmpty); |
| 360 | LoadBitmap("progress_fill", &progressBarFill); |
| 361 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 362 | LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]); |
| 363 | LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]); |
| 364 | LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]); |
| 365 | LoadLocalizedBitmap("error_text", &backgroundText[ERROR]); |
| 366 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 367 | int i; |
| 368 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 369 | progressBarIndeterminate = (gr_surface*)malloc(indeterminate_frames * |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 370 | sizeof(gr_surface)); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 371 | for (i = 0; i < indeterminate_frames; ++i) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 372 | char filename[40]; |
| 373 | // "indeterminate01.png", "indeterminate02.png", ... |
| 374 | sprintf(filename, "indeterminate%02d", i+1); |
| 375 | LoadBitmap(filename, progressBarIndeterminate+i); |
| 376 | } |
| 377 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 378 | if (installing_frames > 0) { |
| 379 | installationOverlay = (gr_surface*)malloc(installing_frames * |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 380 | sizeof(gr_surface)); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 381 | for (i = 0; i < installing_frames; ++i) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 382 | char filename[40]; |
| 383 | // "icon_installing_overlay01.png", |
| 384 | // "icon_installing_overlay02.png", ... |
| 385 | sprintf(filename, "icon_installing_overlay%02d", i+1); |
| 386 | LoadBitmap(filename, installationOverlay+i); |
| 387 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 388 | } else { |
| 389 | installationOverlay = NULL; |
| 390 | } |
| 391 | |
| 392 | pthread_create(&progress_t, NULL, progress_thread, NULL); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 393 | |
| 394 | RecoveryUI::Init(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 397 | void ScreenRecoveryUI::SetLocale(const char* locale) { |
| 398 | if (locale) { |
| 399 | char* lang = strdup(locale); |
| 400 | for (char* p = lang; *p; ++p) { |
| 401 | if (*p == '_') { |
| 402 | *p = '\0'; |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | // A bit cheesy: keep an explicit list of supported languages |
| 408 | // that are RTL. |
| 409 | if (strcmp(lang, "ar") == 0 || // Arabic |
| 410 | strcmp(lang, "fa") == 0 || // Persian (Farsi) |
| 411 | strcmp(lang, "he") == 0 || // Hebrew (new language code) |
Doug Zongker | b66cb69 | 2012-09-18 14:52:18 -0700 | [diff] [blame] | 412 | strcmp(lang, "iw") == 0 || // Hebrew (old language code) |
| 413 | strcmp(lang, "ur") == 0) { // Urdu |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 414 | rtl_locale = true; |
| 415 | } |
| 416 | free(lang); |
| 417 | } |
| 418 | } |
| 419 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 420 | void ScreenRecoveryUI::SetBackground(Icon icon) |
| 421 | { |
| 422 | pthread_mutex_lock(&updateMutex); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 423 | |
| 424 | // Adjust the offset to account for the positioning of the |
| 425 | // base image on the screen. |
| 426 | if (backgroundIcon[icon] != NULL) { |
| 427 | gr_surface bg = backgroundIcon[icon]; |
| 428 | gr_surface text = backgroundText[icon]; |
| 429 | overlay_offset_x = install_overlay_offset_x + (gr_fb_width() - gr_get_width(bg)) / 2; |
| 430 | overlay_offset_y = install_overlay_offset_y + |
| 431 | (gr_fb_height() - (gr_get_height(bg) + gr_get_height(text) + 40)) / 2; |
| 432 | } |
| 433 | |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 434 | currentIcon = icon; |
| 435 | update_screen_locked(); |
| 436 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 437 | pthread_mutex_unlock(&updateMutex); |
| 438 | } |
| 439 | |
| 440 | void ScreenRecoveryUI::SetProgressType(ProgressType type) |
| 441 | { |
| 442 | pthread_mutex_lock(&updateMutex); |
| 443 | if (progressBarType != type) { |
| 444 | progressBarType = type; |
| 445 | update_progress_locked(); |
| 446 | } |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 447 | progressScopeStart = 0; |
| 448 | progress = 0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 449 | pthread_mutex_unlock(&updateMutex); |
| 450 | } |
| 451 | |
| 452 | void ScreenRecoveryUI::ShowProgress(float portion, float seconds) |
| 453 | { |
| 454 | pthread_mutex_lock(&updateMutex); |
| 455 | progressBarType = DETERMINATE; |
| 456 | progressScopeStart += progressScopeSize; |
| 457 | progressScopeSize = portion; |
| 458 | progressScopeTime = now(); |
| 459 | progressScopeDuration = seconds; |
| 460 | progress = 0; |
| 461 | update_progress_locked(); |
| 462 | pthread_mutex_unlock(&updateMutex); |
| 463 | } |
| 464 | |
| 465 | void ScreenRecoveryUI::SetProgress(float fraction) |
| 466 | { |
| 467 | pthread_mutex_lock(&updateMutex); |
| 468 | if (fraction < 0.0) fraction = 0.0; |
| 469 | if (fraction > 1.0) fraction = 1.0; |
| 470 | if (progressBarType == DETERMINATE && fraction > progress) { |
| 471 | // Skip updates that aren't visibly different. |
| 472 | int width = gr_get_width(progressBarIndeterminate[0]); |
| 473 | float scale = width * progressScopeSize; |
| 474 | if ((int) (progress * scale) != (int) (fraction * scale)) { |
| 475 | progress = fraction; |
| 476 | update_progress_locked(); |
| 477 | } |
| 478 | } |
| 479 | pthread_mutex_unlock(&updateMutex); |
| 480 | } |
| 481 | |
| 482 | void ScreenRecoveryUI::Print(const char *fmt, ...) |
| 483 | { |
| 484 | char buf[256]; |
| 485 | va_list ap; |
| 486 | va_start(ap, fmt); |
| 487 | vsnprintf(buf, 256, fmt, ap); |
| 488 | va_end(ap); |
| 489 | |
| 490 | fputs(buf, stdout); |
| 491 | |
| 492 | // This can get called before ui_init(), so be careful. |
| 493 | pthread_mutex_lock(&updateMutex); |
| 494 | if (text_rows > 0 && text_cols > 0) { |
| 495 | char *ptr; |
| 496 | for (ptr = buf; *ptr != '\0'; ++ptr) { |
| 497 | if (*ptr == '\n' || text_col >= text_cols) { |
| 498 | text[text_row][text_col] = '\0'; |
| 499 | text_col = 0; |
| 500 | text_row = (text_row + 1) % text_rows; |
| 501 | if (text_row == text_top) text_top = (text_top + 1) % text_rows; |
| 502 | } |
| 503 | if (*ptr != '\n') text[text_row][text_col++] = *ptr; |
| 504 | } |
| 505 | text[text_row][text_col] = '\0'; |
| 506 | update_screen_locked(); |
| 507 | } |
| 508 | pthread_mutex_unlock(&updateMutex); |
| 509 | } |
| 510 | |
| 511 | void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items, |
| 512 | int initial_selection) { |
| 513 | int i; |
| 514 | pthread_mutex_lock(&updateMutex); |
| 515 | if (text_rows > 0 && text_cols > 0) { |
| 516 | for (i = 0; i < text_rows; ++i) { |
| 517 | if (headers[i] == NULL) break; |
| 518 | strncpy(menu[i], headers[i], text_cols-1); |
| 519 | menu[i][text_cols-1] = '\0'; |
| 520 | } |
| 521 | menu_top = i; |
| 522 | for (; i < text_rows; ++i) { |
| 523 | if (items[i-menu_top] == NULL) break; |
| 524 | strncpy(menu[i], items[i-menu_top], text_cols-1); |
| 525 | menu[i][text_cols-1] = '\0'; |
| 526 | } |
| 527 | menu_items = i - menu_top; |
| 528 | show_menu = 1; |
| 529 | menu_sel = initial_selection; |
| 530 | update_screen_locked(); |
| 531 | } |
| 532 | pthread_mutex_unlock(&updateMutex); |
| 533 | } |
| 534 | |
| 535 | int ScreenRecoveryUI::SelectMenu(int sel) { |
| 536 | int old_sel; |
| 537 | pthread_mutex_lock(&updateMutex); |
| 538 | if (show_menu > 0) { |
| 539 | old_sel = menu_sel; |
| 540 | menu_sel = sel; |
| 541 | if (menu_sel < 0) menu_sel = 0; |
| 542 | if (menu_sel >= menu_items) menu_sel = menu_items-1; |
| 543 | sel = menu_sel; |
| 544 | if (menu_sel != old_sel) update_screen_locked(); |
| 545 | } |
| 546 | pthread_mutex_unlock(&updateMutex); |
| 547 | return sel; |
| 548 | } |
| 549 | |
| 550 | void ScreenRecoveryUI::EndMenu() { |
| 551 | int i; |
| 552 | pthread_mutex_lock(&updateMutex); |
| 553 | if (show_menu > 0 && text_rows > 0 && text_cols > 0) { |
| 554 | show_menu = 0; |
| 555 | update_screen_locked(); |
| 556 | } |
| 557 | pthread_mutex_unlock(&updateMutex); |
| 558 | } |
| 559 | |
| 560 | bool ScreenRecoveryUI::IsTextVisible() |
| 561 | { |
| 562 | pthread_mutex_lock(&updateMutex); |
| 563 | int visible = show_text; |
| 564 | pthread_mutex_unlock(&updateMutex); |
| 565 | return visible; |
| 566 | } |
| 567 | |
| 568 | bool ScreenRecoveryUI::WasTextEverVisible() |
| 569 | { |
| 570 | pthread_mutex_lock(&updateMutex); |
| 571 | int ever_visible = show_text_ever; |
| 572 | pthread_mutex_unlock(&updateMutex); |
| 573 | return ever_visible; |
| 574 | } |
| 575 | |
| 576 | void ScreenRecoveryUI::ShowText(bool visible) |
| 577 | { |
| 578 | pthread_mutex_lock(&updateMutex); |
| 579 | show_text = visible; |
| 580 | if (show_text) show_text_ever = 1; |
| 581 | update_screen_locked(); |
| 582 | pthread_mutex_unlock(&updateMutex); |
| 583 | } |