4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.36 2008/02/28 22:00:42 james
14 * *** empty log message ***
16 * Revision 1.35 2008/02/28 16:57:51 james
17 * *** empty log message ***
19 * Revision 1.34 2008/02/27 09:42:53 james
20 * *** empty log message ***
22 * Revision 1.33 2008/02/27 09:42:21 james
23 * *** empty log message ***
25 * Revision 1.32 2008/02/26 23:56:12 james
26 * *** empty log message ***
28 * Revision 1.31 2008/02/26 23:23:17 james
29 * *** empty log message ***
31 * Revision 1.30 2008/02/24 00:42:53 james
32 * *** empty log message ***
34 * Revision 1.29 2008/02/23 11:48:37 james
35 * *** empty log message ***
37 * Revision 1.28 2008/02/22 17:07:00 james
38 * *** empty log message ***
40 * Revision 1.27 2008/02/20 22:54:22 staffcvs
41 * *** empty log message ***
43 * Revision 1.26 2008/02/20 20:16:07 james
44 * *** empty log message ***
46 * Revision 1.25 2008/02/20 19:44:37 james
49 * Revision 1.24 2008/02/20 19:36:06 james
52 * Revision 1.23 2008/02/20 19:25:09 james
53 * *** empty log message ***
55 * Revision 1.22 2008/02/15 03:32:07 james
56 * *** empty log message ***
58 * Revision 1.21 2008/02/14 02:46:44 james
59 * *** empty log message ***
61 * Revision 1.20 2008/02/14 01:55:57 james
62 * *** empty log message ***
64 * Revision 1.19 2008/02/13 16:57:29 james
65 * *** empty log message ***
67 * Revision 1.18 2008/02/13 09:12:21 james
68 * *** empty log message ***
70 * Revision 1.17 2008/02/13 01:08:18 james
71 * *** empty log message ***
73 * Revision 1.16 2008/02/07 13:22:51 james
74 * *** empty log message ***
76 * Revision 1.15 2008/02/07 13:19:48 james
77 * *** empty log message ***
79 * Revision 1.14 2008/02/07 12:21:16 james
80 * *** empty log message ***
82 * Revision 1.13 2008/02/07 12:16:04 james
83 * *** empty log message ***
85 * Revision 1.12 2008/02/07 11:32:41 james
86 * *** empty log message ***
88 * Revision 1.11 2008/02/07 11:11:14 staffcvs
89 * *** empty log message ***
91 * Revision 1.10 2008/02/07 01:02:52 james
92 * *** empty log message ***
94 * Revision 1.9 2008/02/07 00:43:27 james
95 * *** empty log message ***
97 * Revision 1.8 2008/02/07 00:39:13 james
98 * *** empty log message ***
100 * Revision 1.7 2008/02/06 20:26:57 james
101 * *** empty log message ***
103 * Revision 1.6 2008/02/06 17:53:28 james
104 * *** empty log message ***
106 * Revision 1.5 2008/02/06 15:53:22 james
107 * *** empty log message ***
109 * Revision 1.4 2008/02/04 20:23:55 james
110 * *** empty log message ***
112 * Revision 1.3 2008/02/04 05:45:55 james
115 * Revision 1.2 2008/02/04 02:05:06 james
116 * *** empty log message ***
118 * Revision 1.1 2008/02/03 23:31:25 james
119 * *** empty log message ***
126 ansi_move (ANSI * a, CRT_Pos p)
130 int dx = p.x - a->pos.x;
131 int dy = p.y - a->pos.y;
133 // a->pos.x = ANSI_INVAL;
135 if (a->pos.x != ANSI_INVAL)
145 a->terminal->xmit (a->terminal, "\033[C", 3);
149 a->terminal->xmit (a->terminal, "\033[D", 3);
153 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
154 a->terminal->xmit (a->terminal, buf, n);
161 a->terminal->xmit (a->terminal, "\033[A", 3);
165 a->terminal->xmit (a->terminal, "\033[B", 3);
169 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
170 a->terminal->xmit (a->terminal, buf, n);
174 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
175 a->terminal->xmit (a->terminal, buf, n);
182 a->terminal->xmit (a->terminal, "\033[E", 3);
186 a->terminal->xmit (a->terminal, "\033[F", 3);
190 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
191 a->terminal->xmit (a->terminal, buf, n);
195 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
196 a->terminal->xmit (a->terminal, buf, n);
201 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
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);
216 ansi_showhide_cursor (ANSI * a, int hide)
218 if (a->hide_cursor == hide)
223 a->terminal->xmit (a->terminal, "\033[?25l", 6);
227 a->terminal->xmit (a->terminal, "\033[?25h", 6);
230 a->hide_cursor = hide;
235 ansi_force_attr_normal (ANSI * a)
237 a->terminal->xmit (a->terminal, "\033[0m", 4);
238 a->attr = CRT_ATTR_NORMAL;
239 a->color = ANSI_INVAL;
243 ansi_set_color (ANSI * a, int color)
250 if ((a->color == ANSI_INVAL) || (color != a->color))
252 fg = CRT_COLOR_FG (color);
253 bg = CRT_COLOR_BG (color);
255 if (fg & CRT_COLOR_INTENSITY)
264 if (bg & CRT_COLOR_INTENSITY)
273 i = sprintf (buf, "\033[%d;%dm", fg, bg);
275 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
278 a->terminal->xmit (a->terminal, buf, i);
284 ansi_set_attr (ANSI * a, int attr)
288 dif = attr ^ a->attr;
296 if (attr == CRT_ATTR_NORMAL)
298 ansi_force_attr_normal (a);
303 if (dif & CRT_ATTR_UNDERLINE)
305 if (attr & CRT_ATTR_UNDERLINE)
307 a->terminal->xmit (a->terminal, "\033[4m", 4);
311 a->terminal->xmit (a->terminal, "\033[24m", 5);
314 if (dif & CRT_ATTR_REVERSE)
316 if (attr & CRT_ATTR_REVERSE)
318 a->terminal->xmit (a->terminal, "\033[7m", 4);
322 a->terminal->xmit (a->terminal, "\033[27m", 5);
325 if (dif & CRT_ATTR_BOLD)
327 if (attr & CRT_ATTR_BOLD)
329 a->terminal->xmit (a->terminal, "\033[1m", 4);
333 a->terminal->xmit (a->terminal, "\033[21m", 5);
334 a->terminal->xmit (a->terminal, "\033[22m", 5);
341 ascii_emit (TTY * t, uint32_t ch)
345 /*Some quick obvious subsititons for quotation marks*/
360 /*Short cut the easy stuff*/
368 for (i = 0; i < VT102_CHARSET_SIZE; ++i)
370 if (vt102_charset_gl[i] == ch)
372 uint8_t c[3] = { 016, i, 017 };
383 ansi_render (ANSI * a, CRT_CA ca)
387 if ((ca.chr < VT102_CHARSET_SIZE) && (vt102_charset_c0[ca.chr]))
388 ca.chr = vt102_charset_c0[ca.chr];
390 if ((ca.chr >= 0x80) && (ca.chr < 0xa0))
393 ansi_set_attr (a, ca.attr);
394 ansi_set_color (a, ca.color);
397 utf8_emit (a->terminal, ca.chr);
399 ascii_emit (a->terminal, ca.chr);
403 /*Can't easily wrap round here as don't know size of destination screen*/
404 /*so invalidate the cached cursor position*/
406 if (a->pos.x >= a->size.x)
407 a->pos.x = ANSI_INVAL;
418 ansi_set_attr (a, CRT_ATTR_NORMAL);
419 ansi_set_color (a, CRT_COLOR_NORMAL);
421 a->terminal->xmit (a->terminal, "\033[2J", 4);
422 /*different emulators leave cursor in different places after cls differently*/
423 a->pos.x = ANSI_INVAL;
428 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
434 if (e.x != (CRT_COLS - 1))
438 if (s.y >= a->size.y)
441 ansi_showhide_cursor (a, 1);
442 i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
443 a->terminal->xmit (a->terminal, buf, i);
444 i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
445 a->terminal->xmit (a->terminal, buf, i);
446 a->terminal->xmit (a->terminal, "\033D", 2);
447 a->terminal->xmit (a->terminal, "\033[r", 3);
450 crt_erase (&a->crt, s, e, 1);
452 a->pos.x = ANSI_INVAL;
459 ansi_spot_scroll_up (ANSI * a, CRT * c)
463 l = c->sh.e.x - c->sh.s.x;
465 l *= sizeof (CRT_CA);
467 n = c->sh.e.y - c->sh.s.y;
468 p = CRT_ADDR_POS (&c->sh.s);
472 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
477 if (ansi_scroll_up (a, c->sh.s, c->sh.e))
480 n = c->sh.e.y - c->sh.s.y;
481 p = CRT_ADDR_POS (&c->sh.s);
485 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
489 c->sh.dir = 0; //FIXME: horrid hack
494 ansi_spot_scroll (ANSI * a, CRT * c)
500 /*we only care about up for the moment */
501 ansi_spot_scroll_up (a, c);
512 ansi_draw_line (ANSI * a, CRT_CA * cap, int y)
514 CRT_Pos p = { 0, y };
515 CRT_CA *acap = &a->crt.screen[CRT_ADDR_POS (&p)];
517 for (p.x = 0; p.x < a->crt.size.x; ++p.x)
519 if (p.x >= a->size.x)
522 if (crt_ca_cmp (*acap, *cap))
524 ansi_showhide_cursor (a, 1);
528 ansi_render (a, *acap);
537 ansi_resize_check (ANSI * a, CRT_Pos * size)
540 if ((size && crt_pos_cmp (a->crt.size, *size))
541 || crt_pos_cmp (a->terminal->size, a->size))
544 terminal_getsize (a->terminal);
546 a->size = a->terminal->size;
548 a->pos.x = ANSI_INVAL;
549 a->hide_cursor = ANSI_INVAL;
556 // FIXME: -- echos back crap?
557 // a->terminal->xmit (a->terminal, "\033[c", 3);
559 // maybe - issue 132 column command if we're 132?
562 a->terminal->xmit (a->terminal, "\033=", 2);
563 a->terminal->xmit (a->terminal, "\033[?6l", 5);
564 a->terminal->xmit (a->terminal, "\033[r", 3);
567 a->terminal->xmit (a->terminal, "\033%G", 3);
571 a->terminal->xmit (a->terminal, "\033(B", 3);
572 a->terminal->xmit (a->terminal, "\033)0", 3);
573 a->terminal->xmit (a->terminal, "\017", 1);
579 /*if they haven't then ansi_draw will patch it up*/
582 ansi_history (ANSI * a, History * h)
587 /*Do we need to catch up on history?*/
589 if (a->history_ptr == h->wptr)
591 ansi_resize_check (a, NULL);
593 if ((a->size.x < a->crt.size.x) || (a->size.y < a->crt.size.y))
596 guess_scroll = a->crt.size.y - 1; /*Bototm line should be a status line */
599 ansi_force_attr_normal (a);
600 ansi_set_color (a, CRT_COLOR_NORMAL);
602 i = sprintf (buf, "\033[%d;%dr", 1, guess_scroll);
603 a->terminal->xmit (a->terminal, buf, i);
606 while (a->history_ptr != h->wptr)
609 History_ent *e = &h->lines[a->history_ptr];
611 HISTORY_INC (h, a->history_ptr);
616 /*If so write the line ot the top of the screen */
617 ansi_draw_line (a, e->line, 0);
620 /*Roll guess_scroll lines up putting the top line into the xterm's history */
623 /*Make extra lines a predictable colour */
624 ansi_set_color (a, CRT_COLOR_NORMAL);
626 ansi_showhide_cursor (a, 1);
627 i = sprintf (buf, "\033[%d;%dH", guess_scroll, 1);
628 a->terminal->xmit (a->terminal, buf, i);
629 a->terminal->xmit (a->terminal, "\033D", 2);
630 a->pos.x = ANSI_INVAL;
632 /*now do the same in our image of the screen */
641 for (s.y++; s.y < guess_scroll; s.y++, e.y++)
643 memcpy (&a->crt.screen[CRT_ADDR_POS (&e)],
644 &a->crt.screen[CRT_ADDR_POS (&s)],
645 sizeof (CRT_CA) * a->crt.size.x);
651 crt_erase (&a->crt, s, e, 1, CRT_COLOR_NORMAL);
656 a->terminal->xmit (a->terminal, "\033[r", 3);
657 a->pos.x = ANSI_INVAL;
665 ansi_draw (ANSI * a, CRT * c)
669 int hidden_cursor = 0;
671 ansi_resize_check (a, &c->size);
674 for (p.y = 0; p.y < a->crt.size.y; ++p.y)
676 if (p.y >= a->size.y)
679 ansi_draw_line (a, &c->screen[CRT_ADDR (p.y, 0)], p.y);
684 if ((c->size.x > a->size.x) || (c->size.y > a->size.y))
686 char msg[1024]; // = "Window is too small";
692 sprintf (msg, "Window too small (%dx%d need %dx%d)", a->size.x,
693 a->size.y, c->size.x, c->size.y);
695 ansi_showhide_cursor (a, 1);
696 ansi_set_attr (a, CRT_ATTR_REVERSE);
697 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
700 a->terminal->xmit (a->terminal, msg, i);
701 a->pos.x = ANSI_INVAL;
705 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
707 ansi_showhide_cursor (a, 1);
712 ansi_move (a, a->crt.pos);
714 a->crt.hide_cursor = c->hide_cursor;
715 ansi_showhide_cursor (a, a->crt.hide_cursor);
719 ansi_reset (ANSI * a, CRT * c)
722 ansi_draw (a, c ? c : &a->crt);
726 ansi_terminal_reset (ANSI * a)
728 CRT_Pos p = { 0, a->crt.size.y };
729 ansi_force_attr_normal (a);
735 int ansi_key(ANSI *a,Context *c,int key)
739 return c->k->key (c->k, c, key);
741 cmd_show_status (c->d, c);
744 return cmd_key (c->d, c,a, key);
747 return cmd_activate (c->d, c);
750 return c->k->key (c->k, c, key);
755 ansi_flush_escape (ANSI * a, Context * c)
757 ANSI_Parser *p = &a->parser;
760 for (i = 0; i < p->escape_ptr; ++i)
762 ansi_key (a, c, p->escape_buf[i]);
770 ansi_parse_deckey (ANSI * a, Context * c)
772 ANSI_Parser *p = &a->parser;
773 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
775 ansi_flush_escape (a, c);
779 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
781 ansi_key (a, c, KEY_UP + (p->escape_buf[2] - 'A'));
783 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
785 ansi_key (a, c, KEY_154 + (p->escape_buf[2] - 'a'));
789 ansi_flush_escape (a, c);
797 ansi_parse_ansikey (ANSI * a, Context * c)
799 ANSI_Parser *p = &a->parser;
801 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
803 ansi_flush_escape (a, c);
806 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
808 ansi_key (a, c, KEY_180 + (p->escape_buf[2] - '0'));
812 ansi_flush_escape (a, c);
823 ansi_parse_escape (ANSI * a, Context * c)
825 ANSI_Parser *p = &a->parser;
826 switch (p->escape_ptr)
832 switch (p->escape_buf[1])
838 ansi_flush_escape (a, c);
842 switch (p->escape_buf[1])
845 ansi_parse_deckey (a, c);
848 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
849 ansi_parse_deckey (a, c);
852 ansi_flush_escape (a, c);
856 switch (p->escape_buf[1])
859 ansi_parse_ansikey (a, c);
862 ansi_flush_escape (a, c);
866 ansi_flush_escape (a, c);
872 ansi_check_escape (ANSI * a, Context * c)
874 ANSI_Parser *p = &a->parser;
875 struct timeval now, diff;
876 gettimeofday (&now, NULL);
877 timersub (&now, &p->last_escape, &diff);
880 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
881 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
889 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
890 ansi_flush_escape (a, c);
896 ansi_parse_char (ANSI * a, Context * c, int ch)
898 ANSI_Parser *p = &a->parser;
901 /*See if it's time to flush the escape*/
902 ansi_check_escape (a, c);
907 ansi_flush_escape (a, c);
911 gettimeofday (&p->last_escape, NULL);
916 p->escape_buf[p->escape_ptr++] = ch;
917 ansi_parse_escape (a, c);
927 ansi_parse (ANSI * a, Context * c, char *buf, int len)
930 ansi_parse_char (a, c, *(buf++));
934 ansi_dispatch (ANSI * a, Context * c)
939 ansi_check_escape (a, c);
945 red = a->terminal->recv (a->terminal, buf, sizeof (buf));
958 a->history_ptr = c->h->wptr;
959 HISTORY_INC (c->h, a->history_ptr);
966 ansi_parse (a, c, buf, red);
973 ansi_update (ANSI * a, Context * c)
975 ansi_history (a, c->h);
976 ansi_draw (a, &c->v->crt);
977 tty_length(a->terminal,c->v->crt.size.y);
983 a->terminal_reset (a);
985 a->terminal->close (a->terminal);
992 ansi_new_from_terminal (TTY * t, int utf8)
996 ret = malloc (sizeof (ANSI));
997 memset (ret, 0, sizeof (ANSI));
1002 ret->update = ansi_update;
1003 ret->reset = ansi_reset;
1004 ret->terminal_reset = ansi_terminal_reset;
1005 ret->close = ansi_free;
1006 ret->dispatch = ansi_dispatch;