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