4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id: ansi.c,v 1.50 2008/03/07 13:16:02 james Exp $";
13 * Revision 1.50 2008/03/07 13:16:02 james
14 * *** empty log message ***
16 * Revision 1.49 2008/03/07 12:37:04 james
17 * *** empty log message ***
19 * Revision 1.48 2008/03/06 22:51:39 james
20 * *** empty log message ***
22 * Revision 1.47 2008/03/06 21:34:09 james
23 * *** empty log message ***
25 * Revision 1.46 2008/03/06 21:33:02 james
26 * *** empty log message ***
28 * Revision 1.45 2008/03/06 17:21:41 james
29 * *** empty log message ***
31 * Revision 1.44 2008/03/06 16:49:05 james
32 * *** empty log message ***
34 * Revision 1.43 2008/03/06 01:41:48 james
35 * *** empty log message ***
37 * Revision 1.42 2008/03/03 06:04:42 james
38 * *** empty log message ***
40 * Revision 1.41 2008/03/02 12:30:54 staffcvs
41 * *** empty log message ***
43 * Revision 1.40 2008/03/02 10:50:32 staffcvs
44 * *** empty log message ***
46 * Revision 1.39 2008/03/02 10:37:56 james
47 * *** empty log message ***
49 * Revision 1.38 2008/03/02 10:27:24 james
50 * *** empty log message ***
52 * Revision 1.37 2008/02/29 22:50:29 james
53 * *** empty log message ***
55 * Revision 1.36 2008/02/28 22:00:42 james
56 * *** empty log message ***
58 * Revision 1.35 2008/02/28 16:57:51 james
59 * *** empty log message ***
61 * Revision 1.34 2008/02/27 09:42:53 james
62 * *** empty log message ***
64 * Revision 1.33 2008/02/27 09:42:21 james
65 * *** empty log message ***
67 * Revision 1.32 2008/02/26 23:56:12 james
68 * *** empty log message ***
70 * Revision 1.31 2008/02/26 23:23:17 james
71 * *** empty log message ***
73 * Revision 1.30 2008/02/24 00:42:53 james
74 * *** empty log message ***
76 * Revision 1.29 2008/02/23 11:48:37 james
77 * *** empty log message ***
79 * Revision 1.28 2008/02/22 17:07:00 james
80 * *** empty log message ***
82 * Revision 1.27 2008/02/20 22:54:22 staffcvs
83 * *** empty log message ***
85 * Revision 1.26 2008/02/20 20:16:07 james
86 * *** empty log message ***
88 * Revision 1.25 2008/02/20 19:44:37 james
91 * Revision 1.24 2008/02/20 19:36:06 james
94 * Revision 1.23 2008/02/20 19:25:09 james
95 * *** empty log message ***
97 * Revision 1.22 2008/02/15 03:32:07 james
98 * *** empty log message ***
100 * Revision 1.21 2008/02/14 02:46:44 james
101 * *** empty log message ***
103 * Revision 1.20 2008/02/14 01:55:57 james
104 * *** empty log message ***
106 * Revision 1.19 2008/02/13 16:57:29 james
107 * *** empty log message ***
109 * Revision 1.18 2008/02/13 09:12:21 james
110 * *** empty log message ***
112 * Revision 1.17 2008/02/13 01:08:18 james
113 * *** empty log message ***
115 * Revision 1.16 2008/02/07 13:22:51 james
116 * *** empty log message ***
118 * Revision 1.15 2008/02/07 13:19:48 james
119 * *** empty log message ***
121 * Revision 1.14 2008/02/07 12:21:16 james
122 * *** empty log message ***
124 * Revision 1.13 2008/02/07 12:16:04 james
125 * *** empty log message ***
127 * Revision 1.12 2008/02/07 11:32:41 james
128 * *** empty log message ***
130 * Revision 1.11 2008/02/07 11:11:14 staffcvs
131 * *** empty log message ***
133 * Revision 1.10 2008/02/07 01:02:52 james
134 * *** empty log message ***
136 * Revision 1.9 2008/02/07 00:43:27 james
137 * *** empty log message ***
139 * Revision 1.8 2008/02/07 00:39:13 james
140 * *** empty log message ***
142 * Revision 1.7 2008/02/06 20:26:57 james
143 * *** empty log message ***
145 * Revision 1.6 2008/02/06 17:53:28 james
146 * *** empty log message ***
148 * Revision 1.5 2008/02/06 15:53:22 james
149 * *** empty log message ***
151 * Revision 1.4 2008/02/04 20:23:55 james
152 * *** empty log message ***
154 * Revision 1.3 2008/02/04 05:45:55 james
157 * Revision 1.2 2008/02/04 02:05:06 james
158 * *** empty log message ***
160 * Revision 1.1 2008/02/03 23:31:25 james
161 * *** empty log message ***
168 ansi_move (ANSI * a, CRT_Pos p)
172 int dx = p.x - a->pos.x;
173 int dy = p.y - a->pos.y;
176 // a->pos.x = ANSI_INVAL;
178 if (a->pos.x != ANSI_INVAL) {
185 if (a->terminal->xmit (a->terminal, "\033[C", 3) != 3)
187 } else if (dx == -1) {
188 if (a->terminal->xmit (a->terminal, "\033[D", 3) != 3)
191 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
192 if (a->terminal->xmit (a->terminal, buf, n) != n)
197 if (a->terminal->xmit (a->terminal, "\033[A", 3) != 3)
199 } else if (dy == 1) {
200 if (a->terminal->xmit (a->terminal, "\033[B", 3) != 3)
203 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
204 if (a->terminal->xmit (a->terminal, buf, n) != n)
207 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
208 if (a->terminal->xmit (a->terminal, buf, n) != n)
213 if (a->terminal->xmit (a->terminal, "\033[E", 3) != 3)
215 } else if (dy == -1) {
216 if (a->terminal->xmit (a->terminal, "\033[F", 3) != 3)
219 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
220 if (a->terminal->xmit (a->terminal, buf, n) != n)
223 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
224 if (a->terminal->xmit (a->terminal, buf, n) != n)
228 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
229 if (a->terminal->xmit (a->terminal, buf, n) != n)
233 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
234 if (a->terminal->xmit (a->terminal, buf, n) != n)
245 ansi_showhide_cursor (ANSI * a, int hide)
248 if (a->hide_cursor == hide)
252 if (a->terminal->xmit (a->terminal, "\033[?25l", 6) != 6)
255 if (a->terminal->xmit (a->terminal, "\033[?25h", 6) != 6)
259 a->hide_cursor = hide;
265 ansi_force_attr_normal (ANSI * a)
267 if (a->terminal->xmit (a->terminal, "\033[0m", 4) != 4)
269 a->attr = CRT_ATTR_NORMAL;
270 a->color = ANSI_INVAL;
275 ansi_set_title (ANSI * a, char *t)
279 char *term = getenv ("TERM");
283 if (strncmp (term, "xterm", 5) && strncmp (term, "rxvt", 4))
286 i = sprintf (buf, "\033]0;%s\007", t);
288 if (a->terminal->xmit (a->terminal, buf, i) != i)
294 ansi_set_color (ANSI * a, int color)
301 if ((a->color != ANSI_INVAL) && (color == a->color))
303 fg = CRT_COLOR_FG (color);
304 bg = CRT_COLOR_BG (color);
306 if (fg & CRT_COLOR_INTENSITY) {
312 if (bg & CRT_COLOR_INTENSITY) {
318 i = sprintf (buf, "\033[%d;%dm", fg, bg);
320 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
323 if (a->terminal->xmit (a->terminal, buf, i) != i)
331 ansi_set_attr (ANSI * a, int attr)
336 dif = attr ^ a->attr;
344 if (attr == CRT_ATTR_NORMAL) {
345 ansi_force_attr_normal (a);
350 if (dif & CRT_ATTR_UNDERLINE) {
351 if (attr & CRT_ATTR_UNDERLINE) {
352 if (a->terminal->xmit (a->terminal, "\033[4m", 4) != 4)
355 if (a->terminal->xmit (a->terminal, "\033[24m", 5) != 5)
359 if (dif & CRT_ATTR_REVERSE) {
360 if (attr & CRT_ATTR_REVERSE) {
361 if (a->terminal->xmit (a->terminal, "\033[7m", 4) != 4)
364 if (a->terminal->xmit (a->terminal, "\033[27m", 5) != 5)
368 if (dif & CRT_ATTR_BOLD) {
369 if (attr & CRT_ATTR_BOLD) {
370 if (a->terminal->xmit (a->terminal, "\033[1m", 4) != 4)
373 if (a->terminal->xmit (a->terminal, "\033[21m", 5) != 5)
375 if (a->terminal->xmit (a->terminal, "\033[22m", 5) != 5)
384 ascii_emit (TTY * t, uint32_t ch)
388 /* Some quick obvious subsititons for quotation marks */
402 /* Short cut the easy stuff */
405 return (t->xmit (t, &c, 1) == 1) ? 0 : -1;
408 for (i = 0; i < VT102_CHARSET_SIZE; ++i) {
409 if (vt102_charset_gl[i] == ch) {
410 uint8_t c[3] = { 016, i, 017 };
411 return (t->xmit (t, &c, 3) == 3) ? 0 : -1;
415 return (t->xmit (t, "?", 1) == 1) ? 0 : -1;
420 ansi_render (ANSI * a, CRT_CA ca)
425 if ((ca.chr < VT102_CHARSET_SIZE) && (vt102_charset_c0[ca.chr]))
426 ca.chr = vt102_charset_c0[ca.chr];
428 if ((ca.chr >= 0x80) && (ca.chr < 0xa0))
431 ansi_set_attr (a, ca.attr);
432 ansi_set_color (a, ca.color);
435 if (utf8_emit (a->terminal, ca.chr))
438 if (ascii_emit (a->terminal, ca.chr))
444 /* Can't easily wrap round here as don't know size of destination screen */
445 /* so invalidate the cached cursor position */
447 if (a->pos.x >= a->size.x)
448 a->pos.x = ANSI_INVAL;
461 err += ansi_set_attr (a, CRT_ATTR_NORMAL);
462 err += ansi_set_color (a, CRT_COLOR_NORMAL);
463 err += ansi_move (a, p);
464 if (a->terminal->xmit (a->terminal, "\033[2J", 4) != 4)
466 /* different emulators leave cursor in different places after cls
468 a->pos.x = ANSI_INVAL;
474 ansi_draw_line (ANSI * a, CRT_CA * cap, int y)
478 CRT_Pos p = { 0, y };
479 CRT_CA *acap = &a->crt.screen[CRT_ADDR_POS (&p)];
481 for (p.x = 0; p.x < a->crt.size.x; ++p.x) {
482 if (p.x >= a->size.x)
485 if (crt_ca_cmp (*acap, *cap)) {
486 err += ansi_showhide_cursor (a, 1);
490 err += ansi_move (a, p);
491 err += ansi_render (a, *acap);
502 ansi_resize_check (ANSI * a, CRT_Pos * size)
506 if ((size && crt_pos_cmp (a->crt.size, *size))
507 || crt_pos_cmp (a->terminal->size, a->size)) {
509 terminal_getsize (a->terminal);
511 a->size = a->terminal->size;
513 a->pos.x = ANSI_INVAL;
514 a->hide_cursor = ANSI_INVAL;
521 // a->terminal->xmit (a->terminal, "\033c", 3);
522 // maybe - issue 132 column command if we're 132?
525 if (a->terminal->xmit (a->terminal, "\033=", 2) != 2)
527 if (a->terminal->xmit (a->terminal, "\033[?6l", 5) != 5)
529 if (a->terminal->xmit (a->terminal, "\033[r", 3) != 3)
532 if (a->terminal->xmit (a->terminal, "\033%G", 3) != 3)
535 if (a->terminal->xmit (a->terminal, "\033(B", 3) != 3)
537 if (a->terminal->xmit (a->terminal, "\033)0", 3) != 3)
539 if (a->terminal->xmit (a->terminal, "\017", 1) != 3)
549 ansi_history (ANSI * a, History * h)
555 /* Do we need to catch up on history? */
557 if (a->history_ptr == h->wptr)
560 err += ansi_resize_check (a, NULL);
562 if ((a->size.x < a->crt.size.x) || (a->size.y < a->crt.size.y))
565 guess_scroll = a->crt.size.y - 1; /* Bototm line should be a status line */
568 err += ansi_force_attr_normal (a);
569 err += ansi_set_color (a, CRT_COLOR_NORMAL);
571 i = sprintf (buf, "\033[%d;%dr", 1, guess_scroll);
572 if (a->terminal->xmit (a->terminal, buf, i) != i)
576 while (a->history_ptr != h->wptr) {
578 History_ent *e = &h->lines[a->history_ptr];
580 HISTORY_INC (h, a->history_ptr);
585 /* If so write the line ot the top of the screen */
586 err += ansi_draw_line (a, e->line, 0);
589 /* Roll guess_scroll lines up putting the top line into the xterm's
593 /* Make extra lines a predictable colour */
594 err += ansi_set_color (a, CRT_COLOR_NORMAL);
596 err += ansi_showhide_cursor (a, 1);
598 i = sprintf (buf, "\033[%d;%dH", guess_scroll, 1);
599 if (a->terminal->xmit (a->terminal, buf, i) != i)
602 if (a->terminal->xmit (a->terminal, "\033D", 2) != 2)
605 a->pos.x = ANSI_INVAL;
607 /* now do the same in our image of the screen */
614 /* scroll lines up */
615 for (s.y++; s.y < guess_scroll; s.y++, e.y++) {
616 memcpy (&a->crt.screen[CRT_ADDR_POS (&e)],
617 &a->crt.screen[CRT_ADDR_POS (&s)],
618 sizeof (CRT_CA) * a->crt.size.x);
624 crt_erase (&a->crt, s, e, 1, CRT_COLOR_NORMAL);
629 if (a->terminal->xmit (a->terminal, "\033[r", 3) != 3)
632 a->pos.x = ANSI_INVAL;
641 ansi_draw (ANSI * a, CRT * c)
645 int hidden_cursor = 0;
648 err += ansi_resize_check (a, &c->size);
651 for (p.y = 0; p.y < a->crt.size.y; ++p.y) {
652 if (p.y >= a->size.y)
655 err += ansi_draw_line (a, &c->screen[CRT_ADDR (p.y, 0)], p.y);
660 if ((c->size.x > a->size.x) || (c->size.y > a->size.y)) {
661 char msg[1024]; // = "Window is too small";
667 sprintf (msg, "Window too small (%dx%d need %dx%d)", a->size.x,
668 a->size.y, c->size.x, c->size.y);
670 err += ansi_showhide_cursor (a, 1);
671 err += ansi_set_attr (a, CRT_ATTR_REVERSE);
673 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
674 err += ansi_move (a, p);
676 if (a->terminal->xmit (a->terminal, msg, i) != i)
679 a->pos.x = ANSI_INVAL;
683 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y)) {
684 err += ansi_showhide_cursor (a, 1);
689 err += ansi_move (a, a->crt.pos);
691 a->crt.hide_cursor = c->hide_cursor;
692 err += ansi_showhide_cursor (a, a->crt.hide_cursor);
698 ansi_reset (ANSI * a, CRT * c)
701 return ansi_draw (a, c ? c : &a->crt);
705 ansi_terminal_reset (ANSI * a)
707 CRT_Pos p = { 0, a->crt.size.y };
708 ansi_force_attr_normal (a);
715 ansi_key (ANSI * a, Context * c, int key)
718 return c->k->key (c->k, c, key);
720 cmd_show_status (c->d, c);
723 if (key == CMD_CANCEL_KEY) {
724 return cmd_deactivate (c->d, c);
725 } else if (key == CMD_KEY) {
726 cmd_deactivate (c->d, c);
728 return cmd_key (c->d, c, a, key);
730 } else if (key == CMD_KEY) {
731 return cmd_activate (c->d, c);
734 return c->k->key (c->k, c, key);
739 ansi_flush_escape (ANSI * a, Context * c)
741 ANSI_Parser *p = &a->parser;
744 for (i = 0; i < p->escape_ptr; ++i) {
745 ansi_key (a, c, p->escape_buf[i]);
753 ansi_parse_deckey (ANSI * a, Context * c)
755 ANSI_Parser *p = &a->parser;
756 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O')) {
757 ansi_flush_escape (a, c);
761 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z')) {
762 ansi_key (a, c, KEY_UP + (p->escape_buf[2] - 'A'));
763 } else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z')) {
764 ansi_key (a, c, KEY_154 + (p->escape_buf[2] - 'a'));
766 ansi_flush_escape (a, c);
774 ansi_parse_ansikey (ANSI * a, Context * c)
776 ANSI_Parser *p = &a->parser;
777 int l = p->escape_ptr - 1;
781 if ((p->escape_buf[1] != '[') || (p->escape_buf[l] != '~')) {
782 ansi_flush_escape (a, c);
786 k = strtol (&p->escape_buf[2], &end, 10);
788 if (end != &p->escape_buf[l]) {
789 ansi_flush_escape (a, c);
793 ansi_key (a, c, KEY_180 + k);
802 ansi_parse_escape (ANSI * a, Context * c)
804 ANSI_Parser *p = &a->parser;
805 switch (p->escape_ptr) {
810 switch (p->escape_buf[1]) {
815 ansi_flush_escape (a, c);
819 switch (p->escape_buf[1]) {
821 ansi_parse_deckey (a, c);
824 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
825 ansi_parse_deckey (a, c);
828 ansi_flush_escape (a, c);
835 if (p->escape_buf[p->escape_ptr - 1] == '~')
836 ansi_parse_ansikey (a, c);
839 ansi_flush_escape (a, c);
845 ansi_check_escape (ANSI * a, Context * c)
847 ANSI_Parser *p = &a->parser;
848 struct timeval now, diff;
849 gettimeofday (&now, NULL);
850 timersub (&now, &p->last_escape, &diff);
853 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
854 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
862 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
863 ansi_flush_escape (a, c);
869 ansi_parse_char (ANSI * a, Context * c, int ch)
871 ANSI_Parser *p = &a->parser;
874 /* See if it's time to flush the escape */
875 ansi_check_escape (a, c);
879 ansi_flush_escape (a, c);
883 gettimeofday (&p->last_escape, NULL);
887 p->escape_buf[p->escape_ptr++] = ch;
888 ansi_parse_escape (a, c);
896 ansi_parse (ANSI * a, Context * c, char *buf, int len)
899 ansi_parse_char (a, c, *(buf++));
903 ansi_dispatch (ANSI * a, Context * c)
908 ansi_check_escape (a, c);
914 red = a->terminal->recv (a->terminal, buf, sizeof (buf));
918 ansi_parse (a, c, buf, red);
926 ansi_update (ANSI * a, Context * c)
930 err += ansi_history (a, c->h);
931 err += ansi_draw (a, &c->v->crt);
932 tty_length (a->terminal, c->v->crt.size.y);
939 a->terminal_reset (a);
941 a->terminal->close (a->terminal);
947 ansi_new_from_terminal (TTY * t, int utf8)
951 ret = xmalloc (sizeof (ANSI));
952 memset (ret, 0, sizeof (ANSI));
957 ret->update = ansi_update;
958 ret->reset = ansi_reset;
959 ret->terminal_reset = ansi_terminal_reset;
960 ret->set_title = ansi_set_title;
961 ret->close = ansi_free;
962 ret->dispatch = ansi_dispatch;