Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 <pthread.h> |
| 20 | #include <stdarg.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/time.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <time.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <vector> |
| 31 | |
| 32 | #include "common.h" |
| 33 | #include "device.h" |
| 34 | #include "minui/minui.h" |
| 35 | #include "wear_ui.h" |
| 36 | #include "ui.h" |
| 37 | #include "cutils/properties.h" |
| 38 | #include "base/strings.h" |
| 39 | |
| 40 | static int char_width; |
| 41 | static int char_height; |
| 42 | |
| 43 | // There's only (at most) one of these objects, and global callbacks |
| 44 | // (for pthread_create, and the input event system) need to find it, |
| 45 | // so use a global variable. |
| 46 | static WearRecoveryUI* self = NULL; |
| 47 | |
| 48 | // Return the current time as a double (including fractions of a second). |
| 49 | static double now() { |
| 50 | struct timeval tv; |
| 51 | gettimeofday(&tv, NULL); |
| 52 | return tv.tv_sec + tv.tv_usec / 1000000.0; |
| 53 | } |
| 54 | |
| 55 | WearRecoveryUI::WearRecoveryUI() : |
| 56 | progress_bar_height(3), |
| 57 | progress_bar_width(200), |
| 58 | progress_bar_y(259), |
| 59 | outer_height(0), |
| 60 | outer_width(0), |
| 61 | menu_unusable_rows(0), |
| 62 | intro_frames(22), |
| 63 | loop_frames(60), |
| 64 | currentIcon(NONE), |
| 65 | intro_done(false), |
| 66 | current_frame(0), |
| 67 | animation_fps(30), |
| 68 | rtl_locale(false), |
| 69 | progressBarType(EMPTY), |
| 70 | progressScopeStart(0), |
| 71 | progressScopeSize(0), |
| 72 | progress(0), |
| 73 | text_cols(0), |
| 74 | text_rows(0), |
| 75 | text_col(0), |
| 76 | text_row(0), |
| 77 | text_top(0), |
| 78 | show_text(false), |
| 79 | show_text_ever(false), |
| 80 | show_menu(false), |
| 81 | menu_items(0), |
| 82 | menu_sel(0) { |
| 83 | |
| 84 | for (size_t i = 0; i < 5; i++) |
| 85 | backgroundIcon[i] = NULL; |
| 86 | |
| 87 | pthread_mutex_init(&updateMutex, NULL); |
| 88 | self = this; |
| 89 | } |
| 90 | |
| 91 | // Draw background frame on the screen. Does not flip pages. |
| 92 | // Should only be called with updateMutex locked. |
| 93 | void WearRecoveryUI::draw_background_locked(Icon icon) |
| 94 | { |
| 95 | gr_color(0, 0, 0, 255); |
| 96 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 97 | |
| 98 | if (icon) { |
| 99 | GRSurface* surface; |
| 100 | if (icon == INSTALLING_UPDATE || icon == ERASING) { |
| 101 | if (!intro_done) { |
| 102 | surface = introFrames[current_frame]; |
| 103 | } else { |
| 104 | surface = loopFrames[current_frame]; |
| 105 | } |
| 106 | } |
| 107 | else { |
| 108 | surface = backgroundIcon[icon]; |
| 109 | } |
| 110 | |
| 111 | int width = gr_get_width(surface); |
| 112 | int height = gr_get_height(surface); |
| 113 | |
| 114 | int x = (gr_fb_width() - width) / 2; |
| 115 | int y = (gr_fb_height() - height) / 2; |
| 116 | |
| 117 | gr_blit(surface, 0, 0, width, height, x, y); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Draw the progress bar (if any) on the screen. Does not flip pages. |
| 122 | // Should only be called with updateMutex locked. |
| 123 | void WearRecoveryUI::draw_progress_locked() |
| 124 | { |
| 125 | if (currentIcon == ERROR) return; |
| 126 | if (progressBarType != DETERMINATE) return; |
| 127 | |
| 128 | int width = progress_bar_width; |
| 129 | int height = progress_bar_height; |
| 130 | int dx = (gr_fb_width() - width)/2; |
| 131 | int dy = progress_bar_y; |
| 132 | |
| 133 | float p = progressScopeStart + progress * progressScopeSize; |
| 134 | int pos = (int) (p * width); |
| 135 | |
| 136 | gr_color(0x43, 0x43, 0x43, 0xff); |
| 137 | gr_fill(dx, dy, dx + width, dy + height); |
| 138 | |
| 139 | if (pos > 0) { |
| 140 | gr_color(0x02, 0xa8, 0xf3, 255); |
| 141 | if (rtl_locale) { |
| 142 | // Fill the progress bar from right to left. |
| 143 | gr_fill(dx + width - pos, dy, dx + width, dy + height); |
| 144 | } else { |
| 145 | // Fill the progress bar from left to right. |
| 146 | gr_fill(dx, dy, dx + pos, dy + height); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void WearRecoveryUI::SetColor(UIElement e) { |
| 152 | switch (e) { |
| 153 | case HEADER: |
| 154 | gr_color(247, 0, 6, 255); |
| 155 | break; |
| 156 | case MENU: |
| 157 | case MENU_SEL_BG: |
| 158 | gr_color(0, 106, 157, 255); |
| 159 | break; |
| 160 | case MENU_SEL_FG: |
| 161 | gr_color(255, 255, 255, 255); |
| 162 | break; |
| 163 | case LOG: |
| 164 | gr_color(249, 194, 0, 255); |
| 165 | break; |
| 166 | case TEXT_FILL: |
| 167 | gr_color(0, 0, 0, 160); |
| 168 | break; |
| 169 | default: |
| 170 | gr_color(255, 255, 255, 255); |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void WearRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) { |
| 176 | gr_text(x, *y, line, bold); |
| 177 | *y += char_height + 4; |
| 178 | } |
| 179 | |
| 180 | void WearRecoveryUI::DrawTextLines(int x, int* y, const char* const* lines) { |
| 181 | for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) { |
| 182 | DrawTextLine(x, y, lines[i], false); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | static const char* HEADERS[] = { |
| 187 | "Swipe up/down to move.", |
| 188 | "Swipe left/right to select.", |
| 189 | "", |
| 190 | NULL |
| 191 | }; |
| 192 | |
| 193 | void WearRecoveryUI::draw_screen_locked() |
| 194 | { |
| 195 | draw_background_locked(currentIcon); |
| 196 | draw_progress_locked(); |
| 197 | char cur_selection_str[50]; |
| 198 | |
| 199 | if (show_text) { |
| 200 | SetColor(TEXT_FILL); |
| 201 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 202 | |
| 203 | int y = outer_height; |
| 204 | int x = outer_width; |
| 205 | if (show_menu) { |
| 206 | char recovery_fingerprint[PROPERTY_VALUE_MAX]; |
| 207 | property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, ""); |
| 208 | SetColor(HEADER); |
| 209 | DrawTextLine(x + 4, &y, "Android Recovery", true); |
| 210 | for (auto& chunk: android::base::Split(recovery_fingerprint, ":")) { |
| 211 | DrawTextLine(x +4, &y, chunk.c_str(), false); |
| 212 | } |
| 213 | |
| 214 | // This is actually the help strings. |
| 215 | DrawTextLines(x + 4, &y, HEADERS); |
| 216 | SetColor(HEADER); |
| 217 | DrawTextLines(x + 4, &y, menu_headers_); |
| 218 | |
| 219 | // Show the current menu item number in relation to total number if |
| 220 | // items don't fit on the screen. |
| 221 | if (menu_items > menu_end - menu_start) { |
| 222 | sprintf(cur_selection_str, "Current item: %d/%d", menu_sel + 1, menu_items); |
| 223 | gr_text(x+4, y, cur_selection_str, 1); |
| 224 | y += char_height+4; |
| 225 | } |
| 226 | |
| 227 | // Menu begins here |
| 228 | SetColor(MENU); |
| 229 | |
| 230 | for (int i = menu_start; i < menu_end; ++i) { |
| 231 | |
| 232 | if (i == menu_sel) { |
| 233 | // draw the highlight bar |
| 234 | SetColor(MENU_SEL_BG); |
| 235 | gr_fill(x, y-2, gr_fb_width()-x, y+char_height+2); |
| 236 | // white text of selected item |
| 237 | SetColor(MENU_SEL_FG); |
| 238 | if (menu[i][0]) gr_text(x+4, y, menu[i], 1); |
| 239 | SetColor(MENU); |
| 240 | } else { |
| 241 | if (menu[i][0]) gr_text(x+4, y, menu[i], 0); |
| 242 | } |
| 243 | y += char_height+4; |
| 244 | } |
| 245 | SetColor(MENU); |
| 246 | y += 4; |
| 247 | gr_fill(0, y, gr_fb_width(), y+2); |
| 248 | y += 4; |
| 249 | } |
| 250 | |
| 251 | SetColor(LOG); |
| 252 | |
| 253 | // display from the bottom up, until we hit the top of the |
| 254 | // screen, the bottom of the menu, or we've displayed the |
| 255 | // entire text buffer. |
| 256 | int ty; |
| 257 | int row = (text_top+text_rows-1) % text_rows; |
| 258 | size_t count = 0; |
| 259 | for (int ty = gr_fb_height() - char_height - outer_height; |
| 260 | ty > y+2 && count < text_rows; |
| 261 | ty -= char_height, ++count) { |
| 262 | gr_text(x+4, ty, text[row], 0); |
| 263 | --row; |
| 264 | if (row < 0) row = text_rows-1; |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void WearRecoveryUI::update_screen_locked() |
| 270 | { |
| 271 | draw_screen_locked(); |
| 272 | gr_flip(); |
| 273 | } |
| 274 | |
| 275 | // Keeps the progress bar updated, even when the process is otherwise busy. |
| 276 | void* WearRecoveryUI::progress_thread(void *cookie) { |
| 277 | self->progress_loop(); |
| 278 | return NULL; |
| 279 | } |
| 280 | |
| 281 | void WearRecoveryUI::progress_loop() { |
| 282 | double interval = 1.0 / animation_fps; |
| 283 | for (;;) { |
| 284 | double start = now(); |
| 285 | pthread_mutex_lock(&updateMutex); |
| 286 | int redraw = 0; |
| 287 | |
| 288 | if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) |
| 289 | && !show_text) { |
| 290 | if (!intro_done) { |
| 291 | if (current_frame == intro_frames - 1) { |
| 292 | intro_done = true; |
| 293 | current_frame = 0; |
| 294 | } else { |
| 295 | current_frame++; |
| 296 | } |
| 297 | } else { |
| 298 | current_frame = (current_frame + 1) % loop_frames; |
| 299 | } |
| 300 | redraw = 1; |
| 301 | } |
| 302 | |
| 303 | // move the progress bar forward on timed intervals, if configured |
| 304 | int duration = progressScopeDuration; |
| 305 | if (progressBarType == DETERMINATE && duration > 0) { |
| 306 | double elapsed = now() - progressScopeTime; |
| 307 | float p = 1.0 * elapsed / duration; |
| 308 | if (p > 1.0) p = 1.0; |
| 309 | if (p > progress) { |
| 310 | progress = p; |
| 311 | redraw = 1; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (redraw) |
| 316 | update_screen_locked(); |
| 317 | |
| 318 | pthread_mutex_unlock(&updateMutex); |
| 319 | double end = now(); |
| 320 | // minimum of 20ms delay between frames |
| 321 | double delay = interval - (end-start); |
| 322 | if (delay < 0.02) delay = 0.02; |
| 323 | usleep((long)(delay * 1000000)); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | void WearRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) { |
| 328 | int result = res_create_display_surface(filename, surface); |
| 329 | if (result < 0) { |
| 330 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | void WearRecoveryUI::Init() |
| 335 | { |
| 336 | gr_init(); |
| 337 | |
| 338 | gr_font_size(&char_width, &char_height); |
| 339 | |
| 340 | text_col = text_row = 0; |
| 341 | text_rows = (gr_fb_height()) / char_height; |
| 342 | visible_text_rows = (gr_fb_height() - (outer_height * 2)) / char_height; |
| 343 | if (text_rows > kMaxRows) text_rows = kMaxRows; |
| 344 | text_top = 1; |
| 345 | |
| 346 | text_cols = (gr_fb_width() - (outer_width * 2)) / char_width; |
| 347 | if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1; |
| 348 | |
| 349 | LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]); |
| 350 | backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE]; |
| 351 | LoadBitmap("icon_error", &backgroundIcon[ERROR]); |
| 352 | backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; |
| 353 | |
| 354 | introFrames = (GRSurface**)malloc(intro_frames * sizeof(GRSurface*)); |
| 355 | for (int i = 0; i < intro_frames; ++i) { |
| 356 | char filename[40]; |
| 357 | sprintf(filename, "intro%02d", i); |
| 358 | LoadBitmap(filename, introFrames + i); |
| 359 | } |
| 360 | |
| 361 | loopFrames = (GRSurface**)malloc(loop_frames * sizeof(GRSurface*)); |
| 362 | for (int i = 0; i < loop_frames; ++i) { |
| 363 | char filename[40]; |
| 364 | sprintf(filename, "loop%02d", i); |
| 365 | LoadBitmap(filename, loopFrames + i); |
| 366 | } |
| 367 | |
| 368 | pthread_create(&progress_t, NULL, progress_thread, NULL); |
| 369 | RecoveryUI::Init(); |
| 370 | } |
| 371 | |
| 372 | void WearRecoveryUI::SetLocale(const char* locale) { |
| 373 | if (locale) { |
| 374 | char* lang = strdup(locale); |
| 375 | for (char* p = lang; *p; ++p) { |
| 376 | if (*p == '_') { |
| 377 | *p = '\0'; |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | // A bit cheesy: keep an explicit list of supported languages |
| 383 | // that are RTL. |
| 384 | if (strcmp(lang, "ar") == 0 || // Arabic |
| 385 | strcmp(lang, "fa") == 0 || // Persian (Farsi) |
| 386 | strcmp(lang, "he") == 0 || // Hebrew (new language code) |
| 387 | strcmp(lang, "iw") == 0 || // Hebrew (old language code) |
| 388 | strcmp(lang, "ur") == 0) { // Urdu |
| 389 | rtl_locale = true; |
| 390 | } |
| 391 | free(lang); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | void WearRecoveryUI::SetBackground(Icon icon) |
| 396 | { |
| 397 | pthread_mutex_lock(&updateMutex); |
| 398 | currentIcon = icon; |
| 399 | update_screen_locked(); |
| 400 | pthread_mutex_unlock(&updateMutex); |
| 401 | } |
| 402 | |
| 403 | void WearRecoveryUI::SetProgressType(ProgressType type) |
| 404 | { |
| 405 | pthread_mutex_lock(&updateMutex); |
| 406 | if (progressBarType != type) { |
| 407 | progressBarType = type; |
| 408 | } |
| 409 | progressScopeStart = 0; |
| 410 | progressScopeSize = 0; |
| 411 | progress = 0; |
| 412 | update_screen_locked(); |
| 413 | pthread_mutex_unlock(&updateMutex); |
| 414 | } |
| 415 | |
| 416 | void WearRecoveryUI::ShowProgress(float portion, float seconds) |
| 417 | { |
| 418 | pthread_mutex_lock(&updateMutex); |
| 419 | progressBarType = DETERMINATE; |
| 420 | progressScopeStart += progressScopeSize; |
| 421 | progressScopeSize = portion; |
| 422 | progressScopeTime = now(); |
| 423 | progressScopeDuration = seconds; |
| 424 | progress = 0; |
| 425 | update_screen_locked(); |
| 426 | pthread_mutex_unlock(&updateMutex); |
| 427 | } |
| 428 | |
| 429 | void WearRecoveryUI::SetProgress(float fraction) |
| 430 | { |
| 431 | pthread_mutex_lock(&updateMutex); |
| 432 | if (fraction < 0.0) fraction = 0.0; |
| 433 | if (fraction > 1.0) fraction = 1.0; |
| 434 | if (progressBarType == DETERMINATE && fraction > progress) { |
| 435 | // Skip updates that aren't visibly different. |
| 436 | int width = progress_bar_width; |
| 437 | float scale = width * progressScopeSize; |
| 438 | if ((int) (progress * scale) != (int) (fraction * scale)) { |
| 439 | progress = fraction; |
| 440 | update_screen_locked(); |
| 441 | } |
| 442 | } |
| 443 | pthread_mutex_unlock(&updateMutex); |
| 444 | } |
| 445 | |
| 446 | void WearRecoveryUI::SetStage(int current, int max) |
| 447 | { |
| 448 | } |
| 449 | |
| 450 | void WearRecoveryUI::Print(const char *fmt, ...) |
| 451 | { |
| 452 | char buf[256]; |
| 453 | va_list ap; |
| 454 | va_start(ap, fmt); |
| 455 | vsnprintf(buf, 256, fmt, ap); |
| 456 | va_end(ap); |
| 457 | |
| 458 | fputs(buf, stdout); |
| 459 | |
| 460 | // This can get called before ui_init(), so be careful. |
| 461 | pthread_mutex_lock(&updateMutex); |
| 462 | if (text_rows > 0 && text_cols > 0) { |
| 463 | char *ptr; |
| 464 | for (ptr = buf; *ptr != '\0'; ++ptr) { |
| 465 | if (*ptr == '\n' || text_col >= text_cols) { |
| 466 | text[text_row][text_col] = '\0'; |
| 467 | text_col = 0; |
| 468 | text_row = (text_row + 1) % text_rows; |
| 469 | if (text_row == text_top) text_top = (text_top + 1) % text_rows; |
| 470 | } |
| 471 | if (*ptr != '\n') text[text_row][text_col++] = *ptr; |
| 472 | } |
| 473 | text[text_row][text_col] = '\0'; |
| 474 | update_screen_locked(); |
| 475 | } |
| 476 | pthread_mutex_unlock(&updateMutex); |
| 477 | } |
| 478 | |
| 479 | void WearRecoveryUI::StartMenu(const char* const * headers, const char* const * items, |
| 480 | int initial_selection) { |
| 481 | pthread_mutex_lock(&updateMutex); |
| 482 | if (text_rows > 0 && text_cols > 0) { |
| 483 | menu_headers_ = headers; |
| 484 | size_t i = 0; |
| 485 | for (; i < text_rows && items[i] != nullptr; i++) { |
| 486 | strncpy(menu[i], items[i], text_cols - 1); |
| 487 | menu[i][text_cols - 1] = '\0'; |
| 488 | } |
| 489 | menu_items = i; |
| 490 | show_menu = 1; |
| 491 | menu_sel = initial_selection; |
| 492 | menu_start = 0; |
| 493 | menu_end = visible_text_rows - 1 - menu_unusable_rows; |
| 494 | if (menu_items <= menu_end) |
| 495 | menu_end = menu_items; |
| 496 | update_screen_locked(); |
| 497 | } |
| 498 | pthread_mutex_unlock(&updateMutex); |
| 499 | } |
| 500 | |
| 501 | int WearRecoveryUI::SelectMenu(int sel) { |
| 502 | int old_sel; |
| 503 | pthread_mutex_lock(&updateMutex); |
| 504 | if (show_menu > 0) { |
| 505 | old_sel = menu_sel; |
| 506 | menu_sel = sel; |
| 507 | if (menu_sel < 0) menu_sel = 0; |
| 508 | if (menu_sel >= menu_items) menu_sel = menu_items-1; |
| 509 | if (menu_sel < menu_start) { |
| 510 | menu_start--; |
| 511 | menu_end--; |
| 512 | } else if (menu_sel >= menu_end && menu_sel < menu_items) { |
| 513 | menu_end++; |
| 514 | menu_start++; |
| 515 | } |
| 516 | sel = menu_sel; |
| 517 | if (menu_sel != old_sel) update_screen_locked(); |
| 518 | } |
| 519 | pthread_mutex_unlock(&updateMutex); |
| 520 | return sel; |
| 521 | } |
| 522 | |
| 523 | void WearRecoveryUI::EndMenu() { |
| 524 | int i; |
| 525 | pthread_mutex_lock(&updateMutex); |
| 526 | if (show_menu > 0 && text_rows > 0 && text_cols > 0) { |
| 527 | show_menu = 0; |
| 528 | update_screen_locked(); |
| 529 | } |
| 530 | pthread_mutex_unlock(&updateMutex); |
| 531 | } |
| 532 | |
| 533 | bool WearRecoveryUI::IsTextVisible() |
| 534 | { |
| 535 | pthread_mutex_lock(&updateMutex); |
| 536 | int visible = show_text; |
| 537 | pthread_mutex_unlock(&updateMutex); |
| 538 | return visible; |
| 539 | } |
| 540 | |
| 541 | bool WearRecoveryUI::WasTextEverVisible() |
| 542 | { |
| 543 | pthread_mutex_lock(&updateMutex); |
| 544 | int ever_visible = show_text_ever; |
| 545 | pthread_mutex_unlock(&updateMutex); |
| 546 | return ever_visible; |
| 547 | } |
| 548 | |
| 549 | void WearRecoveryUI::ShowText(bool visible) |
| 550 | { |
| 551 | pthread_mutex_lock(&updateMutex); |
| 552 | // Don't show text during ota install or factory reset |
| 553 | if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) { |
| 554 | pthread_mutex_unlock(&updateMutex); |
| 555 | return; |
| 556 | } |
| 557 | show_text = visible; |
| 558 | if (show_text) show_text_ever = 1; |
| 559 | update_screen_locked(); |
| 560 | pthread_mutex_unlock(&updateMutex); |
| 561 | } |
| 562 | |
| 563 | void WearRecoveryUI::Redraw() |
| 564 | { |
| 565 | pthread_mutex_lock(&updateMutex); |
| 566 | update_screen_locked(); |
| 567 | pthread_mutex_unlock(&updateMutex); |
| 568 | } |
| 569 | |
| 570 | void WearRecoveryUI::ShowFile(FILE* fp) { |
| 571 | std::vector<long> offsets; |
| 572 | offsets.push_back(ftell(fp)); |
| 573 | ClearText(); |
| 574 | |
| 575 | struct stat sb; |
| 576 | fstat(fileno(fp), &sb); |
| 577 | |
| 578 | bool show_prompt = false; |
| 579 | while (true) { |
| 580 | if (show_prompt) { |
| 581 | Print("--(%d%% of %d bytes)--", |
| 582 | static_cast<int>(100 * (double(ftell(fp)) / double(sb.st_size))), |
| 583 | static_cast<int>(sb.st_size)); |
| 584 | Redraw(); |
| 585 | while (show_prompt) { |
| 586 | show_prompt = false; |
| 587 | int key = WaitKey(); |
| 588 | if (key == KEY_POWER || key == KEY_ENTER) { |
| 589 | return; |
| 590 | } else if (key == KEY_UP || key == KEY_VOLUMEUP) { |
| 591 | if (offsets.size() <= 1) { |
| 592 | show_prompt = true; |
| 593 | } else { |
| 594 | offsets.pop_back(); |
| 595 | fseek(fp, offsets.back(), SEEK_SET); |
| 596 | } |
| 597 | } else { |
| 598 | if (feof(fp)) { |
| 599 | return; |
| 600 | } |
| 601 | offsets.push_back(ftell(fp)); |
| 602 | } |
| 603 | } |
| 604 | ClearText(); |
| 605 | } |
| 606 | |
| 607 | int ch = getc(fp); |
| 608 | if (ch == EOF) { |
| 609 | text_row = text_top = text_rows - 2; |
| 610 | show_prompt = true; |
| 611 | } else { |
| 612 | PutChar(ch); |
| 613 | if (text_col == 0 && text_row >= text_rows - 2) { |
| 614 | text_top = text_row; |
| 615 | show_prompt = true; |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | void WearRecoveryUI::PutChar(char ch) { |
| 622 | pthread_mutex_lock(&updateMutex); |
| 623 | if (ch != '\n') text[text_row][text_col++] = ch; |
| 624 | if (ch == '\n' || text_col >= text_cols) { |
| 625 | text_col = 0; |
| 626 | ++text_row; |
| 627 | } |
| 628 | pthread_mutex_unlock(&updateMutex); |
| 629 | } |
| 630 | |
| 631 | void WearRecoveryUI::ShowFile(const char* filename) { |
| 632 | FILE* fp = fopen_path(filename, "re"); |
| 633 | if (fp == nullptr) { |
| 634 | Print(" Unable to open %s: %s\n", filename, strerror(errno)); |
| 635 | return; |
| 636 | } |
| 637 | ShowFile(fp); |
| 638 | fclose(fp); |
| 639 | } |
| 640 | |
| 641 | void WearRecoveryUI::ClearText() { |
| 642 | pthread_mutex_lock(&updateMutex); |
| 643 | text_col = 0; |
| 644 | text_row = 0; |
| 645 | text_top = 1; |
| 646 | for (size_t i = 0; i < text_rows; ++i) { |
| 647 | memset(text[i], 0, text_cols + 1); |
| 648 | } |
| 649 | pthread_mutex_unlock(&updateMutex); |
| 650 | } |