Merge lp:~jpakkane/evemu/robustness into lp:evemu

Proposed by Jussi Pakkanen
Status: Merged
Merged at revision: 41
Proposed branch: lp:~jpakkane/evemu/robustness
Merge into: lp:evemu
Diff against target: 77 lines (+38/-2)
2 files modified
src/evemu.c (+1/-0)
tools/evemu-record.c (+37/-2)
To merge this branch: bzr merge lp:~jpakkane/evemu/robustness
Reviewer Review Type Date Requested Status
Stephen M. Webb (community) Approve
Chase Douglas (community) Approve
Review via email: mp+66123@code.launchpad.net

Description of the change

Makes evemu-record write its output more robustly. Flush every event write and allow the user to specify an output file.

To post a comment you must log in.
lp:~jpakkane/evemu/robustness updated
43. By Jussi Pakkanen

Add signal handler to flush data in case of abrupt exit.

44. By Jussi Pakkanen

Indentation fix.

Revision history for this message
Chase Douglas (chasedouglas) wrote :

Looks good to me, and fixes a long standing issue :).

review: Approve
Revision history for this message
Stephen M. Webb (bregma) wrote :

Does the right thing.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/evemu.c'
2--- src/evemu.c 2011-04-21 08:49:45 +0000
3+++ src/evemu.c 2011-06-28 16:24:39 +0000
4@@ -379,6 +379,7 @@
5 return ret;
6 if (ret == sizeof(ev))
7 evemu_write_event(fp, &ev);
8+ fflush(fp);
9 }
10
11 return 0;
12
13=== modified file 'tools/evemu-record.c'
14--- tools/evemu-record.c 2011-02-03 02:29:13 +0000
15+++ tools/evemu-record.c 2011-06-28 16:24:39 +0000
16@@ -45,14 +45,24 @@
17 #include <fcntl.h>
18 #include <string.h>
19 #include <unistd.h>
20+#include <signal.h>
21
22 #define WAIT_MS 10000
23
24+FILE *output;
25+
26+static void handler (int sig)
27+{
28+ fflush(output);
29+ if (output != stdout)
30+ fclose(output);
31+}
32+
33 int main(int argc, char *argv[])
34 {
35 int fd;
36 if (argc < 2) {
37- fprintf(stderr, "Usage: %s <device>\n", argv[0]);
38+ fprintf(stderr, "Usage: %s <device> [output file]\n", argv[0]);
39 return -1;
40 }
41 fd = open(argv[1], O_RDONLY | O_NONBLOCK);
42@@ -60,9 +70,34 @@
43 fprintf(stderr, "error: could not open device\n");
44 return -1;
45 }
46- if (evemu_record(stdout, fd, WAIT_MS)) {
47+
48+ struct sigaction act;
49+ memset (&act, '\0', sizeof(act));
50+ act.sa_handler = &handler;
51+
52+ if (sigaction(SIGTERM, &act, NULL) < 0) {
53+ fprintf (stderr, "Could not attach TERM signal handler.\n");
54+ return 1;
55+ }
56+ if (sigaction(SIGINT, &act, NULL) < 0) {
57+ fprintf (stderr, "Could not attach INT signal handler.\n");
58+ return 1;
59+ }
60+
61+ if (argc < 3)
62+ output = stdout;
63+ else {
64+ output = fopen(argv[2], "w");
65+ if (!output) {
66+ fprintf(stderr, "error: could not open output file");
67+ }
68+ }
69+
70+ if (evemu_record(output, fd, WAIT_MS)) {
71 fprintf(stderr, "error: could not describe device\n");
72 }
73 close(fd);
74+ if (output != stdout)
75+ fclose(output);
76 return 0;
77 }

Subscribers

People subscribed via source and target branches