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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 | #include <asm-generic/errno-base.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <linux/input-event-codes.h>
#define BITS_PER_LONG (sizeof(unsigned long) * 8)
#define TEST_BIT(bit, array) \
(((array)[(bit) / BITS_PER_LONG] >> ((bit) % BITS_PER_LONG)) & 1UL)
void error(char *t,char *m,...)
{
va_list args;
va_start(args,m);
fprintf(stderr, "\033[1;31m[E]\033[0m \033[1;33m%s\033[0m:", t);
vfprintf(stderr, m,args);
fprintf(stderr, "\n");
va_end(args);
exit(1);
}
struct dirent *entry;
struct input_event ev;
typedef struct {
int fd;
int keys[KEY_MAX + 1];
} kb_args;
typedef struct {
char *Key;
char *value;
}KeyValue ;
typedef struct {
KeyValue *items;
int count;
} Section;
typedef struct {
Section macro;
Section block;
Section repeat;
} Config;
Config *init_config()
{
Config *list = malloc(sizeof(Config));
list->macro.items = malloc(sizeof(KeyValue));
list->block.items = malloc(sizeof(KeyValue));
list->repeat.items = malloc(sizeof(KeyValue));
list->macro.count = 0;
list->block.count = 0;
list->repeat.count = 0;
return list;
}
void add_config(Section *cfg,char *key,char *value)
{
cfg->count++;
cfg->items = realloc(cfg->items,sizeof(KeyValue) * cfg->count);
if (!cfg->items) error("Memory","memory allocation failed.");
cfg->items[cfg->count -1].Key = strdup(key);
cfg->items[cfg->count -1].value = strdup(value);
}
void free_cfg(Config *cfg)
{
Section *sections[] = {&cfg->macro,&cfg->block,&cfg->repeat};
for (int i=0;i<3;++i){
for (int j=0; j< sections[i]->count;++j) {
free(sections[i]->items[j].Key);
free(sections[i]->items[j].value);
}
free(sections[i]->items);
}
free(cfg);
}
int is_keyboard(struct dirent *entry, int *fd, char *full_entry,char *device)
{
if (strncmp(entry->d_name, "event",5) != 0) return 0;
sprintf(full_entry,"/dev/input/%s",entry->d_name);
if ((*fd=open(full_entry, O_RDONLY)) < 0) return 0;
unsigned long keybits[(KEY_MAX + BITS_PER_LONG) / BITS_PER_LONG];
memset(keybits, 0, sizeof(keybits));
ioctl(*fd,EVIOCGBIT(EV_KEY, sizeof(keybits)),keybits);
if (
!TEST_BIT(KEY_SPACE,keybits) &&
!TEST_BIT(KEY_BRIGHTNESS_AUTO, keybits) &&
!TEST_BIT(KEY_VIDEO, keybits) &&
!TEST_BIT(KEY_BLUETOOTH, keybits)
) return 0;
if (ioctl(*fd, EVIOCGNAME(256), device) < 0) return 0;
return 1;
}
int remove_spaces(char *str) {
int j = 0, h = 0;
for (int i = 0; str[i]; i++) {
if (!isspace(str[i]) || str[i] == '\n') {
if (!isspace(str[i])) h = 1;
str[j++] = str[i];
}
}
str[j] = '\0';
return h;
}
void read_config(Config *cfg)
{
FILE *config_file;
char *config_path = getenv("STA");
if (!config_path) error("Config", "can not find config path (check man page)");
if(!(config_file=fopen(config_path,"r")))
{
if (errno == ENOENT) error("config","File does not exist");
if (errno == EISDIR) error("config","Is a directory");
if (errno == EACCES) error("config","Permission denied");
}
char buf[256];
char header;
int a = 0;
while (fgets(buf, sizeof(buf), config_file)) {
++a;
if (buf[0]=='#') continue;
if (!remove_spaces(buf)) continue;
if (!strncmp(buf,">",1)){
switch (buf[1]) {
case 'm': header = 'm'; break;
case 'b': header = 'b'; break;
case 'r': header = 'r'; break;
}
continue;
}
char *spliter = strchr(buf, '=');
if (!spliter) error("config","syntax error in line %d",a);
*spliter = '\0';
char *key = buf;
char *value = spliter + 1;
switch (header) {
case 'm': add_config(&cfg->macro , key, value); break;
case 'b': add_config(&cfg->block , key, value); break;
case 'r': add_config(&cfg->repeat, key, value); break;
}
}
}
void *keyboard_loop(void *arg)
{
kb_args *kb = arg;
while (read(kb->fd, &ev, sizeof(ev))) {
if (ev.type == EV_KEY) {
kb->keys[ev.code] = ev.value;
if (kb->keys[KEY_SPACE] >= 1 && kb->keys[KEY_A] >= 1){
printf("asdf");
}
}
}
return NULL;
}
int main(void)
{
Config *cfg = init_config();
read_config(cfg);
DIR *events_entry = opendir("/dev/input");
if (!events_entry) error("Devices","can not read devices directory.");
int keys[KEY_MAX +1];
int fd;
char device[256],full_entry[50];
while ((entry = readdir(events_entry))) {
if (!is_keyboard(entry,&fd,full_entry,device)) continue;
printf("%s\n",device);
if (ioctl(fd,EVIOCGRAB,1) <0){
error("grap", "");
}
int keys[KEY_MAX + 1]= {0};
keyboard_loop();
// while (read(fd, &ev, sizeof(ev))) {
// if (ev.type == EV_KEY) {
// keys[ev.code] = ev.value;
// if (keys[KEY_SPACE] >= 1 && keys[KEY_A] >= 1){
// printf("asdf");
// }
// }
// }
}
free_cfg(cfg);
sleep(5);
closedir(events_entry);
return 0;
}
|