Support any encoding available here, mostly.
[apps/madtty.git] / madtty / madtty.c
index e6d9dde..2b5fae2 100644 (file)
 
 #include "madtty.h"
 
+static char const * const keytable[KEY_MAX+1] = {
+    ['\n']          = "\r",
+    [KEY_UP]        = "\e[A",
+    [KEY_DOWN]      = "\e[B",
+    [KEY_RIGHT]     = "\e[C",
+    [KEY_LEFT]      = "\e[D",
+    [KEY_BACKSPACE] = "\b",
+    [KEY_HOME]      = "\e[1~",
+    [KEY_IC]        = "\e[2~",
+    [KEY_DC]        = "\e[3~",
+    [KEY_END]       = "\e[4~",
+    [KEY_PPAGE]     = "\e[5~",
+    [KEY_NPAGE]     = "\e[6~",
+    [KEY_SUSPEND]   = "\x1A",  /* Ctrl+Z gets mapped to this */
+    [KEY_F(1)]      = "\e[[A",
+    [KEY_F(2)]      = "\e[[B",
+    [KEY_F(3)]      = "\e[[C",
+    [KEY_F(4)]      = "\e[[D",
+    [KEY_F(5)]      = "\e[[E",
+    [KEY_F(6)]      = "\e[17~",
+    [KEY_F(7)]      = "\e[18~",
+    [KEY_F(8)]      = "\e[19~",
+    [KEY_F(9)]      = "\e[20~",
+    [KEY_F(10)]     = "\e[21~",
+};
+
+static void mtty_row_set(mtty_row_t *row, int start, int len, uint16_t attr)
+{
+    wmemset(row->text + start, 0, len);
+    for (int i = start; i < len + start; i++) {
+        row->attr[i] = attr;
+    }
+}
+
+__attribute__((noinline))
+static void mtty_row_roll(mtty_row_t *start, mtty_row_t *end, int count)
+{
+    int n = end - start;
+
+    count %= n;
+    if (count < 0)
+        count += n;
+
+    if (count) {
+        mtty_row_t *buf = alloca(count * sizeof(mtty_row_t));
+
+        memcpy(buf, start, count * sizeof(mtty_row_t));
+        memmove(start, start + count, (n - count) * sizeof(mtty_row_t));
+        memcpy(end - count, buf, count * sizeof(mtty_row_t));
+    }
+}
+
 static void clamp_cursor_to_bounds(madtty_t *rt)
 {
-    if (rt->curs_row < 0) {
-        rt->curs_row = 0;
+    if (rt->curs_row < rt->lines) {
+        rt->curs_row = rt->lines;
     }
-    if (rt->curs_col < 0) {
-        rt->curs_col = 0;
+    if (rt->curs_row >= rt->lines + rt->rows) {
+        rt->curs_row = rt->lines + rt->rows - 1;
     }
 
-    if (rt->curs_row >= rt->rows) {
-        rt->curs_row = rt->rows - 1;
+    if (rt->curs_col < 0) {
+        rt->curs_col = 0;
     }
     if (rt->curs_col >= rt->cols) {
         rt->curs_col = rt->cols - 1;
@@ -50,70 +102,54 @@ static void clamp_cursor_to_bounds(madtty_t *rt)
 
 static void cursor_line_down(madtty_t *rt)
 {
-    int i;
-
     rt->curs_row++;
-    if (rt->curs_row <= rt->scrollbottom)
+    if (rt->curs_row < rt->scroll_bot)
         return;
 
-    /* must scroll the scrolling region up by 1 line, and put cursor on
-     * last line of it */
-    rt->curs_row = rt->scrollbottom;
-
-    for (i = rt->scrolltop; i < rt->scrollbottom; i++) {
-        memcpy(rt->cells[i], rt->cells[i+1], sizeof(RoteCell) * rt->cols);
-    }
-
-    /* clear last row of the scrolling region */
-    for (i = 0; i < rt->cols; i++) {
-        rt->cells[rt->scrollbottom][i].s[0]  = 0x20;
-        rt->cells[rt->scrollbottom][i].len   = 1;
-        rt->cells[rt->scrollbottom][i].attrs = A_NORMAL;
-    }
+    rt->curs_row = rt->scroll_bot - 1;
+    mtty_row_roll(rt->scroll_top, rt->scroll_bot, 1);
+    mtty_row_set(rt->curs_row, 0, rt->cols, 0);
 }
 
 static void cursor_line_up(madtty_t *rt)
 {
-    int i;
-
     rt->curs_row--;
-    if (rt->curs_row >= rt->scrolltop)
+    if (rt->curs_row >= rt->scroll_top)
         return;
 
     /* must scroll the scrolling region up by 1 line, and put cursor on
      * first line of it */
-    rt->curs_row = rt->scrolltop;
-
-    for (i = rt->scrollbottom; i > rt->scrolltop; i--) {
-        memcpy(rt->cells[i], rt->cells[i-1], sizeof(RoteCell) * rt->cols);
-    }
+    rt->curs_row = rt->scroll_top;
+    mtty_row_roll(rt->scroll_top, rt->scroll_bot, -1);
+    mtty_row_set(rt->curs_row, 0, rt->cols, 0);
+}
 
-    /* clear first row of the scrolling region */
-    for (i = 0; i < rt->cols; i++) {
-        rt->cells[rt->scrolltop][i].s[0]  = 0x20;
-        rt->cells[rt->scrolltop][i].len   = 1;
-        rt->cells[rt->scrolltop][i].attrs = A_NORMAL;
-    }
+static uint16_t build_attrs(unsigned curattrs)
+{
+    return ((curattrs & ~A_COLOR) | COLOR_PAIR(curattrs & 0xff))
+        >> NCURSES_ATTR_SHIFT;
 }
 
-static void put_normal_char(madtty_t *rt, const char *s, int len)
+static void put_normal_char(madtty_t *rt, wchar_t c)
 {
+    mtty_row_t *tmp;
+
     if (rt->curs_col >= rt->cols) {
         rt->curs_col = 0;
         cursor_line_down(rt);
     }
 
-    if (rt->insert) {
-        int i;
+    tmp = rt->curs_row;
 
-        for (i = rt->cols - 1; i >= rt->curs_col+1; i--) {
-            rt->cells[rt->curs_row][i] = rt->cells[rt->curs_row][i-1];
-        }
+    if (rt->insert) {
+        wmemmove(tmp->text + rt->curs_col + 1, tmp->text + rt->curs_col,
+                 (rt->cols - rt->curs_col - 1));
+        memmove(tmp->attr + rt->curs_col + 1, tmp->attr + rt->curs_col,
+                (rt->cols - rt->curs_col - 1) * sizeof(tmp->attr[0]));
     }
 
-    memcpy(rt->cells[rt->curs_row][rt->curs_col].s, s, len);
-    rt->cells[rt->curs_row][rt->curs_col].len   = len;
-    rt->cells[rt->curs_row][rt->curs_col].attrs = rt->curattrs;
+    tmp->text[rt->curs_col] = c;
+    tmp->attr[rt->curs_col] = build_attrs(rt->curattrs);
     rt->curs_col++;
 }
 
@@ -131,7 +167,7 @@ static void put_graphmode_char(madtty_t *rt, int c)
         nc = '%';
     }
 
-    put_normal_char(rt, &nc, 1);
+    put_normal_char(rt, nc);
 }
 
 static void new_escape_sequence(madtty_t *rt)
@@ -217,34 +253,13 @@ static void interpret_csi_SGR(madtty_t *rt, int param[], int pcount)
     }
 
     for (i = 0; i < pcount; i++) {
-
-        // From http://vt100.net/docs/vt510-rm/SGR table 5-16
-        // 0   All attributes off
-        // 1   Bold
-        // 4   Underline
-        // 5   Blinking
-        // 7   Negative image
-        // 8   Invisible image
-        // 10  The ASCII character set is the current 7-bit
-        //     display character set (default) - SCO Console only.
-        // 11  Map Hex 00-7F of the PC character set codes
-        //     to the current 7-bit display character set
-        //     - SCO Console only.
-        // 12  Map Hex 80-FF of the current character set to
-        //     the current 7-bit display character set - SCO
-        //     Console only.
-        // 22  Bold off
-        // 24  Underline off
-        // 25  Blinking off
-        // 27  Negative image off
-        // 28  Invisible image off
-
         switch (param[i]) {
 #define CASE(x, op)  case x: op; break
             CASE(0,  rt->curattrs = A_NORMAL);
             CASE(1,  rt->curattrs |= A_BOLD);
             CASE(4,  rt->curattrs |= A_UNDERLINE);
             CASE(5,  rt->curattrs |= A_BLINK);
+            CASE(6,  rt->curattrs |= A_BLINK);
             CASE(7,  rt->curattrs |= A_REVERSE);
             CASE(8,  rt->curattrs |= A_INVIS);
             CASE(22, rt->curattrs &= ~A_BOLD);
@@ -254,21 +269,21 @@ static void interpret_csi_SGR(madtty_t *rt, int param[], int pcount)
             CASE(28, rt->curattrs &= ~A_INVIS);
 
           case 30 ... 37:
-            rt->curattrs &= ~070;
-            rt->curattrs |= (param[i] - 30) << 3;
+            rt->curattrs &= ~0xf0;
+            rt->curattrs |= (param[i] - 29) << 4;
             break;
 
-          case 40 ... 47:
-            rt->curattrs &= ~007;
-            rt->curattrs |= (param[i] - 40);
+          case 39:
+            rt->curattrs &= ~0xf0;
             break;
 
-          case 39:
-            rt->curattrs &= ~070;
+          case 40 ... 47:
+            rt->curattrs &= ~0x0f;
+            rt->curattrs |= (param[i] - 39);
             break;
 
           case 49:
-            rt->curattrs &= ~007;
+            rt->curattrs &= ~0x0f;
             break;
 
           default:
@@ -280,7 +295,7 @@ static void interpret_csi_SGR(madtty_t *rt, int param[], int pcount)
 /* interprets an 'erase display' (ED) escape sequence */
 static void interpret_csi_ED(madtty_t *rt, int param[], int pcount)
 {
-    int r, c;
+    int r;
     int start_row, start_col, end_row, end_col;
 
     /* decide range */
@@ -293,26 +308,23 @@ static void interpret_csi_ED(madtty_t *rt, int param[], int pcount)
     if (pcount && param[0] == 1) {
         start_row = 0;
         start_col = 0;
-        end_row = rt->curs_row;
+        end_row = rt->curs_row - rt->lines;
         end_col = rt->curs_col;
     } else {
-        start_row = rt->curs_row;
+        start_row = rt->curs_row - rt->lines;
         start_col = rt->curs_col;
         end_row = rt->rows - 1;
         end_col = rt->cols - 1;
     }
 
     /* clean range */
-    for (r = start_row; r <= end_row; r++) {
-        for (c = (r == start_row ? start_col : 0);
-             c <= (r == end_row ? end_col : rt->cols - 1);
-             c++)
-        {
-            rt->cells[r][c].s[0]  = 0x20;
-            rt->cells[r][c].len   = 1;
-            rt->cells[r][c].attrs = rt->curattrs;
-        }
+    mtty_row_set(rt->lines + start_row, start_col, rt->cols - start_col,
+                 build_attrs(rt->curattrs));
+    for (r = start_row + 1; r < end_row; r++) {
+        mtty_row_set(rt->lines + r, 0, rt->cols, build_attrs(rt->curattrs));
     }
+    mtty_row_set(rt->lines + end_row, 0, end_col + 1,
+                 build_attrs(rt->curattrs));
 }
 
 /* interprets a 'move cursor' (CUP) escape sequence */
@@ -320,14 +332,15 @@ static void interpret_csi_CUP(madtty_t *rt, int param[], int pcount)
 {
     if (pcount == 0) {
         /* special case */
-        rt->curs_row = rt->curs_col = 0;
+        rt->curs_row = rt->lines;
+        rt->curs_col = 0;
         return;
     } else
     if (pcount < 2) {
         return;  /* malformed */
     }
 
-    rt->curs_row = param[0] - 1;  /* convert from 1-based to 0-based */
+    rt->curs_row = rt->lines + param[0] - 1;  /* convert from 1-based to 0-based */
     rt->curs_col = param[1] - 1;  /* convert from 1-based to 0-based */
 
     clamp_cursor_to_bounds(rt);
@@ -347,7 +360,7 @@ static void interpret_csi_C(madtty_t *rt, char verb, int param[], int pcount)
       case 'E':           rt->curs_row += n; rt->curs_col = 0; break;
       case 'F':           rt->curs_row -= n; rt->curs_col = 0; break;
       case 'G': case '`': rt->curs_col  = param[0] - 1; break;
-      case 'd':           rt->curs_row  = param[0] - 1; break;
+      case 'd':           rt->curs_row  = rt->lines + param[0] - 1; break;
     }
 
     clamp_cursor_to_bounds(rt);
@@ -356,52 +369,56 @@ static void interpret_csi_C(madtty_t *rt, char verb, int param[], int pcount)
 /* Interpret the 'erase line' escape sequence */
 static void interpret_csi_EL(madtty_t *rt, int param[], int pcount)
 {
-    int erase_start, erase_end, i;
+    int start, len;
     int cmd = pcount ? param[0] : 0;
 
     switch (cmd) {
-      case 1:  erase_start = 0;        erase_end = rt->curs_col;     break;
-      case 2:  erase_start = 0;        erase_end = rt->cols - 1; break;
-      default: erase_start = rt->curs_col; erase_end = rt->cols - 1; break;
+      case 1:
+        start = 0;
+        len   = rt->curs_col + 1;
+        break;
+      case 2:
+        start = 0;
+        len   = rt->cols;
+        break;
+      default:
+        start = rt->curs_col;
+        len   = rt->cols - start;
+        break;
     }
 
-    for (i = erase_start; i <= erase_end; i++) {
-        rt->cells[rt->curs_row][i].s[0]  = 0x20;
-        rt->cells[rt->curs_row][i].len   = 1;
-        rt->cells[rt->curs_row][i].attrs = rt->curattrs;
-    }
+    mtty_row_set(rt->curs_row, start, len, build_attrs(rt->curattrs));
 }
 
 /* Interpret the 'insert blanks' sequence (ICH) */
 static void interpret_csi_ICH(madtty_t *rt, int param[], int pcount)
 {
+    mtty_row_t *row = rt->curs_row;
     int n = (pcount && param[0] > 0) ? param[0] : 1;
     int i;
 
     for (i = rt->cols - 1; i >= rt->curs_col + n; i--) {
-        rt->cells[rt->curs_row][i] = rt->cells[rt->curs_row][i - n];
+        row->text[i] = row->text[i - n];
+        row->attr[i] = row->attr[i - n];
     }
 
-    for (i = rt->curs_col; i < rt->curs_col + n; i++) {
-        rt->cells[rt->curs_row][i].s[0]  = 0x20;
-        rt->cells[rt->curs_row][i].len   = 1;
-        rt->cells[rt->curs_row][i].attrs = rt->curattrs;
-    }
+    mtty_row_set(row, rt->curs_col, n, build_attrs(rt->curattrs));
 }
 
 /* Interpret the 'delete chars' sequence (DCH) */
 static void interpret_csi_DCH(madtty_t *rt, int param[], int pcount)
 {
+    mtty_row_t *row = rt->curs_row;
     int n = (pcount && param[0] > 0) ? param[0] : 1;
     int i;
 
     for (i = rt->curs_col; i < rt->cols; i++) {
         if (i + n < rt->cols) {
-            rt->cells[rt->curs_row][i] = rt->cells[rt->curs_row][i + n];
+            row->text[i] = row->text[i + n];
+            row->attr[i] = row->attr[i + n];
         } else {
-            rt->cells[rt->curs_row][i].s[0]  = 0x20;
-            rt->cells[rt->curs_row][i].len   = 1;
-            rt->cells[rt->curs_row][i].attrs = rt->curattrs;
+            row->text[i] = 0;
+            row->attr[i] = build_attrs(rt->curattrs);
         }
     }
 }
@@ -410,17 +427,15 @@ static void interpret_csi_DCH(madtty_t *rt, int param[], int pcount)
 static void interpret_csi_IL(madtty_t *rt, int param[], int pcount)
 {
     int n = (pcount && param[0] > 0) ? param[0] : 1;
-    int i, j;
-
-    for (i = rt->scrollbottom; i >= rt->curs_row + n; i--) {
-        memcpy(rt->cells[i], rt->cells[i - n], sizeof(RoteCell) * rt->cols);
-    }
 
-    for (i = rt->curs_row; i < rt->curs_row + n && i <= rt->scrollbottom; i++) {
-        for (j = 0; j < rt->cols; j++) {
-            rt->cells[i][j].s[0]  = 0x20;
-            rt->cells[i][j].len   = 1;
-            rt->cells[i][j].attrs = rt->curattrs;
+    if (rt->curs_row + n >= rt->scroll_bot) {
+        for (mtty_row_t *row = rt->curs_row; row < rt->scroll_bot; row++) {
+            mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
+        }
+    } else {
+        mtty_row_roll(rt->curs_row, rt->scroll_bot, n);
+        for (mtty_row_t *row = rt->curs_row; row < rt->curs_row + n; row++) {
+            mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
         }
     }
 
@@ -430,17 +445,16 @@ static void interpret_csi_IL(madtty_t *rt, int param[], int pcount)
 static void interpret_csi_DL(madtty_t *rt, int param[], int pcount)
 {
     int n = (pcount && param[0] > 0) ? param[0] : 1;
-    int i, j;
 
-    for (i = rt->curs_row; i <= rt->scrollbottom; i++) {
-        if (i + n <= rt->scrollbottom) {
-            memcpy(rt->cells[i], rt->cells[i+n], sizeof(RoteCell) * rt->cols);
-        } else {
-            for (j = 0; j < rt->cols; j++) {
-                rt->cells[i][j].s[0]  = 0x20;
-                rt->cells[i][j].len   = 1;
-                rt->cells[i][j].attrs = rt->curattrs;
-            }
+    if (rt->curs_row + n >= rt->scroll_bot) {
+        for (mtty_row_t *row = rt->curs_row; row < rt->scroll_bot; row++) {
+            mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
+        }
+    } else {
+        mtty_row_roll(rt->curs_row, rt->scroll_bot, -n);
+
+        for (mtty_row_t *row = rt->scroll_bot - n; row < rt->scroll_bot; row++) {
+            mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
         }
     }
 }
@@ -449,46 +463,47 @@ static void interpret_csi_DL(madtty_t *rt, int param[], int pcount)
 static void interpret_csi_ECH(madtty_t *rt, int param[], int pcount)
 {
     int n = (pcount && param[0] > 0) ? param[0] : 1;
-    int i;
 
-    for (i = rt->curs_col; i < rt->curs_col + n && i < rt->cols; i++) {
-        rt->cells[rt->curs_row][i].s[0]  = 0x20;
-        rt->cells[rt->curs_row][i].len   = 1;
-        rt->cells[rt->curs_row][i].attrs = rt->curattrs;
+    if (rt->curs_col + n < rt->cols) {
+        n = rt->cols - rt->curs_col;
     }
+    mtty_row_set(rt->curs_row, rt->curs_col, n, build_attrs(rt->curattrs));
 }
 
 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
 static void interpret_csi_DECSTBM(madtty_t *rt, int param[], int pcount)
 {
-    int newtop, newbottom;
+    int new_top, new_bot;
 
-    if (!pcount) {
-        newtop = 0;
-        newbottom = rt->rows - 1;
-    } else
-    if (pcount < 2) {
+    switch (pcount) {
+      case 0:
+        rt->scroll_top = rt->lines;
+        rt->scroll_bot = rt->lines + rt->rows;
+        break;
+      default:
         return; /* malformed */
-    }
 
-    newtop = param[0] - 1;
-    newbottom = param[1] - 1;
-
-    /* clamp to bounds */
-    if (newtop < 0)
-        newtop = 0;
-    if (newtop >= rt->rows)
-        newtop = rt->rows - 1;
-    if (newbottom < 0)
-        newbottom = 0;
-    if (newbottom >= rt->rows)
-        newbottom = rt->rows - 1;
-
-    /* check for range validity */
-    if (newtop > newbottom)
-        return;
-    rt->scrolltop = newtop;
-    rt->scrollbottom = newbottom;
+      case 2:
+        new_top = param[0] - 1;
+        new_bot = param[1] - 1;
+
+        /* clamp to bounds */
+        if (new_top < 0)
+            new_top = 0;
+        if (new_top >= rt->rows)
+            new_top = rt->rows - 1;
+        if (new_bot < 0)
+            new_bot = 0;
+        if (new_bot >= rt->rows)
+            new_bot = rt->rows;
+
+        /* check for range validity */
+        if (new_top < new_bot) {
+            rt->scroll_top = rt->lines + new_top;
+            rt->scroll_bot = rt->lines + new_bot;
+        }
+        break;
+    }
 }
 
 static void es_interpret_csi(madtty_t *rt)
@@ -551,12 +566,13 @@ static void es_interpret_csi(madtty_t *rt)
       case 'r': /* set scrolling region */
         interpret_csi_DECSTBM(rt, csiparam, param_count); break;
       case 's': /* save cursor location */
-        rt->curs_srow = rt->curs_col;
-        rt->curs_scol = rt->curs_row;
+        rt->curs_srow = rt->curs_row - rt->lines;
+        rt->curs_scol = rt->curs_col;
         break;
       case 'u': /* restore cursor location */
-        rt->curs_col = rt->curs_srow;
-        rt->curs_row = rt->curs_scol;
+        rt->curs_row = rt->lines + rt->curs_srow;
+        rt->curs_col = rt->curs_scol;
+        clamp_cursor_to_bounds(rt);
         break;
       default:
         break;
@@ -600,38 +616,57 @@ static void try_interpret_escape_seq(madtty_t *rt)
         cancel_escape_sequence(rt);
 }
 
-int madtty_inject(madtty_t *rt, const char *data, int len)
+int madtty_process(madtty_t *rt)
 {
-    int pos;
+    int res, pos = 0;
+
+    if (rt->pty < 0) {
+        errno = EINVAL;
+        return -1;
+    }
+
+    res = read(rt->pty, rt->rbuf + rt->rbuf_len,
+               sizeof(rt->rbuf) - rt->rbuf_len);
+    if (res < 0)
+        return -1;
 
-    for (pos = 0; pos < len; pos++) {
-        if ((unsigned char)data[pos] <= 31) {
-            handle_control_char(rt, data[pos]);
+    rt->rbuf_len += res;
+    while (pos < rt->rbuf_len) {
+        wchar_t wc;
+        ssize_t len;
+
+        len = (ssize_t)mbrtowc(&wc, rt->rbuf + pos, rt->rbuf_len - pos,
+                               &rt->ps);
+        if (len < 0) {
+            rt->rbuf_len -= pos;
+            memmove(rt->rbuf, rt->rbuf + pos, rt->rbuf_len);
+            return len == -2 ? 0 : -1;
+        }
+
+        pos += len ? len : 1;
+
+        if (wc < ' ') {
+            handle_control_char(rt, wc);
             continue;
         }
 
         if (rt->escaped && rt->esbuf_len < ESEQ_BUF_SIZE) {
             /* append character to ongoing escape sequence */
-            rt->esbuf[rt->esbuf_len] = data[pos];
+            rt->esbuf[rt->esbuf_len]   = wc;
             rt->esbuf[++rt->esbuf_len] = 0;
 
             try_interpret_escape_seq(rt);
         } else
         if (rt->graphmode) {
-            put_graphmode_char(rt, data[pos]);
+            put_graphmode_char(rt, wc);
         } else {
-            static int8_t const lens[5] = { 1, -1, 2, 3, 4 };
-            int bsf = __builtin_clz(~((unsigned char)data[pos] << 24));
-
-            if (pos + lens[bsf] > len)
-                return pos;
-
-            put_normal_char(rt, data + pos, lens[bsf]);
-            pos += lens[bsf] - 1;
+            put_normal_char(rt, wc);
         }
     }
 
-    return len;
+    rt->rbuf_len -= pos;
+    memmove(rt->rbuf, rt->rbuf + pos, rt->rbuf_len);
+    return 0;
 }
 
 madtty_t *madtty_create(int rows, int cols)
@@ -654,20 +689,22 @@ madtty_t *madtty_create(int rows, int cols)
     rt->insert = false; 
 
     /* create the cell matrix */
-    rt->cells = (RoteCell**)calloc(sizeof(RoteCell*), rt->rows);
+    rt->lines = (mtty_row_t*)calloc(sizeof(mtty_row_t), rt->rows);
     for (i = 0; i < rt->rows; i++) {
-        rt->cells[i] = (RoteCell*)calloc(sizeof(RoteCell), rt->cols);
+        rt->lines[i].text = (wchar_t *)calloc(sizeof(wchar_t), rt->cols);
+        rt->lines[i].attr = (uint16_t *)calloc(sizeof(uint16_t), rt->cols);
     }
 
+    rt->pty = -1;  /* no pty for now */
+
     /* initialization of other public fields */
-    rt->curs_row = rt->curs_col = 0;
+    rt->curs_row = rt->lines;
+    rt->curs_col = 0;
     rt->curattrs = A_NORMAL;  /* white text over black background */
 
-    rt->pty = -1;  /* no pty for now */
-
     /* initial scrolling area is the whole window */
-    rt->scrolltop = 0;
-    rt->scrollbottom = rt->rows - 1;
+    rt->scroll_top = rt->lines;
+    rt->scroll_bot = rt->lines + rt->rows;
 
     return rt;
 }
@@ -679,9 +716,10 @@ void madtty_destroy(madtty_t *rt)
         return;
 
     for (i = 0; i < rt->rows; i++) {
-        free(rt->cells[i]);
+        free(rt->lines[i].text);
+        free(rt->lines[i].attr);
     }
-    free(rt->cells);
+    free(rt->lines);
     free(rt);
 }
 
@@ -690,18 +728,25 @@ void madtty_draw(madtty_t *rt, WINDOW *win, int srow, int scol)
     int i, j;
 
     for (i = 0; i < rt->rows; i++) {
+        mtty_row_t *row = rt->lines + i;
         wmove(win, srow + i, scol);
+
         for (j = 0; j < rt->cols; j++) {
-            wattrset(win, (rt->cells[i][j].attrs & ~077) | COLOR_PAIR(rt->cells[i][j].attrs & 077));
-            if (rt->cells[i][j].len && rt->cells[i][j].s[0] >= ' ') {
-                waddnstr(win, rt->cells[i][j].s, rt->cells[i][j].len);
+            if (!j || row->attr[j] != row->attr[j - 1])
+                wattrset(win, (attr_t)row->attr[j] << NCURSES_ATTR_SHIFT);
+            if (row->text[j] >= 128) {
+                char buf[MB_CUR_MAX + 1];
+                int len;
+
+                len = wcrtomb(buf, row->text[j], NULL);
+                waddnstr(win, buf, len);
             } else {
-                waddch(win, ' ');
+                waddch(win, row->text[j] > ' ' ? row->text[j] : ' ');
             }
         }
     }
 
-    wmove(win, srow + rt->curs_row, scol + rt->curs_col);
+    wmove(win, srow + rt->curs_row - rt->lines, scol + rt->curs_col);
 }
 
 /******************************************************/
@@ -731,17 +776,7 @@ pid_t madtty_forkpty(madtty_t *rt, const char *path, const char *argv[])
     return rt->childpid = pid;
 }
 
-int madtty_read(madtty_t *rt, char *buf, int buflen)
-{
-    if (rt->pty < 0) {
-        errno = EINVAL;
-        return -1;
-    }
-
-    return read(rt->pty, buf, buflen);
-}
-
-int madtty_write(madtty_t *rt, const char *data, int len)
+static int madtty_write(madtty_t *rt, const char *data, int len)
 {
     int res;
 
@@ -760,32 +795,6 @@ again:
     return res;
 }
 
-static char const * const keytable[KEY_MAX+1] = {
-    ['\n']          = "\r",
-    [KEY_UP]        = "\e[A",
-    [KEY_DOWN]      = "\e[B",
-    [KEY_RIGHT]     = "\e[C",
-    [KEY_LEFT]      = "\e[D",
-    [KEY_BACKSPACE] = "\b",
-    [KEY_HOME]      = "\e[1~",
-    [KEY_IC]        = "\e[2~",
-    [KEY_DC]        = "\e[3~",
-    [KEY_END]       = "\e[4~",
-    [KEY_PPAGE]     = "\e[5~",
-    [KEY_NPAGE]     = "\e[6~",
-    [KEY_SUSPEND]   = "\x1A",  /* Ctrl+Z gets mapped to this */
-    [KEY_F(1)]      = "\e[[A",
-    [KEY_F(2)]      = "\e[[B",
-    [KEY_F(3)]      = "\e[[C",
-    [KEY_F(4)]      = "\e[[D",
-    [KEY_F(5)]      = "\e[[E",
-    [KEY_F(6)]      = "\e[17~",
-    [KEY_F(7)]      = "\e[18~",
-    [KEY_F(8)]      = "\e[19~",
-    [KEY_F(9)]      = "\e[20~",
-    [KEY_F(10)]     = "\e[21~",
-};
-
 void madtty_keypress(madtty_t *rt, int keycode)
 {
     char c = (char)keycode;
@@ -816,10 +825,14 @@ void madtty_initialize(void)
     initscr();
     noecho();
     start_color();
+    use_default_colors();
     raw();
     nodelay(stdscr, TRUE);
     keypad(stdscr, TRUE);
 
-    for (int i = 0; i < 8 * 8; i++)
-        init_pair(i, i >> 3, i & 7);
+    for (int i = -1; i < 8; i++) {
+        for (int j = -1; j < 8; j++) {
+            init_pair((i + 1) * 16 + j + 1, i, j);
+        }
+    }
 }