blob: a0df455cd85c0558abe1d311c9335d8421419e07 [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.
89void ScreenRecoveryUI::draw_background_locked(Icon icon)
90{
91 pagesIdentical = false;
Doug Zongker5b5f6c22014-06-03 10:50:13 -070092 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -080093 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -070094
95 if (icon) {
96 gr_surface surface = backgroundIcon[icon];
Doug Zongkereac881c2014-03-07 09:21:25 -080097 if (icon == INSTALLING_UPDATE || icon == ERASING) {
98 surface = installation[installingFrame];
99 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700100 gr_surface text_surface = backgroundText[icon];
101
Doug Zongker211aebc2011-10-28 15:13:10 -0700102 int iconWidth = gr_get_width(surface);
103 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700104 int textWidth = gr_get_width(text_surface);
105 int textHeight = gr_get_height(text_surface);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800106 int stageHeight = gr_get_height(stageMarkerEmpty);
107
108 int sh = (max_stage >= 0) ? stageHeight : 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700109
Doug Zongkereac881c2014-03-07 09:21:25 -0800110 iconX = (gr_fb_width() - iconWidth) / 2;
111 iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700112
113 int textX = (gr_fb_width() - textWidth) / 2;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800114 int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700115
Doug Zongker211aebc2011-10-28 15:13:10 -0700116 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800117 if (stageHeight > 0) {
118 int sw = gr_get_width(stageMarkerEmpty);
119 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
120 int y = iconY + iconHeight + 20;
121 for (int i = 0; i < max_stage; ++i) {
122 gr_blit((i < stage) ? stageMarkerFill : stageMarkerEmpty,
123 0, 0, sw, stageHeight, x, y);
124 x += sw;
125 }
126 }
127
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700128 gr_color(255, 255, 255, 255);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700129 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700130 }
131}
132
133// Draw the progress bar (if any) on the screen. Does not flip pages.
134// Should only be called with updateMutex locked.
135void ScreenRecoveryUI::draw_progress_locked()
136{
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;
190 case MENU_SEL_FG:
191 gr_color(255, 255, 255, 255);
192 break;
193 case LOG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700194 gr_color(249, 194, 0, 255);
195 break;
196 case TEXT_FILL:
197 gr_color(0, 0, 0, 160);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700198 break;
199 default:
200 gr_color(255, 255, 255, 255);
201 break;
202 }
203}
Doug Zongker211aebc2011-10-28 15:13:10 -0700204
205// Redraw everything on the screen. Does not flip pages.
206// Should only be called with updateMutex locked.
207void ScreenRecoveryUI::draw_screen_locked()
208{
Doug Zongker39cf4172014-03-06 16:16:05 -0800209 if (!show_text) {
210 draw_background_locked(currentIcon);
211 draw_progress_locked();
212 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700213 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800214 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700215
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800216 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700217 int i = 0;
218 if (show_menu) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700219 SetColor(HEADER);
Doug Zongker211aebc2011-10-28 15:13:10 -0700220
221 for (; i < menu_top + menu_items; ++i) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700222 if (i == menu_top) SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800223
Doug Zongker211aebc2011-10-28 15:13:10 -0700224 if (i == menu_top + menu_sel) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800225 // draw the highlight bar
Doug Zongkerc0441d12013-07-31 11:28:24 -0700226 SetColor(MENU_SEL_BG);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700227 gr_fill(0, y-2, gr_fb_width(), y+char_height+2);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800228 // white text of selected item
Doug Zongkerc0441d12013-07-31 11:28:24 -0700229 SetColor(MENU_SEL_FG);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700230 if (menu[i][0]) gr_text(4, y, menu[i], 1);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700231 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700232 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700233 if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top);
Doug Zongker211aebc2011-10-28 15:13:10 -0700234 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800235 y += char_height+4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700236 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700237 SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800238 y += 4;
239 gr_fill(0, y, gr_fb_width(), y+2);
240 y += 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700241 ++i;
242 }
243
Doug Zongkerc0441d12013-07-31 11:28:24 -0700244 SetColor(LOG);
Doug Zongker211aebc2011-10-28 15:13:10 -0700245
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800246 // display from the bottom up, until we hit the top of the
247 // screen, the bottom of the menu, or we've displayed the
248 // entire text buffer.
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800249 int row = (text_top+text_rows-1) % text_rows;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700250 size_t count = 0;
251 for (int ty = gr_fb_height() - char_height;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800252 ty > y+2 && count < text_rows;
253 ty -= char_height, ++count) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700254 gr_text(0, ty, text[row], 0);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800255 --row;
256 if (row < 0) row = text_rows-1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700257 }
258 }
259}
260
261// Redraw everything on the screen and flip the screen (make it visible).
262// Should only be called with updateMutex locked.
263void ScreenRecoveryUI::update_screen_locked()
264{
265 draw_screen_locked();
266 gr_flip();
267}
268
269// Updates only the progress bar, if possible, otherwise redraws the screen.
270// Should only be called with updateMutex locked.
271void ScreenRecoveryUI::update_progress_locked()
272{
273 if (show_text || !pagesIdentical) {
274 draw_screen_locked(); // Must redraw the whole screen
275 pagesIdentical = true;
276 } else {
277 draw_progress_locked(); // Draw only the progress bar and overlays
278 }
279 gr_flip();
280}
281
282// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700283void* ScreenRecoveryUI::progress_thread(void *cookie) {
284 self->progress_loop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700285 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700286}
287
288void ScreenRecoveryUI::progress_loop() {
289 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700290 for (;;) {
291 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700292 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700293
294 int redraw = 0;
295
296 // update the installation animation, if active
297 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700298 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
299 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700300 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700301 redraw = 1;
302 }
303
Doug Zongker211aebc2011-10-28 15:13:10 -0700304 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700305 int duration = progressScopeDuration;
306 if (progressBarType == DETERMINATE && duration > 0) {
307 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700308 float p = 1.0 * elapsed / duration;
309 if (p > 1.0) p = 1.0;
310 if (p > progress) {
311 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700312 redraw = 1;
313 }
314 }
315
Doug Zongker32a0a472011-11-01 11:00:20 -0700316 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700317
Doug Zongker32a0a472011-11-01 11:00:20 -0700318 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700319 double end = now();
320 // minimum of 20ms delay between frames
321 double delay = interval - (end-start);
322 if (delay < 0.02) delay = 0.02;
323 usleep((long)(delay * 1000000));
324 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700325}
326
327void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700328 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700329 if (result < 0) {
330 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
331 }
332}
333
Doug Zongkereac881c2014-03-07 09:21:25 -0800334void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700335 int result = res_create_multi_display_surface(filename, frames, surface);
Doug Zongkereac881c2014-03-07 09:21:25 -0800336 if (result < 0) {
337 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
338 }
339}
340
Doug Zongker02ec6b82012-08-22 17:26:40 -0700341void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700342 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700343 if (result < 0) {
344 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
345 }
346}
347
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700348static char** Alloc2d(size_t rows, size_t cols) {
349 char** result = new char*[rows];
350 for (size_t i = 0; i < rows; ++i) {
351 result[i] = new char[cols];
352 memset(result[i], 0, cols);
353 }
354 return result;
355}
356
Doug Zongker211aebc2011-10-28 15:13:10 -0700357void ScreenRecoveryUI::Init()
358{
359 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700360
Doug Zongker55a36ac2013-03-04 15:49:02 -0800361 gr_font_size(&char_width, &char_height);
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700362 text_rows = gr_fb_height() / char_height;
363 text_cols = gr_fb_width() / char_width;
364
365 text = Alloc2d(text_rows, text_cols + 1);
366 menu = Alloc2d(text_rows, text_cols + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800367
Doug Zongker211aebc2011-10-28 15:13:10 -0700368 text_col = text_row = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700369 text_top = 1;
370
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700371 backgroundIcon[NONE] = nullptr;
Doug Zongkereac881c2014-03-07 09:21:25 -0800372 LoadBitmapArray("icon_installing", &installing_frames, &installation);
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700373 backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : nullptr;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700374 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700375 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700376 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
377
Doug Zongker211aebc2011-10-28 15:13:10 -0700378 LoadBitmap("progress_empty", &progressBarEmpty);
379 LoadBitmap("progress_fill", &progressBarFill);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800380 LoadBitmap("stage_empty", &stageMarkerEmpty);
381 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700382
Doug Zongker02ec6b82012-08-22 17:26:40 -0700383 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
384 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
385 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
386 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
387
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700388 pthread_create(&progress_t, nullptr, progress_thread, nullptr);
Doug Zongker32a0a472011-11-01 11:00:20 -0700389
390 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700391}
392
Doug Zongkera418aa72014-03-17 12:10:02 -0700393void ScreenRecoveryUI::SetLocale(const char* new_locale) {
394 if (new_locale) {
395 this->locale = new_locale;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700396 char* lang = strdup(locale);
397 for (char* p = lang; *p; ++p) {
398 if (*p == '_') {
399 *p = '\0';
400 break;
401 }
402 }
403
404 // A bit cheesy: keep an explicit list of supported languages
405 // that are RTL.
406 if (strcmp(lang, "ar") == 0 || // Arabic
407 strcmp(lang, "fa") == 0 || // Persian (Farsi)
408 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700409 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
410 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700411 rtl_locale = true;
412 }
413 free(lang);
Doug Zongkera418aa72014-03-17 12:10:02 -0700414 } else {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700415 new_locale = nullptr;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700416 }
417}
418
Doug Zongker211aebc2011-10-28 15:13:10 -0700419void ScreenRecoveryUI::SetBackground(Icon icon)
420{
421 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700422
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700423 currentIcon = icon;
424 update_screen_locked();
425
Doug Zongker211aebc2011-10-28 15:13:10 -0700426 pthread_mutex_unlock(&updateMutex);
427}
428
429void ScreenRecoveryUI::SetProgressType(ProgressType type)
430{
431 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
442void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
443{
444 pthread_mutex_lock(&updateMutex);
445 progressBarType = DETERMINATE;
446 progressScopeStart += progressScopeSize;
447 progressScopeSize = portion;
448 progressScopeTime = now();
449 progressScopeDuration = seconds;
450 progress = 0;
451 update_progress_locked();
452 pthread_mutex_unlock(&updateMutex);
453}
454
455void ScreenRecoveryUI::SetProgress(float fraction)
456{
457 pthread_mutex_lock(&updateMutex);
458 if (fraction < 0.0) fraction = 0.0;
459 if (fraction > 1.0) fraction = 1.0;
460 if (progressBarType == DETERMINATE && fraction > progress) {
461 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800462 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700463 float scale = width * progressScopeSize;
464 if ((int) (progress * scale) != (int) (fraction * scale)) {
465 progress = fraction;
466 update_progress_locked();
467 }
468 }
469 pthread_mutex_unlock(&updateMutex);
470}
471
Doug Zongkerc87bab12013-11-25 13:53:25 -0800472void ScreenRecoveryUI::SetStage(int current, int max) {
473 pthread_mutex_lock(&updateMutex);
474 stage = current;
475 max_stage = max;
476 pthread_mutex_unlock(&updateMutex);
477}
478
Doug Zongker211aebc2011-10-28 15:13:10 -0700479void ScreenRecoveryUI::Print(const char *fmt, ...)
480{
481 char buf[256];
482 va_list ap;
483 va_start(ap, fmt);
484 vsnprintf(buf, 256, fmt, ap);
485 va_end(ap);
486
487 fputs(buf, stdout);
488
489 // This can get called before ui_init(), so be careful.
490 pthread_mutex_lock(&updateMutex);
491 if (text_rows > 0 && text_cols > 0) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700492 for (char* ptr = buf; *ptr != '\0'; ++ptr) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700493 if (*ptr == '\n' || text_col >= text_cols) {
494 text[text_row][text_col] = '\0';
495 text_col = 0;
496 text_row = (text_row + 1) % text_rows;
497 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
498 }
499 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
500 }
501 text[text_row][text_col] = '\0';
502 update_screen_locked();
503 }
504 pthread_mutex_unlock(&updateMutex);
505}
506
507void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
508 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700509 pthread_mutex_lock(&updateMutex);
510 if (text_rows > 0 && text_cols > 0) {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700511 size_t i;
Doug Zongker211aebc2011-10-28 15:13:10 -0700512 for (i = 0; i < text_rows; ++i) {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700513 if (headers[i] == nullptr) break;
Doug Zongker211aebc2011-10-28 15:13:10 -0700514 strncpy(menu[i], headers[i], text_cols-1);
515 menu[i][text_cols-1] = '\0';
516 }
517 menu_top = i;
518 for (; i < text_rows; ++i) {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700519 if (items[i-menu_top] == nullptr) break;
Doug Zongker211aebc2011-10-28 15:13:10 -0700520 strncpy(menu[i], items[i-menu_top], text_cols-1);
521 menu[i][text_cols-1] = '\0';
522 }
523 menu_items = i - menu_top;
524 show_menu = 1;
525 menu_sel = initial_selection;
526 update_screen_locked();
527 }
528 pthread_mutex_unlock(&updateMutex);
529}
530
531int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700532 pthread_mutex_lock(&updateMutex);
533 if (show_menu > 0) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700534 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700535 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700536
537 // Wrap at top and bottom.
538 if (menu_sel < 0) menu_sel = menu_items - 1;
539 if (menu_sel >= menu_items) menu_sel = 0;
540
Doug Zongker211aebc2011-10-28 15:13:10 -0700541 sel = menu_sel;
542 if (menu_sel != old_sel) update_screen_locked();
543 }
544 pthread_mutex_unlock(&updateMutex);
545 return sel;
546}
547
548void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700549 pthread_mutex_lock(&updateMutex);
550 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
551 show_menu = 0;
552 update_screen_locked();
553 }
554 pthread_mutex_unlock(&updateMutex);
555}
556
557bool ScreenRecoveryUI::IsTextVisible()
558{
559 pthread_mutex_lock(&updateMutex);
560 int visible = show_text;
561 pthread_mutex_unlock(&updateMutex);
562 return visible;
563}
564
565bool ScreenRecoveryUI::WasTextEverVisible()
566{
567 pthread_mutex_lock(&updateMutex);
568 int ever_visible = show_text_ever;
569 pthread_mutex_unlock(&updateMutex);
570 return ever_visible;
571}
572
573void ScreenRecoveryUI::ShowText(bool visible)
574{
575 pthread_mutex_lock(&updateMutex);
576 show_text = visible;
577 if (show_text) show_text_ever = 1;
578 update_screen_locked();
579 pthread_mutex_unlock(&updateMutex);
580}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700581
582void ScreenRecoveryUI::Redraw()
583{
584 pthread_mutex_lock(&updateMutex);
585 update_screen_locked();
586 pthread_mutex_unlock(&updateMutex);
587}