blob: 133384b21316028c8603e4d4284d255874b18c36 [file] [log] [blame]
b.liud440f9f2025-04-18 10:44:31 +08001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <stdarg.h>
5#include <unistd.h>
6#include <sys/ioctl.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
10#include <sys/select.h>
11#include <sys/time.h>
12#include <errno.h>
13#include <linux/input.h>
14#include <unistd.h>
15#include <linux/fb.h>
16#include <sys/mman.h>
17#include <time.h>
18#include <pthread.h>
19#include <sys/poll.h>
20#include <dirent.h>
21#include <stdbool.h>
22
23// #include "mbtk_log.h"
24
25#ifndef TRUE
26#define TRUE 1 /* Boolean true value. */
27#endif
28
29#ifndef true
30#define true 1 /* Boolean true value. */
31#endif
32
33#ifndef FALSE
34#define FALSE 0 /* Boolean false value. */
35#endif
36
37#ifndef false
38#define false 0 /* Boolean false value. */
39#endif
40
41
42#ifndef NULL
43#define NULL 0
44#endif
45
46
47#ifndef null
48#define null 0
49#endif
50
51#define LOGI printf
52#define LOGE printf
53
54/**
55 * Compiler-digit : 16
56 * char : 1 (%c)
57 * char* : 2
58 * short int : 2
59 * int : 2 (%d)
60 * unsigned int : 2 (%u)
61 * float : 4 (%f)
62 * double : 8 (%f)
63 * long : 4
64 * unsigned long : 4
65 * long long : 8
66 * unsigned long long : 8
67 *
68 *
69 * Compiler-digit : 32
70 * char : 1
71 * char* : 4
72 * short int : 2
73 * int : 4
74 * unsigned int : 4
75 * float : 4
76 * double : 8
77 * long : 4
78 * unsigned long : 4
79 * long long : 8
80 * unsigned long long : 8
81 *
82 *
83 * Compiler-digit : 64
84 * char : 1
85 * char* : 8
86 * short int : 2
87 * int : 4
88 * unsigned int : 4
89 * float : 4
90 * double : 8
91 * long : 8
92 * unsigned long : 8
93 * long long : 8
94 * unsigned long long : 8
95 */
96typedef unsigned char boolean; /* Boolean value type. */
97// typedef unsigned char bool; /* Boolean value type. */
98typedef unsigned long long uint64; /* Unsigned 64 bit value */
99typedef unsigned long long uint64_t; /* Unsigned 64 bit value */
100typedef unsigned int uint32; /* Unsigned 32 bit value */
101typedef unsigned int uint32_t; /* Unsigned 32 bit value */
102typedef unsigned short uint16; /* Unsigned 16 bit value */
103typedef unsigned short uint16_t;
104typedef unsigned char uint8; /* Unsigned 8 bit value */
105typedef unsigned char uint8_t;
106typedef signed long long int64; /* Signed 64 bit value */
107typedef signed long long sint64; /* Signed 64 bit value */
108typedef signed int int32; /* Signed 32 bit value */
109typedef signed int sint32; /* Signed 32 bit value */
110typedef signed short int16; /* Signed 16 bit value */
111typedef signed short sint16; /* Signed 16 bit value */
112typedef signed char int8; /* Signed 8 bit value */
113typedef signed char sint8; /* Signed 8 bit value */
114typedef unsigned char byte; /* byte type */
115
116//#include "mbtk_type.h"
117//#include "mbtk_log.h"
118
119typedef int (*ev_callback)(int fd, uint32_t epevents, void *data);
120
121typedef enum {
122 ACTION_DOWN,
123 ACTION_MOVE,
124 ACTION_UP,
125 ACTION_CANCEL
126} touch_event_action_enum;
127
128typedef struct {
129 touch_event_action_enum action;
130 int x;
131 int y;
132} touch_event;
133
134static int move_x;
135static touch_event event_pre;
136static bool action_down_get = false;
137static bool action_touched = false;
138
139#define MAX_DEVICES 16
140#define MAX_MISC_FDS 16
141
142#define BITS_PER_LONG (sizeof(unsigned long) * 8)
143#define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG)
144
145#define test_bit(bit, array) \
146 ((array)[(bit)/BITS_PER_LONG] & (1 << ((bit) % BITS_PER_LONG)))
147
148struct fd_info {
149 ev_callback cb;
150 void *data;
151};
152
153static struct pollfd ev_fds[MAX_DEVICES + MAX_MISC_FDS];
154static struct fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
155
156static unsigned ev_count = 0;
157static unsigned ev_dev_count = 0;
158static unsigned ev_misc_count = 0;
159
160int ev_get_input(int fd, short revents, struct input_event *ev);
161bool event_process(struct input_event ev);
162
163int ev_cb(int fd, uint32_t epevents, void *data)
164{
165 struct input_event ev;
166
167 int retval = ev_get_input(fd, epevents, &ev);
168 if(retval < 0) return -1;
169
170 if(!event_process(ev)) return 0;
171
172 return 0;
173}
174
175int ev_init(void *data)
176{
177 DIR *dir;
178 struct dirent *de;
179 int fd;
180
181 dir = opendir("/dev/input");
182 if(dir != 0) {
183 while((de = readdir(dir))) {
184 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];
185
186// fprintf(stderr,"/dev/input/%s\n", de->d_name);
187 if(strncmp(de->d_name,"event",5)) continue;
188 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
189 if(fd < 0) continue;
190
191 /* read the evbits of the input device */
192 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) < 0) {
193 close(fd);
194 continue;
195 }
196
197 /* TODO: add ability to specify event masks. For now, just assume
198 * that only EV_KEY and EV_REL event types are ever needed. */
199 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits)) {
200 close(fd);
201 continue;
202 }
203
204 ev_fds[ev_count].fd = fd;
205 ev_fds[ev_count].events = POLLIN;
206 ev_fdinfo[ev_count].cb = ev_cb;
207 ev_fdinfo[ev_count].data = data;
208 ev_count++;
209 ev_dev_count++;
210 if(ev_dev_count == MAX_DEVICES) break;
211 }
212 }
213
214 return 0;
215}
216
217void ev_exit(void)
218{
219 while (ev_count > 0) {
220 close(ev_fds[--ev_count].fd);
221 }
222 ev_misc_count = 0;
223 ev_dev_count = 0;
224}
225
226int ev_wait(int timeout)
227{
228 int r;
229
230 r = poll(ev_fds, ev_count, timeout);
231 if (r <= 0)
232 return -1;
233 return 0;
234}
235
236void ev_dispatch(void)
237{
238 unsigned n;
239// int ret;
240
241 for (n = 0; n < ev_count; n++) {
242 ev_callback cb = ev_fdinfo[n].cb;
243 if (cb && (ev_fds[n].revents & ev_fds[n].events))
244 cb(ev_fds[n].fd, ev_fds[n].revents, ev_fdinfo[n].data);
245 }
246}
247
248int ev_get_input(int fd, short revents, struct input_event *ev)
249{
250 int r;
251
252 if (revents & POLLIN) {
253 r = read(fd, ev, sizeof(*ev));
254 if (r == sizeof(*ev))
255 return 0;
256 }
257 return -1;
258}
259
260bool event_process(struct input_event ev)
261{
262 LOGI("Event:%d,%d,%d\n", ev.type, ev.code, ev.value);
263
264// if(ev.type != EV_KEY){
265// return false;
266// }
267
268 bool is_touch = true;
269 // Touch Down/Up
270 if(ev.type == EV_KEY && ev.code == BTN_TOUCH) {
271 if(!!ev.value) { // Down
272 action_down_get = true;
273 action_touched = true;
274 }
275 else // UP
276 {
277 action_down_get = false;
278 action_touched = false;
279 }
280 } else if(ev.type == EV_ABS) { // Touch move
281 if(!action_touched) // No down
282 return false;
283 } else if(ev.type != EV_KEY) {
284 return false;
285 } else {
286 is_touch = false;
287 }
288
289 if (!is_touch && ev.value < 2){ // 2 is long press events
290 int down = !!ev.value;
291 if (down){
292 LOGI("LongPress : DOWN.");
293 //if(!l->onKeyDown(ev.type, ev.code)) return false;
294 }else{
295 //if(!l->onKeyUp(ev.type, ev.code)) return false;
296 LOGI("LongPress : UP.");
297 }
298 } else if (is_touch) {
299 touch_event m_event;
300 if(ev.type == EV_ABS) { // Move
301 if(ev.code == KEY_SLASH) { // X
302 move_x = ev.value;
303 } else if(ev.code == KEY_RIGHTSHIFT) { // Y
304 if(action_down_get)
305 {
306 m_event.action = ACTION_DOWN;
307 action_down_get = false;
308 } else {
309 m_event.action = ACTION_MOVE;
310 }
311 m_event.x = move_x;
312 m_event.y = ev.value;
313
314 if(event_pre.x != m_event.x
315 || event_pre.y != m_event.y)
316 {
317 event_pre.x = m_event.x;
318 event_pre.y = m_event.y;
319 #ifdef MBTK_TP_RESIZE_SUPPORT
320 point_resize(getScreenWidth(),
321 getScreenHeight(),
322 mStatusBar->isVisibility() ? mStatusBar->getHeight() : 0,
323 &m_event);
324 #endif
325 LOGI("Window onTouchEvent action:%d (%d,%d) -> (%d,%d)",
326 m_event.action, event_pre.x, event_pre.y, m_event.x, m_event.y);
327
328 }
329 } else {
330 // Do nothing
331 }
332 } else if(!action_down_get){ // UP
333 m_event.action = ACTION_UP;
334 m_event.x = event_pre.x;
335 m_event.y = event_pre.y;
336
337 #ifdef MBTK_TP_RESIZE_SUPPORT
338 point_resize(getScreenWidth(),
339 getScreenHeight(),
340 mStatusBar->isVisibility() ? mStatusBar->getHeight() : 0,
341 &m_event);
342 #endif
343
344 LOGI("Window onTouchEvent action:%d (%d,%d) -> (%d,%d)",
345 m_event.action, event_pre.x, event_pre.y, m_event.x, m_event.y);
346
347 } else {
348 // Do nothing
349 }
350 } else {
351 // Do nothing
352 }
353
354 //invalidate();
355
356 return true;
357}
358
359int main(int argc, char *argv[])
360{
361 // mbtk_log_init(NULL, "MBTK_EVENT");
362
363 if(ev_init(NULL)) {
364 LOGE("ev_init() fail.");
365 return -1;
366 }
367
368 LOGI("event getting...");
369 while(1) {
370 if(!ev_wait(-1))
371 ev_dispatch();
372 }
373
374 LOGI("exit!!!");
375 return 0;
376}