4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.37 2008/02/29 22:50:29 james
14 * *** empty log message ***
16 * Revision 1.36 2008/02/28 22:00:42 james
17 * *** empty log message ***
19 * Revision 1.35 2008/02/28 16:57:51 james
20 * *** empty log message ***
22 * Revision 1.34 2008/02/27 09:42:53 james
23 * *** empty log message ***
25 * Revision 1.33 2008/02/27 09:42:21 james
26 * *** empty log message ***
28 * Revision 1.32 2008/02/26 23:56:12 james
29 * *** empty log message ***
31 * Revision 1.31 2008/02/26 23:23:17 james
32 * *** empty log message ***
34 * Revision 1.30 2008/02/24 00:42:53 james
35 * *** empty log message ***
37 * Revision 1.29 2008/02/23 11:48:37 james
38 * *** empty log message ***
40 * Revision 1.28 2008/02/22 17:07:00 james
41 * *** empty log message ***
43 * Revision 1.27 2008/02/20 22:54:22 staffcvs
44 * *** empty log message ***
46 * Revision 1.26 2008/02/20 20:16:07 james
47 * *** empty log message ***
49 * Revision 1.25 2008/02/20 19:44:37 james
52 * Revision 1.24 2008/02/20 19:36:06 james
55 * Revision 1.23 2008/02/20 19:25:09 james
56 * *** empty log message ***
58 * Revision 1.22 2008/02/15 03:32:07 james
59 * *** empty log message ***
61 * Revision 1.21 2008/02/14 02:46:44 james
62 * *** empty log message ***
64 * Revision 1.20 2008/02/14 01:55:57 james
65 * *** empty log message ***
67 * Revision 1.19 2008/02/13 16:57:29 james
68 * *** empty log message ***
70 * Revision 1.18 2008/02/13 09:12:21 james
71 * *** empty log message ***
73 * Revision 1.17 2008/02/13 01:08:18 james
74 * *** empty log message ***
76 * Revision 1.16 2008/02/07 13:22:51 james
77 * *** empty log message ***
79 * Revision 1.15 2008/02/07 13:19:48 james
80 * *** empty log message ***
82 * Revision 1.14 2008/02/07 12:21:16 james
83 * *** empty log message ***
85 * Revision 1.13 2008/02/07 12:16:04 james
86 * *** empty log message ***
88 * Revision 1.12 2008/02/07 11:32:41 james
89 * *** empty log message ***
91 * Revision 1.11 2008/02/07 11:11:14 staffcvs
92 * *** empty log message ***
94 * Revision 1.10 2008/02/07 01:02:52 james
95 * *** empty log message ***
97 * Revision 1.9 2008/02/07 00:43:27 james
98 * *** empty log message ***
100 * Revision 1.8 2008/02/07 00:39:13 james
101 * *** empty log message ***
103 * Revision 1.7 2008/02/06 20:26:57 james
104 * *** empty log message ***
106 * Revision 1.6 2008/02/06 17:53:28 james
107 * *** empty log message ***
109 * Revision 1.5 2008/02/06 15:53:22 james
110 * *** empty log message ***
112 * Revision 1.4 2008/02/04 20:23:55 james
113 * *** empty log message ***
115 * Revision 1.3 2008/02/04 05:45:55 james
118 * Revision 1.2 2008/02/04 02:05:06 james
119 * *** empty log message ***
121 * Revision 1.1 2008/02/03 23:31:25 james
122 * *** empty log message ***
129 ansi_move (ANSI * a, CRT_Pos p)
133 int dx = p.x - a->pos.x;
134 int dy = p.y - a->pos.y;
136 // a->pos.x = ANSI_INVAL;
138 if (a->pos.x != ANSI_INVAL)
148 a->terminal->xmit (a->terminal, "\033[C", 3);
152 a->terminal->xmit (a->terminal, "\033[D", 3);
156 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
157 a->terminal->xmit (a->terminal, buf, n);
164 a->terminal->xmit (a->terminal, "\033[A", 3);
168 a->terminal->xmit (a->terminal, "\033[B", 3);
172 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
173 a->terminal->xmit (a->terminal, buf, n);
177 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
178 a->terminal->xmit (a->terminal, buf, n);
185 a->terminal->xmit (a->terminal, "\033[E", 3);
189 a->terminal->xmit (a->terminal, "\033[F", 3);
193 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
194 a->terminal->xmit (a->terminal, buf, n);
198 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
199 a->terminal->xmit (a->terminal, buf, n);
204 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
205 a->terminal->xmit (a->terminal, buf, n);
210 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
211 a->terminal->xmit (a->terminal, buf, n);
219 ansi_showhide_cursor (ANSI * a, int hide)
221 if (a->hide_cursor == hide)
226 a->terminal->xmit (a->terminal, "\033[?25l", 6);
230 a->terminal->xmit (a->terminal, "\033[?25h", 6);
233 a->hide_cursor = hide;
238 ansi_force_attr_normal (ANSI * a)
240 a->terminal->xmit (a->terminal, "\033[0m", 4);
241 a->attr = CRT_ATTR_NORMAL;
242 a->color = ANSI_INVAL;
246 ansi_set_color (ANSI * a, int color)
253 if ((a->color == ANSI_INVAL) || (color != a->color))
255 fg = CRT_COLOR_FG (color);
256 bg = CRT_COLOR_BG (color);
258 if (fg & CRT_COLOR_INTENSITY)
267 if (bg & CRT_COLOR_INTENSITY)
276 i = sprintf (buf, "\033[%d;%dm", fg, bg);
278 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
281 a->terminal->xmit (a->terminal, buf, i);
287 ansi_set_attr (ANSI * a, int attr)
291 dif = attr ^ a->attr;
299 if (attr == CRT_ATTR_NORMAL)
301 ansi_force_attr_normal (a);
306 if (dif & CRT_ATTR_UNDERLINE)
308 if (attr & CRT_ATTR_UNDERLINE)
310 a->terminal->xmit (a->terminal, "\033[4m", 4);
314 a->terminal->xmit (a->terminal, "\033[24m", 5);
317 if (dif & CRT_ATTR_REVERSE)
319 if (attr & CRT_ATTR_REVERSE)
321 a->terminal->xmit (a->terminal, "\033[7m", 4);
325 a->terminal->xmit (a->terminal, "\033[27m", 5);
328 if (dif & CRT_ATTR_BOLD)
330 if (attr & CRT_ATTR_BOLD)
332 a->terminal->xmit (a->terminal, "\033[1m", 4);
336 a->terminal->xmit (a->terminal, "\033[21m", 5);
337 a->terminal->xmit (a->terminal, "\033[22m", 5);
344 ascii_emit (TTY * t, uint32_t ch)
348 /*Some quick obvious subsititons for quotation marks*/
363 /*Short cut the easy stuff*/
371 for (i = 0; i < VT102_CHARSET_SIZE; ++i)
373 if (vt102_charset_gl[i] == ch)
375 uint8_t c[3] = { 016, i, 017 };
386 ansi_render (ANSI * a, CRT_CA ca)
390 if ((ca.chr < VT102_CHARSET_SIZE) && (vt102_charset_c0[ca.chr]))
391 ca.chr = vt102_charset_c0[ca.chr];
393 if ((ca.chr >= 0x80) && (ca.chr < 0xa0))
396 ansi_set_attr (a, ca.attr);
397 ansi_set_color (a, ca.color);
400 utf8_emit (a->terminal, ca.chr);
402 ascii_emit (a->terminal, ca.chr);
406 /*Can't easily wrap round here as don't know size of destination screen*/
407 /*so invalidate the cached cursor position*/
409 if (a->pos.x >= a->size.x)
410 a->pos.x = ANSI_INVAL;
421 ansi_set_attr (a, CRT_ATTR_NORMAL);
422 ansi_set_color (a, CRT_COLOR_NORMAL);
424 a->terminal->xmit (a->terminal, "\033[2J", 4);
425 /*different emulators leave cursor in different places after cls differently*/
426 a->pos.x = ANSI_INVAL;
431 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
437 if (e.x != (CRT_COLS - 1))
441 if (s.y >= a->size.y)
444 ansi_showhide_cursor (a, 1);
445 i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
446 a->terminal->xmit (a->terminal, buf, i);
447 i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
448 a->terminal->xmit (a->terminal, buf, i);
449 a->terminal->xmit (a->terminal, "\033D", 2);
450 a->terminal->xmit (a->terminal, "\033[r", 3);
453 crt_erase (&a->crt, s, e, 1);
455 a->pos.x = ANSI_INVAL;
462 ansi_spot_scroll_up (ANSI * a, CRT * c)
466 l = c->sh.e.x - c->sh.s.x;
468 l *= sizeof (CRT_CA);
470 n = c->sh.e.y - c->sh.s.y;
471 p = CRT_ADDR_POS (&c->sh.s);
475 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
480 if (ansi_scroll_up (a, c->sh.s, c->sh.e))
483 n = c->sh.e.y - c->sh.s.y;
484 p = CRT_ADDR_POS (&c->sh.s);
488 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
492 c->sh.dir = 0; //FIXME: horrid hack
497 ansi_spot_scroll (ANSI * a, CRT * c)
503 /*we only care about up for the moment */
504 ansi_spot_scroll_up (a, c);
515 ansi_draw_line (ANSI * a, CRT_CA * cap, int y)
517 CRT_Pos p = { 0, y };
518 CRT_CA *acap = &a->crt.screen[CRT_ADDR_POS (&p)];
520 for (p.x = 0; p.x < a->crt.size.x; ++p.x)
522 if (p.x >= a->size.x)
525 if (crt_ca_cmp (*acap, *cap))
527 ansi_showhide_cursor (a, 1);
531 ansi_render (a, *acap);
540 ansi_resize_check (ANSI * a, CRT_Pos * size)
543 if ((size && crt_pos_cmp (a->crt.size, *size))
544 || crt_pos_cmp (a->terminal->size, a->size))
547 terminal_getsize (a->terminal);
549 a->size = a->terminal->size;
551 a->pos.x = ANSI_INVAL;
552 a->hide_cursor = ANSI_INVAL;
559 // FIXME: -- echos back crap?
560 // a->terminal->xmit (a->terminal, "\033[c", 3);
562 // maybe - issue 132 column command if we're 132?
565 a->terminal->xmit (a->terminal, "\033=", 2);
566 a->terminal->xmit (a->terminal, "\033[?6l", 5);
567 a->terminal->xmit (a->terminal, "\033[r", 3);
570 a->terminal->xmit (a->terminal, "\033%G", 3);
574 a->terminal->xmit (a->terminal, "\033(B", 3);
575 a->terminal->xmit (a->terminal, "\033)0", 3);
576 a->terminal->xmit (a->terminal, "\017", 1);
582 /*if they haven't then ansi_draw will patch it up*/
585 ansi_history (ANSI * a, History * h)
590 /*Do we need to catch up on history?*/
592 if (a->history_ptr == h->wptr)
594 ansi_resize_check (a, NULL);
596 if ((a->size.x < a->crt.size.x) || (a->size.y < a->crt.size.y))
599 guess_scroll = a->crt.size.y - 1; /*Bototm line should be a status line */
602 ansi_force_attr_normal (a);
603 ansi_set_color (a, CRT_COLOR_NORMAL);
605 i = sprintf (buf, "\033[%d;%dr", 1, guess_scroll);
606 a->terminal->xmit (a->terminal, buf, i);
609 while (a->history_ptr != h->wptr)
612 History_ent *e = &h->lines[a->history_ptr];
614 HISTORY_INC (h, a->history_ptr);
619 /*If so write the line ot the top of the screen */
620 ansi_draw_line (a, e->line, 0);
623 /*Roll guess_scroll lines up putting the top line into the xterm's history */
626 /*Make extra lines a predictable colour */
627 ansi_set_color (a, CRT_COLOR_NORMAL);
629 ansi_showhide_cursor (a, 1);
630 i = sprintf (buf, "\033[%d;%dH", guess_scroll, 1);
631 a->terminal->xmit (a->terminal, buf, i);
632 a->terminal->xmit (a->terminal, "\033D", 2);
633 a->pos.x = ANSI_INVAL;
635 /*now do the same in our image of the screen */
644 for (s.y++; s.y < guess_scroll; s.y++, e.y++)
646 memcpy (&a->crt.screen[CRT_ADDR_POS (&e)],
647 &a->crt.screen[CRT_ADDR_POS (&s)],
648 sizeof (CRT_CA) * a->crt.size.x);
654 crt_erase (&a->crt, s, e, 1, CRT_COLOR_NORMAL);
659 a->terminal->xmit (a->terminal, "\033[r", 3);
660 a->pos.x = ANSI_INVAL;
668 ansi_draw (ANSI * a, CRT * c)
672 int hidden_cursor = 0;
674 ansi_resize_check (a, &c->size);
677 for (p.y = 0; p.y < a->crt.size.y; ++p.y)
679 if (p.y >= a->size.y)
682 ansi_draw_line (a, &c->screen[CRT_ADDR (p.y, 0)], p.y);
687 if ((c->size.x > a->size.x) || (c->size.y > a->size.y))
689 char msg[1024]; // = "Window is too small";
695 sprintf (msg, "Window too small (%dx%d need %dx%d)", a->size.x,
696 a->size.y, c->size.x, c->size.y);
698 ansi_showhide_cursor (a, 1);
699 ansi_set_attr (a, CRT_ATTR_REVERSE);
700 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
703 a->terminal->xmit (a->terminal, msg, i);
704 a->pos.x = ANSI_INVAL;
708 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
710 ansi_showhide_cursor (a, 1);
715 ansi_move (a, a->crt.pos);
717 a->crt.hide_cursor = c->hide_cursor;
718 ansi_showhide_cursor (a, a->crt.hide_cursor);
722 ansi_reset (ANSI * a, CRT * c)
725 ansi_draw (a, c ? c : &a->crt);
729 ansi_terminal_reset (ANSI * a)
731 CRT_Pos p = { 0, a->crt.size.y };
732 ansi_force_attr_normal (a);
738 int ansi_key(ANSI *a,Context *c,int key)
742 return c->k->key (c->k, c, key);
744 cmd_show_status (c->d, c);
749 return cmd_deactivate (c->d, c);
751 return cmd_key (c->d, c,a, key);
753 } else if (key == CMD_KEY)
754 return cmd_activate (c->d, c);
758 return c->k->key (c->k, c, key);
763 ansi_flush_escape (ANSI * a, Context * c)
765 ANSI_Parser *p = &a->parser;
768 for (i = 0; i < p->escape_ptr; ++i)
770 ansi_key (a, c, p->escape_buf[i]);
778 ansi_parse_deckey (ANSI * a, Context * c)
780 ANSI_Parser *p = &a->parser;
781 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
783 ansi_flush_escape (a, c);
787 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
789 ansi_key (a, c, KEY_UP + (p->escape_buf[2] - 'A'));
791 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
793 ansi_key (a, c, KEY_154 + (p->escape_buf[2] - 'a'));
797 ansi_flush_escape (a, c);
805 ansi_parse_ansikey (ANSI * a, Context * c)
807 ANSI_Parser *p = &a->parser;
809 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
811 ansi_flush_escape (a, c);
814 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
816 ansi_key (a, c, KEY_180 + (p->escape_buf[2] - '0'));
820 ansi_flush_escape (a, c);
831 ansi_parse_escape (ANSI * a, Context * c)
833 ANSI_Parser *p = &a->parser;
834 switch (p->escape_ptr)
840 switch (p->escape_buf[1])
846 ansi_flush_escape (a, c);
850 switch (p->escape_buf[1])
853 ansi_parse_deckey (a, c);
856 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
857 ansi_parse_deckey (a, c);
860 ansi_flush_escape (a, c);
864 switch (p->escape_buf[1])
867 ansi_parse_ansikey (a, c);
870 ansi_flush_escape (a, c);
874 ansi_flush_escape (a, c);
880 ansi_check_escape (ANSI * a, Context * c)
882 ANSI_Parser *p = &a->parser;
883 struct timeval now, diff;
884 gettimeofday (&now, NULL);
885 timersub (&now, &p->last_escape, &diff);
888 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
889 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
897 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
898 ansi_flush_escape (a, c);
904 ansi_parse_char (ANSI * a, Context * c, int ch)
906 ANSI_Parser *p = &a->parser;
909 /*See if it's time to flush the escape*/
910 ansi_check_escape (a, c);
915 ansi_flush_escape (a, c);
919 gettimeofday (&p->last_escape, NULL);
924 p->escape_buf[p->escape_ptr++] = ch;
925 ansi_parse_escape (a, c);
935 ansi_parse (ANSI * a, Context * c, char *buf, int len)
938 ansi_parse_char (a, c, *(buf++));
942 ansi_dispatch (ANSI * a, Context * c)
947 ansi_check_escape (a, c);
953 red = a->terminal->recv (a->terminal, buf, sizeof (buf));
966 a->history_ptr = c->h->wptr;
967 HISTORY_INC (c->h, a->history_ptr);
974 ansi_parse (a, c, buf, red);
981 ansi_update (ANSI * a, Context * c)
983 ansi_history (a, c->h);
984 ansi_draw (a, &c->v->crt);
985 tty_length(a->terminal,c->v->crt.size.y);
991 a->terminal_reset (a);
993 a->terminal->close (a->terminal);
1000 ansi_new_from_terminal (TTY * t, int utf8)
1004 ret = malloc (sizeof (ANSI));
1005 memset (ret, 0, sizeof (ANSI));
1010 ret->update = ansi_update;
1011 ret->reset = ansi_reset;
1012 ret->terminal_reset = ansi_terminal_reset;
1013 ret->close = ansi_free;
1014 ret->dispatch = ansi_dispatch;