-
Notifications
You must be signed in to change notification settings - Fork 0
/
beauty.c
399 lines (341 loc) · 9.43 KB
/
beauty.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*************************************************************************
Translates Beauty-R1 clicker mouse movements into keypresses
Copyright (C) 2023 Luca Olivetti
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#include <string.h>
#include <stdbool.h>
#include <sys/time.h>
#include <sys/inotify.h>
#include <signal.h>
#include "keycodes.h"
int button=0;
int x=1904;
int y=1904;
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
int direction=0;
bool debug=false;
#define debugln(...) if (debug) printf(__VA_ARGS__)
/*
Checks if a device in /dev/input is the Beauty-R1.
If it is, opens it and return the handle
*/
int check_beauty(char *devname) {
int fd;
char name[256];
char filename[300];
if (strncmp("event", devname, strlen("event"))!=0) {
return -1;
}
snprintf(filename, 300, "/dev/input/%s", devname);
if ((fd=open(filename, O_RDONLY)) > 0) {
ioctl(fd, EVIOCGNAME(sizeof(name)),name);
printf("Device %s name %s\n",filename,name);
if (strcmp("Beauty-R1",name)==0) {
printf("***FOUND\n");
ioctl(fd,EVIOCGRAB,1);
return fd;
}
close(fd);
} else {
printf("cannot open %s\n",filename);
}
return -1;
}
/*
inotify event, checks if the created file is the Beauty-R1
*/
int get_event (int fd) {
char buffer[sizeof(struct inotify_event) + PATH_MAX+ 1];
int length, i = 0;
int result=-1;
length = read( fd, buffer, sizeof(buffer) );
if ( length < 0 ) {
perror( "read" );
}
struct inotify_event *event = ( struct inotify_event * ) &buffer;
if ( event->len ) {
if ( event->mask & IN_CREATE) {
if (event->mask & IN_ISDIR) {
printf( "The directory %s was Created.\n", event->name );
} else {
printf( "The file %s was Created with WD %d\n", event->name, event->wd );
result=check_beauty(event->name);
}
}
}
return result;
}
/*
Opens the /dev/input/eventX device corresponding to the
Beauty-R1.
First checks if the device already exists in the directory,
if it doesn't uses inotify to wait for its creation.
*/
int open_beauty() {
DIR *dp;
int fd,wd,result;
struct dirent *ep;
struct inotify_event ev;
dp = opendir("/dev/input/");
if (dp != NULL) {
while ((ep = readdir(dp)) != NULL) {
if ((fd=check_beauty(ep->d_name)) > 0) {
closedir(dp);
return fd;
}
}
closedir(dp);
//
fd = inotify_init();
if ( fd < 0 ) {
perror( "Couldn't initialize inotify");
}
wd = inotify_add_watch(fd, "/dev/input/", IN_CREATE);
if (wd == -1) {
printf("Couldn't add watch to /dev/input/\n");
} else {
printf("Watching:: /dev/input/\n");
}
/* do it forever*/
result=-1;
while(result<0) {
result=get_event(fd);
}
/* Clean up*/
inotify_rm_watch( fd, wd );
close( fd );
return result;
} else {
printf("cannot open /dev/input\n");
return -1;
}
}
/*
Forks and sends a keycode using the "input" program
*/
static int sendkeycode(int k)
{
int my_pid;
char buffer[100];
snprintf(buffer, sizeof(buffer), "input keyevent %d", k);
if (0 == (my_pid = fork())) {
system(buffer);
exit(0);
}
return 0;
}
/*
Actions for a button
*/
enum buttons {btUp, btDown, btLeft, btRight, btEnter, btPhoto, btLongUp, btLongDown, btLongLeft, btLongRight};
char *btname[] = {"UP", "DOWN", "LEFT", "RIGHT", "ENTER", "PHOTO", "LONGUP", "LONGDOWN", "LONGLEFT", "LONGRIGHT"};
int btcodes[] = {AKEYCODE_DPAD_UP, //btUp
AKEYCODE_DPAD_DOWN, //btDown
AKEYCODE_DPAD_LEFT, //btLeft
AKEYCODE_DPAD_RIGHT, //btRight
AKEYCODE_ENTER, //btEnter
AKEYCODE_CAMERA, //btPhoto
AKEYCODE_CALL, //btLongUp
AKEYCODE_MUTE, //btLongDown
AKEYCODE_MEDIA_PREVIOUS, //btLongLeft
AKEYCODE_MEDIA_NEXT //btLongRight
};
void dobutton(enum buttons b) {
printf(":%s\n",btname[b]);
sendkeycode(btcodes[b]);
}
/*
New x position received, checks if it's moving right/left
*/
void setx(struct input_event *ev)
{
if (button) {
if (ev->value>x) {
debugln("right\n");
direction=LEFT;
}
if (ev->value<x) {
debugln("left\n");
direction=RIGHT;
}
}
x=ev->value;
debugln("x now is %d\n",x);
}
/*
New y position received, checks if it moves up or down
When x is 2048 it's a long press, report just the first
repetition (y 784 or 2912).
*/
void sety(struct input_event *ev)
{
struct timeval timediff;
if (button) {
if (ev->value>y) {
debugln("down\n");
direction=UP;
}
if (ev->value<y) {
debugln("up\n");
direction=DOWN;
}
}
y=ev->value;
debugln("y now is %d\n",y);
if (x==2048 && button) {
//to avoid repetitions only use the first event (depending on y)
if (y==784)
dobutton(btLongUp);
if (y==2912)
dobutton(btLongDown);
}
}
/*
Checks the elapsed time between two timestamps
*/
bool lapsed(struct timeval *then, struct timeval *now, long secs, long usecs) {
struct timeval timediff;
timersub(now, then, &timediff);
if (timediff.tv_sec>secs)
return true;
if (timediff.tv_sec<secs)
return false;
return timediff.tv_usec>=usecs;
}
/*
Register button presses and releases
When pressed, reset the direction.
when released,check the position:
1904/1904 is the central button
1536/608 is the photo button
otherwise use the direction to know if it was
up/down/left/right
*/
void setbutton(struct input_event *ev) {
if (ev->value!=button) {
button=ev->value;
if (button>0) {
debugln("button press\n");
direction=0;
} else {
debugln("button release\n");
if ( x==1904 && y==1904) {
dobutton(btEnter);
return;
}
if ( x==1536 && y==608) {
dobutton(btPhoto);
return;
}
debugln("direction %d\n",direction);
if (direction==RIGHT) {
dobutton(btRight);
return;
}
if (direction==LEFT) {
dobutton(btLeft);
return;
}
//when x is 2048 it's a long press, don't report normal up/down
if (x!=2048 && direction==UP) {
dobutton(btUp);
return;
}
if (x!=2048 && direction==DOWN) {
dobutton(btDown);
return;
}
}
}
}
/*
Checks for the long press of the left/right buttons.
These button are reported repeatedly as KEY_VOLUMEDOWN, KEY_VOLUMEUP,
here we're using the elapsed time to only report the first repetition.
*/
#define LONGLEFT 0
#define LONGRIGHT 1
void longpress(struct input_event* ev, int index) {
static struct timeval prevlong[2];
static bool longdone[2]={false,false};
static int longkeycodes[2]={KEY_VOLUMEDOWN, KEY_VOLUMEUP};
static enum buttons longbuttons[2]={btLongLeft, btLongRight};
//after a different keypress or 1.3 seconds (the repetition is about 1.1 seconds)
//we can accept a new long press
if (longdone[index]) {
if (ev->code!=longkeycodes[index] || lapsed(&prevlong[index],&ev->time,1,300000))
longdone[index]=false;
}
if (ev->code==longkeycodes[index]) {
if (!longdone[index])
dobutton(longbuttons[index]);
longdone[index]=true;
prevlong[index]=ev->time;
}
}
/*
Main
*/
int main(int argc, char **argv)
{
if (argc>1) {
if (strcmp(argv[1],"debug")==0) {
debug=true;
}
}
int fd;
struct input_event ie;
signal(SIGCHLD, SIG_IGN); //avoid zombies
while (true) {
fd=open_beauty();
if (fd<0)
exit;
while(read(fd, &ie, sizeof(struct input_event))>0)
{
debugln("ts %10ld.%10ld type %d code %d value %d\n",ie.time.tv_sec,ie.time.tv_usec,ie.type, ie.code, ie.value);
switch(ie.type) {
case EV_ABS:
switch(ie.code) {
case ABS_X:
setx(&ie);
break;
case ABS_Y:
sety(&ie);
break;
}
break;
case EV_KEY:
if (ie.code==BTN_TOOL_PEN)
setbutton(&ie);
if (ie.value==1) {
longpress(&ie, LONGLEFT);
longpress(&ie, LONGRIGHT);
}
break;
}
}
close(fd);
}
return 0;
}