blob: 728b5d0d043e330c85e2dedea30759869c25fee4 [file] [log] [blame]
b.liub17525e2025-05-14 17:22:29 +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
79#if 0
80static void ev_exit(void)
81{
82 while (ev_count > 0)
83 {
84 close(ev_fds[--ev_count].fd);
85 }
86}
87#endif
88
89/* wait: 0 dont wait; -1 wait forever; >0 wait ms */
90static int ev_get(struct input_event *ev, int wait_ms)
91{
92 int r;
93 unsigned n;
94 unsigned long alarm_data;
95
96// if(wait_ms < 0)
97// {
98// LOGE("poll event return\n");
99// return -1;
100// }
101
102 LOGI("Waitting data...");
103 r = poll(ev_fds, ev_count, wait_ms);
104 LOGI("Get Data:result = %d",r);
105
106 if (r > 0)
107 {
108 for (n = 0; n < ev_count; n++)
109 {
110 if (ev_fds[n].revents & POLLIN)
111 {
112 if (n == 0)
113 {
114 r = read(ev_fds[n].fd, &alarm_data, sizeof(alarm_data));
115 LOGD("get form 0 is %ld", alarm_data);
116 ev->type = EV_KEY;
117 ev->code = KEY_BRL_DOT8;
118 ev->value = 1;
119 return 0;
120 }
121 else
122 {
123 r = read(ev_fds[n].fd, ev, sizeof(*ev));
124 if (r == sizeof(*ev))
125 return 0;
126 }
127 }
128 }
129 }
130 return -1;
131}
132
133
134static void power_process(bool down, struct timeval *time)
135{
136 printf("POWER_KEY - %s,Time : %ld %ld \n", down ? "DOWN" : "UP", time->tv_sec, time->tv_usec);
137#if 0
138 static struct timeval down_time;
139 static bool key_down;
140 if(down) // Down
141 {
142 key_down = true;
143 power_long_press_timeout = false;
144 down_time.tv_sec = time->tv_sec;
145 down_time.tv_usec = time->tv_usec;
146 signal(SIGALRM, power_key_timer_alrm);
147 struct itimerval val;
148 // Only time
149 val.it_interval.tv_sec = 0;
150 val.it_interval.tv_usec = 0;
151 // Time
152 if(POWER_KEY_LONG_PRESS_TIMEOUT >= 1000)
153 {
154 val.it_value.tv_sec = POWER_KEY_LONG_PRESS_TIMEOUT/1000;
155 val.it_value.tv_usec = POWER_KEY_LONG_PRESS_TIMEOUT%1000;
156 }
157 else
158 {
159 val.it_value.tv_sec = 0;
160 val.it_value.tv_usec = POWER_KEY_LONG_PRESS_TIMEOUT;
161 }
162 if (setitimer(ITIMER_REAL, &val, NULL) == -1)
163 {
164 LOGE("setitimer fail.[%d]",errno);
165 return;
166 }
167 }
168 else // UP
169 {
170 if(key_down)
171 {
172 // ms
173 long time_used = (time->tv_sec - down_time.tv_sec) * 1000 + (time->tv_usec - down_time.tv_usec) / 1000;
174 LOGI("Down time[%ld,%ld], Up time[%ld,%ld], time_used = %ld ms",down_time.tv_sec,down_time.tv_usec,
175 time->tv_sec,time->tv_usec,time_used);
176
177 if(!power_long_press_timeout)
178 {
179 // Cancel alarm
180 struct itimerval value;
181 value.it_value.tv_sec = 0;
182 value.it_value.tv_usec = 0;
183 value.it_interval = value.it_value;
184 setitimer(ITIMER_REAL, &value, NULL);
185
186 if(time_used <= POWER_KEY_PRESS_TIMEOUT)
187 {
188 screen_state_change(false);
189 }
190 else
191 {
192 LOGI("Press timeout.");
193 }
194 }
195 else
196 {
197 LOGI("Long Press timeout.");
198 }
199 }
200 else
201 {
202 LOGI("UP key for screen on.");
203 }
204
205 key_down = false;
206 }
207#endif
208}
209
210
211int main(int argc, char *argv[])
212{
213 mbtk_log_init(NULL, "MBTK_KEY");
214 // Open dev
215 ev_init();
216
217 int ret = 0;
218 struct input_event ev;
219 while(1)
220 {
221 ret = ev_get(&ev, -1);
222 if(ret)
223 {
224 LOGW("ev_get() fail.");
225 continue;
226 }
227
228 LOGI("ev:time[%ld,%ld],type:%d,code:%d,value:%d",ev.time.tv_sec,ev.time.tv_usec,
229 ev.type,ev.code,ev.value);
230 if(ev.type != EV_KEY)
231 {
232 LOGW("event type error.[%d]",ev.type);
233 continue;
234 }
235
236 switch(ev.code)
237 {
238 case KEY_POWER: // Power key
239 {
240 power_process(ev.value, &(ev.time));
241 break;
242 }
243 default:
244 {
245 LOGD("Unknown KEY[%d]",ev.code);
246 break;
247 }
248 }
249 }
250 return 0;
251}