xref: /OK3568_Linux_fs/external/rk_pcba_test/pcba_minui/ui.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 #include "recovery_ui.h"
31 
32 #define MAX_COLS 96
33 #define MAX_ROWS 32
34 
35 #define CHAR_WIDTH 10
36 #define CHAR_HEIGHT 18
37 
38 extern int g_key_test ;
39 
40 
41 #define PROGRESSBAR_INDETERMINATE_STATES 6
42 #define PROGRESSBAR_INDETERMINATE_FPS 15
43 
44 static pthread_mutex_t gUpdateMutex = PTHREAD_MUTEX_INITIALIZER;
45 static gr_surface gBackgroundIcon[NUM_BACKGROUND_ICONS];
46 static gr_surface gProgressBarIndeterminate[PROGRESSBAR_INDETERMINATE_STATES];
47 static gr_surface gProgressBarEmpty;
48 static gr_surface gProgressBarFill;
49 
50 static const struct { gr_surface* surface; const char *name; } BITMAPS[] = {
51     { &gBackgroundIcon[BACKGROUND_ICON_INSTALLING], "icon_installing" },
52     { &gBackgroundIcon[BACKGROUND_ICON_ERROR],      "icon_error" },
53     { &gProgressBarIndeterminate[0],    "indeterminate1" },
54     { &gProgressBarIndeterminate[1],    "indeterminate2" },
55     { &gProgressBarIndeterminate[2],    "indeterminate3" },
56     { &gProgressBarIndeterminate[3],    "indeterminate4" },
57     { &gProgressBarIndeterminate[4],    "indeterminate5" },
58     { &gProgressBarIndeterminate[5],    "indeterminate6" },
59     { &gProgressBarEmpty,               "progress_empty" },
60     { &gProgressBarFill,                "progress_fill" },
61     { NULL,                             NULL },
62 };
63 
64 static gr_surface gCurrentIcon = NULL;
65 
66 static enum ProgressBarType {
67     PROGRESSBAR_TYPE_NONE,
68     PROGRESSBAR_TYPE_INDETERMINATE,
69     PROGRESSBAR_TYPE_NORMAL,
70 } gProgressBarType = PROGRESSBAR_TYPE_NONE;
71 
72 // Progress bar scope of current operation
73 static float gProgressScopeStart = 0, gProgressScopeSize = 0, gProgress = 0;
74 static time_t gProgressScopeTime, gProgressScopeDuration;
75 
76 // Set to 1 when both graphics pages are the same (except for the progress bar)
77 static int gPagesIdentical = 0;
78 
79 // Log text overlay, displayed when a magic key is pressed
80 static char text[MAX_ROWS][MAX_COLS];
81 static int text_cols = 0, text_rows = 0;
82 static int text_col = 0, text_row = 0, text_top = 0;
83 static int show_text = 1;
84 
85 static char menu[MAX_ROWS][MAX_COLS];
86 static int show_menu = 0;
87 static int menu_top = 0, menu_items = 0, menu_sel = 0;
88 
89 // Key event input queue
90 static pthread_mutex_t key_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
91 static pthread_cond_t key_queue_cond = PTHREAD_COND_INITIALIZER;
92 static int key_queue[256], key_queue_len = 0;
93 static volatile char key_pressed[KEY_MAX + 1];
94 
95 
96 //-----------------------------------------------------------------------------
97 //#define _EVENT_LOGGING 0
98 
99 static int touch_tp_state = 0;
100 
101 typedef struct{
102 	int t_col;
103 	int t_row;
104 	int r;
105 	int g;
106 	int b;
107 	int a;
108 } textInfo;
109 
110 textInfo itemsInfo[MAX_ROWS];
111 
112 typedef struct{
113 	   int left;
114 	   int top;
115 	   int right;
116 	   int bottom;
117 	   int r;
118 	   int g;
119 	   int b;
120 	   int a;
121 } FillColorTile;
122 FillColorTile  tiles[kMaxTiles];
123 int tiles_count = 0;
124 
125 typedef struct{
126 	   int x0;
127 	   int y0;
128 	   int x1;
129 	   int y1;
130 	   int linewidth;
131 	   int r;
132 	   int g;
133 	   int b;
134 	   int a;
135 } LineInfo;
136 LineInfo  lines[kMaxTiles];
137 int lines_count = 0;
138 
139 
140 typedef enum {
141 	TOUCH_START = 0,
142 	TOUCH_DRAG = 1,
143 	TOUCH_RELEASE = 2,
144 	TOUCH_HOLD = 3,
145 	TOUCH_REPEAT = 4
146 } TOUCH_STATE;
147 
148 static int gUiInitialized = 0;
149 
150 extern int NotifyTouch(int action, int x, int y);
151 
152 //-----------------------------------------------------------------------------
153 
154 
155 
156 // Clear the screen and draw the currently selected background icon (if any).
157 // Should only be called with gUpdateMutex locked.
draw_background_locked(gr_surface icon)158 static void draw_background_locked(gr_surface icon)
159 {
160     gPagesIdentical = 0;
161     gr_color(0, 0, 0, 255);
162     gr_fill(0, 0, gr_fb_width(), gr_fb_height());
163 
164     if (icon) {
165         int iconWidth = gr_get_width(icon);
166         int iconHeight = gr_get_height(icon);
167         int iconX = (gr_fb_width() - iconWidth) / 2;
168         int iconY = (gr_fb_height() - iconHeight) / 2;
169         gr_blit(icon, 0, 0, iconWidth, iconHeight, iconX, iconY);
170     }
171 }
172 
173 // Draw the progress bar (if any) on the screen.  Does not flip pages.
174 // Should only be called with gUpdateMutex locked.
draw_progress_locked()175 static void draw_progress_locked()
176 {
177     if (gProgressBarType == PROGRESSBAR_TYPE_NONE) return;
178 
179     int iconHeight = gr_get_height(gBackgroundIcon[BACKGROUND_ICON_INSTALLING]);
180     int width = gr_get_width(gProgressBarEmpty);
181     int height = gr_get_height(gProgressBarEmpty);
182 
183     int dx = (gr_fb_width() - width)/2;
184     int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
185 
186     // Erase behind the progress bar (in case this was a progress-only update)
187     gr_color(0, 0, 0, 255);
188     gr_fill(dx, dy, width, height);
189 
190     if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL) {
191         float progress = gProgressScopeStart + gProgress * gProgressScopeSize;
192         int pos = (int) (progress * width);
193 
194         if (pos > 0) {
195           gr_blit(gProgressBarFill, 0, 0, pos, height, dx, dy);
196         }
197         if (pos < width-1) {
198           gr_blit(gProgressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
199         }
200     }
201 
202     if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE) {
203         static int frame = 0;
204         gr_blit(gProgressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
205         frame = (frame + 1) % PROGRESSBAR_INDETERMINATE_STATES;
206     }
207 }
208 
draw_text_line(int left,int top,const char * t)209 static void  draw_text_line(int left, int top, const char* t) {
210   if (t[0] != '\0') {
211     gr_text(left,top , t,0);
212   }
213 }
214 
215 
216 // Redraw everything on the screen.  Does not flip pages.
217 // Should only be called with gUpdateMutex locked.
draw_screen_locked(void)218 static void draw_screen_locked(void)
219 {
220     draw_background_locked(gCurrentIcon);
221     draw_progress_locked();
222 
223     if (show_text)
224 	{
225 		gr_color(0, 0, 0, 160);
226 		gr_fill(0, 0, gr_fb_width(), gr_fb_height());
227 
228 		int k = 0;
229 		for (k = 0; k < text_row; ++k) {
230 			gr_color(itemsInfo[k].r, itemsInfo[k].g, itemsInfo[k].b,
231 				itemsInfo[k].a);
232 			draw_text_line((itemsInfo[k].t_col + 1)*CHAR_WIDTH + 1,
233 				(itemsInfo[k].t_row)*CHAR_HEIGHT, text[k]);
234 		}
235 
236 		for(k = 0; k < lines_count;k++){
237         	gr_color(lines[k].r, lines[k].g, lines[k].b, lines[k].a);
238         	//gr_line(lines[k].x0, lines[k].y0, lines[k].x1, lines[k].y1,lines[k].linewidth);
239 		}
240 
241 		for(k = 0; k < tiles_count;k++){
242 			gr_color(tiles[k].r, tiles[k].g, tiles[k].b, tiles[k].a);
243 			gr_fill(tiles[k].left, tiles[k].top, tiles[k].right, tiles[k].bottom);
244 		}
245 	}
246 }
247 
248 // Redraw everything on the screen and flip the screen (make it visible).
249 // Should only be called with gUpdateMutex locked.
update_screen_locked(void)250 static void update_screen_locked(void)
251 {
252     draw_screen_locked();
253     gr_flip();
254 }
255 
256 // Updates only the progress bar, if possible, otherwise redraws the screen.
257 // Should only be called with gUpdateMutex locked.
update_progress_locked(void)258 static void update_progress_locked(void)
259 {
260     printf("%s in >>>>>> \n",__func__);
261     if (show_text || !gPagesIdentical) {
262         draw_screen_locked();    // Must redraw the whole screen
263         gPagesIdentical = 1;
264     } else {
265         draw_progress_locked();  // Draw only the progress bar
266     }
267     gr_flip();
268 }
269 
270 // Keeps the progress bar updated, even when the process is otherwise busy.
progress_thread(void * cookie)271 static void *progress_thread(void *cookie)
272 {
273     printf("%s in >>>>>> \n",__func__);
274     for (;;) {
275         usleep(1000000 / PROGRESSBAR_INDETERMINATE_FPS);
276         pthread_mutex_lock(&gUpdateMutex);
277 
278         // update the progress bar animation, if active
279         // skip this if we have a text overlay (too expensive to update)
280         if (gProgressBarType == PROGRESSBAR_TYPE_INDETERMINATE && !show_text) {
281             update_progress_locked();
282         }
283 
284         // move the progress bar forward on timed intervals, if configured
285         int duration = gProgressScopeDuration;
286         if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && duration > 0) {
287             int elapsed = time(NULL) - gProgressScopeTime;
288             float progress = 1.0 * elapsed / duration;
289             if (progress > 1.0) progress = 1.0;
290             if (progress > gProgress) {
291                 gProgress = progress;
292                 update_progress_locked();
293             }
294         }
295 
296         pthread_mutex_unlock(&gUpdateMutex);
297     }
298     return NULL;
299 }
300 
301 static int rel_sum = 0;
302 
input_callback(int fd,short revents,void * data)303 static int input_callback(int fd, short revents, void *data)
304 {
305     struct input_event ev;
306     int ret;
307     int fake_key = 0;
308 
309     ret = ev_get_input(fd, revents, &ev);
310     if (ret)
311         return -1;
312 
313     if (ev.type == EV_SYN) {
314         return 0;
315     } else if (ev.type == EV_REL) {
316         if (ev.code == REL_Y) {
317             // accumulate the up or down motion reported by
318             // the trackball.  When it exceeds a threshold
319             // (positive or negative), fake an up/down
320             // key event.
321             rel_sum += ev.value;
322             if (rel_sum > 3) {
323                 fake_key = 1;
324                 ev.type = EV_KEY;
325                 ev.code = KEY_DOWN;
326                 ev.value = 1;
327                 rel_sum = 0;
328             } else if (rel_sum < -3) {
329                 fake_key = 1;
330                 ev.type = EV_KEY;
331                 ev.code = KEY_UP;
332                 ev.value = 1;
333                 rel_sum = 0;
334             }
335         }
336     } else {
337         rel_sum = 0;
338     }
339 
340     if (ev.type != EV_KEY || ev.code > KEY_MAX)
341         return 0;
342 
343     pthread_mutex_lock(&key_queue_mutex);
344     if (!fake_key) {
345         // our "fake" keys only report a key-down event (no
346         // key-up), so don't record them in the key_pressed
347         // table.
348         key_pressed[ev.code] = ev.value;
349     }
350     const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
351     if (ev.value > 0 && key_queue_len < queue_max) {
352         key_queue[key_queue_len++] = ev.code;
353         pthread_cond_signal(&key_queue_cond);
354     }
355     pthread_mutex_unlock(&key_queue_mutex);
356 
357     if (ev.value > 0 && device_toggle_display(key_pressed, ev.code)) {
358         pthread_mutex_lock(&gUpdateMutex);
359         show_text = !show_text;
360         update_screen_locked();
361         pthread_mutex_unlock(&gUpdateMutex);
362     }
363 
364     if (ev.value > 0 && device_reboot_now(key_pressed, ev.code)) {
365             reboot(RB_AUTOBOOT);
366     }
367 
368     return 0;
369 }
370 
371 
372 // Reads input events, handles special hot keys, and adds to the key queue.
input_thread(void * cookie)373 static void *input_thread(void *cookie)
374 {
375     printf("%s in >>>>>> \n",__func__);
376 
377         int drag = 0;
378         static int touch_and_hold = 0, dontwait = 0, touch_repeat = 0, x = 0, y = 0, lshift = 0, rshift = 0, key_repeat = 0;
379         static struct timeval touchStart;
380         static struct timeval keyStart, keyEnd;
381         //LOGE("start input thread!\n");
382         printf("start input thread! \n");
383 
384         memset(&touchStart, 0, sizeof(struct timeval));
385         memset(&keyStart, 0, sizeof(struct timeval));
386         memset(&keyEnd, 0, sizeof(struct timeval));
387 
388         for (;;)
389         {
390             // wait for the next event
391             struct input_event ev;
392             int state = 0, ret = 0;
393 
394             ret = ev_get(&ev, dontwait);
395             //LOGE("input_thread::type:%d>>code:%d>>value:%d>>EV_ABS:%d>>EV_KEY:%d\n",ev.type,ev.code,ev.value,EV_ABS,EV_KEY);
396             if (ret < 0)
397             {
398                 struct timeval curTime;
399                 gettimeofday(&curTime, NULL);
400                 long mtime, seconds, useconds;
401 
402                 seconds  = curTime.tv_sec  - touchStart.tv_sec;
403                 useconds = curTime.tv_usec - touchStart.tv_usec;
404 
405                 mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
406                 if (touch_and_hold && mtime > 500)
407                 {
408                     touch_and_hold = 0;
409                     touch_repeat = 1;
410                     gettimeofday(&touchStart, NULL);
411 #ifdef _EVENT_LOGGING
412                     LOGE("TOUCH_HOLD: %d,%d\n", x, y);
413 #endif
414                     NotifyTouch(TOUCH_HOLD, x, y);
415                 }
416                 else if (touch_repeat && mtime > 100)
417                 {
418 #ifdef _EVENT_LOGGING
419                     LOGE("TOUCH_REPEAT: %d,%d\n", x, y);
420 #endif
421                     gettimeofday(&touchStart, NULL);
422                     NotifyTouch(TOUCH_REPEAT, x, y);
423                 }
424                 else if (key_repeat == 1 && mtime > 500)
425                 {
426 #ifdef _EVENT_LOGGING
427                     LOGE("KEY_HOLD: %d,%d\n", x, y);
428 #endif
429                     gettimeofday(&touchStart, NULL);
430                     key_repeat = 2;
431                 }
432                 else if (key_repeat == 2 && mtime > 100)
433                 {
434 #ifdef _EVENT_LOGGING
435                     LOGE("KEY_REPEAT: %d,%d\n", x, y);
436 #endif
437                     gettimeofday(&touchStart, NULL);
438                 }
439             }
440             else if (ev.type == EV_ABS)
441             {
442                 if (ev.value==-1)
443                 {
444                     continue;
445                 }
446 
447                 x = ev.value >> 16;
448                 y = ev.value & 0xFFFF;
449 
450                 if (ev.code == 0)
451                 {
452                     if (state == 0)
453                     {
454 #ifdef _EVENT_LOGGING
455                         LOGE("TOUCH_RELEASE: %d,%d\n", x, y);
456 #endif
457                         touch_tp_state = 0;
458 
459 #if 0 // chad.ma close
460                         start_manual_test_item(x,y);
461 #endif
462                         NotifyTouch(TOUCH_RELEASE, x, y);
463                         touch_and_hold = 0;
464                         touch_repeat = 0;
465                         if (!key_repeat)
466                             dontwait = 0;
467                     }
468                     state = 0;
469                     drag = 0;
470                 }
471                 else
472                 {
473                     if (!drag)
474                     {
475 #ifdef _EVENT_LOGGING
476                         LOGE("TOUCH_START: %d,%d\n", x, y);
477 #endif
478                         NotifyTouch(TOUCH_START, x, y);
479                         state = 1;
480                         drag = 1;
481                         touch_and_hold = 1;
482                         dontwait = 1;
483                         key_repeat = 0;
484                         gettimeofday(&touchStart, NULL);
485                     }
486                     else
487                     {
488                         if (state == 0)
489                         {
490                             touch_tp_state = 1;
491 #ifdef _EVENT_LOGGING
492                             LOGE("TOUCH_DRAG: %d,%d\n", x, y);
493 #endif
494                             NotifyTouch(TOUCH_DRAG, x, y);
495                             state = 1;
496                             key_repeat = 0;
497                         }
498                     }
499                 }
500             }
501             else if (ev.type == EV_KEY)
502             {
503                 // Handle key-press here
504 			#ifdef _EVENT_LOGGING
505                     LOGE("TOUCH_KEY: %d\n", ev.code);
506 			#endif
507 
508                 if (ev.code==330)
509                 {
510                     continue;
511                 }
512 
513                 if (ev.value != 0 && ev.code != 143)
514                 {
515                     gettimeofday(&keyStart, NULL);
516                     printf("key down:%lu\n",keyStart.tv_sec);
517 
518                     key_repeat = 0;
519                     touch_and_hold = 0;
520                     touch_repeat = 0;
521                     dontwait = 0;
522                 }
523                 else if (ev.code != 143)
524                 {
525                     // This is a key release
526                     gettimeofday(&keyEnd, NULL);
527 #if 1   //chad.ma close
528                     printf("key hold time:%lu, g_key_test=%d\n",keyEnd.tv_sec-keyStart.tv_sec, g_key_test);
529                     if(g_key_test)
530                         set_gkey(ev.code);
531 #endif
532                     key_repeat = 0;
533                     touch_and_hold = 0;
534                     touch_repeat = 0;
535                     dontwait = 0;
536                     if((keyEnd.tv_sec-keyStart.tv_sec) >= 5)
537                     {
538                         break;
539                     }
540                 }
541             }
542         //printf("tp touch : state : %d\r\n",state);
543         //touch_tp_state = state;
544         }
545 
546     return NULL;
547 }
548 
start_input_thread(void)549 void start_input_thread(void)
550 {
551     pthread_t t;
552     pthread_create(&t, NULL, input_thread, NULL);
553 	pthread_join(t,NULL);
554 }
555 
ui_init(void)556 void ui_init(void)
557 {
558     printf("%s in >>>>>> \n",__func__);
559 
560     gr_init();
561     ev_init();
562 
563     text_col = text_row = 0;
564     text_rows = gr_fb_height() / CHAR_HEIGHT;
565     if (text_rows > MAX_ROWS) text_rows = MAX_ROWS;
566     text_top = 1;
567     text_cols = gr_fb_width() / CHAR_WIDTH;
568     if (text_cols > MAX_COLS - 1) text_cols = MAX_COLS - 1;
569 
570     pthread_t t;
571     pthread_create(&t, NULL, progress_thread, NULL);
572     pthread_create(&t, NULL, input_thread, NULL);
573 
574     gUiInitialized = 1;
575 }
576 
ui_set_background(int icon)577 void ui_set_background(int icon)
578 {
579     pthread_mutex_lock(&gUpdateMutex);
580     gCurrentIcon = gBackgroundIcon[icon];
581     update_screen_locked();
582     pthread_mutex_unlock(&gUpdateMutex);
583 }
584 
ui_show_indeterminate_progress()585 void ui_show_indeterminate_progress()
586 {
587     pthread_mutex_lock(&gUpdateMutex);
588     if (gProgressBarType != PROGRESSBAR_TYPE_INDETERMINATE) {
589         gProgressBarType = PROGRESSBAR_TYPE_INDETERMINATE;
590         update_progress_locked();
591     }
592     pthread_mutex_unlock(&gUpdateMutex);
593 }
594 
ui_show_progress(float portion,int seconds)595 void ui_show_progress(float portion, int seconds)
596 {
597     pthread_mutex_lock(&gUpdateMutex);
598     gProgressBarType = PROGRESSBAR_TYPE_NORMAL;
599     gProgressScopeStart += gProgressScopeSize;
600     gProgressScopeSize = portion;
601     gProgressScopeTime = time(NULL);
602     gProgressScopeDuration = seconds;
603     gProgress = 0;
604     update_progress_locked();
605     pthread_mutex_unlock(&gUpdateMutex);
606 }
607 
ui_set_progress(float fraction)608 void ui_set_progress(float fraction)
609 {
610     pthread_mutex_lock(&gUpdateMutex);
611     if (fraction < 0.0) fraction = 0.0;
612     if (fraction > 1.0) fraction = 1.0;
613     if (gProgressBarType == PROGRESSBAR_TYPE_NORMAL && fraction > gProgress) {
614         // Skip updates that aren't visibly different.
615         int width = gr_get_width(gProgressBarIndeterminate[0]);
616         float scale = width * gProgressScopeSize;
617         if ((int) (gProgress * scale) != (int) (fraction * scale)) {
618             gProgress = fraction;
619             update_progress_locked();
620         }
621     }
622     pthread_mutex_unlock(&gUpdateMutex);
623 }
624 
ui_reset_progress()625 void ui_reset_progress()
626 {
627     pthread_mutex_lock(&gUpdateMutex);
628     gProgressBarType = PROGRESSBAR_TYPE_NONE;
629     gProgressScopeStart = gProgressScopeSize = 0;
630     gProgressScopeTime = gProgressScopeDuration = 0;
631     gProgress = 0;
632     update_screen_locked();
633     pthread_mutex_unlock(&gUpdateMutex);
634 }
635 
ui_print(const char * fmt,...)636 void ui_print(const char *fmt, ...)
637 {
638     char buf[256];
639     va_list ap;
640     va_start(ap, fmt);
641     vsnprintf(buf, 256, fmt, ap);
642     va_end(ap);
643 
644     fputs(buf, stdout);
645 
646     // This can get called before ui_init(), so be careful.
647     pthread_mutex_lock(&gUpdateMutex);
648 printf("vvvvv %s in\n",__func__);
649     if (text_rows > 0 && text_cols > 0) {
650         char *ptr;
651         for (ptr = buf; *ptr != '\0'; ++ptr) {
652             if (*ptr == '\n' || text_col >= text_cols) {
653                 text[text_row][text_col] = '\0';
654                 text_col = 0;
655                 text_row = (text_row + 1) % text_rows;
656                 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
657             }
658             if (*ptr != '\n') text[text_row][text_col++] = *ptr;
659         }
660         text[text_row][text_col] = '\0';
661         update_screen_locked();
662     }
663 printf("vvvvv %s out\n",__func__);
664     pthread_mutex_unlock(&gUpdateMutex);
665 }
666 
ui_start_menu(char ** headers,char ** items,int initial_selection)667 void ui_start_menu(char** headers, char** items, int initial_selection) {
668     int i;
669     pthread_mutex_lock(&gUpdateMutex);
670     if (text_rows > 0 && text_cols > 0) {
671         for (i = 0; i < text_rows; ++i) {
672             if (headers[i] == NULL) break;
673             strncpy(menu[i], headers[i], text_cols-1);
674             menu[i][text_cols-1] = '\0';
675         }
676         menu_top = i;
677         for (; i < text_rows; ++i) {
678             if (items[i-menu_top] == NULL) break;
679             strncpy(menu[i], items[i-menu_top], text_cols-1);
680             menu[i][text_cols-1] = '\0';
681         }
682         menu_items = i - menu_top;
683         show_menu = 1;
684         menu_sel = initial_selection;
685         update_screen_locked();
686     }
687     pthread_mutex_unlock(&gUpdateMutex);
688 }
689 
ui_menu_select(int sel)690 int ui_menu_select(int sel) {
691     int old_sel;
692     pthread_mutex_lock(&gUpdateMutex);
693     if (show_menu > 0) {
694         old_sel = menu_sel;
695         menu_sel = sel;
696     #if 0
697         if (menu_sel < 0) menu_sel = 0;
698         if (menu_sel >= menu_items) menu_sel = menu_items-1;
699     #else
700         if (menu_sel < 0) {
701             menu_sel = menu_items-1;//�����ʾ�����ǰ��Ϊ�����������Ϊ�����л�ʱ���л���β��
702         } else if (menu_sel >= menu_items) {
703             menu_sel = 0;//�����ʾ�����ǰ��Ϊβ���������Ϊ�����л�ʱ���л�������
704         }
705     #endif
706         sel = menu_sel;
707         if (menu_sel != old_sel) update_screen_locked();
708     }
709     pthread_mutex_unlock(&gUpdateMutex);
710     return sel;
711 }
712 
ui_end_menu()713 void ui_end_menu() {
714     int i;
715     pthread_mutex_lock(&gUpdateMutex);
716     if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
717         show_menu = 0;
718         update_screen_locked();
719     }
720     pthread_mutex_unlock(&gUpdateMutex);
721 }
722 
ui_text_visible()723 int ui_text_visible()
724 {
725     pthread_mutex_lock(&gUpdateMutex);
726     int visible = show_text;
727     pthread_mutex_unlock(&gUpdateMutex);
728     return visible;
729 }
730 
ui_show_text(int visible)731 void ui_show_text(int visible)
732 {
733     pthread_mutex_lock(&gUpdateMutex);
734     show_text = visible;
735     update_screen_locked();
736     pthread_mutex_unlock(&gUpdateMutex);
737 }
738 
ui_wait_key()739 int ui_wait_key()
740 {
741     pthread_mutex_lock(&key_queue_mutex);
742     while (key_queue_len == 0) {
743         pthread_cond_wait(&key_queue_cond, &key_queue_mutex);
744     }
745 
746     int key = key_queue[0];
747     memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
748     pthread_mutex_unlock(&key_queue_mutex);
749     return key;
750 }
751 
ui_key_pressed(int key)752 int ui_key_pressed(int key)
753 {
754     // This is a volatile static array, don't bother locking
755     return key_pressed[key];
756 }
757 
ui_clear_key_queue()758 void ui_clear_key_queue() {
759     pthread_mutex_lock(&key_queue_mutex);
760     key_queue_len = 0;
761     pthread_mutex_unlock(&key_queue_mutex);
762 }
763 
ui_print_init(void)764 void ui_print_init(void)  //add by yxj
765 {
766 	int i = 0;
767 
768 	text_col = text_row = 0;
769 	text_rows = gr_fb_height() / CHAR_HEIGHT;
770 	if (text_rows > MAX_ROWS)
771 		text_rows = MAX_ROWS;
772 	text_top = 1;
773     text_cols = gr_fb_width() / CHAR_WIDTH;
774 	if (text_cols > MAX_COLS - 1)
775 		text_cols = MAX_COLS - 1;
776 
777 	//set_theme("0"); //set r g b a
778 
779 	LOGI("text_col:%d>>text_row:%d>>text_cols:%d>>text_rows:%d\n",text_col,
780 		text_row,text_cols,text_rows);
781 
782 	for(i=0 ;i< text_rows;i++)
783 	{
784 		itemsInfo[i].t_row = i;
785 	}
786 
787 	gUiInitialized = 1;
788 }
789 
drawline(int r,int g,int b,int a,int x0,int y0,int x1,int y1,int linewidth)790 void drawline(int r,int g,int b,int a,int x0,int y0,int x1,int y1,int linewidth)
791 {
792 	lines_count += 1;
793 	if(lines_count > kMaxTiles)
794 	{
795 		lines_count = kMaxTiles;
796 	}
797 	lines[lines_count-1].x0 = x0;
798 	lines[lines_count-1].y0 = y0;
799 	lines[lines_count-1].x1 = x1;
800 	lines[lines_count-1].y1 = y1;
801 	lines[lines_count-1].linewidth = linewidth;
802 	lines[lines_count-1].r = r;
803 	lines[lines_count-1].g = g;
804 	lines[lines_count-1].b = b;
805 	lines[lines_count-1].a = a;
806 }
drawline_4(int r,int g,int b,int a,int left,int top,int width,int height,int linewidth)807 void drawline_4(int r,int g,int b,int a,int left,int top,int width,int height,int linewidth)
808 {
809 	drawline(r,g,b,a,left,top,left+width,top,linewidth);
810 	drawline(r,g,b,a,left+width,top,left+width,top+height,linewidth);
811 	drawline(r,g,b,a,left+width,top+height,left,top+height,linewidth);
812 	drawline(r,g,b,a,left,top+height,left,top,linewidth);
813 	pthread_mutex_lock(&gUpdateMutex);
814 	update_screen_locked();
815 	pthread_mutex_unlock(&gUpdateMutex);
816 }
817 
ui_print_xy_rgba(int t_col,int t_row,int r,int g,int b,int a,const char * fmt,...)818 void ui_print_xy_rgba(int t_col,int t_row,int r,int g,int b,int a,const char* fmt, ...)
819 {
820 	char buf[512];
821 	va_list ap;
822 	va_start(ap, fmt);
823 	vsnprintf(buf, 512, fmt, ap);
824 	va_end(ap);
825 
826 	/*fputs(buf, stdout);*/
827 
828     /*This can get called before ui_init(), so be careful.*/
829     int temp_row = t_row;
830     pthread_mutex_lock(&gUpdateMutex);
831     t_col+=2;
832 
833 	/*1080P screen : text_rows is:18 , text_cols is: 64*/
834     if (text_rows > 0 && text_cols > 0)
835     {
836 		char *ptr;
837 		for (ptr = buf; *ptr != '\0'; ++ptr)
838 		{
839 		    /*if (*ptr == '\n' || text_col >= text_cols)*/
840 		    if (*ptr == '\n' || text_col >= text_cols*2) {
841 				text[temp_row][text_col] = '\0';
842 				text_col = 0;
843 
844 				if (temp_row < text_rows) {
845 					itemsInfo[temp_row].t_col = t_col;
846 					itemsInfo[temp_row].t_row = temp_row;
847 
848 				} else {
849 					itemsInfo[temp_row].t_col =
850 						t_col+text_cols/2;
851 					itemsInfo[temp_row].t_row =
852 						(temp_row+2)%text_rows;
853 				}
854 				itemsInfo[temp_row].r = r;
855 				itemsInfo[temp_row].g = g;
856 				itemsInfo[temp_row].b = b;
857 				itemsInfo[temp_row].a = a;
858 
859 				temp_row = temp_row + 1;
860 				if (temp_row >= (2*text_rows))
861 					temp_row = 0;
862 
863 				if (temp_row > text_row)
864 					text_row = temp_row;
865 		    }
866 		    if (*ptr != '\n') {
867 				text[temp_row][text_col++] = *ptr;
868 		    }
869 		}
870 	        text[text_row][text_col] = '\0';
871 	        update_screen_locked();
872     }
873 
874     pthread_mutex_unlock(&gUpdateMutex);
875 }
876 
ui_print_xy_rgba_multi(struct display_info * info,int count)877 void ui_print_xy_rgba_multi(struct display_info *info, int count)
878 {
879 	int i = 0;
880 
881 	/*This can get called before ui_init(), so be careful.*/
882 	pthread_mutex_lock(&gUpdateMutex);
883 	if (text_rows > 0 && text_cols > 0)	{
884 		for (i = 0; i < count; i++) {
885 			int temp_row = info[i].row;
886 			int t_col = info[i].col+2;
887 			char *buf = info[i].string;
888 			char *ptr;
889 
890 			for (ptr = buf; *ptr != '\0'; ++ptr) {
891 				if (*ptr == '\n' || text_col >= text_cols*2) {
892 					text[temp_row][text_col] = '\0';
893 					text_col = 0;
894 					if (temp_row < text_rows) {
895 						itemsInfo[temp_row].t_col =
896 							t_col;
897 						itemsInfo[temp_row].t_row =
898 							temp_row;
899 					} else {
900 						itemsInfo[temp_row].t_col =
901 							t_col+text_cols/2;
902 						itemsInfo[temp_row].t_row =
903 							(temp_row+2)%text_rows;
904 					}
905 					itemsInfo[temp_row].r = info[i].r;
906 					itemsInfo[temp_row].g = info[i].g;
907 					itemsInfo[temp_row].b = info[i].b;
908 					itemsInfo[temp_row].a = info[i].a;
909 
910 					temp_row = temp_row + 1;
911 					if (temp_row >= (2*text_rows))
912 						temp_row = 0;
913 					if (temp_row > text_row)
914 						text_row = temp_row;
915 				}
916 			    if (*ptr != '\n')
917 					text[temp_row][text_col++] = *ptr;
918 			}
919 			/*text[text_row][text_col] = '\0';*/
920 		}
921 	    update_screen_locked();
922 	}
923 	pthread_mutex_unlock(&gUpdateMutex);
924 }
925 
ui_display_sync(int t_col,int t_row,int r,int g,int b,int a,const char * fmt,...)926 void ui_display_sync(int t_col,int t_row,int r,int g,int b,int a,const char* fmt, ...)
927 {
928 	char buf[512];
929 	va_list ap;
930 
931 	if(touch_tp_state == 0)
932 	{
933 		va_start(ap, fmt);
934 		vsnprintf(buf, 512, fmt, ap);
935 		va_end(ap);
936 		ui_print_xy_rgba(t_col,t_row,r,g,b,a,buf);
937 	}
938 }
939 
FillColor(int r,int g,int b,int a,int left,int top,int width,int height)940 void FillColor(int r,int g,int b,int a,int left,int top,int width,int height)
941 {
942 	tiles_count += 1;
943 	if(tiles_count > kMaxTiles)
944 	{
945 		tiles_count = kMaxTiles;
946 	}
947 	tiles[tiles_count-1].left = left;
948 	tiles[tiles_count-1].top = top;
949 	tiles[tiles_count-1].right = width+left;
950 	tiles[tiles_count-1].bottom = height+top;
951 	tiles[tiles_count-1].r = r;
952 	tiles[tiles_count-1].g = g;
953 	tiles[tiles_count-1].b = b;
954 	tiles[tiles_count-1].a = a;
955 	pthread_mutex_lock(&gUpdateMutex);
956 	update_screen_locked();
957 	pthread_mutex_unlock(&gUpdateMutex);
958 }
959 
960 
961