blob: cca261fc44410674e19b932cf5fdc038196e5f34 [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
Elliott Hughes95fc63e2015-04-10 19:12:01 -070031#include <vector>
32
Doug Zongker211aebc2011-10-28 15:13:10 -070033#include "common.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070034#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070035#include "minui/minui.h"
36#include "screen_ui.h"
37#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070038
Doug Zongker55a36ac2013-03-04 15:49:02 -080039static int char_width;
40static int char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -070041
Doug Zongker211aebc2011-10-28 15:13:10 -070042// There's only (at most) one of these objects, and global callbacks
43// (for pthread_create, and the input event system) need to find it,
44// so use a global variable.
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070045static ScreenRecoveryUI* self = nullptr;
Doug Zongker211aebc2011-10-28 15:13:10 -070046
47// Return the current time as a double (including fractions of a second).
48static double now() {
49 struct timeval tv;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070050 gettimeofday(&tv, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070051 return tv.tv_sec + tv.tv_usec / 1000000.0;
52}
53
54ScreenRecoveryUI::ScreenRecoveryUI() :
55 currentIcon(NONE),
56 installingFrame(0),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070057 locale(nullptr),
Doug Zongker5fa8c232012-09-18 12:37:02 -070058 rtl_locale(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070059 progressBarType(EMPTY),
60 progressScopeStart(0),
61 progressScopeSize(0),
62 progress(0),
63 pagesIdentical(false),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070064 text(nullptr),
Doug Zongker211aebc2011-10-28 15:13:10 -070065 text_cols(0),
66 text_rows(0),
67 text_col(0),
68 text_row(0),
69 text_top(0),
70 show_text(false),
71 show_text_ever(false),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070072 menu(nullptr),
Doug Zongker211aebc2011-10-28 15:13:10 -070073 show_menu(false),
74 menu_top(0),
75 menu_items(0),
76 menu_sel(0),
Doug Zongker32a0a472011-11-01 11:00:20 -070077 animation_fps(20),
Doug Zongkereac881c2014-03-07 09:21:25 -080078 installing_frames(-1),
Doug Zongkerc87bab12013-11-25 13:53:25 -080079 stage(-1),
80 max_stage(-1) {
yetta_wu5b468fc2013-06-25 15:03:11 +080081
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070082 for (int i = 0; i < 5; i++) {
83 backgroundIcon[i] = nullptr;
84 }
85 pthread_mutex_init(&updateMutex, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070086 self = this;
87}
88
Doug Zongker211aebc2011-10-28 15:13:10 -070089// Clear the screen and draw the currently selected background icon (if any).
90// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -070091void ScreenRecoveryUI::draw_background_locked(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -070092 pagesIdentical = false;
Doug Zongker5b5f6c22014-06-03 10:50:13 -070093 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -080094 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -070095
96 if (icon) {
97 gr_surface surface = backgroundIcon[icon];
Doug Zongkereac881c2014-03-07 09:21:25 -080098 if (icon == INSTALLING_UPDATE || icon == ERASING) {
99 surface = installation[installingFrame];
100 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700101 gr_surface text_surface = backgroundText[icon];
102
Doug Zongker211aebc2011-10-28 15:13:10 -0700103 int iconWidth = gr_get_width(surface);
104 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700105 int textWidth = gr_get_width(text_surface);
106 int textHeight = gr_get_height(text_surface);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800107 int stageHeight = gr_get_height(stageMarkerEmpty);
108
109 int sh = (max_stage >= 0) ? stageHeight : 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700110
Doug Zongkereac881c2014-03-07 09:21:25 -0800111 iconX = (gr_fb_width() - iconWidth) / 2;
112 iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700113
114 int textX = (gr_fb_width() - textWidth) / 2;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800115 int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700116
Doug Zongker211aebc2011-10-28 15:13:10 -0700117 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800118 if (stageHeight > 0) {
119 int sw = gr_get_width(stageMarkerEmpty);
120 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
121 int y = iconY + iconHeight + 20;
122 for (int i = 0; i < max_stage; ++i) {
123 gr_blit((i < stage) ? stageMarkerFill : stageMarkerEmpty,
124 0, 0, sw, stageHeight, x, y);
125 x += sw;
126 }
127 }
128
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700129 gr_color(255, 255, 255, 255);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700130 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700131 }
132}
133
134// Draw the progress bar (if any) on the screen. Does not flip pages.
135// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700136void ScreenRecoveryUI::draw_progress_locked() {
Doug Zongker69f4b672012-04-26 14:37:53 -0700137 if (currentIcon == ERROR) return;
138
Doug Zongker02ec6b82012-08-22 17:26:40 -0700139 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Doug Zongkereac881c2014-03-07 09:21:25 -0800140 gr_surface icon = installation[installingFrame];
141 gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY);
Doug Zongker211aebc2011-10-28 15:13:10 -0700142 }
143
144 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700145 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700146 int width = gr_get_width(progressBarEmpty);
147 int height = gr_get_height(progressBarEmpty);
148
149 int dx = (gr_fb_width() - width)/2;
150 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
151
152 // Erase behind the progress bar (in case this was a progress-only update)
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700153 gr_color(0, 0, 0, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700154 gr_fill(dx, dy, width, height);
155
156 if (progressBarType == DETERMINATE) {
157 float p = progressScopeStart + progress * progressScopeSize;
158 int pos = (int) (p * width);
159
Doug Zongker5fa8c232012-09-18 12:37:02 -0700160 if (rtl_locale) {
161 // Fill the progress bar from right to left.
162 if (pos > 0) {
163 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
164 }
165 if (pos < width-1) {
166 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
167 }
168 } else {
169 // Fill the progress bar from left to right.
170 if (pos > 0) {
171 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
172 }
173 if (pos < width-1) {
174 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
175 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700176 }
177 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700178 }
179}
180
Doug Zongkerc0441d12013-07-31 11:28:24 -0700181void ScreenRecoveryUI::SetColor(UIElement e) {
182 switch (e) {
183 case HEADER:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700184 gr_color(247, 0, 6, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700185 break;
186 case MENU:
187 case MENU_SEL_BG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700188 gr_color(0, 106, 157, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700189 break;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700190 case MENU_SEL_BG_ACTIVE:
191 gr_color(0, 156, 100, 255);
192 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700193 case MENU_SEL_FG:
194 gr_color(255, 255, 255, 255);
195 break;
196 case LOG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700197 gr_color(249, 194, 0, 255);
198 break;
199 case TEXT_FILL:
200 gr_color(0, 0, 0, 160);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700201 break;
202 default:
203 gr_color(255, 255, 255, 255);
204 break;
205 }
206}
Doug Zongker211aebc2011-10-28 15:13:10 -0700207
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700208void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
209 SetColor(MENU);
210 *y += 4;
211 gr_fill(0, *y, gr_fb_width(), *y + 2);
212 *y += 8;
213}
214
Doug Zongker211aebc2011-10-28 15:13:10 -0700215// Redraw everything on the screen. Does not flip pages.
216// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700217void ScreenRecoveryUI::draw_screen_locked() {
Doug Zongker39cf4172014-03-06 16:16:05 -0800218 if (!show_text) {
219 draw_background_locked(currentIcon);
220 draw_progress_locked();
221 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700222 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800223 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700224
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800225 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700226 if (show_menu) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700227 SetColor(HEADER);
Doug Zongker211aebc2011-10-28 15:13:10 -0700228
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700229 for (int i = 0; i < menu_top + menu_items; ++i) {
230 if (i == menu_top) DrawHorizontalRule(&y);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800231
Doug Zongker211aebc2011-10-28 15:13:10 -0700232 if (i == menu_top + menu_sel) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800233 // draw the highlight bar
Elliott Hughes642aaa72015-04-10 12:47:46 -0700234 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700235 gr_fill(0, y-2, gr_fb_width(), y+char_height+2);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800236 // white text of selected item
Doug Zongkerc0441d12013-07-31 11:28:24 -0700237 SetColor(MENU_SEL_FG);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700238 if (menu[i][0]) gr_text(4, y, menu[i], 1);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700239 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700240 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700241 if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top);
Doug Zongker211aebc2011-10-28 15:13:10 -0700242 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800243 y += char_height+4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700244 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700245
246 DrawHorizontalRule(&y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700247 }
248
Doug Zongkerc0441d12013-07-31 11:28:24 -0700249 SetColor(LOG);
Doug Zongker211aebc2011-10-28 15:13:10 -0700250
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800251 // display from the bottom up, until we hit the top of the
252 // screen, the bottom of the menu, or we've displayed the
253 // entire text buffer.
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800254 int row = (text_top+text_rows-1) % text_rows;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700255 size_t count = 0;
256 for (int ty = gr_fb_height() - char_height;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700257 ty >= y && count < text_rows;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800258 ty -= char_height, ++count) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700259 gr_text(0, ty, text[row], 0);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800260 --row;
261 if (row < 0) row = text_rows-1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700262 }
263 }
264}
265
266// Redraw everything on the screen and flip the screen (make it visible).
267// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700268void ScreenRecoveryUI::update_screen_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700269 draw_screen_locked();
270 gr_flip();
271}
272
273// Updates only the progress bar, if possible, otherwise redraws the screen.
274// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700275void ScreenRecoveryUI::update_progress_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700276 if (show_text || !pagesIdentical) {
277 draw_screen_locked(); // Must redraw the whole screen
278 pagesIdentical = true;
279 } else {
280 draw_progress_locked(); // Draw only the progress bar and overlays
281 }
282 gr_flip();
283}
284
285// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700286void* ScreenRecoveryUI::progress_thread(void *cookie) {
287 self->progress_loop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700288 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700289}
290
291void ScreenRecoveryUI::progress_loop() {
292 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700293 for (;;) {
294 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700295 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700296
297 int redraw = 0;
298
299 // update the installation animation, if active
300 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700301 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
302 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700303 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700304 redraw = 1;
305 }
306
Doug Zongker211aebc2011-10-28 15:13:10 -0700307 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700308 int duration = progressScopeDuration;
309 if (progressBarType == DETERMINATE && duration > 0) {
310 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700311 float p = 1.0 * elapsed / duration;
312 if (p > 1.0) p = 1.0;
313 if (p > progress) {
314 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700315 redraw = 1;
316 }
317 }
318
Doug Zongker32a0a472011-11-01 11:00:20 -0700319 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700320
Doug Zongker32a0a472011-11-01 11:00:20 -0700321 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700322 double end = now();
323 // minimum of 20ms delay between frames
324 double delay = interval - (end-start);
325 if (delay < 0.02) delay = 0.02;
326 usleep((long)(delay * 1000000));
327 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700328}
329
330void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700331 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700332 if (result < 0) {
333 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
334 }
335}
336
Doug Zongkereac881c2014-03-07 09:21:25 -0800337void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700338 int result = res_create_multi_display_surface(filename, frames, surface);
Doug Zongkereac881c2014-03-07 09:21:25 -0800339 if (result < 0) {
340 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
341 }
342}
343
Doug Zongker02ec6b82012-08-22 17:26:40 -0700344void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700345 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700346 if (result < 0) {
347 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
348 }
349}
350
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700351static char** Alloc2d(size_t rows, size_t cols) {
352 char** result = new char*[rows];
353 for (size_t i = 0; i < rows; ++i) {
354 result[i] = new char[cols];
355 memset(result[i], 0, cols);
356 }
357 return result;
358}
359
Elliott Hughes8de52072015-04-08 20:06:50 -0700360void ScreenRecoveryUI::Init() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700361 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700362
Doug Zongker55a36ac2013-03-04 15:49:02 -0800363 gr_font_size(&char_width, &char_height);
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700364 text_rows = gr_fb_height() / char_height;
365 text_cols = gr_fb_width() / char_width;
366
367 text = Alloc2d(text_rows, text_cols + 1);
368 menu = Alloc2d(text_rows, text_cols + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800369
Doug Zongker211aebc2011-10-28 15:13:10 -0700370 text_col = text_row = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700371 text_top = 1;
372
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700373 backgroundIcon[NONE] = nullptr;
Doug Zongkereac881c2014-03-07 09:21:25 -0800374 LoadBitmapArray("icon_installing", &installing_frames, &installation);
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700375 backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : nullptr;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700376 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700377 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700378 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
379
Doug Zongker211aebc2011-10-28 15:13:10 -0700380 LoadBitmap("progress_empty", &progressBarEmpty);
381 LoadBitmap("progress_fill", &progressBarFill);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800382 LoadBitmap("stage_empty", &stageMarkerEmpty);
383 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700384
Doug Zongker02ec6b82012-08-22 17:26:40 -0700385 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
386 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
387 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
388 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
389
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700390 pthread_create(&progress_t, nullptr, progress_thread, nullptr);
Doug Zongker32a0a472011-11-01 11:00:20 -0700391
392 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700393}
394
Doug Zongkera418aa72014-03-17 12:10:02 -0700395void ScreenRecoveryUI::SetLocale(const char* new_locale) {
396 if (new_locale) {
397 this->locale = new_locale;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700398 char* lang = strdup(locale);
399 for (char* p = lang; *p; ++p) {
400 if (*p == '_') {
401 *p = '\0';
402 break;
403 }
404 }
405
406 // A bit cheesy: keep an explicit list of supported languages
407 // that are RTL.
408 if (strcmp(lang, "ar") == 0 || // Arabic
409 strcmp(lang, "fa") == 0 || // Persian (Farsi)
410 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700411 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
412 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700413 rtl_locale = true;
414 }
415 free(lang);
Doug Zongkera418aa72014-03-17 12:10:02 -0700416 } else {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700417 new_locale = nullptr;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700418 }
419}
420
Elliott Hughes8de52072015-04-08 20:06:50 -0700421void ScreenRecoveryUI::SetBackground(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700422 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700423
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700424 currentIcon = icon;
425 update_screen_locked();
426
Doug Zongker211aebc2011-10-28 15:13:10 -0700427 pthread_mutex_unlock(&updateMutex);
428}
429
Elliott Hughes8de52072015-04-08 20:06:50 -0700430void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700431 pthread_mutex_lock(&updateMutex);
432 if (progressBarType != type) {
433 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700434 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700435 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700436 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700437 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700438 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700439 pthread_mutex_unlock(&updateMutex);
440}
441
Elliott Hughes8de52072015-04-08 20:06:50 -0700442void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700443 pthread_mutex_lock(&updateMutex);
444 progressBarType = DETERMINATE;
445 progressScopeStart += progressScopeSize;
446 progressScopeSize = portion;
447 progressScopeTime = now();
448 progressScopeDuration = seconds;
449 progress = 0;
450 update_progress_locked();
451 pthread_mutex_unlock(&updateMutex);
452}
453
Elliott Hughes8de52072015-04-08 20:06:50 -0700454void ScreenRecoveryUI::SetProgress(float fraction) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700455 pthread_mutex_lock(&updateMutex);
456 if (fraction < 0.0) fraction = 0.0;
457 if (fraction > 1.0) fraction = 1.0;
458 if (progressBarType == DETERMINATE && fraction > progress) {
459 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800460 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700461 float scale = width * progressScopeSize;
462 if ((int) (progress * scale) != (int) (fraction * scale)) {
463 progress = fraction;
464 update_progress_locked();
465 }
466 }
467 pthread_mutex_unlock(&updateMutex);
468}
469
Doug Zongkerc87bab12013-11-25 13:53:25 -0800470void ScreenRecoveryUI::SetStage(int current, int max) {
471 pthread_mutex_lock(&updateMutex);
472 stage = current;
473 max_stage = max;
474 pthread_mutex_unlock(&updateMutex);
475}
476
Elliott Hughes8de52072015-04-08 20:06:50 -0700477void ScreenRecoveryUI::Print(const char *fmt, ...) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700478 char buf[256];
479 va_list ap;
480 va_start(ap, fmt);
481 vsnprintf(buf, 256, fmt, ap);
482 va_end(ap);
483
484 fputs(buf, stdout);
485
Doug Zongker211aebc2011-10-28 15:13:10 -0700486 pthread_mutex_lock(&updateMutex);
487 if (text_rows > 0 && text_cols > 0) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700488 for (const char* ptr = buf; *ptr != '\0'; ++ptr) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700489 if (*ptr == '\n' || text_col >= text_cols) {
490 text[text_row][text_col] = '\0';
491 text_col = 0;
492 text_row = (text_row + 1) % text_rows;
493 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
494 }
495 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
496 }
497 text[text_row][text_col] = '\0';
498 update_screen_locked();
499 }
500 pthread_mutex_unlock(&updateMutex);
501}
502
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700503void ScreenRecoveryUI::PutChar(char ch) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700504 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700505 if (ch != '\n') text[text_row][text_col++] = ch;
506 if (ch == '\n' || text_col >= text_cols) {
507 text_col = 0;
508 ++text_row;
Elliott Hughes8de52072015-04-08 20:06:50 -0700509 }
510 pthread_mutex_unlock(&updateMutex);
511}
512
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700513void ScreenRecoveryUI::ClearText() {
514 pthread_mutex_lock(&updateMutex);
515 text_col = 0;
516 text_row = 0;
517 text_top = 1;
518 for (size_t i = 0; i < text_rows; ++i) {
519 memset(text[i], 0, text_cols + 1);
520 }
521 pthread_mutex_unlock(&updateMutex);
522}
523
524void ScreenRecoveryUI::ShowFile(FILE* fp) {
525 std::vector<long> offsets;
526 offsets.push_back(ftell(fp));
527 ClearText();
528
529 struct stat sb;
530 fstat(fileno(fp), &sb);
531
532 bool show_prompt = false;
533 while (true) {
534 if (show_prompt) {
535 Print("--(%d%% of %d bytes)--",
536 static_cast<int>(100 * (double(ftell(fp)) / double(sb.st_size))),
537 static_cast<int>(sb.st_size));
538 Redraw();
539 while (show_prompt) {
540 show_prompt = false;
541 int key = WaitKey();
Elliott Hughes300ed082015-04-13 10:47:59 -0700542 if (key == KEY_POWER || key == KEY_ENTER) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700543 return;
544 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
545 if (offsets.size() <= 1) {
546 show_prompt = true;
547 } else {
548 offsets.pop_back();
549 fseek(fp, offsets.back(), SEEK_SET);
550 }
551 } else {
552 if (feof(fp)) {
553 return;
554 }
555 offsets.push_back(ftell(fp));
556 }
557 }
558 ClearText();
559 }
560
561 int ch = getc(fp);
562 if (ch == EOF) {
563 text_row = text_top = text_rows - 2;
564 show_prompt = true;
565 } else {
566 PutChar(ch);
567 if (text_col == 0 && text_row >= text_rows - 2) {
568 text_top = text_row;
569 show_prompt = true;
570 }
571 }
572 }
573}
574
Elliott Hughes8de52072015-04-08 20:06:50 -0700575void ScreenRecoveryUI::ShowFile(const char* filename) {
576 FILE* fp = fopen_path(filename, "re");
577 if (fp == nullptr) {
578 Print(" Unable to open %s: %s\n", filename, strerror(errno));
579 return;
580 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700581 ShowFile(fp);
Elliott Hughes8de52072015-04-08 20:06:50 -0700582 fclose(fp);
583}
584
Doug Zongker211aebc2011-10-28 15:13:10 -0700585void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
586 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700587 pthread_mutex_lock(&updateMutex);
588 if (text_rows > 0 && text_cols > 0) {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700589 size_t i;
Doug Zongker211aebc2011-10-28 15:13:10 -0700590 for (i = 0; i < text_rows; ++i) {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700591 if (headers[i] == nullptr) break;
Doug Zongker211aebc2011-10-28 15:13:10 -0700592 strncpy(menu[i], headers[i], text_cols-1);
593 menu[i][text_cols-1] = '\0';
594 }
595 menu_top = i;
596 for (; i < text_rows; ++i) {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700597 if (items[i-menu_top] == nullptr) break;
Doug Zongker211aebc2011-10-28 15:13:10 -0700598 strncpy(menu[i], items[i-menu_top], text_cols-1);
599 menu[i][text_cols-1] = '\0';
600 }
601 menu_items = i - menu_top;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700602 show_menu = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700603 menu_sel = initial_selection;
604 update_screen_locked();
605 }
606 pthread_mutex_unlock(&updateMutex);
607}
608
609int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700610 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700611 if (show_menu) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700612 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700613 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700614
615 // Wrap at top and bottom.
616 if (menu_sel < 0) menu_sel = menu_items - 1;
617 if (menu_sel >= menu_items) menu_sel = 0;
618
Doug Zongker211aebc2011-10-28 15:13:10 -0700619 sel = menu_sel;
620 if (menu_sel != old_sel) update_screen_locked();
621 }
622 pthread_mutex_unlock(&updateMutex);
623 return sel;
624}
625
626void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700627 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700628 if (show_menu && text_rows > 0 && text_cols > 0) {
629 show_menu = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700630 update_screen_locked();
631 }
632 pthread_mutex_unlock(&updateMutex);
633}
634
Elliott Hughes8de52072015-04-08 20:06:50 -0700635bool ScreenRecoveryUI::IsTextVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700636 pthread_mutex_lock(&updateMutex);
637 int visible = show_text;
638 pthread_mutex_unlock(&updateMutex);
639 return visible;
640}
641
Elliott Hughes8de52072015-04-08 20:06:50 -0700642bool ScreenRecoveryUI::WasTextEverVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700643 pthread_mutex_lock(&updateMutex);
644 int ever_visible = show_text_ever;
645 pthread_mutex_unlock(&updateMutex);
646 return ever_visible;
647}
648
Elliott Hughes8de52072015-04-08 20:06:50 -0700649void ScreenRecoveryUI::ShowText(bool visible) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700650 pthread_mutex_lock(&updateMutex);
651 show_text = visible;
Elliott Hughes8de52072015-04-08 20:06:50 -0700652 if (show_text) show_text_ever = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700653 update_screen_locked();
654 pthread_mutex_unlock(&updateMutex);
655}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700656
Elliott Hughes8de52072015-04-08 20:06:50 -0700657void ScreenRecoveryUI::Redraw() {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700658 pthread_mutex_lock(&updateMutex);
659 update_screen_locked();
660 pthread_mutex_unlock(&updateMutex);
661}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700662
663void ScreenRecoveryUI::KeyLongPress(int) {
664 // Redraw so that if we're in the menu, the highlight
665 // will change color to indicate a successful long press.
666 Redraw();
667}