Support resizing
authorPierre Habouzit <madcoder@debian.org>
Sat, 10 Nov 2007 13:59:48 +0000 (14:59 +0100)
committerPierre Habouzit <madcoder@debian.org>
Sat, 10 Nov 2007 13:59:48 +0000 (14:59 +0100)
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
demo/boxshell.c
madtty/madtty.c
madtty/madtty.h

index 00a11a0..fa7d2ee 100644 (file)
@@ -7,6 +7,7 @@
 
 #include <ncurses.h>
 
 
 #include <ncurses.h>
 
+#include <fcntl.h>
 #include <signal.h>
 #include <stdio.h>
 #include <string.h>
 #include <signal.h>
 #include <stdio.h>
 #include <string.h>
@@ -17,7 +18,7 @@
 
 #include <madtty/madtty.h>
 
 
 #include <madtty/madtty.h>
 
-static unsigned char getout = 0;
+static int getout = 0, sigwinch = 0;
 static int screen_w, screen_h;
 static WINDOW *term_win;
 
 static int screen_w, screen_h;
 static WINDOW *term_win;
 
@@ -28,6 +29,7 @@ void handler(int signo)
         getout = 1;
         break;
       case SIGWINCH:
         getout = 1;
         break;
       case SIGWINCH:
+        sigwinch = 1;
         break;
     }
 }
         break;
     }
 }
@@ -45,7 +47,7 @@ int main(void)
 
     /* create a window with a frame */
     term_win = newwin(screen_h - 2, screen_w - 2, 1, 1);
 
     /* create a window with a frame */
     term_win = newwin(screen_h - 2, screen_w - 2, 1, 1);
-    rt = madtty_create(screen_h - 2, screen_w - 2);
+    rt = madtty_create(screen_h - 2, screen_w -2);
     {
         const char *path = getenv("SHELL") ?: "/bin/sh";
         const char *args[] = { path, "--login", NULL};
     {
         const char *path = getenv("SHELL") ?: "/bin/sh";
         const char *args[] = { path, "--login", NULL};
@@ -71,19 +73,32 @@ int main(void)
             }
         }
 
             }
         }
 
-        while ((ch = getch()) != ERR) {
-#if 0
-            if (ch == KEY_F(1)) {
-                struct winsize ws = {
-                    .ws_row = --rt->rows,
-                    .ws_col = --rt->cols,
-                };
-
-                erase();
-                ioctl(rt->pty, TIOCSWINSZ, &ws);
-                wresize(term_win, rt->rows, rt->cols);
+        if (sigwinch) {
+            int fd, cols = -1, rows = -1;
+            struct winsize w;
+
+            if ((fd = open("/dev/tty", O_RDONLY)) != -1) {
+                if (ioctl(fd, TIOCGWINSZ, &w) != -1) {
+                    rows = w.ws_row;
+                    cols = w.ws_col;
+                }
+                close(fd);
+            }
+            if (rows <= 0) {
+                rows = atoi(getenv("LINES") ?: "24");
+            }
+            if (cols <= 0) {
+                cols = atoi(getenv("COLUMNS") ?: "80");
             }
             }
-#endif
+
+            resizeterm(rows, cols);
+            madtty_resize(rt, rows - 2, cols - 2);
+            wresize(term_win, rows - 2, cols - 2);
+            sigwinch = 0;
+            erase();
+        }
+
+        while ((ch = getch()) != ERR) {
             madtty_keypress(rt, ch); /* pass the keypress for handling */
             dirty = 1;
         }
             madtty_keypress(rt, ch); /* pass the keypress for handling */
             dirty = 1;
         }
index 7b85f27..8a3d26f 100644 (file)
@@ -30,7 +30,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/ioctl.h>
 #include <sys/types.h>
 #include <sys/types.h>
+#include <termios.h>
 #include <wchar.h>
 
 #include "madtty.h"
 #include <wchar.h>
 
 #include "madtty.h"
@@ -800,6 +802,51 @@ madtty_t *madtty_create(int rows, int cols)
     return rt;
 }
 
     return rt;
 }
 
+void madtty_resize(madtty_t *rt, int rows, int cols)
+{
+    struct winsize ws = { .ws_row = rows, .ws_col = cols };
+    mtty_row_t *lines = rt->lines;
+
+    if (rows <= 0 || cols <= 0)
+        return;
+
+    if (rt->rows != rows) {
+        while (rt->rows > rows) {
+            free(lines[rt->rows - 1].text);
+            free(lines[rt->rows - 1].attr);
+            rt->rows--;
+        }
+
+        lines = realloc(lines, sizeof(mtty_row_t) * rows);
+    }
+
+    if (rt->cols != cols) {
+        for (int row = 0; row < rt->rows; row++) {
+            lines[row].text = realloc(lines[row].text, sizeof(wchar_t) * cols);
+            lines[row].attr = realloc(lines[row].attr, sizeof(uint16_t) * cols);
+            if (rt->cols < cols)
+                mtty_row_set(lines + row, rt->cols, cols - rt->cols, 0);
+        }
+        rt->cols = cols;
+    }
+
+    while (rt->rows < rows) {
+        lines[rt->rows].text = (wchar_t *)calloc(sizeof(wchar_t), cols);
+        lines[rt->rows].attr = (uint16_t *)calloc(sizeof(uint16_t), cols);
+        rt->rows++;
+    }
+
+    rt->curs_row   += lines - rt->lines;
+    rt->scroll_top += lines - rt->lines;
+    rt->scroll_bot += lines - rt->lines;
+    if (rt->scroll_bot > lines + rt->rows)
+        rt->scroll_bot = lines + rt->rows;
+    rt->lines = lines;
+    clamp_cursor_to_bounds(rt);
+    ioctl(rt->pty, TIOCSWINSZ, &ws);
+    kill(-rt->childpid, SIGWINCH);
+}
+
 void madtty_destroy(madtty_t *rt)
 {
     int i;
 void madtty_destroy(madtty_t *rt)
 {
     int i;
@@ -879,16 +926,6 @@ void madtty_keypress(madtty_t *rt, int keycode)
     const char *buf;
     int len;
 
     const char *buf;
     int len;
 
-#if 0
-    if (keycode == KEY_F(1)) {
-#define MIN(a, b) ((a < (b)) ? a : (b))
-        kill(-rt->childpid, SIGWINCH);
-        rt->scroll_bot = MIN(rt->scroll_bot, rt->lines + rt->rows);
-        rt->curs_row = MIN(rt->curs_row, rt->lines + rt->rows);
-        printf(stderr, "%d\n", rt->rows);
-        return;
-    }
-#endif
     if (keycode >= 0 && keycode < KEY_MAX && keytable[keycode]) {
         buf = keytable[keycode];
         len = strlen(keytable[keycode]);
     if (keycode >= 0 && keycode < KEY_MAX && keytable[keycode]) {
         buf = keytable[keycode];
         len = strlen(keytable[keycode]);
index c7e2a82..edecde9 100644 (file)
@@ -68,6 +68,7 @@ typedef struct {
 void madtty_initialize(void);
 
 madtty_t *madtty_create(int rows, int cols);
 void madtty_initialize(void);
 
 madtty_t *madtty_create(int rows, int cols);
+void madtty_resize(madtty_t *rt, int rows, int cols);
 void madtty_destroy(madtty_t *rt);
 pid_t madtty_forkpty(madtty_t *rt, const char *path, const char *argv[]);
 
 void madtty_destroy(madtty_t *rt);
 pid_t madtty_forkpty(madtty_t *rt, const char *path, const char *argv[]);