blob: 7e7bd8a993148089569c9a492d4b4a85bf2d2688 [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 Zongkerc0441d12013-07-31 11:28:24 -0700205void ScreenRecoveryUI::SetColor(UIElement e) {
206 switch (e) {
207 case HEADER:
208 gr_color(247, 0, 6, 255);
209 break;
210 case MENU:
211 case MENU_SEL_BG:
212 gr_color(0, 106, 157, 255);
213 break;
214 case MENU_SEL_FG:
215 gr_color(255, 255, 255, 255);
216 break;
217 case LOG:
218 gr_color(249, 194, 0, 255);
219 break;
220 case TEXT_FILL:
221 gr_color(0, 0, 0, 160);
222 break;
223 default:
224 gr_color(255, 255, 255, 255);
225 break;
226 }
227}
Doug Zongker211aebc2011-10-28 15:13:10 -0700228
229// Redraw everything on the screen. Does not flip pages.
230// Should only be called with updateMutex locked.
231void ScreenRecoveryUI::draw_screen_locked()
232{
233 draw_background_locked(currentIcon);
234 draw_progress_locked();
235
236 if (show_text) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700237 SetColor(TEXT_FILL);
Doug Zongker211aebc2011-10-28 15:13:10 -0700238 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
239
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800240 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700241 int i = 0;
242 if (show_menu) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700243 SetColor(HEADER);
Doug Zongker211aebc2011-10-28 15:13:10 -0700244
245 for (; i < menu_top + menu_items; ++i) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700246 if (i == menu_top) SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800247
Doug Zongker211aebc2011-10-28 15:13:10 -0700248 if (i == menu_top + menu_sel) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800249 // draw the highlight bar
Doug Zongkerc0441d12013-07-31 11:28:24 -0700250 SetColor(MENU_SEL_BG);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800251 gr_fill(0, y-2, gr_fb_width(), y+char_height+2);
252 // white text of selected item
Doug Zongkerc0441d12013-07-31 11:28:24 -0700253 SetColor(MENU_SEL_FG);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800254 if (menu[i][0]) gr_text(4, y, menu[i], 1);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700255 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700256 } else {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800257 if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top);
Doug Zongker211aebc2011-10-28 15:13:10 -0700258 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800259 y += char_height+4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700260 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700261 SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800262 y += 4;
263 gr_fill(0, y, gr_fb_width(), y+2);
264 y += 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700265 ++i;
266 }
267
Doug Zongkerc0441d12013-07-31 11:28:24 -0700268 SetColor(LOG);
Doug Zongker211aebc2011-10-28 15:13:10 -0700269
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800270 // display from the bottom up, until we hit the top of the
271 // screen, the bottom of the menu, or we've displayed the
272 // entire text buffer.
273 int ty;
274 int row = (text_top+text_rows-1) % text_rows;
275 for (int ty = gr_fb_height() - char_height, count = 0;
276 ty > y+2 && count < text_rows;
277 ty -= char_height, ++count) {
278 gr_text(4, ty, text[row], 0);
279 --row;
280 if (row < 0) row = text_rows-1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700281 }
282 }
283}
284
285// Redraw everything on the screen and flip the screen (make it visible).
286// Should only be called with updateMutex locked.
287void ScreenRecoveryUI::update_screen_locked()
288{
289 draw_screen_locked();
290 gr_flip();
291}
292
293// Updates only the progress bar, if possible, otherwise redraws the screen.
294// Should only be called with updateMutex locked.
295void ScreenRecoveryUI::update_progress_locked()
Dees_Troy32c8eb82012-09-11 15:28:06 -0400296{return;
Doug Zongker211aebc2011-10-28 15:13:10 -0700297 if (show_text || !pagesIdentical) {
298 draw_screen_locked(); // Must redraw the whole screen
299 pagesIdentical = true;
300 } else {
301 draw_progress_locked(); // Draw only the progress bar and overlays
302 }
303 gr_flip();
304}
305
306// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700307void* ScreenRecoveryUI::progress_thread(void *cookie) {
308 self->progress_loop();
309 return NULL;
310}
311
312void ScreenRecoveryUI::progress_loop() {
313 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700314 for (;;) {
315 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700316 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700317
318 int redraw = 0;
319
320 // update the installation animation, if active
321 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700322 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
323 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700324 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700325 redraw = 1;
326 }
327
328 // update the progress bar animation, if active
329 // skip this if we have a text overlay (too expensive to update)
Doug Zongker32a0a472011-11-01 11:00:20 -0700330 if (progressBarType == INDETERMINATE && !show_text) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700331 redraw = 1;
332 }
333
334 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700335 int duration = progressScopeDuration;
336 if (progressBarType == DETERMINATE && duration > 0) {
337 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700338 float p = 1.0 * elapsed / duration;
339 if (p > 1.0) p = 1.0;
340 if (p > progress) {
341 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700342 redraw = 1;
343 }
344 }
345
Doug Zongker32a0a472011-11-01 11:00:20 -0700346 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700347
Doug Zongker32a0a472011-11-01 11:00:20 -0700348 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700349 double end = now();
350 // minimum of 20ms delay between frames
351 double delay = interval - (end-start);
352 if (delay < 0.02) delay = 0.02;
353 usleep((long)(delay * 1000000));
354 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700355}
356
357void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
358 int result = res_create_surface(filename, surface);
359 if (result < 0) {
360 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
361 }
362}
363
Doug Zongker02ec6b82012-08-22 17:26:40 -0700364void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
365 int result = res_create_localized_surface(filename, surface);
366 if (result < 0) {
367 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
368 }
369}
370
Doug Zongker211aebc2011-10-28 15:13:10 -0700371void ScreenRecoveryUI::Init()
372{
373 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700374
Doug Zongker55a36ac2013-03-04 15:49:02 -0800375 gr_font_size(&char_width, &char_height);
376
Doug Zongker211aebc2011-10-28 15:13:10 -0700377 text_col = text_row = 0;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800378 text_rows = gr_fb_height() / char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -0700379 if (text_rows > kMaxRows) text_rows = kMaxRows;
380 text_top = 1;
381
Doug Zongker55a36ac2013-03-04 15:49:02 -0800382 text_cols = gr_fb_width() / char_width;
Doug Zongker211aebc2011-10-28 15:13:10 -0700383 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
384
Doug Zongker02ec6b82012-08-22 17:26:40 -0700385 LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]);
386 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700387 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700388 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
389
Doug Zongker211aebc2011-10-28 15:13:10 -0700390 LoadBitmap("progress_empty", &progressBarEmpty);
391 LoadBitmap("progress_fill", &progressBarFill);
392
Doug Zongker02ec6b82012-08-22 17:26:40 -0700393 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
394 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
395 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
396 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
397
Doug Zongker211aebc2011-10-28 15:13:10 -0700398 int i;
399
Doug Zongker32a0a472011-11-01 11:00:20 -0700400 progressBarIndeterminate = (gr_surface*)malloc(indeterminate_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700401 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700402 for (i = 0; i < indeterminate_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700403 char filename[40];
404 // "indeterminate01.png", "indeterminate02.png", ...
405 sprintf(filename, "indeterminate%02d", i+1);
406 LoadBitmap(filename, progressBarIndeterminate+i);
407 }
408
Doug Zongker32a0a472011-11-01 11:00:20 -0700409 if (installing_frames > 0) {
410 installationOverlay = (gr_surface*)malloc(installing_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700411 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700412 for (i = 0; i < installing_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700413 char filename[40];
414 // "icon_installing_overlay01.png",
415 // "icon_installing_overlay02.png", ...
416 sprintf(filename, "icon_installing_overlay%02d", i+1);
417 LoadBitmap(filename, installationOverlay+i);
418 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700419 } else {
420 installationOverlay = NULL;
421 }
422
423 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700424
425 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700426}
427
Doug Zongker5fa8c232012-09-18 12:37:02 -0700428void ScreenRecoveryUI::SetLocale(const char* locale) {
429 if (locale) {
430 char* lang = strdup(locale);
431 for (char* p = lang; *p; ++p) {
432 if (*p == '_') {
433 *p = '\0';
434 break;
435 }
436 }
437
438 // A bit cheesy: keep an explicit list of supported languages
439 // that are RTL.
440 if (strcmp(lang, "ar") == 0 || // Arabic
441 strcmp(lang, "fa") == 0 || // Persian (Farsi)
442 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700443 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
444 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700445 rtl_locale = true;
446 }
447 free(lang);
448 }
449}
450
Doug Zongker211aebc2011-10-28 15:13:10 -0700451void ScreenRecoveryUI::SetBackground(Icon icon)
452{
453 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700454
455 // Adjust the offset to account for the positioning of the
456 // base image on the screen.
457 if (backgroundIcon[icon] != NULL) {
458 gr_surface bg = backgroundIcon[icon];
459 gr_surface text = backgroundText[icon];
460 overlay_offset_x = install_overlay_offset_x + (gr_fb_width() - gr_get_width(bg)) / 2;
461 overlay_offset_y = install_overlay_offset_y +
462 (gr_fb_height() - (gr_get_height(bg) + gr_get_height(text) + 40)) / 2;
463 }
464
Doug Zongker211aebc2011-10-28 15:13:10 -0700465 currentIcon = icon;
466 update_screen_locked();
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700467
Doug Zongker211aebc2011-10-28 15:13:10 -0700468 pthread_mutex_unlock(&updateMutex);
469}
470
471void ScreenRecoveryUI::SetProgressType(ProgressType type)
472{
473 pthread_mutex_lock(&updateMutex);
474 if (progressBarType != type) {
475 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700476 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700477 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700478 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700479 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700480 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700481 pthread_mutex_unlock(&updateMutex);
482}
483
484void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
485{
Dees_Troy32c8eb82012-09-11 15:28:06 -0400486 DataManager::SetValue("ui_progress_portion", (float)(portion * 100.0));
487 DataManager::SetValue("ui_progress_frames", seconds * 30);
488
Doug Zongker211aebc2011-10-28 15:13:10 -0700489 pthread_mutex_lock(&updateMutex);
490 progressBarType = DETERMINATE;
491 progressScopeStart += progressScopeSize;
492 progressScopeSize = portion;
493 progressScopeTime = now();
494 progressScopeDuration = seconds;
495 progress = 0;
496 update_progress_locked();
497 pthread_mutex_unlock(&updateMutex);
498}
499
500void ScreenRecoveryUI::SetProgress(float fraction)
501{
Dees_Troy32c8eb82012-09-11 15:28:06 -0400502 DataManager::SetValue("ui_progress", (float) (fraction * 100.0)); return;
503
Doug Zongker211aebc2011-10-28 15:13:10 -0700504 pthread_mutex_lock(&updateMutex);
505 if (fraction < 0.0) fraction = 0.0;
506 if (fraction > 1.0) fraction = 1.0;
507 if (progressBarType == DETERMINATE && fraction > progress) {
508 // Skip updates that aren't visibly different.
509 int width = gr_get_width(progressBarIndeterminate[0]);
510 float scale = width * progressScopeSize;
511 if ((int) (progress * scale) != (int) (fraction * scale)) {
512 progress = fraction;
513 update_progress_locked();
514 }
515 }
516 pthread_mutex_unlock(&updateMutex);
517}
518
519void ScreenRecoveryUI::Print(const char *fmt, ...)
520{
521 char buf[256];
522 va_list ap;
523 va_start(ap, fmt);
524 vsnprintf(buf, 256, fmt, ap);
525 va_end(ap);
526
Dees_Troy32c8eb82012-09-11 15:28:06 -0400527 gui_print("%s", buf);
528 return;
529
Doug Zongker211aebc2011-10-28 15:13:10 -0700530 fputs(buf, stdout);
531
532 // This can get called before ui_init(), so be careful.
533 pthread_mutex_lock(&updateMutex);
534 if (text_rows > 0 && text_cols > 0) {
535 char *ptr;
536 for (ptr = buf; *ptr != '\0'; ++ptr) {
537 if (*ptr == '\n' || text_col >= text_cols) {
538 text[text_row][text_col] = '\0';
539 text_col = 0;
540 text_row = (text_row + 1) % text_rows;
541 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
542 }
543 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
544 }
545 text[text_row][text_col] = '\0';
546 update_screen_locked();
547 }
548 pthread_mutex_unlock(&updateMutex);
549}
550
551void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
552 int initial_selection) {
553 int i;
554 pthread_mutex_lock(&updateMutex);
555 if (text_rows > 0 && text_cols > 0) {
556 for (i = 0; i < text_rows; ++i) {
557 if (headers[i] == NULL) break;
558 strncpy(menu[i], headers[i], text_cols-1);
559 menu[i][text_cols-1] = '\0';
560 }
561 menu_top = i;
562 for (; i < text_rows; ++i) {
563 if (items[i-menu_top] == NULL) break;
564 strncpy(menu[i], items[i-menu_top], text_cols-1);
565 menu[i][text_cols-1] = '\0';
566 }
567 menu_items = i - menu_top;
568 show_menu = 1;
569 menu_sel = initial_selection;
570 update_screen_locked();
571 }
572 pthread_mutex_unlock(&updateMutex);
573}
574
575int ScreenRecoveryUI::SelectMenu(int sel) {
576 int old_sel;
577 pthread_mutex_lock(&updateMutex);
578 if (show_menu > 0) {
579 old_sel = menu_sel;
580 menu_sel = sel;
581 if (menu_sel < 0) menu_sel = 0;
582 if (menu_sel >= menu_items) menu_sel = menu_items-1;
583 sel = menu_sel;
584 if (menu_sel != old_sel) update_screen_locked();
585 }
586 pthread_mutex_unlock(&updateMutex);
587 return sel;
588}
589
590void ScreenRecoveryUI::EndMenu() {
591 int i;
592 pthread_mutex_lock(&updateMutex);
593 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
594 show_menu = 0;
595 update_screen_locked();
596 }
597 pthread_mutex_unlock(&updateMutex);
598}
599
600bool ScreenRecoveryUI::IsTextVisible()
601{
602 pthread_mutex_lock(&updateMutex);
603 int visible = show_text;
604 pthread_mutex_unlock(&updateMutex);
605 return visible;
606}
607
608bool ScreenRecoveryUI::WasTextEverVisible()
609{
610 pthread_mutex_lock(&updateMutex);
611 int ever_visible = show_text_ever;
612 pthread_mutex_unlock(&updateMutex);
613 return ever_visible;
614}
615
616void ScreenRecoveryUI::ShowText(bool visible)
617{
618 pthread_mutex_lock(&updateMutex);
619 show_text = visible;
620 if (show_text) show_text_ever = 1;
621 update_screen_locked();
622 pthread_mutex_unlock(&updateMutex);
623}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700624
625void ScreenRecoveryUI::Redraw()
626{
627 pthread_mutex_lock(&updateMutex);
628 update_screen_locked();
629 pthread_mutex_unlock(&updateMutex);
630}