blob: be7abca1d63c2913383df39a253bf6ac4961c9cf [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 Zongker52eeea4f2012-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 Zongker52eeea4f2012-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
Alistair Strachan9b8ae802013-07-17 10:34:36 -0700379 backgroundIcon[NONE] = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700380 LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]);
381 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700382 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700383 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
384
Doug Zongker211aebc2011-10-28 15:13:10 -0700385 LoadBitmap("progress_empty", &progressBarEmpty);
386 LoadBitmap("progress_fill", &progressBarFill);
387
Doug Zongker02ec6b82012-08-22 17:26:40 -0700388 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
389 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
390 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
391 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
392
Doug Zongker211aebc2011-10-28 15:13:10 -0700393 int i;
394
Doug Zongker32a0a472011-11-01 11:00:20 -0700395 progressBarIndeterminate = (gr_surface*)malloc(indeterminate_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700396 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700397 for (i = 0; i < indeterminate_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700398 char filename[40];
399 // "indeterminate01.png", "indeterminate02.png", ...
400 sprintf(filename, "indeterminate%02d", i+1);
401 LoadBitmap(filename, progressBarIndeterminate+i);
402 }
403
Doug Zongker32a0a472011-11-01 11:00:20 -0700404 if (installing_frames > 0) {
405 installationOverlay = (gr_surface*)malloc(installing_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700406 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700407 for (i = 0; i < installing_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700408 char filename[40];
409 // "icon_installing_overlay01.png",
410 // "icon_installing_overlay02.png", ...
411 sprintf(filename, "icon_installing_overlay%02d", i+1);
412 LoadBitmap(filename, installationOverlay+i);
413 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700414 } else {
415 installationOverlay = NULL;
416 }
417
418 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700419
420 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700421}
422
Doug Zongker5fa8c232012-09-18 12:37:02 -0700423void ScreenRecoveryUI::SetLocale(const char* locale) {
424 if (locale) {
425 char* lang = strdup(locale);
426 for (char* p = lang; *p; ++p) {
427 if (*p == '_') {
428 *p = '\0';
429 break;
430 }
431 }
432
433 // A bit cheesy: keep an explicit list of supported languages
434 // that are RTL.
435 if (strcmp(lang, "ar") == 0 || // Arabic
436 strcmp(lang, "fa") == 0 || // Persian (Farsi)
437 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700438 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
439 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700440 rtl_locale = true;
441 }
442 free(lang);
443 }
444}
445
Doug Zongker211aebc2011-10-28 15:13:10 -0700446void ScreenRecoveryUI::SetBackground(Icon icon)
447{
448 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700449
450 // Adjust the offset to account for the positioning of the
451 // base image on the screen.
452 if (backgroundIcon[icon] != NULL) {
453 gr_surface bg = backgroundIcon[icon];
454 gr_surface text = backgroundText[icon];
455 overlay_offset_x = install_overlay_offset_x + (gr_fb_width() - gr_get_width(bg)) / 2;
456 overlay_offset_y = install_overlay_offset_y +
457 (gr_fb_height() - (gr_get_height(bg) + gr_get_height(text) + 40)) / 2;
458 }
459
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700460 currentIcon = icon;
461 update_screen_locked();
462
Doug Zongker211aebc2011-10-28 15:13:10 -0700463 pthread_mutex_unlock(&updateMutex);
464}
465
466void ScreenRecoveryUI::SetProgressType(ProgressType type)
467{
468 pthread_mutex_lock(&updateMutex);
469 if (progressBarType != type) {
470 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700471 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700472 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700473 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700474 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700475 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700476 pthread_mutex_unlock(&updateMutex);
477}
478
479void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
480{
481 pthread_mutex_lock(&updateMutex);
482 progressBarType = DETERMINATE;
483 progressScopeStart += progressScopeSize;
484 progressScopeSize = portion;
485 progressScopeTime = now();
486 progressScopeDuration = seconds;
487 progress = 0;
488 update_progress_locked();
489 pthread_mutex_unlock(&updateMutex);
490}
491
492void ScreenRecoveryUI::SetProgress(float fraction)
493{
494 pthread_mutex_lock(&updateMutex);
495 if (fraction < 0.0) fraction = 0.0;
496 if (fraction > 1.0) fraction = 1.0;
497 if (progressBarType == DETERMINATE && fraction > progress) {
498 // Skip updates that aren't visibly different.
499 int width = gr_get_width(progressBarIndeterminate[0]);
500 float scale = width * progressScopeSize;
501 if ((int) (progress * scale) != (int) (fraction * scale)) {
502 progress = fraction;
503 update_progress_locked();
504 }
505 }
506 pthread_mutex_unlock(&updateMutex);
507}
508
509void ScreenRecoveryUI::Print(const char *fmt, ...)
510{
511 char buf[256];
512 va_list ap;
513 va_start(ap, fmt);
514 vsnprintf(buf, 256, fmt, ap);
515 va_end(ap);
516
517 fputs(buf, stdout);
518
519 // This can get called before ui_init(), so be careful.
520 pthread_mutex_lock(&updateMutex);
521 if (text_rows > 0 && text_cols > 0) {
522 char *ptr;
523 for (ptr = buf; *ptr != '\0'; ++ptr) {
524 if (*ptr == '\n' || text_col >= text_cols) {
525 text[text_row][text_col] = '\0';
526 text_col = 0;
527 text_row = (text_row + 1) % text_rows;
528 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
529 }
530 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
531 }
532 text[text_row][text_col] = '\0';
533 update_screen_locked();
534 }
535 pthread_mutex_unlock(&updateMutex);
536}
537
538void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
539 int initial_selection) {
540 int i;
541 pthread_mutex_lock(&updateMutex);
542 if (text_rows > 0 && text_cols > 0) {
543 for (i = 0; i < text_rows; ++i) {
544 if (headers[i] == NULL) break;
545 strncpy(menu[i], headers[i], text_cols-1);
546 menu[i][text_cols-1] = '\0';
547 }
548 menu_top = i;
549 for (; i < text_rows; ++i) {
550 if (items[i-menu_top] == NULL) break;
551 strncpy(menu[i], items[i-menu_top], text_cols-1);
552 menu[i][text_cols-1] = '\0';
553 }
554 menu_items = i - menu_top;
555 show_menu = 1;
556 menu_sel = initial_selection;
557 update_screen_locked();
558 }
559 pthread_mutex_unlock(&updateMutex);
560}
561
562int ScreenRecoveryUI::SelectMenu(int sel) {
563 int old_sel;
564 pthread_mutex_lock(&updateMutex);
565 if (show_menu > 0) {
566 old_sel = menu_sel;
567 menu_sel = sel;
568 if (menu_sel < 0) menu_sel = 0;
569 if (menu_sel >= menu_items) menu_sel = menu_items-1;
570 sel = menu_sel;
571 if (menu_sel != old_sel) update_screen_locked();
572 }
573 pthread_mutex_unlock(&updateMutex);
574 return sel;
575}
576
577void ScreenRecoveryUI::EndMenu() {
578 int i;
579 pthread_mutex_lock(&updateMutex);
580 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
581 show_menu = 0;
582 update_screen_locked();
583 }
584 pthread_mutex_unlock(&updateMutex);
585}
586
587bool ScreenRecoveryUI::IsTextVisible()
588{
589 pthread_mutex_lock(&updateMutex);
590 int visible = show_text;
591 pthread_mutex_unlock(&updateMutex);
592 return visible;
593}
594
595bool ScreenRecoveryUI::WasTextEverVisible()
596{
597 pthread_mutex_lock(&updateMutex);
598 int ever_visible = show_text_ever;
599 pthread_mutex_unlock(&updateMutex);
600 return ever_visible;
601}
602
603void ScreenRecoveryUI::ShowText(bool visible)
604{
605 pthread_mutex_lock(&updateMutex);
606 show_text = visible;
607 if (show_text) show_text_ever = 1;
608 update_screen_locked();
609 pthread_mutex_unlock(&updateMutex);
610}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700611
612void ScreenRecoveryUI::Redraw()
613{
614 pthread_mutex_lock(&updateMutex);
615 update_screen_locked();
616 pthread_mutex_unlock(&updateMutex);
617}