1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <pthread.h>
5 #include <unistd.h>
6
7 #include <linux/input.h>
8 #include <fcntl.h>
9 #include <dirent.h>
10 #include "common.h"
11 #include "key_test.h"
12 #include "test_case.h"
13 #include "language.h"
14 #define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
15
16 #include "screen_test.h"
17
18 unsigned char keybitmask[(KEY_MAX + 1) / 8];
19 struct key key_code[KEY_MAX];
20 unsigned char key_cnt = 0; /*key counter */
21 unsigned int gkey = 0;
22 static pthread_mutex_t gkeymutex = PTHREAD_MUTEX_INITIALIZER;
23
24 int g_key_test = 0;
25 static struct testcase_info *tc_info;
26
set_gkey(unsigned int code)27 int set_gkey(unsigned int code)
28 {
29 int i;
30 for (i = 0; i < key_cnt; i++) {
31 if (code == key_code[i].code) {
32 ui_print_xy_rgba(0, tc_info->y, 0, 255, 0, 255,
33 "%s:[%s] key down\n", PCBA_KEY,
34 key_code[i].name);
35 screenTest_key_detect(code);
36 break;
37 }
38 }
39 return 0;
40 }
41
scan_key_code(void)42 int scan_key_code(void)
43 {
44 unsigned temp = 0;
45
46 pthread_mutex_lock(&gkeymutex);
47 if (gkey) {
48 temp = gkey;
49 gkey = 0;
50 }
51 pthread_mutex_unlock(&gkeymutex);
52
53 return temp;
54 }
switch_key(int i)55 void switch_key(int i)
56 {
57 key_code[key_cnt].code = i;
58 switch (i) {
59 case KEY_VOLUMEDOWN:
60 key_code[key_cnt].name = "vol-";
61 break;
62 case KEY_VOLUMEUP:
63 key_code[key_cnt].name = "vol+";
64 break;
65 case KEY_POWER:
66 key_code[key_cnt].name = "power";
67 break;
68 case KEY_F1:
69 key_code[key_cnt].name = "menu";
70 #ifdef RK312X_PCBA
71 key_code[key_cnt].name = "vol+";
72 #endif
73 break;
74 case KEY_HOME:
75 key_code[key_cnt].name = "home";
76 break;
77 case KEY_BACK:
78 key_code[key_cnt].name = "ESC";
79 break;
80 case KEY_UP:
81 key_code[key_cnt].name = "UP";
82 break;
83 case KEY_DOWN:
84 key_code[key_cnt].name = "DOWN";
85 break;
86 case KEY_LEFT:
87 key_code[key_cnt].name = "LEFT";
88 break;
89 case KEY_RIGHT:
90 key_code[key_cnt].name = "RIGHT";
91 break;
92 case KEY_MENU:
93 key_code[key_cnt].name = "MENU";
94 break;
95 case KEY_ENTER:
96 case KEY_REPLY:
97 key_code[key_cnt].name = "OK";
98 break;
99 default:
100 printf("un supported key:%d\n", i);
101 break;
102 }
103 key_cnt++;
104 }
key_code_probe(void)105 int key_code_probe(void)
106 {
107 DIR *dir;
108 struct dirent *de;
109 int fd;
110 char name[80];
111 int i;
112
113 dir = opendir("/dev/input");
114 if (dir != 0) {
115 while ((de = readdir(dir))) {
116 if (strncmp(de->d_name, "event", 5))
117 continue;
118 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
119 if (fd < 0) {
120 continue;
121 } else {
122 if (ioctl
123 (fd, EVIOCGNAME(sizeof(name) - 1),
124 &name) < 1) {
125 name[0] = '\0';
126 }
127 /*do not open gsensor here */
128 if (!strcmp(name, "gsensor"))
129 continue;
130 ioctl(fd,
131 EVIOCGBIT(EV_KEY, sizeof(keybitmask)),
132 keybitmask);
133 for (i = 0; i < KEY_MAX; i++) {
134 if (test_bit(i, keybitmask) &&
135 (i != KEY_WAKEUP))
136 switch_key(i);
137 }
138 }
139 }
140 }
141
142 return 0;
143 }
key_test(void * argc)144 void *key_test(void *argc)
145 {
146 int i = 0;
147 int code;
148 int run = 1;
149
150 tc_info = (struct testcase_info *)argc;
151
152 if (tc_info->y <= 0)
153 tc_info->y = get_cur_print_y();
154
155 ui_print_xy_rgba(0, tc_info->y, 255, 255, 0, 255, "%s: Waiting press key...\n", PCBA_KEY);
156 key_code_probe();
157 g_key_test = 1;
158
159 return NULL;
160 }
161