blob: af58643dcf0484c914a9c6be7f31d5886cd818c7 [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 Zongkera418aa72014-03-17 12:10:02 -070055 locale(NULL),
Doug Zongker5fa8c232012-09-18 12:37:02 -070056 rtl_locale(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070057 progressBarType(EMPTY),
58 progressScopeStart(0),
59 progressScopeSize(0),
60 progress(0),
61 pagesIdentical(false),
62 text_cols(0),
63 text_rows(0),
64 text_col(0),
65 text_row(0),
66 text_top(0),
67 show_text(false),
68 show_text_ever(false),
69 show_menu(false),
70 menu_top(0),
71 menu_items(0),
72 menu_sel(0),
Doug Zongker32a0a472011-11-01 11:00:20 -070073 animation_fps(20),
Doug Zongkereac881c2014-03-07 09:21:25 -080074 installing_frames(-1),
Doug Zongkerc87bab12013-11-25 13:53:25 -080075 stage(-1),
76 max_stage(-1) {
yetta_wu5b468fc2013-06-25 15:03:11 +080077
78 for (int i = 0; i < 5; i++)
79 backgroundIcon[i] = NULL;
80
Doug Zongker211aebc2011-10-28 15:13:10 -070081 pthread_mutex_init(&updateMutex, NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -070082 self = this;
83}
84
Doug Zongker211aebc2011-10-28 15:13:10 -070085// Clear the screen and draw the currently selected background icon (if any).
86// Should only be called with updateMutex locked.
87void ScreenRecoveryUI::draw_background_locked(Icon icon)
88{
89 pagesIdentical = false;
90 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -080091 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -070092
93 if (icon) {
94 gr_surface surface = backgroundIcon[icon];
Doug Zongkereac881c2014-03-07 09:21:25 -080095 if (icon == INSTALLING_UPDATE || icon == ERASING) {
96 surface = installation[installingFrame];
97 }
Doug Zongker02ec6b82012-08-22 17:26:40 -070098 gr_surface text_surface = backgroundText[icon];
99
Doug Zongker211aebc2011-10-28 15:13:10 -0700100 int iconWidth = gr_get_width(surface);
101 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700102 int textWidth = gr_get_width(text_surface);
103 int textHeight = gr_get_height(text_surface);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800104 int stageHeight = gr_get_height(stageMarkerEmpty);
105
106 int sh = (max_stage >= 0) ? stageHeight : 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700107
Doug Zongkereac881c2014-03-07 09:21:25 -0800108 iconX = (gr_fb_width() - iconWidth) / 2;
109 iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700110
111 int textX = (gr_fb_width() - textWidth) / 2;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800112 int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700113
Doug Zongker211aebc2011-10-28 15:13:10 -0700114 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800115 if (stageHeight > 0) {
116 int sw = gr_get_width(stageMarkerEmpty);
117 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
118 int y = iconY + iconHeight + 20;
119 for (int i = 0; i < max_stage; ++i) {
120 gr_blit((i < stage) ? stageMarkerFill : stageMarkerEmpty,
121 0, 0, sw, stageHeight, x, y);
122 x += sw;
123 }
124 }
125
Doug Zongker02ec6b82012-08-22 17:26:40 -0700126 gr_color(255, 255, 255, 255);
127 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700128 }
129}
130
131// Draw the progress bar (if any) on the screen. Does not flip pages.
132// Should only be called with updateMutex locked.
133void ScreenRecoveryUI::draw_progress_locked()
134{
Doug Zongker69f4b672012-04-26 14:37:53 -0700135 if (currentIcon == ERROR) return;
136
Doug Zongker02ec6b82012-08-22 17:26:40 -0700137 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Doug Zongkereac881c2014-03-07 09:21:25 -0800138 gr_surface icon = installation[installingFrame];
139 gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY);
Doug Zongker211aebc2011-10-28 15:13:10 -0700140 }
141
142 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700143 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700144 int width = gr_get_width(progressBarEmpty);
145 int height = gr_get_height(progressBarEmpty);
146
147 int dx = (gr_fb_width() - width)/2;
148 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
149
150 // Erase behind the progress bar (in case this was a progress-only update)
151 gr_color(0, 0, 0, 255);
152 gr_fill(dx, dy, width, height);
153
154 if (progressBarType == DETERMINATE) {
155 float p = progressScopeStart + progress * progressScopeSize;
156 int pos = (int) (p * width);
157
Doug Zongker5fa8c232012-09-18 12:37:02 -0700158 if (rtl_locale) {
159 // Fill the progress bar from right to left.
160 if (pos > 0) {
161 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
162 }
163 if (pos < width-1) {
164 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
165 }
166 } else {
167 // Fill the progress bar from left to right.
168 if (pos > 0) {
169 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
170 }
171 if (pos < width-1) {
172 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
173 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700174 }
175 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700176 }
177}
178
Doug Zongkerc0441d12013-07-31 11:28:24 -0700179void ScreenRecoveryUI::SetColor(UIElement e) {
180 switch (e) {
181 case HEADER:
182 gr_color(247, 0, 6, 255);
183 break;
184 case MENU:
185 case MENU_SEL_BG:
186 gr_color(0, 106, 157, 255);
187 break;
188 case MENU_SEL_FG:
189 gr_color(255, 255, 255, 255);
190 break;
191 case LOG:
192 gr_color(249, 194, 0, 255);
193 break;
194 case TEXT_FILL:
195 gr_color(0, 0, 0, 160);
196 break;
197 default:
198 gr_color(255, 255, 255, 255);
199 break;
200 }
201}
Doug Zongker211aebc2011-10-28 15:13:10 -0700202
203// Redraw everything on the screen. Does not flip pages.
204// Should only be called with updateMutex locked.
205void ScreenRecoveryUI::draw_screen_locked()
206{
Doug Zongker39cf4172014-03-06 16:16:05 -0800207 if (!show_text) {
208 draw_background_locked(currentIcon);
209 draw_progress_locked();
210 } else {
211 gr_color(0, 0, 0, 255);
212 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700213
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800214 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700215 int i = 0;
216 if (show_menu) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700217 SetColor(HEADER);
Doug Zongker211aebc2011-10-28 15:13:10 -0700218
219 for (; i < menu_top + menu_items; ++i) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700220 if (i == menu_top) SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800221
Doug Zongker211aebc2011-10-28 15:13:10 -0700222 if (i == menu_top + menu_sel) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800223 // draw the highlight bar
Doug Zongkerc0441d12013-07-31 11:28:24 -0700224 SetColor(MENU_SEL_BG);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800225 gr_fill(0, y-2, gr_fb_width(), y+char_height+2);
226 // white text of selected item
Doug Zongkerc0441d12013-07-31 11:28:24 -0700227 SetColor(MENU_SEL_FG);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800228 if (menu[i][0]) gr_text(4, y, menu[i], 1);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700229 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700230 } else {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800231 if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top);
Doug Zongker211aebc2011-10-28 15:13:10 -0700232 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800233 y += char_height+4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700234 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700235 SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800236 y += 4;
237 gr_fill(0, y, gr_fb_width(), y+2);
238 y += 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700239 ++i;
240 }
241
Doug Zongkerc0441d12013-07-31 11:28:24 -0700242 SetColor(LOG);
Doug Zongker211aebc2011-10-28 15:13:10 -0700243
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800244 // display from the bottom up, until we hit the top of the
245 // screen, the bottom of the menu, or we've displayed the
246 // entire text buffer.
247 int ty;
248 int row = (text_top+text_rows-1) % text_rows;
249 for (int ty = gr_fb_height() - char_height, count = 0;
250 ty > y+2 && count < text_rows;
251 ty -= char_height, ++count) {
252 gr_text(4, ty, text[row], 0);
253 --row;
254 if (row < 0) row = text_rows-1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700255 }
256 }
257}
258
259// Redraw everything on the screen and flip the screen (make it visible).
260// Should only be called with updateMutex locked.
261void ScreenRecoveryUI::update_screen_locked()
262{
263 draw_screen_locked();
264 gr_flip();
265}
266
267// Updates only the progress bar, if possible, otherwise redraws the screen.
268// Should only be called with updateMutex locked.
269void ScreenRecoveryUI::update_progress_locked()
270{
271 if (show_text || !pagesIdentical) {
272 draw_screen_locked(); // Must redraw the whole screen
273 pagesIdentical = true;
274 } else {
275 draw_progress_locked(); // Draw only the progress bar and overlays
276 }
277 gr_flip();
278}
279
280// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700281void* ScreenRecoveryUI::progress_thread(void *cookie) {
282 self->progress_loop();
283 return NULL;
284}
285
286void ScreenRecoveryUI::progress_loop() {
287 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700288 for (;;) {
289 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700290 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700291
292 int redraw = 0;
293
294 // update the installation animation, if active
295 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700296 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
297 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700298 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700299 redraw = 1;
300 }
301
Doug Zongker211aebc2011-10-28 15:13:10 -0700302 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700303 int duration = progressScopeDuration;
304 if (progressBarType == DETERMINATE && duration > 0) {
305 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700306 float p = 1.0 * elapsed / duration;
307 if (p > 1.0) p = 1.0;
308 if (p > progress) {
309 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700310 redraw = 1;
311 }
312 }
313
Doug Zongker32a0a472011-11-01 11:00:20 -0700314 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700315
Doug Zongker32a0a472011-11-01 11:00:20 -0700316 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700317 double end = now();
318 // minimum of 20ms delay between frames
319 double delay = interval - (end-start);
320 if (delay < 0.02) delay = 0.02;
321 usleep((long)(delay * 1000000));
322 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700323}
324
325void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700326 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700327 if (result < 0) {
328 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
329 }
330}
331
Doug Zongkereac881c2014-03-07 09:21:25 -0800332void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700333 int result = res_create_multi_display_surface(filename, frames, surface);
Doug Zongkereac881c2014-03-07 09:21:25 -0800334 if (result < 0) {
335 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
336 }
337}
338
Doug Zongker02ec6b82012-08-22 17:26:40 -0700339void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700340 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700341 if (result < 0) {
342 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
343 }
344}
345
Doug Zongker211aebc2011-10-28 15:13:10 -0700346void ScreenRecoveryUI::Init()
347{
348 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700349
Doug Zongker55a36ac2013-03-04 15:49:02 -0800350 gr_font_size(&char_width, &char_height);
351
Doug Zongker211aebc2011-10-28 15:13:10 -0700352 text_col = text_row = 0;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800353 text_rows = gr_fb_height() / char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -0700354 if (text_rows > kMaxRows) text_rows = kMaxRows;
355 text_top = 1;
356
Doug Zongker55a36ac2013-03-04 15:49:02 -0800357 text_cols = gr_fb_width() / char_width;
Doug Zongker211aebc2011-10-28 15:13:10 -0700358 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
359
Alistair Strachan9b8ae802013-07-17 10:34:36 -0700360 backgroundIcon[NONE] = NULL;
Doug Zongkereac881c2014-03-07 09:21:25 -0800361 LoadBitmapArray("icon_installing", &installing_frames, &installation);
362 backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700363 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700364 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700365 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
366
Doug Zongker211aebc2011-10-28 15:13:10 -0700367 LoadBitmap("progress_empty", &progressBarEmpty);
368 LoadBitmap("progress_fill", &progressBarFill);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800369 LoadBitmap("stage_empty", &stageMarkerEmpty);
370 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700371
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 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700378
379 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700380}
381
Doug Zongkera418aa72014-03-17 12:10:02 -0700382void ScreenRecoveryUI::SetLocale(const char* new_locale) {
383 if (new_locale) {
384 this->locale = new_locale;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700385 char* lang = strdup(locale);
386 for (char* p = lang; *p; ++p) {
387 if (*p == '_') {
388 *p = '\0';
389 break;
390 }
391 }
392
393 // A bit cheesy: keep an explicit list of supported languages
394 // that are RTL.
395 if (strcmp(lang, "ar") == 0 || // Arabic
396 strcmp(lang, "fa") == 0 || // Persian (Farsi)
397 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700398 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
399 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700400 rtl_locale = true;
401 }
402 free(lang);
Doug Zongkera418aa72014-03-17 12:10:02 -0700403 } else {
404 new_locale = NULL;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700405 }
406}
407
Doug Zongker211aebc2011-10-28 15:13:10 -0700408void ScreenRecoveryUI::SetBackground(Icon icon)
409{
410 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700411
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700412 currentIcon = icon;
413 update_screen_locked();
414
Doug Zongker211aebc2011-10-28 15:13:10 -0700415 pthread_mutex_unlock(&updateMutex);
416}
417
418void ScreenRecoveryUI::SetProgressType(ProgressType type)
419{
420 pthread_mutex_lock(&updateMutex);
421 if (progressBarType != type) {
422 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700423 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700424 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700425 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700426 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700427 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700428 pthread_mutex_unlock(&updateMutex);
429}
430
431void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
432{
433 pthread_mutex_lock(&updateMutex);
434 progressBarType = DETERMINATE;
435 progressScopeStart += progressScopeSize;
436 progressScopeSize = portion;
437 progressScopeTime = now();
438 progressScopeDuration = seconds;
439 progress = 0;
440 update_progress_locked();
441 pthread_mutex_unlock(&updateMutex);
442}
443
444void ScreenRecoveryUI::SetProgress(float fraction)
445{
446 pthread_mutex_lock(&updateMutex);
447 if (fraction < 0.0) fraction = 0.0;
448 if (fraction > 1.0) fraction = 1.0;
449 if (progressBarType == DETERMINATE && fraction > progress) {
450 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800451 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700452 float scale = width * progressScopeSize;
453 if ((int) (progress * scale) != (int) (fraction * scale)) {
454 progress = fraction;
455 update_progress_locked();
456 }
457 }
458 pthread_mutex_unlock(&updateMutex);
459}
460
Doug Zongkerc87bab12013-11-25 13:53:25 -0800461void ScreenRecoveryUI::SetStage(int current, int max) {
462 pthread_mutex_lock(&updateMutex);
463 stage = current;
464 max_stage = max;
465 pthread_mutex_unlock(&updateMutex);
466}
467
Doug Zongker211aebc2011-10-28 15:13:10 -0700468void ScreenRecoveryUI::Print(const char *fmt, ...)
469{
470 char buf[256];
471 va_list ap;
472 va_start(ap, fmt);
473 vsnprintf(buf, 256, fmt, ap);
474 va_end(ap);
475
476 fputs(buf, stdout);
477
478 // This can get called before ui_init(), so be careful.
479 pthread_mutex_lock(&updateMutex);
480 if (text_rows > 0 && text_cols > 0) {
481 char *ptr;
482 for (ptr = buf; *ptr != '\0'; ++ptr) {
483 if (*ptr == '\n' || text_col >= text_cols) {
484 text[text_row][text_col] = '\0';
485 text_col = 0;
486 text_row = (text_row + 1) % text_rows;
487 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
488 }
489 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
490 }
491 text[text_row][text_col] = '\0';
492 update_screen_locked();
493 }
494 pthread_mutex_unlock(&updateMutex);
495}
496
497void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
498 int initial_selection) {
499 int i;
500 pthread_mutex_lock(&updateMutex);
501 if (text_rows > 0 && text_cols > 0) {
502 for (i = 0; i < text_rows; ++i) {
503 if (headers[i] == NULL) break;
504 strncpy(menu[i], headers[i], text_cols-1);
505 menu[i][text_cols-1] = '\0';
506 }
507 menu_top = i;
508 for (; i < text_rows; ++i) {
509 if (items[i-menu_top] == NULL) break;
510 strncpy(menu[i], items[i-menu_top], text_cols-1);
511 menu[i][text_cols-1] = '\0';
512 }
513 menu_items = i - menu_top;
514 show_menu = 1;
515 menu_sel = initial_selection;
516 update_screen_locked();
517 }
518 pthread_mutex_unlock(&updateMutex);
519}
520
521int ScreenRecoveryUI::SelectMenu(int sel) {
522 int old_sel;
523 pthread_mutex_lock(&updateMutex);
524 if (show_menu > 0) {
525 old_sel = menu_sel;
526 menu_sel = sel;
527 if (menu_sel < 0) menu_sel = 0;
528 if (menu_sel >= menu_items) menu_sel = menu_items-1;
529 sel = menu_sel;
530 if (menu_sel != old_sel) update_screen_locked();
531 }
532 pthread_mutex_unlock(&updateMutex);
533 return sel;
534}
535
536void ScreenRecoveryUI::EndMenu() {
537 int i;
538 pthread_mutex_lock(&updateMutex);
539 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
540 show_menu = 0;
541 update_screen_locked();
542 }
543 pthread_mutex_unlock(&updateMutex);
544}
545
546bool ScreenRecoveryUI::IsTextVisible()
547{
548 pthread_mutex_lock(&updateMutex);
549 int visible = show_text;
550 pthread_mutex_unlock(&updateMutex);
551 return visible;
552}
553
554bool ScreenRecoveryUI::WasTextEverVisible()
555{
556 pthread_mutex_lock(&updateMutex);
557 int ever_visible = show_text_ever;
558 pthread_mutex_unlock(&updateMutex);
559 return ever_visible;
560}
561
562void ScreenRecoveryUI::ShowText(bool visible)
563{
564 pthread_mutex_lock(&updateMutex);
565 show_text = visible;
566 if (show_text) show_text_ever = 1;
567 update_screen_locked();
568 pthread_mutex_unlock(&updateMutex);
569}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700570
571void ScreenRecoveryUI::Redraw()
572{
573 pthread_mutex_lock(&updateMutex);
574 update_screen_locked();
575 pthread_mutex_unlock(&updateMutex);
576}