blob: 2aa3dab1945e88df22f140c4a29e291c307ce9fb [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// Return the current time as a double (including fractions of a second).
43static double now() {
44 struct timeval tv;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070045 gettimeofday(&tv, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070046 return tv.tv_sec + tv.tv_usec / 1000000.0;
47}
48
49ScreenRecoveryUI::ScreenRecoveryUI() :
50 currentIcon(NONE),
51 installingFrame(0),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070052 locale(nullptr),
Doug Zongker5fa8c232012-09-18 12:37:02 -070053 rtl_locale(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070054 progressBarType(EMPTY),
55 progressScopeStart(0),
56 progressScopeSize(0),
57 progress(0),
58 pagesIdentical(false),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070059 text(nullptr),
Doug Zongker211aebc2011-10-28 15:13:10 -070060 text_cols(0),
61 text_rows(0),
62 text_col(0),
63 text_row(0),
64 text_top(0),
65 show_text(false),
66 show_text_ever(false),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070067 menu(nullptr),
Doug Zongker211aebc2011-10-28 15:13:10 -070068 show_menu(false),
69 menu_top(0),
70 menu_items(0),
71 menu_sel(0),
Doug Zongker32a0a472011-11-01 11:00:20 -070072 animation_fps(20),
Doug Zongkereac881c2014-03-07 09:21:25 -080073 installing_frames(-1),
Doug Zongkerc87bab12013-11-25 13:53:25 -080074 stage(-1),
75 max_stage(-1) {
yetta_wu5b468fc2013-06-25 15:03:11 +080076
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070077 for (int i = 0; i < 5; i++) {
78 backgroundIcon[i] = nullptr;
79 }
80 pthread_mutex_init(&updateMutex, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070081}
82
Doug Zongker211aebc2011-10-28 15:13:10 -070083// Clear the screen and draw the currently selected background icon (if any).
84// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -070085void ScreenRecoveryUI::draw_background_locked(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -070086 pagesIdentical = false;
Doug Zongker5b5f6c22014-06-03 10:50:13 -070087 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -080088 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -070089
90 if (icon) {
91 gr_surface surface = backgroundIcon[icon];
Doug Zongkereac881c2014-03-07 09:21:25 -080092 if (icon == INSTALLING_UPDATE || icon == ERASING) {
93 surface = installation[installingFrame];
94 }
Doug Zongker02ec6b82012-08-22 17:26:40 -070095 gr_surface text_surface = backgroundText[icon];
96
Doug Zongker211aebc2011-10-28 15:13:10 -070097 int iconWidth = gr_get_width(surface);
98 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -070099 int textWidth = gr_get_width(text_surface);
100 int textHeight = gr_get_height(text_surface);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800101 int stageHeight = gr_get_height(stageMarkerEmpty);
102
103 int sh = (max_stage >= 0) ? stageHeight : 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700104
Doug Zongkereac881c2014-03-07 09:21:25 -0800105 iconX = (gr_fb_width() - iconWidth) / 2;
106 iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700107
108 int textX = (gr_fb_width() - textWidth) / 2;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800109 int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700110
Doug Zongker211aebc2011-10-28 15:13:10 -0700111 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800112 if (stageHeight > 0) {
113 int sw = gr_get_width(stageMarkerEmpty);
114 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
115 int y = iconY + iconHeight + 20;
116 for (int i = 0; i < max_stage; ++i) {
117 gr_blit((i < stage) ? stageMarkerFill : stageMarkerEmpty,
118 0, 0, sw, stageHeight, x, y);
119 x += sw;
120 }
121 }
122
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700123 gr_color(255, 255, 255, 255);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700124 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700125 }
126}
127
128// Draw the progress bar (if any) on the screen. Does not flip pages.
129// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700130void ScreenRecoveryUI::draw_progress_locked() {
Doug Zongker69f4b672012-04-26 14:37:53 -0700131 if (currentIcon == ERROR) return;
132
Doug Zongker02ec6b82012-08-22 17:26:40 -0700133 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Doug Zongkereac881c2014-03-07 09:21:25 -0800134 gr_surface icon = installation[installingFrame];
135 gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY);
Doug Zongker211aebc2011-10-28 15:13:10 -0700136 }
137
138 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700139 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700140 int width = gr_get_width(progressBarEmpty);
141 int height = gr_get_height(progressBarEmpty);
142
143 int dx = (gr_fb_width() - width)/2;
144 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
145
146 // Erase behind the progress bar (in case this was a progress-only update)
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700147 gr_color(0, 0, 0, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700148 gr_fill(dx, dy, width, height);
149
150 if (progressBarType == DETERMINATE) {
151 float p = progressScopeStart + progress * progressScopeSize;
152 int pos = (int) (p * width);
153
Doug Zongker5fa8c232012-09-18 12:37:02 -0700154 if (rtl_locale) {
155 // Fill the progress bar from right to left.
156 if (pos > 0) {
157 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
158 }
159 if (pos < width-1) {
160 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
161 }
162 } else {
163 // Fill the progress bar from left to right.
164 if (pos > 0) {
165 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
166 }
167 if (pos < width-1) {
168 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
169 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700170 }
171 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700172 }
173}
174
Doug Zongkerc0441d12013-07-31 11:28:24 -0700175void ScreenRecoveryUI::SetColor(UIElement e) {
176 switch (e) {
177 case HEADER:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700178 gr_color(247, 0, 6, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700179 break;
180 case MENU:
181 case MENU_SEL_BG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700182 gr_color(0, 106, 157, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700183 break;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700184 case MENU_SEL_BG_ACTIVE:
185 gr_color(0, 156, 100, 255);
186 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700187 case MENU_SEL_FG:
188 gr_color(255, 255, 255, 255);
189 break;
190 case LOG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700191 gr_color(249, 194, 0, 255);
192 break;
193 case TEXT_FILL:
194 gr_color(0, 0, 0, 160);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700195 break;
196 default:
197 gr_color(255, 255, 255, 255);
198 break;
199 }
200}
Doug Zongker211aebc2011-10-28 15:13:10 -0700201
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700202void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
203 SetColor(MENU);
204 *y += 4;
205 gr_fill(0, *y, gr_fb_width(), *y + 2);
206 *y += 8;
207}
208
Doug Zongker211aebc2011-10-28 15:13:10 -0700209// Redraw everything on the screen. Does not flip pages.
210// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700211void ScreenRecoveryUI::draw_screen_locked() {
Doug Zongker39cf4172014-03-06 16:16:05 -0800212 if (!show_text) {
213 draw_background_locked(currentIcon);
214 draw_progress_locked();
215 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700216 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800217 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700218
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800219 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700220 if (show_menu) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700221 SetColor(HEADER);
Doug Zongker211aebc2011-10-28 15:13:10 -0700222
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700223 for (int i = 0; i < menu_top + menu_items; ++i) {
224 if (i == menu_top) DrawHorizontalRule(&y);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800225
Doug Zongker211aebc2011-10-28 15:13:10 -0700226 if (i == menu_top + menu_sel) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800227 // draw the highlight bar
Elliott Hughes642aaa72015-04-10 12:47:46 -0700228 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700229 gr_fill(0, y-2, gr_fb_width(), y+char_height+2);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800230 // white text of selected item
Doug Zongkerc0441d12013-07-31 11:28:24 -0700231 SetColor(MENU_SEL_FG);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700232 if (menu[i][0]) gr_text(4, y, menu[i], 1);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700233 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700234 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700235 if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top);
Doug Zongker211aebc2011-10-28 15:13:10 -0700236 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800237 y += char_height+4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700238 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700239
240 DrawHorizontalRule(&y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700241 }
242
Doug Zongkerc0441d12013-07-31 11:28:24 -0700243 SetColor(LOG);
Doug Zongker211aebc2011-10-28 15:13:10 -0700244
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800245 // display from the bottom up, until we hit the top of the
246 // screen, the bottom of the menu, or we've displayed the
247 // entire text buffer.
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800248 int row = (text_top+text_rows-1) % text_rows;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700249 size_t count = 0;
250 for (int ty = gr_fb_height() - char_height;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700251 ty >= y && count < text_rows;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800252 ty -= char_height, ++count) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700253 gr_text(0, ty, text[row], 0);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800254 --row;
255 if (row < 0) row = text_rows-1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700256 }
257 }
258}
259
260// Redraw everything on the screen and flip the screen (make it visible).
261// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700262void ScreenRecoveryUI::update_screen_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700263 draw_screen_locked();
264 gr_flip();
265}
266
267// Updates only the progress bar, if possible, otherwise redraws the screen.
268// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700269void ScreenRecoveryUI::update_progress_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700270 if (show_text || !pagesIdentical) {
271 draw_screen_locked(); // Must redraw the whole screen
272 pagesIdentical = true;
273 } else {
274 draw_progress_locked(); // Draw only the progress bar and overlays
275 }
276 gr_flip();
277}
278
279// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700280void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
281 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700282 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700283}
284
Elliott Hughes985022a2015-04-13 13:04:32 -0700285void ScreenRecoveryUI::ProgressThreadLoop() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700286 double interval = 1.0 / animation_fps;
Elliott Hughes985022a2015-04-13 13:04:32 -0700287 while (true) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700288 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700289 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700290
291 int redraw = 0;
292
293 // update the installation animation, if active
294 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700295 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
296 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700297 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700298 redraw = 1;
299 }
300
Doug Zongker211aebc2011-10-28 15:13:10 -0700301 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700302 int duration = progressScopeDuration;
303 if (progressBarType == DETERMINATE && duration > 0) {
304 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700305 float p = 1.0 * elapsed / duration;
306 if (p > 1.0) p = 1.0;
307 if (p > progress) {
308 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700309 redraw = 1;
310 }
311 }
312
Doug Zongker32a0a472011-11-01 11:00:20 -0700313 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700314
Doug Zongker32a0a472011-11-01 11:00:20 -0700315 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700316 double end = now();
317 // minimum of 20ms delay between frames
318 double delay = interval - (end-start);
319 if (delay < 0.02) delay = 0.02;
320 usleep((long)(delay * 1000000));
321 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700322}
323
324void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700325 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700326 if (result < 0) {
327 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
328 }
329}
330
Doug Zongkereac881c2014-03-07 09:21:25 -0800331void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700332 int result = res_create_multi_display_surface(filename, frames, surface);
Doug Zongkereac881c2014-03-07 09:21:25 -0800333 if (result < 0) {
334 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
335 }
336}
337
Doug Zongker02ec6b82012-08-22 17:26:40 -0700338void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700339 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700340 if (result < 0) {
341 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
342 }
343}
344
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700345static char** Alloc2d(size_t rows, size_t cols) {
346 char** result = new char*[rows];
347 for (size_t i = 0; i < rows; ++i) {
348 result[i] = new char[cols];
349 memset(result[i], 0, cols);
350 }
351 return result;
352}
353
Elliott Hughes8de52072015-04-08 20:06:50 -0700354void ScreenRecoveryUI::Init() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700355 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700356
Doug Zongker55a36ac2013-03-04 15:49:02 -0800357 gr_font_size(&char_width, &char_height);
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700358 text_rows = gr_fb_height() / char_height;
359 text_cols = gr_fb_width() / char_width;
360
361 text = Alloc2d(text_rows, text_cols + 1);
362 menu = Alloc2d(text_rows, text_cols + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800363
Doug Zongker211aebc2011-10-28 15:13:10 -0700364 text_col = text_row = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700365 text_top = 1;
366
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700367 backgroundIcon[NONE] = nullptr;
Doug Zongkereac881c2014-03-07 09:21:25 -0800368 LoadBitmapArray("icon_installing", &installing_frames, &installation);
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700369 backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : nullptr;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700370 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700371 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700372 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
373
Doug Zongker211aebc2011-10-28 15:13:10 -0700374 LoadBitmap("progress_empty", &progressBarEmpty);
375 LoadBitmap("progress_fill", &progressBarFill);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800376 LoadBitmap("stage_empty", &stageMarkerEmpty);
377 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700378
Doug Zongker02ec6b82012-08-22 17:26:40 -0700379 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
380 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
381 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
382 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
383
Elliott Hughes985022a2015-04-13 13:04:32 -0700384 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Doug Zongker32a0a472011-11-01 11:00:20 -0700385
386 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700387}
388
Doug Zongkera418aa72014-03-17 12:10:02 -0700389void ScreenRecoveryUI::SetLocale(const char* new_locale) {
390 if (new_locale) {
391 this->locale = new_locale;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700392 char* lang = strdup(locale);
393 for (char* p = lang; *p; ++p) {
394 if (*p == '_') {
395 *p = '\0';
396 break;
397 }
398 }
399
400 // A bit cheesy: keep an explicit list of supported languages
401 // that are RTL.
402 if (strcmp(lang, "ar") == 0 || // Arabic
403 strcmp(lang, "fa") == 0 || // Persian (Farsi)
404 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700405 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
406 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700407 rtl_locale = true;
408 }
409 free(lang);
Doug Zongkera418aa72014-03-17 12:10:02 -0700410 } else {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700411 new_locale = nullptr;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700412 }
413}
414
Elliott Hughes8de52072015-04-08 20:06:50 -0700415void ScreenRecoveryUI::SetBackground(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700416 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700417
Doug Zongker52eeea42012-09-04 14:28:25 -0700418 currentIcon = icon;
419 update_screen_locked();
420
Doug Zongker211aebc2011-10-28 15:13:10 -0700421 pthread_mutex_unlock(&updateMutex);
422}
423
Elliott Hughes8de52072015-04-08 20:06:50 -0700424void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700425 pthread_mutex_lock(&updateMutex);
426 if (progressBarType != type) {
427 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700428 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700429 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700430 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700431 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700432 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700433 pthread_mutex_unlock(&updateMutex);
434}
435
Elliott Hughes8de52072015-04-08 20:06:50 -0700436void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700437 pthread_mutex_lock(&updateMutex);
438 progressBarType = DETERMINATE;
439 progressScopeStart += progressScopeSize;
440 progressScopeSize = portion;
441 progressScopeTime = now();
442 progressScopeDuration = seconds;
443 progress = 0;
444 update_progress_locked();
445 pthread_mutex_unlock(&updateMutex);
446}
447
Elliott Hughes8de52072015-04-08 20:06:50 -0700448void ScreenRecoveryUI::SetProgress(float fraction) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700449 pthread_mutex_lock(&updateMutex);
450 if (fraction < 0.0) fraction = 0.0;
451 if (fraction > 1.0) fraction = 1.0;
452 if (progressBarType == DETERMINATE && fraction > progress) {
453 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800454 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700455 float scale = width * progressScopeSize;
456 if ((int) (progress * scale) != (int) (fraction * scale)) {
457 progress = fraction;
458 update_progress_locked();
459 }
460 }
461 pthread_mutex_unlock(&updateMutex);
462}
463
Doug Zongkerc87bab12013-11-25 13:53:25 -0800464void ScreenRecoveryUI::SetStage(int current, int max) {
465 pthread_mutex_lock(&updateMutex);
466 stage = current;
467 max_stage = max;
468 pthread_mutex_unlock(&updateMutex);
469}
470
Elliott Hughes8de52072015-04-08 20:06:50 -0700471void ScreenRecoveryUI::Print(const char *fmt, ...) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700472 char buf[256];
473 va_list ap;
474 va_start(ap, fmt);
475 vsnprintf(buf, 256, fmt, ap);
476 va_end(ap);
477
478 fputs(buf, stdout);
479
Doug Zongker211aebc2011-10-28 15:13:10 -0700480 pthread_mutex_lock(&updateMutex);
481 if (text_rows > 0 && text_cols > 0) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700482 for (const char* ptr = buf; *ptr != '\0'; ++ptr) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700483 if (*ptr == '\n' || text_col >= text_cols) {
484 text[text_row][text_col] = '\0';
485 text_col = 0;
486 text_row = (text_row + 1) % text_rows;
487 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
488 }
489 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
490 }
491 text[text_row][text_col] = '\0';
492 update_screen_locked();
493 }
494 pthread_mutex_unlock(&updateMutex);
495}
496
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700497void ScreenRecoveryUI::PutChar(char ch) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700498 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700499 if (ch != '\n') text[text_row][text_col++] = ch;
500 if (ch == '\n' || text_col >= text_cols) {
501 text_col = 0;
502 ++text_row;
Elliott Hughes8de52072015-04-08 20:06:50 -0700503 }
504 pthread_mutex_unlock(&updateMutex);
505}
506
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700507void ScreenRecoveryUI::ClearText() {
508 pthread_mutex_lock(&updateMutex);
509 text_col = 0;
510 text_row = 0;
511 text_top = 1;
512 for (size_t i = 0; i < text_rows; ++i) {
513 memset(text[i], 0, text_cols + 1);
514 }
515 pthread_mutex_unlock(&updateMutex);
516}
517
518void ScreenRecoveryUI::ShowFile(FILE* fp) {
519 std::vector<long> offsets;
520 offsets.push_back(ftell(fp));
521 ClearText();
522
523 struct stat sb;
524 fstat(fileno(fp), &sb);
525
526 bool show_prompt = false;
527 while (true) {
528 if (show_prompt) {
529 Print("--(%d%% of %d bytes)--",
530 static_cast<int>(100 * (double(ftell(fp)) / double(sb.st_size))),
531 static_cast<int>(sb.st_size));
532 Redraw();
533 while (show_prompt) {
534 show_prompt = false;
535 int key = WaitKey();
536 if (key == KEY_POWER) {
537 return;
538 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
539 if (offsets.size() <= 1) {
540 show_prompt = true;
541 } else {
542 offsets.pop_back();
543 fseek(fp, offsets.back(), SEEK_SET);
544 }
545 } else {
546 if (feof(fp)) {
547 return;
548 }
549 offsets.push_back(ftell(fp));
550 }
551 }
552 ClearText();
553 }
554
555 int ch = getc(fp);
556 if (ch == EOF) {
557 text_row = text_top = text_rows - 2;
558 show_prompt = true;
559 } else {
560 PutChar(ch);
561 if (text_col == 0 && text_row >= text_rows - 2) {
562 text_top = text_row;
563 show_prompt = true;
564 }
565 }
566 }
567}
568
Elliott Hughes8de52072015-04-08 20:06:50 -0700569void ScreenRecoveryUI::ShowFile(const char* filename) {
570 FILE* fp = fopen_path(filename, "re");
571 if (fp == nullptr) {
572 Print(" Unable to open %s: %s\n", filename, strerror(errno));
573 return;
574 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700575 ShowFile(fp);
Elliott Hughes8de52072015-04-08 20:06:50 -0700576 fclose(fp);
577}
578
Doug Zongker211aebc2011-10-28 15:13:10 -0700579void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
580 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700581 pthread_mutex_lock(&updateMutex);
582 if (text_rows > 0 && text_cols > 0) {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700583 size_t i;
Doug Zongker211aebc2011-10-28 15:13:10 -0700584 for (i = 0; i < text_rows; ++i) {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700585 if (headers[i] == nullptr) break;
Doug Zongker211aebc2011-10-28 15:13:10 -0700586 strncpy(menu[i], headers[i], text_cols-1);
587 menu[i][text_cols-1] = '\0';
588 }
589 menu_top = i;
590 for (; i < text_rows; ++i) {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700591 if (items[i-menu_top] == nullptr) break;
Doug Zongker211aebc2011-10-28 15:13:10 -0700592 strncpy(menu[i], items[i-menu_top], text_cols-1);
593 menu[i][text_cols-1] = '\0';
594 }
595 menu_items = i - menu_top;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700596 show_menu = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700597 menu_sel = initial_selection;
598 update_screen_locked();
599 }
600 pthread_mutex_unlock(&updateMutex);
601}
602
603int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700604 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700605 if (show_menu) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700606 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700607 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700608
609 // Wrap at top and bottom.
610 if (menu_sel < 0) menu_sel = menu_items - 1;
611 if (menu_sel >= menu_items) menu_sel = 0;
612
Doug Zongker211aebc2011-10-28 15:13:10 -0700613 sel = menu_sel;
614 if (menu_sel != old_sel) update_screen_locked();
615 }
616 pthread_mutex_unlock(&updateMutex);
617 return sel;
618}
619
620void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700621 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700622 if (show_menu && text_rows > 0 && text_cols > 0) {
623 show_menu = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700624 update_screen_locked();
625 }
626 pthread_mutex_unlock(&updateMutex);
627}
628
Elliott Hughes8de52072015-04-08 20:06:50 -0700629bool ScreenRecoveryUI::IsTextVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700630 pthread_mutex_lock(&updateMutex);
631 int visible = show_text;
632 pthread_mutex_unlock(&updateMutex);
633 return visible;
634}
635
Elliott Hughes8de52072015-04-08 20:06:50 -0700636bool ScreenRecoveryUI::WasTextEverVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700637 pthread_mutex_lock(&updateMutex);
638 int ever_visible = show_text_ever;
639 pthread_mutex_unlock(&updateMutex);
640 return ever_visible;
641}
642
Elliott Hughes8de52072015-04-08 20:06:50 -0700643void ScreenRecoveryUI::ShowText(bool visible) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700644 pthread_mutex_lock(&updateMutex);
645 show_text = visible;
Elliott Hughes8de52072015-04-08 20:06:50 -0700646 if (show_text) show_text_ever = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700647 update_screen_locked();
648 pthread_mutex_unlock(&updateMutex);
649}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700650
Elliott Hughes8de52072015-04-08 20:06:50 -0700651void ScreenRecoveryUI::Redraw() {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700652 pthread_mutex_lock(&updateMutex);
653 update_screen_locked();
654 pthread_mutex_unlock(&updateMutex);
655}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700656
657void ScreenRecoveryUI::KeyLongPress(int) {
658 // Redraw so that if we're in the menu, the highlight
659 // will change color to indicate a successful long press.
660 Redraw();
661}