4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.33 2008/02/27 09:42:21 james
14 * *** empty log message ***
16 * Revision 1.32 2008/02/26 23:56:12 james
17 * *** empty log message ***
19 * Revision 1.31 2008/02/26 23:23:17 james
20 * *** empty log message ***
22 * Revision 1.30 2008/02/24 00:42:53 james
23 * *** empty log message ***
25 * Revision 1.29 2008/02/23 11:48:37 james
26 * *** empty log message ***
28 * Revision 1.28 2008/02/22 17:07:00 james
29 * *** empty log message ***
31 * Revision 1.27 2008/02/20 22:54:22 staffcvs
32 * *** empty log message ***
34 * Revision 1.26 2008/02/20 20:16:07 james
35 * *** empty log message ***
37 * Revision 1.25 2008/02/20 19:44:37 james
40 * Revision 1.24 2008/02/20 19:36:06 james
43 * Revision 1.23 2008/02/20 19:25:09 james
44 * *** empty log message ***
46 * Revision 1.22 2008/02/15 03:32:07 james
47 * *** empty log message ***
49 * Revision 1.21 2008/02/14 02:46:44 james
50 * *** empty log message ***
52 * Revision 1.20 2008/02/14 01:55:57 james
53 * *** empty log message ***
55 * Revision 1.19 2008/02/13 16:57:29 james
56 * *** empty log message ***
58 * Revision 1.18 2008/02/13 09:12:21 james
59 * *** empty log message ***
61 * Revision 1.17 2008/02/13 01:08:18 james
62 * *** empty log message ***
64 * Revision 1.16 2008/02/07 13:22:51 james
65 * *** empty log message ***
67 * Revision 1.15 2008/02/07 13:19:48 james
68 * *** empty log message ***
70 * Revision 1.14 2008/02/07 12:21:16 james
71 * *** empty log message ***
73 * Revision 1.13 2008/02/07 12:16:04 james
74 * *** empty log message ***
76 * Revision 1.12 2008/02/07 11:32:41 james
77 * *** empty log message ***
79 * Revision 1.11 2008/02/07 11:11:14 staffcvs
80 * *** empty log message ***
82 * Revision 1.10 2008/02/07 01:02:52 james
83 * *** empty log message ***
85 * Revision 1.9 2008/02/07 00:43:27 james
86 * *** empty log message ***
88 * Revision 1.8 2008/02/07 00:39:13 james
89 * *** empty log message ***
91 * Revision 1.7 2008/02/06 20:26:57 james
92 * *** empty log message ***
94 * Revision 1.6 2008/02/06 17:53:28 james
95 * *** empty log message ***
97 * Revision 1.5 2008/02/06 15:53:22 james
98 * *** empty log message ***
100 * Revision 1.4 2008/02/04 20:23:55 james
101 * *** empty log message ***
103 * Revision 1.3 2008/02/04 05:45:55 james
106 * Revision 1.2 2008/02/04 02:05:06 james
107 * *** empty log message ***
109 * Revision 1.1 2008/02/03 23:31:25 james
110 * *** empty log message ***
117 ansi_move (ANSI * a, CRT_Pos p)
121 int dx = p.x - a->pos.x;
122 int dy = p.y - a->pos.y;
124 // a->pos.x = ANSI_INVAL;
126 if (a->pos.x != ANSI_INVAL)
136 a->terminal->xmit (a->terminal, "\033[C", 3);
140 a->terminal->xmit (a->terminal, "\033[D", 3);
144 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
145 a->terminal->xmit (a->terminal, buf, n);
152 a->terminal->xmit (a->terminal, "\033[A", 3);
156 a->terminal->xmit (a->terminal, "\033[B", 3);
160 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
161 a->terminal->xmit (a->terminal, buf, n);
165 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
166 a->terminal->xmit (a->terminal, buf, n);
173 a->terminal->xmit (a->terminal, "\033[E", 3);
177 a->terminal->xmit (a->terminal, "\033[F", 3);
181 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
182 a->terminal->xmit (a->terminal, buf, n);
186 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
187 a->terminal->xmit (a->terminal, buf, n);
192 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
193 a->terminal->xmit (a->terminal, buf, n);
198 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
199 a->terminal->xmit (a->terminal, buf, n);
207 ansi_showhide_cursor (ANSI * a, int hide)
209 if (a->hide_cursor == hide)
214 a->terminal->xmit (a->terminal, "\033[?25l", 6);
218 a->terminal->xmit (a->terminal, "\033[?25h", 6);
221 a->hide_cursor = hide;
226 ansi_force_attr_normal (ANSI * a)
228 a->terminal->xmit (a->terminal, "\033[0m", 4);
229 a->attr = CRT_ATTR_NORMAL;
230 a->color = ANSI_INVAL;
234 ansi_set_color (ANSI * a, int color)
241 if ((a->color == ANSI_INVAL) || (color != a->color))
243 fg = CRT_COLOR_FG (color);
244 bg = CRT_COLOR_BG (color);
246 if (fg & CRT_COLOR_INTENSITY)
255 if (bg & CRT_COLOR_INTENSITY)
264 i = sprintf (buf, "\033[%d;%dm", fg, bg);
266 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
269 a->terminal->xmit (a->terminal, buf, i);
275 ansi_set_attr (ANSI * a, int attr)
279 dif = attr ^ a->attr;
287 if (attr == CRT_ATTR_NORMAL)
289 ansi_force_attr_normal (a);
294 if (dif & CRT_ATTR_UNDERLINE)
296 if (attr & CRT_ATTR_UNDERLINE)
298 a->terminal->xmit (a->terminal, "\033[4m", 4);
302 a->terminal->xmit (a->terminal, "\033[24m", 5);
305 if (dif & CRT_ATTR_REVERSE)
307 if (attr & CRT_ATTR_REVERSE)
309 a->terminal->xmit (a->terminal, "\033[7m", 4);
313 a->terminal->xmit (a->terminal, "\033[27m", 5);
316 if (dif & CRT_ATTR_BOLD)
318 if (attr & CRT_ATTR_BOLD)
320 a->terminal->xmit (a->terminal, "\033[1m", 4);
324 a->terminal->xmit (a->terminal, "\033[21m", 5);
325 a->terminal->xmit (a->terminal, "\033[22m", 5);
332 ascii_emit (TTY * t, uint32_t ch)
336 /*Some quick obvious subsititons for quotation marks*/
351 /*Short cut the easy stuff*/
359 for (i = 0; i < VT102_CHARSET_SIZE; ++i)
361 if (vt102_charset_gl[i] == ch)
363 uint8_t c[3] = { 016, i, 017 };
374 ansi_render (ANSI * a, CRT_CA ca)
378 if ((ca.chr < VT102_CHARSET_SIZE) && (vt102_charset_c0[ca.chr]))
379 ca.chr = vt102_charset_c0[ca.chr];
381 if ((ca.chr >= 0x80) && (ca.chr < 0xa0))
384 ansi_set_attr (a, ca.attr);
385 ansi_set_color (a, ca.color);
388 utf8_emit (a->terminal, ca.chr);
390 ascii_emit (a->terminal, ca.chr);
394 /*Can't easily wrap round here as don't know size of destination screen*/
395 /*so invalidate the cached cursor position*/
397 if (a->pos.x >= a->size.x)
398 a->pos.x = ANSI_INVAL;
409 ansi_set_attr (a, CRT_ATTR_NORMAL);
410 ansi_set_color (a, CRT_COLOR_NORMAL);
412 a->terminal->xmit (a->terminal, "\033[2J", 4);
413 /*different emulators leave cursor in different places after cls differently*/
414 a->pos.x = ANSI_INVAL;
419 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
425 if (e.x != (CRT_COLS - 1))
429 if (s.y >= a->size.y)
432 ansi_showhide_cursor (a, 1);
433 i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
434 a->terminal->xmit (a->terminal, buf, i);
435 i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
436 a->terminal->xmit (a->terminal, buf, i);
437 a->terminal->xmit (a->terminal, "\033D", 2);
438 a->terminal->xmit (a->terminal, "\033[r", 3);
441 crt_erase (&a->crt, s, e, 1);
443 a->pos.x = ANSI_INVAL;
450 ansi_spot_scroll_up (ANSI * a, CRT * c)
454 l = c->sh.e.x - c->sh.s.x;
456 l *= sizeof (CRT_CA);
458 n = c->sh.e.y - c->sh.s.y;
459 p = CRT_ADDR_POS (&c->sh.s);
463 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
468 if (ansi_scroll_up (a, c->sh.s, c->sh.e))
471 n = c->sh.e.y - c->sh.s.y;
472 p = CRT_ADDR_POS (&c->sh.s);
476 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
480 c->sh.dir = 0; //FIXME: horrid hack
485 ansi_spot_scroll (ANSI * a, CRT * c)
491 /*we only care about up for the moment */
492 ansi_spot_scroll_up (a, c);
503 ansi_draw_line (ANSI * a, CRT_CA * cap, int y)
505 CRT_Pos p = { 0, y };
506 CRT_CA *acap = &a->crt.screen[CRT_ADDR_POS (&p)];
508 for (p.x = 0; p.x < a->crt.size.x; ++p.x)
510 if (p.x >= a->size.x)
513 if (crt_ca_cmp (*acap, *cap))
515 ansi_showhide_cursor (a, 1);
519 ansi_render (a, *acap);
528 ansi_resize_check (ANSI * a, CRT_Pos *size)
531 if ((size && crt_pos_cmp(a->crt.size,*size))
532 || crt_pos_cmp (a->terminal->size, a->size))
535 terminal_getsize (a->terminal);
537 a->size = a->terminal->size;
539 a->pos.x = ANSI_INVAL;
540 a->hide_cursor = ANSI_INVAL;
547 // FIXME: -- echos back crap?
548 // a->terminal->xmit (a->terminal, "\033[c", 3);
550 // maybe - issue 132 column command if we're 132?
553 a->terminal->xmit (a->terminal, "\033=", 2);
554 a->terminal->xmit (a->terminal, "\033[?6l", 5);
555 a->terminal->xmit (a->terminal, "\033[r", 3);
558 a->terminal->xmit (a->terminal, "\033%G", 3);
562 a->terminal->xmit (a->terminal, "\033(B", 3);
563 a->terminal->xmit (a->terminal, "\033)0", 3);
564 a->terminal->xmit (a->terminal, "\017", 1);
570 /*if they haven't then ansi_draw will patch it up*/
573 ansi_history (ANSI * a, History * h)
578 /*Do we need to catch up on history?*/
580 if (a->history_ptr == h->wptr)
582 ansi_resize_check (a, NULL);
584 if ((a->size.x < a->crt.size.x) || (a->size.y < a->crt.size.y))
587 guess_scroll=a->crt.size.y-1; /*Bototm line should be a status line*/
590 ansi_force_attr_normal (a);
591 ansi_set_color (a, CRT_COLOR_NORMAL);
593 i = sprintf (buf, "\033[%d;%dr", 1, guess_scroll);
594 a->terminal->xmit (a->terminal, buf, i);
597 while (a->history_ptr != h->wptr)
600 History_ent *e = &h->lines[a->history_ptr];
602 HISTORY_INC (h, a->history_ptr);
607 /*If so write the line ot the top of the screen */
608 ansi_draw_line (a, e->line, 0);
611 /*Roll guess_scroll lines up putting the top line into the xterm's history */
614 /*Make extra lines a predictable colour */
615 ansi_set_color (a, CRT_COLOR_NORMAL);
617 ansi_showhide_cursor (a, 1);
618 i = sprintf (buf, "\033[%d;%dH", guess_scroll, 1);
619 a->terminal->xmit (a->terminal, buf, i);
620 a->terminal->xmit (a->terminal, "\033D", 2);
621 a->pos.x = ANSI_INVAL;
623 /*now do the same in our image of the screen */
632 for (s.y++; s.y < guess_scroll; s.y++, e.y++)
634 memcpy (&a->crt.screen[CRT_ADDR_POS (&e)],
635 &a->crt.screen[CRT_ADDR_POS (&s)],
636 sizeof (CRT_CA) * a->crt.size.x);
642 crt_erase (&a->crt, s, e, 1, CRT_COLOR_NORMAL);
647 a->terminal->xmit (a->terminal, "\033[r", 3);
648 a->pos.x = ANSI_INVAL;
656 ansi_draw (ANSI * a, CRT * c)
660 int hidden_cursor = 0;
662 ansi_resize_check (a, &c->size);
665 for (p.y = 0; p.y < a->crt.size.y; ++p.y)
667 if (p.y >= a->size.y)
670 ansi_draw_line (a, &c->screen[CRT_ADDR (p.y, 0)], p.y);
675 if ((c->size.x > a->size.x) || (c->size.y > a->size.y))
677 char msg[1024]; // = "Window is too small";
683 sprintf (msg, "Window too small (%dx%d need %dx%d)", a->size.x,
684 a->size.y, c->size.x, c->size.y);
686 ansi_showhide_cursor (a, 1);
687 ansi_set_attr (a, CRT_ATTR_REVERSE);
688 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
691 a->terminal->xmit (a->terminal, msg, i);
692 a->pos.x = ANSI_INVAL;
696 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
698 ansi_showhide_cursor (a, 1);
703 ansi_move (a, a->crt.pos);
705 a->crt.hide_cursor = c->hide_cursor;
706 ansi_showhide_cursor (a, a->crt.hide_cursor);
710 ansi_reset (ANSI * a, CRT * c)
713 ansi_draw (a, c ? c : &a->crt);
717 ansi_terminal_reset (ANSI * a)
719 CRT_Pos p = { 0, a->crt.size.y};
720 ansi_force_attr_normal (a);
726 ansi_flush_escape (ANSI * a, Context * c)
728 ANSI_Parser *p = &a->parser;
731 for (i = 0; i < p->escape_ptr; ++i)
733 keydis_key (c->k, c, p->escape_buf[i]);
741 ansi_parse_deckey (ANSI * a, Context * c)
743 ANSI_Parser *p = &a->parser;
744 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
746 ansi_flush_escape (a, c);
750 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
752 keydis_key (c->k, c, KEY_UP + (p->escape_buf[2] - 'A'));
754 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
756 keydis_key (c->k, c, KEY_154 + (p->escape_buf[2] - 'a'));
760 ansi_flush_escape (a, c);
768 ansi_parse_ansikey (ANSI * a, Context * c)
770 ANSI_Parser *p = &a->parser;
772 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
774 ansi_flush_escape (a, c);
777 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
779 keydis_key (c->k, c, KEY_180 + (p->escape_buf[2] - '0'));
783 ansi_flush_escape (a, c);
794 ansi_parse_escape (ANSI * a, Context * c)
796 ANSI_Parser *p = &a->parser;
797 switch (p->escape_ptr)
803 switch (p->escape_buf[1])
809 ansi_flush_escape (a, c);
813 switch (p->escape_buf[1])
816 ansi_parse_deckey (a, c);
819 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
820 ansi_parse_deckey (a, c);
823 ansi_flush_escape (a, c);
827 switch (p->escape_buf[1])
830 ansi_parse_ansikey (a, c);
833 ansi_flush_escape (a, c);
837 ansi_flush_escape (a, c);
843 ansi_check_escape (ANSI * a, Context * c)
845 ANSI_Parser *p = &a->parser;
846 struct timeval now, diff;
847 gettimeofday (&now, NULL);
848 timersub (&now, &p->last_escape, &diff);
851 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
852 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
860 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
861 ansi_flush_escape (a, c);
867 ansi_parse_char (ANSI * a, Context * c, int ch)
869 ANSI_Parser *p = &a->parser;
872 /*See if it's time to flush the escape*/
873 ansi_check_escape (a, c);
878 ansi_flush_escape (a, c);
882 gettimeofday (&p->last_escape, NULL);
887 p->escape_buf[p->escape_ptr++] = ch;
888 ansi_parse_escape (a, c);
892 keydis_key (c->k, c, ch);
898 ansi_parse (ANSI * a, Context * c, char *buf, int len)
901 ansi_parse_char (a, c, *(buf++));
905 ansi_dispatch (ANSI * a, Context * c)
910 ansi_check_escape (a, c);
916 red = a->terminal->recv (a->terminal, buf, sizeof (buf));
929 a->history_ptr = c->h->wptr;
930 HISTORY_INC (c->h, a->history_ptr);
937 ansi_parse (a, c, buf, red);
944 ansi_update (ANSI * a, Context * c)
946 ansi_history (a, c->h);
947 ansi_draw (a, &c->v->crt);
953 a->terminal_reset (a);
955 a->terminal->close (a->terminal);
962 ansi_new_from_terminal (TTY * t, int utf8)
966 ret = malloc (sizeof (ANSI));
967 memset (ret, 0, sizeof (ANSI));
972 ret->update = ansi_update;
973 ret->reset = ansi_reset;
974 ret->terminal_reset = ansi_terminal_reset;
975 ret->close = ansi_free;
976 ret->dispatch = ansi_dispatch;