-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbind-keys.c
More file actions
319 lines (287 loc) · 8.69 KB
/
bind-keys.c
File metadata and controls
319 lines (287 loc) · 8.69 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <libconfig.h>
#include <time.h>
#include <sys/wait.h>
#include <pthread.h>
typedef struct node {
const char *command;
time_t execute_at;
struct node *next;
} node;
int show_keys = 0;
char *mode;
node *delayed;
config_t cfg;
config_setting_t *key_binds;
const char *keyboard;
int wait_to_execute = 0;
int read_config();
int read_input(const char *keyboard, config_setting_t *key_binds);
int add_key_pressed(struct input_event *keys, struct input_event *ev);
int remove_key_pressed(struct input_event *keys, struct input_event *ev);
int get_action(struct input_event *keys, config_setting_t *key_binds);
node *add_item(node *listpointer, const char *command, time_t execute_at);
node *remove_item(node *listpointer);
void* wait_to_exec(void *ptr);
void check_queue(node *listpointer);
void execute_command(const char *command);
int help();
int main(int argc, char **argv) {
if(argc > 1) {
argv[1][sizeof(argv[1])-1] = '\0';
printf("%i\n", strcmp(argv[1], "-h"));
if(strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
help();
} else if(strcmp(argv[1], "-s") == 0 || strcmp(argv[1], "--show-keys") == 0) {
show_keys = 1;
} else {
printf("Unknown option: %s\n", argv[1]);
help();
}
}
/* create a second thread to check whether any delayed commands need to be exectued */
pthread_t wait_to_exec_thread;
pthread_create(&wait_to_exec_thread, NULL, &wait_to_exec, NULL);
read_config();
read_input(keyboard, key_binds);
config_destroy(&cfg);
}
int read_input(const char *keyboard, config_setting_t *key_binds) {
int fd;
fd = open(keyboard, O_RDONLY);
struct input_event ev;
struct input_event keys[]= {
{ .type = 0 },
{ .type = 0 },
{ .type = 0 },
{ .type = 0 }
};
while (1) {
read(fd, &ev, sizeof(struct input_event));
if(ev.type != 0 && ev.type < 1000) {
if(ev.value == 1) {
add_key_pressed(keys, &ev);
}
if(ev.value == 0) {
if(show_keys && ev.code != 0) {
printf(" - %i \n", ev.code);
}
get_action(keys, key_binds);
remove_key_pressed(keys, &ev);
}
}
}
}
int get_action(struct input_event *keys, config_setting_t *key_binds) {
/* Get all key-binds */
if(key_binds != NULL) {
unsigned int count = config_setting_length(key_binds);
unsigned int i,j;
// iterate over all key_binds
for(i = 0; i < count; ++i) {
config_setting_t *key_bind = config_setting_get_elem(key_binds, i);
config_setting_t *config_keys = config_setting_get_member(key_bind, "keys");
if(config_keys != NULL) {
unsigned int key_count = config_setting_length(config_keys);
// iterate over pressed keys
unsigned int found = 0;
for (int i = 0; i < 4; i++) {
if(keys[i].type != 0) {
for(j = 0; j < key_count; ++j) {
int key_code = config_setting_get_int_elem(config_keys, j);
if(keys[i].code == key_code) {
found++;
break;
}
}
}
}
// if not all keys were pressed continue to next keybind
if(found != key_count) {
continue;
} else {
const char *command, *command_mode, *change_mode;
int delay = 0;
// execute command
if(config_setting_lookup_string(key_bind, "command", &command)) {
// if a mode is supplied check if it's satisfied
if(config_setting_lookup_string(key_bind, "mode", &command_mode) && mode) {
if(strcmp(command_mode, mode) == 0) {
printf("execute %s - %s\n\n", command, mode);
if(config_setting_lookup_int(key_bind, "delay", &delay)) {
delayed = add_item(delayed, command, time(NULL)+delay);
printf("delayed execution in %d seconds\n", delay);
} else {
// fork() to create child process
execute_command(command);
}
}
} else {
printf("execute %s\n\n", command);
if(config_setting_lookup_int(key_bind, "delay", &delay)) {
delayed = add_item(delayed, command, time(NULL)+delay);
printf("delayed execution in %d seconds\n", delay);
} else {
execute_command(command);
}
}
}
// change mode
if(config_setting_lookup_string(key_bind, "change_mode", &change_mode)) {
if(mode) {
free(mode);
mode = strdup(change_mode);
printf("change_mode %s\n\n", change_mode);
}
}
}
} else {
printf("Could not get keys\n");
}
}
} else {
printf("no key binds\n");
}
return 0;
}
int add_key_pressed(struct input_event *keys, struct input_event *ev) {
for (int i = 0; i < 4; i++) {
// remove keys that have been pressed for longer than 20 seconds
if(ev->time.tv_sec - keys[i].time.tv_sec > 20) {
keys[i].type = 0;
}
if(keys[i].type == 0) {
keys[i] = *ev;
return 0;
}
}
return 0;
}
int remove_key_pressed(struct input_event *keys, struct input_event *ev) {
for (int i = 0; i < 4; i++) {
if(keys[i].code == ev->code) {
keys[i].type = 0;
}
}
return 0;
}
int read_config() {
const char *default_mode;
config_init(&cfg);
/* Read the file. If there is an error, report it and exit. */
if(! config_read_file(&cfg, "bind-keys.cfg")) {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL)
fprintf(stderr, "Could not read config file - Current working dir: %s\n", cwd);
else
perror("getcwd() error");
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
return(EXIT_FAILURE);
}
/* Get the keyboard name. */
if(config_lookup_string(&cfg, "keyboard", &keyboard)) {
printf("Keyboard: %s\n\n", keyboard);
} else {
fprintf(stderr, "No 'keyboard' setting in configuration file.\n");
exit(1);
}
/* Get the default mode name. */
if(config_lookup_string(&cfg, "default_mode", &default_mode)) {
printf("mode: %s\n\n", default_mode);
mode = strdup(default_mode);
} else {
printf("no default mode");
}
key_binds = config_lookup(&cfg, "key_binds");
return 0;
}
void execute_command(const char *command) {
/* double fork to avoid zombie processes */
pid_t pid1;
pid_t pid2;
int status;
if ((pid1 = fork())) {
/* parent process */
waitpid(pid1, &status, WSTOPPED);
} else if (!pid1) {
/* child process */
if ((pid2 = fork())) {
exit(0);
} else if (!pid2) {
/* child process B */
system(command);
exit(0);
}
}
}
void* wait_to_exec(void *ptr) {
// check whether the delayed queue has any entries that need to be executed
while(1) {
if(delayed != NULL) {
check_queue(delayed);
}
sleep(1);
}
}
node *add_item(node *listpointer, const char *command, time_t execute_at) {
node *lp = listpointer;
if (listpointer != NULL) {
while (listpointer->next != NULL) {
// command already in queue, replace the execute at time
if(strcmp(listpointer->command,command) == 0) {
printf("execute at time updated\n");
printf("old time %ld\n", listpointer->execute_at);
listpointer->execute_at = execute_at;
printf("new time %ld\n", listpointer->execute_at);
return lp;
}
listpointer = listpointer->next;
}
listpointer->next = (struct node *) malloc (sizeof (node));
listpointer = listpointer->next;
listpointer->next = NULL;
listpointer->command = command;
listpointer->execute_at = execute_at;
return lp;
} else {
listpointer = (struct node *) malloc (sizeof (node));
listpointer->next = NULL;
listpointer->command = command;
listpointer->execute_at = execute_at;
return listpointer;
}
}
node *remove_item(node *listpointer) {
node *tempp;
tempp = listpointer->next;
free (listpointer);
return tempp;
}
void check_queue(node *listpointer) {
if (listpointer == NULL) {
printf ("queue is empty!\n");
} else {
while (listpointer != NULL) {
if(time(NULL) > listpointer->execute_at) {
execute_command(listpointer->command);
delayed = remove_item(listpointer);
}
listpointer = listpointer->next;
}
}
}
int help() {
printf("Options are:\n");
printf("-h; --help:\t\t Display this help\n");
printf("-s; --show-keys:\t Show the key codes\n");
exit(0);
}