-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathuinput_device.c
150 lines (124 loc) · 3.4 KB
/
uinput_device.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
/*
* This file is part of hid_mapper.
*
* hid_mapper 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.
*
* hid_mapper 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 hid_mapper. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Thibault Kummer <[email protected]>
*/
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#include <linux/uinput.h>
int setup_device(int key_max)
{
int uinput_fd;
int i;
struct uinput_user_dev uidev;
uinput_fd = open("/dev/uinput",O_WRONLY|O_NONBLOCK);
if(uinput_fd<0)
{
uinput_fd = open("/dev/input/uinput",O_WRONLY|O_NONBLOCK);
if(uinput_fd<0)
return -1;
}
if(ioctl(uinput_fd,UI_SET_EVBIT,EV_KEY)<0)
return -1;
if(ioctl(uinput_fd,UI_SET_EVBIT,EV_REL)<0)
return -1;
if(ioctl(uinput_fd,UI_SET_RELBIT,REL_X)<0)
return -1;
if(ioctl(uinput_fd,UI_SET_RELBIT,REL_Y)<0)
return -1;
for(i=0;i<key_max;i++)
if(ioctl(uinput_fd,UI_SET_KEYBIT,i)<0)
return -1;
// Set device properties
memset(&uidev,0,sizeof(uidev));
snprintf(uidev.name,UINPUT_MAX_NAME_SIZE,"Generic USB input mapper");
uidev.id.bustype = BUS_USB;
uidev.id.vendor = 0x1;
uidev.id.product = 0x1;
uidev.id.version = 1;
if(write(uinput_fd, &uidev, sizeof(uidev)) < 0)
return -1;
if(ioctl(uinput_fd, UI_DEV_CREATE) < 0)
return -1;
return uinput_fd;
}
int destroy_device(int uinput_fd)
{
if(ioctl(uinput_fd,UI_DEV_DESTROY)<0)
return -1;
return close(uinput_fd);
}
void send_key_down_event(int uinput_fd,int code)
{
struct input_event ev;
memset(&ev,0,sizeof(struct input_event));
ev.type = EV_KEY;
ev.code = code;
ev.value = 1;
if(write(uinput_fd,&ev,sizeof(struct input_event))<0)
return;
memset(&ev,0,sizeof(struct input_event));
ev.type = EV_SYN;
if(write(uinput_fd, &ev, sizeof(struct input_event)) < 0)
return;
}
void send_key_up_event(int uinput_fd,int code)
{
struct input_event ev;
memset(&ev,0,sizeof(struct input_event));
ev.type = EV_KEY;
ev.code = code;
ev.value = 0;
if(write(uinput_fd,&ev,sizeof(struct input_event))<0)
return;
memset(&ev,0,sizeof(struct input_event));
ev.type = EV_SYN;
if(write(uinput_fd, &ev, sizeof(struct input_event)) < 0)
return;
}
void send_mouse_X_event(int uinput_fd,int pixels)
{
struct input_event ev;
memset(&ev,0,sizeof(struct input_event));
ev.type = EV_REL;
ev.code = REL_X;
ev.value = pixels;
if(write(uinput_fd,&ev,sizeof(struct input_event))<0)
return;
memset(&ev,0,sizeof(struct input_event));
ev.type = EV_SYN;
ev.code = SYN_REPORT;
if(write(uinput_fd, &ev, sizeof(struct input_event)) < 0)
return;
}
void send_mouse_Y_event(int uinput_fd,int pixels)
{
struct input_event ev;
memset(&ev,0,sizeof(struct input_event));
ev.type = EV_REL;
ev.code = REL_Y;
ev.value = pixels;
if(write(uinput_fd,&ev,sizeof(struct input_event))<0)
return;
memset(&ev,0,sizeof(struct input_event));
ev.type = EV_SYN;
ev.code = SYN_REPORT;
if(write(uinput_fd, &ev, sizeof(struct input_event)) < 0)
return;
}