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