Add some basic tests for the library itself
[libautomation.git] / lib / inputevent.c
blob5314f56b31e015de6186bca4a59f85163dcc1ac4
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <linux/input.h>
8 #include "libautomation.h"
10 static void atm_input_cb(EV_P_ ev_io *w, int revents) {
11 struct ATM_INPUT *input = (struct ATM_INPUT *) w;
12 struct input_event event;
14 if (read(w->fd, &event, sizeof(event)) == sizeof(event))
15 input->cb(&event);
18 struct ATM_INPUT *atm_input_by_path(const char *path,
19 void (*cb)(struct input_event *)) {
20 char *realpath = atm_globdup(path);
21 int fd;
22 struct ATM_INPUT *input = NULL;
24 if (!realpath)
25 return NULL;
27 if ((fd = open(realpath, O_RDONLY | O_NONBLOCK)) < 0) {
28 atm_log("Can't open %s", realpath);
29 goto err;
32 input = malloc(sizeof(*input));
33 input->cb = cb;
34 ev_io_init(&input->watcher, atm_input_cb, fd, EV_READ);
35 ev_io_start(EV_A_ &input->watcher);
37 err:
38 free(realpath);
40 return input;