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