Split out a struct timeval version of timeout_to_ms.
authorTimo Korvola <tkorvola@iki.fi>
Sun, 11 Feb 2018 15:58:46 +0000 (17:58 +0200)
committerTimo Korvola <tkorvola@iki.fi>
Sun, 11 Feb 2018 15:58:46 +0000 (17:58 +0200)
Planning to move to select for timeout handling, and select uses
struct timeval.

#42: Handle terminal resizing

src/curses/ux_input.c

index 7d8be5c585570baf11f774c6b93ad266e6e23732..8a8525df867851a5f33a3958132fd031340d77de 100644 (file)
@@ -87,28 +87,43 @@ static void unix_set_global_timeout(int timeout)
 
 
 /*
- * timeout_to_ms
- *
- * This returns the number of milliseconds until the input timeout
- * elapses or zero if it has already elapsed.  -1 is returned if no
- * timeout is in effect, otherwise the return value is non-negative.
+ * Time left until input timeout.  Return whether an input timeout
+ * is in effect and it it is, set diff to the time left until the
+ * timeout elapses or zero if it has already elapsed.  If false is returned,
+ * diff is not modified, otherwise it is set to a non-negative value.
  */
-static int timeout_to_ms()
+static bool timeout_left(struct timeval *diff)
 {
-    struct timeval now, diff;
+    struct timeval now;
 
-    if (global_timeout.tv_sec == 0) return -1;
+    if (global_timeout.tv_sec == 0)
+        return false;
     gettimeofday( &now, NULL);
-    diff.tv_usec = global_timeout.tv_usec - now.tv_usec;
-    if (diff.tv_usec < 0) {
+    diff->tv_usec = global_timeout.tv_usec - now.tv_usec;
+    if (diff->tv_usec < 0) {
        /* Carry */
        now.tv_sec++;
-       diff.tv_usec += 1000000;
+       diff->tv_usec += 1000000;
+    }
+    diff->tv_sec = global_timeout.tv_sec - now.tv_sec;
+    if (diff->tv_sec < 0) {
+        diff->tv_sec = diff->tv_usec = 0;
     }
-    diff.tv_sec = global_timeout.tv_sec - now.tv_sec;
-    if (diff.tv_sec < 0) return 0;
+    return true;
+}
+
+/*
+ * Time left until input timeout.  Return the number of milliseconds left
+ * until the input timeout elapses, zero if it has already elapsed, -1 if
+ * no timeout is in effect.
+ */
+static int timeout_to_ms()
+{
+    struct timeval diff;
+    if (!timeout_left(&diff))
+        return -1;
     if (diff.tv_sec >= INT_MAX / 1000 - 1) /* Paranoia... */
-       return INT_MAX - 1000;
+        return INT_MAX - 1000;
     return diff.tv_sec * 1000 + diff.tv_usec / 1000;
 }