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 <dirent.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <linux/input.h>
21 #include <pthread.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <time.h>
30 #include <unistd.h>
31
32 #include <vector>
33
34 #include <android-base/strings.h>
35 #include <android-base/stringprintf.h>
36 #include <cutils/properties.h>
37
38 #include "common.h"
39 #include "device.h"
40 #include "minui/minui.h"
41 #include "screen_ui.h"
42 #include "ui.h"
43
44 #define TEXT_INDENT 4
45
46 // Return the current time as a double (including fractions of a second).
now()47 static double now() {
48 struct timeval tv;
49 gettimeofday(&tv, nullptr);
50 return tv.tv_sec + tv.tv_usec / 1000000.0;
51 }
52
ScreenRecoveryUI()53 ScreenRecoveryUI::ScreenRecoveryUI() :
54 currentIcon(NONE),
55 locale(nullptr),
56 intro_done(false),
57 current_frame(0),
58 progressBarType(EMPTY),
59 progressScopeStart(0),
60 progressScopeSize(0),
61 progress(0),
62 pagesIdentical(false),
63 text_cols_(0),
64 text_rows_(0),
65 text_(nullptr),
66 text_col_(0),
67 text_row_(0),
68 text_top_(0),
69 show_text(false),
70 show_text_ever(false),
71 menu_(nullptr),
72 show_menu(false),
73 menu_items(0),
74 menu_sel(0),
75 file_viewer_text_(nullptr),
76 intro_frames(0),
77 loop_frames(0),
78 animation_fps(30), // TODO: there's currently no way to infer this.
79 stage(-1),
80 max_stage(-1),
81 updateMutex(PTHREAD_MUTEX_INITIALIZER),
82 rtl_locale(false) {
83 }
84
GetCurrentFrame()85 GRSurface* ScreenRecoveryUI::GetCurrentFrame() {
86 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
87 return intro_done ? loopFrames[current_frame] : introFrames[current_frame];
88 }
89 return error_icon;
90 }
91
GetCurrentText()92 GRSurface* ScreenRecoveryUI::GetCurrentText() {
93 switch (currentIcon) {
94 case ERASING: return erasing_text;
95 case ERROR: return error_text;
96 case INSTALLING_UPDATE: return installing_text;
97 case NO_COMMAND: return no_command_text;
98 case NONE: abort();
99 }
100 }
101
PixelsFromDp(int dp)102 int ScreenRecoveryUI::PixelsFromDp(int dp) {
103 return dp * density_;
104 }
105
106 // Here's the intended layout:
107
108 // | regular large
109 // ---------+--------------------
110 // | 220dp 366dp
111 // icon | (200dp) (200dp)
112 // | 68dp 68dp
113 // text | (14sp) (14sp)
114 // | 32dp 32dp
115 // progress | (2dp) (2dp)
116 // | 194dp 340dp
117
118 // Note that "baseline" is actually the *top* of each icon (because that's how our drawing
119 // routines work), so that's the more useful measurement for calling code.
120
GetAnimationBaseline()121 int ScreenRecoveryUI::GetAnimationBaseline() {
122 return GetTextBaseline() - PixelsFromDp(68) - gr_get_height(loopFrames[0]);
123 }
124
GetTextBaseline()125 int ScreenRecoveryUI::GetTextBaseline() {
126 return GetProgressBaseline() - PixelsFromDp(32) - gr_get_height(installing_text);
127 }
128
GetProgressBaseline()129 int ScreenRecoveryUI::GetProgressBaseline() {
130 return gr_fb_height() - PixelsFromDp(is_large_ ? 340 : 194) - gr_get_height(progressBarFill);
131 }
132
133 // Clear the screen and draw the currently selected background icon (if any).
134 // Should only be called with updateMutex locked.
draw_background_locked()135 void ScreenRecoveryUI::draw_background_locked() {
136 pagesIdentical = false;
137 gr_color(0, 0, 0, 255);
138 gr_clear();
139
140 if (currentIcon != NONE) {
141 if (max_stage != -1) {
142 int stage_height = gr_get_height(stageMarkerEmpty);
143 int stage_width = gr_get_width(stageMarkerEmpty);
144 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
145 int y = gr_fb_height() - stage_height;
146 for (int i = 0; i < max_stage; ++i) {
147 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
148 gr_blit(stage_surface, 0, 0, stage_width, stage_height, x, y);
149 x += stage_width;
150 }
151 }
152
153 GRSurface* text_surface = GetCurrentText();
154 int text_x = (gr_fb_width() - gr_get_width(text_surface)) / 2;
155 int text_y = GetTextBaseline();
156 gr_color(255, 255, 255, 255);
157 gr_texticon(text_x, text_y, text_surface);
158 }
159 }
160
161 // Draws the animation and progress bar (if any) on the screen.
162 // Does not flip pages.
163 // Should only be called with updateMutex locked.
draw_foreground_locked()164 void ScreenRecoveryUI::draw_foreground_locked() {
165 if (currentIcon != NONE) {
166 GRSurface* frame = GetCurrentFrame();
167 int frame_width = gr_get_width(frame);
168 int frame_height = gr_get_height(frame);
169 int frame_x = (gr_fb_width() - frame_width) / 2;
170 int frame_y = GetAnimationBaseline();
171 gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
172 }
173
174 if (progressBarType != EMPTY) {
175 int width = gr_get_width(progressBarEmpty);
176 int height = gr_get_height(progressBarEmpty);
177
178 int progress_x = (gr_fb_width() - width)/2;
179 int progress_y = GetProgressBaseline();
180
181 // Erase behind the progress bar (in case this was a progress-only update)
182 gr_color(0, 0, 0, 255);
183 gr_fill(progress_x, progress_y, width, height);
184
185 if (progressBarType == DETERMINATE) {
186 float p = progressScopeStart + progress * progressScopeSize;
187 int pos = (int) (p * width);
188
189 if (rtl_locale) {
190 // Fill the progress bar from right to left.
191 if (pos > 0) {
192 gr_blit(progressBarFill, width-pos, 0, pos, height,
193 progress_x+width-pos, progress_y);
194 }
195 if (pos < width-1) {
196 gr_blit(progressBarEmpty, 0, 0, width-pos, height, progress_x, progress_y);
197 }
198 } else {
199 // Fill the progress bar from left to right.
200 if (pos > 0) {
201 gr_blit(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
202 }
203 if (pos < width-1) {
204 gr_blit(progressBarEmpty, pos, 0, width-pos, height,
205 progress_x+pos, progress_y);
206 }
207 }
208 }
209 }
210 }
211
SetColor(UIElement e)212 void ScreenRecoveryUI::SetColor(UIElement e) {
213 switch (e) {
214 case INFO:
215 gr_color(249, 194, 0, 255);
216 break;
217 case HEADER:
218 gr_color(247, 0, 6, 255);
219 break;
220 case MENU:
221 case MENU_SEL_BG:
222 gr_color(0, 106, 157, 255);
223 break;
224 case MENU_SEL_BG_ACTIVE:
225 gr_color(0, 156, 100, 255);
226 break;
227 case MENU_SEL_FG:
228 gr_color(255, 255, 255, 255);
229 break;
230 case LOG:
231 gr_color(196, 196, 196, 255);
232 break;
233 case TEXT_FILL:
234 gr_color(0, 0, 0, 160);
235 break;
236 default:
237 gr_color(255, 255, 255, 255);
238 break;
239 }
240 }
241
DrawHorizontalRule(int * y)242 void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
243 SetColor(MENU);
244 *y += 4;
245 gr_fill(0, *y, gr_fb_width(), *y + 2);
246 *y += 4;
247 }
248
DrawTextLine(int x,int * y,const char * line,bool bold)249 void ScreenRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) {
250 gr_text(x, *y, line, bold);
251 *y += char_height_ + 4;
252 }
253
DrawTextLines(int x,int * y,const char * const * lines)254 void ScreenRecoveryUI::DrawTextLines(int x, int* y, const char* const* lines) {
255 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
256 DrawTextLine(x, y, lines[i], false);
257 }
258 }
259
260 static const char* REGULAR_HELP[] = {
261 "Use volume up/down and power.",
262 NULL
263 };
264
265 static const char* LONG_PRESS_HELP[] = {
266 "Any button cycles highlight.",
267 "Long-press activates.",
268 NULL
269 };
270
271 // Redraw everything on the screen. Does not flip pages.
272 // Should only be called with updateMutex locked.
draw_screen_locked()273 void ScreenRecoveryUI::draw_screen_locked() {
274 if (!show_text) {
275 draw_background_locked();
276 draw_foreground_locked();
277 } else {
278 gr_color(0, 0, 0, 255);
279 gr_clear();
280
281 int y = 0;
282 if (show_menu) {
283 char recovery_fingerprint[PROPERTY_VALUE_MAX];
284 property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, "");
285
286 SetColor(INFO);
287 DrawTextLine(TEXT_INDENT, &y, "Android Recovery", true);
288 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
289 DrawTextLine(TEXT_INDENT, &y, chunk.c_str(), false);
290 }
291 DrawTextLines(TEXT_INDENT, &y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
292
293 SetColor(HEADER);
294 DrawTextLines(TEXT_INDENT, &y, menu_headers_);
295
296 SetColor(MENU);
297 DrawHorizontalRule(&y);
298 y += 4;
299 for (int i = 0; i < menu_items; ++i) {
300 if (i == menu_sel) {
301 // Draw the highlight bar.
302 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
303 gr_fill(0, y - 2, gr_fb_width(), y + char_height_ + 2);
304 // Bold white text for the selected item.
305 SetColor(MENU_SEL_FG);
306 gr_text(4, y, menu_[i], true);
307 SetColor(MENU);
308 } else {
309 gr_text(4, y, menu_[i], false);
310 }
311 y += char_height_ + 4;
312 }
313 DrawHorizontalRule(&y);
314 }
315
316 // display from the bottom up, until we hit the top of the
317 // screen, the bottom of the menu, or we've displayed the
318 // entire text buffer.
319 SetColor(LOG);
320 int row = (text_top_ + text_rows_ - 1) % text_rows_;
321 size_t count = 0;
322 for (int ty = gr_fb_height() - char_height_;
323 ty >= y && count < text_rows_;
324 ty -= char_height_, ++count) {
325 gr_text(0, ty, text_[row], false);
326 --row;
327 if (row < 0) row = text_rows_ - 1;
328 }
329 }
330 }
331
332 // Redraw everything on the screen and flip the screen (make it visible).
333 // Should only be called with updateMutex locked.
update_screen_locked()334 void ScreenRecoveryUI::update_screen_locked() {
335 draw_screen_locked();
336 gr_flip();
337 }
338
339 // Updates only the progress bar, if possible, otherwise redraws the screen.
340 // Should only be called with updateMutex locked.
update_progress_locked()341 void ScreenRecoveryUI::update_progress_locked() {
342 if (show_text || !pagesIdentical) {
343 draw_screen_locked(); // Must redraw the whole screen
344 pagesIdentical = true;
345 } else {
346 draw_foreground_locked(); // Draw only the progress bar and overlays
347 }
348 gr_flip();
349 }
350
351 // Keeps the progress bar updated, even when the process is otherwise busy.
ProgressThreadStartRoutine(void * data)352 void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
353 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
354 return nullptr;
355 }
356
ProgressThreadLoop()357 void ScreenRecoveryUI::ProgressThreadLoop() {
358 double interval = 1.0 / animation_fps;
359 while (true) {
360 double start = now();
361 pthread_mutex_lock(&updateMutex);
362
363 bool redraw = false;
364
365 // update the installation animation, if active
366 // skip this if we have a text overlay (too expensive to update)
367 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
368 if (!intro_done) {
369 if (current_frame == intro_frames - 1) {
370 intro_done = true;
371 current_frame = 0;
372 } else {
373 ++current_frame;
374 }
375 } else {
376 current_frame = (current_frame + 1) % loop_frames;
377 }
378
379 redraw = true;
380 }
381
382 // move the progress bar forward on timed intervals, if configured
383 int duration = progressScopeDuration;
384 if (progressBarType == DETERMINATE && duration > 0) {
385 double elapsed = now() - progressScopeTime;
386 float p = 1.0 * elapsed / duration;
387 if (p > 1.0) p = 1.0;
388 if (p > progress) {
389 progress = p;
390 redraw = true;
391 }
392 }
393
394 if (redraw) update_progress_locked();
395
396 pthread_mutex_unlock(&updateMutex);
397 double end = now();
398 // minimum of 20ms delay between frames
399 double delay = interval - (end-start);
400 if (delay < 0.02) delay = 0.02;
401 usleep((long)(delay * 1000000));
402 }
403 }
404
LoadBitmap(const char * filename,GRSurface ** surface)405 void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
406 int result = res_create_display_surface(filename, surface);
407 if (result < 0) {
408 LOGE("couldn't load bitmap %s (error %d)\n", filename, result);
409 }
410 }
411
LoadLocalizedBitmap(const char * filename,GRSurface ** surface)412 void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
413 int result = res_create_localized_alpha_surface(filename, locale, surface);
414 if (result < 0) {
415 LOGE("couldn't load bitmap %s (error %d)\n", filename, result);
416 }
417 }
418
Alloc2d(size_t rows,size_t cols)419 static char** Alloc2d(size_t rows, size_t cols) {
420 char** result = new char*[rows];
421 for (size_t i = 0; i < rows; ++i) {
422 result[i] = new char[cols];
423 memset(result[i], 0, cols);
424 }
425 return result;
426 }
427
428 // Choose the right background string to display during update.
SetSystemUpdateText(bool security_update)429 void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
430 if (security_update) {
431 LoadLocalizedBitmap("installing_security_text", &installing_text);
432 } else {
433 LoadLocalizedBitmap("installing_text", &installing_text);
434 }
435 Redraw();
436 }
437
Init()438 void ScreenRecoveryUI::Init() {
439 gr_init();
440
441 density_ = static_cast<float>(property_get_int32("ro.sf.lcd_density", 160)) / 160.f;
442 is_large_ = gr_fb_height() > PixelsFromDp(800);
443
444 gr_font_size(&char_width_, &char_height_);
445 text_rows_ = gr_fb_height() / char_height_;
446 text_cols_ = gr_fb_width() / char_width_;
447
448 text_ = Alloc2d(text_rows_, text_cols_ + 1);
449 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
450 menu_ = Alloc2d(text_rows_, text_cols_ + 1);
451
452 text_col_ = text_row_ = 0;
453 text_top_ = 1;
454
455 LoadBitmap("icon_error", &error_icon);
456
457 LoadBitmap("progress_empty", &progressBarEmpty);
458 LoadBitmap("progress_fill", &progressBarFill);
459
460 LoadBitmap("stage_empty", &stageMarkerEmpty);
461 LoadBitmap("stage_fill", &stageMarkerFill);
462
463 // Background text for "installing_update" could be "installing update"
464 // or "installing security update". It will be set after UI init according
465 // to commands in BCB.
466 installing_text = nullptr;
467 LoadLocalizedBitmap("erasing_text", &erasing_text);
468 LoadLocalizedBitmap("no_command_text", &no_command_text);
469 LoadLocalizedBitmap("error_text", &error_text);
470
471 LoadAnimation();
472
473 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
474
475 RecoveryUI::Init();
476 }
477
LoadAnimation()478 void ScreenRecoveryUI::LoadAnimation() {
479 // How many frames of intro and loop do we have?
480 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
481 dirent* de;
482 while ((de = readdir(dir.get())) != nullptr) {
483 int value;
484 if (sscanf(de->d_name, "intro%d", &value) == 1 && intro_frames < (value + 1)) {
485 intro_frames = value + 1;
486 } else if (sscanf(de->d_name, "loop%d", &value) == 1 && loop_frames < (value + 1)) {
487 loop_frames = value + 1;
488 }
489 }
490
491 // It's okay to not have an intro.
492 if (intro_frames == 0) intro_done = true;
493 // But you must have an animation.
494 if (loop_frames == 0) abort();
495
496 introFrames = new GRSurface*[intro_frames];
497 for (int i = 0; i < intro_frames; ++i) {
498 // TODO: remember the names above, so we don't have to hard-code the number of 0s.
499 LoadBitmap(android::base::StringPrintf("intro%05d", i).c_str(), &introFrames[i]);
500 }
501
502 loopFrames = new GRSurface*[loop_frames];
503 for (int i = 0; i < loop_frames; ++i) {
504 LoadBitmap(android::base::StringPrintf("loop%05d", i).c_str(), &loopFrames[i]);
505 }
506 }
507
SetLocale(const char * new_locale)508 void ScreenRecoveryUI::SetLocale(const char* new_locale) {
509 this->locale = new_locale;
510 this->rtl_locale = false;
511
512 if (locale) {
513 char* lang = strdup(locale);
514 for (char* p = lang; *p; ++p) {
515 if (*p == '_') {
516 *p = '\0';
517 break;
518 }
519 }
520
521 // A bit cheesy: keep an explicit list of supported RTL languages.
522 if (strcmp(lang, "ar") == 0 || // Arabic
523 strcmp(lang, "fa") == 0 || // Persian (Farsi)
524 strcmp(lang, "he") == 0 || // Hebrew (new language code)
525 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
526 strcmp(lang, "ur") == 0) { // Urdu
527 rtl_locale = true;
528 }
529 free(lang);
530 }
531 }
532
SetBackground(Icon icon)533 void ScreenRecoveryUI::SetBackground(Icon icon) {
534 pthread_mutex_lock(&updateMutex);
535
536 currentIcon = icon;
537 update_screen_locked();
538
539 pthread_mutex_unlock(&updateMutex);
540 }
541
SetProgressType(ProgressType type)542 void ScreenRecoveryUI::SetProgressType(ProgressType type) {
543 pthread_mutex_lock(&updateMutex);
544 if (progressBarType != type) {
545 progressBarType = type;
546 }
547 progressScopeStart = 0;
548 progressScopeSize = 0;
549 progress = 0;
550 update_progress_locked();
551 pthread_mutex_unlock(&updateMutex);
552 }
553
ShowProgress(float portion,float seconds)554 void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
555 pthread_mutex_lock(&updateMutex);
556 progressBarType = DETERMINATE;
557 progressScopeStart += progressScopeSize;
558 progressScopeSize = portion;
559 progressScopeTime = now();
560 progressScopeDuration = seconds;
561 progress = 0;
562 update_progress_locked();
563 pthread_mutex_unlock(&updateMutex);
564 }
565
SetProgress(float fraction)566 void ScreenRecoveryUI::SetProgress(float fraction) {
567 pthread_mutex_lock(&updateMutex);
568 if (fraction < 0.0) fraction = 0.0;
569 if (fraction > 1.0) fraction = 1.0;
570 if (progressBarType == DETERMINATE && fraction > progress) {
571 // Skip updates that aren't visibly different.
572 int width = gr_get_width(progressBarEmpty);
573 float scale = width * progressScopeSize;
574 if ((int) (progress * scale) != (int) (fraction * scale)) {
575 progress = fraction;
576 update_progress_locked();
577 }
578 }
579 pthread_mutex_unlock(&updateMutex);
580 }
581
SetStage(int current,int max)582 void ScreenRecoveryUI::SetStage(int current, int max) {
583 pthread_mutex_lock(&updateMutex);
584 stage = current;
585 max_stage = max;
586 pthread_mutex_unlock(&updateMutex);
587 }
588
PrintV(const char * fmt,bool copy_to_stdout,va_list ap)589 void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
590 std::string str;
591 android::base::StringAppendV(&str, fmt, ap);
592
593 if (copy_to_stdout) {
594 fputs(str.c_str(), stdout);
595 }
596
597 pthread_mutex_lock(&updateMutex);
598 if (text_rows_ > 0 && text_cols_ > 0) {
599 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
600 if (*ptr == '\n' || text_col_ >= text_cols_) {
601 text_[text_row_][text_col_] = '\0';
602 text_col_ = 0;
603 text_row_ = (text_row_ + 1) % text_rows_;
604 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
605 }
606 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
607 }
608 text_[text_row_][text_col_] = '\0';
609 update_screen_locked();
610 }
611 pthread_mutex_unlock(&updateMutex);
612 }
613
Print(const char * fmt,...)614 void ScreenRecoveryUI::Print(const char* fmt, ...) {
615 va_list ap;
616 va_start(ap, fmt);
617 PrintV(fmt, true, ap);
618 va_end(ap);
619 }
620
PrintOnScreenOnly(const char * fmt,...)621 void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
622 va_list ap;
623 va_start(ap, fmt);
624 PrintV(fmt, false, ap);
625 va_end(ap);
626 }
627
PutChar(char ch)628 void ScreenRecoveryUI::PutChar(char ch) {
629 pthread_mutex_lock(&updateMutex);
630 if (ch != '\n') text_[text_row_][text_col_++] = ch;
631 if (ch == '\n' || text_col_ >= text_cols_) {
632 text_col_ = 0;
633 ++text_row_;
634
635 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
636 }
637 pthread_mutex_unlock(&updateMutex);
638 }
639
ClearText()640 void ScreenRecoveryUI::ClearText() {
641 pthread_mutex_lock(&updateMutex);
642 text_col_ = 0;
643 text_row_ = 0;
644 text_top_ = 1;
645 for (size_t i = 0; i < text_rows_; ++i) {
646 memset(text_[i], 0, text_cols_ + 1);
647 }
648 pthread_mutex_unlock(&updateMutex);
649 }
650
ShowFile(FILE * fp)651 void ScreenRecoveryUI::ShowFile(FILE* fp) {
652 std::vector<long> offsets;
653 offsets.push_back(ftell(fp));
654 ClearText();
655
656 struct stat sb;
657 fstat(fileno(fp), &sb);
658
659 bool show_prompt = false;
660 while (true) {
661 if (show_prompt) {
662 PrintOnScreenOnly("--(%d%% of %d bytes)--",
663 static_cast<int>(100 * (double(ftell(fp)) / double(sb.st_size))),
664 static_cast<int>(sb.st_size));
665 Redraw();
666 while (show_prompt) {
667 show_prompt = false;
668 int key = WaitKey();
669 if (key == KEY_POWER || key == KEY_ENTER) {
670 return;
671 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
672 if (offsets.size() <= 1) {
673 show_prompt = true;
674 } else {
675 offsets.pop_back();
676 fseek(fp, offsets.back(), SEEK_SET);
677 }
678 } else {
679 if (feof(fp)) {
680 return;
681 }
682 offsets.push_back(ftell(fp));
683 }
684 }
685 ClearText();
686 }
687
688 int ch = getc(fp);
689 if (ch == EOF) {
690 while (text_row_ < text_rows_ - 1) PutChar('\n');
691 show_prompt = true;
692 } else {
693 PutChar(ch);
694 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
695 show_prompt = true;
696 }
697 }
698 }
699 }
700
ShowFile(const char * filename)701 void ScreenRecoveryUI::ShowFile(const char* filename) {
702 FILE* fp = fopen_path(filename, "re");
703 if (fp == nullptr) {
704 Print(" Unable to open %s: %s\n", filename, strerror(errno));
705 return;
706 }
707
708 char** old_text = text_;
709 size_t old_text_col = text_col_;
710 size_t old_text_row = text_row_;
711 size_t old_text_top = text_top_;
712
713 // Swap in the alternate screen and clear it.
714 text_ = file_viewer_text_;
715 ClearText();
716
717 ShowFile(fp);
718 fclose(fp);
719
720 text_ = old_text;
721 text_col_ = old_text_col;
722 text_row_ = old_text_row;
723 text_top_ = old_text_top;
724 }
725
StartMenu(const char * const * headers,const char * const * items,int initial_selection)726 void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
727 int initial_selection) {
728 pthread_mutex_lock(&updateMutex);
729 if (text_rows_ > 0 && text_cols_ > 0) {
730 menu_headers_ = headers;
731 size_t i = 0;
732 for (; i < text_rows_ && items[i] != nullptr; ++i) {
733 strncpy(menu_[i], items[i], text_cols_ - 1);
734 menu_[i][text_cols_ - 1] = '\0';
735 }
736 menu_items = i;
737 show_menu = true;
738 menu_sel = initial_selection;
739 update_screen_locked();
740 }
741 pthread_mutex_unlock(&updateMutex);
742 }
743
SelectMenu(int sel)744 int ScreenRecoveryUI::SelectMenu(int sel) {
745 pthread_mutex_lock(&updateMutex);
746 if (show_menu) {
747 int old_sel = menu_sel;
748 menu_sel = sel;
749
750 // Wrap at top and bottom.
751 if (menu_sel < 0) menu_sel = menu_items - 1;
752 if (menu_sel >= menu_items) menu_sel = 0;
753
754 sel = menu_sel;
755 if (menu_sel != old_sel) update_screen_locked();
756 }
757 pthread_mutex_unlock(&updateMutex);
758 return sel;
759 }
760
EndMenu()761 void ScreenRecoveryUI::EndMenu() {
762 pthread_mutex_lock(&updateMutex);
763 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
764 show_menu = false;
765 update_screen_locked();
766 }
767 pthread_mutex_unlock(&updateMutex);
768 }
769
IsTextVisible()770 bool ScreenRecoveryUI::IsTextVisible() {
771 pthread_mutex_lock(&updateMutex);
772 int visible = show_text;
773 pthread_mutex_unlock(&updateMutex);
774 return visible;
775 }
776
WasTextEverVisible()777 bool ScreenRecoveryUI::WasTextEverVisible() {
778 pthread_mutex_lock(&updateMutex);
779 int ever_visible = show_text_ever;
780 pthread_mutex_unlock(&updateMutex);
781 return ever_visible;
782 }
783
ShowText(bool visible)784 void ScreenRecoveryUI::ShowText(bool visible) {
785 pthread_mutex_lock(&updateMutex);
786 show_text = visible;
787 if (show_text) show_text_ever = true;
788 update_screen_locked();
789 pthread_mutex_unlock(&updateMutex);
790 }
791
Redraw()792 void ScreenRecoveryUI::Redraw() {
793 pthread_mutex_lock(&updateMutex);
794 update_screen_locked();
795 pthread_mutex_unlock(&updateMutex);
796 }
797
KeyLongPress(int)798 void ScreenRecoveryUI::KeyLongPress(int) {
799 // Redraw so that if we're in the menu, the highlight
800 // will change color to indicate a successful long press.
801 Redraw();
802 }
803