Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <linux/input.h> |
| 20 | #include <pthread.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <time.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #include "common.h" |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 32 | #include "device.h" |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 33 | #include "minui/minui.h" |
| 34 | #include "screen_ui.h" |
| 35 | #include "ui.h" |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 36 | |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 37 | static int char_width; |
| 38 | static int char_height; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 39 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 40 | // There's only (at most) one of these objects, and global callbacks |
| 41 | // (for pthread_create, and the input event system) need to find it, |
| 42 | // so use a global variable. |
| 43 | static ScreenRecoveryUI* self = NULL; |
| 44 | |
| 45 | // Return the current time as a double (including fractions of a second). |
| 46 | static double now() { |
| 47 | struct timeval tv; |
| 48 | gettimeofday(&tv, NULL); |
| 49 | return tv.tv_sec + tv.tv_usec / 1000000.0; |
| 50 | } |
| 51 | |
| 52 | ScreenRecoveryUI::ScreenRecoveryUI() : |
| 53 | currentIcon(NONE), |
| 54 | installingFrame(0), |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 55 | rtl_locale(false), |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 56 | progressBarType(EMPTY), |
| 57 | progressScopeStart(0), |
| 58 | progressScopeSize(0), |
| 59 | progress(0), |
| 60 | pagesIdentical(false), |
| 61 | text_cols(0), |
| 62 | text_rows(0), |
| 63 | text_col(0), |
| 64 | text_row(0), |
| 65 | text_top(0), |
| 66 | show_text(false), |
| 67 | show_text_ever(false), |
| 68 | show_menu(false), |
| 69 | menu_top(0), |
| 70 | menu_items(0), |
| 71 | menu_sel(0), |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 72 | animation_fps(20), |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 73 | installing_frames(-1), |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 74 | stage(-1), |
| 75 | max_stage(-1) { |
yetta_wu | 5b468fc | 2013-06-25 15:03:11 +0800 | [diff] [blame] | 76 | |
| 77 | for (int i = 0; i < 5; i++) |
| 78 | backgroundIcon[i] = NULL; |
| 79 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 80 | pthread_mutex_init(&updateMutex, NULL); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 81 | self = this; |
| 82 | } |
| 83 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 84 | // Clear the screen and draw the currently selected background icon (if any). |
| 85 | // Should only be called with updateMutex locked. |
| 86 | void ScreenRecoveryUI::draw_background_locked(Icon icon) |
| 87 | { |
| 88 | pagesIdentical = false; |
| 89 | gr_color(0, 0, 0, 255); |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame^] | 90 | gr_clear(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 91 | |
| 92 | if (icon) { |
| 93 | gr_surface surface = backgroundIcon[icon]; |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 94 | if (icon == INSTALLING_UPDATE || icon == ERASING) { |
| 95 | surface = installation[installingFrame]; |
| 96 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 97 | gr_surface text_surface = backgroundText[icon]; |
| 98 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 99 | int iconWidth = gr_get_width(surface); |
| 100 | int iconHeight = gr_get_height(surface); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 101 | int textWidth = gr_get_width(text_surface); |
| 102 | int textHeight = gr_get_height(text_surface); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 103 | int stageHeight = gr_get_height(stageMarkerEmpty); |
| 104 | |
| 105 | int sh = (max_stage >= 0) ? stageHeight : 0; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 106 | |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 107 | iconX = (gr_fb_width() - iconWidth) / 2; |
| 108 | iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 109 | |
| 110 | int textX = (gr_fb_width() - textWidth) / 2; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 111 | int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 112 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 113 | gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 114 | if (stageHeight > 0) { |
| 115 | int sw = gr_get_width(stageMarkerEmpty); |
| 116 | int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2; |
| 117 | int y = iconY + iconHeight + 20; |
| 118 | for (int i = 0; i < max_stage; ++i) { |
| 119 | gr_blit((i < stage) ? stageMarkerFill : stageMarkerEmpty, |
| 120 | 0, 0, sw, stageHeight, x, y); |
| 121 | x += sw; |
| 122 | } |
| 123 | } |
| 124 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 125 | gr_color(255, 255, 255, 255); |
| 126 | gr_texticon(textX, textY, text_surface); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | // Draw the progress bar (if any) on the screen. Does not flip pages. |
| 131 | // Should only be called with updateMutex locked. |
| 132 | void ScreenRecoveryUI::draw_progress_locked() |
| 133 | { |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 134 | if (currentIcon == ERROR) return; |
| 135 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 136 | if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) { |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 137 | gr_surface icon = installation[installingFrame]; |
| 138 | gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | if (progressBarType != EMPTY) { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 142 | int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 143 | int width = gr_get_width(progressBarEmpty); |
| 144 | int height = gr_get_height(progressBarEmpty); |
| 145 | |
| 146 | int dx = (gr_fb_width() - width)/2; |
| 147 | int dy = (3*gr_fb_height() + iconHeight - 2*height)/4; |
| 148 | |
| 149 | // Erase behind the progress bar (in case this was a progress-only update) |
| 150 | gr_color(0, 0, 0, 255); |
| 151 | gr_fill(dx, dy, width, height); |
| 152 | |
| 153 | if (progressBarType == DETERMINATE) { |
| 154 | float p = progressScopeStart + progress * progressScopeSize; |
| 155 | int pos = (int) (p * width); |
| 156 | |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 157 | if (rtl_locale) { |
| 158 | // Fill the progress bar from right to left. |
| 159 | if (pos > 0) { |
| 160 | gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy); |
| 161 | } |
| 162 | if (pos < width-1) { |
| 163 | gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy); |
| 164 | } |
| 165 | } else { |
| 166 | // Fill the progress bar from left to right. |
| 167 | if (pos > 0) { |
| 168 | gr_blit(progressBarFill, 0, 0, pos, height, dx, dy); |
| 169 | } |
| 170 | if (pos < width-1) { |
| 171 | gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy); |
| 172 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 178 | void ScreenRecoveryUI::SetColor(UIElement e) { |
| 179 | switch (e) { |
| 180 | case HEADER: |
| 181 | gr_color(247, 0, 6, 255); |
| 182 | break; |
| 183 | case MENU: |
| 184 | case MENU_SEL_BG: |
| 185 | gr_color(0, 106, 157, 255); |
| 186 | break; |
| 187 | case MENU_SEL_FG: |
| 188 | gr_color(255, 255, 255, 255); |
| 189 | break; |
| 190 | case LOG: |
| 191 | gr_color(249, 194, 0, 255); |
| 192 | break; |
| 193 | case TEXT_FILL: |
| 194 | gr_color(0, 0, 0, 160); |
| 195 | break; |
| 196 | default: |
| 197 | gr_color(255, 255, 255, 255); |
| 198 | break; |
| 199 | } |
| 200 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 201 | |
| 202 | // Redraw everything on the screen. Does not flip pages. |
| 203 | // Should only be called with updateMutex locked. |
| 204 | void ScreenRecoveryUI::draw_screen_locked() |
| 205 | { |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame^] | 206 | if (!show_text) { |
| 207 | draw_background_locked(currentIcon); |
| 208 | draw_progress_locked(); |
| 209 | } else { |
| 210 | gr_color(0, 0, 0, 255); |
| 211 | gr_clear(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 212 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 213 | int y = 0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 214 | int i = 0; |
| 215 | if (show_menu) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 216 | SetColor(HEADER); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 217 | |
| 218 | for (; i < menu_top + menu_items; ++i) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 219 | if (i == menu_top) SetColor(MENU); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 220 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 221 | if (i == menu_top + menu_sel) { |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 222 | // draw the highlight bar |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 223 | SetColor(MENU_SEL_BG); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 224 | gr_fill(0, y-2, gr_fb_width(), y+char_height+2); |
| 225 | // white text of selected item |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 226 | SetColor(MENU_SEL_FG); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 227 | if (menu[i][0]) gr_text(4, y, menu[i], 1); |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 228 | SetColor(MENU); |
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 | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 234 | SetColor(MENU); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 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 | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 241 | SetColor(LOG); |
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 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 301 | // move the progress bar forward on timed intervals, if configured |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 302 | int duration = progressScopeDuration; |
| 303 | if (progressBarType == DETERMINATE && duration > 0) { |
| 304 | double elapsed = now() - progressScopeTime; |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 305 | float p = 1.0 * elapsed / duration; |
| 306 | if (p > 1.0) p = 1.0; |
| 307 | if (p > progress) { |
| 308 | progress = p; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 309 | redraw = 1; |
| 310 | } |
| 311 | } |
| 312 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 313 | if (redraw) update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 314 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 315 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 316 | double end = now(); |
| 317 | // minimum of 20ms delay between frames |
| 318 | double delay = interval - (end-start); |
| 319 | if (delay < 0.02) delay = 0.02; |
| 320 | usleep((long)(delay * 1000000)); |
| 321 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) { |
| 325 | int result = res_create_surface(filename, surface); |
| 326 | if (result < 0) { |
| 327 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 328 | } |
| 329 | } |
| 330 | |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 331 | void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) { |
| 332 | int result = res_create_multi_surface(filename, frames, surface); |
| 333 | if (result < 0) { |
| 334 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 335 | } |
| 336 | } |
| 337 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 338 | void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) { |
| 339 | int result = res_create_localized_surface(filename, surface); |
| 340 | if (result < 0) { |
| 341 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 342 | } |
| 343 | } |
| 344 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 345 | void ScreenRecoveryUI::Init() |
| 346 | { |
| 347 | gr_init(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 348 | |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 349 | gr_font_size(&char_width, &char_height); |
| 350 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 351 | text_col = text_row = 0; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 352 | text_rows = gr_fb_height() / char_height; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 353 | if (text_rows > kMaxRows) text_rows = kMaxRows; |
| 354 | text_top = 1; |
| 355 | |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 356 | text_cols = gr_fb_width() / char_width; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 357 | if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1; |
| 358 | |
Alistair Strachan | 9b8ae80 | 2013-07-17 10:34:36 -0700 | [diff] [blame] | 359 | backgroundIcon[NONE] = NULL; |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 360 | LoadBitmapArray("icon_installing", &installing_frames, &installation); |
| 361 | backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : NULL; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 362 | backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE]; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 363 | LoadBitmap("icon_error", &backgroundIcon[ERROR]); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 364 | backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; |
| 365 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 366 | LoadBitmap("progress_empty", &progressBarEmpty); |
| 367 | LoadBitmap("progress_fill", &progressBarFill); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 368 | LoadBitmap("stage_empty", &stageMarkerEmpty); |
| 369 | LoadBitmap("stage_fill", &stageMarkerFill); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 370 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 371 | LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]); |
| 372 | LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]); |
| 373 | LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]); |
| 374 | LoadLocalizedBitmap("error_text", &backgroundText[ERROR]); |
| 375 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 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) |
Doug Zongker | b66cb69 | 2012-09-18 14:52:18 -0700 | [diff] [blame] | 396 | strcmp(lang, "iw") == 0 || // Hebrew (old language code) |
| 397 | strcmp(lang, "ur") == 0) { // Urdu |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 398 | rtl_locale = true; |
| 399 | } |
| 400 | free(lang); |
| 401 | } |
| 402 | } |
| 403 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 404 | void ScreenRecoveryUI::SetBackground(Icon icon) |
| 405 | { |
| 406 | pthread_mutex_lock(&updateMutex); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 407 | |
Doug Zongker | 52eeea4 | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 408 | currentIcon = icon; |
| 409 | update_screen_locked(); |
| 410 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 411 | pthread_mutex_unlock(&updateMutex); |
| 412 | } |
| 413 | |
| 414 | void ScreenRecoveryUI::SetProgressType(ProgressType type) |
| 415 | { |
| 416 | pthread_mutex_lock(&updateMutex); |
| 417 | if (progressBarType != type) { |
| 418 | progressBarType = type; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 419 | } |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 420 | progressScopeStart = 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 421 | progressScopeSize = 0; |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 422 | progress = 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 423 | update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 424 | pthread_mutex_unlock(&updateMutex); |
| 425 | } |
| 426 | |
| 427 | void ScreenRecoveryUI::ShowProgress(float portion, float seconds) |
| 428 | { |
| 429 | pthread_mutex_lock(&updateMutex); |
| 430 | progressBarType = DETERMINATE; |
| 431 | progressScopeStart += progressScopeSize; |
| 432 | progressScopeSize = portion; |
| 433 | progressScopeTime = now(); |
| 434 | progressScopeDuration = seconds; |
| 435 | progress = 0; |
| 436 | update_progress_locked(); |
| 437 | pthread_mutex_unlock(&updateMutex); |
| 438 | } |
| 439 | |
| 440 | void ScreenRecoveryUI::SetProgress(float fraction) |
| 441 | { |
| 442 | pthread_mutex_lock(&updateMutex); |
| 443 | if (fraction < 0.0) fraction = 0.0; |
| 444 | if (fraction > 1.0) fraction = 1.0; |
| 445 | if (progressBarType == DETERMINATE && fraction > progress) { |
| 446 | // Skip updates that aren't visibly different. |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 447 | int width = gr_get_width(progressBarEmpty); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 448 | float scale = width * progressScopeSize; |
| 449 | if ((int) (progress * scale) != (int) (fraction * scale)) { |
| 450 | progress = fraction; |
| 451 | update_progress_locked(); |
| 452 | } |
| 453 | } |
| 454 | pthread_mutex_unlock(&updateMutex); |
| 455 | } |
| 456 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 457 | void ScreenRecoveryUI::SetStage(int current, int max) { |
| 458 | pthread_mutex_lock(&updateMutex); |
| 459 | stage = current; |
| 460 | max_stage = max; |
| 461 | pthread_mutex_unlock(&updateMutex); |
| 462 | } |
| 463 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 464 | void ScreenRecoveryUI::Print(const char *fmt, ...) |
| 465 | { |
| 466 | char buf[256]; |
| 467 | va_list ap; |
| 468 | va_start(ap, fmt); |
| 469 | vsnprintf(buf, 256, fmt, ap); |
| 470 | va_end(ap); |
| 471 | |
| 472 | fputs(buf, stdout); |
| 473 | |
| 474 | // This can get called before ui_init(), so be careful. |
| 475 | pthread_mutex_lock(&updateMutex); |
| 476 | if (text_rows > 0 && text_cols > 0) { |
| 477 | char *ptr; |
| 478 | for (ptr = buf; *ptr != '\0'; ++ptr) { |
| 479 | if (*ptr == '\n' || text_col >= text_cols) { |
| 480 | text[text_row][text_col] = '\0'; |
| 481 | text_col = 0; |
| 482 | text_row = (text_row + 1) % text_rows; |
| 483 | if (text_row == text_top) text_top = (text_top + 1) % text_rows; |
| 484 | } |
| 485 | if (*ptr != '\n') text[text_row][text_col++] = *ptr; |
| 486 | } |
| 487 | text[text_row][text_col] = '\0'; |
| 488 | update_screen_locked(); |
| 489 | } |
| 490 | pthread_mutex_unlock(&updateMutex); |
| 491 | } |
| 492 | |
| 493 | void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items, |
| 494 | int initial_selection) { |
| 495 | int i; |
| 496 | pthread_mutex_lock(&updateMutex); |
| 497 | if (text_rows > 0 && text_cols > 0) { |
| 498 | for (i = 0; i < text_rows; ++i) { |
| 499 | if (headers[i] == NULL) break; |
| 500 | strncpy(menu[i], headers[i], text_cols-1); |
| 501 | menu[i][text_cols-1] = '\0'; |
| 502 | } |
| 503 | menu_top = i; |
| 504 | for (; i < text_rows; ++i) { |
| 505 | if (items[i-menu_top] == NULL) break; |
| 506 | strncpy(menu[i], items[i-menu_top], text_cols-1); |
| 507 | menu[i][text_cols-1] = '\0'; |
| 508 | } |
| 509 | menu_items = i - menu_top; |
| 510 | show_menu = 1; |
| 511 | menu_sel = initial_selection; |
| 512 | update_screen_locked(); |
| 513 | } |
| 514 | pthread_mutex_unlock(&updateMutex); |
| 515 | } |
| 516 | |
| 517 | int ScreenRecoveryUI::SelectMenu(int sel) { |
| 518 | int old_sel; |
| 519 | pthread_mutex_lock(&updateMutex); |
| 520 | if (show_menu > 0) { |
| 521 | old_sel = menu_sel; |
| 522 | menu_sel = sel; |
| 523 | if (menu_sel < 0) menu_sel = 0; |
| 524 | if (menu_sel >= menu_items) menu_sel = menu_items-1; |
| 525 | sel = menu_sel; |
| 526 | if (menu_sel != old_sel) update_screen_locked(); |
| 527 | } |
| 528 | pthread_mutex_unlock(&updateMutex); |
| 529 | return sel; |
| 530 | } |
| 531 | |
| 532 | void ScreenRecoveryUI::EndMenu() { |
| 533 | int i; |
| 534 | pthread_mutex_lock(&updateMutex); |
| 535 | if (show_menu > 0 && text_rows > 0 && text_cols > 0) { |
| 536 | show_menu = 0; |
| 537 | update_screen_locked(); |
| 538 | } |
| 539 | pthread_mutex_unlock(&updateMutex); |
| 540 | } |
| 541 | |
| 542 | bool ScreenRecoveryUI::IsTextVisible() |
| 543 | { |
| 544 | pthread_mutex_lock(&updateMutex); |
| 545 | int visible = show_text; |
| 546 | pthread_mutex_unlock(&updateMutex); |
| 547 | return visible; |
| 548 | } |
| 549 | |
| 550 | bool ScreenRecoveryUI::WasTextEverVisible() |
| 551 | { |
| 552 | pthread_mutex_lock(&updateMutex); |
| 553 | int ever_visible = show_text_ever; |
| 554 | pthread_mutex_unlock(&updateMutex); |
| 555 | return ever_visible; |
| 556 | } |
| 557 | |
| 558 | void ScreenRecoveryUI::ShowText(bool visible) |
| 559 | { |
| 560 | pthread_mutex_lock(&updateMutex); |
| 561 | show_text = visible; |
| 562 | if (show_text) show_text_ever = 1; |
| 563 | update_screen_locked(); |
| 564 | pthread_mutex_unlock(&updateMutex); |
| 565 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 566 | |
| 567 | void ScreenRecoveryUI::Redraw() |
| 568 | { |
| 569 | pthread_mutex_lock(&updateMutex); |
| 570 | update_screen_locked(); |
| 571 | pthread_mutex_unlock(&updateMutex); |
| 572 | } |