-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMKeyDevice.c
More file actions
128 lines (112 loc) · 2.96 KB
/
Copy pathMKeyDevice.c
File metadata and controls
128 lines (112 loc) · 2.96 KB
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
/* Copyright (C) Marko Petrović 2022
*
* 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 2 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/>.
*/
/*
Notes about the program:
Replace event19 in line 68 with corresponding keyboard event. (check /proc/bus/input/devices)
Replace number 61 in "input61::numlock" in line 69 with corresponding number (check what folder exists in /sys/class/leds).
*/
#include <unistd.h>
#include <fcntl.h>
#include <linux/uinput.h>
#include <string.h>
void send(int fd, int type, int code, int val)
{
struct input_event ie;
ie.type = type;
ie.code = code;
ie.value = val;
ie.time.tv_sec = 0;
ie.time.tv_usec = 0;
write(fd, &ie, sizeof(ie));
}
int main()
{
struct uinput_setup usetup;
int a, b, c;
struct input_event ie;
char led;
b = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if(b < 0)
goto End;
ioctl(b, UI_SET_EVBIT, EV_KEY);
ioctl(b, UI_SET_KEYBIT, BTN_LEFT);
ioctl(b, UI_SET_KEYBIT, BTN_RIGHT);
ioctl(b, UI_SET_EVBIT, EV_REL);
ioctl(b, UI_SET_RELBIT, REL_X);
ioctl(b, UI_SET_RELBIT, REL_Y);
memset(&usetup, 0, sizeof(usetup));
usetup.id.vendor = 0x1234;
usetup.id.product = 0x5678;
strcpy(usetup.name, "Virtual mouse");
ioctl(b, UI_DEV_SETUP, &usetup);
ioctl(b, UI_DEV_CREATE);
sleep(1);
a = open("/dev/input/event19", O_RDONLY);
c = open("/sys/class/leds/input61::numlock/brightness", O_RDONLY);
while(a > 0)
{
read(a, &ie, sizeof(ie));
if(ie.type == EV_KEY && ie.value != 0)
{
if(ie.code == KEY_ESC)
break;
lseek(c, 0, SEEK_SET);
read(c, &led, 1);
if(led == '0')
{
if(ie.code == KEY_KP8)
{
send(b, EV_REL, REL_Y, -5);
send(b, EV_SYN, SYN_REPORT, 0);
}
if(ie.code == KEY_KP5)
{
send(b, EV_REL, REL_Y, 5);
send(b, EV_SYN, SYN_REPORT, 0);
}
if(ie.code == KEY_KP4)
{
send(b, EV_REL, REL_X, -5);
send(b, EV_SYN, SYN_REPORT, 0);
}
if(ie.code == KEY_KP6)
{
send(b, EV_REL, REL_X, 5);
send(b, EV_SYN, SYN_REPORT, 0);
}
if(ie.code == KEY_KP7)
{
send(b, EV_KEY, BTN_LEFT, 1);
send(b, EV_SYN, SYN_REPORT, 0);
send(b, EV_KEY, BTN_LEFT, 0);
send(b, EV_SYN, SYN_REPORT, 0);
}
if(ie.code == KEY_KP9)
{
send(b, EV_KEY, BTN_RIGHT, 1);
send(b, EV_SYN, SYN_REPORT, 0);
send(b, EV_KEY, BTN_RIGHT, 0);
send(b, EV_SYN, SYN_REPORT, 0);
}
}
}
}
sleep(1);
ioctl(b, UI_DEV_DESTROY);
close(a);
close(b);
End:
return 0;
}