Add an alternate screen for viewing recovery logs.

This makes it easier to go back and forth without losing current output.

Also make the display more like regular more(1).

Bug: http://b/20834540
Change-Id: Icc5703e9c8a378cc7072d8ebb79e34451267ee1b
(cherry picked from commit c049163234)
This commit is contained in:
Elliott Hughes 2015-05-06 12:40:05 -07:00
parent 3e8d28b547
commit df52e1e119
3 changed files with 79 additions and 55 deletions

View file

@ -701,12 +701,11 @@ static bool wipe_cache(bool should_confirm, Device* device) {
} }
static void choose_recovery_file(Device* device) { static void choose_recovery_file(Device* device) {
// "Go back" + KEEP_LOG_COUNT * 2 + terminating nullptr entry // "Back" + KEEP_LOG_COUNT * 2 + terminating nullptr entry
char* entries[KEEP_LOG_COUNT * 2 + 2]; char* entries[1 + KEEP_LOG_COUNT * 2 + 1];
memset(entries, 0, sizeof(entries)); memset(entries, 0, sizeof(entries));
unsigned int n = 0; unsigned int n = 0;
entries[n++] = strdup("Go back");
// Add LAST_LOG_FILE + LAST_LOG_FILE.x // Add LAST_LOG_FILE + LAST_LOG_FILE.x
// Add LAST_KMSG_FILE + LAST_KMSG_FILE.x // Add LAST_KMSG_FILE + LAST_KMSG_FILE.x
@ -734,11 +733,13 @@ static void choose_recovery_file(Device* device) {
} }
} }
entries[n++] = strdup("Back");
const char* headers[] = { "Select file to view", nullptr }; const char* headers[] = { "Select file to view", nullptr };
while (true) { while (true) {
int chosen_item = get_menu_selection(headers, entries, 1, 0, device); int chosen_item = get_menu_selection(headers, entries, 1, 0, device);
if (chosen_item == 0) break; if (strcmp(entries[chosen_item], "Back") == 0) break;
// TODO: do we need to redirect? ShowFile could just avoid writing to stdio. // TODO: do we need to redirect? ShowFile could just avoid writing to stdio.
redirect_stdio("/dev/null"); redirect_stdio("/dev/null");

View file

@ -58,18 +58,19 @@ ScreenRecoveryUI::ScreenRecoveryUI() :
progressScopeSize(0), progressScopeSize(0),
progress(0), progress(0),
pagesIdentical(false), pagesIdentical(false),
text(nullptr), text_cols_(0),
text_cols(0), text_rows_(0),
text_rows(0), text_(nullptr),
text_col(0), text_col_(0),
text_row(0), text_row_(0),
text_top(0), text_top_(0),
show_text(false), show_text(false),
show_text_ever(false), show_text_ever(false),
menu(nullptr), menu_(nullptr),
show_menu(false), show_menu(false),
menu_items(0), menu_items(0),
menu_sel(0), menu_sel(0),
file_viewer_text_(nullptr),
animation_fps(20), animation_fps(20),
installing_frames(-1), installing_frames(-1),
stage(-1), stage(-1),
@ -255,7 +256,7 @@ void ScreenRecoveryUI::draw_screen_locked() {
DrawTextLines(&y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP); DrawTextLines(&y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
SetColor(HEADER); SetColor(HEADER);
DrawTextLines(&y, menu_headers); DrawTextLines(&y, menu_headers_);
SetColor(MENU); SetColor(MENU);
DrawHorizontalRule(&y); DrawHorizontalRule(&y);
@ -267,10 +268,10 @@ void ScreenRecoveryUI::draw_screen_locked() {
gr_fill(0, y - 2, gr_fb_width(), y + char_height + 2); gr_fill(0, y - 2, gr_fb_width(), y + char_height + 2);
// Bold white text for the selected item. // Bold white text for the selected item.
SetColor(MENU_SEL_FG); SetColor(MENU_SEL_FG);
gr_text(4, y, menu[i], true); gr_text(4, y, menu_[i], true);
SetColor(MENU); SetColor(MENU);
} else { } else {
gr_text(4, y, menu[i], false); gr_text(4, y, menu_[i], false);
} }
y += char_height + 4; y += char_height + 4;
} }
@ -281,14 +282,14 @@ void ScreenRecoveryUI::draw_screen_locked() {
// screen, the bottom of the menu, or we've displayed the // screen, the bottom of the menu, or we've displayed the
// entire text buffer. // entire text buffer.
SetColor(LOG); SetColor(LOG);
int row = (text_top+text_rows-1) % text_rows; int row = (text_top_ + text_rows_ - 1) % text_rows_;
size_t count = 0; size_t count = 0;
for (int ty = gr_fb_height() - char_height; for (int ty = gr_fb_height() - char_height;
ty >= y && count < text_rows; ty >= y && count < text_rows_;
ty -= char_height, ++count) { ty -= char_height, ++count) {
gr_text(0, ty, text[row], false); gr_text(0, ty, text_[row], false);
--row; --row;
if (row < 0) row = text_rows-1; if (row < 0) row = text_rows_ - 1;
} }
} }
} }
@ -391,14 +392,15 @@ void ScreenRecoveryUI::Init() {
gr_init(); gr_init();
gr_font_size(&char_width, &char_height); gr_font_size(&char_width, &char_height);
text_rows = gr_fb_height() / char_height; text_rows_ = gr_fb_height() / char_height;
text_cols = gr_fb_width() / char_width; text_cols_ = gr_fb_width() / char_width;
text = Alloc2d(text_rows, text_cols + 1); text_ = Alloc2d(text_rows_, text_cols_ + 1);
menu = Alloc2d(text_rows, text_cols + 1); file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
menu_ = Alloc2d(text_rows_, text_cols_ + 1);
text_col = text_row = 0; text_col_ = text_row_ = 0;
text_top = 1; text_top_ = 1;
backgroundIcon[NONE] = nullptr; backgroundIcon[NONE] = nullptr;
LoadBitmapArray("icon_installing", &installing_frames, &installation); LoadBitmapArray("icon_installing", &installing_frames, &installation);
@ -514,17 +516,17 @@ void ScreenRecoveryUI::Print(const char *fmt, ...) {
fputs(buf, stdout); fputs(buf, stdout);
pthread_mutex_lock(&updateMutex); pthread_mutex_lock(&updateMutex);
if (text_rows > 0 && text_cols > 0) { if (text_rows_ > 0 && text_cols_ > 0) {
for (const char* ptr = buf; *ptr != '\0'; ++ptr) { for (const char* ptr = buf; *ptr != '\0'; ++ptr) {
if (*ptr == '\n' || text_col >= text_cols) { if (*ptr == '\n' || text_col_ >= text_cols_) {
text[text_row][text_col] = '\0'; text_[text_row_][text_col_] = '\0';
text_col = 0; text_col_ = 0;
text_row = (text_row + 1) % text_rows; text_row_ = (text_row_ + 1) % text_rows_;
if (text_row == text_top) text_top = (text_top + 1) % text_rows; if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
} }
if (*ptr != '\n') text[text_row][text_col++] = *ptr; if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
} }
text[text_row][text_col] = '\0'; text_[text_row_][text_col_] = '\0';
update_screen_locked(); update_screen_locked();
} }
pthread_mutex_unlock(&updateMutex); pthread_mutex_unlock(&updateMutex);
@ -532,21 +534,23 @@ void ScreenRecoveryUI::Print(const char *fmt, ...) {
void ScreenRecoveryUI::PutChar(char ch) { void ScreenRecoveryUI::PutChar(char ch) {
pthread_mutex_lock(&updateMutex); pthread_mutex_lock(&updateMutex);
if (ch != '\n') text[text_row][text_col++] = ch; if (ch != '\n') text_[text_row_][text_col_++] = ch;
if (ch == '\n' || text_col >= text_cols) { if (ch == '\n' || text_col_ >= text_cols_) {
text_col = 0; text_col_ = 0;
++text_row; ++text_row_;
if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
} }
pthread_mutex_unlock(&updateMutex); pthread_mutex_unlock(&updateMutex);
} }
void ScreenRecoveryUI::ClearText() { void ScreenRecoveryUI::ClearText() {
pthread_mutex_lock(&updateMutex); pthread_mutex_lock(&updateMutex);
text_col = 0; text_col_ = 0;
text_row = 0; text_row_ = 0;
text_top = 1; text_top_ = 1;
for (size_t i = 0; i < text_rows; ++i) { for (size_t i = 0; i < text_rows_; ++i) {
memset(text[i], 0, text_cols + 1); memset(text_[i], 0, text_cols_ + 1);
} }
pthread_mutex_unlock(&updateMutex); pthread_mutex_unlock(&updateMutex);
} }
@ -590,12 +594,11 @@ void ScreenRecoveryUI::ShowFile(FILE* fp) {
int ch = getc(fp); int ch = getc(fp);
if (ch == EOF) { if (ch == EOF) {
text_row = text_top = text_rows - 2; while (text_row_ < text_rows_ - 1) PutChar('\n');
show_prompt = true; show_prompt = true;
} else { } else {
PutChar(ch); PutChar(ch);
if (text_col == 0 && text_row >= text_rows - 2) { if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
text_top = text_row;
show_prompt = true; show_prompt = true;
} }
} }
@ -608,19 +611,34 @@ void ScreenRecoveryUI::ShowFile(const char* filename) {
Print(" Unable to open %s: %s\n", filename, strerror(errno)); Print(" Unable to open %s: %s\n", filename, strerror(errno));
return; return;
} }
char** old_text = text_;
size_t old_text_col = text_col_;
size_t old_text_row = text_row_;
size_t old_text_top = text_top_;
// Swap in the alternate screen and clear it.
text_ = file_viewer_text_;
ClearText();
ShowFile(fp); ShowFile(fp);
fclose(fp); fclose(fp);
text_ = old_text;
text_col_ = old_text_col;
text_row_ = old_text_row;
text_top_ = old_text_top;
} }
void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items, void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
int initial_selection) { int initial_selection) {
pthread_mutex_lock(&updateMutex); pthread_mutex_lock(&updateMutex);
if (text_rows > 0 && text_cols > 0) { if (text_rows_ > 0 && text_cols_ > 0) {
menu_headers = headers; menu_headers_ = headers;
size_t i = 0; size_t i = 0;
for (; i < text_rows && items[i] != nullptr; ++i) { for (; i < text_rows_ && items[i] != nullptr; ++i) {
strncpy(menu[i], items[i], text_cols-1); strncpy(menu_[i], items[i], text_cols_ - 1);
menu[i][text_cols-1] = '\0'; menu_[i][text_cols_ - 1] = '\0';
} }
menu_items = i; menu_items = i;
show_menu = true; show_menu = true;
@ -649,7 +667,7 @@ int ScreenRecoveryUI::SelectMenu(int sel) {
void ScreenRecoveryUI::EndMenu() { void ScreenRecoveryUI::EndMenu() {
pthread_mutex_lock(&updateMutex); pthread_mutex_lock(&updateMutex);
if (show_menu && text_rows > 0 && text_cols > 0) { if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
show_menu = false; show_menu = false;
update_screen_locked(); update_screen_locked();
} }

View file

@ -89,18 +89,23 @@ class ScreenRecoveryUI : public RecoveryUI {
// true when both graphics pages are the same (except for the progress bar). // true when both graphics pages are the same (except for the progress bar).
bool pagesIdentical; bool pagesIdentical;
size_t text_cols_, text_rows_;
// Log text overlay, displayed when a magic key is pressed. // Log text overlay, displayed when a magic key is pressed.
char** text; char** text_;
size_t text_cols, text_rows; size_t text_col_, text_row_, text_top_;
size_t text_col, text_row, text_top;
bool show_text; bool show_text;
bool show_text_ever; // has show_text ever been true? bool show_text_ever; // has show_text ever been true?
char** menu; char** menu_;
const char* const* menu_headers; const char* const* menu_headers_;
bool show_menu; bool show_menu;
int menu_items, menu_sel; int menu_items, menu_sel;
// An alternate text screen, swapped with 'text_' when we're viewing a log file.
char** file_viewer_text_;
pthread_t progress_thread_; pthread_t progress_thread_;
int animation_fps; int animation_fps;