blob: 190e4a60ada9a387257735e0fc1b5554be51ba53 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <fcntl.h>
5#include <unistd.h>
6#include <dirent.h>
7#include <poll.h>
8#include <errno.h>
9#include <linux/input.h>
10#include <sys/stat.h>
11#include <sys/reboot.h>
12#include <sys/timerfd.h>
13#include <time.h>
14
15#include "mbtk_type.h"
16#include "mbtk_log.h"
17
18
19#define MAX_DEVICES 16
20#define EVIOCSSUSPENDBLOCK _IOW('E', 0x91, int)
21#define POWER_KEY_LONG_PRESS_TIMEOUT 8000 // 8s
22#define POWER_KEY_PRESS_TIMEOUT 2000 // 2s
23
24static struct pollfd ev_fds[MAX_DEVICES];
25static unsigned ev_count = 0;
26
27
28static int ev_init(void)
29{
30 DIR *dir;
31 struct dirent *de;
32 int fd;
33
34 fd = open("/dev/rtc0", O_RDONLY);
35 if (fd < 0)
36 {
37 LOGW("open rtc0 error\n");
38 }
39 else
40 {
41 ev_fds[ev_count].fd = fd;
42 ev_fds[ev_count].events = POLLIN;
43 ev_count++;
44 LOGI("Monitor /dev/rtc0");
45 }
46
47 dir = opendir("/dev/input");
48 if (dir != NULL)
49 {
50 LOGI("dir = /dev/input");
51 while ((de = readdir(dir)) != NULL)
52 {
53 if (strncmp(de->d_name, "event", 5))
54 continue;
55 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
56 if (fd < 0)
57 continue;
58
59 ev_fds[ev_count].fd = fd;
60 ev_fds[ev_count].events = POLLIN;
61 ioctl(fd, EVIOCSSUSPENDBLOCK, 1);
62 ev_count++;
63 if (ev_count == MAX_DEVICES)
64 break;
65
66 LOGI("Monitor /dev/input/%s", de->d_name);
67 }
68
69 closedir(dir);
70 }
71 else
72 {
73 LOGE("opendir() fail.[%d]",errno);
74 return -1;
75 }
76 return 0;
77}
78
79static void ev_exit(void)
80{
81 while (ev_count > 0)
82 {
83 close(ev_fds[--ev_count].fd);
84 }
85}
86
87/* wait: 0 dont wait; -1 wait forever; >0 wait ms */
88static int ev_get(struct input_event *ev, int wait_ms)
89{
90 int r;
91 unsigned n;
92 unsigned long alarm_data;
93
94// if(wait_ms < 0)
95// {
96// LOGE("poll event return\n");
97// return -1;
98// }
99
100 LOGI("Waitting data...");
101 r = poll(ev_fds, ev_count, wait_ms);
102 LOGI("Get Data:result = %d",r);
103
104 if (r > 0)
105 {
106 for (n = 0; n < ev_count; n++)
107 {
108 if (ev_fds[n].revents & POLLIN)
109 {
110 if (n == 0)
111 {
112 r = read(ev_fds[n].fd, &alarm_data, sizeof(alarm_data));
113 LOGD("get form 0 is %ld", alarm_data);
114 ev->type = EV_KEY;
115 ev->code = KEY_BRL_DOT8;
116 ev->value = 1;
117 return 0;
118 }
119 else
120 {
121 r = read(ev_fds[n].fd, ev, sizeof(*ev));
122 if (r == sizeof(*ev))
123 return 0;
124 }
125 }
126 }
127 }
128 return -1;
129}
130
131
132static void power_process(bool down, struct timeval *time)
133{
134 printf("POWER_KEY - %s,Time : %ld %ld \n", down ? "DOWN" : "UP", time->tv_sec, time->tv_usec);
135#if 0
136 static struct timeval down_time;
137 static bool key_down;
138 if(down) // Down
139 {
140 key_down = true;
141 power_long_press_timeout = false;
142 down_time.tv_sec = time->tv_sec;
143 down_time.tv_usec = time->tv_usec;
144 signal(SIGALRM, power_key_timer_alrm);
145 struct itimerval val;
146 // Only time
147 val.it_interval.tv_sec = 0;
148 val.it_interval.tv_usec = 0;
149 // Time
150 if(POWER_KEY_LONG_PRESS_TIMEOUT >= 1000)
151 {
152 val.it_value.tv_sec = POWER_KEY_LONG_PRESS_TIMEOUT/1000;
153 val.it_value.tv_usec = POWER_KEY_LONG_PRESS_TIMEOUT%1000;
154 }
155 else
156 {
157 val.it_value.tv_sec = 0;
158 val.it_value.tv_usec = POWER_KEY_LONG_PRESS_TIMEOUT;
159 }
160 if (setitimer(ITIMER_REAL, &val, NULL) == -1)
161 {
162 LOGE("setitimer fail.[%d]",errno);
163 return;
164 }
165 }
166 else // UP
167 {
168 if(key_down)
169 {
170 // ms
171 long time_used = (time->tv_sec - down_time.tv_sec) * 1000 + (time->tv_usec - down_time.tv_usec) / 1000;
172 LOGI("Down time[%ld,%ld], Up time[%ld,%ld], time_used = %ld ms",down_time.tv_sec,down_time.tv_usec,
173 time->tv_sec,time->tv_usec,time_used);
174
175 if(!power_long_press_timeout)
176 {
177 // Cancel alarm
178 struct itimerval value;
179 value.it_value.tv_sec = 0;
180 value.it_value.tv_usec = 0;
181 value.it_interval = value.it_value;
182 setitimer(ITIMER_REAL, &value, NULL);
183
184 if(time_used <= POWER_KEY_PRESS_TIMEOUT)
185 {
186 screen_state_change(false);
187 }
188 else
189 {
190 LOGI("Press timeout.");
191 }
192 }
193 else
194 {
195 LOGI("Long Press timeout.");
196 }
197 }
198 else
199 {
200 LOGI("UP key for screen on.");
201 }
202
203 key_down = false;
204 }
205#endif
206}
207
208
209int main(int argc, char *argv[])
210{
211 mbtk_log_init(NULL, "MBTK_KEY");
212 // Open dev
213 ev_init();
214
215 int ret = 0;
216 struct input_event ev;
217 while(1)
218 {
219 ret = ev_get(&ev, -1);
220 if(ret)
221 {
222 LOGW("ev_get() fail.");
223 continue;
224 }
225
226 LOGI("ev:time[%ld,%ld],type:%d,code:%d,value:%d",ev.time.tv_sec,ev.time.tv_usec,
227 ev.type,ev.code,ev.value);
228 if(ev.type != EV_KEY)
229 {
230 LOGW("event type error.[%d]",ev.type);
231 continue;
232 }
233
234 switch(ev.code)
235 {
236 case KEY_POWER: // Power key
237 {
238 power_process(ev.value, &(ev.time));
239 break;
240 }
241 default:
242 {
243 LOGD("Unknown KEY[%d]",ev.code);
244 break;
245 }
246 }
247 }
248 return 0;
249}