-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindkeys.c
107 lines (83 loc) · 2.7 KB
/
bindkeys.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
/*
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 <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
extern int errno;
#define DATA_MAX 8
int main(int argc, char * argv[])
{
int i;
int fd;
FILE * fd2;
int arg;
unsigned char data[DATA_MAX];
unsigned int data2[DATA_MAX];
unsigned char command[255];
ssize_t datasize;
fprintf(stdout, "bindkeys v0.2\n");
if (argc < 3)
{
fprintf(stdout, "usage : bindkeys /dev/hidraw? file.conf\n");
return 1;
}
// open hidraw device
if ( (fd = open(argv[1], O_RDONLY)) == -1)
{
fprintf(stdout, "open error %s : %s\n", argv[1], strerror(errno));
return 1;
}
// open conf file
if ( (fd2 = fopen(argv[2], "r")) == NULL)
{
fprintf(stdout, "open error %s : %s\n", argv[2], strerror(errno));
close(fd);
return 1;
}
while(1)
{
for (i=0; i < DATA_MAX; i++)
{
data[i] = 0;
data2[i] = 0;
}
if ( (datasize = read(fd, data, sizeof data)) == -1)
{
fprintf(stdout, "read error %s : %s\n", argv[1], strerror(errno));
close(fd);
return 1;
}
fprintf(stdout, "datasize = %lu, Key : ", datasize);
for(i=0; i< DATA_MAX; i++)
{
fprintf(stdout, "%02X ", data[i]);
}
fseek(fd2, 0, SEEK_SET);
// search for the command for the key pressed
while(1)
{
if ( ( arg = fscanf(fd2, "%02X %02X %02X %02X %02X %02X %02X %02X %254[^\n]", &data2[0], &data2[1], &data2[2], &data2[3], &data2[4], &data2[5], &data2[6], &data2[7], command)) == EOF)
{
break;
}
if (arg != DATA_MAX+1) break;
if ((unsigned int)data[0] == data2[0] && (unsigned int)data[1] == data2[1] && (unsigned int)data[2] == data2[2] && (unsigned int)data[3] == data2[3] && (unsigned int)data[4] == data2[4] && (unsigned int)data[5] == data2[5] && (unsigned int)data[6] == data2[6] && (unsigned int)data[7] == data2[7])
{
fprintf(stdout, "Command -> %s", command);
system((const char*)command);
break;
}
}
fprintf(stdout, "\n");
}
fclose(fd2);
close(fd);
return 0;
}