blob: c87c00ce5bebb5f22d21640273ec770642abc30f [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 Zongker5fa8c232012-09-18 12:37:02 -070055 rtl_locale(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070056 progressBarType(EMPTY),
57 progressScopeStart(0),
58 progressScopeSize(0),
59 progress(0),
60 pagesIdentical(false),
61 text_cols(0),
62 text_rows(0),
63 text_col(0),
64 text_row(0),
65 text_top(0),
66 show_text(false),
67 show_text_ever(false),
68 show_menu(false),
69 menu_top(0),
70 menu_items(0),
71 menu_sel(0),
Doug Zongker32a0a472011-11-01 11:00:20 -070072 animation_fps(20),
Doug Zongkereac881c2014-03-07 09:21:25 -080073 installing_frames(-1),
Doug Zongkerc87bab12013-11-25 13:53:25 -080074 stage(-1),
75 max_stage(-1) {
yetta_wu5b468fc2013-06-25 15:03:11 +080076
77 for (int i = 0; i < 5; i++)
78 backgroundIcon[i] = NULL;
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 Zongker39cf4172014-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 Zongkereac881c2014-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);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800103 int stageHeight = gr_get_height(stageMarkerEmpty);
104
105 int sh = (max_stage >= 0) ? stageHeight : 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700106
Doug Zongkereac881c2014-03-07 09:21:25 -0800107 iconX = (gr_fb_width() - iconWidth) / 2;
108 iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700109
110 int textX = (gr_fb_width() - textWidth) / 2;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800111 int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700112
Doug Zongker211aebc2011-10-28 15:13:10 -0700113 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800114 if (stageHeight > 0) {
115 int sw = gr_get_width(stageMarkerEmpty);
116 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
117 int y = iconY + iconHeight + 20;
118 for (int i = 0; i < max_stage; ++i) {
119 gr_blit((i < stage) ? stageMarkerFill : stageMarkerEmpty,
120 0, 0, sw, stageHeight, x, y);
121 x += sw;
122 }
123 }
124
Doug Zongker02ec6b82012-08-22 17:26:40 -0700125 gr_color(255, 255, 255, 255);
126 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700127 }
128}
129
130// Draw the progress bar (if any) on the screen. Does not flip pages.
131// Should only be called with updateMutex locked.
132void ScreenRecoveryUI::draw_progress_locked()
133{
Doug Zongker69f4b672012-04-26 14:37:53 -0700134 if (currentIcon == ERROR) return;
135
Doug Zongker02ec6b82012-08-22 17:26:40 -0700136 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Doug Zongkereac881c2014-03-07 09:21:25 -0800137 gr_surface icon = installation[installingFrame];
138 gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY);
Doug Zongker211aebc2011-10-28 15:13:10 -0700139 }
140
141 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700142 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700143 int width = gr_get_width(progressBarEmpty);
144 int height = gr_get_height(progressBarEmpty);
145
146 int dx = (gr_fb_width() - width)/2;
147 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
148
149 // Erase behind the progress bar (in case this was a progress-only update)
150 gr_color(0, 0, 0, 255);
151 gr_fill(dx, dy, width, height);
152
153 if (progressBarType == DETERMINATE) {
154 float p = progressScopeStart + progress * progressScopeSize;
155 int pos = (int) (p * width);
156
Doug Zongker5fa8c232012-09-18 12:37:02 -0700157 if (rtl_locale) {
158 // Fill the progress bar from right to left.
159 if (pos > 0) {
160 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
161 }
162 if (pos < width-1) {
163 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
164 }
165 } else {
166 // Fill the progress bar from left to right.
167 if (pos > 0) {
168 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
169 }
170 if (pos < width-1) {
171 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
172 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700173 }
174 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700175 }
176}
177
Doug Zongkerc0441d12013-07-31 11:28:24 -0700178void ScreenRecoveryUI::SetColor(UIElement e) {
179 switch (e) {
180 case HEADER:
181 gr_color(247, 0, 6, 255);
182 break;
183 case MENU:
184 case MENU_SEL_BG:
185 gr_color(0, 106, 157, 255);
186 break;
187 case MENU_SEL_FG:
188 gr_color(255, 255, 255, 255);
189 break;
190 case LOG:
191 gr_color(249, 194, 0, 255);
192 break;
193 case TEXT_FILL:
194 gr_color(0, 0, 0, 160);
195 break;
196 default:
197 gr_color(255, 255, 255, 255);
198 break;
199 }
200}
Doug Zongker211aebc2011-10-28 15:13:10 -0700201
202// Redraw everything on the screen. Does not flip pages.
203// Should only be called with updateMutex locked.
204void ScreenRecoveryUI::draw_screen_locked()
205{
Doug Zongker39cf4172014-03-06 16:16:05 -0800206 if (!show_text) {
207 draw_background_locked(currentIcon);
208 draw_progress_locked();
209 } else {
210 gr_color(0, 0, 0, 255);
211 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700212
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800213 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700214 int i = 0;
215 if (show_menu) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700216 SetColor(HEADER);
Doug Zongker211aebc2011-10-28 15:13:10 -0700217
218 for (; i < menu_top + menu_items; ++i) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700219 if (i == menu_top) SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800220
Doug Zongker211aebc2011-10-28 15:13:10 -0700221 if (i == menu_top + menu_sel) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800222 // draw the highlight bar
Doug Zongkerc0441d12013-07-31 11:28:24 -0700223 SetColor(MENU_SEL_BG);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800224 gr_fill(0, y-2, gr_fb_width(), y+char_height+2);
225 // white text of selected item
Doug Zongkerc0441d12013-07-31 11:28:24 -0700226 SetColor(MENU_SEL_FG);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800227 if (menu[i][0]) gr_text(4, y, menu[i], 1);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700228 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700229 } else {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800230 if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top);
Doug Zongker211aebc2011-10-28 15:13:10 -0700231 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800232 y += char_height+4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700233 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700234 SetColor(MENU);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800235 y += 4;
236 gr_fill(0, y, gr_fb_width(), y+2);
237 y += 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700238 ++i;
239 }
240
Doug Zongkerc0441d12013-07-31 11:28:24 -0700241 SetColor(LOG);
Doug Zongker211aebc2011-10-28 15:13:10 -0700242
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800243 // display from the bottom up, until we hit the top of the
244 // screen, the bottom of the menu, or we've displayed the
245 // entire text buffer.
246 int ty;
247 int row = (text_top+text_rows-1) % text_rows;
248 for (int ty = gr_fb_height() - char_height, count = 0;
249 ty > y+2 && count < text_rows;
250 ty -= char_height, ++count) {
251 gr_text(4, ty, text[row], 0);
252 --row;
253 if (row < 0) row = text_rows-1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700254 }
255 }
256}
257
258// Redraw everything on the screen and flip the screen (make it visible).
259// Should only be called with updateMutex locked.
260void ScreenRecoveryUI::update_screen_locked()
261{
262 draw_screen_locked();
263 gr_flip();
264}
265
266// Updates only the progress bar, if possible, otherwise redraws the screen.
267// Should only be called with updateMutex locked.
268void ScreenRecoveryUI::update_progress_locked()
269{
270 if (show_text || !pagesIdentical) {
271 draw_screen_locked(); // Must redraw the whole screen
272 pagesIdentical = true;
273 } else {
274 draw_progress_locked(); // Draw only the progress bar and overlays
275 }
276 gr_flip();
277}
278
279// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700280void* ScreenRecoveryUI::progress_thread(void *cookie) {
281 self->progress_loop();
282 return NULL;
283}
284
285void ScreenRecoveryUI::progress_loop() {
286 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700287 for (;;) {
288 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700289 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700290
291 int redraw = 0;
292
293 // update the installation animation, if active
294 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700295 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
296 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700297 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700298 redraw = 1;
299 }
300
Doug Zongker211aebc2011-10-28 15:13:10 -0700301 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700302 int duration = progressScopeDuration;
303 if (progressBarType == DETERMINATE && duration > 0) {
304 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700305 float p = 1.0 * elapsed / duration;
306 if (p > 1.0) p = 1.0;
307 if (p > progress) {
308 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700309 redraw = 1;
310 }
311 }
312
Doug Zongker32a0a472011-11-01 11:00:20 -0700313 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700314
Doug Zongker32a0a472011-11-01 11:00:20 -0700315 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700316 double end = now();
317 // minimum of 20ms delay between frames
318 double delay = interval - (end-start);
319 if (delay < 0.02) delay = 0.02;
320 usleep((long)(delay * 1000000));
321 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700322}
323
324void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
325 int result = res_create_surface(filename, surface);
326 if (result < 0) {
327 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
328 }
329}
330
Doug Zongkereac881c2014-03-07 09:21:25 -0800331void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) {
332 int result = res_create_multi_surface(filename, frames, surface);
333 if (result < 0) {
334 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
335 }
336}
337
Doug Zongker02ec6b82012-08-22 17:26:40 -0700338void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
339 int result = res_create_localized_surface(filename, surface);
340 if (result < 0) {
341 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
342 }
343}
344
Doug Zongker211aebc2011-10-28 15:13:10 -0700345void ScreenRecoveryUI::Init()
346{
347 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700348
Doug Zongker55a36ac2013-03-04 15:49:02 -0800349 gr_font_size(&char_width, &char_height);
350
Doug Zongker211aebc2011-10-28 15:13:10 -0700351 text_col = text_row = 0;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800352 text_rows = gr_fb_height() / char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -0700353 if (text_rows > kMaxRows) text_rows = kMaxRows;
354 text_top = 1;
355
Doug Zongker55a36ac2013-03-04 15:49:02 -0800356 text_cols = gr_fb_width() / char_width;
Doug Zongker211aebc2011-10-28 15:13:10 -0700357 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
358
Alistair Strachan9b8ae802013-07-17 10:34:36 -0700359 backgroundIcon[NONE] = NULL;
Doug Zongkereac881c2014-03-07 09:21:25 -0800360 LoadBitmapArray("icon_installing", &installing_frames, &installation);
361 backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700362 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700363 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700364 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
365
Doug Zongker211aebc2011-10-28 15:13:10 -0700366 LoadBitmap("progress_empty", &progressBarEmpty);
367 LoadBitmap("progress_fill", &progressBarFill);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800368 LoadBitmap("stage_empty", &stageMarkerEmpty);
369 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700370
Doug Zongker02ec6b82012-08-22 17:26:40 -0700371 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
372 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
373 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
374 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
375
Doug Zongker211aebc2011-10-28 15:13:10 -0700376 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700377
378 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700379}
380
Doug Zongker5fa8c232012-09-18 12:37:02 -0700381void ScreenRecoveryUI::SetLocale(const char* locale) {
382 if (locale) {
383 char* lang = strdup(locale);
384 for (char* p = lang; *p; ++p) {
385 if (*p == '_') {
386 *p = '\0';
387 break;
388 }
389 }
390
391 // A bit cheesy: keep an explicit list of supported languages
392 // that are RTL.
393 if (strcmp(lang, "ar") == 0 || // Arabic
394 strcmp(lang, "fa") == 0 || // Persian (Farsi)
395 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700396 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
397 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700398 rtl_locale = true;
399 }
400 free(lang);
401 }
402}
403
Doug Zongker211aebc2011-10-28 15:13:10 -0700404void ScreenRecoveryUI::SetBackground(Icon icon)
405{
406 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700407
Doug Zongker52eeea42012-09-04 14:28:25 -0700408 currentIcon = icon;
409 update_screen_locked();
410
Doug Zongker211aebc2011-10-28 15:13:10 -0700411 pthread_mutex_unlock(&updateMutex);
412}
413
414void ScreenRecoveryUI::SetProgressType(ProgressType type)
415{
416 pthread_mutex_lock(&updateMutex);
417 if (progressBarType != type) {
418 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700419 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700420 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700421 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700422 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700423 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700424 pthread_mutex_unlock(&updateMutex);
425}
426
427void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
428{
429 pthread_mutex_lock(&updateMutex);
430 progressBarType = DETERMINATE;
431 progressScopeStart += progressScopeSize;
432 progressScopeSize = portion;
433 progressScopeTime = now();
434 progressScopeDuration = seconds;
435 progress = 0;
436 update_progress_locked();
437 pthread_mutex_unlock(&updateMutex);
438}
439
440void ScreenRecoveryUI::SetProgress(float fraction)
441{
442 pthread_mutex_lock(&updateMutex);
443 if (fraction < 0.0) fraction = 0.0;
444 if (fraction > 1.0) fraction = 1.0;
445 if (progressBarType == DETERMINATE && fraction > progress) {
446 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800447 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700448 float scale = width * progressScopeSize;
449 if ((int) (progress * scale) != (int) (fraction * scale)) {
450 progress = fraction;
451 update_progress_locked();
452 }
453 }
454 pthread_mutex_unlock(&updateMutex);
455}
456
Doug Zongkerc87bab12013-11-25 13:53:25 -0800457void ScreenRecoveryUI::SetStage(int current, int max) {
458 pthread_mutex_lock(&updateMutex);
459 stage = current;
460 max_stage = max;
461 pthread_mutex_unlock(&updateMutex);
462}
463
Doug Zongker211aebc2011-10-28 15:13:10 -0700464void ScreenRecoveryUI::Print(const char *fmt, ...)
465{
466 char buf[256];
467 va_list ap;
468 va_start(ap, fmt);
469 vsnprintf(buf, 256, fmt, ap);
470 va_end(ap);
471
472 fputs(buf, stdout);
473
474 // This can get called before ui_init(), so be careful.
475 pthread_mutex_lock(&updateMutex);
476 if (text_rows > 0 && text_cols > 0) {
477 char *ptr;
478 for (ptr = buf; *ptr != '\0'; ++ptr) {
479 if (*ptr == '\n' || text_col >= text_cols) {
480 text[text_row][text_col] = '\0';
481 text_col = 0;
482 text_row = (text_row + 1) % text_rows;
483 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
484 }
485 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
486 }
487 text[text_row][text_col] = '\0';
488 update_screen_locked();
489 }
490 pthread_mutex_unlock(&updateMutex);
491}
492
493void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
494 int initial_selection) {
495 int i;
496 pthread_mutex_lock(&updateMutex);
497 if (text_rows > 0 && text_cols > 0) {
498 for (i = 0; i < text_rows; ++i) {
499 if (headers[i] == NULL) break;
500 strncpy(menu[i], headers[i], text_cols-1);
501 menu[i][text_cols-1] = '\0';
502 }
503 menu_top = i;
504 for (; i < text_rows; ++i) {
505 if (items[i-menu_top] == NULL) break;
506 strncpy(menu[i], items[i-menu_top], text_cols-1);
507 menu[i][text_cols-1] = '\0';
508 }
509 menu_items = i - menu_top;
510 show_menu = 1;
511 menu_sel = initial_selection;
512 update_screen_locked();
513 }
514 pthread_mutex_unlock(&updateMutex);
515}
516
517int ScreenRecoveryUI::SelectMenu(int sel) {
518 int old_sel;
519 pthread_mutex_lock(&updateMutex);
520 if (show_menu > 0) {
521 old_sel = menu_sel;
522 menu_sel = sel;
523 if (menu_sel < 0) menu_sel = 0;
524 if (menu_sel >= menu_items) menu_sel = menu_items-1;
525 sel = menu_sel;
526 if (menu_sel != old_sel) update_screen_locked();
527 }
528 pthread_mutex_unlock(&updateMutex);
529 return sel;
530}
531
532void ScreenRecoveryUI::EndMenu() {
533 int i;
534 pthread_mutex_lock(&updateMutex);
535 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
536 show_menu = 0;
537 update_screen_locked();
538 }
539 pthread_mutex_unlock(&updateMutex);
540}
541
542bool ScreenRecoveryUI::IsTextVisible()
543{
544 pthread_mutex_lock(&updateMutex);
545 int visible = show_text;
546 pthread_mutex_unlock(&updateMutex);
547 return visible;
548}
549
550bool ScreenRecoveryUI::WasTextEverVisible()
551{
552 pthread_mutex_lock(&updateMutex);
553 int ever_visible = show_text_ever;
554 pthread_mutex_unlock(&updateMutex);
555 return ever_visible;
556}
557
558void ScreenRecoveryUI::ShowText(bool visible)
559{
560 pthread_mutex_lock(&updateMutex);
561 show_text = visible;
562 if (show_text) show_text_ever = 1;
563 update_screen_locked();
564 pthread_mutex_unlock(&updateMutex);
565}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700566
567void ScreenRecoveryUI::Redraw()
568{
569 pthread_mutex_lock(&updateMutex);
570 update_screen_locked();
571 pthread_mutex_unlock(&updateMutex);
572}