/
touch.c
150 lines (122 loc) · 3.1 KB
/
touch.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Raspberry Pi 7-inch tft touch driver
#include <linux/i2c-dev.h>
#include <linux/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#define T_FILE "/dev/i2c-1"
#define T_ADDR 0x38
Display* display;
void init(void) {
display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "Can't open display");
exit(-1);
}
}
void mouseDown(void) {
printf("Down\n");
XEvent event;
memset (&event, 0, sizeof (event));
event.xbutton.button = 1;
event.xbutton.same_screen = True;
event.xbutton.subwindow = DefaultRootWindow (display);
while (event.xbutton.subwindow) {
event.xbutton.window = event.xbutton.subwindow;
XQueryPointer (display, event.xbutton.window,
&event.xbutton.root, &event.xbutton.subwindow,
&event.xbutton.x_root, &event.xbutton.y_root,
&event.xbutton.x, &event.xbutton.y,
&event.xbutton.state);
}
event.type = ButtonPress;
if (XSendEvent (display, PointerWindow, True, ButtonPressMask, &event) == 0)
fprintf (stderr, "Error to send the event!\n");
XFlush (display);
}
void mouseUp(void) {
printf("Up\n");
XEvent event;
memset (&event, 0, sizeof (event));
event.xbutton.button = 1;
event.xbutton.same_screen = True;
event.xbutton.subwindow = DefaultRootWindow (display);
while (event.xbutton.subwindow) {
event.xbutton.window = event.xbutton.subwindow;
XQueryPointer (display, event.xbutton.window,
&event.xbutton.root, &event.xbutton.subwindow,
&event.xbutton.x_root, &event.xbutton.y_root,
&event.xbutton.x, &event.xbutton.y,
&event.xbutton.state);
}
event.type = ButtonRelease;
if (XSendEvent (display, PointerWindow, True, ButtonReleaseMask, &event) == 0)
fprintf (stderr, "Error to send the event!\n");
XFlush (display);
}
void mouseLocation(int x, int y) {
printf("Absolute\n");
XEvent event;
XQueryPointer (display, DefaultRootWindow (display),
&event.xbutton.root, &event.xbutton.window,
&event.xbutton.x_root, &event.xbutton.y_root,
&event.xbutton.x, &event.xbutton.y,
&event.xbutton.state);
XWarpPointer(display, None, None, 0, 0, 0, 0, x - event.xbutton.x, y - event.xbutton.y);
XFlush(display);
}
void mouseMove(int x, int y) {
printf("Relative\n");
XWarpPointer(display, None, None, 0, 0, 0, 0, x, y);
}
int main() {
init();
int file;
if((file = open(T_FILE, O_RDWR)) < 0) {
printf("E: open failed\n");
return -1;
}
if(ioctl(file, I2C_SLAVE, T_ADDR) < 0) {
printf("E: ioctl failed\n");
printf("%d\n", errno);
return -1;
}
int lastDown;
int down;
char buf[66];
int lastX;
int lastY;
int x;
int y;
while(1) {
write(file, (char[]){0x1}, 1);
read(file, buf, 66);
int npoints = buf[1];
int ev = buf[2] >> 6;
y = ((buf[2] & 0x7) << 8 | buf[3]);
x = ((buf[4] & 0x7) << 8 | buf[5]);
printf("%d %d\n", x, y);
if(x != lastX || y != lastY) {
mouseLocation(x, y);
}
lastX = x;
lastY = y;
down = ev == 2;
if(down != lastDown) {
if(down) {
mouseLocation(x, y);
lastX = x;
lastY = y;
mouseDown();
}
else {
mouseUp();
}
}
lastDown = down;
}
return 0;
}