-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotkey.c
57 lines (47 loc) · 1.01 KB
/
hotkey.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
/*
* Hotkey
* Executes commands in response to EV_KEY events from an evdev device.
*
* Copyright (C) 2014 Wiktor W Brodlo
*
* Hotkey is released under the MIT licence. See the LICENSE file for copyright
* details.
*/
#include <stdint.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <err.h>
#include <unistd.h>
typedef char *const command;
struct event {
uint16_t code;
int32_t value;
command *cmd;
};
#include "config.h"
const size_t nevents = sizeof(events) / sizeof(events[0]);
int main(void)
{
struct input_event ev;
size_t i;
int fd = open(device, O_RDONLY);
if (fd == -1)
err(1, "open %s", device);
while (1) {
read(fd, &ev, sizeof(ev));
for (i = 0; i < nevents; ++i)
if (ev.code == events[i].code &&
ev.value == events[i].value &&
ev.type == EV_KEY &&
#if DELAY
!usleep(DELAY) &&
#endif
!fork()) {
close(fd);
execvp(events[i].cmd[0], events[i].cmd);
err(1, "execvp");
}
}
}