-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyTouch.c
145 lines (117 loc) · 4.15 KB
/
myTouch.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <linux/input.h>
#include <fcntl.h>
#include <X11/Xlib.h>
void mouseClick(Display *display, Window root, int button);
void mouseMove (Display *display, Window root, int x, int y);
void getInput(Display *display, Window root, char* devName);
int main(int argc, char** argv) {
printf("Init\n");
//Open X Display
Display *display = XOpenDisplay(NULL);
assert(display != NULL);
Window root = XDefaultRootWindow(display);
char* eventName = "/dev/input/event2";
mouseMove(display, root, 0, 0);
mouseClick(display, root, Button1);
getInput(display, root, eventName);
//Close the display
XCloseDisplay(display);
printf("You are awesome :) \n");
return EXIT_SUCCESS;
}
void mouseClick(Display *display, Window root, int button) {
XEvent clickEvent;
int click;
memset (&clickEvent, 0, sizeof (clickEvent));
clickEvent.xbutton.button = button;
clickEvent.xbutton.same_screen = True;
clickEvent.xbutton.subwindow = root;
while (clickEvent.xbutton.subwindow)
{
clickEvent.xbutton.window = clickEvent.xbutton.subwindow;
XQueryPointer (display, clickEvent.xbutton.window,
&clickEvent.xbutton.root, &clickEvent.xbutton.subwindow,
&clickEvent.xbutton.x_root, &clickEvent.xbutton.y_root,
&clickEvent.xbutton.x, &clickEvent.xbutton.y,
&clickEvent.xbutton.state);
}
//Press
clickEvent.type = ButtonPress;
click = XSendEvent (display, PointerWindow, True, ButtonPressMask, &clickEvent);
if (click == 0) {
printf("Click Failed\n");
}
XFlush (display);
usleep (1);
// Release
clickEvent.type = ButtonRelease;
click = XSendEvent (display, PointerWindow, True, ButtonPressMask, &clickEvent);
if (click == 0) {
printf("Click Failed\n");
}
XFlush (display);
usleep (1);
}
void mouseMove(Display *display, Window root, int x, int y) {
XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
XFlush(display);
usleep(1);
}
void getInput(Display *display, Window root, char* devName) {
int fd, rd, i, root_x, root_y;
struct input_event ev[64];
Window window_returned;
int win_x, win_y;
unsigned int mask_return;
int prev_x, prev_y;
int slots = 0;
fd = open(devName, O_RDONLY);
if(fd < 0) {
printf("Error Opening device. Are you root?\n");
}
while(1) {
rd = read(fd, ev, sizeof(struct input_event) * 64);
if(rd < (int) sizeof(struct input_event)) {
printf("Error Reading Input");
}
for(i = 0; i < rd / sizeof(struct input_event); i++) {
if(ev[i].code >= 47 && ev[i].code <= 60) {
//Get current pointer information
XQueryPointer(display, root, &window_returned,
&window_returned, &root_x, &root_y, &win_x, &win_y,
&mask_return);
int ii;
//Fill the slots
if(ev[i].code == ABS_MT_SLOT && (ev[i].value - slots) == 1){
slots++;
printf("ADD SLOTS: %d\n",slots);
}
//X-Axis
if(ev[i].code == ABS_MT_POSITION_X) {
mouseMove(display, root, ev[i].value, root_y);
prev_x = ev[i].value;
}
//Y-Axis
if(ev[i].code == ABS_MT_POSITION_Y) {
mouseMove(display, root, root_x, ev[i].value);
prev_y = ev[i].value;
}
if(ev[i].code == ABS_MT_TRACKING_ID && ev[i].value == -1){
slots--;
printf("REMOVE SLOTS: %d\n",slots);
//We can click with one slot ;)
/*if(slots == 0){
mouseClick(display, root, Button1);
}*/
mouseClick(display, root, Button1);
}
}
}
}
close(fd);
}