blob: b53ab11588558709b57e8a0797f07e5c10d3c39a [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
73 // These values are correct for the default image resources
74 // provided with the android platform. Devices which use
75 // different resources should have a subclass of ScreenRecoveryUI
76 // that overrides Init() to set these values appropriately and
77 // then call the superclass Init().
78 animation_fps(20),
Doug Zongker8347cb22012-10-08 08:38:16 -070079 indeterminate_frames(6),
80 installing_frames(7),
81 install_overlay_offset_x(13),
82 install_overlay_offset_y(190),
Doug Zongker52eeea4f2012-09-04 14:28:25 -070083 overlay_offset_x(-1),
84 overlay_offset_y(-1) {
yetta_wu2f6877a2013-06-25 15:03:11 +080085
86 for (int i = 0; i < 5; i++)
87 backgroundIcon[i] = NULL;
88
Bjorn Andersson80a7a462013-08-30 16:59:06 -070089 memset(text, 0, sizeof(text));
90
Doug Zongker211aebc2011-10-28 15:13:10 -070091 pthread_mutex_init(&updateMutex, NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -070092 self = this;
93}
94
95// Draw the given frame over the installation overlay animation. The
96// background is not cleared or draw with the base icon first; we
97// assume that the frame already contains some other frame of the
98// animation. Does nothing if no overlay animation is defined.
99// Should only be called with updateMutex locked.
100void ScreenRecoveryUI::draw_install_overlay_locked(int frame) {
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700101 if (installationOverlay == NULL || overlay_offset_x < 0) return;
Doug Zongker211aebc2011-10-28 15:13:10 -0700102 gr_surface surface = installationOverlay[frame];
103 int iconWidth = gr_get_width(surface);
104 int iconHeight = gr_get_height(surface);
105 gr_blit(surface, 0, 0, iconWidth, iconHeight,
Doug Zongker02ec6b82012-08-22 17:26:40 -0700106 overlay_offset_x, overlay_offset_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700107}
108
109// Clear the screen and draw the currently selected background icon (if any).
110// Should only be called with updateMutex locked.
111void ScreenRecoveryUI::draw_background_locked(Icon icon)
112{
113 pagesIdentical = false;
114 gr_color(0, 0, 0, 255);
115 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
116
117 if (icon) {
118 gr_surface surface = backgroundIcon[icon];
Doug Zongker02ec6b82012-08-22 17:26:40 -0700119 gr_surface text_surface = backgroundText[icon];
120
Doug Zongker211aebc2011-10-28 15:13:10 -0700121 int iconWidth = gr_get_width(surface);
122 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700123 int textWidth = gr_get_width(text_surface);
124 int textHeight = gr_get_height(text_surface);
125
Doug Zongker211aebc2011-10-28 15:13:10 -0700126 int iconX = (gr_fb_width() - iconWidth) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700127 int iconY = (gr_fb_height() - (iconHeight+textHeight+40)) / 2;
128
129 int textX = (gr_fb_width() - textWidth) / 2;
130 int textY = ((gr_fb_height() - (iconHeight+textHeight+40)) / 2) + iconHeight + 40;
131
Doug Zongker211aebc2011-10-28 15:13:10 -0700132 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700133 if (icon == INSTALLING_UPDATE || icon == ERASING) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700134 draw_install_overlay_locked(installingFrame);
135 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700136
137 gr_color(255, 255, 255, 255);
138 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700139 }
140}
141
142// Draw the progress bar (if any) on the screen. Does not flip pages.
143// Should only be called with updateMutex locked.
144void ScreenRecoveryUI::draw_progress_locked()
145{
Doug Zongker69f4b672012-04-26 14:37:53 -0700146 if (currentIcon == ERROR) return;
147
Doug Zongker02ec6b82012-08-22 17:26:40 -0700148 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700149 draw_install_overlay_locked(installingFrame);
150 }
151
152 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700153 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700154 int width = gr_get_width(progressBarEmpty);
155 int height = gr_get_height(progressBarEmpty);
156
157 int dx = (gr_fb_width() - width)/2;
158 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
159
160 // Erase behind the progress bar (in case this was a progress-only update)
161 gr_color(0, 0, 0, 255);
162 gr_fill(dx, dy, width, height);
163
164 if (progressBarType == DETERMINATE) {
165 float p = progressScopeStart + progress * progressScopeSize;
166 int pos = (int) (p * width);
167
Doug Zongker5fa8c232012-09-18 12:37:02 -0700168 if (rtl_locale) {
169 // Fill the progress bar from right to left.
170 if (pos > 0) {
171 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
172 }
173 if (pos < width-1) {
174 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
175 }
176 } else {
177 // Fill the progress bar from left to right.
178 if (pos > 0) {
179 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
180 }
181 if (pos < width-1) {
182 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
183 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700184 }
185 }
186
187 if (progressBarType == INDETERMINATE) {
188 static int frame = 0;
189 gr_blit(progressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700190 // in RTL locales, we run the animation backwards, which
191 // makes the spinner spin the other way.
192 if (rtl_locale) {
193 frame = (frame + indeterminate_frames - 1) % indeterminate_frames;
194 } else {
195 frame = (frame + 1) % indeterminate_frames;
196 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700197 }
198 }
199}
200
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800201#define C_HEADER 247,0,6
202#define C_MENU 0,106,157
203#define C_LOG 249,194,0
Doug Zongker211aebc2011-10-28 15:13:10 -0700204
205// Redraw everything on the screen. Does not flip pages.
206// Should only be called with updateMutex locked.
207void ScreenRecoveryUI::draw_screen_locked()
208{
209 draw_background_locked(currentIcon);
210 draw_progress_locked();
211
212 if (show_text) {
213 gr_color(0, 0, 0, 160);
214 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
215
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800216 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700217 int i = 0;
218 if (show_menu) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800219 gr_color(C_HEADER, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700220
221 for (; i < menu_top + menu_items; ++i) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800222 if (i == menu_top) gr_color(C_MENU, 255);
223
Doug Zongker211aebc2011-10-28 15:13:10 -0700224 if (i == menu_top + menu_sel) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800225 // draw the highlight bar
226 gr_fill(0, y-2, gr_fb_width(), y+char_height+2);
227 // white text of selected item
Doug Zongker211aebc2011-10-28 15:13:10 -0700228 gr_color(255, 255, 255, 255);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800229 if (menu[i][0]) gr_text(4, y, menu[i], 1);
230 gr_color(C_MENU, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700231 } else {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800232 if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top);
Doug Zongker211aebc2011-10-28 15:13:10 -0700233 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800234 y += char_height+4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700235 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800236 gr_color(C_MENU, 255);
237 y += 4;
238 gr_fill(0, y, gr_fb_width(), y+2);
239 y += 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700240 ++i;
241 }
242
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800243 gr_color(C_LOG, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700244
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800245 // display from the bottom up, until we hit the top of the
246 // screen, the bottom of the menu, or we've displayed the
247 // entire text buffer.
248 int ty;
249 int row = (text_top+text_rows-1) % text_rows;
250 for (int ty = gr_fb_height() - char_height, count = 0;
251 ty > y+2 && count < text_rows;
252 ty -= char_height, ++count) {
253 gr_text(4, ty, text[row], 0);
254 --row;
255 if (row < 0) row = text_rows-1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700256 }
257 }
258}
259
260// Redraw everything on the screen and flip the screen (make it visible).
261// Should only be called with updateMutex locked.
262void ScreenRecoveryUI::update_screen_locked()
263{
264 draw_screen_locked();
265 gr_flip();
266}
267
268// Updates only the progress bar, if possible, otherwise redraws the screen.
269// Should only be called with updateMutex locked.
270void ScreenRecoveryUI::update_progress_locked()
271{
272 if (show_text || !pagesIdentical) {
273 draw_screen_locked(); // Must redraw the whole screen
274 pagesIdentical = true;
275 } else {
276 draw_progress_locked(); // Draw only the progress bar and overlays
277 }
278 gr_flip();
279}
280
281// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700282void* ScreenRecoveryUI::progress_thread(void *cookie) {
283 self->progress_loop();
284 return NULL;
285}
286
287void ScreenRecoveryUI::progress_loop() {
288 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700289 for (;;) {
290 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700291 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700292
293 int redraw = 0;
294
295 // update the installation animation, if active
296 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700297 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
298 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700299 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700300 redraw = 1;
301 }
302
303 // update the progress bar animation, if active
304 // skip this if we have a text overlay (too expensive to update)
Doug Zongker32a0a472011-11-01 11:00:20 -0700305 if (progressBarType == INDETERMINATE && !show_text) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700306 redraw = 1;
307 }
308
309 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700310 int duration = progressScopeDuration;
311 if (progressBarType == DETERMINATE && duration > 0) {
312 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700313 float p = 1.0 * elapsed / duration;
314 if (p > 1.0) p = 1.0;
315 if (p > progress) {
316 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700317 redraw = 1;
318 }
319 }
320
Doug Zongker32a0a472011-11-01 11:00:20 -0700321 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700322
Doug Zongker32a0a472011-11-01 11:00:20 -0700323 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700324 double end = now();
325 // minimum of 20ms delay between frames
326 double delay = interval - (end-start);
327 if (delay < 0.02) delay = 0.02;
328 usleep((long)(delay * 1000000));
329 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700330}
331
332void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
333 int result = res_create_surface(filename, surface);
334 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) {
340 int result = res_create_localized_surface(filename, surface);
341 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
Doug Zongker02ec6b82012-08-22 17:26:40 -0700360 LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]);
361 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700362 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700363 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
364
Doug Zongker211aebc2011-10-28 15:13:10 -0700365 LoadBitmap("progress_empty", &progressBarEmpty);
366 LoadBitmap("progress_fill", &progressBarFill);
367
Doug Zongker02ec6b82012-08-22 17:26:40 -0700368 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
369 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
370 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
371 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
372
Doug Zongker211aebc2011-10-28 15:13:10 -0700373 int i;
374
Doug Zongker32a0a472011-11-01 11:00:20 -0700375 progressBarIndeterminate = (gr_surface*)malloc(indeterminate_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700376 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700377 for (i = 0; i < indeterminate_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700378 char filename[40];
379 // "indeterminate01.png", "indeterminate02.png", ...
380 sprintf(filename, "indeterminate%02d", i+1);
381 LoadBitmap(filename, progressBarIndeterminate+i);
382 }
383
Doug Zongker32a0a472011-11-01 11:00:20 -0700384 if (installing_frames > 0) {
385 installationOverlay = (gr_surface*)malloc(installing_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700386 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700387 for (i = 0; i < installing_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700388 char filename[40];
389 // "icon_installing_overlay01.png",
390 // "icon_installing_overlay02.png", ...
391 sprintf(filename, "icon_installing_overlay%02d", i+1);
392 LoadBitmap(filename, installationOverlay+i);
393 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700394 } else {
395 installationOverlay = NULL;
396 }
397
398 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700399
400 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700401}
402
Doug Zongker5fa8c232012-09-18 12:37:02 -0700403void ScreenRecoveryUI::SetLocale(const char* locale) {
404 if (locale) {
405 char* lang = strdup(locale);
406 for (char* p = lang; *p; ++p) {
407 if (*p == '_') {
408 *p = '\0';
409 break;
410 }
411 }
412
413 // A bit cheesy: keep an explicit list of supported languages
414 // that are RTL.
415 if (strcmp(lang, "ar") == 0 || // Arabic
416 strcmp(lang, "fa") == 0 || // Persian (Farsi)
417 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700418 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
419 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700420 rtl_locale = true;
421 }
422 free(lang);
423 }
424}
425
Doug Zongker211aebc2011-10-28 15:13:10 -0700426void ScreenRecoveryUI::SetBackground(Icon icon)
427{
428 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700429
430 // Adjust the offset to account for the positioning of the
431 // base image on the screen.
432 if (backgroundIcon[icon] != NULL) {
433 gr_surface bg = backgroundIcon[icon];
434 gr_surface text = backgroundText[icon];
435 overlay_offset_x = install_overlay_offset_x + (gr_fb_width() - gr_get_width(bg)) / 2;
436 overlay_offset_y = install_overlay_offset_y +
437 (gr_fb_height() - (gr_get_height(bg) + gr_get_height(text) + 40)) / 2;
438 }
439
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700440 currentIcon = icon;
441 update_screen_locked();
442
Doug Zongker211aebc2011-10-28 15:13:10 -0700443 pthread_mutex_unlock(&updateMutex);
444}
445
446void ScreenRecoveryUI::SetProgressType(ProgressType type)
447{
448 pthread_mutex_lock(&updateMutex);
449 if (progressBarType != type) {
450 progressBarType = type;
451 update_progress_locked();
452 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700453 progressScopeStart = 0;
454 progress = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700455 pthread_mutex_unlock(&updateMutex);
456}
457
458void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
459{
460 pthread_mutex_lock(&updateMutex);
461 progressBarType = DETERMINATE;
462 progressScopeStart += progressScopeSize;
463 progressScopeSize = portion;
464 progressScopeTime = now();
465 progressScopeDuration = seconds;
466 progress = 0;
467 update_progress_locked();
468 pthread_mutex_unlock(&updateMutex);
469}
470
471void ScreenRecoveryUI::SetProgress(float fraction)
472{
473 pthread_mutex_lock(&updateMutex);
474 if (fraction < 0.0) fraction = 0.0;
475 if (fraction > 1.0) fraction = 1.0;
476 if (progressBarType == DETERMINATE && fraction > progress) {
477 // Skip updates that aren't visibly different.
478 int width = gr_get_width(progressBarIndeterminate[0]);
479 float scale = width * progressScopeSize;
480 if ((int) (progress * scale) != (int) (fraction * scale)) {
481 progress = fraction;
482 update_progress_locked();
483 }
484 }
485 pthread_mutex_unlock(&updateMutex);
486}
487
488void ScreenRecoveryUI::Print(const char *fmt, ...)
489{
490 char buf[256];
491 va_list ap;
492 va_start(ap, fmt);
493 vsnprintf(buf, 256, fmt, ap);
494 va_end(ap);
495
496 fputs(buf, stdout);
497
498 // This can get called before ui_init(), so be careful.
499 pthread_mutex_lock(&updateMutex);
500 if (text_rows > 0 && text_cols > 0) {
501 char *ptr;
502 for (ptr = buf; *ptr != '\0'; ++ptr) {
503 if (*ptr == '\n' || text_col >= text_cols) {
504 text[text_row][text_col] = '\0';
505 text_col = 0;
506 text_row = (text_row + 1) % text_rows;
507 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
508 }
509 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
510 }
511 text[text_row][text_col] = '\0';
512 update_screen_locked();
513 }
514 pthread_mutex_unlock(&updateMutex);
515}
516
517void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
518 int initial_selection) {
519 int i;
520 pthread_mutex_lock(&updateMutex);
521 if (text_rows > 0 && text_cols > 0) {
522 for (i = 0; i < text_rows; ++i) {
523 if (headers[i] == NULL) break;
524 strncpy(menu[i], headers[i], text_cols-1);
525 menu[i][text_cols-1] = '\0';
526 }
527 menu_top = i;
528 for (; i < text_rows; ++i) {
529 if (items[i-menu_top] == NULL) break;
530 strncpy(menu[i], items[i-menu_top], text_cols-1);
531 menu[i][text_cols-1] = '\0';
532 }
533 menu_items = i - menu_top;
534 show_menu = 1;
535 menu_sel = initial_selection;
536 update_screen_locked();
537 }
538 pthread_mutex_unlock(&updateMutex);
539}
540
541int ScreenRecoveryUI::SelectMenu(int sel) {
542 int old_sel;
543 pthread_mutex_lock(&updateMutex);
544 if (show_menu > 0) {
545 old_sel = menu_sel;
546 menu_sel = sel;
547 if (menu_sel < 0) menu_sel = 0;
548 if (menu_sel >= menu_items) menu_sel = menu_items-1;
549 sel = menu_sel;
550 if (menu_sel != old_sel) update_screen_locked();
551 }
552 pthread_mutex_unlock(&updateMutex);
553 return sel;
554}
555
556void ScreenRecoveryUI::EndMenu() {
557 int i;
558 pthread_mutex_lock(&updateMutex);
559 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
560 show_menu = 0;
561 update_screen_locked();
562 }
563 pthread_mutex_unlock(&updateMutex);
564}
565
566bool ScreenRecoveryUI::IsTextVisible()
567{
568 pthread_mutex_lock(&updateMutex);
569 int visible = show_text;
570 pthread_mutex_unlock(&updateMutex);
571 return visible;
572}
573
574bool ScreenRecoveryUI::WasTextEverVisible()
575{
576 pthread_mutex_lock(&updateMutex);
577 int ever_visible = show_text_ever;
578 pthread_mutex_unlock(&updateMutex);
579 return ever_visible;
580}
581
582void ScreenRecoveryUI::ShowText(bool visible)
583{
584 pthread_mutex_lock(&updateMutex);
585 show_text = visible;
586 if (show_text) show_text_ever = 1;
587 update_screen_locked();
588 pthread_mutex_unlock(&updateMutex);
589}