Skip to content

Commit 4bc4fd5

Browse files
author
Robin Müller
committed
Split code into multiple files
- added file for the uinput stuff that creates input events for a gesture - added common.h that contains some common stuff used by multiple modules - added int_array for dynamic int arrays with a length field
1 parent 2b5b485 commit 4bc4fd5

File tree

6 files changed

+142
-1
lines changed

6 files changed

+142
-1
lines changed

src/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
bin_PROGRAMS = touch_gestures
2-
touch_gestures_SOURCES = main.c
2+
touch_gestures_SOURCES = main.c int_array.c gestures_device.c

src/common.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef COMMON_H_
2+
#define COMMON_H_
3+
4+
#include <errno.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
#define die(str, args...) do { \
9+
perror(str); \
10+
exit(EXIT_FAILURE); \
11+
} while(0)
12+
13+
#endif // COMMON_H_

src/gestures_device.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <fcntl.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
#include <unistd.h>
6+
7+
#include <linux/input.h>
8+
#include <linux/uinput.h>
9+
10+
#include "common.h"
11+
#include "gestures_device.h"
12+
13+
int init_uinput(int_array keys) {
14+
int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
15+
if(fd < 0) {
16+
die("error: open");
17+
}
18+
19+
ioctl(fd, UI_SET_EVBIT, EV_KEY);
20+
ioctl(fd, UI_SET_EVBIT, EV_SYN);
21+
ioctl(fd, UI_SET_EVBIT, EV_REL);
22+
23+
ioctl(fd, UI_SET_RELBIT, REL_WHEEL);
24+
25+
ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
26+
ioctl(fd, UI_SET_KEYBIT, BTN_RIGHT);
27+
ioctl(fd, UI_SET_KEYBIT, BTN_MIDDLE);
28+
29+
int i;
30+
for (i = 0; i < keys.length; i++) {
31+
ioctl(fd, UI_SET_KEYBIT, keys.data[i]);
32+
}
33+
34+
struct uinput_user_dev uidev;
35+
36+
memset(&uidev, 0, sizeof(uidev));
37+
38+
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-touch-gestures");
39+
uidev.id.bustype = BUS_USB;
40+
uidev.id.vendor = 0x1;
41+
uidev.id.product = 0x1;
42+
uidev.id.version = 1;
43+
44+
if (write(fd, &uidev, sizeof(uidev)) < 0) {
45+
die("error: write");
46+
}
47+
if (ioctl(fd, UI_DEV_CREATE) < 0) {
48+
die("error: ioctl");
49+
}
50+
return fd;
51+
}
52+
53+
int destroy_uinput(int fd) {
54+
if (ioctl(fd, UI_DEV_DESTROY) < 0) {
55+
die("error: ioctl");
56+
}
57+
58+
close(fd);
59+
}
60+
61+
void send_key(int fd, int key) {
62+
struct input_event key_ev, syn_ev;
63+
64+
memset(&key_ev, 0, sizeof(struct input_event));
65+
key_ev.type = EV_KEY;
66+
key_ev.code = key;
67+
// press the key
68+
key_ev.value = 1;
69+
if (write(fd, &key_ev, sizeof(struct input_event)) < 0) {
70+
die("error: write");
71+
}
72+
73+
memset(&syn_ev, 0, sizeof(struct input_event));
74+
syn_ev.type = EV_SYN;
75+
syn_ev.code = 0;
76+
syn_ev.value = 0;
77+
if (write(fd, &syn_ev, sizeof(struct input_event)) < 0) {
78+
die("error: write");
79+
}
80+
81+
// release the key
82+
key_ev.value = 0;
83+
if (write(fd, &key_ev, sizeof(struct input_event)) < 0) {
84+
die("error: write");
85+
}
86+
if (write(fd, &syn_ev, sizeof(struct input_event)) < 0) {
87+
die("error: write");
88+
}
89+
}

src/gestures_device.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef GESTURES_DEVICE_H_
2+
#define GESTURES_DEVICE_H_
3+
4+
#include "int_array.h"
5+
6+
int init_uinput(int_array keys);
7+
int destroy_uinput(int fd);
8+
void send_key(int fd, int key);
9+
10+
#endif // GESTURES_DEVICE_H_

src/int_array.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdlib.h>
2+
3+
#include "int_array.h"
4+
5+
int_array *new_int_array(size_t length) {
6+
int_array *array;
7+
/* we're allocating the size of basic t_int_array
8+
(which already contains space for one int)
9+
and additional space for length-1 ints */
10+
array = malloc(sizeof(int_array) + sizeof(int) * (length - 1));
11+
if(!array) {
12+
return 0;
13+
}
14+
array->length = length;
15+
return array;
16+
}

src/int_array.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef INTEGER_ARRAY_H_
2+
#define INTEGER_ARRAY_H_
3+
4+
#include <stddef.h>
5+
6+
typedef struct t_int_array {
7+
size_t length;
8+
int data[1];
9+
} int_array;
10+
11+
int_array * new_int_array(size_t length);
12+
13+
#endif // INTEGER_ARRAY_H_

0 commit comments

Comments
 (0)