4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.38 2008/03/02 10:27:24 james
14 * *** empty log message ***
16 * Revision 1.37 2008/02/29 22:50:29 james
17 * *** empty log message ***
19 * Revision 1.36 2008/02/28 22:00:42 james
20 * *** empty log message ***
22 * Revision 1.35 2008/02/28 16:57:51 james
23 * *** empty log message ***
25 * Revision 1.34 2008/02/27 09:42:53 james
26 * *** empty log message ***
28 * Revision 1.33 2008/02/27 09:42:21 james
29 * *** empty log message ***
31 * Revision 1.32 2008/02/26 23:56:12 james
32 * *** empty log message ***
34 * Revision 1.31 2008/02/26 23:23:17 james
35 * *** empty log message ***
37 * Revision 1.30 2008/02/24 00:42:53 james
38 * *** empty log message ***
40 * Revision 1.29 2008/02/23 11:48:37 james
41 * *** empty log message ***
43 * Revision 1.28 2008/02/22 17:07:00 james
44 * *** empty log message ***
46 * Revision 1.27 2008/02/20 22:54:22 staffcvs
47 * *** empty log message ***
49 * Revision 1.26 2008/02/20 20:16:07 james
50 * *** empty log message ***
52 * Revision 1.25 2008/02/20 19:44:37 james
55 * Revision 1.24 2008/02/20 19:36:06 james
58 * Revision 1.23 2008/02/20 19:25:09 james
59 * *** empty log message ***
61 * Revision 1.22 2008/02/15 03:32:07 james
62 * *** empty log message ***
64 * Revision 1.21 2008/02/14 02:46:44 james
65 * *** empty log message ***
67 * Revision 1.20 2008/02/14 01:55:57 james
68 * *** empty log message ***
70 * Revision 1.19 2008/02/13 16:57:29 james
71 * *** empty log message ***
73 * Revision 1.18 2008/02/13 09:12:21 james
74 * *** empty log message ***
76 * Revision 1.17 2008/02/13 01:08:18 james
77 * *** empty log message ***
79 * Revision 1.16 2008/02/07 13:22:51 james
80 * *** empty log message ***
82 * Revision 1.15 2008/02/07 13:19:48 james
83 * *** empty log message ***
85 * Revision 1.14 2008/02/07 12:21:16 james
86 * *** empty log message ***
88 * Revision 1.13 2008/02/07 12:16:04 james
89 * *** empty log message ***
91 * Revision 1.12 2008/02/07 11:32:41 james
92 * *** empty log message ***
94 * Revision 1.11 2008/02/07 11:11:14 staffcvs
95 * *** empty log message ***
97 * Revision 1.10 2008/02/07 01:02:52 james
98 * *** empty log message ***
100 * Revision 1.9 2008/02/07 00:43:27 james
101 * *** empty log message ***
103 * Revision 1.8 2008/02/07 00:39:13 james
104 * *** empty log message ***
106 * Revision 1.7 2008/02/06 20:26:57 james
107 * *** empty log message ***
109 * Revision 1.6 2008/02/06 17:53:28 james
110 * *** empty log message ***
112 * Revision 1.5 2008/02/06 15:53:22 james
113 * *** empty log message ***
115 * Revision 1.4 2008/02/04 20:23:55 james
116 * *** empty log message ***
118 * Revision 1.3 2008/02/04 05:45:55 james
121 * Revision 1.2 2008/02/04 02:05:06 james
122 * *** empty log message ***
124 * Revision 1.1 2008/02/03 23:31:25 james
125 * *** empty log message ***
132 ansi_move (ANSI * a, CRT_Pos p)
136 int dx = p.x - a->pos.x;
137 int dy = p.y - a->pos.y;
139 // a->pos.x = ANSI_INVAL;
141 if (a->pos.x != ANSI_INVAL)
151 a->terminal->xmit (a->terminal, "\033[C", 3);
155 a->terminal->xmit (a->terminal, "\033[D", 3);
159 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
160 a->terminal->xmit (a->terminal, buf, n);
167 a->terminal->xmit (a->terminal, "\033[A", 3);
171 a->terminal->xmit (a->terminal, "\033[B", 3);
175 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
176 a->terminal->xmit (a->terminal, buf, n);
180 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
181 a->terminal->xmit (a->terminal, buf, n);
188 a->terminal->xmit (a->terminal, "\033[E", 3);
192 a->terminal->xmit (a->terminal, "\033[F", 3);
196 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
197 a->terminal->xmit (a->terminal, buf, n);
201 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
202 a->terminal->xmit (a->terminal, buf, n);
207 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
208 a->terminal->xmit (a->terminal, buf, n);
213 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
214 a->terminal->xmit (a->terminal, buf, n);
222 ansi_showhide_cursor (ANSI * a, int hide)
224 if (a->hide_cursor == hide)
229 a->terminal->xmit (a->terminal, "\033[?25l", 6);
233 a->terminal->xmit (a->terminal, "\033[?25h", 6);
236 a->hide_cursor = hide;
241 ansi_force_attr_normal (ANSI * a)
243 a->terminal->xmit (a->terminal, "\033[0m", 4);
244 a->attr = CRT_ATTR_NORMAL;
245 a->color = ANSI_INVAL;
249 ansi_set_color (ANSI * a, int color)
256 if ((a->color == ANSI_INVAL) || (color != a->color))
258 fg = CRT_COLOR_FG (color);
259 bg = CRT_COLOR_BG (color);
261 if (fg & CRT_COLOR_INTENSITY)
270 if (bg & CRT_COLOR_INTENSITY)
279 i = sprintf (buf, "\033[%d;%dm", fg, bg);
281 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
284 a->terminal->xmit (a->terminal, buf, i);
290 ansi_set_attr (ANSI * a, int attr)
294 dif = attr ^ a->attr;
302 if (attr == CRT_ATTR_NORMAL)
304 ansi_force_attr_normal (a);
309 if (dif & CRT_ATTR_UNDERLINE)
311 if (attr & CRT_ATTR_UNDERLINE)
313 a->terminal->xmit (a->terminal, "\033[4m", 4);
317 a->terminal->xmit (a->terminal, "\033[24m", 5);
320 if (dif & CRT_ATTR_REVERSE)
322 if (attr & CRT_ATTR_REVERSE)
324 a->terminal->xmit (a->terminal, "\033[7m", 4);
328 a->terminal->xmit (a->terminal, "\033[27m", 5);
331 if (dif & CRT_ATTR_BOLD)
333 if (attr & CRT_ATTR_BOLD)
335 a->terminal->xmit (a->terminal, "\033[1m", 4);
339 a->terminal->xmit (a->terminal, "\033[21m", 5);
340 a->terminal->xmit (a->terminal, "\033[22m", 5);
347 ascii_emit (TTY * t, uint32_t ch)
351 /*Some quick obvious subsititons for quotation marks*/
366 /*Short cut the easy stuff*/
374 for (i = 0; i < VT102_CHARSET_SIZE; ++i)
376 if (vt102_charset_gl[i] == ch)
378 uint8_t c[3] = { 016, i, 017 };
389 ansi_render (ANSI * a, CRT_CA ca)
393 if ((ca.chr < VT102_CHARSET_SIZE) && (vt102_charset_c0[ca.chr]))
394 ca.chr = vt102_charset_c0[ca.chr];
396 if ((ca.chr >= 0x80) && (ca.chr < 0xa0))
399 ansi_set_attr (a, ca.attr);
400 ansi_set_color (a, ca.color);
403 utf8_emit (a->terminal, ca.chr);
405 ascii_emit (a->terminal, ca.chr);
409 /*Can't easily wrap round here as don't know size of destination screen*/
410 /*so invalidate the cached cursor position*/
412 if (a->pos.x >= a->size.x)
413 a->pos.x = ANSI_INVAL;
424 ansi_set_attr (a, CRT_ATTR_NORMAL);
425 ansi_set_color (a, CRT_COLOR_NORMAL);
427 a->terminal->xmit (a->terminal, "\033[2J", 4);
428 /*different emulators leave cursor in different places after cls differently*/
429 a->pos.x = ANSI_INVAL;
434 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
440 if (e.x != (CRT_COLS - 1))
444 if (s.y >= a->size.y)
447 ansi_showhide_cursor (a, 1);
448 i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
449 a->terminal->xmit (a->terminal, buf, i);
450 i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
451 a->terminal->xmit (a->terminal, buf, i);
452 a->terminal->xmit (a->terminal, "\033D", 2);
453 a->terminal->xmit (a->terminal, "\033[r", 3);
456 crt_erase (&a->crt, s, e, 1);
458 a->pos.x = ANSI_INVAL;
465 ansi_spot_scroll_up (ANSI * a, CRT * c)
469 l = c->sh.e.x - c->sh.s.x;
471 l *= sizeof (CRT_CA);
473 n = c->sh.e.y - c->sh.s.y;
474 p = CRT_ADDR_POS (&c->sh.s);
478 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
483 if (ansi_scroll_up (a, c->sh.s, c->sh.e))
486 n = c->sh.e.y - c->sh.s.y;
487 p = CRT_ADDR_POS (&c->sh.s);
491 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
495 c->sh.dir = 0; //FIXME: horrid hack
500 ansi_spot_scroll (ANSI * a, CRT * c)
506 /*we only care about up for the moment */
507 ansi_spot_scroll_up (a, c);
518 ansi_draw_line (ANSI * a, CRT_CA * cap, int y)
520 CRT_Pos p = { 0, y };
521 CRT_CA *acap = &a->crt.screen[CRT_ADDR_POS (&p)];
523 for (p.x = 0; p.x < a->crt.size.x; ++p.x)
525 if (p.x >= a->size.x)
528 if (crt_ca_cmp (*acap, *cap))
530 ansi_showhide_cursor (a, 1);
534 ansi_render (a, *acap);
543 ansi_resize_check (ANSI * a, CRT_Pos * size)
546 if ((size && crt_pos_cmp (a->crt.size, *size))
547 || crt_pos_cmp (a->terminal->size, a->size))
550 terminal_getsize (a->terminal);
552 a->size = a->terminal->size;
554 a->pos.x = ANSI_INVAL;
555 a->hide_cursor = ANSI_INVAL;
562 // FIXME: -- echos back crap?
563 // a->terminal->xmit (a->terminal, "\033[c", 3);
565 // maybe - issue 132 column command if we're 132?
568 a->terminal->xmit (a->terminal, "\033=", 2);
569 a->terminal->xmit (a->terminal, "\033[?6l", 5);
570 a->terminal->xmit (a->terminal, "\033[r", 3);
573 a->terminal->xmit (a->terminal, "\033%G", 3);
577 a->terminal->xmit (a->terminal, "\033(B", 3);
578 a->terminal->xmit (a->terminal, "\033)0", 3);
579 a->terminal->xmit (a->terminal, "\017", 1);
585 /*if they haven't then ansi_draw will patch it up*/
588 ansi_history (ANSI * a, History * h)
593 /*Do we need to catch up on history?*/
595 if (a->history_ptr == h->wptr)
597 ansi_resize_check (a, NULL);
599 if ((a->size.x < a->crt.size.x) || (a->size.y < a->crt.size.y))
602 guess_scroll = a->crt.size.y - 1; /*Bototm line should be a status line */
605 ansi_force_attr_normal (a);
606 ansi_set_color (a, CRT_COLOR_NORMAL);
608 i = sprintf (buf, "\033[%d;%dr", 1, guess_scroll);
609 a->terminal->xmit (a->terminal, buf, i);
612 while (a->history_ptr != h->wptr)
615 History_ent *e = &h->lines[a->history_ptr];
617 HISTORY_INC (h, a->history_ptr);
622 /*If so write the line ot the top of the screen */
623 ansi_draw_line (a, e->line, 0);
626 /*Roll guess_scroll lines up putting the top line into the xterm's history */
629 /*Make extra lines a predictable colour */
630 ansi_set_color (a, CRT_COLOR_NORMAL);
632 ansi_showhide_cursor (a, 1);
633 i = sprintf (buf, "\033[%d;%dH", guess_scroll, 1);
634 a->terminal->xmit (a->terminal, buf, i);
635 a->terminal->xmit (a->terminal, "\033D", 2);
636 a->pos.x = ANSI_INVAL;
638 /*now do the same in our image of the screen */
647 for (s.y++; s.y < guess_scroll; s.y++, e.y++)
649 memcpy (&a->crt.screen[CRT_ADDR_POS (&e)],
650 &a->crt.screen[CRT_ADDR_POS (&s)],
651 sizeof (CRT_CA) * a->crt.size.x);
657 crt_erase (&a->crt, s, e, 1, CRT_COLOR_NORMAL);
662 a->terminal->xmit (a->terminal, "\033[r", 3);
663 a->pos.x = ANSI_INVAL;
671 ansi_draw (ANSI * a, CRT * c)
675 int hidden_cursor = 0;
677 ansi_resize_check (a, &c->size);
680 for (p.y = 0; p.y < a->crt.size.y; ++p.y)
682 if (p.y >= a->size.y)
685 ansi_draw_line (a, &c->screen[CRT_ADDR (p.y, 0)], p.y);
690 if ((c->size.x > a->size.x) || (c->size.y > a->size.y))
692 char msg[1024]; // = "Window is too small";
698 sprintf (msg, "Window too small (%dx%d need %dx%d)", a->size.x,
699 a->size.y, c->size.x, c->size.y);
701 ansi_showhide_cursor (a, 1);
702 ansi_set_attr (a, CRT_ATTR_REVERSE);
703 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
706 a->terminal->xmit (a->terminal, msg, i);
707 a->pos.x = ANSI_INVAL;
711 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
713 ansi_showhide_cursor (a, 1);
718 ansi_move (a, a->crt.pos);
720 a->crt.hide_cursor = c->hide_cursor;
721 ansi_showhide_cursor (a, a->crt.hide_cursor);
725 ansi_reset (ANSI * a, CRT * c)
728 ansi_draw (a, c ? c : &a->crt);
732 ansi_terminal_reset (ANSI * a)
734 CRT_Pos p = { 0, a->crt.size.y };
735 ansi_force_attr_normal (a);
741 int ansi_key(ANSI *a,Context *c,int key)
745 return c->k->key (c->k, c, key);
747 cmd_show_status (c->d, c);
751 if (key == CMD_KEY) {
752 return cmd_deactivate (c->d, c);
754 return cmd_key (c->d, c,a, key);
756 } else if (key == CMD_KEY)
757 return cmd_activate (c->d, c);
761 return c->k->key (c->k, c, key);
766 ansi_flush_escape (ANSI * a, Context * c)
768 ANSI_Parser *p = &a->parser;
771 for (i = 0; i < p->escape_ptr; ++i)
773 ansi_key (a, c, p->escape_buf[i]);
781 ansi_parse_deckey (ANSI * a, Context * c)
783 ANSI_Parser *p = &a->parser;
784 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
786 ansi_flush_escape (a, c);
790 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
792 ansi_key (a, c, KEY_UP + (p->escape_buf[2] - 'A'));
794 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
796 ansi_key (a, c, KEY_154 + (p->escape_buf[2] - 'a'));
800 ansi_flush_escape (a, c);
808 ansi_parse_ansikey (ANSI * a, Context * c)
810 ANSI_Parser *p = &a->parser;
812 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
814 ansi_flush_escape (a, c);
817 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
819 ansi_key (a, c, KEY_180 + (p->escape_buf[2] - '0'));
823 ansi_flush_escape (a, c);
834 ansi_parse_escape (ANSI * a, Context * c)
836 ANSI_Parser *p = &a->parser;
837 switch (p->escape_ptr)
843 switch (p->escape_buf[1])
849 ansi_flush_escape (a, c);
853 switch (p->escape_buf[1])
856 ansi_parse_deckey (a, c);
859 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
860 ansi_parse_deckey (a, c);
863 ansi_flush_escape (a, c);
867 switch (p->escape_buf[1])
870 ansi_parse_ansikey (a, c);
873 ansi_flush_escape (a, c);
877 ansi_flush_escape (a, c);
883 ansi_check_escape (ANSI * a, Context * c)
885 ANSI_Parser *p = &a->parser;
886 struct timeval now, diff;
887 gettimeofday (&now, NULL);
888 timersub (&now, &p->last_escape, &diff);
891 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
892 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
900 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
901 ansi_flush_escape (a, c);
907 ansi_parse_char (ANSI * a, Context * c, int ch)
909 ANSI_Parser *p = &a->parser;
912 /*See if it's time to flush the escape*/
913 ansi_check_escape (a, c);
918 ansi_flush_escape (a, c);
922 gettimeofday (&p->last_escape, NULL);
927 p->escape_buf[p->escape_ptr++] = ch;
928 ansi_parse_escape (a, c);
938 ansi_parse (ANSI * a, Context * c, char *buf, int len)
941 ansi_parse_char (a, c, *(buf++));
945 ansi_dispatch (ANSI * a, Context * c)
950 ansi_check_escape (a, c);
956 red = a->terminal->recv (a->terminal, buf, sizeof (buf));
969 a->history_ptr = c->h->wptr;
970 HISTORY_INC (c->h, a->history_ptr);
977 ansi_parse (a, c, buf, red);
984 ansi_update (ANSI * a, Context * c)
986 ansi_history (a, c->h);
987 ansi_draw (a, &c->v->crt);
988 tty_length(a->terminal,c->v->crt.size.y);
994 a->terminal_reset (a);
996 a->terminal->close (a->terminal);
1003 ansi_new_from_terminal (TTY * t, int utf8)
1007 ret = malloc (sizeof (ANSI));
1008 memset (ret, 0, sizeof (ANSI));
1013 ret->update = ansi_update;
1014 ret->reset = ansi_reset;
1015 ret->terminal_reset = ansi_terminal_reset;
1016 ret->close = ansi_free;
1017 ret->dispatch = ansi_dispatch;