blob: 7826693b2fa3c6ea8b809c0938edcac69db159f7 [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
Bjorn Andersson80a7a462013-08-30 16:59:06 -070078 memset(text, 0, sizeof(text));
79
Doug Zongker211aebc2011-10-28 15:13:10 -070080 pthread_mutex_init(&updateMutex, NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -070081 self = this;
82}
83
Doug Zongker211aebc2011-10-28 15:13:10 -070084// Clear the screen and draw the currently selected background icon (if any).
85// Should only be called with updateMutex locked.
86void ScreenRecoveryUI::draw_background_locked(Icon icon)
87{
88 pagesIdentical = false;
89 gr_color(0, 0, 0, 255);
Doug Zongker16f97c32014-03-06 16:16:05 -080090 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -070091
92 if (icon) {
93 gr_surface surface = backgroundIcon[icon];
Doug Zongker469954f2014-03-07 09:21:25 -080094 if (icon == INSTALLING_UPDATE || icon == ERASING) {
95 surface = installation[installingFrame];
96 }
Doug Zongker02ec6b82012-08-22 17:26:40 -070097 gr_surface text_surface = backgroundText[icon];
98
Doug Zongker211aebc2011-10-28 15:13:10 -070099 int iconWidth = gr_get_width(surface);
100 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700101 int textWidth = gr_get_width(text_surface);
102 int textHeight = gr_get_height(text_surface);
103
Doug Zongker469954f2014-03-07 09:21:25 -0800104 iconX = (gr_fb_width() - iconWidth) / 2;
105 iconY = (gr_fb_height() - (iconHeight+textHeight+40)) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700106
107 int textX = (gr_fb_width() - textWidth) / 2;
108 int textY = ((gr_fb_height() - (iconHeight+textHeight+40)) / 2) + iconHeight + 40;
109
Doug Zongker211aebc2011-10-28 15:13:10 -0700110 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700111
112 gr_color(255, 255, 255, 255);
113 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700114 }
115}
116
117// Draw the progress bar (if any) on the screen. Does not flip pages.
118// Should only be called with updateMutex locked.
119void ScreenRecoveryUI::draw_progress_locked()
120{
Doug Zongker69f4b672012-04-26 14:37:53 -0700121 if (currentIcon == ERROR) return;
122
Doug Zongker02ec6b82012-08-22 17:26:40 -0700123 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Doug Zongker469954f2014-03-07 09:21:25 -0800124 gr_surface icon = installation[installingFrame];
125 gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY);
Doug Zongker211aebc2011-10-28 15:13:10 -0700126 }
127
128 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700129 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700130 int width = gr_get_width(progressBarEmpty);
131 int height = gr_get_height(progressBarEmpty);
132
133 int dx = (gr_fb_width() - width)/2;
134 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
135
136 // Erase behind the progress bar (in case this was a progress-only update)
137 gr_color(0, 0, 0, 255);
138 gr_fill(dx, dy, width, height);
139
140 if (progressBarType == DETERMINATE) {
141 float p = progressScopeStart + progress * progressScopeSize;
142 int pos = (int) (p * width);
143
Doug Zongker5fa8c232012-09-18 12:37:02 -0700144 if (rtl_locale) {
145 // Fill the progress bar from right to left.
146 if (pos > 0) {
147 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
148 }
149 if (pos < width-1) {
150 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
151 }
152 } else {
153 // Fill the progress bar from left to right.
154 if (pos > 0) {
155 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
156 }
157 if (pos < width-1) {
158 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
159 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700160 }
161 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700162 }
163}
164
Doug Zongkerc0441d12013-07-31 11:28:24 -0700165void ScreenRecoveryUI::SetColor(UIElement e) {
166 switch (e) {
167 case HEADER:
168 gr_color(247, 0, 6, 255);
169 break;
170 case MENU:
171 case MENU_SEL_BG:
172 gr_color(0, 106, 157, 255);
173 break;
174 case MENU_SEL_FG:
175 gr_color(255, 255, 255, 255);
176 break;
177 case LOG:
178 gr_color(249, 194, 0, 255);
179 break;
180 case TEXT_FILL:
181 gr_color(0, 0, 0, 160);
182 break;
183 default:
184 gr_color(255, 255, 255, 255);
185 break;
186 }
187}
Doug Zongker211aebc2011-10-28 15:13:10 -0700188
189// Redraw everything on the screen. Does not flip pages.
190// Should only be called with updateMutex locked.
191void ScreenRecoveryUI::draw_screen_locked()
192{
Doug Zongker16f97c32014-03-06 16:16:05 -0800193 if (!show_text) {
194 draw_background_locked(currentIcon);
195 draw_progress_locked();
196 } else {
197 gr_color(0, 0, 0, 255);
198 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700199
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800200 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700201 int i = 0;
202 if (show_menu) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700203 SetColor(HEADER);
Doug Zongker211aebc2011-10-28 15:13:10 -0700204
205 for (; i < menu_top + menu_items; ++i) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700206 if (i == menu_top) SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800207
Doug Zongker211aebc2011-10-28 15:13:10 -0700208 if (i == menu_top + menu_sel) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800209 // draw the highlight bar
Doug Zongkerc0441d12013-07-31 11:28:24 -0700210 SetColor(MENU_SEL_BG);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800211 gr_fill(0, y-2, gr_fb_width(), y+char_height+2);
212 // white text of selected item
Doug Zongkerc0441d12013-07-31 11:28:24 -0700213 SetColor(MENU_SEL_FG);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800214 if (menu[i][0]) gr_text(4, y, menu[i], 1);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700215 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700216 } else {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800217 if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top);
Doug Zongker211aebc2011-10-28 15:13:10 -0700218 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800219 y += char_height+4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700220 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700221 SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800222 y += 4;
223 gr_fill(0, y, gr_fb_width(), y+2);
224 y += 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700225 ++i;
226 }
227
Doug Zongkerc0441d12013-07-31 11:28:24 -0700228 SetColor(LOG);
Doug Zongker211aebc2011-10-28 15:13:10 -0700229
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800230 // display from the bottom up, until we hit the top of the
231 // screen, the bottom of the menu, or we've displayed the
232 // entire text buffer.
233 int ty;
234 int row = (text_top+text_rows-1) % text_rows;
235 for (int ty = gr_fb_height() - char_height, count = 0;
236 ty > y+2 && count < text_rows;
237 ty -= char_height, ++count) {
238 gr_text(4, ty, text[row], 0);
239 --row;
240 if (row < 0) row = text_rows-1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700241 }
242 }
243}
244
245// Redraw everything on the screen and flip the screen (make it visible).
246// Should only be called with updateMutex locked.
247void ScreenRecoveryUI::update_screen_locked()
248{
249 draw_screen_locked();
250 gr_flip();
251}
252
253// Updates only the progress bar, if possible, otherwise redraws the screen.
254// Should only be called with updateMutex locked.
255void ScreenRecoveryUI::update_progress_locked()
256{
257 if (show_text || !pagesIdentical) {
258 draw_screen_locked(); // Must redraw the whole screen
259 pagesIdentical = true;
260 } else {
261 draw_progress_locked(); // Draw only the progress bar and overlays
262 }
263 gr_flip();
264}
265
266// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700267void* ScreenRecoveryUI::progress_thread(void *cookie) {
268 self->progress_loop();
269 return NULL;
270}
271
272void ScreenRecoveryUI::progress_loop() {
273 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700274 for (;;) {
275 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700276 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700277
278 int redraw = 0;
279
280 // update the installation animation, if active
281 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700282 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
283 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700284 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700285 redraw = 1;
286 }
287
Doug Zongker211aebc2011-10-28 15:13:10 -0700288 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700289 int duration = progressScopeDuration;
290 if (progressBarType == DETERMINATE && duration > 0) {
291 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700292 float p = 1.0 * elapsed / duration;
293 if (p > 1.0) p = 1.0;
294 if (p > progress) {
295 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700296 redraw = 1;
297 }
298 }
299
Doug Zongker32a0a472011-11-01 11:00:20 -0700300 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700301
Doug Zongker32a0a472011-11-01 11:00:20 -0700302 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700303 double end = now();
304 // minimum of 20ms delay between frames
305 double delay = interval - (end-start);
306 if (delay < 0.02) delay = 0.02;
307 usleep((long)(delay * 1000000));
308 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700309}
310
311void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700312 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700313 if (result < 0) {
314 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
315 }
316}
317
Doug Zongker469954f2014-03-07 09:21:25 -0800318void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700319 int result = res_create_multi_display_surface(filename, frames, surface);
Doug Zongker469954f2014-03-07 09:21:25 -0800320 if (result < 0) {
321 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
322 }
323}
324
Doug Zongker02ec6b82012-08-22 17:26:40 -0700325void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700326 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700327 if (result < 0) {
328 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
329 }
330}
331
Doug Zongker211aebc2011-10-28 15:13:10 -0700332void ScreenRecoveryUI::Init()
333{
334 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700335
Doug Zongker55a36ac2013-03-04 15:49:02 -0800336 gr_font_size(&char_width, &char_height);
337
Doug Zongker211aebc2011-10-28 15:13:10 -0700338 text_col = text_row = 0;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800339 text_rows = gr_fb_height() / char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -0700340 if (text_rows > kMaxRows) text_rows = kMaxRows;
341 text_top = 1;
342
Doug Zongker55a36ac2013-03-04 15:49:02 -0800343 text_cols = gr_fb_width() / char_width;
Doug Zongker211aebc2011-10-28 15:13:10 -0700344 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
345
Doug Zongker469954f2014-03-07 09:21:25 -0800346 backgroundIcon[NONE] = NULL;
347 LoadBitmapArray("icon_installing", &installing_frames, &installation);
348 backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700349 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700350 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700351 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
352
Doug Zongker211aebc2011-10-28 15:13:10 -0700353 LoadBitmap("progress_empty", &progressBarEmpty);
354 LoadBitmap("progress_fill", &progressBarFill);
355
Doug Zongker02ec6b82012-08-22 17:26:40 -0700356 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
357 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
358 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
359 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
360
Doug Zongker211aebc2011-10-28 15:13:10 -0700361 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700362
363 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700364}
365
Doug Zongkera418aa72014-03-17 12:10:02 -0700366void ScreenRecoveryUI::SetLocale(const char* new_locale) {
367 if (new_locale) {
368 this->locale = new_locale;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700369 char* lang = strdup(locale);
370 for (char* p = lang; *p; ++p) {
371 if (*p == '_') {
372 *p = '\0';
373 break;
374 }
375 }
376
377 // A bit cheesy: keep an explicit list of supported languages
378 // that are RTL.
379 if (strcmp(lang, "ar") == 0 || // Arabic
380 strcmp(lang, "fa") == 0 || // Persian (Farsi)
381 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700382 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
383 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700384 rtl_locale = true;
385 }
386 free(lang);
Doug Zongkera418aa72014-03-17 12:10:02 -0700387 } else {
388 new_locale = NULL;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700389 }
390}
391
Doug Zongker211aebc2011-10-28 15:13:10 -0700392void ScreenRecoveryUI::SetBackground(Icon icon)
393{
394 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700395
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700396 currentIcon = icon;
397 update_screen_locked();
398
Doug Zongker211aebc2011-10-28 15:13:10 -0700399 pthread_mutex_unlock(&updateMutex);
400}
401
402void ScreenRecoveryUI::SetProgressType(ProgressType type)
403{
404 pthread_mutex_lock(&updateMutex);
405 if (progressBarType != type) {
406 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700407 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700408 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700409 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700410 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700411 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700412 pthread_mutex_unlock(&updateMutex);
413}
414
415void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
416{
417 pthread_mutex_lock(&updateMutex);
418 progressBarType = DETERMINATE;
419 progressScopeStart += progressScopeSize;
420 progressScopeSize = portion;
421 progressScopeTime = now();
422 progressScopeDuration = seconds;
423 progress = 0;
424 update_progress_locked();
425 pthread_mutex_unlock(&updateMutex);
426}
427
428void ScreenRecoveryUI::SetProgress(float fraction)
429{
430 pthread_mutex_lock(&updateMutex);
431 if (fraction < 0.0) fraction = 0.0;
432 if (fraction > 1.0) fraction = 1.0;
433 if (progressBarType == DETERMINATE && fraction > progress) {
434 // Skip updates that aren't visibly different.
Doug Zongker469954f2014-03-07 09:21:25 -0800435 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700436 float scale = width * progressScopeSize;
437 if ((int) (progress * scale) != (int) (fraction * scale)) {
438 progress = fraction;
439 update_progress_locked();
440 }
441 }
442 pthread_mutex_unlock(&updateMutex);
443}
444
445void ScreenRecoveryUI::Print(const char *fmt, ...)
446{
447 char buf[256];
448 va_list ap;
449 va_start(ap, fmt);
450 vsnprintf(buf, 256, fmt, ap);
451 va_end(ap);
452
453 fputs(buf, stdout);
454
455 // This can get called before ui_init(), so be careful.
456 pthread_mutex_lock(&updateMutex);
457 if (text_rows > 0 && text_cols > 0) {
458 char *ptr;
459 for (ptr = buf; *ptr != '\0'; ++ptr) {
460 if (*ptr == '\n' || text_col >= text_cols) {
461 text[text_row][text_col] = '\0';
462 text_col = 0;
463 text_row = (text_row + 1) % text_rows;
464 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
465 }
466 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
467 }
468 text[text_row][text_col] = '\0';
469 update_screen_locked();
470 }
471 pthread_mutex_unlock(&updateMutex);
472}
473
474void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
475 int initial_selection) {
476 int i;
477 pthread_mutex_lock(&updateMutex);
478 if (text_rows > 0 && text_cols > 0) {
479 for (i = 0; i < text_rows; ++i) {
480 if (headers[i] == NULL) break;
481 strncpy(menu[i], headers[i], text_cols-1);
482 menu[i][text_cols-1] = '\0';
483 }
484 menu_top = i;
485 for (; i < text_rows; ++i) {
486 if (items[i-menu_top] == NULL) break;
487 strncpy(menu[i], items[i-menu_top], text_cols-1);
488 menu[i][text_cols-1] = '\0';
489 }
490 menu_items = i - menu_top;
491 show_menu = 1;
492 menu_sel = initial_selection;
493 update_screen_locked();
494 }
495 pthread_mutex_unlock(&updateMutex);
496}
497
498int ScreenRecoveryUI::SelectMenu(int sel) {
499 int old_sel;
500 pthread_mutex_lock(&updateMutex);
501 if (show_menu > 0) {
502 old_sel = menu_sel;
503 menu_sel = sel;
504 if (menu_sel < 0) menu_sel = 0;
505 if (menu_sel >= menu_items) menu_sel = menu_items-1;
506 sel = menu_sel;
507 if (menu_sel != old_sel) update_screen_locked();
508 }
509 pthread_mutex_unlock(&updateMutex);
510 return sel;
511}
512
513void ScreenRecoveryUI::EndMenu() {
514 int i;
515 pthread_mutex_lock(&updateMutex);
516 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
517 show_menu = 0;
518 update_screen_locked();
519 }
520 pthread_mutex_unlock(&updateMutex);
521}
522
523bool ScreenRecoveryUI::IsTextVisible()
524{
525 pthread_mutex_lock(&updateMutex);
526 int visible = show_text;
527 pthread_mutex_unlock(&updateMutex);
528 return visible;
529}
530
531bool ScreenRecoveryUI::WasTextEverVisible()
532{
533 pthread_mutex_lock(&updateMutex);
534 int ever_visible = show_text_ever;
535 pthread_mutex_unlock(&updateMutex);
536 return ever_visible;
537}
538
539void ScreenRecoveryUI::ShowText(bool visible)
540{
541 pthread_mutex_lock(&updateMutex);
542 show_text = visible;
543 if (show_text) show_text_ever = 1;
544 update_screen_locked();
545 pthread_mutex_unlock(&updateMutex);
546}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700547
548void ScreenRecoveryUI::Redraw()
549{
550 pthread_mutex_lock(&updateMutex);
551 update_screen_locked();
552 pthread_mutex_unlock(&updateMutex);
553}