-
Notifications
You must be signed in to change notification settings - Fork 0
/
rkbd.c
143 lines (116 loc) · 2.31 KB
/
rkbd.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
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "net.h"
static int uinput_fd;
int init_uinput(){
int a=0,b=0;
a = open("/dev/uinput", O_WRONLY | O_NDELAY);
b = open("/dev/input/uinput", O_WRONLY | O_NDELAY);
if(a>=0)
{
uinput_fd=a;
}
else if(b>=0)
{
uinput_fd=b;
}
else
{
return -1;
}
struct uinput_user_dev dev;
memset(&dev, 0, sizeof(struct uinput_user_dev));
strcpy(dev.name, "RKBD");
for(a=0;a<=EV_MAX;a++)
ioctl(uinput_fd, UI_SET_EVBIT, a);
for(a=0;a<=KEY_MAX;a++)
ioctl(uinput_fd, UI_SET_KEYBIT, a);
for(a=0;a<=REL_MAX;a++)
ioctl(uinput_fd, UI_SET_RELBIT, a);
for(a=0;a<=ABS_MAX;a++)
ioctl(uinput_fd, UI_SET_ABSBIT, a);
for(a=0;a<=MSC_MAX;a++)
ioctl(uinput_fd, UI_SET_MSCBIT, a);
write(uinput_fd, &dev, sizeof(struct uinput_user_dev));
return ioctl(uinput_fd, UI_DEV_CREATE)?-1:0;
}
void destroy_uinput(){
ioctl(uinput_fd, UI_DEV_DESTROY);
close(uinput_fd);
}
void press_key(int keycode){
struct input_event ev;
memset(&ev, 0, sizeof(struct input_event));
gettimeofday(&ev.time, NULL);
ev.type=EV_KEY;
ev.code=keycode;
ev.value=1;
write(uinput_fd, &ev, sizeof(struct input_event));
ev.type = EV_SYN;
ev.code = SYN_REPORT;
ev.value = 0;
write(uinput_fd, &ev, sizeof(struct input_event));
}
void release_key(int keycode){
struct input_event ev;
memset(&ev, 0, sizeof(struct input_event));
gettimeofday(&ev.time, NULL);
ev.type=EV_KEY;
ev.code=keycode;
ev.value=0;
write(uinput_fd, &ev, sizeof(struct input_event));
ev.type = EV_SYN;
ev.code = SYN_REPORT;
ev.value = 0;
write(uinput_fd, &ev, sizeof(struct input_event));
}
int main(int argc, char **argv)
{
char keys[1024]={0};
if(init_uinput()<0)
{
perror("init_uinput failed");
return -1;
}
int src=server(12345);
int data;
while(1)
{
if(!read(src, &data, 4)){
exit(1);
}
if(data<1024)
{
printf("Got Keycode: %i\n", data);
keys[data]^=1;
if(keys[data])
{
press_key(data);
}
else
{
release_key(data);
}
}
else
{
int i;
for(i=0;i<1024;i++)
{
release_key(i);
}
memset(keys, 0, 1024);
}
}
destroy_uinput();
return 0;
}