File: 0011-ttyplay-p-wait-for-input-with-inotify-if-available-C.patch

package info (click to toggle)
ttyrec 1.1.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 856 kB
  • sloc: ansic: 2,490; sh: 246; makefile: 49; perl: 48
file content (104 lines) | stat: -rw-r--r-- 2,887 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
From: =?utf-8?b?0L3QsNCx?= <nabijaczleweli@nabijaczleweli.xyz>
Date: Sat, 25 Jan 2025 18:20:26 +0100
Subject: ttyplay -p: wait for input with inotify,
 if available (Closes: #564826)

---
 configure | 13 +++++++++++++
 ttyplay.c | 38 +++++++++++++++++++++++++++++++++++---
 2 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/configure b/configure
index 34f912e..021083a 100755
--- a/configure
+++ b/configure
@@ -119,6 +119,19 @@ else
     echo "no"
 fi
 
+printf "%b" "Looking for inotify(7)... "
+cat >"$srcfile.c" <<EOF
+#include <sys/inotify.h>
+int main(void) { return inotify_init1(IN_NONBLOCK); }
+EOF
+if $CC "$srcfile.c" -o /dev/null >/dev/null 2>&1; then
+    echo "yes"
+    echo '#define HAVE_inotify' >>"$curdir/configure.h"
+    DEFINES_STR="$DEFINES_STR inotify"
+else
+    echo "no"
+fi
+
 printf "%b" "Looking for isastream()... "
 cat >"$srcfile.c" <<EOF
 #include <stropts.h>
diff --git a/ttyplay.c b/ttyplay.c
index 4602801..1580e4b 100644
--- a/ttyplay.c
+++ b/ttyplay.c
@@ -51,6 +51,9 @@
 #include "io.h"
 #include "compress.h"
 #include "configure.h"
+#ifdef HAVE_inotify
+#include <sys/inotify.h>
+#endif
 
 typedef double (*WaitFunc) (struct timeval prev,
                             struct timeval cur,
@@ -134,7 +137,7 @@ double ttywait(struct timeval prev, struct timeval cur, double speed)
      */
     struct timeval orig_diff = diff;
 
-    int r = select(1, &readfs, NULL, NULL, diffp); /* skip if a user hits any key */
+    int r = select(STDIN_FILENO + 1, &readfs, NULL, NULL, diffp); /* skip if a user hits any key */
     diff = orig_diff;              /* Restore the original diff value. */
     if (r > 0 && FD_ISSET(0, &readfs))      /* a user hits a character? */
     {
@@ -231,15 +234,44 @@ err:
 }
 
 
+static int inotify = -1;
 int ttypread(FILE *fp, Header *h, char **buf)
 {
+    fd_set readfs;
+    FD_ZERO(&readfs);
+
+    char tmp[4096];
+#ifdef HAVE_inotify
+    if (inotify == -1) {
+        inotify = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
+        if (inotify == -1)
+            inotify = -2;
+        else {
+            sprintf(tmp, "/dev/fd/%d", fileno(fp));;
+            if (inotify_add_watch(inotify, tmp, IN_MODIFY) == -1) {
+                close(inotify);
+                inotify = -2;
+            }
+
+        }
+    }
+#endif
+    if (inotify >= 0) {
+        FD_SET(inotify, &readfs);
+    }
+
     /*
      * Read persistently just like tail -f.
      */
     while (ttyread(fp, h, buf) == 0)
     {
-        struct timeval w = { 0, 250000 };
-        select(0, NULL, NULL, NULL, &w);
+        if (inotify < 0) {
+            struct timeval w = { 0, 250000 };
+            select(0, NULL, NULL, NULL, &w);
+        } else {
+            if (select(inotify + 1, &readfs, NULL, NULL, NULL) == 1)
+                read(inotify, tmp, sizeof(tmp));
+        }
         clearerr(fp);
     }
     return 1;