]> skyeroc.xyz Git - dgamelaunch/commitdiff
Allow resizing the terminal while watching a game. This probably won't work everywher...
authorPasi Kallinen <paxed@alt.org>
Sun, 18 Oct 2009 11:44:47 +0000 (11:44 +0000)
committerPasi Kallinen <paxed@alt.org>
Sun, 18 Oct 2009 11:44:47 +0000 (11:44 +0000)
git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@485 db0b04b0-f4d1-0310-9a6d-de3e77497b0e

ttyplay.c

index 176fcd91b8d8561fd3b13ad3e5588e5680e323f5..3d80f56e693d60aac7822039d940d0e52ff02090 100644 (file)
--- a/ttyplay.c
+++ b/ttyplay.c
@@ -33,6 +33,8 @@
 
 #include "config.h"
 
+#define _GNU_SOURCE /* need sighandler_t */
+
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/stat.h>
@@ -47,6 +49,8 @@
 #include <termios.h>
 #include <string.h>
 #include <curses.h>
+#include <signal.h>
+#include <errno.h>
 
 #include "dgamelaunch.h"
 #include "ttyplay.h"
 #include "stripgfx.h"
 
 int stripped = NO_GRAPHICS;
+static int got_sigwinch = 0;
+
+void
+ttyplay_sigwinch_func(int sig)
+{
+    signal(SIGWINCH, ttyplay_sigwinch_func);
+    got_sigwinch = 1;
+}
 
 struct timeval
 timeval_diff (struct timeval tv1, struct timeval tv2)
@@ -153,6 +165,7 @@ ttypread (FILE * fp, Header * h, char **buf, int pread)
   fd_set readfs;
   struct termios t;
   int doread = 0;
+  static int tried_resize = 0;
 
 #ifdef HAVE_KQUEUE
   if (kq == -1)
@@ -215,8 +228,13 @@ ttypread (FILE * fp, Header * h, char **buf, int pread)
       }
       if (n == -1)
        {
-         printf("select()/kevent() failed.\n");
-         exit (1);
+           if ((errno == EINTR) && got_sigwinch) {
+               got_sigwinch = 0;
+               return READ_RESTART;
+           } else {
+               printf("select()/kevent() failed.\n");
+               exit (1);
+           }
        }
       if (doread)
         {                       /* user hits a character? */
@@ -433,6 +451,7 @@ ttyplay_main (char *ttyfile, int mode)
   WaitFunc wait_func = ttywait;
   FILE *input = stdin;
   struct termios old, new;
+  sighandler_t old_sigwinch;
 
   populate_gfx_array (stripped);
 
@@ -445,6 +464,9 @@ ttyplay_main (char *ttyfile, int mode)
   new.c_cc[VTIME] = 0;
   tcsetattr (0, TCSANOW, &new); /* Make it current */
 
+  got_sigwinch = 0;
+  old_sigwinch = signal(SIGWINCH, ttyplay_sigwinch_func);
+
   if (mode == 1)
     ttypeek (input, speed);
   else
@@ -453,5 +475,8 @@ ttyplay_main (char *ttyfile, int mode)
   tcsetattr (0, TCSANOW, &old); /* Return terminal state */
   fclose (input);
 
+  if (old_sigwinch != SIG_ERR)
+      signal(SIGWINCH, old_sigwinch);
+
   return 0;
 }