4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.31 2008/02/26 23:23:17 james
14 * *** empty log message ***
16 * Revision 1.30 2008/02/24 00:42:53 james
17 * *** empty log message ***
19 * Revision 1.29 2008/02/23 11:48:37 james
20 * *** empty log message ***
22 * Revision 1.28 2008/02/22 17:07:00 james
23 * *** empty log message ***
25 * Revision 1.27 2008/02/20 22:54:22 staffcvs
26 * *** empty log message ***
28 * Revision 1.26 2008/02/20 20:16:07 james
29 * *** empty log message ***
31 * Revision 1.25 2008/02/20 19:44:37 james
34 * Revision 1.24 2008/02/20 19:36:06 james
37 * Revision 1.23 2008/02/20 19:25:09 james
38 * *** empty log message ***
40 * Revision 1.22 2008/02/15 03:32:07 james
41 * *** empty log message ***
43 * Revision 1.21 2008/02/14 02:46:44 james
44 * *** empty log message ***
46 * Revision 1.20 2008/02/14 01:55:57 james
47 * *** empty log message ***
49 * Revision 1.19 2008/02/13 16:57:29 james
50 * *** empty log message ***
52 * Revision 1.18 2008/02/13 09:12:21 james
53 * *** empty log message ***
55 * Revision 1.17 2008/02/13 01:08:18 james
56 * *** empty log message ***
58 * Revision 1.16 2008/02/07 13:22:51 james
59 * *** empty log message ***
61 * Revision 1.15 2008/02/07 13:19:48 james
62 * *** empty log message ***
64 * Revision 1.14 2008/02/07 12:21:16 james
65 * *** empty log message ***
67 * Revision 1.13 2008/02/07 12:16:04 james
68 * *** empty log message ***
70 * Revision 1.12 2008/02/07 11:32:41 james
71 * *** empty log message ***
73 * Revision 1.11 2008/02/07 11:11:14 staffcvs
74 * *** empty log message ***
76 * Revision 1.10 2008/02/07 01:02:52 james
77 * *** empty log message ***
79 * Revision 1.9 2008/02/07 00:43:27 james
80 * *** empty log message ***
82 * Revision 1.8 2008/02/07 00:39:13 james
83 * *** empty log message ***
85 * Revision 1.7 2008/02/06 20:26:57 james
86 * *** empty log message ***
88 * Revision 1.6 2008/02/06 17:53:28 james
89 * *** empty log message ***
91 * Revision 1.5 2008/02/06 15:53:22 james
92 * *** empty log message ***
94 * Revision 1.4 2008/02/04 20:23:55 james
95 * *** empty log message ***
97 * Revision 1.3 2008/02/04 05:45:55 james
100 * Revision 1.2 2008/02/04 02:05:06 james
101 * *** empty log message ***
103 * Revision 1.1 2008/02/03 23:31:25 james
104 * *** empty log message ***
111 ansi_move (ANSI * a, CRT_Pos p)
115 int dx = p.x - a->pos.x;
116 int dy = p.y - a->pos.y;
118 // a->pos.x = ANSI_INVAL;
120 if (a->pos.x != ANSI_INVAL)
130 a->terminal->xmit (a->terminal, "\033[C", 3);
134 a->terminal->xmit (a->terminal, "\033[D", 3);
138 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
139 a->terminal->xmit (a->terminal, buf, n);
146 a->terminal->xmit (a->terminal, "\033[A", 3);
150 a->terminal->xmit (a->terminal, "\033[B", 3);
154 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
155 a->terminal->xmit (a->terminal, buf, n);
159 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
160 a->terminal->xmit (a->terminal, buf, n);
167 a->terminal->xmit (a->terminal, "\033[E", 3);
171 a->terminal->xmit (a->terminal, "\033[F", 3);
175 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
176 a->terminal->xmit (a->terminal, buf, n);
180 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
181 a->terminal->xmit (a->terminal, buf, n);
186 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
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);
201 ansi_showhide_cursor (ANSI * a, int hide)
203 if (a->hide_cursor == hide)
208 a->terminal->xmit (a->terminal, "\033[?25l", 6);
212 a->terminal->xmit (a->terminal, "\033[?25h", 6);
215 a->hide_cursor = hide;
220 ansi_force_attr_normal (ANSI * a)
222 a->terminal->xmit (a->terminal, "\033[0m", 4);
223 a->attr = CRT_ATTR_NORMAL;
224 a->color = ANSI_INVAL;
228 ansi_set_color (ANSI * a, int color)
235 if ((a->color == ANSI_INVAL) || (color != a->color))
237 fg = CRT_COLOR_FG (color);
238 bg = CRT_COLOR_BG (color);
240 if (fg & CRT_COLOR_INTENSITY)
249 if (bg & CRT_COLOR_INTENSITY)
258 i = sprintf (buf, "\033[%d;%dm", fg, bg);
260 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
263 a->terminal->xmit (a->terminal, buf, i);
269 ansi_set_attr (ANSI * a, int attr)
273 dif = attr ^ a->attr;
281 if (attr == CRT_ATTR_NORMAL)
283 ansi_force_attr_normal (a);
288 if (dif & CRT_ATTR_UNDERLINE)
290 if (attr & CRT_ATTR_UNDERLINE)
292 a->terminal->xmit (a->terminal, "\033[4m", 4);
296 a->terminal->xmit (a->terminal, "\033[24m", 5);
299 if (dif & CRT_ATTR_REVERSE)
301 if (attr & CRT_ATTR_REVERSE)
303 a->terminal->xmit (a->terminal, "\033[7m", 4);
307 a->terminal->xmit (a->terminal, "\033[27m", 5);
310 if (dif & CRT_ATTR_BOLD)
312 if (attr & CRT_ATTR_BOLD)
314 a->terminal->xmit (a->terminal, "\033[1m", 4);
318 a->terminal->xmit (a->terminal, "\033[21m", 5);
319 a->terminal->xmit (a->terminal, "\033[22m", 5);
326 ascii_emit (TTY * t, uint32_t ch)
330 /*Some quick obvious subsititons for quotation marks*/
344 /*Short cut the easy stuff*/
351 for (i=0;i<VT102_CHARSET_SIZE;++i) {
352 if (vt102_charset_gl[i]==ch) {
353 uint8_t c[3] ={016,i,017};
364 ansi_render (ANSI * a, CRT_CA ca)
368 if ((ca.chr < VT102_CHARSET_SIZE) && (vt102_charset_c0[ca.chr]))
369 ca.chr=vt102_charset_c0[ca.chr];
371 if ((ca.chr >= 0x80) && (ca.chr < 0xa0))
374 ansi_set_attr (a, ca.attr);
375 ansi_set_color (a, ca.color);
378 utf8_emit (a->terminal, ca.chr);
380 ascii_emit(a->terminal,ca.chr);
384 /*Can't easily wrap round here as don't know size of destination screen*/
385 /*so invalidate the cached cursor position*/
387 if (a->pos.x >= a->size.x)
388 a->pos.x = ANSI_INVAL;
399 ansi_set_attr (a, CRT_ATTR_NORMAL);
400 ansi_set_color (a, CRT_COLOR_NORMAL);
402 a->terminal->xmit (a->terminal, "\033[2J", 4);
403 /*different emulators leave cursor in different places after cls differently*/
404 a->pos.x = ANSI_INVAL;
409 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
415 if (e.x != (CRT_COLS - 1))
419 if (s.y >= a->size.y)
422 ansi_showhide_cursor (a, 1);
423 i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
424 a->terminal->xmit (a->terminal, buf, i);
425 i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
426 a->terminal->xmit (a->terminal, buf, i);
427 a->terminal->xmit (a->terminal, "\033D", 2);
428 a->terminal->xmit (a->terminal, "\033[r", 3);
431 crt_erase (&a->crt, s, e, 1);
433 a->pos.x = ANSI_INVAL;
440 ansi_spot_scroll_up (ANSI * a, CRT * c)
444 l = c->sh.e.x - c->sh.s.x;
446 l *= sizeof (CRT_CA);
448 n = c->sh.e.y - c->sh.s.y;
449 p = CRT_ADDR_POS (&c->sh.s);
453 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
458 if (ansi_scroll_up (a, c->sh.s, c->sh.e))
461 n = c->sh.e.y - c->sh.s.y;
462 p = CRT_ADDR_POS (&c->sh.s);
466 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
470 c->sh.dir = 0; //FIXME: horrid hack
475 ansi_spot_scroll (ANSI * a, CRT * c)
481 /*we only care about up for the moment */
482 ansi_spot_scroll_up (a, c);
493 ansi_draw_line (ANSI * a, CRT_CA * cap, int y)
495 CRT_Pos p = { 0, y };
496 CRT_CA *acap = &a->crt.screen[CRT_ADDR_POS (&p)];
498 for (p.x = 0; p.x < a->crt.width; ++p.x)
500 if (p.x >= a->size.x)
503 if (crt_ca_cmp (*acap, *cap))
505 ansi_showhide_cursor (a, 1);
509 ansi_render (a, *acap);
518 ansi_resize_check (ANSI * a,int new_width)
521 if ((new_width && (new_width != a->crt.width)) || crt_pos_cmp (a->terminal->size, a->size)) {
523 terminal_getsize (a->terminal);
525 a->size = a->terminal->size;
527 a->pos.x = ANSI_INVAL;
528 a->hide_cursor = ANSI_INVAL;
533 a->crt.width=new_width;
535 // FIXME: -- echos back crap?
536 // a->terminal->xmit (a->terminal, "\033[c", 3);
538 // maybe - issue 132 column command if we're 132?
541 a->terminal->xmit (a->terminal, "\033=", 2);
542 a->terminal->xmit (a->terminal, "\033[?6l", 5);
543 a->terminal->xmit (a->terminal, "\033[r", 3);
545 a->terminal->xmit (a->terminal,"\033%G",3);
547 a->terminal->xmit (a->terminal,"\033(B",3);
548 a->terminal->xmit (a->terminal,"\033)0",3);
549 a->terminal->xmit (a->terminal,"\017",1);
555 #define HISTORY_GUESS_SCROLL 24 /*guess all 24 lines usually scroll */
556 /*if they haven't then ansi_draw will patch it up*/
559 ansi_history (ANSI * a, History * h)
563 /*Do we need to catch up on history?*/
565 if (a->history_ptr == h->wptr)
567 ansi_resize_check (a,0);
569 if ((a->size.x < a->crt.width) || (a->size.y < CRT_ROWS))
572 ansi_force_attr_normal (a);
573 ansi_set_color (a, CRT_COLOR_NORMAL);
575 i = sprintf (buf, "\033[%d;%dr", 1, HISTORY_GUESS_SCROLL);
576 a->terminal->xmit (a->terminal, buf, i);
579 while (a->history_ptr != h->wptr)
582 History_ent *e = &h->lines[a->history_ptr];
584 HISTORY_INC (h, a->history_ptr);
589 /*If so write the line ot the top of the screen */
590 ansi_draw_line (a, e->line, 0);
593 /*Roll HISTORY_GUESS_SCROLL lines up putting the top line into the xterm's history */
596 /*Make extra lines a predictable colour */
597 ansi_set_color (a, CRT_COLOR_NORMAL);
599 ansi_showhide_cursor (a, 1);
600 i = sprintf (buf, "\033[%d;%dH", HISTORY_GUESS_SCROLL, 1);
601 a->terminal->xmit (a->terminal, buf, i);
602 a->terminal->xmit (a->terminal, "\033D", 2);
603 a->pos.x = ANSI_INVAL;
605 /*now do the same in our image of the screen */
614 for (s.y++; s.y < HISTORY_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.width);
624 crt_erase (&a->crt, s, e, 1, CRT_COLOR_NORMAL);
629 a->terminal->xmit (a->terminal, "\033[r", 3);
630 a->pos.x = ANSI_INVAL;
638 ansi_draw (ANSI * a, CRT * c)
642 int hidden_cursor = 0;
644 ansi_resize_check (a,c->width);
647 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
649 if (p.y >= a->size.y)
652 ansi_draw_line (a, &c->screen[CRT_ADDR (p.y, 0)], p.y);
657 if ((c->width > a->size.x) || (CRT_ROWS > a->size.y))
659 char msg[1024]; // = "Window is too small";
665 sprintf (msg, "Window too small (%dx%d need %dx%d)", a->size.x,
666 a->size.y, c->width, CRT_ROWS);
668 ansi_showhide_cursor (a, 1);
669 ansi_set_attr (a, CRT_ATTR_REVERSE);
670 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
673 a->terminal->xmit (a->terminal, msg, i);
674 a->pos.x = ANSI_INVAL;
678 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
680 ansi_showhide_cursor (a, 1);
685 ansi_move (a, a->crt.pos);
687 a->crt.hide_cursor = c->hide_cursor;
688 ansi_showhide_cursor (a, a->crt.hide_cursor);
692 ansi_reset (ANSI * a, CRT * c)
695 ansi_draw (a, c ? c : &a->crt);
699 ansi_terminal_reset (ANSI * a)
701 CRT_Pos p = { 0, CRT_ROWS };
702 ansi_force_attr_normal (a);
708 ansi_flush_escape (ANSI * a, Context * c)
710 ANSI_Parser *p = &a->parser;
713 for (i = 0; i < p->escape_ptr; ++i)
715 keydis_key (c->k, c, p->escape_buf[i]);
723 ansi_parse_deckey (ANSI * a, Context * c)
725 ANSI_Parser *p = &a->parser;
726 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
728 ansi_flush_escape (a, c);
732 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
734 keydis_key (c->k, c, KEY_UP + (p->escape_buf[2] - 'A'));
736 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
738 keydis_key (c->k, c, KEY_154 + (p->escape_buf[2] - 'a'));
742 ansi_flush_escape (a, c);
750 ansi_parse_ansikey (ANSI * a, Context * c)
752 ANSI_Parser *p = &a->parser;
754 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
756 ansi_flush_escape (a, c);
759 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
761 keydis_key (c->k, c, KEY_180 + (p->escape_buf[2] - '0'));
765 ansi_flush_escape (a, c);
776 ansi_parse_escape (ANSI * a, Context * c)
778 ANSI_Parser *p = &a->parser;
779 switch (p->escape_ptr)
785 switch (p->escape_buf[1])
791 ansi_flush_escape (a, c);
795 switch (p->escape_buf[1])
798 ansi_parse_deckey (a, c);
801 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
802 ansi_parse_deckey (a, c);
805 ansi_flush_escape (a, c);
809 switch (p->escape_buf[1])
812 ansi_parse_ansikey (a, c);
815 ansi_flush_escape (a, c);
819 ansi_flush_escape (a, c);
825 ansi_check_escape (ANSI * a, Context * c)
827 ANSI_Parser *p = &a->parser;
828 struct timeval now, diff;
829 gettimeofday (&now, NULL);
830 timersub (&now, &p->last_escape, &diff);
833 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
834 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
842 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
843 ansi_flush_escape (a, c);
849 ansi_parse_char (ANSI * a, Context * c, int ch)
851 ANSI_Parser *p = &a->parser;
854 /*See if it's time to flush the escape*/
855 ansi_check_escape (a, c);
860 ansi_flush_escape (a, c);
864 gettimeofday (&p->last_escape, NULL);
869 p->escape_buf[p->escape_ptr++] = ch;
870 ansi_parse_escape (a, c);
874 keydis_key (c->k, c, ch);
880 ansi_parse (ANSI * a, Context * c, char *buf, int len)
883 ansi_parse_char (a, c, *(buf++));
887 ansi_dispatch (ANSI * a, Context * c)
892 ansi_check_escape (a, c);
898 red = a->terminal->recv (a->terminal, buf, sizeof (buf));
911 a->history_ptr = c->h->wptr;
912 HISTORY_INC (c->h, a->history_ptr);
919 ansi_parse (a, c, buf, red);
926 ansi_update (ANSI * a, Context * c)
928 ansi_history (a, c->h);
929 ansi_draw (a, &c->v->crt);
935 a->terminal_reset (a);
937 a->terminal->close (a->terminal);
944 ansi_new_from_terminal (TTY * t, int utf8)
948 ret = malloc (sizeof (ANSI));
949 memset (ret, 0, sizeof (ANSI));
954 ret->update = ansi_update;
955 ret->reset = ansi_reset;
956 ret->terminal_reset = ansi_terminal_reset;
957 ret->close = ansi_free;
958 ret->dispatch = ansi_dispatch;