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