blob: 656f7244527e01ead1d01b85006ffa410eceee3b [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 Zongker469954f2014-03-07 09:21:25 -080074 installing_frames(-1) {
yetta_wu5b468fc2013-06-25 15:03:11 +080075 for (int i = 0; i < 5; i++)
76 backgroundIcon[i] = NULL;
77
Doug Zongker211aebc2011-10-28 15:13:10 -070078 pthread_mutex_init(&updateMutex, NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -070079 self = this;
80}
81
Doug Zongker211aebc2011-10-28 15:13:10 -070082// Clear the screen and draw the currently selected background icon (if any).
83// Should only be called with updateMutex locked.
84void ScreenRecoveryUI::draw_background_locked(Icon icon)
85{
86 pagesIdentical = false;
87 gr_color(0, 0, 0, 255);
Doug Zongker16f97c32014-03-06 16:16:05 -080088 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -070089
90 if (icon) {
91 gr_surface surface = backgroundIcon[icon];
Doug Zongker469954f2014-03-07 09:21:25 -080092 if (icon == INSTALLING_UPDATE || icon == ERASING) {
93 surface = installation[installingFrame];
94 }
Doug Zongker02ec6b82012-08-22 17:26:40 -070095 gr_surface text_surface = backgroundText[icon];
96
Doug Zongker211aebc2011-10-28 15:13:10 -070097 int iconWidth = gr_get_width(surface);
98 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -070099 int textWidth = gr_get_width(text_surface);
100 int textHeight = gr_get_height(text_surface);
101
Doug Zongker469954f2014-03-07 09:21:25 -0800102 iconX = (gr_fb_width() - iconWidth) / 2;
103 iconY = (gr_fb_height() - (iconHeight+textHeight+40)) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700104
105 int textX = (gr_fb_width() - textWidth) / 2;
106 int textY = ((gr_fb_height() - (iconHeight+textHeight+40)) / 2) + iconHeight + 40;
107
Doug Zongker211aebc2011-10-28 15:13:10 -0700108 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700109
110 gr_color(255, 255, 255, 255);
111 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700112 }
113}
114
115// Draw the progress bar (if any) on the screen. Does not flip pages.
116// Should only be called with updateMutex locked.
117void ScreenRecoveryUI::draw_progress_locked()
118{
Doug Zongker69f4b672012-04-26 14:37:53 -0700119 if (currentIcon == ERROR) return;
120
Doug Zongker02ec6b82012-08-22 17:26:40 -0700121 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Doug Zongker469954f2014-03-07 09:21:25 -0800122 gr_surface icon = installation[installingFrame];
123 gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY);
Doug Zongker211aebc2011-10-28 15:13:10 -0700124 }
125
126 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700127 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700128 int width = gr_get_width(progressBarEmpty);
129 int height = gr_get_height(progressBarEmpty);
130
131 int dx = (gr_fb_width() - width)/2;
132 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
133
134 // Erase behind the progress bar (in case this was a progress-only update)
135 gr_color(0, 0, 0, 255);
136 gr_fill(dx, dy, width, height);
137
138 if (progressBarType == DETERMINATE) {
139 float p = progressScopeStart + progress * progressScopeSize;
140 int pos = (int) (p * width);
141
Doug Zongker5fa8c232012-09-18 12:37:02 -0700142 if (rtl_locale) {
143 // Fill the progress bar from right to left.
144 if (pos > 0) {
145 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
146 }
147 if (pos < width-1) {
148 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
149 }
150 } else {
151 // Fill the progress bar from left to right.
152 if (pos > 0) {
153 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
154 }
155 if (pos < width-1) {
156 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
157 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700158 }
159 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700160 }
161}
162
Doug Zongkerc0441d12013-07-31 11:28:24 -0700163void ScreenRecoveryUI::SetColor(UIElement e) {
164 switch (e) {
165 case HEADER:
166 gr_color(247, 0, 6, 255);
167 break;
168 case MENU:
169 case MENU_SEL_BG:
170 gr_color(0, 106, 157, 255);
171 break;
172 case MENU_SEL_FG:
173 gr_color(255, 255, 255, 255);
174 break;
175 case LOG:
176 gr_color(249, 194, 0, 255);
177 break;
178 case TEXT_FILL:
179 gr_color(0, 0, 0, 160);
180 break;
181 default:
182 gr_color(255, 255, 255, 255);
183 break;
184 }
185}
Doug Zongker211aebc2011-10-28 15:13:10 -0700186
187// Redraw everything on the screen. Does not flip pages.
188// Should only be called with updateMutex locked.
189void ScreenRecoveryUI::draw_screen_locked()
190{
Doug Zongker16f97c32014-03-06 16:16:05 -0800191 if (!show_text) {
192 draw_background_locked(currentIcon);
193 draw_progress_locked();
194 } else {
195 gr_color(0, 0, 0, 255);
196 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700197
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800198 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700199 int i = 0;
200 if (show_menu) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700201 SetColor(HEADER);
Doug Zongker211aebc2011-10-28 15:13:10 -0700202
203 for (; i < menu_top + menu_items; ++i) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700204 if (i == menu_top) SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800205
Doug Zongker211aebc2011-10-28 15:13:10 -0700206 if (i == menu_top + menu_sel) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800207 // draw the highlight bar
Doug Zongkerc0441d12013-07-31 11:28:24 -0700208 SetColor(MENU_SEL_BG);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800209 gr_fill(0, y-2, gr_fb_width(), y+char_height+2);
210 // white text of selected item
Doug Zongkerc0441d12013-07-31 11:28:24 -0700211 SetColor(MENU_SEL_FG);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800212 if (menu[i][0]) gr_text(4, y, menu[i], 1);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700213 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700214 } else {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800215 if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top);
Doug Zongker211aebc2011-10-28 15:13:10 -0700216 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800217 y += char_height+4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700218 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700219 SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800220 y += 4;
221 gr_fill(0, y, gr_fb_width(), y+2);
222 y += 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700223 ++i;
224 }
225
Doug Zongkerc0441d12013-07-31 11:28:24 -0700226 SetColor(LOG);
Doug Zongker211aebc2011-10-28 15:13:10 -0700227
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800228 // display from the bottom up, until we hit the top of the
229 // screen, the bottom of the menu, or we've displayed the
230 // entire text buffer.
231 int ty;
232 int row = (text_top+text_rows-1) % text_rows;
233 for (int ty = gr_fb_height() - char_height, count = 0;
234 ty > y+2 && count < text_rows;
235 ty -= char_height, ++count) {
236 gr_text(4, ty, text[row], 0);
237 --row;
238 if (row < 0) row = text_rows-1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700239 }
240 }
241}
242
243// Redraw everything on the screen and flip the screen (make it visible).
244// Should only be called with updateMutex locked.
245void ScreenRecoveryUI::update_screen_locked()
246{
247 draw_screen_locked();
248 gr_flip();
249}
250
251// Updates only the progress bar, if possible, otherwise redraws the screen.
252// Should only be called with updateMutex locked.
253void ScreenRecoveryUI::update_progress_locked()
254{
255 if (show_text || !pagesIdentical) {
256 draw_screen_locked(); // Must redraw the whole screen
257 pagesIdentical = true;
258 } else {
259 draw_progress_locked(); // Draw only the progress bar and overlays
260 }
261 gr_flip();
262}
263
264// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700265void* ScreenRecoveryUI::progress_thread(void *cookie) {
266 self->progress_loop();
267 return NULL;
268}
269
270void ScreenRecoveryUI::progress_loop() {
271 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700272 for (;;) {
273 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700274 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700275
276 int redraw = 0;
277
278 // update the installation animation, if active
279 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700280 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
281 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700282 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700283 redraw = 1;
284 }
285
Doug Zongker211aebc2011-10-28 15:13:10 -0700286 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700287 int duration = progressScopeDuration;
288 if (progressBarType == DETERMINATE && duration > 0) {
289 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700290 float p = 1.0 * elapsed / duration;
291 if (p > 1.0) p = 1.0;
292 if (p > progress) {
293 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700294 redraw = 1;
295 }
296 }
297
Doug Zongker32a0a472011-11-01 11:00:20 -0700298 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700299
Doug Zongker32a0a472011-11-01 11:00:20 -0700300 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700301 double end = now();
302 // minimum of 20ms delay between frames
303 double delay = interval - (end-start);
304 if (delay < 0.02) delay = 0.02;
305 usleep((long)(delay * 1000000));
306 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700307}
308
309void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700310 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700311 if (result < 0) {
312 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
313 }
314}
315
Doug Zongker469954f2014-03-07 09:21:25 -0800316void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700317 int result = res_create_multi_display_surface(filename, frames, surface);
Doug Zongker469954f2014-03-07 09:21:25 -0800318 if (result < 0) {
319 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
320 }
321}
322
Doug Zongker02ec6b82012-08-22 17:26:40 -0700323void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700324 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700325 if (result < 0) {
326 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
327 }
328}
329
Doug Zongker211aebc2011-10-28 15:13:10 -0700330void ScreenRecoveryUI::Init()
331{
332 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700333
Doug Zongker55a36ac2013-03-04 15:49:02 -0800334 gr_font_size(&char_width, &char_height);
335
Doug Zongker211aebc2011-10-28 15:13:10 -0700336 text_col = text_row = 0;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800337 text_rows = gr_fb_height() / char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -0700338 if (text_rows > kMaxRows) text_rows = kMaxRows;
339 text_top = 1;
340
Doug Zongker55a36ac2013-03-04 15:49:02 -0800341 text_cols = gr_fb_width() / char_width;
Doug Zongker211aebc2011-10-28 15:13:10 -0700342 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
343
Doug Zongker469954f2014-03-07 09:21:25 -0800344 backgroundIcon[NONE] = NULL;
345 LoadBitmapArray("icon_installing", &installing_frames, &installation);
346 backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700347 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700348 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700349 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
350
Doug Zongker211aebc2011-10-28 15:13:10 -0700351 LoadBitmap("progress_empty", &progressBarEmpty);
352 LoadBitmap("progress_fill", &progressBarFill);
353
Doug Zongker02ec6b82012-08-22 17:26:40 -0700354 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
355 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
356 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
357 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
358
Doug Zongker211aebc2011-10-28 15:13:10 -0700359 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700360
361 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700362}
363
Doug Zongkera418aa72014-03-17 12:10:02 -0700364void ScreenRecoveryUI::SetLocale(const char* new_locale) {
365 if (new_locale) {
366 this->locale = new_locale;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700367 char* lang = strdup(locale);
368 for (char* p = lang; *p; ++p) {
369 if (*p == '_') {
370 *p = '\0';
371 break;
372 }
373 }
374
375 // A bit cheesy: keep an explicit list of supported languages
376 // that are RTL.
377 if (strcmp(lang, "ar") == 0 || // Arabic
378 strcmp(lang, "fa") == 0 || // Persian (Farsi)
379 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700380 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
381 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700382 rtl_locale = true;
383 }
384 free(lang);
Doug Zongkera418aa72014-03-17 12:10:02 -0700385 } else {
386 new_locale = NULL;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700387 }
388}
389
Doug Zongker211aebc2011-10-28 15:13:10 -0700390void ScreenRecoveryUI::SetBackground(Icon icon)
391{
392 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700393
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700394 currentIcon = icon;
395 update_screen_locked();
396
Doug Zongker211aebc2011-10-28 15:13:10 -0700397 pthread_mutex_unlock(&updateMutex);
398}
399
400void ScreenRecoveryUI::SetProgressType(ProgressType type)
401{
402 pthread_mutex_lock(&updateMutex);
403 if (progressBarType != type) {
404 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700405 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700406 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700407 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700408 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700409 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700410 pthread_mutex_unlock(&updateMutex);
411}
412
413void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
414{
415 pthread_mutex_lock(&updateMutex);
416 progressBarType = DETERMINATE;
417 progressScopeStart += progressScopeSize;
418 progressScopeSize = portion;
419 progressScopeTime = now();
420 progressScopeDuration = seconds;
421 progress = 0;
422 update_progress_locked();
423 pthread_mutex_unlock(&updateMutex);
424}
425
426void ScreenRecoveryUI::SetProgress(float fraction)
427{
428 pthread_mutex_lock(&updateMutex);
429 if (fraction < 0.0) fraction = 0.0;
430 if (fraction > 1.0) fraction = 1.0;
431 if (progressBarType == DETERMINATE && fraction > progress) {
432 // Skip updates that aren't visibly different.
Doug Zongker469954f2014-03-07 09:21:25 -0800433 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700434 float scale = width * progressScopeSize;
435 if ((int) (progress * scale) != (int) (fraction * scale)) {
436 progress = fraction;
437 update_progress_locked();
438 }
439 }
440 pthread_mutex_unlock(&updateMutex);
441}
442
443void ScreenRecoveryUI::Print(const char *fmt, ...)
444{
445 char buf[256];
446 va_list ap;
447 va_start(ap, fmt);
448 vsnprintf(buf, 256, fmt, ap);
449 va_end(ap);
450
451 fputs(buf, stdout);
452
453 // This can get called before ui_init(), so be careful.
454 pthread_mutex_lock(&updateMutex);
455 if (text_rows > 0 && text_cols > 0) {
456 char *ptr;
457 for (ptr = buf; *ptr != '\0'; ++ptr) {
458 if (*ptr == '\n' || text_col >= text_cols) {
459 text[text_row][text_col] = '\0';
460 text_col = 0;
461 text_row = (text_row + 1) % text_rows;
462 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
463 }
464 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
465 }
466 text[text_row][text_col] = '\0';
467 update_screen_locked();
468 }
469 pthread_mutex_unlock(&updateMutex);
470}
471
472void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
473 int initial_selection) {
474 int i;
475 pthread_mutex_lock(&updateMutex);
476 if (text_rows > 0 && text_cols > 0) {
477 for (i = 0; i < text_rows; ++i) {
478 if (headers[i] == NULL) break;
479 strncpy(menu[i], headers[i], text_cols-1);
480 menu[i][text_cols-1] = '\0';
481 }
482 menu_top = i;
483 for (; i < text_rows; ++i) {
484 if (items[i-menu_top] == NULL) break;
485 strncpy(menu[i], items[i-menu_top], text_cols-1);
486 menu[i][text_cols-1] = '\0';
487 }
488 menu_items = i - menu_top;
489 show_menu = 1;
490 menu_sel = initial_selection;
491 update_screen_locked();
492 }
493 pthread_mutex_unlock(&updateMutex);
494}
495
496int ScreenRecoveryUI::SelectMenu(int sel) {
497 int old_sel;
498 pthread_mutex_lock(&updateMutex);
499 if (show_menu > 0) {
500 old_sel = menu_sel;
501 menu_sel = sel;
502 if (menu_sel < 0) menu_sel = 0;
503 if (menu_sel >= menu_items) menu_sel = menu_items-1;
504 sel = menu_sel;
505 if (menu_sel != old_sel) update_screen_locked();
506 }
507 pthread_mutex_unlock(&updateMutex);
508 return sel;
509}
510
511void ScreenRecoveryUI::EndMenu() {
512 int i;
513 pthread_mutex_lock(&updateMutex);
514 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
515 show_menu = 0;
516 update_screen_locked();
517 }
518 pthread_mutex_unlock(&updateMutex);
519}
520
521bool ScreenRecoveryUI::IsTextVisible()
522{
523 pthread_mutex_lock(&updateMutex);
524 int visible = show_text;
525 pthread_mutex_unlock(&updateMutex);
526 return visible;
527}
528
529bool ScreenRecoveryUI::WasTextEverVisible()
530{
531 pthread_mutex_lock(&updateMutex);
532 int ever_visible = show_text_ever;
533 pthread_mutex_unlock(&updateMutex);
534 return ever_visible;
535}
536
537void ScreenRecoveryUI::ShowText(bool visible)
538{
539 pthread_mutex_lock(&updateMutex);
540 show_text = visible;
541 if (show_text) show_text_ever = 1;
542 update_screen_locked();
543 pthread_mutex_unlock(&updateMutex);
544}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700545
546void ScreenRecoveryUI::Redraw()
547{
548 pthread_mutex_lock(&updateMutex);
549 update_screen_locked();
550 pthread_mutex_unlock(&updateMutex);
551}