Initial commit - move development to a public repo
[libautomation/elektra-notification.git] / lib / inputevent.c
blob65bf11ea9393a01fb16742d68fc329853ae27104
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <unistd.h>
7 #include "libautomation.h"
9 static void atm_input_cb(EV_P_ ev_io *w, int revents) {
10 struct ATM_INPUT *input = (struct ATM_INPUT *) w;
11 struct input_event event;
13 if (read(w->fd, &event, sizeof(event)) == sizeof(event))
14 input->cb(&event);
17 struct ATM_INPUT *atm_input_by_path(const char *path,
18 void (*cb)(struct input_event *)) {
19 char *realpath = atm_globdup(path);
20 int fd;
21 struct ATM_INPUT *input = NULL;
23 if (!realpath)
24 return NULL;
26 if ((fd = open(realpath, O_RDONLY | O_NONBLOCK)) < 0) {
27 atm_log("Can't open %s", realpath);
28 goto err;
31 input = malloc(sizeof(*input));
32 input->cb = cb;
33 ev_io_init(&input->watcher, atm_input_cb, fd, EV_READ);
34 ev_io_start(EV_A_ &input->watcher);
36 err:
37 free(realpath);
39 return input;