blob: 4a83a5c7319a85b62d384b1ce8475f854b3b89de [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"
Dees_Troy51a0e822012-09-05 15:24:24 -040036extern "C" {
37#include "minuitwrp/minui.h"
38int twgr_text(int x, int y, const char *s);
Dees_Troy32c8eb82012-09-11 15:28:06 -040039#include "gui/gui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040040}
Dees_Troy32c8eb82012-09-11 15:28:06 -040041#include "data.hpp"
Doug Zongker211aebc2011-10-28 15:13:10 -070042
Doug Zongker55a36ac2013-03-04 15:49:02 -080043static int char_width;
44static int char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -070045
Doug Zongker211aebc2011-10-28 15:13:10 -070046// There's only (at most) one of these objects, and global callbacks
47// (for pthread_create, and the input event system) need to find it,
48// so use a global variable.
49static ScreenRecoveryUI* self = NULL;
50
51// Return the current time as a double (including fractions of a second).
52static double now() {
53 struct timeval tv;
54 gettimeofday(&tv, NULL);
55 return tv.tv_sec + tv.tv_usec / 1000000.0;
56}
57
58ScreenRecoveryUI::ScreenRecoveryUI() :
59 currentIcon(NONE),
60 installingFrame(0),
Doug Zongker5fa8c232012-09-18 12:37:02 -070061 rtl_locale(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070062 progressBarType(EMPTY),
63 progressScopeStart(0),
64 progressScopeSize(0),
65 progress(0),
66 pagesIdentical(false),
67 text_cols(0),
68 text_rows(0),
69 text_col(0),
70 text_row(0),
71 text_top(0),
72 show_text(false),
73 show_text_ever(false),
74 show_menu(false),
75 menu_top(0),
76 menu_items(0),
77 menu_sel(0),
Doug Zongker32a0a472011-11-01 11:00:20 -070078
79 // These values are correct for the default image resources
80 // provided with the android platform. Devices which use
81 // different resources should have a subclass of ScreenRecoveryUI
82 // that overrides Init() to set these values appropriately and
83 // then call the superclass Init().
84 animation_fps(20),
85 indeterminate_frames(6),
86 installing_frames(7),
87 install_overlay_offset_x(13),
Doug Zongker8347cb22012-10-08 08:38:16 -070088 install_overlay_offset_y(190),
Doug Zongker52eeea4f2012-09-04 14:28:25 -070089 overlay_offset_x(-1),
90 overlay_offset_y(-1) {
yetta_wu2f6877a2013-06-25 15:03:11 +080091
92 for (int i = 0; i < 5; i++)
93 backgroundIcon[i] = NULL;
94
Doug Zongker211aebc2011-10-28 15:13:10 -070095 pthread_mutex_init(&updateMutex, NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -070096 self = this;
97}
98
99// Draw the given frame over the installation overlay animation. The
100// background is not cleared or draw with the base icon first; we
101// assume that the frame already contains some other frame of the
102// animation. Does nothing if no overlay animation is defined.
103// Should only be called with updateMutex locked.
104void ScreenRecoveryUI::draw_install_overlay_locked(int frame) {
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700105 if (installationOverlay == NULL || overlay_offset_x < 0) return;
Doug Zongker211aebc2011-10-28 15:13:10 -0700106 gr_surface surface = installationOverlay[frame];
107 int iconWidth = gr_get_width(surface);
108 int iconHeight = gr_get_height(surface);
109 gr_blit(surface, 0, 0, iconWidth, iconHeight,
Doug Zongker02ec6b82012-08-22 17:26:40 -0700110 overlay_offset_x, overlay_offset_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700111}
112
113// Clear the screen and draw the currently selected background icon (if any).
114// Should only be called with updateMutex locked.
115void ScreenRecoveryUI::draw_background_locked(Icon icon)
116{
117 pagesIdentical = false;
118 gr_color(0, 0, 0, 255);
119 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
120
121 if (icon) {
122 gr_surface surface = backgroundIcon[icon];
Doug Zongker02ec6b82012-08-22 17:26:40 -0700123 gr_surface text_surface = backgroundText[icon];
124
Doug Zongker211aebc2011-10-28 15:13:10 -0700125 int iconWidth = gr_get_width(surface);
126 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700127 int textWidth = gr_get_width(text_surface);
128 int textHeight = gr_get_height(text_surface);
129
Doug Zongker211aebc2011-10-28 15:13:10 -0700130 int iconX = (gr_fb_width() - iconWidth) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700131 int iconY = (gr_fb_height() - (iconHeight+textHeight+40)) / 2;
132
133 int textX = (gr_fb_width() - textWidth) / 2;
134 int textY = ((gr_fb_height() - (iconHeight+textHeight+40)) / 2) + iconHeight + 40;
135
Doug Zongker211aebc2011-10-28 15:13:10 -0700136 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700137 if (icon == INSTALLING_UPDATE || icon == ERASING) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700138 draw_install_overlay_locked(installingFrame);
139 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700140
141 gr_color(255, 255, 255, 255);
142 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700143 }
144}
145
146// Draw the progress bar (if any) on the screen. Does not flip pages.
147// Should only be called with updateMutex locked.
148void ScreenRecoveryUI::draw_progress_locked()
149{
Doug Zongker69f4b672012-04-26 14:37:53 -0700150 if (currentIcon == ERROR) return;
151
Doug Zongker02ec6b82012-08-22 17:26:40 -0700152 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700153 draw_install_overlay_locked(installingFrame);
154 }
155
156 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700157 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700158 int width = gr_get_width(progressBarEmpty);
159 int height = gr_get_height(progressBarEmpty);
160
161 int dx = (gr_fb_width() - width)/2;
162 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
163
164 // Erase behind the progress bar (in case this was a progress-only update)
165 gr_color(0, 0, 0, 255);
166 gr_fill(dx, dy, width, height);
167
168 if (progressBarType == DETERMINATE) {
169 float p = progressScopeStart + progress * progressScopeSize;
170 int pos = (int) (p * width);
171
Doug Zongker5fa8c232012-09-18 12:37:02 -0700172 if (rtl_locale) {
173 // Fill the progress bar from right to left.
174 if (pos > 0) {
175 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
176 }
177 if (pos < width-1) {
178 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
179 }
180 } else {
181 // Fill the progress bar from left to right.
182 if (pos > 0) {
183 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
184 }
185 if (pos < width-1) {
186 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
187 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700188 }
189 }
190
191 if (progressBarType == INDETERMINATE) {
192 static int frame = 0;
193 gr_blit(progressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700194 // in RTL locales, we run the animation backwards, which
195 // makes the spinner spin the other way.
196 if (rtl_locale) {
197 frame = (frame + indeterminate_frames - 1) % indeterminate_frames;
198 } else {
199 frame = (frame + 1) % indeterminate_frames;
200 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700201 }
202 }
203}
204
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800205#define C_HEADER 247,0,6
206#define C_MENU 0,106,157
207#define C_LOG 249,194,0
Doug Zongker211aebc2011-10-28 15:13:10 -0700208
209// Redraw everything on the screen. Does not flip pages.
210// Should only be called with updateMutex locked.
211void ScreenRecoveryUI::draw_screen_locked()
212{
213 draw_background_locked(currentIcon);
214 draw_progress_locked();
215
216 if (show_text) {
217 gr_color(0, 0, 0, 160);
218 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
219
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800220 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700221 int i = 0;
222 if (show_menu) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800223 gr_color(C_HEADER, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700224
225 for (; i < menu_top + menu_items; ++i) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800226 if (i == menu_top) gr_color(C_MENU, 255);
227
Doug Zongker211aebc2011-10-28 15:13:10 -0700228 if (i == menu_top + menu_sel) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800229 // draw the highlight bar
230 gr_fill(0, y-2, gr_fb_width(), y+char_height+2);
231 // white text of selected item
Doug Zongker211aebc2011-10-28 15:13:10 -0700232 gr_color(255, 255, 255, 255);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800233 if (menu[i][0]) gr_text(4, y, menu[i], 1);
234 gr_color(C_MENU, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700235 } else {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800236 if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top);
Doug Zongker211aebc2011-10-28 15:13:10 -0700237 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800238 y += char_height+4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700239 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800240 gr_color(C_MENU, 255);
241 y += 4;
242 gr_fill(0, y, gr_fb_width(), y+2);
243 y += 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700244 ++i;
245 }
246
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800247 gr_color(C_LOG, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700248
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800249 // display from the bottom up, until we hit the top of the
250 // screen, the bottom of the menu, or we've displayed the
251 // entire text buffer.
252 int ty;
253 int row = (text_top+text_rows-1) % text_rows;
254 for (int ty = gr_fb_height() - char_height, count = 0;
255 ty > y+2 && count < text_rows;
256 ty -= char_height, ++count) {
257 gr_text(4, ty, text[row], 0);
258 --row;
259 if (row < 0) row = text_rows-1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700260 }
261 }
262}
263
264// Redraw everything on the screen and flip the screen (make it visible).
265// Should only be called with updateMutex locked.
266void ScreenRecoveryUI::update_screen_locked()
267{
268 draw_screen_locked();
269 gr_flip();
270}
271
272// Updates only the progress bar, if possible, otherwise redraws the screen.
273// Should only be called with updateMutex locked.
274void ScreenRecoveryUI::update_progress_locked()
Dees_Troy32c8eb82012-09-11 15:28:06 -0400275{return;
Doug Zongker211aebc2011-10-28 15:13:10 -0700276 if (show_text || !pagesIdentical) {
277 draw_screen_locked(); // Must redraw the whole screen
278 pagesIdentical = true;
279 } else {
280 draw_progress_locked(); // Draw only the progress bar and overlays
281 }
282 gr_flip();
283}
284
285// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700286void* ScreenRecoveryUI::progress_thread(void *cookie) {
287 self->progress_loop();
288 return NULL;
289}
290
291void ScreenRecoveryUI::progress_loop() {
292 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700293 for (;;) {
294 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700295 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700296
297 int redraw = 0;
298
299 // update the installation animation, if active
300 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700301 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
302 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700303 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700304 redraw = 1;
305 }
306
307 // update the progress bar animation, if active
308 // skip this if we have a text overlay (too expensive to update)
Doug Zongker32a0a472011-11-01 11:00:20 -0700309 if (progressBarType == INDETERMINATE && !show_text) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700310 redraw = 1;
311 }
312
313 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700314 int duration = progressScopeDuration;
315 if (progressBarType == DETERMINATE && duration > 0) {
316 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700317 float p = 1.0 * elapsed / duration;
318 if (p > 1.0) p = 1.0;
319 if (p > progress) {
320 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700321 redraw = 1;
322 }
323 }
324
Doug Zongker32a0a472011-11-01 11:00:20 -0700325 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700326
Doug Zongker32a0a472011-11-01 11:00:20 -0700327 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700328 double end = now();
329 // minimum of 20ms delay between frames
330 double delay = interval - (end-start);
331 if (delay < 0.02) delay = 0.02;
332 usleep((long)(delay * 1000000));
333 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700334}
335
336void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
337 int result = res_create_surface(filename, surface);
338 if (result < 0) {
339 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
340 }
341}
342
Doug Zongker02ec6b82012-08-22 17:26:40 -0700343void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
344 int result = res_create_localized_surface(filename, surface);
345 if (result < 0) {
346 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
347 }
348}
349
Doug Zongker211aebc2011-10-28 15:13:10 -0700350void ScreenRecoveryUI::Init()
351{
352 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700353
Doug Zongker55a36ac2013-03-04 15:49:02 -0800354 gr_font_size(&char_width, &char_height);
355
Doug Zongker211aebc2011-10-28 15:13:10 -0700356 text_col = text_row = 0;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800357 text_rows = gr_fb_height() / char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -0700358 if (text_rows > kMaxRows) text_rows = kMaxRows;
359 text_top = 1;
360
Doug Zongker55a36ac2013-03-04 15:49:02 -0800361 text_cols = gr_fb_width() / char_width;
Doug Zongker211aebc2011-10-28 15:13:10 -0700362 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
363
Doug Zongker02ec6b82012-08-22 17:26:40 -0700364 LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]);
365 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700366 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700367 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
368
Doug Zongker211aebc2011-10-28 15:13:10 -0700369 LoadBitmap("progress_empty", &progressBarEmpty);
370 LoadBitmap("progress_fill", &progressBarFill);
371
Doug Zongker02ec6b82012-08-22 17:26:40 -0700372 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
373 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
374 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
375 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
376
Doug Zongker211aebc2011-10-28 15:13:10 -0700377 int i;
378
Doug Zongker32a0a472011-11-01 11:00:20 -0700379 progressBarIndeterminate = (gr_surface*)malloc(indeterminate_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700380 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700381 for (i = 0; i < indeterminate_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700382 char filename[40];
383 // "indeterminate01.png", "indeterminate02.png", ...
384 sprintf(filename, "indeterminate%02d", i+1);
385 LoadBitmap(filename, progressBarIndeterminate+i);
386 }
387
Doug Zongker32a0a472011-11-01 11:00:20 -0700388 if (installing_frames > 0) {
389 installationOverlay = (gr_surface*)malloc(installing_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700390 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700391 for (i = 0; i < installing_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700392 char filename[40];
393 // "icon_installing_overlay01.png",
394 // "icon_installing_overlay02.png", ...
395 sprintf(filename, "icon_installing_overlay%02d", i+1);
396 LoadBitmap(filename, installationOverlay+i);
397 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700398 } else {
399 installationOverlay = NULL;
400 }
401
402 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700403
404 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700405}
406
Doug Zongker5fa8c232012-09-18 12:37:02 -0700407void ScreenRecoveryUI::SetLocale(const char* locale) {
408 if (locale) {
409 char* lang = strdup(locale);
410 for (char* p = lang; *p; ++p) {
411 if (*p == '_') {
412 *p = '\0';
413 break;
414 }
415 }
416
417 // A bit cheesy: keep an explicit list of supported languages
418 // that are RTL.
419 if (strcmp(lang, "ar") == 0 || // Arabic
420 strcmp(lang, "fa") == 0 || // Persian (Farsi)
421 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700422 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
423 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700424 rtl_locale = true;
425 }
426 free(lang);
427 }
428}
429
Doug Zongker211aebc2011-10-28 15:13:10 -0700430void ScreenRecoveryUI::SetBackground(Icon icon)
431{
432 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700433
434 // Adjust the offset to account for the positioning of the
435 // base image on the screen.
436 if (backgroundIcon[icon] != NULL) {
437 gr_surface bg = backgroundIcon[icon];
438 gr_surface text = backgroundText[icon];
439 overlay_offset_x = install_overlay_offset_x + (gr_fb_width() - gr_get_width(bg)) / 2;
440 overlay_offset_y = install_overlay_offset_y +
441 (gr_fb_height() - (gr_get_height(bg) + gr_get_height(text) + 40)) / 2;
442 }
443
Doug Zongker211aebc2011-10-28 15:13:10 -0700444 currentIcon = icon;
445 update_screen_locked();
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700446
Doug Zongker211aebc2011-10-28 15:13:10 -0700447 pthread_mutex_unlock(&updateMutex);
448}
449
450void ScreenRecoveryUI::SetProgressType(ProgressType type)
451{
452 pthread_mutex_lock(&updateMutex);
453 if (progressBarType != type) {
454 progressBarType = type;
455 update_progress_locked();
456 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700457 progressScopeStart = 0;
458 progress = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700459 pthread_mutex_unlock(&updateMutex);
460}
461
462void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
463{
Dees_Troy32c8eb82012-09-11 15:28:06 -0400464 DataManager::SetValue("ui_progress_portion", (float)(portion * 100.0));
465 DataManager::SetValue("ui_progress_frames", seconds * 30);
466
Doug Zongker211aebc2011-10-28 15:13:10 -0700467 pthread_mutex_lock(&updateMutex);
468 progressBarType = DETERMINATE;
469 progressScopeStart += progressScopeSize;
470 progressScopeSize = portion;
471 progressScopeTime = now();
472 progressScopeDuration = seconds;
473 progress = 0;
474 update_progress_locked();
475 pthread_mutex_unlock(&updateMutex);
476}
477
478void ScreenRecoveryUI::SetProgress(float fraction)
479{
Dees_Troy32c8eb82012-09-11 15:28:06 -0400480 DataManager::SetValue("ui_progress", (float) (fraction * 100.0)); return;
481
Doug Zongker211aebc2011-10-28 15:13:10 -0700482 pthread_mutex_lock(&updateMutex);
483 if (fraction < 0.0) fraction = 0.0;
484 if (fraction > 1.0) fraction = 1.0;
485 if (progressBarType == DETERMINATE && fraction > progress) {
486 // Skip updates that aren't visibly different.
487 int width = gr_get_width(progressBarIndeterminate[0]);
488 float scale = width * progressScopeSize;
489 if ((int) (progress * scale) != (int) (fraction * scale)) {
490 progress = fraction;
491 update_progress_locked();
492 }
493 }
494 pthread_mutex_unlock(&updateMutex);
495}
496
497void ScreenRecoveryUI::Print(const char *fmt, ...)
498{
499 char buf[256];
500 va_list ap;
501 va_start(ap, fmt);
502 vsnprintf(buf, 256, fmt, ap);
503 va_end(ap);
504
Dees_Troy32c8eb82012-09-11 15:28:06 -0400505 gui_print("%s", buf);
506 return;
507
Doug Zongker211aebc2011-10-28 15:13:10 -0700508 fputs(buf, stdout);
509
510 // This can get called before ui_init(), so be careful.
511 pthread_mutex_lock(&updateMutex);
512 if (text_rows > 0 && text_cols > 0) {
513 char *ptr;
514 for (ptr = buf; *ptr != '\0'; ++ptr) {
515 if (*ptr == '\n' || text_col >= text_cols) {
516 text[text_row][text_col] = '\0';
517 text_col = 0;
518 text_row = (text_row + 1) % text_rows;
519 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
520 }
521 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
522 }
523 text[text_row][text_col] = '\0';
524 update_screen_locked();
525 }
526 pthread_mutex_unlock(&updateMutex);
527}
528
529void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
530 int initial_selection) {
531 int i;
532 pthread_mutex_lock(&updateMutex);
533 if (text_rows > 0 && text_cols > 0) {
534 for (i = 0; i < text_rows; ++i) {
535 if (headers[i] == NULL) break;
536 strncpy(menu[i], headers[i], text_cols-1);
537 menu[i][text_cols-1] = '\0';
538 }
539 menu_top = i;
540 for (; i < text_rows; ++i) {
541 if (items[i-menu_top] == NULL) break;
542 strncpy(menu[i], items[i-menu_top], text_cols-1);
543 menu[i][text_cols-1] = '\0';
544 }
545 menu_items = i - menu_top;
546 show_menu = 1;
547 menu_sel = initial_selection;
548 update_screen_locked();
549 }
550 pthread_mutex_unlock(&updateMutex);
551}
552
553int ScreenRecoveryUI::SelectMenu(int sel) {
554 int old_sel;
555 pthread_mutex_lock(&updateMutex);
556 if (show_menu > 0) {
557 old_sel = menu_sel;
558 menu_sel = sel;
559 if (menu_sel < 0) menu_sel = 0;
560 if (menu_sel >= menu_items) menu_sel = menu_items-1;
561 sel = menu_sel;
562 if (menu_sel != old_sel) update_screen_locked();
563 }
564 pthread_mutex_unlock(&updateMutex);
565 return sel;
566}
567
568void ScreenRecoveryUI::EndMenu() {
569 int i;
570 pthread_mutex_lock(&updateMutex);
571 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
572 show_menu = 0;
573 update_screen_locked();
574 }
575 pthread_mutex_unlock(&updateMutex);
576}
577
578bool ScreenRecoveryUI::IsTextVisible()
579{
580 pthread_mutex_lock(&updateMutex);
581 int visible = show_text;
582 pthread_mutex_unlock(&updateMutex);
583 return visible;
584}
585
586bool ScreenRecoveryUI::WasTextEverVisible()
587{
588 pthread_mutex_lock(&updateMutex);
589 int ever_visible = show_text_ever;
590 pthread_mutex_unlock(&updateMutex);
591 return ever_visible;
592}
593
594void ScreenRecoveryUI::ShowText(bool visible)
595{
596 pthread_mutex_lock(&updateMutex);
597 show_text = visible;
598 if (show_text) show_text_ever = 1;
599 update_screen_locked();
600 pthread_mutex_unlock(&updateMutex);
601}