blob: 222de00ee838ebdbf7f4f0484b6b3699e4262b03 [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) {
Doug Zongker211aebc2011-10-28 15:13:10 -070085 pthread_mutex_init(&updateMutex, NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -070086 self = this;
87}
88
89// Draw the given frame over the installation overlay animation. The
90// background is not cleared or draw with the base icon first; we
91// assume that the frame already contains some other frame of the
92// animation. Does nothing if no overlay animation is defined.
93// Should only be called with updateMutex locked.
94void ScreenRecoveryUI::draw_install_overlay_locked(int frame) {
Doug Zongker52eeea4f2012-09-04 14:28:25 -070095 if (installationOverlay == NULL || overlay_offset_x < 0) return;
Doug Zongker211aebc2011-10-28 15:13:10 -070096 gr_surface surface = installationOverlay[frame];
97 int iconWidth = gr_get_width(surface);
98 int iconHeight = gr_get_height(surface);
99 gr_blit(surface, 0, 0, iconWidth, iconHeight,
Doug Zongker02ec6b82012-08-22 17:26:40 -0700100 overlay_offset_x, overlay_offset_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700101}
102
103// Clear the screen and draw the currently selected background icon (if any).
104// Should only be called with updateMutex locked.
105void ScreenRecoveryUI::draw_background_locked(Icon icon)
106{
107 pagesIdentical = false;
108 gr_color(0, 0, 0, 255);
109 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
110
111 if (icon) {
112 gr_surface surface = backgroundIcon[icon];
Doug Zongker02ec6b82012-08-22 17:26:40 -0700113 gr_surface text_surface = backgroundText[icon];
114
Doug Zongker211aebc2011-10-28 15:13:10 -0700115 int iconWidth = gr_get_width(surface);
116 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700117 int textWidth = gr_get_width(text_surface);
118 int textHeight = gr_get_height(text_surface);
119
Doug Zongker211aebc2011-10-28 15:13:10 -0700120 int iconX = (gr_fb_width() - iconWidth) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700121 int iconY = (gr_fb_height() - (iconHeight+textHeight+40)) / 2;
122
123 int textX = (gr_fb_width() - textWidth) / 2;
124 int textY = ((gr_fb_height() - (iconHeight+textHeight+40)) / 2) + iconHeight + 40;
125
Doug Zongker211aebc2011-10-28 15:13:10 -0700126 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700127 if (icon == INSTALLING_UPDATE || icon == ERASING) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700128 draw_install_overlay_locked(installingFrame);
129 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700130
131 gr_color(255, 255, 255, 255);
132 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700133 }
134}
135
136// Draw the progress bar (if any) on the screen. Does not flip pages.
137// Should only be called with updateMutex locked.
138void ScreenRecoveryUI::draw_progress_locked()
139{
Doug Zongker69f4b672012-04-26 14:37:53 -0700140 if (currentIcon == ERROR) return;
141
Doug Zongker02ec6b82012-08-22 17:26:40 -0700142 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700143 draw_install_overlay_locked(installingFrame);
144 }
145
146 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700147 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700148 int width = gr_get_width(progressBarEmpty);
149 int height = gr_get_height(progressBarEmpty);
150
151 int dx = (gr_fb_width() - width)/2;
152 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
153
154 // Erase behind the progress bar (in case this was a progress-only update)
155 gr_color(0, 0, 0, 255);
156 gr_fill(dx, dy, width, height);
157
158 if (progressBarType == DETERMINATE) {
159 float p = progressScopeStart + progress * progressScopeSize;
160 int pos = (int) (p * width);
161
Doug Zongker5fa8c232012-09-18 12:37:02 -0700162 if (rtl_locale) {
163 // Fill the progress bar from right to left.
164 if (pos > 0) {
165 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
166 }
167 if (pos < width-1) {
168 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
169 }
170 } else {
171 // Fill the progress bar from left to right.
172 if (pos > 0) {
173 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
174 }
175 if (pos < width-1) {
176 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
177 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700178 }
179 }
180
181 if (progressBarType == INDETERMINATE) {
182 static int frame = 0;
183 gr_blit(progressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700184 // in RTL locales, we run the animation backwards, which
185 // makes the spinner spin the other way.
186 if (rtl_locale) {
187 frame = (frame + indeterminate_frames - 1) % indeterminate_frames;
188 } else {
189 frame = (frame + 1) % indeterminate_frames;
190 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700191 }
192 }
193}
194
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800195#define C_HEADER 247,0,6
196#define C_MENU 0,106,157
197#define C_LOG 249,194,0
Doug Zongker211aebc2011-10-28 15:13:10 -0700198
199// Redraw everything on the screen. Does not flip pages.
200// Should only be called with updateMutex locked.
201void ScreenRecoveryUI::draw_screen_locked()
202{
203 draw_background_locked(currentIcon);
204 draw_progress_locked();
205
206 if (show_text) {
207 gr_color(0, 0, 0, 160);
208 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
209
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800210 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700211 int i = 0;
212 if (show_menu) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800213 gr_color(C_HEADER, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700214
215 for (; i < menu_top + menu_items; ++i) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800216 if (i == menu_top) gr_color(C_MENU, 255);
217
Doug Zongker211aebc2011-10-28 15:13:10 -0700218 if (i == menu_top + menu_sel) {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800219 // draw the highlight bar
220 gr_fill(0, y-2, gr_fb_width(), y+char_height+2);
221 // white text of selected item
Doug Zongker211aebc2011-10-28 15:13:10 -0700222 gr_color(255, 255, 255, 255);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800223 if (menu[i][0]) gr_text(4, y, menu[i], 1);
224 gr_color(C_MENU, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700225 } else {
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800226 if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top);
Doug Zongker211aebc2011-10-28 15:13:10 -0700227 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800228 y += char_height+4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700229 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800230 gr_color(C_MENU, 255);
231 y += 4;
232 gr_fill(0, y, gr_fb_width(), y+2);
233 y += 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700234 ++i;
235 }
236
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800237 gr_color(C_LOG, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700238
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800239 // display from the bottom up, until we hit the top of the
240 // screen, the bottom of the menu, or we've displayed the
241 // entire text buffer.
242 int ty;
243 int row = (text_top+text_rows-1) % text_rows;
244 for (int ty = gr_fb_height() - char_height, count = 0;
245 ty > y+2 && count < text_rows;
246 ty -= char_height, ++count) {
247 gr_text(4, ty, text[row], 0);
248 --row;
249 if (row < 0) row = text_rows-1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700250 }
251 }
252}
253
254// Redraw everything on the screen and flip the screen (make it visible).
255// Should only be called with updateMutex locked.
256void ScreenRecoveryUI::update_screen_locked()
257{
258 draw_screen_locked();
259 gr_flip();
260}
261
262// Updates only the progress bar, if possible, otherwise redraws the screen.
263// Should only be called with updateMutex locked.
264void ScreenRecoveryUI::update_progress_locked()
265{
266 if (show_text || !pagesIdentical) {
267 draw_screen_locked(); // Must redraw the whole screen
268 pagesIdentical = true;
269 } else {
270 draw_progress_locked(); // Draw only the progress bar and overlays
271 }
272 gr_flip();
273}
274
275// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700276void* ScreenRecoveryUI::progress_thread(void *cookie) {
277 self->progress_loop();
278 return NULL;
279}
280
281void ScreenRecoveryUI::progress_loop() {
282 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700283 for (;;) {
284 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700285 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700286
287 int redraw = 0;
288
289 // update the installation animation, if active
290 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700291 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
292 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700293 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700294 redraw = 1;
295 }
296
297 // update the progress bar animation, if active
298 // skip this if we have a text overlay (too expensive to update)
Doug Zongker32a0a472011-11-01 11:00:20 -0700299 if (progressBarType == INDETERMINATE && !show_text) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700300 redraw = 1;
301 }
302
303 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700304 int duration = progressScopeDuration;
305 if (progressBarType == DETERMINATE && duration > 0) {
306 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700307 float p = 1.0 * elapsed / duration;
308 if (p > 1.0) p = 1.0;
309 if (p > progress) {
310 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700311 redraw = 1;
312 }
313 }
314
Doug Zongker32a0a472011-11-01 11:00:20 -0700315 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700316
Doug Zongker32a0a472011-11-01 11:00:20 -0700317 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700318 double end = now();
319 // minimum of 20ms delay between frames
320 double delay = interval - (end-start);
321 if (delay < 0.02) delay = 0.02;
322 usleep((long)(delay * 1000000));
323 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700324}
325
326void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
327 int result = res_create_surface(filename, surface);
328 if (result < 0) {
329 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
330 }
331}
332
Doug Zongker02ec6b82012-08-22 17:26:40 -0700333void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
334 int result = res_create_localized_surface(filename, surface);
335 if (result < 0) {
336 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
337 }
338}
339
Doug Zongker211aebc2011-10-28 15:13:10 -0700340void ScreenRecoveryUI::Init()
341{
342 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700343
Doug Zongker55a36ac2013-03-04 15:49:02 -0800344 gr_font_size(&char_width, &char_height);
345
Doug Zongker211aebc2011-10-28 15:13:10 -0700346 text_col = text_row = 0;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800347 text_rows = gr_fb_height() / char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -0700348 if (text_rows > kMaxRows) text_rows = kMaxRows;
349 text_top = 1;
350
Doug Zongker55a36ac2013-03-04 15:49:02 -0800351 text_cols = gr_fb_width() / char_width;
Doug Zongker211aebc2011-10-28 15:13:10 -0700352 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
353
Doug Zongker02ec6b82012-08-22 17:26:40 -0700354 LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]);
355 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700356 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700357 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
358
Doug Zongker211aebc2011-10-28 15:13:10 -0700359 LoadBitmap("progress_empty", &progressBarEmpty);
360 LoadBitmap("progress_fill", &progressBarFill);
361
Doug Zongker02ec6b82012-08-22 17:26:40 -0700362 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
363 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
364 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
365 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
366
Doug Zongker211aebc2011-10-28 15:13:10 -0700367 int i;
368
Doug Zongker32a0a472011-11-01 11:00:20 -0700369 progressBarIndeterminate = (gr_surface*)malloc(indeterminate_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700370 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700371 for (i = 0; i < indeterminate_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700372 char filename[40];
373 // "indeterminate01.png", "indeterminate02.png", ...
374 sprintf(filename, "indeterminate%02d", i+1);
375 LoadBitmap(filename, progressBarIndeterminate+i);
376 }
377
Doug Zongker32a0a472011-11-01 11:00:20 -0700378 if (installing_frames > 0) {
379 installationOverlay = (gr_surface*)malloc(installing_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700380 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700381 for (i = 0; i < installing_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700382 char filename[40];
383 // "icon_installing_overlay01.png",
384 // "icon_installing_overlay02.png", ...
385 sprintf(filename, "icon_installing_overlay%02d", i+1);
386 LoadBitmap(filename, installationOverlay+i);
387 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700388 } else {
389 installationOverlay = NULL;
390 }
391
392 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700393
394 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700395}
396
Doug Zongker5fa8c232012-09-18 12:37:02 -0700397void ScreenRecoveryUI::SetLocale(const char* locale) {
398 if (locale) {
399 char* lang = strdup(locale);
400 for (char* p = lang; *p; ++p) {
401 if (*p == '_') {
402 *p = '\0';
403 break;
404 }
405 }
406
407 // A bit cheesy: keep an explicit list of supported languages
408 // that are RTL.
409 if (strcmp(lang, "ar") == 0 || // Arabic
410 strcmp(lang, "fa") == 0 || // Persian (Farsi)
411 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700412 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
413 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700414 rtl_locale = true;
415 }
416 free(lang);
417 }
418}
419
Doug Zongker211aebc2011-10-28 15:13:10 -0700420void ScreenRecoveryUI::SetBackground(Icon icon)
421{
422 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700423
424 // Adjust the offset to account for the positioning of the
425 // base image on the screen.
426 if (backgroundIcon[icon] != NULL) {
427 gr_surface bg = backgroundIcon[icon];
428 gr_surface text = backgroundText[icon];
429 overlay_offset_x = install_overlay_offset_x + (gr_fb_width() - gr_get_width(bg)) / 2;
430 overlay_offset_y = install_overlay_offset_y +
431 (gr_fb_height() - (gr_get_height(bg) + gr_get_height(text) + 40)) / 2;
432 }
433
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700434 currentIcon = icon;
435 update_screen_locked();
436
Doug Zongker211aebc2011-10-28 15:13:10 -0700437 pthread_mutex_unlock(&updateMutex);
438}
439
440void ScreenRecoveryUI::SetProgressType(ProgressType type)
441{
442 pthread_mutex_lock(&updateMutex);
443 if (progressBarType != type) {
444 progressBarType = type;
445 update_progress_locked();
446 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700447 progressScopeStart = 0;
448 progress = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700449 pthread_mutex_unlock(&updateMutex);
450}
451
452void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
453{
454 pthread_mutex_lock(&updateMutex);
455 progressBarType = DETERMINATE;
456 progressScopeStart += progressScopeSize;
457 progressScopeSize = portion;
458 progressScopeTime = now();
459 progressScopeDuration = seconds;
460 progress = 0;
461 update_progress_locked();
462 pthread_mutex_unlock(&updateMutex);
463}
464
465void ScreenRecoveryUI::SetProgress(float fraction)
466{
467 pthread_mutex_lock(&updateMutex);
468 if (fraction < 0.0) fraction = 0.0;
469 if (fraction > 1.0) fraction = 1.0;
470 if (progressBarType == DETERMINATE && fraction > progress) {
471 // Skip updates that aren't visibly different.
472 int width = gr_get_width(progressBarIndeterminate[0]);
473 float scale = width * progressScopeSize;
474 if ((int) (progress * scale) != (int) (fraction * scale)) {
475 progress = fraction;
476 update_progress_locked();
477 }
478 }
479 pthread_mutex_unlock(&updateMutex);
480}
481
482void ScreenRecoveryUI::Print(const char *fmt, ...)
483{
484 char buf[256];
485 va_list ap;
486 va_start(ap, fmt);
487 vsnprintf(buf, 256, fmt, ap);
488 va_end(ap);
489
490 fputs(buf, stdout);
491
492 // This can get called before ui_init(), so be careful.
493 pthread_mutex_lock(&updateMutex);
494 if (text_rows > 0 && text_cols > 0) {
495 char *ptr;
496 for (ptr = buf; *ptr != '\0'; ++ptr) {
497 if (*ptr == '\n' || text_col >= text_cols) {
498 text[text_row][text_col] = '\0';
499 text_col = 0;
500 text_row = (text_row + 1) % text_rows;
501 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
502 }
503 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
504 }
505 text[text_row][text_col] = '\0';
506 update_screen_locked();
507 }
508 pthread_mutex_unlock(&updateMutex);
509}
510
511void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
512 int initial_selection) {
513 int i;
514 pthread_mutex_lock(&updateMutex);
515 if (text_rows > 0 && text_cols > 0) {
516 for (i = 0; i < text_rows; ++i) {
517 if (headers[i] == NULL) break;
518 strncpy(menu[i], headers[i], text_cols-1);
519 menu[i][text_cols-1] = '\0';
520 }
521 menu_top = i;
522 for (; i < text_rows; ++i) {
523 if (items[i-menu_top] == NULL) break;
524 strncpy(menu[i], items[i-menu_top], text_cols-1);
525 menu[i][text_cols-1] = '\0';
526 }
527 menu_items = i - menu_top;
528 show_menu = 1;
529 menu_sel = initial_selection;
530 update_screen_locked();
531 }
532 pthread_mutex_unlock(&updateMutex);
533}
534
535int ScreenRecoveryUI::SelectMenu(int sel) {
536 int old_sel;
537 pthread_mutex_lock(&updateMutex);
538 if (show_menu > 0) {
539 old_sel = menu_sel;
540 menu_sel = sel;
541 if (menu_sel < 0) menu_sel = 0;
542 if (menu_sel >= menu_items) menu_sel = menu_items-1;
543 sel = menu_sel;
544 if (menu_sel != old_sel) update_screen_locked();
545 }
546 pthread_mutex_unlock(&updateMutex);
547 return sel;
548}
549
550void ScreenRecoveryUI::EndMenu() {
551 int i;
552 pthread_mutex_lock(&updateMutex);
553 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
554 show_menu = 0;
555 update_screen_locked();
556 }
557 pthread_mutex_unlock(&updateMutex);
558}
559
560bool ScreenRecoveryUI::IsTextVisible()
561{
562 pthread_mutex_lock(&updateMutex);
563 int visible = show_text;
564 pthread_mutex_unlock(&updateMutex);
565 return visible;
566}
567
568bool ScreenRecoveryUI::WasTextEverVisible()
569{
570 pthread_mutex_lock(&updateMutex);
571 int ever_visible = show_text_ever;
572 pthread_mutex_unlock(&updateMutex);
573 return ever_visible;
574}
575
576void ScreenRecoveryUI::ShowText(bool visible)
577{
578 pthread_mutex_lock(&updateMutex);
579 show_text = visible;
580 if (show_text) show_text_ever = 1;
581 update_screen_locked();
582 pthread_mutex_unlock(&updateMutex);
583}