diff --git a/.gitignore b/.gitignore index f47cb20..15a9e6c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.out +*.swp diff --git a/.macrod.c.swp b/.macrod.c.swp index 84a3fb6..d1b5c36 100644 Binary files a/.macrod.c.swp and b/.macrod.c.swp differ diff --git a/macrod.c b/macrod.c index f88853f..d2ee133 100644 --- a/macrod.c +++ b/macrod.c @@ -3,9 +3,12 @@ #include /* Standard errors */ #include -/* Directory control */ +/* Directory and file control */ #include +#include +#include #include +#include #ifdef __linux__ #include @@ -24,18 +27,29 @@ int pressBufferRemove (struct pressed_buffer*, unsigned short); int main (void) // remember getopt() to automaically parse options { - FILE *fp; - fp = fopen("/dev/input/event0", "r"); - if (fp == NULL) { + int fd; + fd = open("/dev/input/event0", O_RDONLY); // TEST: O_NONBLOCK + if (!fd) { fputs(strerror(errno), stderr); exit(errno); } + //int f2 = open("/dev/input/event3", O_RDONLY); + //struct input_event ev; + struct input_event event; struct pressed_buffer pb = {NULL, 0}; // Pressed keys buffer - while (!ferror(fp)) { - fread(&event, sizeof(struct input_event), 1, fp); + while (1) { + /* Use polling poll(2) to wait por a file dscriptor to become ready for reading. + * NOTE: this could use select(2) but I don't know how */ + + //fread(&event, sizeof(struct input_event), 1, fp); + + ssize_t rb; + rb = read(fd, &event, sizeof(struct input_event)); + if (rb != sizeof(struct input_event)) continue; + if (event.type == EV_KEY) { switch (event.value) { @@ -54,7 +68,7 @@ int main (void) // remember getopt() to automaically parse options } } - if (fclose(fp) == EOF) { + if (close(fd) == -1) { fputs(strerror(errno), stderr); exit(errno); }