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