blob: 6d8df68b3fc7fd9cbd4964229d4e2d2da734849c [file] [log] [blame]
Doug Zongker211aebc2011-10-28 15:13:10 -07001/*
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 Zongkerdaefc1d2011-10-31 09:34:15 -070032#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070033#include "minui/minui.h"
34#include "screen_ui.h"
35#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070036
Doug Zongker55a36ac2013-03-04 15:49:02 -080037static int char_width;
38static int char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -070039
Doug Zongker211aebc2011-10-28 15:13:10 -070040// There's only (at most) one of these objects, and global callbacks
41// (for pthread_create, and the input event system) need to find it,
42// so use a global variable.
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070043static ScreenRecoveryUI* self = nullptr;
Doug Zongker211aebc2011-10-28 15:13:10 -070044
45// Return the current time as a double (including fractions of a second).
46static double now() {
47 struct timeval tv;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070048 gettimeofday(&tv, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070049 return tv.tv_sec + tv.tv_usec / 1000000.0;
50}
51
52ScreenRecoveryUI::ScreenRecoveryUI() :
53 currentIcon(NONE),
54 installingFrame(0),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070055 locale(nullptr),
Doug Zongker5fa8c232012-09-18 12:37:02 -070056 rtl_locale(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070057 progressBarType(EMPTY),
58 progressScopeStart(0),
59 progressScopeSize(0),
60 progress(0),
61 pagesIdentical(false),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070062 text(nullptr),
Doug Zongker211aebc2011-10-28 15:13:10 -070063 text_cols(0),
64 text_rows(0),
65 text_col(0),
66 text_row(0),
67 text_top(0),
68 show_text(false),
69 show_text_ever(false),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070070 menu(nullptr),
Doug Zongker211aebc2011-10-28 15:13:10 -070071 show_menu(false),
72 menu_top(0),
73 menu_items(0),
74 menu_sel(0),
Doug Zongker32a0a472011-11-01 11:00:20 -070075 animation_fps(20),
Doug Zongkereac881c2014-03-07 09:21:25 -080076 installing_frames(-1),
Doug Zongkerc87bab12013-11-25 13:53:25 -080077 stage(-1),
78 max_stage(-1) {
yetta_wu5b468fc2013-06-25 15:03:11 +080079
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070080 for (int i = 0; i < 5; i++) {
81 backgroundIcon[i] = nullptr;
82 }
83 pthread_mutex_init(&updateMutex, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070084 self = this;
85}
86
Doug Zongker211aebc2011-10-28 15:13:10 -070087// Clear the screen and draw the currently selected background icon (if any).
88// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -070089void ScreenRecoveryUI::draw_background_locked(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -070090 pagesIdentical = false;
Doug Zongker5b5f6c22014-06-03 10:50:13 -070091 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -080092 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -070093
94 if (icon) {
95 gr_surface surface = backgroundIcon[icon];
Doug Zongkereac881c2014-03-07 09:21:25 -080096 if (icon == INSTALLING_UPDATE || icon == ERASING) {
97 surface = installation[installingFrame];
98 }
Doug Zongker02ec6b82012-08-22 17:26:40 -070099 gr_surface text_surface = backgroundText[icon];
100
Doug Zongker211aebc2011-10-28 15:13:10 -0700101 int iconWidth = gr_get_width(surface);
102 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700103 int textWidth = gr_get_width(text_surface);
104 int textHeight = gr_get_height(text_surface);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800105 int stageHeight = gr_get_height(stageMarkerEmpty);
106
107 int sh = (max_stage >= 0) ? stageHeight : 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700108
Doug Zongkereac881c2014-03-07 09:21:25 -0800109 iconX = (gr_fb_width() - iconWidth) / 2;
110 iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700111
112 int textX = (gr_fb_width() - textWidth) / 2;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800113 int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700114
Doug Zongker211aebc2011-10-28 15:13:10 -0700115 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800116 if (stageHeight > 0) {
117 int sw = gr_get_width(stageMarkerEmpty);
118 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
119 int y = iconY + iconHeight + 20;
120 for (int i = 0; i < max_stage; ++i) {
121 gr_blit((i < stage) ? stageMarkerFill : stageMarkerEmpty,
122 0, 0, sw, stageHeight, x, y);
123 x += sw;
124 }
125 }
126
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700127 gr_color(255, 255, 255, 255);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700128 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700129 }
130}
131
132// Draw the progress bar (if any) on the screen. Does not flip pages.
133// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700134void ScreenRecoveryUI::draw_progress_locked() {
Doug Zongker69f4b672012-04-26 14:37:53 -0700135 if (currentIcon == ERROR) return;
136
Doug Zongker02ec6b82012-08-22 17:26:40 -0700137 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Doug Zongkereac881c2014-03-07 09:21:25 -0800138 gr_surface icon = installation[installingFrame];
139 gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY);
Doug Zongker211aebc2011-10-28 15:13:10 -0700140 }
141
142 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700143 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700144 int width = gr_get_width(progressBarEmpty);
145 int height = gr_get_height(progressBarEmpty);
146
147 int dx = (gr_fb_width() - width)/2;
148 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
149
150 // Erase behind the progress bar (in case this was a progress-only update)
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700151 gr_color(0, 0, 0, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700152 gr_fill(dx, dy, width, height);
153
154 if (progressBarType == DETERMINATE) {
155 float p = progressScopeStart + progress * progressScopeSize;
156 int pos = (int) (p * width);
157
Doug Zongker5fa8c232012-09-18 12:37:02 -0700158 if (rtl_locale) {
159 // Fill the progress bar from right to left.
160 if (pos > 0) {
161 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
162 }
163 if (pos < width-1) {
164 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
165 }
166 } else {
167 // Fill the progress bar from left to right.
168 if (pos > 0) {
169 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
170 }
171 if (pos < width-1) {
172 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
173 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700174 }
175 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700176 }
177}
178
Doug Zongkerc0441d12013-07-31 11:28:24 -0700179void ScreenRecoveryUI::SetColor(UIElement e) {
180 switch (e) {
181 case HEADER:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700182 gr_color(247, 0, 6, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700183 break;
184 case MENU:
185 case MENU_SEL_BG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700186 gr_color(0, 106, 157, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700187 break;
188 case MENU_SEL_FG:
189 gr_color(255, 255, 255, 255);
190 break;
191 case LOG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700192 gr_color(249, 194, 0, 255);
193 break;
194 case TEXT_FILL:
195 gr_color(0, 0, 0, 160);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700196 break;
197 default:
198 gr_color(255, 255, 255, 255);
199 break;
200 }
201}
Doug Zongker211aebc2011-10-28 15:13:10 -0700202
203// Redraw everything on the screen. Does not flip pages.
204// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700205void ScreenRecoveryUI::draw_screen_locked() {
Doug Zongker39cf4172014-03-06 16:16:05 -0800206 if (!show_text) {
207 draw_background_locked(currentIcon);
208 draw_progress_locked();
209 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700210 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800211 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700212
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800213 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700214 int i = 0;
215 if (show_menu) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700216 SetColor(HEADER);
Doug Zongker211aebc2011-10-28 15:13:10 -0700217
218 for (; i < menu_top + menu_items; ++i) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700219 if (i == menu_top) SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800220
Doug Zongker211aebc2011-10-28 15:13:10 -0700221 if (i == menu_top + menu_sel) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800222 // draw the highlight bar
Doug Zongkerc0441d12013-07-31 11:28:24 -0700223 SetColor(MENU_SEL_BG);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700224 gr_fill(0, y-2, gr_fb_width(), y+char_height+2);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800225 // white text of selected item
Doug Zongkerc0441d12013-07-31 11:28:24 -0700226 SetColor(MENU_SEL_FG);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700227 if (menu[i][0]) gr_text(4, y, menu[i], 1);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700228 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700229 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700230 if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top);
Doug Zongker211aebc2011-10-28 15:13:10 -0700231 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800232 y += char_height+4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700233 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700234 SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800235 y += 4;
236 gr_fill(0, y, gr_fb_width(), y+2);
237 y += 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700238 ++i;
239 }
240
Doug Zongkerc0441d12013-07-31 11:28:24 -0700241 SetColor(LOG);
Doug Zongker211aebc2011-10-28 15:13:10 -0700242
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800243 // display from the bottom up, until we hit the top of the
244 // screen, the bottom of the menu, or we've displayed the
245 // entire text buffer.
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800246 int row = (text_top+text_rows-1) % text_rows;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700247 size_t count = 0;
248 for (int ty = gr_fb_height() - char_height;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800249 ty > y+2 && count < text_rows;
250 ty -= char_height, ++count) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700251 gr_text(0, ty, text[row], 0);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800252 --row;
253 if (row < 0) row = text_rows-1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700254 }
255 }
256}
257
258// Redraw everything on the screen and flip the screen (make it visible).
259// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700260void ScreenRecoveryUI::update_screen_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700261 draw_screen_locked();
262 gr_flip();
263}
264
265// Updates only the progress bar, if possible, otherwise redraws the screen.
266// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700267void ScreenRecoveryUI::update_progress_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700268 if (show_text || !pagesIdentical) {
269 draw_screen_locked(); // Must redraw the whole screen
270 pagesIdentical = true;
271 } else {
272 draw_progress_locked(); // Draw only the progress bar and overlays
273 }
274 gr_flip();
275}
276
277// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700278void* ScreenRecoveryUI::progress_thread(void *cookie) {
279 self->progress_loop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700280 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700281}
282
283void ScreenRecoveryUI::progress_loop() {
284 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700285 for (;;) {
286 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700287 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700288
289 int redraw = 0;
290
291 // update the installation animation, if active
292 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700293 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
294 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700295 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700296 redraw = 1;
297 }
298
Doug Zongker211aebc2011-10-28 15:13:10 -0700299 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700300 int duration = progressScopeDuration;
301 if (progressBarType == DETERMINATE && duration > 0) {
302 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700303 float p = 1.0 * elapsed / duration;
304 if (p > 1.0) p = 1.0;
305 if (p > progress) {
306 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700307 redraw = 1;
308 }
309 }
310
Doug Zongker32a0a472011-11-01 11:00:20 -0700311 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700312
Doug Zongker32a0a472011-11-01 11:00:20 -0700313 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700314 double end = now();
315 // minimum of 20ms delay between frames
316 double delay = interval - (end-start);
317 if (delay < 0.02) delay = 0.02;
318 usleep((long)(delay * 1000000));
319 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700320}
321
322void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700323 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700324 if (result < 0) {
325 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
326 }
327}
328
Doug Zongkereac881c2014-03-07 09:21:25 -0800329void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700330 int result = res_create_multi_display_surface(filename, frames, surface);
Doug Zongkereac881c2014-03-07 09:21:25 -0800331 if (result < 0) {
332 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
333 }
334}
335
Doug Zongker02ec6b82012-08-22 17:26:40 -0700336void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700337 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700338 if (result < 0) {
339 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
340 }
341}
342
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700343static char** Alloc2d(size_t rows, size_t cols) {
344 char** result = new char*[rows];
345 for (size_t i = 0; i < rows; ++i) {
346 result[i] = new char[cols];
347 memset(result[i], 0, cols);
348 }
349 return result;
350}
351
Elliott Hughes8de52072015-04-08 20:06:50 -0700352void ScreenRecoveryUI::Init() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700353 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700354
Doug Zongker55a36ac2013-03-04 15:49:02 -0800355 gr_font_size(&char_width, &char_height);
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700356 text_rows = gr_fb_height() / char_height;
357 text_cols = gr_fb_width() / char_width;
358
359 text = Alloc2d(text_rows, text_cols + 1);
360 menu = Alloc2d(text_rows, text_cols + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800361
Doug Zongker211aebc2011-10-28 15:13:10 -0700362 text_col = text_row = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700363 text_top = 1;
364
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700365 backgroundIcon[NONE] = nullptr;
Doug Zongkereac881c2014-03-07 09:21:25 -0800366 LoadBitmapArray("icon_installing", &installing_frames, &installation);
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700367 backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : nullptr;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700368 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700369 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700370 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
371
Doug Zongker211aebc2011-10-28 15:13:10 -0700372 LoadBitmap("progress_empty", &progressBarEmpty);
373 LoadBitmap("progress_fill", &progressBarFill);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800374 LoadBitmap("stage_empty", &stageMarkerEmpty);
375 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700376
Doug Zongker02ec6b82012-08-22 17:26:40 -0700377 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
378 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
379 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
380 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
381
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700382 pthread_create(&progress_t, nullptr, progress_thread, nullptr);
Doug Zongker32a0a472011-11-01 11:00:20 -0700383
384 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700385}
386
Doug Zongkera418aa72014-03-17 12:10:02 -0700387void ScreenRecoveryUI::SetLocale(const char* new_locale) {
388 if (new_locale) {
389 this->locale = new_locale;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700390 char* lang = strdup(locale);
391 for (char* p = lang; *p; ++p) {
392 if (*p == '_') {
393 *p = '\0';
394 break;
395 }
396 }
397
398 // A bit cheesy: keep an explicit list of supported languages
399 // that are RTL.
400 if (strcmp(lang, "ar") == 0 || // Arabic
401 strcmp(lang, "fa") == 0 || // Persian (Farsi)
402 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700403 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
404 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700405 rtl_locale = true;
406 }
407 free(lang);
Doug Zongkera418aa72014-03-17 12:10:02 -0700408 } else {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700409 new_locale = nullptr;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700410 }
411}
412
Elliott Hughes8de52072015-04-08 20:06:50 -0700413void ScreenRecoveryUI::SetBackground(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700414 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700415
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700416 currentIcon = icon;
417 update_screen_locked();
418
Doug Zongker211aebc2011-10-28 15:13:10 -0700419 pthread_mutex_unlock(&updateMutex);
420}
421
Elliott Hughes8de52072015-04-08 20:06:50 -0700422void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700423 pthread_mutex_lock(&updateMutex);
424 if (progressBarType != type) {
425 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700426 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700427 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700428 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700429 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700430 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700431 pthread_mutex_unlock(&updateMutex);
432}
433
Elliott Hughes8de52072015-04-08 20:06:50 -0700434void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700435 pthread_mutex_lock(&updateMutex);
436 progressBarType = DETERMINATE;
437 progressScopeStart += progressScopeSize;
438 progressScopeSize = portion;
439 progressScopeTime = now();
440 progressScopeDuration = seconds;
441 progress = 0;
442 update_progress_locked();
443 pthread_mutex_unlock(&updateMutex);
444}
445
Elliott Hughes8de52072015-04-08 20:06:50 -0700446void ScreenRecoveryUI::SetProgress(float fraction) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700447 pthread_mutex_lock(&updateMutex);
448 if (fraction < 0.0) fraction = 0.0;
449 if (fraction > 1.0) fraction = 1.0;
450 if (progressBarType == DETERMINATE && fraction > progress) {
451 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800452 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700453 float scale = width * progressScopeSize;
454 if ((int) (progress * scale) != (int) (fraction * scale)) {
455 progress = fraction;
456 update_progress_locked();
457 }
458 }
459 pthread_mutex_unlock(&updateMutex);
460}
461
Doug Zongkerc87bab12013-11-25 13:53:25 -0800462void ScreenRecoveryUI::SetStage(int current, int max) {
463 pthread_mutex_lock(&updateMutex);
464 stage = current;
465 max_stage = max;
466 pthread_mutex_unlock(&updateMutex);
467}
468
Elliott Hughes8de52072015-04-08 20:06:50 -0700469void ScreenRecoveryUI::Print(const char *fmt, ...) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700470 char buf[256];
471 va_list ap;
472 va_start(ap, fmt);
473 vsnprintf(buf, 256, fmt, ap);
474 va_end(ap);
475
476 fputs(buf, stdout);
477
Doug Zongker211aebc2011-10-28 15:13:10 -0700478 pthread_mutex_lock(&updateMutex);
479 if (text_rows > 0 && text_cols > 0) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700480 for (const char* ptr = buf; *ptr != '\0'; ++ptr) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700481 if (*ptr == '\n' || text_col >= text_cols) {
482 text[text_row][text_col] = '\0';
483 text_col = 0;
484 text_row = (text_row + 1) % text_rows;
485 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
486 }
487 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
488 }
489 text[text_row][text_col] = '\0';
490 update_screen_locked();
491 }
492 pthread_mutex_unlock(&updateMutex);
493}
494
Elliott Hughes8de52072015-04-08 20:06:50 -0700495// TODO: replace this with something not line-based so we can wrap correctly without getting
496// confused about what line we're on.
497void ScreenRecoveryUI::print_no_update(const char* s) {
498 pthread_mutex_lock(&updateMutex);
499 if (text_rows > 0 && text_cols > 0) {
500 for (const char* ptr = s; *ptr != '\0'; ++ptr) {
501 if (*ptr == '\n' || text_col >= text_cols) {
502 text[text_row][text_col] = '\0';
503 text_col = 0;
504 text_row = (text_row + 1) % text_rows;
505 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
506 }
507 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
508 }
509 text[text_row][text_col] = '\0';
510 }
511 pthread_mutex_unlock(&updateMutex);
512}
513
514void ScreenRecoveryUI::ShowFile(const char* filename) {
515 FILE* fp = fopen_path(filename, "re");
516 if (fp == nullptr) {
517 Print(" Unable to open %s: %s\n", filename, strerror(errno));
518 return;
519 }
520
521 char line[1024];
522 int ct = 0;
523 int key = 0;
524 while (fgets(line, sizeof(line), fp) != nullptr) {
525 print_no_update(line);
526 ct++;
527 if (ct % text_rows == 0) {
528 Redraw();
529
530 // give the user time to glance at the entries
531 key = WaitKey();
532
533 if (key == KEY_POWER) {
534 break;
535 } else if (key == KEY_VOLUMEUP) {
536 // Go back by seeking to the beginning and dumping ct - n
537 // lines. It's ugly, but this way we don't need to store
538 // the previous offsets. The files we're dumping here aren't
539 // expected to be very large.
540 ct -= 2 * text_rows;
541 if (ct < 0) {
542 ct = 0;
543 }
544 fseek(fp, 0, SEEK_SET);
545 for (int i = 0; i < ct; i++) {
546 fgets(line, sizeof(line), fp);
547 }
548 Print("^^^^^^^^^^\n");
549 } else {
550 // Next page.
551 }
552 }
553 }
554
555 // If the user didn't abort, then give the user time to glance at
556 // the end of the log, sorry, no rewind here
557 if (key != KEY_POWER) {
558 Print("\n--END-- (press any key)\n");
559 WaitKey();
560 }
561 fclose(fp);
562}
563
Doug Zongker211aebc2011-10-28 15:13:10 -0700564void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
565 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700566 pthread_mutex_lock(&updateMutex);
567 if (text_rows > 0 && text_cols > 0) {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700568 size_t i;
Doug Zongker211aebc2011-10-28 15:13:10 -0700569 for (i = 0; i < text_rows; ++i) {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700570 if (headers[i] == nullptr) break;
Doug Zongker211aebc2011-10-28 15:13:10 -0700571 strncpy(menu[i], headers[i], text_cols-1);
572 menu[i][text_cols-1] = '\0';
573 }
574 menu_top = i;
575 for (; i < text_rows; ++i) {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700576 if (items[i-menu_top] == nullptr) break;
Doug Zongker211aebc2011-10-28 15:13:10 -0700577 strncpy(menu[i], items[i-menu_top], text_cols-1);
578 menu[i][text_cols-1] = '\0';
579 }
580 menu_items = i - menu_top;
581 show_menu = 1;
582 menu_sel = initial_selection;
583 update_screen_locked();
584 }
585 pthread_mutex_unlock(&updateMutex);
586}
587
588int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700589 pthread_mutex_lock(&updateMutex);
590 if (show_menu > 0) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700591 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700592 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700593
594 // Wrap at top and bottom.
595 if (menu_sel < 0) menu_sel = menu_items - 1;
596 if (menu_sel >= menu_items) menu_sel = 0;
597
Doug Zongker211aebc2011-10-28 15:13:10 -0700598 sel = menu_sel;
599 if (menu_sel != old_sel) update_screen_locked();
600 }
601 pthread_mutex_unlock(&updateMutex);
602 return sel;
603}
604
605void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700606 pthread_mutex_lock(&updateMutex);
607 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
608 show_menu = 0;
609 update_screen_locked();
610 }
611 pthread_mutex_unlock(&updateMutex);
612}
613
Elliott Hughes8de52072015-04-08 20:06:50 -0700614bool ScreenRecoveryUI::IsTextVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700615 pthread_mutex_lock(&updateMutex);
616 int visible = show_text;
617 pthread_mutex_unlock(&updateMutex);
618 return visible;
619}
620
Elliott Hughes8de52072015-04-08 20:06:50 -0700621bool ScreenRecoveryUI::WasTextEverVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700622 pthread_mutex_lock(&updateMutex);
623 int ever_visible = show_text_ever;
624 pthread_mutex_unlock(&updateMutex);
625 return ever_visible;
626}
627
Elliott Hughes8de52072015-04-08 20:06:50 -0700628void ScreenRecoveryUI::ShowText(bool visible) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700629 pthread_mutex_lock(&updateMutex);
630 show_text = visible;
Elliott Hughes8de52072015-04-08 20:06:50 -0700631 if (show_text) show_text_ever = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700632 update_screen_locked();
633 pthread_mutex_unlock(&updateMutex);
634}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700635
Elliott Hughes8de52072015-04-08 20:06:50 -0700636void ScreenRecoveryUI::Redraw() {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700637 pthread_mutex_lock(&updateMutex);
638 update_screen_locked();
639 pthread_mutex_unlock(&updateMutex);
640}