blob: d087af329abbdff594bcc3db89383e584241391e [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.
43static ScreenRecoveryUI* self = NULL;
44
45// Return the current time as a double (including fractions of a second).
46static double now() {
47 struct timeval tv;
48 gettimeofday(&tv, NULL);
49 return tv.tv_sec + tv.tv_usec / 1000000.0;
50}
51
52ScreenRecoveryUI::ScreenRecoveryUI() :
53 currentIcon(NONE),
54 installingFrame(0),
Doug Zongkera418aa72014-03-17 12:10:02 -070055 locale(NULL),
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),
62 text_cols(0),
63 text_rows(0),
64 text_col(0),
65 text_row(0),
66 text_top(0),
67 show_text(false),
68 show_text_ever(false),
69 show_menu(false),
70 menu_top(0),
71 menu_items(0),
72 menu_sel(0),
Doug Zongker32a0a472011-11-01 11:00:20 -070073 animation_fps(20),
Doug Zongkereac881c2014-03-07 09:21:25 -080074 installing_frames(-1),
Doug Zongkerc87bab12013-11-25 13:53:25 -080075 stage(-1),
76 max_stage(-1) {
yetta_wu5b468fc2013-06-25 15:03:11 +080077
78 for (int i = 0; i < 5; i++)
79 backgroundIcon[i] = NULL;
80
Bjorn Andersson80a7a462013-08-30 16:59:06 -070081 memset(text, 0, sizeof(text));
82
Doug Zongker211aebc2011-10-28 15:13:10 -070083 pthread_mutex_init(&updateMutex, NULL);
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 Zongker9551cf92014-04-04 13:48:33 -070092 gr_color(250, 250, 250, 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 Zongker9551cf92014-04-04 13:48:33 -0700128 gr_color(115, 115, 115, 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 Zongker9551cf92014-04-04 13:48:33 -0700153 gr_color(250, 250, 250, 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 Zongker9551cf92014-04-04 13:48:33 -0700184 gr_color(0xff, 0x57, 0x22, 255); // Quantum "Deep Orange" 500
Doug Zongkerc0441d12013-07-31 11:28:24 -0700185 break;
186 case MENU:
187 case MENU_SEL_BG:
Doug Zongker9551cf92014-04-04 13:48:33 -0700188 gr_color(0x67, 0x3a, 0xb7, 255); // Quantum "Deep Purple" 500
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 Zongker9551cf92014-04-04 13:48:33 -0700194 gr_color(0x3f, 0x51, 0xb5, 255); // Quantum "Indigo" 500
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
202// Redraw everything on the screen. Does not flip pages.
203// Should only be called with updateMutex locked.
204void ScreenRecoveryUI::draw_screen_locked()
205{
Doug Zongker39cf4172014-03-06 16:16:05 -0800206 if (!show_text) {
207 draw_background_locked(currentIcon);
208 draw_progress_locked();
209 } else {
Doug Zongker9551cf92014-04-04 13:48:33 -0700210 gr_color(250, 250, 250, 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 Zongker9551cf92014-04-04 13:48:33 -0700224 gr_fill(0, y-2+kTextYOffset, gr_fb_width(), y+char_height+2+kTextYOffset);
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 Zongker9551cf92014-04-04 13:48:33 -0700227 if (menu[i][0]) gr_text(kTextXOffset, y+kTextYOffset, menu[i], 1);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700228 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700229 } else {
Doug Zongker9551cf92014-04-04 13:48:33 -0700230 if (menu[i][0]) gr_text(kTextXOffset, y+kTextYOffset, 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.
246 int ty;
247 int row = (text_top+text_rows-1) % text_rows;
248 for (int ty = gr_fb_height() - char_height, count = 0;
249 ty > y+2 && count < text_rows;
250 ty -= char_height, ++count) {
Doug Zongker9551cf92014-04-04 13:48:33 -0700251 gr_text(kTextXOffset, ty+kTextYOffset, 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.
260void ScreenRecoveryUI::update_screen_locked()
261{
262 draw_screen_locked();
263 gr_flip();
264}
265
266// Updates only the progress bar, if possible, otherwise redraws the screen.
267// Should only be called with updateMutex locked.
268void ScreenRecoveryUI::update_progress_locked()
269{
270 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.
Doug Zongker32a0a472011-11-01 11:00:20 -0700280void* ScreenRecoveryUI::progress_thread(void *cookie) {
281 self->progress_loop();
282 return NULL;
283}
284
285void ScreenRecoveryUI::progress_loop() {
286 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700287 for (;;) {
288 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
Doug Zongker211aebc2011-10-28 15:13:10 -0700345void ScreenRecoveryUI::Init()
346{
347 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700348
Doug Zongker55a36ac2013-03-04 15:49:02 -0800349 gr_font_size(&char_width, &char_height);
350
Doug Zongker211aebc2011-10-28 15:13:10 -0700351 text_col = text_row = 0;
Doug Zongker9551cf92014-04-04 13:48:33 -0700352 text_rows = (gr_fb_height() - 2 * kTextYOffset) / char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -0700353 if (text_rows > kMaxRows) text_rows = kMaxRows;
354 text_top = 1;
355
Doug Zongker9551cf92014-04-04 13:48:33 -0700356 text_cols = (gr_fb_width() - 2 * kTextXOffset) / char_width;
Doug Zongker211aebc2011-10-28 15:13:10 -0700357 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
358
Alistair Strachan9b8ae802013-07-17 10:34:36 -0700359 backgroundIcon[NONE] = NULL;
Doug Zongkereac881c2014-03-07 09:21:25 -0800360 LoadBitmapArray("icon_installing", &installing_frames, &installation);
361 backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700362 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700363 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700364 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
365
Doug Zongker211aebc2011-10-28 15:13:10 -0700366 LoadBitmap("progress_empty", &progressBarEmpty);
367 LoadBitmap("progress_fill", &progressBarFill);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800368 LoadBitmap("stage_empty", &stageMarkerEmpty);
369 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700370
Doug Zongker02ec6b82012-08-22 17:26:40 -0700371 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
372 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
373 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
374 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
375
Doug Zongker211aebc2011-10-28 15:13:10 -0700376 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700377
378 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700379}
380
Doug Zongkera418aa72014-03-17 12:10:02 -0700381void ScreenRecoveryUI::SetLocale(const char* new_locale) {
382 if (new_locale) {
383 this->locale = new_locale;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700384 char* lang = strdup(locale);
385 for (char* p = lang; *p; ++p) {
386 if (*p == '_') {
387 *p = '\0';
388 break;
389 }
390 }
391
392 // A bit cheesy: keep an explicit list of supported languages
393 // that are RTL.
394 if (strcmp(lang, "ar") == 0 || // Arabic
395 strcmp(lang, "fa") == 0 || // Persian (Farsi)
396 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700397 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
398 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700399 rtl_locale = true;
400 }
401 free(lang);
Doug Zongkera418aa72014-03-17 12:10:02 -0700402 } else {
403 new_locale = NULL;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700404 }
405}
406
Doug Zongker211aebc2011-10-28 15:13:10 -0700407void ScreenRecoveryUI::SetBackground(Icon icon)
408{
409 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700410
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700411 currentIcon = icon;
412 update_screen_locked();
413
Doug Zongker211aebc2011-10-28 15:13:10 -0700414 pthread_mutex_unlock(&updateMutex);
415}
416
417void ScreenRecoveryUI::SetProgressType(ProgressType type)
418{
419 pthread_mutex_lock(&updateMutex);
420 if (progressBarType != type) {
421 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700422 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700423 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700424 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700425 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700426 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700427 pthread_mutex_unlock(&updateMutex);
428}
429
430void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
431{
432 pthread_mutex_lock(&updateMutex);
433 progressBarType = DETERMINATE;
434 progressScopeStart += progressScopeSize;
435 progressScopeSize = portion;
436 progressScopeTime = now();
437 progressScopeDuration = seconds;
438 progress = 0;
439 update_progress_locked();
440 pthread_mutex_unlock(&updateMutex);
441}
442
443void ScreenRecoveryUI::SetProgress(float fraction)
444{
445 pthread_mutex_lock(&updateMutex);
446 if (fraction < 0.0) fraction = 0.0;
447 if (fraction > 1.0) fraction = 1.0;
448 if (progressBarType == DETERMINATE && fraction > progress) {
449 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800450 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700451 float scale = width * progressScopeSize;
452 if ((int) (progress * scale) != (int) (fraction * scale)) {
453 progress = fraction;
454 update_progress_locked();
455 }
456 }
457 pthread_mutex_unlock(&updateMutex);
458}
459
Doug Zongkerc87bab12013-11-25 13:53:25 -0800460void ScreenRecoveryUI::SetStage(int current, int max) {
461 pthread_mutex_lock(&updateMutex);
462 stage = current;
463 max_stage = max;
464 pthread_mutex_unlock(&updateMutex);
465}
466
Doug Zongker211aebc2011-10-28 15:13:10 -0700467void ScreenRecoveryUI::Print(const char *fmt, ...)
468{
469 char buf[256];
470 va_list ap;
471 va_start(ap, fmt);
472 vsnprintf(buf, 256, fmt, ap);
473 va_end(ap);
474
475 fputs(buf, stdout);
476
477 // This can get called before ui_init(), so be careful.
478 pthread_mutex_lock(&updateMutex);
479 if (text_rows > 0 && text_cols > 0) {
480 char *ptr;
481 for (ptr = buf; *ptr != '\0'; ++ptr) {
482 if (*ptr == '\n' || text_col >= text_cols) {
483 text[text_row][text_col] = '\0';
484 text_col = 0;
485 text_row = (text_row + 1) % text_rows;
486 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
487 }
488 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
489 }
490 text[text_row][text_col] = '\0';
491 update_screen_locked();
492 }
493 pthread_mutex_unlock(&updateMutex);
494}
495
496void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
497 int initial_selection) {
498 int i;
499 pthread_mutex_lock(&updateMutex);
500 if (text_rows > 0 && text_cols > 0) {
501 for (i = 0; i < text_rows; ++i) {
502 if (headers[i] == NULL) break;
503 strncpy(menu[i], headers[i], text_cols-1);
504 menu[i][text_cols-1] = '\0';
505 }
506 menu_top = i;
507 for (; i < text_rows; ++i) {
508 if (items[i-menu_top] == NULL) break;
509 strncpy(menu[i], items[i-menu_top], text_cols-1);
510 menu[i][text_cols-1] = '\0';
511 }
512 menu_items = i - menu_top;
513 show_menu = 1;
514 menu_sel = initial_selection;
515 update_screen_locked();
516 }
517 pthread_mutex_unlock(&updateMutex);
518}
519
520int ScreenRecoveryUI::SelectMenu(int sel) {
521 int old_sel;
522 pthread_mutex_lock(&updateMutex);
523 if (show_menu > 0) {
524 old_sel = menu_sel;
525 menu_sel = sel;
526 if (menu_sel < 0) menu_sel = 0;
527 if (menu_sel >= menu_items) menu_sel = menu_items-1;
528 sel = menu_sel;
529 if (menu_sel != old_sel) update_screen_locked();
530 }
531 pthread_mutex_unlock(&updateMutex);
532 return sel;
533}
534
535void ScreenRecoveryUI::EndMenu() {
536 int i;
537 pthread_mutex_lock(&updateMutex);
538 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
539 show_menu = 0;
540 update_screen_locked();
541 }
542 pthread_mutex_unlock(&updateMutex);
543}
544
545bool ScreenRecoveryUI::IsTextVisible()
546{
547 pthread_mutex_lock(&updateMutex);
548 int visible = show_text;
549 pthread_mutex_unlock(&updateMutex);
550 return visible;
551}
552
553bool ScreenRecoveryUI::WasTextEverVisible()
554{
555 pthread_mutex_lock(&updateMutex);
556 int ever_visible = show_text_ever;
557 pthread_mutex_unlock(&updateMutex);
558 return ever_visible;
559}
560
561void ScreenRecoveryUI::ShowText(bool visible)
562{
563 pthread_mutex_lock(&updateMutex);
564 show_text = visible;
565 if (show_text) show_text_ever = 1;
566 update_screen_locked();
567 pthread_mutex_unlock(&updateMutex);
568}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700569
570void ScreenRecoveryUI::Redraw()
571{
572 pthread_mutex_lock(&updateMutex);
573 update_screen_locked();
574 pthread_mutex_unlock(&updateMutex);
575}