From 37990cf7bf93ea383d48e71f6c63925834418e23 Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Tue, 28 Jun 2011 14:43:03 +0300 Subject: Flush written events immediately. --- src/evemu.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/evemu.c b/src/evemu.c index 21187af..f1a9eea 100644 --- a/src/evemu.c +++ b/src/evemu.c @@ -379,6 +379,7 @@ int evemu_record(FILE *fp, int fd, int ms) return ret; if (ret == sizeof(ev)) evemu_write_event(fp, &ev); + fflush(fp); } return 0; -- cgit v1.2.3 From 4b99c44b90aba3800246ed145270b53282a14e55 Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Tue, 28 Jun 2011 14:50:10 +0300 Subject: Can set an output file on the command line. --- tools/evemu-record.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tools/evemu-record.c b/tools/evemu-record.c index afdd400..1acb06f 100644 --- a/tools/evemu-record.c +++ b/tools/evemu-record.c @@ -51,8 +51,9 @@ int main(int argc, char *argv[]) { int fd; + FILE *out; if (argc < 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); + fprintf(stderr, "Usage: %s [output file]\n", argv[0]); return -1; } fd = open(argv[1], O_RDONLY | O_NONBLOCK); @@ -60,9 +61,21 @@ int main(int argc, char *argv[]) fprintf(stderr, "error: could not open device\n"); return -1; } - if (evemu_record(stdout, fd, WAIT_MS)) { + + if (argc < 3) + out = stdout; + else { + out = fopen(argv[2], "w"); + if (!out) { + fprintf(stderr, "error: could not open output file"); + } + } + + if (evemu_record(out, fd, WAIT_MS)) { fprintf(stderr, "error: could not describe device\n"); } close(fd); + if (argc > 2) + fclose(out); return 0; } -- cgit v1.2.3 From d2ef7446d7243518d41b1fd0e6a1fd2481011beb Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Tue, 28 Jun 2011 18:54:35 +0300 Subject: Add signal handler to flush data in case of abrupt exit. --- tools/evemu-record.c | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/tools/evemu-record.c b/tools/evemu-record.c index 1acb06f..25e7b05 100644 --- a/tools/evemu-record.c +++ b/tools/evemu-record.c @@ -45,13 +45,22 @@ #include #include #include +#include #define WAIT_MS 10000 +FILE *output; + +static void handler (int sig) +{ + fflush(output); + if (output != stdout) + fclose(output); +} + int main(int argc, char *argv[]) { int fd; - FILE *out; if (argc < 2) { fprintf(stderr, "Usage: %s [output file]\n", argv[0]); return -1; @@ -62,20 +71,33 @@ int main(int argc, char *argv[]) return -1; } + struct sigaction act; + memset (&act, '\0', sizeof(act)); + act.sa_handler = &handler; + + if (sigaction(SIGTERM, &act, NULL) < 0) { + fprintf (stderr, "Could not attach TERM signal handler.\n"); + return 1; + } + if (sigaction(SIGINT, &act, NULL) < 0) { + fprintf (stderr, "Could not attach INT signal handler.\n"); + return 1; + } + if (argc < 3) - out = stdout; + output = stdout; else { - out = fopen(argv[2], "w"); - if (!out) { + output = fopen(argv[2], "w"); + if (!output) { fprintf(stderr, "error: could not open output file"); } } - if (evemu_record(out, fd, WAIT_MS)) { + if (evemu_record(output, fd, WAIT_MS)) { fprintf(stderr, "error: could not describe device\n"); } close(fd); - if (argc > 2) - fclose(out); + if (output != stdout) + fclose(output); return 0; } -- cgit v1.2.3 From d2f570cc2d844667233039cf8759329d67053a68 Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Tue, 28 Jun 2011 19:23:25 +0300 Subject: Indentation fix. --- tools/evemu-record.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/evemu-record.c b/tools/evemu-record.c index 25e7b05..7cf1161 100644 --- a/tools/evemu-record.c +++ b/tools/evemu-record.c @@ -53,9 +53,9 @@ FILE *output; static void handler (int sig) { - fflush(output); - if (output != stdout) - fclose(output); + fflush(output); + if (output != stdout) + fclose(output); } int main(int argc, char *argv[]) -- cgit v1.2.3