• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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 <linux/input.h>
18 #include <pthread.h>
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/reboot.h>
24 #include <sys/time.h>
25 #include <time.h>
26 #include <unistd.h>
27 
28 #include "common.h"
29 #include "minui/minui.h"
30 
31 #define MAX_COLS 64
32 #define MAX_ROWS 32
33 
34 #define CHAR_WIDTH 10
35 #define CHAR_HEIGHT 18
36 
37 #define PROGRESSBAR_INDETERMINATE_STATES 6
38 #define PROGRESSBAR_INDETERMINATE_FPS 15
39 
40 enum { LEFT_SIDE, CENTER_TILE, RIGHT_SIDE, NUM_SIDES };
41 
42 static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER;
43 static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS];
44 static gr_surface gProgressBarIndeterminate[PROGRESSBAR_INDETERMINATE_STATES];
45 static gr_surface gProgressBarEmpty[NUM_SIDES];
46 static gr_surface gProgressBarFill[NUM_SIDES];
47 
48 static const struct { gr_surface* surface; const char *name; } BITMAPS[] = {
49     { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" },
50     { &gBackgroundIcon[BACKGROUND_ICON_ERROR],      "icon_error" },
51     { &gBackgroundIcon[BACKGROUND_ICON_FIRMWARE_INSTALLING],
52         "icon_firmware_install" },
53     { &gBackgroundIcon[BACKGROUND_ICON_FIRMWARE_ERROR],
54         "icon_firmware_error" },
55     { &gProgressBarIndeterminate[0],    "indeterminate1" },
56     { &gProgressBarIndeterminate[1],    "indeterminate2" },
57     { &gProgressBarIndeterminate[2],    "indeterminate3" },
58     { &gProgressBarIndeterminate[3],    "indeterminate4" },
59     { &gProgressBarIndeterminate[4],    "indeterminate5" },
60     { &gProgressBarIndeterminate[5],    "indeterminate6" },
61     { &gProgressBarEmpty[LEFT_SIDE],    "progress_bar_empty_left_round" },
62     { &gProgressBarEmpty[CENTER_TILE],  "progress_bar_empty" },
63     { &gProgressBarEmpty[RIGHT_SIDE],   "progress_bar_empty_right_round" },
64     { &gProgressBarFill[LEFT_SIDE],     "progress_bar_left_round" },
65     { &gProgressBarFill[CENTER_TILE],   "progress_bar_fill" },
66     { &gProgressBarFill[RIGHT_SIDE],    "progress_bar_right_round" },
67     { NULL,                             NULL },
68 };
69 
70 static gr_surface gCurrentIcon = NULL;
71 
72 static enum ProgressBarType {
73     PROGRESSBAR_TYPE_NONE,
74     PROGRESSBAR_TYPE_INDETERMINATE,
75     PROGRESSBAR_TYPE_NORMAL,
76 } gProgressBarType = PROGRESSBAR_TYPE_NONE;
77 
78 // Progress bar scope of current operation
79 static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
80 static time_t gProgressScopeTime, gProgressScopeDuration;
81 
82 // Set to 1 when both graphics pages are the same (except for the progress bar)
83 static int gPagesIdentical = 0;
84 
85 // Log text overlay, displayed when a magic key is pressed
86 static char text[MAX_ROWS][MAX_COLS];
87 static int text_cols = 0, text_rows = 0;
88 static int text_col = 0, text_row = 0, text_top = 0;
89 static int show_text = 0;
90 
91 static char menu[MAX_ROWS][MAX_COLS];
92 static int show_menu = 0;
93 static int menu_top = 0, menu_items = 0, menu_sel = 0;
94 
95 // Key event input queue
96 static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
97 static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER;
98 static int key_queue[256], key_queue_len = 0;
99 static volatile char key_pressed[KEY_MAX + 1];
100 
101 // Clear the screen and draw the currently selected background icon (if any).
102 // Should only be called with gUpdateMutex locked.
draw_background_locked(gr_surface icon)103 static void draw_background_locked(gr_surface icon)
104 {
105     gPagesIdentical = 0;
106     gr_color(0, 0, 0, 255);
107     gr_fill(0, 0, gr_fb_width(), gr_fb_height());
108 
109     if (icon) {
110         int iconWidth = gr_get_width(icon);
111         int iconHeight = gr_get_height(icon);
112         int iconX = (gr_fb_width() - iconWidth) / 2;
113         int iconY = (gr_fb_height() - iconHeight) / 2;
114         gr_blit(icon, 0, 0, iconWidth, iconHeight, iconX, iconY);
115     }
116 }
117 
118 // Draw the progress bar (if any) on the screen.  Does not flip pages.
119 // Should only be called with gUpdateMutex locked.
draw_progress_locked()120 static void draw_progress_locked()
121 {
122     if (gProgressBarType == PROGRESSBAR_TYPE_NONE) return;
123 
124     int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]);
125     int width = gr_get_width(gProgressBarIndeterminate[0]);
126     int height = gr_get_height(gProgressBarIndeterminate[0]);
127 
128     int dx = (gr_fb_width() - width)/2;
129     int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
130 
131     // Erase behind the progress bar (in case this was a progress-only update)
132     gr_color(0, 0, 0, 255);
133     gr_fill(dx, dy, width, height);
134 
135     if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) {
136         float progress = gProgressScopeStart + gProgress * gProgressScopeSize;
137         int pos = (int) (progress * width);
138 
139         gr_surface s = (pos ? gProgressBarFill : gProgressBarEmpty)[LEFT_SIDE];
140         gr_blit(s, 0, 0, gr_get_width(s), gr_get_height(s), dx, dy);
141 
142         int x = gr_get_width(s);
143         while (x + (int) gr_get_width(gProgressBarEmpty[RIGHT_SIDE]) < width) {
144             s = (pos > x ? gProgressBarFill : gProgressBarEmpty)[CENTER_TILE];
145             gr_blit(s, 0, 0, gr_get_width(s), gr_get_height(s), dx + x, dy);
146             x += gr_get_width(s);
147         }
148 
149         s = (pos > x ? gProgressBarFill : gProgressBarEmpty)[RIGHT_SIDE];
150         gr_blit(s, 0, 0, gr_get_width(s), gr_get_height(s), dx + x, dy);
151     }
152 
153     if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) {
154         static int frame = 0;
155         gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
156         frame = (frame + 1) % PROGRESSBAR_INDETERMINATE_STATES;
157     }
158 }
159 
draw_text_line(int row,const char * t)160 static void draw_text_line(int row, const char* t) {
161   if (t[0] != '\0') {
162     gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
163   }
164 }
165 
166 // Redraw everything on the screen.  Does not flip pages.
167 // Should only be called with gUpdateMutex locked.
draw_screen_locked(void)168 static void draw_screen_locked(void)
169 {
170     draw_background_locked(gCurrentIcon);
171     draw_progress_locked();
172 
173     if (show_text) {
174         gr_color(0, 0, 0, 160);
175         gr_fill(0, 0, gr_fb_width(), gr_fb_height());
176 
177         int i = 0;
178         if (show_menu) {
179             gr_color(64, 96, 255, 255);
180             gr_fill(0, (menu_top+menu_sel) * CHAR_HEIGHT,
181                     gr_fb_width(), (menu_top+menu_sel+1)*CHAR_HEIGHT+1);
182 
183             for (; i < menu_top + menu_items; ++i) {
184                 if (i == menu_top + menu_sel) {
185                     gr_color(255, 255, 255, 255);
186                     draw_text_line(i, menu[i]);
187                     gr_color(64, 96, 255, 255);
188                 } else {
189                     draw_text_line(i, menu[i]);
190                 }
191             }
192             gr_fill(0, i*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
193                     gr_fb_width(), i*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
194             ++i;
195         }
196 
197         gr_color(255, 255, 0, 255);
198 
199         for (; i < text_rows; ++i) {
200             draw_text_line(i, text[(i+text_top) % text_rows]);
201         }
202     }
203 }
204 
205 // Redraw everything on the screen and flip the screen (make it visible).
206 // Should only be called with gUpdateMutex locked.
update_screen_locked(void)207 static void update_screen_locked(void)
208 {
209     draw_screen_locked();
210     gr_flip();
211 }
212 
213 // Updates only the progress bar, if possible, otherwise redraws the screen.
214 // Should only be called with gUpdateMutex locked.
update_progress_locked(void)215 static void update_progress_locked(void)
216 {
217     if (show_text || !gPagesIdentical) {
218         draw_screen_locked();    // Must redraw the whole screen
219         gPagesIdentical = 1;
220     } else {
221         draw_progress_locked();  // Draw only the progress bar
222     }
223     gr_flip();
224 }
225 
226 // Keeps the progress bar updated, even when the process is otherwise busy.
progress_thread(void * cookie)227 static void *progress_thread(void *cookie)
228 {
229     for (;;) {
230         usleep(1000000 / PROGRESSBAR_INDETERMINATE_FPS);
231         pthread_mutex_lock(&gUpdateMutex);
232 
233         // update the progress bar animation, if active
234         // skip this if we have a text overlay (too expensive to update)
235         if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) {
236             update_progress_locked();
237         }
238 
239         // move the progress bar forward on timed intervals, if configured
240         int duration = gProgressScopeDuration;
241         if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) {
242             int elapsed = time(NULL) - gProgressScopeTime;
243             float progress = 1.0 * elapsed / duration;
244             if (progress > 1.0) progress = 1.0;
245             if (progress > gProgress) {
246                 gProgress = progress;
247                 update_progress_locked();
248             }
249         }
250 
251         pthread_mutex_unlock(&gUpdateMutex);
252     }
253     return NULL;
254 }
255 
256 // Reads input events, handles special hot keys, and adds to the key queue.
input_thread(void * cookie)257 static void *input_thread(void *cookie)
258 {
259     int rel_sum = 0;
260     int fake_key = 0;
261     for (;;) {
262         // wait for the next key event
263         struct input_event ev;
264         do {
265             ev_get(&ev, 0);
266 
267             if (ev.type == EV_SYN) {
268                 continue;
269             } else if (ev.type == EV_REL) {
270                 if (ev.code == REL_Y) {
271                     // accumulate the up or down motion reported by
272                     // the trackball.  When it exceeds a threshold
273                     // (positive or negative), fake an up/down
274                     // key event.
275                     rel_sum += ev.value;
276                     if (rel_sum > 3) {
277                         fake_key = 1;
278                         ev.type = EV_KEY;
279                         ev.code = KEY_DOWN;
280                         ev.value = 1;
281                         rel_sum = 0;
282                     } else if (rel_sum < -3) {
283                         fake_key = 1;
284                         ev.type = EV_KEY;
285                         ev.code = KEY_UP;
286                         ev.value = 1;
287                         rel_sum = 0;
288                     }
289                 }
290             } else {
291                 rel_sum = 0;
292             }
293         } while (ev.type != EV_KEY || ev.code > KEY_MAX);
294 
295         pthread_mutex_lock(&key_queue_mutex);
296         if (!fake_key) {
297             // our "fake" keys only report a key-down event (no
298             // key-up), so don't record them in the key_pressed
299             // table.
300             key_pressed[ev.code] = ev.value;
301         }
302         fake_key = 0;
303         const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
304         if (ev.value > 0 && key_queue_len < queue_max) {
305             key_queue[key_queue_len++] = ev.code;
306             pthread_cond_signal(&key_queue_cond);
307         }
308         pthread_mutex_unlock(&key_queue_mutex);
309 
310         // Alt+L or Home+End: toggle log display
311         int alt = key_pressed[KEY_LEFTALT] || key_pressed[KEY_RIGHTALT];
312         if ((alt && ev.code == KEY_L && ev.value > 0) ||
313             (key_pressed[KEY_HOME] && ev.code == KEY_END && ev.value > 0)) {
314             pthread_mutex_lock(&gUpdateMutex);
315             show_text = !show_text;
316             update_screen_locked();
317             pthread_mutex_unlock(&gUpdateMutex);
318         }
319 
320         // Green+Menu+Red: reboot immediately
321         if (ev.code == KEY_DREAM_RED &&
322             key_pressed[KEY_DREAM_MENU] &&
323             key_pressed[KEY_DREAM_GREEN]) {
324             reboot(RB_AUTOBOOT);
325         }
326     }
327     return NULL;
328 }
329 
ui_init(void)330 void ui_init(void)
331 {
332     gr_init();
333     ev_init();
334 
335     text_col = text_row = 0;
336     text_rows = gr_fb_height() / CHAR_HEIGHT;
337     if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
338     text_top = 1;
339 
340     text_cols = gr_fb_width() / CHAR_WIDTH;
341     if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
342 
343     int i;
344     for (i = 0; BITMAPS[i].name != NULL; ++i) {
345         int result = res_create_surface(BITMAPS[i].name, BITMAPS[i].surface);
346         if (result < 0) {
347             LOGE("Missing bitmap %s\n(Code %d)\n", BITMAPS[i].name, result);
348             *BITMAPS[i].surface = NULL;
349         }
350     }
351 
352     pthread_t t;
353     pthread_create(&t, NULL, progress_thread, NULL);
354     pthread_create(&t, NULL, input_thread, NULL);
355 }
356 
ui_copy_image(int icon,int * width,int * height,int * bpp)357 char *ui_copy_image(int icon, int *width, int *height, int *bpp) {
358     pthread_mutex_lock(&gUpdateMutex);
359     draw_background_locked(gBackgroundIcon[icon]);
360     *width = gr_fb_width();
361     *height = gr_fb_height();
362     *bpp = sizeof(gr_pixel) * 8;
363     int size = *width * *height * sizeof(gr_pixel);
364     char *ret = malloc(size);
365     if (ret == NULL) {
366         LOGE("Can't allocate %d bytes for image\n", size);
367     } else {
368         memcpy(ret, gr_fb_data(), size);
369     }
370     pthread_mutex_unlock(&gUpdateMutex);
371     return ret;
372 }
373 
ui_set_background(int icon)374 void ui_set_background(int icon)
375 {
376     pthread_mutex_lock(&gUpdateMutex);
377     gCurrentIcon = gBackgroundIcon[icon];
378     update_screen_locked();
379     pthread_mutex_unlock(&gUpdateMutex);
380 }
381 
ui_show_indeterminate_progress()382 void ui_show_indeterminate_progress()
383 {
384     pthread_mutex_lock(&gUpdateMutex);
385     if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
386         gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
387         update_progress_locked();
388     }
389     pthread_mutex_unlock(&gUpdateMutex);
390 }
391 
ui_show_progress(float portion,int seconds)392 void ui_show_progress(float portion, int seconds)
393 {
394     pthread_mutex_lock(&gUpdateMutex);
395     gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
396     gProgressScopeStart += gProgressScopeSize;
397     gProgressScopeSize = portion;
398     gProgressScopeTime = time(NULL);
399     gProgressScopeDuration = seconds;
400     gProgress = 0;
401     update_progress_locked();
402     pthread_mutex_unlock(&gUpdateMutex);
403 }
404 
ui_set_progress(float fraction)405 void ui_set_progress(float fraction)
406 {
407     pthread_mutex_lock(&gUpdateMutex);
408     if (fraction < 0.0) fraction = 0.0;
409     if (fraction > 1.0) fraction = 1.0;
410     if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
411         // Skip updates that aren't visibly different.
412         int width = gr_get_width(gProgressBarIndeterminate[0]);
413         float scale = width * gProgressScopeSize;
414         if ((int) (gProgress * scale) != (int) (fraction * scale)) {
415             gProgress = fraction;
416             update_progress_locked();
417         }
418     }
419     pthread_mutex_unlock(&gUpdateMutex);
420 }
421 
ui_reset_progress()422 void ui_reset_progress()
423 {
424     pthread_mutex_lock(&gUpdateMutex);
425     gProgressBarType = PROGRESSBAR_TYPE_NONE;
426     gProgressScopeStart = gProgressScopeSize = 0;
427     gProgressScopeTime = gProgressScopeDuration = 0;
428     gProgress = 0;
429     update_screen_locked();
430     pthread_mutex_unlock(&gUpdateMutex);
431 }
432 
ui_print(const char * fmt,...)433 void ui_print(const char *fmt, ...)
434 {
435     char buf[256];
436     va_list ap;
437     va_start(ap, fmt);
438     vsnprintf(buf, 256, fmt, ap);
439     va_end(ap);
440 
441     fputs(buf, stderr);
442 
443     // This can get called before ui_init(), so be careful.
444     pthread_mutex_lock(&gUpdateMutex);
445     if (text_rows > 0 && text_cols > 0) {
446         char *ptr;
447         for (ptr = buf; *ptr != '\0'; ++ptr) {
448             if (*ptr == '\n' || text_col >= text_cols) {
449                 text[text_row][text_col] = '\0';
450                 text_col = 0;
451                 text_row = (text_row + 1) % text_rows;
452                 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
453             }
454             if (*ptr != '\n') text[text_row][text_col++] = *ptr;
455         }
456         text[text_row][text_col] = '\0';
457         update_screen_locked();
458     }
459     pthread_mutex_unlock(&gUpdateMutex);
460 }
461 
ui_start_menu(char ** headers,char ** items)462 void ui_start_menu(char** headers, char** items) {
463     int i;
464     pthread_mutex_lock(&gUpdateMutex);
465     if (text_rows > 0 && text_cols > 0) {
466         for (i = 0; i < text_rows; ++i) {
467             if (headers[i] == NULL) break;
468             strncpy(menu[i], headers[i], text_cols-1);
469             menu[i][text_cols-1] = '\0';
470         }
471         menu_top = i;
472         for (; i < text_rows; ++i) {
473             if (items[i-menu_top] == NULL) break;
474             strncpy(menu[i], items[i-menu_top], text_cols-1);
475             menu[i][text_cols-1] = '\0';
476         }
477         menu_items = i - menu_top;
478         show_menu = 1;
479         menu_sel = 0;
480         update_screen_locked();
481     }
482     pthread_mutex_unlock(&gUpdateMutex);
483 }
484 
ui_menu_select(int sel)485 int ui_menu_select(int sel) {
486     int old_sel;
487     pthread_mutex_lock(&gUpdateMutex);
488     if (show_menu > 0) {
489         old_sel = menu_sel;
490         menu_sel = sel;
491         if (menu_sel < 0) menu_sel = 0;
492         if (menu_sel >= menu_items) menu_sel = menu_items-1;
493         sel = menu_sel;
494         if (menu_sel != old_sel) update_screen_locked();
495     }
496     pthread_mutex_unlock(&gUpdateMutex);
497     return sel;
498 }
499 
ui_end_menu()500 void ui_end_menu() {
501     int i;
502     pthread_mutex_lock(&gUpdateMutex);
503     if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
504         show_menu = 0;
505         update_screen_locked();
506     }
507     pthread_mutex_unlock(&gUpdateMutex);
508 }
509 
ui_text_visible()510 int ui_text_visible()
511 {
512     pthread_mutex_lock(&gUpdateMutex);
513     int visible = show_text;
514     pthread_mutex_unlock(&gUpdateMutex);
515     return visible;
516 }
517 
ui_wait_key()518 int ui_wait_key()
519 {
520     pthread_mutex_lock(&key_queue_mutex);
521     while (key_queue_len == 0) {
522         pthread_cond_wait(&key_queue_cond, &key_queue_mutex);
523     }
524 
525     int key = key_queue[0];
526     memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
527     pthread_mutex_unlock(&key_queue_mutex);
528     return key;
529 }
530 
ui_key_pressed(int key)531 int ui_key_pressed(int key)
532 {
533     // This is a volatile static array, don't bother locking
534     return key_pressed[key];
535 }
536 
ui_clear_key_queue()537 void ui_clear_key_queue() {
538     pthread_mutex_lock(&key_queue_mutex);
539     key_queue_len = 0;
540     pthread_mutex_unlock(&key_queue_mutex);
541 }
542