X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=madtty%2Fmadtty.c;fp=madtty%2Fmadtty.c;h=8a3d26f4e3cc40571ce555edcaaf7c83b4a24f7b;hb=663accfc1b7feeb66176ed03b8c77d4adaa1bc21;hp=7b85f2766c49128857301495402c087d25d206fd;hpb=800cf6341f10d4c927c5ee77a5351b684a431ac1;p=apps%2Fmadtty.git diff --git a/madtty/madtty.c b/madtty/madtty.c index 7b85f27..8a3d26f 100644 --- a/madtty/madtty.c +++ b/madtty/madtty.c @@ -30,7 +30,9 @@ #include #include #include +#include #include +#include #include #include "madtty.h" @@ -800,6 +802,51 @@ madtty_t *madtty_create(int rows, int cols) 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; @@ -879,16 +926,6 @@ void madtty_keypress(madtty_t *rt, int keycode) 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]);