Skip to content

Commit c669faa

Browse files
author
Robin Müller
committed
Added first draft for gesture detection
1 parent e8ddd3f commit c669faa

File tree

1 file changed

+125
-41
lines changed

1 file changed

+125
-41
lines changed

src/main.c

Lines changed: 125 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,149 @@
44
#include <unistd.h>
55
#include <fcntl.h>
66
#include <errno.h>
7+
#include <stdlib.h>
8+
#include <stdint.h>
9+
710

811
#include <linux/input.h>
9-
#include <linux/uinput.h>
1012

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"
1515

16-
int main() {
17-
int fd;
1816

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;
3018

31-
struct uinput_user_dev uidev;
19+
typedef struct t_point {
20+
int x;
21+
int y;
22+
} point;
3223

33-
memset(&uidev, 0, sizeof(uidev));
24+
point start_point;
3425

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;
4027

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);
4333
}
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;
4645
}
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+
}
4753

48-
struct input_event ev;
54+
static void print_events(int fd) {
55+
struct input_event ev[64];
56+
int i, rd;
4957

50-
memset(&ev, 0, sizeof(ev));
58+
while (1) {
59+
rd = read(fd, ev, sizeof(struct input_event) * 64);
5160

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+
}
5565

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+
}
58128
}
129+
}
59130

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);
61136

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);
65140

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+
}
67151
return 0;
68152
}

0 commit comments

Comments
 (0)