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> |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 19 | #include <stdarg.h> |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/time.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <time.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include <vector> |
| 29 | |
| 30 | #include "common.h" |
| 31 | #include "device.h" |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 32 | #include "wear_ui.h" |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 33 | #include "android-base/properties.h" |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 34 | #include "android-base/strings.h" |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 35 | #include "android-base/stringprintf.h" |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 36 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 37 | // There's only (at most) one of these objects, and global callbacks |
| 38 | // (for pthread_create, and the input event system) need to find it, |
| 39 | // so use a global variable. |
| 40 | static WearRecoveryUI* self = NULL; |
| 41 | |
| 42 | // Return the current time as a double (including fractions of a second). |
| 43 | static double now() { |
| 44 | struct timeval tv; |
| 45 | gettimeofday(&tv, NULL); |
| 46 | return tv.tv_sec + tv.tv_usec / 1000000.0; |
| 47 | } |
| 48 | |
| 49 | WearRecoveryUI::WearRecoveryUI() : |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 50 | progress_bar_y(259), |
| 51 | outer_height(0), |
| 52 | outer_width(0), |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 53 | menu_unusable_rows(0) { |
| 54 | intro_frames = 22; |
| 55 | loop_frames = 60; |
| 56 | animation_fps = 30; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 57 | |
| 58 | for (size_t i = 0; i < 5; i++) |
| 59 | backgroundIcon[i] = NULL; |
| 60 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 61 | self = this; |
| 62 | } |
| 63 | |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 64 | int WearRecoveryUI::GetProgressBaseline() { |
| 65 | return progress_bar_y; |
| 66 | } |
| 67 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 68 | // Draw background frame on the screen. Does not flip pages. |
| 69 | // Should only be called with updateMutex locked. |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 70 | // TODO merge drawing routines with screen_ui |
| 71 | void WearRecoveryUI::draw_background_locked() |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 72 | { |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 73 | pagesIdentical = false; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 74 | gr_color(0, 0, 0, 255); |
| 75 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 76 | |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 77 | if (currentIcon != NONE) { |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 78 | GRSurface* surface; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 79 | if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) { |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 80 | if (!intro_done) { |
| 81 | surface = introFrames[current_frame]; |
| 82 | } else { |
| 83 | surface = loopFrames[current_frame]; |
| 84 | } |
| 85 | } |
| 86 | else { |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 87 | surface = backgroundIcon[currentIcon]; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | int width = gr_get_width(surface); |
| 91 | int height = gr_get_height(surface); |
| 92 | |
| 93 | int x = (gr_fb_width() - width) / 2; |
| 94 | int y = (gr_fb_height() - height) / 2; |
| 95 | |
| 96 | gr_blit(surface, 0, 0, width, height, x, y); |
| 97 | } |
| 98 | } |
| 99 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 100 | static const char* HEADERS[] = { |
| 101 | "Swipe up/down to move.", |
| 102 | "Swipe left/right to select.", |
| 103 | "", |
| 104 | NULL |
| 105 | }; |
| 106 | |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 107 | // TODO merge drawing routines with screen_ui |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 108 | void WearRecoveryUI::draw_screen_locked() |
| 109 | { |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 110 | char cur_selection_str[50]; |
| 111 | |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 112 | draw_background_locked(); |
| 113 | if (!show_text) { |
| 114 | draw_foreground_locked(); |
| 115 | } else { |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 116 | SetColor(TEXT_FILL); |
| 117 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 118 | |
| 119 | int y = outer_height; |
| 120 | int x = outer_width; |
| 121 | if (show_menu) { |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 122 | std::string recovery_fingerprint = |
| 123 | android::base::GetProperty("ro.bootimage.build.fingerprint", ""); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 124 | SetColor(HEADER); |
| 125 | DrawTextLine(x + 4, &y, "Android Recovery", true); |
| 126 | for (auto& chunk: android::base::Split(recovery_fingerprint, ":")) { |
| 127 | DrawTextLine(x +4, &y, chunk.c_str(), false); |
| 128 | } |
| 129 | |
| 130 | // This is actually the help strings. |
| 131 | DrawTextLines(x + 4, &y, HEADERS); |
| 132 | SetColor(HEADER); |
| 133 | DrawTextLines(x + 4, &y, menu_headers_); |
| 134 | |
| 135 | // Show the current menu item number in relation to total number if |
| 136 | // items don't fit on the screen. |
| 137 | if (menu_items > menu_end - menu_start) { |
| 138 | sprintf(cur_selection_str, "Current item: %d/%d", menu_sel + 1, menu_items); |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 139 | gr_text(gr_sys_font(), x+4, y, cur_selection_str, 1); |
Prashant Malani | 7a49122 | 2016-03-11 10:00:55 -0800 | [diff] [blame] | 140 | y += char_height_+4; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // Menu begins here |
| 144 | SetColor(MENU); |
| 145 | |
| 146 | for (int i = menu_start; i < menu_end; ++i) { |
| 147 | |
| 148 | if (i == menu_sel) { |
| 149 | // draw the highlight bar |
| 150 | SetColor(MENU_SEL_BG); |
Prashant Malani | 7a49122 | 2016-03-11 10:00:55 -0800 | [diff] [blame] | 151 | gr_fill(x, y-2, gr_fb_width()-x, y+char_height_+2); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 152 | // white text of selected item |
| 153 | SetColor(MENU_SEL_FG); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 154 | if (menu_[i][0]) { |
| 155 | gr_text(gr_sys_font(), x + 4, y, menu_[i], 1); |
| 156 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 157 | SetColor(MENU); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 158 | } else if (menu_[i][0]) { |
| 159 | gr_text(gr_sys_font(), x + 4, y, menu_[i], 0); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 160 | } |
Prashant Malani | 7a49122 | 2016-03-11 10:00:55 -0800 | [diff] [blame] | 161 | y += char_height_+4; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 162 | } |
| 163 | SetColor(MENU); |
| 164 | y += 4; |
| 165 | gr_fill(0, y, gr_fb_width(), y+2); |
| 166 | y += 4; |
| 167 | } |
| 168 | |
| 169 | SetColor(LOG); |
| 170 | |
| 171 | // display from the bottom up, until we hit the top of the |
| 172 | // screen, the bottom of the menu, or we've displayed the |
| 173 | // entire text buffer. |
| 174 | int ty; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 175 | int row = (text_top_ + text_rows_ - 1) % text_rows_; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 176 | size_t count = 0; |
Prashant Malani | 7a49122 | 2016-03-11 10:00:55 -0800 | [diff] [blame] | 177 | for (int ty = gr_fb_height() - char_height_ - outer_height; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 178 | ty > y + 2 && count < text_rows_; |
Prashant Malani | 7a49122 | 2016-03-11 10:00:55 -0800 | [diff] [blame] | 179 | ty -= char_height_, ++count) { |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 180 | gr_text(gr_sys_font(), x+4, ty, text_[row], 0); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 181 | --row; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 182 | if (row < 0) row = text_rows_ - 1; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
Damien Bargiacchi | ad8b5a6 | 2016-09-09 08:18:06 -0700 | [diff] [blame] | 187 | // TODO merge drawing routines with screen_ui |
| 188 | void WearRecoveryUI::update_progress_locked() { |
| 189 | draw_screen_locked(); |
| 190 | gr_flip(); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Sen Jiang | d530449 | 2016-12-09 16:20:49 -0800 | [diff] [blame] | 193 | bool WearRecoveryUI::InitTextParams() { |
| 194 | if (!ScreenRecoveryUI::InitTextParams()) { |
| 195 | return false; |
| 196 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 197 | |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 198 | text_cols_ = (gr_fb_width() - (outer_width * 2)) / char_width_; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 199 | |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 200 | if (text_rows_ > kMaxRows) text_rows_ = kMaxRows; |
| 201 | if (text_cols_ > kMaxCols) text_cols_ = kMaxCols; |
| 202 | |
Prashant Malani | 7a49122 | 2016-03-11 10:00:55 -0800 | [diff] [blame] | 203 | visible_text_rows = (gr_fb_height() - (outer_height * 2)) / char_height_; |
Sen Jiang | d530449 | 2016-12-09 16:20:49 -0800 | [diff] [blame] | 204 | return true; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 205 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 206 | |
Sen Jiang | d530449 | 2016-12-09 16:20:49 -0800 | [diff] [blame] | 207 | bool WearRecoveryUI::Init() { |
| 208 | if (!ScreenRecoveryUI::Init()) { |
| 209 | return false; |
| 210 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 211 | |
| 212 | LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]); |
| 213 | backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE]; |
| 214 | LoadBitmap("icon_error", &backgroundIcon[ERROR]); |
| 215 | backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; |
Sen Jiang | d530449 | 2016-12-09 16:20:49 -0800 | [diff] [blame] | 216 | return true; |
Prashant Malani | f7f9e50 | 2016-03-10 03:40:20 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 219 | void WearRecoveryUI::SetStage(int current, int max) |
| 220 | { |
| 221 | } |
| 222 | |
| 223 | void WearRecoveryUI::Print(const char *fmt, ...) |
| 224 | { |
| 225 | char buf[256]; |
| 226 | va_list ap; |
| 227 | va_start(ap, fmt); |
| 228 | vsnprintf(buf, 256, fmt, ap); |
| 229 | va_end(ap); |
| 230 | |
| 231 | fputs(buf, stdout); |
| 232 | |
| 233 | // This can get called before ui_init(), so be careful. |
| 234 | pthread_mutex_lock(&updateMutex); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 235 | if (text_rows_ > 0 && text_cols_ > 0) { |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 236 | char *ptr; |
| 237 | for (ptr = buf; *ptr != '\0'; ++ptr) { |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 238 | if (*ptr == '\n' || text_col_ >= text_cols_) { |
| 239 | text_[text_row_][text_col_] = '\0'; |
| 240 | text_col_ = 0; |
| 241 | text_row_ = (text_row_ + 1) % text_rows_; |
| 242 | if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 243 | } |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 244 | if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 245 | } |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 246 | text_[text_row_][text_col_] = '\0'; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 247 | update_screen_locked(); |
| 248 | } |
| 249 | pthread_mutex_unlock(&updateMutex); |
| 250 | } |
| 251 | |
| 252 | void WearRecoveryUI::StartMenu(const char* const * headers, const char* const * items, |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 253 | int initial_selection) { |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 254 | pthread_mutex_lock(&updateMutex); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 255 | if (text_rows_ > 0 && text_cols_ > 0) { |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 256 | menu_headers_ = headers; |
| 257 | size_t i = 0; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 258 | // "i < text_rows_" is removed from the loop termination condition, |
Tao Bao | 8e9c680 | 2015-09-02 11:20:30 -0700 | [diff] [blame] | 259 | // which is different from the one in ScreenRecoveryUI::StartMenu(). |
| 260 | // Because WearRecoveryUI supports scrollable menu, it's fine to have |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 261 | // more entries than text_rows_. The menu may be truncated otherwise. |
Tao Bao | 8e9c680 | 2015-09-02 11:20:30 -0700 | [diff] [blame] | 262 | // Bug: 23752519 |
| 263 | for (; items[i] != nullptr; i++) { |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 264 | strncpy(menu_[i], items[i], text_cols_ - 1); |
| 265 | menu_[i][text_cols_ - 1] = '\0'; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 266 | } |
| 267 | menu_items = i; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 268 | show_menu = true; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 269 | menu_sel = initial_selection; |
| 270 | menu_start = 0; |
| 271 | menu_end = visible_text_rows - 1 - menu_unusable_rows; |
| 272 | if (menu_items <= menu_end) |
| 273 | menu_end = menu_items; |
| 274 | update_screen_locked(); |
| 275 | } |
| 276 | pthread_mutex_unlock(&updateMutex); |
| 277 | } |
| 278 | |
| 279 | int WearRecoveryUI::SelectMenu(int sel) { |
| 280 | int old_sel; |
| 281 | pthread_mutex_lock(&updateMutex); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 282 | if (show_menu) { |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 283 | old_sel = menu_sel; |
| 284 | menu_sel = sel; |
| 285 | if (menu_sel < 0) menu_sel = 0; |
| 286 | if (menu_sel >= menu_items) menu_sel = menu_items-1; |
| 287 | if (menu_sel < menu_start) { |
| 288 | menu_start--; |
| 289 | menu_end--; |
| 290 | } else if (menu_sel >= menu_end && menu_sel < menu_items) { |
| 291 | menu_end++; |
| 292 | menu_start++; |
| 293 | } |
| 294 | sel = menu_sel; |
| 295 | if (menu_sel != old_sel) update_screen_locked(); |
| 296 | } |
| 297 | pthread_mutex_unlock(&updateMutex); |
| 298 | return sel; |
| 299 | } |
| 300 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 301 | void WearRecoveryUI::ShowFile(FILE* fp) { |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 302 | std::vector<off_t> offsets; |
| 303 | offsets.push_back(ftello(fp)); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 304 | ClearText(); |
| 305 | |
| 306 | struct stat sb; |
| 307 | fstat(fileno(fp), &sb); |
| 308 | |
| 309 | bool show_prompt = false; |
| 310 | while (true) { |
| 311 | if (show_prompt) { |
| 312 | Print("--(%d%% of %d bytes)--", |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 313 | static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))), |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 314 | static_cast<int>(sb.st_size)); |
| 315 | Redraw(); |
| 316 | while (show_prompt) { |
| 317 | show_prompt = false; |
| 318 | int key = WaitKey(); |
| 319 | if (key == KEY_POWER || key == KEY_ENTER) { |
| 320 | return; |
| 321 | } else if (key == KEY_UP || key == KEY_VOLUMEUP) { |
| 322 | if (offsets.size() <= 1) { |
| 323 | show_prompt = true; |
| 324 | } else { |
| 325 | offsets.pop_back(); |
| 326 | fseek(fp, offsets.back(), SEEK_SET); |
| 327 | } |
| 328 | } else { |
| 329 | if (feof(fp)) { |
| 330 | return; |
| 331 | } |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 332 | offsets.push_back(ftello(fp)); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | ClearText(); |
| 336 | } |
| 337 | |
| 338 | int ch = getc(fp); |
| 339 | if (ch == EOF) { |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 340 | text_row_ = text_top_ = text_rows_ - 2; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 341 | show_prompt = true; |
| 342 | } else { |
| 343 | PutChar(ch); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 344 | if (text_col_ == 0 && text_row_ >= text_rows_ - 2) { |
| 345 | text_top_ = text_row_; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 346 | show_prompt = true; |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void WearRecoveryUI::PutChar(char ch) { |
| 353 | pthread_mutex_lock(&updateMutex); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 354 | if (ch != '\n') text_[text_row_][text_col_++] = ch; |
| 355 | if (ch == '\n' || text_col_ >= text_cols_) { |
| 356 | text_col_ = 0; |
| 357 | ++text_row_; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 358 | } |
| 359 | pthread_mutex_unlock(&updateMutex); |
| 360 | } |
| 361 | |
| 362 | void WearRecoveryUI::ShowFile(const char* filename) { |
| 363 | FILE* fp = fopen_path(filename, "re"); |
| 364 | if (fp == nullptr) { |
| 365 | Print(" Unable to open %s: %s\n", filename, strerror(errno)); |
| 366 | return; |
| 367 | } |
| 368 | ShowFile(fp); |
| 369 | fclose(fp); |
| 370 | } |
| 371 | |
| 372 | void WearRecoveryUI::ClearText() { |
| 373 | pthread_mutex_lock(&updateMutex); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 374 | text_col_ = 0; |
| 375 | text_row_ = 0; |
| 376 | text_top_ = 1; |
| 377 | for (size_t i = 0; i < text_rows_; ++i) { |
| 378 | memset(text_[i], 0, text_cols_ + 1); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 379 | } |
| 380 | pthread_mutex_unlock(&updateMutex); |
| 381 | } |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 382 | |
| 383 | void WearRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) { |
| 384 | va_list ap; |
| 385 | va_start(ap, fmt); |
| 386 | PrintV(fmt, false, ap); |
| 387 | va_end(ap); |
| 388 | } |
| 389 | |
| 390 | void WearRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) { |
| 391 | std::string str; |
| 392 | android::base::StringAppendV(&str, fmt, ap); |
| 393 | |
| 394 | if (copy_to_stdout) { |
| 395 | fputs(str.c_str(), stdout); |
| 396 | } |
| 397 | |
| 398 | pthread_mutex_lock(&updateMutex); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 399 | if (text_rows_ > 0 && text_cols_ > 0) { |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 400 | for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) { |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 401 | if (*ptr == '\n' || text_col_ >= text_cols_) { |
| 402 | text_[text_row_][text_col_] = '\0'; |
| 403 | text_col_ = 0; |
| 404 | text_row_ = (text_row_ + 1) % text_rows_; |
| 405 | if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_; |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 406 | } |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 407 | if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr; |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 408 | } |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 409 | text_[text_row_][text_col_] = '\0'; |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 410 | update_screen_locked(); |
| 411 | } |
| 412 | pthread_mutex_unlock(&updateMutex); |
| 413 | } |