3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License (LGPL) as published by the Free Software Foundation.
7 Please refer to the COPYING file for more information.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 Copyright © 2004 Bruno T. C. de Oliveira
19 Copyright © 2006 Pierre Habouzit
30 #include <sys/ioctl.h>
34 static void clamp_cursor_to_bounds(madtty_t *rt)
36 if (rt->curs_row < 0) {
39 if (rt->curs_col < 0) {
43 if (rt->curs_row >= rt->rows) {
44 rt->curs_row = rt->rows - 1;
46 if (rt->curs_col >= rt->cols) {
47 rt->curs_col = rt->cols - 1;
51 static void cursor_line_down(madtty_t *rt)
56 if (rt->curs_row <= rt->scrollbottom)
59 /* must scroll the scrolling region up by 1 line, and put cursor on
61 rt->curs_row = rt->scrollbottom;
63 for (i = rt->scrolltop; i < rt->scrollbottom; i++) {
64 memcpy(rt->cells[i], rt->cells[i+1], sizeof(RoteCell) * rt->cols);
67 /* clear last row of the scrolling region */
68 for (i = 0; i < rt->cols; i++) {
69 rt->cells[rt->scrollbottom][i].s[0] = 0x20;
70 rt->cells[rt->scrollbottom][i].len = 1;
71 rt->cells[rt->scrollbottom][i].attrs = A_NORMAL;
75 static void cursor_line_up(madtty_t *rt)
80 if (rt->curs_row >= rt->scrolltop)
83 /* must scroll the scrolling region up by 1 line, and put cursor on
85 rt->curs_row = rt->scrolltop;
87 for (i = rt->scrollbottom; i > rt->scrolltop; i--) {
88 memcpy(rt->cells[i], rt->cells[i-1], sizeof(RoteCell) * rt->cols);
91 /* clear first row of the scrolling region */
92 for (i = 0; i < rt->cols; i++) {
93 rt->cells[rt->scrolltop][i].s[0] = 0x20;
94 rt->cells[rt->scrolltop][i].len = 1;
95 rt->cells[rt->scrolltop][i].attrs = A_NORMAL;
99 static void put_normal_char(madtty_t *rt, const char *s, int len)
101 if (rt->curs_col >= rt->cols) {
103 cursor_line_down(rt);
109 for (i = rt->cols - 1; i >= rt->curs_col+1; i--) {
110 rt->cells[rt->curs_row][i] = rt->cells[rt->curs_row][i-1];
114 memcpy(rt->cells[rt->curs_row][rt->curs_col].s, s, len);
115 rt->cells[rt->curs_row][rt->curs_col].len = len;
116 rt->cells[rt->curs_row][rt->curs_col].attrs = rt->curattrs;
120 static void put_graphmode_char(madtty_t *rt, int c)
123 /* do some very pitiful translation to regular ascii chars */
125 case 'j': case 'k': case 'l': case 'm': case 'n': case 't':
126 case 'u': case 'v': case 'w':
134 put_normal_char(rt, &nc, 1);
137 static void new_escape_sequence(madtty_t *rt)
144 static void cancel_escape_sequence(madtty_t *rt)
151 static void handle_control_char(madtty_t *rt, int c)
154 case '\r': /* carriage return */
158 case '\n': /* line feed */
160 cursor_line_down(rt);
163 case '\b': /* backspace */
164 if (rt->curs_col > 0)
169 rt->curs_col = (rt->curs_col + 8) & ~7;
170 clamp_cursor_to_bounds(rt);
173 case '\x1b': /* begin escape sequence (aborting previous one if any) */
174 new_escape_sequence(rt);
177 case '\x0e': /* enter graphical character mode */
178 rt->graphmode = true;
181 case '\x0f': /* exit graphical character mode */
182 rt->graphmode = false;
185 case '\x9b': /* CSI character. Equivalent to ESC [ */
186 new_escape_sequence(rt);
187 rt->esbuf[rt->esbuf_len++] = '[';
191 case '\x1a': /* these interrupt escape sequences */
192 cancel_escape_sequence(rt);
195 case '\a': /* bell */
196 /* do nothing for now... maybe a visual bell would be nice? */
201 static bool is_valid_csi_ender(int c)
203 return (c >= 'a' && c <= 'z')
204 || (c >= 'A' && c <= 'Z')
205 || (c == '@' || c == '`');
208 /* interprets a 'set attribute' (SGR) CSI escape sequence */
209 static void interpret_csi_SGR(madtty_t *rt, int param[], int pcount)
214 /* special case: reset attributes */
215 rt->curattrs = A_NORMAL;
219 for (i = 0; i < pcount; i++) {
221 // From http://vt100.net/docs/vt510-rm/SGR table 5-16
222 // 0 All attributes off
228 // 10 The ASCII character set is the current 7-bit
229 // display character set (default) - SCO Console only.
230 // 11 Map Hex 00-7F of the PC character set codes
231 // to the current 7-bit display character set
232 // - SCO Console only.
233 // 12 Map Hex 80-FF of the current character set to
234 // the current 7-bit display character set - SCO
239 // 27 Negative image off
240 // 28 Invisible image off
243 #define CASE(x, op) case x: op; break
244 CASE(0, rt->curattrs = A_NORMAL);
245 CASE(1, rt->curattrs |= A_BOLD);
246 CASE(4, rt->curattrs |= A_UNDERLINE);
247 CASE(5, rt->curattrs |= A_BLINK);
248 CASE(7, rt->curattrs |= A_REVERSE);
249 CASE(8, rt->curattrs |= A_INVIS);
250 CASE(22, rt->curattrs &= ~A_BOLD);
251 CASE(24, rt->curattrs &= ~A_UNDERLINE);
252 CASE(25, rt->curattrs &= ~A_BLINK);
253 CASE(27, rt->curattrs &= ~A_REVERSE);
254 CASE(28, rt->curattrs &= ~A_INVIS);
257 rt->curattrs &= ~070;
258 rt->curattrs |= (param[i] - 30) << 3;
262 rt->curattrs &= ~007;
263 rt->curattrs |= (param[i] - 40);
267 rt->curattrs &= ~070;
271 rt->curattrs &= ~007;
280 /* interprets an 'erase display' (ED) escape sequence */
281 static void interpret_csi_ED(madtty_t *rt, int param[], int pcount)
284 int start_row, start_col, end_row, end_col;
287 if (pcount && param[0] == 2) {
290 end_row = rt->rows - 1;
291 end_col = rt->cols - 1;
293 if (pcount && param[0] == 1) {
296 end_row = rt->curs_row;
297 end_col = rt->curs_col;
299 start_row = rt->curs_row;
300 start_col = rt->curs_col;
301 end_row = rt->rows - 1;
302 end_col = rt->cols - 1;
306 for (r = start_row; r <= end_row; r++) {
307 for (c = (r == start_row ? start_col : 0);
308 c <= (r == end_row ? end_col : rt->cols - 1);
311 rt->cells[r][c].s[0] = 0x20;
312 rt->cells[r][c].len = 1;
313 rt->cells[r][c].attrs = rt->curattrs;
318 /* interprets a 'move cursor' (CUP) escape sequence */
319 static void interpret_csi_CUP(madtty_t *rt, int param[], int pcount)
323 rt->curs_row = rt->curs_col = 0;
327 return; /* malformed */
330 rt->curs_row = param[0] - 1; /* convert from 1-based to 0-based */
331 rt->curs_col = param[1] - 1; /* convert from 1-based to 0-based */
333 clamp_cursor_to_bounds(rt);
336 /* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL,
337 * CPL, CHA, HPR, VPA, VPR, HPA */
338 static void interpret_csi_C(madtty_t *rt, char verb, int param[], int pcount)
340 int n = (pcount && param[0] > 0) ? param[0] : 1;
343 case 'A': rt->curs_row -= n; break;
344 case 'B': case 'e': rt->curs_row += n; break;
345 case 'C': case 'a': rt->curs_col += n; break;
346 case 'D': rt->curs_col -= n; break;
347 case 'E': rt->curs_row += n; rt->curs_col = 0; break;
348 case 'F': rt->curs_row -= n; rt->curs_col = 0; break;
349 case 'G': case '`': rt->curs_col = param[0] - 1; break;
350 case 'd': rt->curs_row = param[0] - 1; break;
353 clamp_cursor_to_bounds(rt);
356 /* Interpret the 'erase line' escape sequence */
357 static void interpret_csi_EL(madtty_t *rt, int param[], int pcount)
359 int erase_start, erase_end, i;
360 int cmd = pcount ? param[0] : 0;
363 case 1: erase_start = 0; erase_end = rt->curs_col; break;
364 case 2: erase_start = 0; erase_end = rt->cols - 1; break;
365 default: erase_start = rt->curs_col; erase_end = rt->cols - 1; break;
368 for (i = erase_start; i <= erase_end; i++) {
369 rt->cells[rt->curs_row][i].s[0] = 0x20;
370 rt->cells[rt->curs_row][i].len = 1;
371 rt->cells[rt->curs_row][i].attrs = rt->curattrs;
375 /* Interpret the 'insert blanks' sequence (ICH) */
376 static void interpret_csi_ICH(madtty_t *rt, int param[], int pcount)
378 int n = (pcount && param[0] > 0) ? param[0] : 1;
381 for (i = rt->cols - 1; i >= rt->curs_col + n; i--) {
382 rt->cells[rt->curs_row][i] = rt->cells[rt->curs_row][i - n];
385 for (i = rt->curs_col; i < rt->curs_col + n; i++) {
386 rt->cells[rt->curs_row][i].s[0] = 0x20;
387 rt->cells[rt->curs_row][i].len = 1;
388 rt->cells[rt->curs_row][i].attrs = rt->curattrs;
392 /* Interpret the 'delete chars' sequence (DCH) */
393 static void interpret_csi_DCH(madtty_t *rt, int param[], int pcount)
395 int n = (pcount && param[0] > 0) ? param[0] : 1;
398 for (i = rt->curs_col; i < rt->cols; i++) {
399 if (i + n < rt->cols) {
400 rt->cells[rt->curs_row][i] = rt->cells[rt->curs_row][i + n];
402 rt->cells[rt->curs_row][i].s[0] = 0x20;
403 rt->cells[rt->curs_row][i].len = 1;
404 rt->cells[rt->curs_row][i].attrs = rt->curattrs;
409 /* Interpret an 'insert line' sequence (IL) */
410 static void interpret_csi_IL(madtty_t *rt, int param[], int pcount)
412 int n = (pcount && param[0] > 0) ? param[0] : 1;
415 for (i = rt->scrollbottom; i >= rt->curs_row + n; i--) {
416 memcpy(rt->cells[i], rt->cells[i - n], sizeof(RoteCell) * rt->cols);
419 for (i = rt->curs_row; i < rt->curs_row + n && i <= rt->scrollbottom; i++) {
420 for (j = 0; j < rt->cols; j++) {
421 rt->cells[i][j].s[0] = 0x20;
422 rt->cells[i][j].len = 1;
423 rt->cells[i][j].attrs = rt->curattrs;
429 /* Interpret a 'delete line' sequence (DL) */
430 static void interpret_csi_DL(madtty_t *rt, int param[], int pcount)
432 int n = (pcount && param[0] > 0) ? param[0] : 1;
435 for (i = rt->curs_row; i <= rt->scrollbottom; i++) {
436 if (i + n <= rt->scrollbottom) {
437 memcpy(rt->cells[i], rt->cells[i+n], sizeof(RoteCell) * rt->cols);
439 for (j = 0; j < rt->cols; j++) {
440 rt->cells[i][j].s[0] = 0x20;
441 rt->cells[i][j].len = 1;
442 rt->cells[i][j].attrs = rt->curattrs;
448 /* Interpret an 'erase characters' (ECH) sequence */
449 static void interpret_csi_ECH(madtty_t *rt, int param[], int pcount)
451 int n = (pcount && param[0] > 0) ? param[0] : 1;
454 for (i = rt->curs_col; i < rt->curs_col + n && i < rt->cols; i++) {
455 rt->cells[rt->curs_row][i].s[0] = 0x20;
456 rt->cells[rt->curs_row][i].len = 1;
457 rt->cells[rt->curs_row][i].attrs = rt->curattrs;
461 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
462 static void interpret_csi_DECSTBM(madtty_t *rt, int param[], int pcount)
464 int newtop, newbottom;
468 newbottom = rt->rows - 1;
471 return; /* malformed */
474 newtop = param[0] - 1;
475 newbottom = param[1] - 1;
477 /* clamp to bounds */
480 if (newtop >= rt->rows)
481 newtop = rt->rows - 1;
484 if (newbottom >= rt->rows)
485 newbottom = rt->rows - 1;
487 /* check for range validity */
488 if (newtop > newbottom)
490 rt->scrolltop = newtop;
491 rt->scrollbottom = newbottom;
494 static void es_interpret_csi(madtty_t *rt)
496 static int csiparam[MAX_CSI_ES_PARAMS];
498 const char *p = rt->esbuf + 1;
499 char verb = rt->esbuf[rt->esbuf_len - 1];
501 if (!strncmp(rt->esbuf, "[?", 2)) { /* private-mode CSI, ignore */
505 /* parse numeric parameters */
506 while (isdigit((unsigned char)*p) || *p == ';') {
508 if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
509 csiparam[param_count++] = 0;
511 if (param_count == 0) csiparam[param_count++] = 0;
512 csiparam[param_count - 1] *= 10;
513 csiparam[param_count - 1] += *p - '0';
519 /* delegate handling depending on command character (verb) */
522 if (param_count == 1 && csiparam[0] == 4) /* insert mode */
526 if (param_count == 1 && csiparam[0] == 4) /* replace mode */
529 case 'm': /* it's a 'set attribute' sequence */
530 interpret_csi_SGR(rt, csiparam, param_count); break;
531 case 'J': /* it's an 'erase display' sequence */
532 interpret_csi_ED(rt, csiparam, param_count); break;
533 case 'H': case 'f': /* it's a 'move cursor' sequence */
534 interpret_csi_CUP(rt, csiparam, param_count); break;
535 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
536 case 'e': case 'a': case 'd': case '`':
537 /* it is a 'relative move' */
538 interpret_csi_C(rt, verb, csiparam, param_count); break;
539 case 'K': /* erase line */
540 interpret_csi_EL(rt, csiparam, param_count); break;
541 case '@': /* insert characters */
542 interpret_csi_ICH(rt, csiparam, param_count); break;
543 case 'P': /* delete characters */
544 interpret_csi_DCH(rt, csiparam, param_count); break;
545 case 'L': /* insert lines */
546 interpret_csi_IL(rt, csiparam, param_count); break;
547 case 'M': /* delete lines */
548 interpret_csi_DL(rt, csiparam, param_count); break;
549 case 'X': /* erase chars */
550 interpret_csi_ECH(rt, csiparam, param_count); break;
551 case 'r': /* set scrolling region */
552 interpret_csi_DECSTBM(rt, csiparam, param_count); break;
553 case 's': /* save cursor location */
554 rt->curs_srow = rt->curs_col;
555 rt->curs_scol = rt->curs_row;
557 case 'u': /* restore cursor location */
558 rt->curs_col = rt->curs_srow;
559 rt->curs_row = rt->curs_scol;
566 static void try_interpret_escape_seq(madtty_t *rt)
568 char firstchar = rt->esbuf[0];
569 char lastchar = rt->esbuf[rt->esbuf_len-1];
572 return; /* too early to do anything */
574 /* interpret ESC-M as reverse line-feed */
575 if (firstchar == 'M') {
577 cancel_escape_sequence(rt);
581 if (firstchar != '[' && firstchar != ']') {
582 /* unrecognized escape sequence. Let's forget about it. */
583 cancel_escape_sequence(rt);
587 if (firstchar == '[' && is_valid_csi_ender(lastchar)) {
588 es_interpret_csi(rt);
589 cancel_escape_sequence(rt);
590 } else if (firstchar == ']' && lastchar == '\a') {
591 /* we have an xterm escape sequence: interpret it */
593 /* es_interpret_xterm_es(rt); -- TODO!*/
594 cancel_escape_sequence(rt);
597 /* if the escape sequence took up all available space and could
598 * not yet be parsed, abort it */
599 if (rt->esbuf_len + 1 >= ESEQ_BUF_SIZE)
600 cancel_escape_sequence(rt);
603 int madtty_inject(madtty_t *rt, const char *data, int len)
607 for (pos = 0; pos < len; pos++) {
608 if ((unsigned char)data[pos] <= 31) {
609 handle_control_char(rt, data[pos]);
613 if (rt->escaped && rt->esbuf_len < ESEQ_BUF_SIZE) {
614 /* append character to ongoing escape sequence */
615 rt->esbuf[rt->esbuf_len] = data[pos];
616 rt->esbuf[++rt->esbuf_len] = 0;
618 try_interpret_escape_seq(rt);
621 put_graphmode_char(rt, data[pos]);
623 static int8_t const lens[5] = { 1, -1, 2, 3, 4 };
624 int bsf = __builtin_clz(~((unsigned char)data[pos] << 24));
626 if (pos + lens[bsf] > len)
629 put_normal_char(rt, data + pos, lens[bsf]);
630 pos += lens[bsf] - 1;
637 madtty_t *madtty_create(int rows, int cols)
642 if (rows <= 0 || cols <= 0)
645 rt = (madtty_t*)calloc(sizeof(madtty_t), 1);
649 /* record dimensions */
653 /* default mode is replace */
656 /* create the cell matrix */
657 rt->cells = (RoteCell**)calloc(sizeof(RoteCell*), rt->rows);
658 for (i = 0; i < rt->rows; i++) {
659 rt->cells[i] = (RoteCell*)calloc(sizeof(RoteCell), rt->cols);
662 /* initialization of other public fields */
663 rt->curs_row = rt->curs_col = 0;
664 rt->curattrs = A_NORMAL; /* white text over black background */
666 rt->pty = -1; /* no pty for now */
668 /* initial scrolling area is the whole window */
670 rt->scrollbottom = rt->rows - 1;
675 void madtty_destroy(madtty_t *rt)
681 for (i = 0; i < rt->rows; i++) {
688 void madtty_draw(madtty_t *rt, WINDOW *win, int srow, int scol)
692 for (i = 0; i < rt->rows; i++) {
693 wmove(win, srow + i, scol);
694 for (j = 0; j < rt->cols; j++) {
695 wattrset(win, (rt->cells[i][j].attrs & ~077) | COLOR_PAIR(rt->cells[i][j].attrs & 077));
696 if (rt->cells[i][j].len && rt->cells[i][j].s[0] >= ' ') {
697 waddnstr(win, rt->cells[i][j].s, rt->cells[i][j].len);
704 wmove(win, srow + rt->curs_row, scol + rt->curs_col);
707 /******************************************************/
709 pid_t madtty_forkpty(madtty_t *rt, const char *path, const char *argv[])
714 ws.ws_row = rt->rows;
715 ws.ws_col = rt->cols;
716 ws.ws_xpixel = ws.ws_ypixel = 0;
718 pid = forkpty(&rt->pty, NULL, NULL, &ws);
725 setenv("TERM", "linux", 1);
726 execv(path, (char *const*)argv);
727 fprintf(stderr, "\nexecv() failed.\nCommand: '%s'\n", argv[0]);
731 return rt->childpid = pid;
734 int madtty_read(madtty_t *rt, char *buf, int buflen)
741 return read(rt->pty, buf, buflen);
744 int madtty_write(madtty_t *rt, const char *data, int len)
754 res = write(rt->pty, data, len);
756 if (errno == EINTR || errno == EAGAIN)
763 static char const * const keytable[KEY_MAX+1] = {
767 [KEY_RIGHT] = "\e[C",
769 [KEY_BACKSPACE] = "\b",
770 [KEY_HOME] = "\e[1~",
774 [KEY_PPAGE] = "\e[5~",
775 [KEY_NPAGE] = "\e[6~",
776 [KEY_SUSPEND] = "\x1A", /* Ctrl+Z gets mapped to this */
777 [KEY_F(1)] = "\e[[A",
778 [KEY_F(2)] = "\e[[B",
779 [KEY_F(3)] = "\e[[C",
780 [KEY_F(4)] = "\e[[D",
781 [KEY_F(5)] = "\e[[E",
782 [KEY_F(6)] = "\e[17~",
783 [KEY_F(7)] = "\e[18~",
784 [KEY_F(8)] = "\e[19~",
785 [KEY_F(9)] = "\e[20~",
786 [KEY_F(10)] = "\e[21~",
789 void madtty_keypress(madtty_t *rt, int keycode)
791 char c = (char)keycode;
795 if (keycode >= 0 && keycode < KEY_MAX && keytable[keycode]) {
796 buf = keytable[keycode];
797 len = strlen(keytable[keycode]);
804 int res = madtty_write(rt, buf, len);
813 void madtty_initialize(void)
815 setlocale(LC_ALL, "");
820 nodelay(stdscr, TRUE);
821 keypad(stdscr, TRUE);
823 for (int i = 0; i < 8 * 8; i++)
824 init_pair(i, i >> 3, i & 7);