Repository navigation
Expand file tree
/
Copy pathmultitouch_reader.cpp
More file actions
132 lines (116 loc) · 3.8 KB
/
Copy pathmultitouch_reader.cpp
File metadata and controls
132 lines (116 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <libinput.h>
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>
#include <thread>
/*
running:
sudo ./multitouch_reader
*/
static int open_restricted(const char *path, int flags, void *user_data)
{
int fd = open(path, flags);
return fd < 0 ? -errno : fd;
}
static void close_restricted(int fd, void *user_data)
{
close(fd);
}
const static struct libinput_interface interface = {
.open_restricted = open_restricted,
.close_restricted = close_restricted,
};
// Function to handle libinput events in a separate thread
void event_handler(struct libinput *li)
{
while (1)
{
struct libinput_event *ev;
libinput_dispatch(li);
while ((ev = libinput_get_event(li)))
{
libinput_event_type evType = libinput_event_get_type(ev);
if (evType == LIBINPUT_EVENT_TOUCH_DOWN)
{
// Touch down event
struct libinput_event_touch *tev = libinput_event_get_touch_event(ev);
if (tev)
{
int touch_id = libinput_event_touch_get_slot(tev);
double x = libinput_event_touch_get_x_transformed(tev, 1.0);
double y = libinput_event_touch_get_y_transformed(tev, 1.0);
printf("Touch Down - ID: %d, X: %f, Y: %f\n", touch_id, x, y);
}
}
else if (evType == LIBINPUT_EVENT_TOUCH_UP)
{
// Touch up event
struct libinput_event_touch *tev = libinput_event_get_touch_event(ev);
if (tev)
{
int touch_id = libinput_event_touch_get_slot(tev);
printf("Touch Up - ID: %d\n", touch_id);
}
}
else if (evType == LIBINPUT_EVENT_TOUCH_MOTION)
{
// Touch motion event
struct libinput_event_touch *tev = libinput_event_get_touch_event(ev);
if (tev)
{
int touch_id = libinput_event_touch_get_slot(tev);
double x = libinput_event_touch_get_x_transformed(tev, 1.0);
double y = libinput_event_touch_get_y_transformed(tev, 1.0);
printf("Touch Motion - ID: %d, X: %f, Y: %f\n", touch_id, x, y);
}
}
else if (evType == LIBINPUT_EVENT_TOUCH_CANCEL)
{
// Touch cancel event
struct libinput_event_touch *tev = libinput_event_get_touch_event(ev);
if (tev)
{
int touch_id = libinput_event_touch_get_slot(tev);
printf("Touch Cancel - ID: %d\n", touch_id);
}
}
libinput_event_destroy(ev);
}
}
}
int main() {
// Open the libinput context
struct libinput *li;
struct udev *udev;
udev = udev_new();
if (!udev) {
perror("Failed to create udev context");
return 1;
}
li = libinput_udev_create_context(&interface, NULL, udev);
if (!li) {
perror("Failed to create libinput context");
udev_unref(udev);
return 1;
}
libinput_udev_assign_seat(li, "seat0");
if (!li) {
perror("Failed to create libinput context");
udev_unref(udev);
return 1;
}
// Create a separate thread for event handling
std::thread event_thread(event_handler, li); // Pass the libinput context
// Main event loop
while (1)
{
}
// Clean up and close the libinput context in the event handler thread
event_thread.join();
libinput_unref(li);
udev_unref(udev);
return 0;
}