-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdetect_button.c
213 lines (177 loc) · 4.16 KB
/
detect_button.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright (C) 2007 Olli Salonen <[email protected]>
* see btnx-config.c for detailed license information
*/
/* This file is a leftover from initial testing. Confirm and remove! */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <sys/wait.h>
#include <linux/input.h>
#include <errno.h>
#define EVENT_PATH "/dev/input/event"
#define MAX_EVENTS 20
#define CLEAR_CODE_TRAIL(i) ((i) & 0xFFFFFF00)
#define IS_MOUSE_X(i) (CLEAR_CODE_TRAIL(i) == 0x02000000)
#define IS_MOUSE_Y(i) (CLEAR_CODE_TRAIL(i) == 0x02000100)
#define IS_MOUSE_MOVE(i) (IS_MOUSE_X(i) || IS_MOUSE_Y(i))
#define CHAR2INT(c, x) (((int)(c)) << ((x) * 8))
#define INPUT_BUFFER_SIZE 512
#define MAX_RAWCODES 10
#define HEXDUMP_SIZE 16
typedef struct ev_handler
{
int fd;
int set_fd;
int index;
} ev_handler;
int set_handlers(fd_set *fds, ev_handler *handlers, int on)
{
int max_fd=0;
int i=0;
while (handlers[i].fd > 0)
{
if (handlers[i].set_fd == on)
{
FD_SET(handlers[i].fd, fds);
if (handlers[i].fd > max_fd)
max_fd = handlers[i].fd;
}
i++;
}
return max_fd;
}
void clear_buffer(ev_handler *handlers)
{
int i=0;
unsigned char buffer[32];
while (handlers[i].fd > 0)
{
while (read(handlers[i].fd, buffer, 32) > 0);
i++;
}
}
void get_rawcodes(ev_handler *handlers)
{
int codes[128];
fd_set fds;
int ret, i, j;
unsigned char buffer[INPUT_BUFFER_SIZE];
int max_fd;
memset(codes, 0, 128*sizeof(int));
for (;;)
{
memset(buffer, '\0', INPUT_BUFFER_SIZE);
FD_ZERO(&fds);
max_fd = set_handlers(&fds, handlers, 0);
select(max_fd+1, &fds, NULL, NULL, NULL);
i=0;
while (handlers[i].fd > 0)
{
if ((FD_ISSET(handlers[i].fd, &fds)) == 0)
{
i++;
continue;
}
if ((ret = read(handlers[i].fd, buffer, INPUT_BUFFER_SIZE)) < 1)
{
i++;
continue;
}
/*for (j=0; j<ret; j++)
{
printf("%02x", buffer[j]);
if (j % 2 == 0)
printf(" ");
}*/
//putchar('\n');
for (j=0; (j < (ret / HEXDUMP_SIZE)) && (j < MAX_RAWCODES - 1); j++)
{
codes[j] = CHAR2INT(buffer[8 + j*HEXDUMP_SIZE], 3) |
CHAR2INT(buffer[11 + j*HEXDUMP_SIZE], 2) |
CHAR2INT(buffer[10 + j*HEXDUMP_SIZE], 1) |
CHAR2INT(buffer[13 + j*HEXDUMP_SIZE], 0);
if (!IS_MOUSE_MOVE(codes[j]) && codes[j] != 0)
printf("Caught %016x\n", codes[j]);
}
i++;
}
}
}
int detect(int argc, char *argv[])
{
ev_handler handlers[MAX_EVENTS+1];
char ev_path[32];
int max_fd=0, fd, ready;
fd_set fds;
int i, handlers_size=0;
struct timeval timeout = {1, 0};
time_t start;
int ignore=1;
for (i=0; i <= MAX_EVENTS; i++)
{
sprintf(ev_path, "%s%d", EVENT_PATH, i);
if ((fd = open(ev_path, O_RDONLY | O_NONBLOCK)) > 0)
{
printf("Found handler: %s\n", ev_path);
handlers[handlers_size].fd = fd;
handlers[handlers_size].index = i;
handlers[handlers_size++].set_fd = 1;
}
}
handlers[handlers_size].fd = -1;
if (handlers_size == 0)
{
fprintf(stderr, "Error: could not find any event handlers.\n");
exit(1);
}
// DEBUG
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
handlers[3].set_fd = 0;
goto done;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
printf("Do not move your mouse for the next 5 seconds\n");
start = time(NULL);
sleep(3);
clear_buffer(handlers);
for (;;)
{
FD_ZERO(&fds);
max_fd = set_handlers(&fds, handlers, 1);
ready = select(max_fd+1, &fds, NULL, NULL, &timeout);
//printf("time: %d\n", time(NULL) - start);
if ((time(NULL) - start) > 5 && ignore == 1)
{
printf("OK, move your mouse now.\n");
ignore = 0;
}
if (ready < 0)
{
fprintf(stderr, "Error: select() failed: %s\n", strerror(errno));
exit(1);
}
else if (ready == 0)
{
//printf("Timeout reached with no events. Quitting\n");
//exit(0);
}
for (i=0; i<handlers_size; i++)
{
if (FD_ISSET(handlers[i].fd, &fds) && handlers[i].set_fd != 0)
{
handlers[i].set_fd = 0;
printf("Using handler %s%d\n", EVENT_PATH, handlers[i].index);
goto done;
}
}
}
done:
get_rawcodes(handlers);
return 0;
}