blob: 6fff30a253e72036c50f0ccdc12ac2d50ce7fc3e [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 Zongkera418aa72014-03-17 12:10:02 -070061 locale(NULL),
Doug Zongker5fa8c232012-09-18 12:37:02 -070062 rtl_locale(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070063 progressBarType(EMPTY),
64 progressScopeStart(0),
65 progressScopeSize(0),
66 progress(0),
67 pagesIdentical(false),
68 text_cols(0),
69 text_rows(0),
70 text_col(0),
71 text_row(0),
72 text_top(0),
73 show_text(false),
74 show_text_ever(false),
75 show_menu(false),
76 menu_top(0),
77 menu_items(0),
78 menu_sel(0),
Doug Zongker32a0a472011-11-01 11:00:20 -070079 animation_fps(20),
Doug Zongkereac881c2014-03-07 09:21:25 -080080 installing_frames(-1),
Doug Zongkerc87bab12013-11-25 13:53:25 -080081 stage(-1),
82 max_stage(-1) {
yetta_wu2f6877a2013-06-25 15:03:11 +080083
84 for (int i = 0; i < 5; i++)
85 backgroundIcon[i] = NULL;
86
Bjorn Andersson80a7a462013-08-30 16:59:06 -070087 memset(text, 0, sizeof(text));
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
Doug Zongker211aebc2011-10-28 15:13:10 -070093// Clear the screen and draw the currently selected background icon (if any).
94// Should only be called with updateMutex locked.
95void ScreenRecoveryUI::draw_background_locked(Icon icon)
96{
97 pagesIdentical = false;
98 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -080099 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700100
101 if (icon) {
102 gr_surface surface = backgroundIcon[icon];
Doug Zongkereac881c2014-03-07 09:21:25 -0800103 if (icon == INSTALLING_UPDATE || icon == ERASING) {
104 surface = installation[installingFrame];
105 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700106 gr_surface text_surface = backgroundText[icon];
107
Doug Zongker211aebc2011-10-28 15:13:10 -0700108 int iconWidth = gr_get_width(surface);
109 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700110 int textWidth = gr_get_width(text_surface);
111 int textHeight = gr_get_height(text_surface);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800112 int stageHeight = gr_get_height(stageMarkerEmpty);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700113
Doug Zongkerc87bab12013-11-25 13:53:25 -0800114 int sh = (max_stage >= 0) ? stageHeight : 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700115
Doug Zongkereac881c2014-03-07 09:21:25 -0800116 iconX = (gr_fb_width() - iconWidth) / 2;
117 iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700118
119 int textX = (gr_fb_width() - textWidth) / 2;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800120 int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700121
Doug Zongker211aebc2011-10-28 15:13:10 -0700122 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800123 if (stageHeight > 0) {
124 int sw = gr_get_width(stageMarkerEmpty);
125 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
126 int y = iconY + iconHeight + 20;
127 for (int i = 0; i < max_stage; ++i) {
128 gr_blit((i < stage) ? stageMarkerFill : stageMarkerEmpty,
129 0, 0, sw, stageHeight, x, y);
130 x += sw;
131 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700132 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700133
134 gr_color(255, 255, 255, 255);
135 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700136 }
137}
138
139// Draw the progress bar (if any) on the screen. Does not flip pages.
140// Should only be called with updateMutex locked.
141void ScreenRecoveryUI::draw_progress_locked()
142{
Doug Zongker69f4b672012-04-26 14:37:53 -0700143 if (currentIcon == ERROR) return;
144
Doug Zongker02ec6b82012-08-22 17:26:40 -0700145 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Doug Zongkereac881c2014-03-07 09:21:25 -0800146 gr_surface icon = installation[installingFrame];
147 gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY);
Doug Zongker211aebc2011-10-28 15:13:10 -0700148 }
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 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700184 }
185}
186
Doug Zongkerc0441d12013-07-31 11:28:24 -0700187void ScreenRecoveryUI::SetColor(UIElement e) {
188 switch (e) {
189 case HEADER:
190 gr_color(247, 0, 6, 255);
191 break;
192 case MENU:
193 case MENU_SEL_BG:
194 gr_color(0, 106, 157, 255);
195 break;
196 case MENU_SEL_FG:
197 gr_color(255, 255, 255, 255);
198 break;
199 case LOG:
200 gr_color(249, 194, 0, 255);
201 break;
202 case TEXT_FILL:
203 gr_color(0, 0, 0, 160);
204 break;
205 default:
206 gr_color(255, 255, 255, 255);
207 break;
208 }
209}
Doug Zongker211aebc2011-10-28 15:13:10 -0700210
211// Redraw everything on the screen. Does not flip pages.
212// Should only be called with updateMutex locked.
213void ScreenRecoveryUI::draw_screen_locked()
214{
Doug Zongker39cf4172014-03-06 16:16:05 -0800215 if (!show_text) {
216 draw_background_locked(currentIcon);
217 draw_progress_locked();
218 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700219 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800220 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700221
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800222 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700223 int i = 0;
224 if (show_menu) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700225 SetColor(HEADER);
Doug Zongker211aebc2011-10-28 15:13:10 -0700226
227 for (; i < menu_top + menu_items; ++i) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700228 if (i == menu_top) SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800229
Doug Zongker211aebc2011-10-28 15:13:10 -0700230 if (i == menu_top + menu_sel) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800231 // draw the highlight bar
Doug Zongkerc0441d12013-07-31 11:28:24 -0700232 SetColor(MENU_SEL_BG);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800233 gr_fill(0, y-2, gr_fb_width(), y+char_height+2);
234 // white text of selected item
Doug Zongkerc0441d12013-07-31 11:28:24 -0700235 SetColor(MENU_SEL_FG);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800236 if (menu[i][0]) gr_text(4, y, menu[i], 1);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700237 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700238 } else {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800239 if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top);
Doug Zongker211aebc2011-10-28 15:13:10 -0700240 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800241 y += char_height+4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700242 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700243 SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800244 y += 4;
245 gr_fill(0, y, gr_fb_width(), y+2);
246 y += 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700247 ++i;
248 }
249
Doug Zongkerc0441d12013-07-31 11:28:24 -0700250 SetColor(LOG);
Doug Zongker211aebc2011-10-28 15:13:10 -0700251
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800252 // display from the bottom up, until we hit the top of the
253 // screen, the bottom of the menu, or we've displayed the
254 // entire text buffer.
255 int ty;
256 int row = (text_top+text_rows-1) % text_rows;
257 for (int ty = gr_fb_height() - char_height, count = 0;
258 ty > y+2 && count < text_rows;
259 ty -= char_height, ++count) {
260 gr_text(4, ty, text[row], 0);
261 --row;
262 if (row < 0) row = text_rows-1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700263 }
264 }
265}
266
267// Redraw everything on the screen and flip the screen (make it visible).
268// Should only be called with updateMutex locked.
269void ScreenRecoveryUI::update_screen_locked()
270{
271 draw_screen_locked();
272 gr_flip();
273}
274
275// Updates only the progress bar, if possible, otherwise redraws the screen.
276// Should only be called with updateMutex locked.
277void ScreenRecoveryUI::update_progress_locked()
Dees_Troy32c8eb82012-09-11 15:28:06 -0400278{return;
Doug Zongker211aebc2011-10-28 15:13:10 -0700279 if (show_text || !pagesIdentical) {
280 draw_screen_locked(); // Must redraw the whole screen
281 pagesIdentical = true;
282 } else {
283 draw_progress_locked(); // Draw only the progress bar and overlays
284 }
285 gr_flip();
286}
287
288// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700289void* ScreenRecoveryUI::progress_thread(void *cookie) {
290 self->progress_loop();
291 return NULL;
292}
293
294void ScreenRecoveryUI::progress_loop() {
295 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700296 for (;;) {
297 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700298 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700299
300 int redraw = 0;
301
302 // update the installation animation, if active
303 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700304 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
305 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700306 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700307 redraw = 1;
308 }
309
Doug Zongker211aebc2011-10-28 15:13:10 -0700310 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700311 int duration = progressScopeDuration;
312 if (progressBarType == DETERMINATE && duration > 0) {
313 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700314 float p = 1.0 * elapsed / duration;
315 if (p > 1.0) p = 1.0;
316 if (p > progress) {
317 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700318 redraw = 1;
319 }
320 }
321
Doug Zongker32a0a472011-11-01 11:00:20 -0700322 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700323
Doug Zongker32a0a472011-11-01 11:00:20 -0700324 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700325 double end = now();
326 // minimum of 20ms delay between frames
327 double delay = interval - (end-start);
328 if (delay < 0.02) delay = 0.02;
329 usleep((long)(delay * 1000000));
330 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700331}
332
333void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700334 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700335 if (result < 0) {
336 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
337 }
338}
339
Doug Zongkereac881c2014-03-07 09:21:25 -0800340void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700341 int result = res_create_multi_display_surface(filename, frames, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700342 if (result < 0) {
343 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
344 }
345}
346
Doug Zongker02ec6b82012-08-22 17:26:40 -0700347void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700348 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700349 if (result < 0) {
350 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
351 }
352}
353
Doug Zongker211aebc2011-10-28 15:13:10 -0700354void ScreenRecoveryUI::Init()
355{
356 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700357
Doug Zongker55a36ac2013-03-04 15:49:02 -0800358 gr_font_size(&char_width, &char_height);
359
Doug Zongker211aebc2011-10-28 15:13:10 -0700360 text_col = text_row = 0;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800361 text_rows = gr_fb_height() / char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -0700362 if (text_rows > kMaxRows) text_rows = kMaxRows;
363 text_top = 1;
364
Doug Zongker55a36ac2013-03-04 15:49:02 -0800365 text_cols = gr_fb_width() / char_width;
Doug Zongker211aebc2011-10-28 15:13:10 -0700366 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
367
Alistair Strachan9b8ae802013-07-17 10:34:36 -0700368 backgroundIcon[NONE] = NULL;
Doug Zongkereac881c2014-03-07 09:21:25 -0800369 LoadBitmapArray("icon_installing", &installing_frames, &installation);
370 backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700371 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700372 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700373 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
374
Doug Zongker211aebc2011-10-28 15:13:10 -0700375 LoadBitmap("progress_empty", &progressBarEmpty);
376 LoadBitmap("progress_fill", &progressBarFill);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800377 LoadBitmap("stage_empty", &stageMarkerEmpty);
378 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700379
Doug Zongker02ec6b82012-08-22 17:26:40 -0700380 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
381 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
382 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
383 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
384
Doug Zongker211aebc2011-10-28 15:13:10 -0700385 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700386
387 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700388}
389
Doug Zongkera418aa72014-03-17 12:10:02 -0700390void ScreenRecoveryUI::SetLocale(const char* new_locale) {
391 if (new_locale) {
392 this->locale = new_locale;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700393 char* lang = strdup(locale);
394 for (char* p = lang; *p; ++p) {
395 if (*p == '_') {
396 *p = '\0';
397 break;
398 }
399 }
400
401 // A bit cheesy: keep an explicit list of supported languages
402 // that are RTL.
403 if (strcmp(lang, "ar") == 0 || // Arabic
404 strcmp(lang, "fa") == 0 || // Persian (Farsi)
405 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700406 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
407 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700408 rtl_locale = true;
409 }
410 free(lang);
Doug Zongkera418aa72014-03-17 12:10:02 -0700411 } else {
412 new_locale = NULL;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700413 }
414}
415
Doug Zongker211aebc2011-10-28 15:13:10 -0700416void ScreenRecoveryUI::SetBackground(Icon icon)
417{
418 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700419
Doug Zongker211aebc2011-10-28 15:13:10 -0700420 currentIcon = icon;
421 update_screen_locked();
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700422
Doug Zongker211aebc2011-10-28 15:13:10 -0700423 pthread_mutex_unlock(&updateMutex);
424}
425
426void ScreenRecoveryUI::SetProgressType(ProgressType type)
427{
428 pthread_mutex_lock(&updateMutex);
429 if (progressBarType != type) {
430 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700431 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700432 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700433 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700434 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700435 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700436 pthread_mutex_unlock(&updateMutex);
437}
438
439void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
440{
Dees_Troy32c8eb82012-09-11 15:28:06 -0400441 DataManager::SetValue("ui_progress_portion", (float)(portion * 100.0));
442 DataManager::SetValue("ui_progress_frames", seconds * 30);
443
Doug Zongker211aebc2011-10-28 15:13:10 -0700444 pthread_mutex_lock(&updateMutex);
445 progressBarType = DETERMINATE;
446 progressScopeStart += progressScopeSize;
447 progressScopeSize = portion;
448 progressScopeTime = now();
449 progressScopeDuration = seconds;
450 progress = 0;
451 update_progress_locked();
452 pthread_mutex_unlock(&updateMutex);
453}
454
455void ScreenRecoveryUI::SetProgress(float fraction)
456{
Dees_Troy32c8eb82012-09-11 15:28:06 -0400457 DataManager::SetValue("ui_progress", (float) (fraction * 100.0)); return;
458
Doug Zongker211aebc2011-10-28 15:13:10 -0700459 pthread_mutex_lock(&updateMutex);
460 if (fraction < 0.0) fraction = 0.0;
461 if (fraction > 1.0) fraction = 1.0;
462 if (progressBarType == DETERMINATE && fraction > progress) {
463 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800464 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700465 float scale = width * progressScopeSize;
466 if ((int) (progress * scale) != (int) (fraction * scale)) {
467 progress = fraction;
468 update_progress_locked();
469 }
470 }
471 pthread_mutex_unlock(&updateMutex);
472}
473
Doug Zongkerc87bab12013-11-25 13:53:25 -0800474void ScreenRecoveryUI::SetStage(int current, int max) {
475 pthread_mutex_lock(&updateMutex);
476 stage = current;
477 max_stage = max;
478 pthread_mutex_unlock(&updateMutex);
479}
480
Doug Zongker211aebc2011-10-28 15:13:10 -0700481void ScreenRecoveryUI::Print(const char *fmt, ...)
482{
483 char buf[256];
484 va_list ap;
485 va_start(ap, fmt);
486 vsnprintf(buf, 256, fmt, ap);
487 va_end(ap);
488
Dees_Troy32c8eb82012-09-11 15:28:06 -0400489 gui_print("%s", buf);
490 return;
491
Doug Zongker211aebc2011-10-28 15:13:10 -0700492 fputs(buf, stdout);
493
494 // This can get called before ui_init(), so be careful.
495 pthread_mutex_lock(&updateMutex);
496 if (text_rows > 0 && text_cols > 0) {
497 char *ptr;
498 for (ptr = buf; *ptr != '\0'; ++ptr) {
499 if (*ptr == '\n' || text_col >= text_cols) {
500 text[text_row][text_col] = '\0';
501 text_col = 0;
502 text_row = (text_row + 1) % text_rows;
503 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
504 }
505 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
506 }
507 text[text_row][text_col] = '\0';
508 update_screen_locked();
509 }
510 pthread_mutex_unlock(&updateMutex);
511}
512
513void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
514 int initial_selection) {
515 int i;
516 pthread_mutex_lock(&updateMutex);
517 if (text_rows > 0 && text_cols > 0) {
518 for (i = 0; i < text_rows; ++i) {
519 if (headers[i] == NULL) break;
520 strncpy(menu[i], headers[i], text_cols-1);
521 menu[i][text_cols-1] = '\0';
522 }
523 menu_top = i;
524 for (; i < text_rows; ++i) {
525 if (items[i-menu_top] == NULL) break;
526 strncpy(menu[i], items[i-menu_top], text_cols-1);
527 menu[i][text_cols-1] = '\0';
528 }
529 menu_items = i - menu_top;
530 show_menu = 1;
531 menu_sel = initial_selection;
532 update_screen_locked();
533 }
534 pthread_mutex_unlock(&updateMutex);
535}
536
537int ScreenRecoveryUI::SelectMenu(int sel) {
538 int old_sel;
539 pthread_mutex_lock(&updateMutex);
540 if (show_menu > 0) {
541 old_sel = menu_sel;
542 menu_sel = sel;
543 if (menu_sel < 0) menu_sel = 0;
544 if (menu_sel >= menu_items) menu_sel = menu_items-1;
545 sel = menu_sel;
546 if (menu_sel != old_sel) update_screen_locked();
547 }
548 pthread_mutex_unlock(&updateMutex);
549 return sel;
550}
551
552void ScreenRecoveryUI::EndMenu() {
553 int i;
554 pthread_mutex_lock(&updateMutex);
555 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
556 show_menu = 0;
557 update_screen_locked();
558 }
559 pthread_mutex_unlock(&updateMutex);
560}
561
562bool ScreenRecoveryUI::IsTextVisible()
563{
564 pthread_mutex_lock(&updateMutex);
565 int visible = show_text;
566 pthread_mutex_unlock(&updateMutex);
567 return visible;
568}
569
570bool ScreenRecoveryUI::WasTextEverVisible()
571{
572 pthread_mutex_lock(&updateMutex);
573 int ever_visible = show_text_ever;
574 pthread_mutex_unlock(&updateMutex);
575 return ever_visible;
576}
577
578void ScreenRecoveryUI::ShowText(bool visible)
579{
580 pthread_mutex_lock(&updateMutex);
581 show_text = visible;
582 if (show_text) show_text_ever = 1;
583 update_screen_locked();
584 pthread_mutex_unlock(&updateMutex);
585}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700586
587void ScreenRecoveryUI::Redraw()
588{
589 pthread_mutex_lock(&updateMutex);
590 update_screen_locked();
591 pthread_mutex_unlock(&updateMutex);
592}