|
4 | 4 | #include <unistd.h>
|
5 | 5 | #include <fcntl.h>
|
6 | 6 | #include <errno.h>
|
| 7 | +#include <stdlib.h> |
| 8 | +#include <stdint.h> |
| 9 | + |
7 | 10 |
|
8 | 11 | #include <linux/input.h>
|
9 |
| -#include <linux/uinput.h> |
10 | 12 |
|
11 |
| -#define die(str, args...) do { \ |
12 |
| - perror(str); \ |
13 |
| - exit(EXIT_FAILURE); \ |
14 |
| - } while(0) |
| 13 | +#include "common.h" |
| 14 | +#include "gestures_device.h" |
15 | 15 |
|
16 |
| -int main() { |
17 |
| - int fd; |
18 | 16 |
|
19 |
| - fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK); |
20 |
| - if(fd < 0) { |
21 |
| - die("error: open"); |
22 |
| - } |
23 |
| - int ret; |
24 |
| - if (ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0) { |
25 |
| - die("error: ioctl"); |
26 |
| - } |
27 |
| - if (ioctl(fd, UI_SET_KEYBIT, KEY_D) < 0) { |
28 |
| - die("error: ioctl"); |
29 |
| - } |
| 17 | +int finger_count = 0; |
30 | 18 |
|
31 |
| - struct uinput_user_dev uidev; |
| 19 | +typedef struct t_point { |
| 20 | + int x; |
| 21 | + int y; |
| 22 | +} point; |
32 | 23 |
|
33 |
| - memset(&uidev, 0, sizeof(uidev)); |
| 24 | +point start_point; |
34 | 25 |
|
35 |
| - snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample"); |
36 |
| - uidev.id.bustype = BUS_USB; |
37 |
| - uidev.id.vendor = 0x1234; |
38 |
| - uidev.id.product = 0xfedc; |
39 |
| - uidev.id.version = 1; |
| 26 | +int active_slot; |
40 | 27 |
|
41 |
| - if (write(fd, &uidev, sizeof(uidev)) < 0) { |
42 |
| - die("error: write"); |
| 28 | +static int test_grab(int fd) { |
| 29 | + int rc; |
| 30 | + rc = ioctl(fd, EVIOCGRAB, (void*)1); |
| 31 | + if (!rc) { |
| 32 | + ioctl(fd, EVIOCGRAB, (void*)0); |
43 | 33 | }
|
44 |
| - if (ioctl(fd, UI_DEV_CREATE) < 0) { |
45 |
| - die("error: ioctl"); |
| 34 | + return rc; |
| 35 | +} |
| 36 | + |
| 37 | +void set_start_point(int fd) { |
| 38 | + struct input_mt_request_layout { |
| 39 | + uint32_t code; |
| 40 | + int32_t values[2]; |
| 41 | + } data; |
| 42 | + data.code = ABS_MT_POSITION_X; |
| 43 | + if (ioctl(fd, EVIOCGMTSLOTS(sizeof(data)), &data) < 0) { |
| 44 | + return; |
46 | 45 | }
|
| 46 | + start_point.x = data.values[0]; |
| 47 | + data.code = ABS_MT_POSITION_Y; |
| 48 | + if (ioctl(fd, EVIOCGMTSLOTS(sizeof(data)), &data) < 0) { |
| 49 | + return; |
| 50 | + } |
| 51 | + start_point.y = data.values[0]; |
| 52 | +} |
47 | 53 |
|
48 |
| - struct input_event ev; |
| 54 | +static void print_events(int fd) { |
| 55 | + struct input_event ev[64]; |
| 56 | + int i, rd; |
49 | 57 |
|
50 |
| - memset(&ev, 0, sizeof(ev)); |
| 58 | + while (1) { |
| 59 | + rd = read(fd, ev, sizeof(struct input_event) * 64); |
51 | 60 |
|
52 |
| - ev.type = EV_KEY; |
53 |
| - ev.code = KEY_D; |
54 |
| - ev.value = 1; |
| 61 | + if (rd < (int) sizeof(struct input_event)) { |
| 62 | + printf("expected %d bytes, got %d\n", (int) sizeof(struct input_event), rd); |
| 63 | + return; |
| 64 | + } |
55 | 65 |
|
56 |
| - if (write(fd, &ev, sizeof(ev)) < 0) { |
57 |
| - die("error: write"); |
| 66 | + for (i = 0; i < rd / sizeof(struct input_event); i++) { |
| 67 | + if (ev[i].type == EV_KEY) { |
| 68 | + if (ev[i].value == 1) { |
| 69 | + switch (ev[i].code) { |
| 70 | + case BTN_TOOL_FINGER: |
| 71 | + finger_count = 1; |
| 72 | + set_start_point(fd); |
| 73 | + break; |
| 74 | + case BTN_TOOL_DOUBLETAP: |
| 75 | + finger_count = 2; |
| 76 | + set_start_point(fd); |
| 77 | + break; |
| 78 | + case BTN_TOOL_TRIPLETAP: |
| 79 | + finger_count = 3; |
| 80 | + set_start_point(fd); |
| 81 | + break; |
| 82 | + case BTN_TOOL_QUADTAP: |
| 83 | + finger_count = 4; |
| 84 | + set_start_point(fd); |
| 85 | + break; |
| 86 | + case BTN_TOOL_QUINTTAP: |
| 87 | + finger_count = 5; |
| 88 | + set_start_point(fd); |
| 89 | + break; |
| 90 | + default: |
| 91 | + finger_count = 0; |
| 92 | + } |
| 93 | + } else if (ev[i].value == 0 && BTN_TOOL_FINGER) { |
| 94 | + finger_count = 0; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (finger_count > 0 && ev[i].type == EV_ABS) { |
| 99 | + if (ev[i].code == ABS_MT_SLOT) { |
| 100 | + active_slot = ev[i].value; |
| 101 | + } else if (active_slot == 0) { |
| 102 | + int difference; |
| 103 | + switch (ev[i].code) { |
| 104 | + case ABS_MT_POSITION_X: |
| 105 | + difference = start_point.x - ev[i].value; |
| 106 | + if (difference > 500) { |
| 107 | + printf("%d fingers left\n", finger_count); |
| 108 | + finger_count = 0; |
| 109 | + } else if (difference < -500) { |
| 110 | + printf("%d fingers right\n", finger_count); |
| 111 | + finger_count = 0; |
| 112 | + } |
| 113 | + break; |
| 114 | + case ABS_MT_POSITION_Y: |
| 115 | + difference = start_point.y - ev[i].value; |
| 116 | + if (difference > 300) { |
| 117 | + printf("%d fingers up\n", finger_count); |
| 118 | + finger_count = 0; |
| 119 | + } else if (difference < -300) { |
| 120 | + printf("%d fingers down\n", finger_count); |
| 121 | + finger_count = 0; |
| 122 | + } |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
58 | 128 | }
|
| 129 | +} |
59 | 130 |
|
60 |
| - sleep(2); |
| 131 | +int main(int argc, char *argv[]) { |
| 132 | + int_array *keys = new_int_array(1); |
| 133 | + keys->data[0] = KEY_D; |
| 134 | + int uinput_fd = init_uinput(*keys); |
| 135 | + free(keys); |
61 | 136 |
|
62 |
| - if (ioctl(fd, UI_DEV_DESTROY) < 0) { |
63 |
| - die("error: ioctl"); |
64 |
| - } |
| 137 | +// sleep(1); |
| 138 | +// send_key(uinput_fd, KEY_D); |
| 139 | + destroy_uinput(uinput_fd); |
65 | 140 |
|
66 |
| - close(fd); |
| 141 | + if (argc > 1) { |
| 142 | + int fd = open(argv[1], O_RDONLY); |
| 143 | + if (fd < 0) { |
| 144 | + die("error: open"); |
| 145 | + } |
| 146 | + if (!test_grab(fd)) { |
| 147 | + print_events(fd); |
| 148 | + } |
| 149 | + close(fd); |
| 150 | + } |
67 | 151 | return 0;
|
68 | 152 | }
|
0 commit comments