blob: 6a638582e1dfa31a03e0db46d7cabe89239149aa [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 Zongker5fa8c232012-09-18 12:37:02 -070055 rtl_locale(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070056 progressBarType(EMPTY),
57 progressScopeStart(0),
58 progressScopeSize(0),
59 progress(0),
60 pagesIdentical(false),
61 text_cols(0),
62 text_rows(0),
63 text_col(0),
64 text_row(0),
65 text_top(0),
66 show_text(false),
67 show_text_ever(false),
68 show_menu(false),
69 menu_top(0),
70 menu_items(0),
71 menu_sel(0),
Doug Zongker32a0a472011-11-01 11:00:20 -070072
73 // These values are correct for the default image resources
74 // provided with the android platform. Devices which use
75 // different resources should have a subclass of ScreenRecoveryUI
76 // that overrides Init() to set these values appropriately and
77 // then call the superclass Init().
78 animation_fps(20),
Doug Zongker8347cb22012-10-08 08:38:16 -070079 indeterminate_frames(6),
80 installing_frames(7),
81 install_overlay_offset_x(13),
82 install_overlay_offset_y(190),
Doug Zongker52eeea42012-09-04 14:28:25 -070083 overlay_offset_x(-1),
84 overlay_offset_y(-1) {
yetta_wu5b468fc2013-06-25 15:03:11 +080085
86 for (int i = 0; i < 5; i++)
87 backgroundIcon[i] = NULL;
88
Doug Zongker211aebc2011-10-28 15:13:10 -070089 pthread_mutex_init(&updateMutex, NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -070090 self = this;
91}
92
93// Draw the given frame over the installation overlay animation. The
94// background is not cleared or draw with the base icon first; we
95// assume that the frame already contains some other frame of the
96// animation. Does nothing if no overlay animation is defined.
97// Should only be called with updateMutex locked.
98void ScreenRecoveryUI::draw_install_overlay_locked(int frame) {
Doug Zongker52eeea42012-09-04 14:28:25 -070099 if (installationOverlay == NULL || overlay_offset_x < 0) return;
Doug Zongker211aebc2011-10-28 15:13:10 -0700100 gr_surface surface = installationOverlay[frame];
101 int iconWidth = gr_get_width(surface);
102 int iconHeight = gr_get_height(surface);
103 gr_blit(surface, 0, 0, iconWidth, iconHeight,
Doug Zongker02ec6b82012-08-22 17:26:40 -0700104 overlay_offset_x, overlay_offset_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700105}
106
107// Clear the screen and draw the currently selected background icon (if any).
108// Should only be called with updateMutex locked.
109void ScreenRecoveryUI::draw_background_locked(Icon icon)
110{
111 pagesIdentical = false;
112 gr_color(0, 0, 0, 255);
113 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
114
115 if (icon) {
116 gr_surface surface = backgroundIcon[icon];
Doug Zongker02ec6b82012-08-22 17:26:40 -0700117 gr_surface text_surface = backgroundText[icon];
118
Doug Zongker211aebc2011-10-28 15:13:10 -0700119 int iconWidth = gr_get_width(surface);
120 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700121 int textWidth = gr_get_width(text_surface);
122 int textHeight = gr_get_height(text_surface);
123
Doug Zongker211aebc2011-10-28 15:13:10 -0700124 int iconX = (gr_fb_width() - iconWidth) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700125 int iconY = (gr_fb_height() - (iconHeight+textHeight+40)) / 2;
126
127 int textX = (gr_fb_width() - textWidth) / 2;
128 int textY = ((gr_fb_height() - (iconHeight+textHeight+40)) / 2) + iconHeight + 40;
129
Doug Zongker211aebc2011-10-28 15:13:10 -0700130 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700131 if (icon == INSTALLING_UPDATE || icon == ERASING) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700132 draw_install_overlay_locked(installingFrame);
133 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700134
135 gr_color(255, 255, 255, 255);
136 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700137 }
138}
139
140// Draw the progress bar (if any) on the screen. Does not flip pages.
141// Should only be called with updateMutex locked.
142void ScreenRecoveryUI::draw_progress_locked()
143{
Doug Zongker69f4b672012-04-26 14:37:53 -0700144 if (currentIcon == ERROR) return;
145
Doug Zongker02ec6b82012-08-22 17:26:40 -0700146 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700147 draw_install_overlay_locked(installingFrame);
148 }
149
150 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700151 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700152 int width = gr_get_width(progressBarEmpty);
153 int height = gr_get_height(progressBarEmpty);
154
155 int dx = (gr_fb_width() - width)/2;
156 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
157
158 // Erase behind the progress bar (in case this was a progress-only update)
159 gr_color(0, 0, 0, 255);
160 gr_fill(dx, dy, width, height);
161
162 if (progressBarType == DETERMINATE) {
163 float p = progressScopeStart + progress * progressScopeSize;
164 int pos = (int) (p * width);
165
Doug Zongker5fa8c232012-09-18 12:37:02 -0700166 if (rtl_locale) {
167 // Fill the progress bar from right to left.
168 if (pos > 0) {
169 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
170 }
171 if (pos < width-1) {
172 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
173 }
174 } else {
175 // Fill the progress bar from left to right.
176 if (pos > 0) {
177 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
178 }
179 if (pos < width-1) {
180 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
181 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700182 }
183 }
184
185 if (progressBarType == INDETERMINATE) {
186 static int frame = 0;
187 gr_blit(progressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700188 // in RTL locales, we run the animation backwards, which
189 // makes the spinner spin the other way.
190 if (rtl_locale) {
191 frame = (frame + indeterminate_frames - 1) % indeterminate_frames;
192 } else {
193 frame = (frame + 1) % indeterminate_frames;
194 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700195 }
196 }
197}
198
Doug Zongkerc0441d12013-07-31 11:28:24 -0700199void ScreenRecoveryUI::SetColor(UIElement e) {
200 switch (e) {
201 case HEADER:
202 gr_color(247, 0, 6, 255);
203 break;
204 case MENU:
205 case MENU_SEL_BG:
206 gr_color(0, 106, 157, 255);
207 break;
208 case MENU_SEL_FG:
209 gr_color(255, 255, 255, 255);
210 break;
211 case LOG:
212 gr_color(249, 194, 0, 255);
213 break;
214 case TEXT_FILL:
215 gr_color(0, 0, 0, 160);
216 break;
217 default:
218 gr_color(255, 255, 255, 255);
219 break;
220 }
221}
Doug Zongker211aebc2011-10-28 15:13:10 -0700222
223// Redraw everything on the screen. Does not flip pages.
224// Should only be called with updateMutex locked.
225void ScreenRecoveryUI::draw_screen_locked()
226{
227 draw_background_locked(currentIcon);
228 draw_progress_locked();
229
230 if (show_text) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700231 SetColor(TEXT_FILL);
Doug Zongker211aebc2011-10-28 15:13:10 -0700232 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
233
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800234 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700235 int i = 0;
236 if (show_menu) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700237 SetColor(HEADER);
Doug Zongker211aebc2011-10-28 15:13:10 -0700238
239 for (; i < menu_top + menu_items; ++i) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700240 if (i == menu_top) SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800241
Doug Zongker211aebc2011-10-28 15:13:10 -0700242 if (i == menu_top + menu_sel) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800243 // draw the highlight bar
Doug Zongkerc0441d12013-07-31 11:28:24 -0700244 SetColor(MENU_SEL_BG);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800245 gr_fill(0, y-2, gr_fb_width(), y+char_height+2);
246 // white text of selected item
Doug Zongkerc0441d12013-07-31 11:28:24 -0700247 SetColor(MENU_SEL_FG);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800248 if (menu[i][0]) gr_text(4, y, menu[i], 1);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700249 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700250 } else {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800251 if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top);
Doug Zongker211aebc2011-10-28 15:13:10 -0700252 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800253 y += char_height+4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700254 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700255 SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800256 y += 4;
257 gr_fill(0, y, gr_fb_width(), y+2);
258 y += 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700259 ++i;
260 }
261
Doug Zongkerc0441d12013-07-31 11:28:24 -0700262 SetColor(LOG);
Doug Zongker211aebc2011-10-28 15:13:10 -0700263
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800264 // display from the bottom up, until we hit the top of the
265 // screen, the bottom of the menu, or we've displayed the
266 // entire text buffer.
267 int ty;
268 int row = (text_top+text_rows-1) % text_rows;
269 for (int ty = gr_fb_height() - char_height, count = 0;
270 ty > y+2 && count < text_rows;
271 ty -= char_height, ++count) {
272 gr_text(4, ty, text[row], 0);
273 --row;
274 if (row < 0) row = text_rows-1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700275 }
276 }
277}
278
279// Redraw everything on the screen and flip the screen (make it visible).
280// Should only be called with updateMutex locked.
281void ScreenRecoveryUI::update_screen_locked()
282{
283 draw_screen_locked();
284 gr_flip();
285}
286
287// Updates only the progress bar, if possible, otherwise redraws the screen.
288// Should only be called with updateMutex locked.
289void ScreenRecoveryUI::update_progress_locked()
290{
291 if (show_text || !pagesIdentical) {
292 draw_screen_locked(); // Must redraw the whole screen
293 pagesIdentical = true;
294 } else {
295 draw_progress_locked(); // Draw only the progress bar and overlays
296 }
297 gr_flip();
298}
299
300// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700301void* ScreenRecoveryUI::progress_thread(void *cookie) {
302 self->progress_loop();
303 return NULL;
304}
305
306void ScreenRecoveryUI::progress_loop() {
307 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700308 for (;;) {
309 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700310 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700311
312 int redraw = 0;
313
314 // update the installation animation, if active
315 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700316 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
317 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700318 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700319 redraw = 1;
320 }
321
322 // update the progress bar animation, if active
323 // skip this if we have a text overlay (too expensive to update)
Doug Zongker32a0a472011-11-01 11:00:20 -0700324 if (progressBarType == INDETERMINATE && !show_text) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700325 redraw = 1;
326 }
327
328 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700329 int duration = progressScopeDuration;
330 if (progressBarType == DETERMINATE && duration > 0) {
331 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700332 float p = 1.0 * elapsed / duration;
333 if (p > 1.0) p = 1.0;
334 if (p > progress) {
335 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700336 redraw = 1;
337 }
338 }
339
Doug Zongker32a0a472011-11-01 11:00:20 -0700340 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700341
Doug Zongker32a0a472011-11-01 11:00:20 -0700342 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700343 double end = now();
344 // minimum of 20ms delay between frames
345 double delay = interval - (end-start);
346 if (delay < 0.02) delay = 0.02;
347 usleep((long)(delay * 1000000));
348 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700349}
350
351void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
352 int result = res_create_surface(filename, surface);
353 if (result < 0) {
354 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
355 }
356}
357
Doug Zongker02ec6b82012-08-22 17:26:40 -0700358void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
359 int result = res_create_localized_surface(filename, surface);
360 if (result < 0) {
361 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
362 }
363}
364
Doug Zongker211aebc2011-10-28 15:13:10 -0700365void ScreenRecoveryUI::Init()
366{
367 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700368
Doug Zongker55a36ac2013-03-04 15:49:02 -0800369 gr_font_size(&char_width, &char_height);
370
Doug Zongker211aebc2011-10-28 15:13:10 -0700371 text_col = text_row = 0;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800372 text_rows = gr_fb_height() / char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -0700373 if (text_rows > kMaxRows) text_rows = kMaxRows;
374 text_top = 1;
375
Doug Zongker55a36ac2013-03-04 15:49:02 -0800376 text_cols = gr_fb_width() / char_width;
Doug Zongker211aebc2011-10-28 15:13:10 -0700377 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
378
Doug Zongker02ec6b82012-08-22 17:26:40 -0700379 LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]);
380 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700381 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700382 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
383
Doug Zongker211aebc2011-10-28 15:13:10 -0700384 LoadBitmap("progress_empty", &progressBarEmpty);
385 LoadBitmap("progress_fill", &progressBarFill);
386
Doug Zongker02ec6b82012-08-22 17:26:40 -0700387 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
388 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
389 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
390 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
391
Doug Zongker211aebc2011-10-28 15:13:10 -0700392 int i;
393
Doug Zongker32a0a472011-11-01 11:00:20 -0700394 progressBarIndeterminate = (gr_surface*)malloc(indeterminate_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700395 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700396 for (i = 0; i < indeterminate_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700397 char filename[40];
398 // "indeterminate01.png", "indeterminate02.png", ...
399 sprintf(filename, "indeterminate%02d", i+1);
400 LoadBitmap(filename, progressBarIndeterminate+i);
401 }
402
Doug Zongker32a0a472011-11-01 11:00:20 -0700403 if (installing_frames > 0) {
404 installationOverlay = (gr_surface*)malloc(installing_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700405 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700406 for (i = 0; i < installing_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700407 char filename[40];
408 // "icon_installing_overlay01.png",
409 // "icon_installing_overlay02.png", ...
410 sprintf(filename, "icon_installing_overlay%02d", i+1);
411 LoadBitmap(filename, installationOverlay+i);
412 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700413 } else {
414 installationOverlay = NULL;
415 }
416
417 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700418
419 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700420}
421
Doug Zongker5fa8c232012-09-18 12:37:02 -0700422void ScreenRecoveryUI::SetLocale(const char* locale) {
423 if (locale) {
424 char* lang = strdup(locale);
425 for (char* p = lang; *p; ++p) {
426 if (*p == '_') {
427 *p = '\0';
428 break;
429 }
430 }
431
432 // A bit cheesy: keep an explicit list of supported languages
433 // that are RTL.
434 if (strcmp(lang, "ar") == 0 || // Arabic
435 strcmp(lang, "fa") == 0 || // Persian (Farsi)
436 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700437 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
438 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700439 rtl_locale = true;
440 }
441 free(lang);
442 }
443}
444
Doug Zongker211aebc2011-10-28 15:13:10 -0700445void ScreenRecoveryUI::SetBackground(Icon icon)
446{
447 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700448
449 // Adjust the offset to account for the positioning of the
450 // base image on the screen.
451 if (backgroundIcon[icon] != NULL) {
452 gr_surface bg = backgroundIcon[icon];
453 gr_surface text = backgroundText[icon];
454 overlay_offset_x = install_overlay_offset_x + (gr_fb_width() - gr_get_width(bg)) / 2;
455 overlay_offset_y = install_overlay_offset_y +
456 (gr_fb_height() - (gr_get_height(bg) + gr_get_height(text) + 40)) / 2;
457 }
458
Doug Zongker52eeea42012-09-04 14:28:25 -0700459 currentIcon = icon;
460 update_screen_locked();
461
Doug Zongker211aebc2011-10-28 15:13:10 -0700462 pthread_mutex_unlock(&updateMutex);
463}
464
465void ScreenRecoveryUI::SetProgressType(ProgressType type)
466{
467 pthread_mutex_lock(&updateMutex);
468 if (progressBarType != type) {
469 progressBarType = type;
470 update_progress_locked();
471 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700472 progressScopeStart = 0;
473 progress = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700474 pthread_mutex_unlock(&updateMutex);
475}
476
477void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
478{
479 pthread_mutex_lock(&updateMutex);
480 progressBarType = DETERMINATE;
481 progressScopeStart += progressScopeSize;
482 progressScopeSize = portion;
483 progressScopeTime = now();
484 progressScopeDuration = seconds;
485 progress = 0;
486 update_progress_locked();
487 pthread_mutex_unlock(&updateMutex);
488}
489
490void ScreenRecoveryUI::SetProgress(float fraction)
491{
492 pthread_mutex_lock(&updateMutex);
493 if (fraction < 0.0) fraction = 0.0;
494 if (fraction > 1.0) fraction = 1.0;
495 if (progressBarType == DETERMINATE && fraction > progress) {
496 // Skip updates that aren't visibly different.
497 int width = gr_get_width(progressBarIndeterminate[0]);
498 float scale = width * progressScopeSize;
499 if ((int) (progress * scale) != (int) (fraction * scale)) {
500 progress = fraction;
501 update_progress_locked();
502 }
503 }
504 pthread_mutex_unlock(&updateMutex);
505}
506
507void ScreenRecoveryUI::Print(const char *fmt, ...)
508{
509 char buf[256];
510 va_list ap;
511 va_start(ap, fmt);
512 vsnprintf(buf, 256, fmt, ap);
513 va_end(ap);
514
515 fputs(buf, stdout);
516
517 // This can get called before ui_init(), so be careful.
518 pthread_mutex_lock(&updateMutex);
519 if (text_rows > 0 && text_cols > 0) {
520 char *ptr;
521 for (ptr = buf; *ptr != '\0'; ++ptr) {
522 if (*ptr == '\n' || text_col >= text_cols) {
523 text[text_row][text_col] = '\0';
524 text_col = 0;
525 text_row = (text_row + 1) % text_rows;
526 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
527 }
528 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
529 }
530 text[text_row][text_col] = '\0';
531 update_screen_locked();
532 }
533 pthread_mutex_unlock(&updateMutex);
534}
535
536void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
537 int initial_selection) {
538 int i;
539 pthread_mutex_lock(&updateMutex);
540 if (text_rows > 0 && text_cols > 0) {
541 for (i = 0; i < text_rows; ++i) {
542 if (headers[i] == NULL) break;
543 strncpy(menu[i], headers[i], text_cols-1);
544 menu[i][text_cols-1] = '\0';
545 }
546 menu_top = i;
547 for (; i < text_rows; ++i) {
548 if (items[i-menu_top] == NULL) break;
549 strncpy(menu[i], items[i-menu_top], text_cols-1);
550 menu[i][text_cols-1] = '\0';
551 }
552 menu_items = i - menu_top;
553 show_menu = 1;
554 menu_sel = initial_selection;
555 update_screen_locked();
556 }
557 pthread_mutex_unlock(&updateMutex);
558}
559
560int ScreenRecoveryUI::SelectMenu(int sel) {
561 int old_sel;
562 pthread_mutex_lock(&updateMutex);
563 if (show_menu > 0) {
564 old_sel = menu_sel;
565 menu_sel = sel;
566 if (menu_sel < 0) menu_sel = 0;
567 if (menu_sel >= menu_items) menu_sel = menu_items-1;
568 sel = menu_sel;
569 if (menu_sel != old_sel) update_screen_locked();
570 }
571 pthread_mutex_unlock(&updateMutex);
572 return sel;
573}
574
575void ScreenRecoveryUI::EndMenu() {
576 int i;
577 pthread_mutex_lock(&updateMutex);
578 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
579 show_menu = 0;
580 update_screen_locked();
581 }
582 pthread_mutex_unlock(&updateMutex);
583}
584
585bool ScreenRecoveryUI::IsTextVisible()
586{
587 pthread_mutex_lock(&updateMutex);
588 int visible = show_text;
589 pthread_mutex_unlock(&updateMutex);
590 return visible;
591}
592
593bool ScreenRecoveryUI::WasTextEverVisible()
594{
595 pthread_mutex_lock(&updateMutex);
596 int ever_visible = show_text_ever;
597 pthread_mutex_unlock(&updateMutex);
598 return ever_visible;
599}
600
601void ScreenRecoveryUI::ShowText(bool visible)
602{
603 pthread_mutex_lock(&updateMutex);
604 show_text = visible;
605 if (show_text) show_text_ever = 1;
606 update_screen_locked();
607 pthread_mutex_unlock(&updateMutex);
608}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700609
610void ScreenRecoveryUI::Redraw()
611{
612 pthread_mutex_lock(&updateMutex);
613 update_screen_locked();
614 pthread_mutex_unlock(&updateMutex);
615}