Fix resize bug.
[apps/madtty.git] / madtty / madtty.c
index 8994454..251780f 100644 (file)
@@ -24,7 +24,6 @@
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <pty.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <termios.h>
 #include <wchar.h>
-
+#ifdef __linux__
+# include <pty.h>
+#elif defined(__FreeBSD__)
+# include <libutil.h>
+#elif defined(__OpenBSD__)
+# include <util.h>
+#endif
 #include "madtty.h"
 
+#ifndef NCURSES_ATTR_SHIFT
+# define NCURSES_ATTR_SHIFT 8
+#endif
+
 #define IS_CONTROL(ch) !((ch) & 0xffffff60UL)
 
 static int has_default = 0;
@@ -612,6 +621,17 @@ static void try_interpret_escape_seq(madtty_t *t)
 
     if (t->elen + 1 >= (int)sizeof(t->ebuf)) {
 cancel:
+#ifndef NDEBUG
+        fprintf(stderr, "cancelled: \\033");
+        for (int i = 0; i < (int)t->elen; i++) {
+            if (isprint(t->ebuf[i])) {
+                fputc(t->ebuf[i], stderr);
+            } else {
+                fprintf(stderr, "\\%03o", t->ebuf[i]);
+            }
+        }
+        fputc('\n', stderr);
+#endif
         cancel_escape_sequence(t);
     }
 }
@@ -836,6 +856,8 @@ void madtty_resize(madtty_t *t, int rows, int cols)
             lines[row].attr = realloc(lines[row].attr, sizeof(uint16_t) * cols);
             if (t->cols < cols)
                 t_row_set(lines + row, t->cols, cols - t->cols, 0);
+            else
+                lines[row].dirty = true;
         }
         t->cols = cols;
     }
@@ -843,14 +865,13 @@ void madtty_resize(madtty_t *t, int rows, int cols)
     while (t->rows < rows) {
         lines[t->rows].text = (wchar_t *)calloc(sizeof(wchar_t), cols);
         lines[t->rows].attr = (uint16_t *)calloc(sizeof(uint16_t), cols);
+        t_row_set(lines + t->rows, 0, t->cols, 0);
         t->rows++;
     }
 
     t->curs_row   += lines - t->lines;
-    t->scroll_top += lines - t->lines;
-    t->scroll_bot += lines - t->lines;
-    if (t->scroll_bot > lines + t->rows)
-        t->scroll_bot = lines + t->rows;
+    t->scroll_top = lines;
+    t->scroll_bot = lines + rows;
     t->lines = lines;
     clamp_cursor_to_bounds(t);
     ioctl(t->pty, TIOCSWINSZ, &ws);