-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmouse.c
191 lines (152 loc) · 4.9 KB
/
mouse.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
#include "xwiigun.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/uinput.h>
#include <signal.h>
#include <time.h>
/*****************************************/
/* Function Prototypes */
/*****************************************/
void sigint_handler(int sig_num);
void emit(int fd, int type, int code, int val);
void close_handler(void);
/*****************************************/
/* Variables */
/*****************************************/
static int fd;
static struct xwiigun gun;
/*****************************************/
/* Code */
/*****************************************/
/* Signal Handler for SIGINT */
void sigint_handler(int sig_num)
{
printf("\n User provided signal handler for Ctrl+C \n");
close_handler();
exit(0);
}
/* uinput emit function */
void emit(int fd, int type, int code, int val)
{
struct input_event ie;
ie.type = type;
ie.code = code;
ie.value = val;
/* timestamp values below are ignored */
ie.time.tv_sec = 0;
ie.time.tv_usec = 0;
write(fd, &ie, sizeof(ie));
}
/* Close handler. Close file descriptors */
void close_handler(void)
{
ioctl(fd, UI_DEV_DESTROY);
close(fd);
xwiigun_close(&gun);
}
/* Main. Start Point */
int main(int argc, char **argv)
{
int ret = 1;
struct uinput_user_dev dev;
int width = 65535,
height = 65535;
/* Check for screen resolution parameters */
if (argc == 3) {
width = atoi(argv[1]);
height = atoi(argv[2]);
} else {
/* Incorrect number of params. Show usage */
fprintf(stderr, "usage: %s [width height]\n", argv[0]);
return 1;
}
/* Try to open a uinput device and respond accordingly */
if ((fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK)) < 0) {
perror("open");
printf("No access to /dev/uinput. ! DO NOT USE SUDO ! See Readme.md\n");
return 1;
}
printf("xwiigun-mouse: Emulating an absolute touch device %dx%d\n", width, height);
/* Initialise the sigint_handler */
signal(SIGINT, sigint_handler);
/* enable mouse button left and relative events */
ioctl(fd, UI_SET_EVBIT, EV_KEY);
ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
ioctl(fd, UI_SET_KEYBIT, BTN_RIGHT);
/* Tell uinput to use absolute positioning */
ioctl(fd, UI_SET_EVBIT, EV_ABS);
ioctl(fd, UI_SET_ABSBIT, ABS_X);
ioctl(fd, UI_SET_ABSBIT, ABS_Y);
/* Configure uinput settings and identifier */
memset(&dev, 0, sizeof(dev));
dev.id.bustype = BUS_USB;
dev.id.vendor = 0x1234; /* sample vendor */
dev.id.product = 0x5678; /* sample product */
strcpy(dev.name, "xwiigun");
/* initialise the abs min/max values */
dev.absmax[ABS_X] = width;
dev.absmax[ABS_Y] = height;
dev.absmin[ABS_X] = 0;
dev.absmin[ABS_Y] = 0;
write(fd, &dev, sizeof(dev));
/* Create the uinput device */
ioctl(fd, UI_DEV_CREATE);
/*
* On UI_DEV_CREATE the kernel will create the device node for this
* device. We are inserting a pause here so that userspace has time
* to detect, initialize the new device, and can start listening to
* the event, otherwise it will not notice the event we are about
* to send. This pause is only needed in our example code!
*/
sleep(1);
if (xwiigun_open(&gun)) {
fprintf(stderr, "xwiigun-mouse: xwiigun_open() failed :(\n");
close_handler();
return (1);
}
// we don't have our own event loop
gun.blocking = true;
uint8_t buttons[2] = { 0, 0 };
printf("xwiigun-mouse: Running\n");
while (!xwiigun_poll(&gun))
{
if (gun.hpos < 0) gun.hpos = 0;
if (gun.hpos > 1) gun.hpos = 1;
if (gun.vpos < 0) gun.vpos = 0;
if (gun.vpos > 1) gun.vpos = 1;
#if 0
printf("xwiigun-mouse: %d x %d\n", (int)(gun.hpos * width), (int)(gun.vpos * height));
#endif
/* Calculate and emit the abs position for x and y */
emit(fd, EV_ABS, ABS_X, gun.hpos * width);
emit(fd, EV_ABS, ABS_Y, gun.vpos * height);
/* Check A button press state */
if (gun.keys[XWII_KEY_A] != buttons[0]) {
buttons[0] = !buttons[0];
emit(fd, EV_KEY, BTN_RIGHT, buttons[0]);
}
/* Check B button press state */
if (gun.keys[XWII_KEY_B] != buttons[1]) {
buttons[1] = !buttons[1];
emit(fd, EV_KEY, BTN_LEFT, buttons[1]);
}
/* Tell the os to update the mouse position */
emit(fd, EV_SYN, SYN_REPORT, 0);
}
printf("xwiigun-mouse: Not Running\n");
ret = 0;
/*
* Give userspace some time to read the events before we destroy the
* device with UI_DEV_DESTOY.
*/
sleep(1);
/* close the file descriptors */
close_handler();
return ret;
}