4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.32 2008/02/26 23:56:12 james
14 * *** empty log message ***
16 * Revision 1.31 2008/02/26 23:23:17 james
17 * *** empty log message ***
19 * Revision 1.30 2008/02/24 00:42:53 james
20 * *** empty log message ***
22 * Revision 1.29 2008/02/23 11:48:37 james
23 * *** empty log message ***
25 * Revision 1.28 2008/02/22 17:07:00 james
26 * *** empty log message ***
28 * Revision 1.27 2008/02/20 22:54:22 staffcvs
29 * *** empty log message ***
31 * Revision 1.26 2008/02/20 20:16:07 james
32 * *** empty log message ***
34 * Revision 1.25 2008/02/20 19:44:37 james
37 * Revision 1.24 2008/02/20 19:36:06 james
40 * Revision 1.23 2008/02/20 19:25:09 james
41 * *** empty log message ***
43 * Revision 1.22 2008/02/15 03:32:07 james
44 * *** empty log message ***
46 * Revision 1.21 2008/02/14 02:46:44 james
47 * *** empty log message ***
49 * Revision 1.20 2008/02/14 01:55:57 james
50 * *** empty log message ***
52 * Revision 1.19 2008/02/13 16:57:29 james
53 * *** empty log message ***
55 * Revision 1.18 2008/02/13 09:12:21 james
56 * *** empty log message ***
58 * Revision 1.17 2008/02/13 01:08:18 james
59 * *** empty log message ***
61 * Revision 1.16 2008/02/07 13:22:51 james
62 * *** empty log message ***
64 * Revision 1.15 2008/02/07 13:19:48 james
65 * *** empty log message ***
67 * Revision 1.14 2008/02/07 12:21:16 james
68 * *** empty log message ***
70 * Revision 1.13 2008/02/07 12:16:04 james
71 * *** empty log message ***
73 * Revision 1.12 2008/02/07 11:32:41 james
74 * *** empty log message ***
76 * Revision 1.11 2008/02/07 11:11:14 staffcvs
77 * *** empty log message ***
79 * Revision 1.10 2008/02/07 01:02:52 james
80 * *** empty log message ***
82 * Revision 1.9 2008/02/07 00:43:27 james
83 * *** empty log message ***
85 * Revision 1.8 2008/02/07 00:39:13 james
86 * *** empty log message ***
88 * Revision 1.7 2008/02/06 20:26:57 james
89 * *** empty log message ***
91 * Revision 1.6 2008/02/06 17:53:28 james
92 * *** empty log message ***
94 * Revision 1.5 2008/02/06 15:53:22 james
95 * *** empty log message ***
97 * Revision 1.4 2008/02/04 20:23:55 james
98 * *** empty log message ***
100 * Revision 1.3 2008/02/04 05:45:55 james
103 * Revision 1.2 2008/02/04 02:05:06 james
104 * *** empty log message ***
106 * Revision 1.1 2008/02/03 23:31:25 james
107 * *** empty log message ***
114 ansi_move (ANSI * a, CRT_Pos p)
118 int dx = p.x - a->pos.x;
119 int dy = p.y - a->pos.y;
121 // a->pos.x = ANSI_INVAL;
123 if (a->pos.x != ANSI_INVAL)
133 a->terminal->xmit (a->terminal, "\033[C", 3);
137 a->terminal->xmit (a->terminal, "\033[D", 3);
141 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
142 a->terminal->xmit (a->terminal, buf, n);
149 a->terminal->xmit (a->terminal, "\033[A", 3);
153 a->terminal->xmit (a->terminal, "\033[B", 3);
157 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
158 a->terminal->xmit (a->terminal, buf, n);
162 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
163 a->terminal->xmit (a->terminal, buf, n);
170 a->terminal->xmit (a->terminal, "\033[E", 3);
174 a->terminal->xmit (a->terminal, "\033[F", 3);
178 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
179 a->terminal->xmit (a->terminal, buf, n);
183 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
184 a->terminal->xmit (a->terminal, buf, n);
189 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
190 a->terminal->xmit (a->terminal, buf, n);
195 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
196 a->terminal->xmit (a->terminal, buf, n);
204 ansi_showhide_cursor (ANSI * a, int hide)
206 if (a->hide_cursor == hide)
211 a->terminal->xmit (a->terminal, "\033[?25l", 6);
215 a->terminal->xmit (a->terminal, "\033[?25h", 6);
218 a->hide_cursor = hide;
223 ansi_force_attr_normal (ANSI * a)
225 a->terminal->xmit (a->terminal, "\033[0m", 4);
226 a->attr = CRT_ATTR_NORMAL;
227 a->color = ANSI_INVAL;
231 ansi_set_color (ANSI * a, int color)
238 if ((a->color == ANSI_INVAL) || (color != a->color))
240 fg = CRT_COLOR_FG (color);
241 bg = CRT_COLOR_BG (color);
243 if (fg & CRT_COLOR_INTENSITY)
252 if (bg & CRT_COLOR_INTENSITY)
261 i = sprintf (buf, "\033[%d;%dm", fg, bg);
263 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
266 a->terminal->xmit (a->terminal, buf, i);
272 ansi_set_attr (ANSI * a, int attr)
276 dif = attr ^ a->attr;
284 if (attr == CRT_ATTR_NORMAL)
286 ansi_force_attr_normal (a);
291 if (dif & CRT_ATTR_UNDERLINE)
293 if (attr & CRT_ATTR_UNDERLINE)
295 a->terminal->xmit (a->terminal, "\033[4m", 4);
299 a->terminal->xmit (a->terminal, "\033[24m", 5);
302 if (dif & CRT_ATTR_REVERSE)
304 if (attr & CRT_ATTR_REVERSE)
306 a->terminal->xmit (a->terminal, "\033[7m", 4);
310 a->terminal->xmit (a->terminal, "\033[27m", 5);
313 if (dif & CRT_ATTR_BOLD)
315 if (attr & CRT_ATTR_BOLD)
317 a->terminal->xmit (a->terminal, "\033[1m", 4);
321 a->terminal->xmit (a->terminal, "\033[21m", 5);
322 a->terminal->xmit (a->terminal, "\033[22m", 5);
329 ascii_emit (TTY * t, uint32_t ch)
333 /*Some quick obvious subsititons for quotation marks*/
348 /*Short cut the easy stuff*/
356 for (i = 0; i < VT102_CHARSET_SIZE; ++i)
358 if (vt102_charset_gl[i] == ch)
360 uint8_t c[3] = { 016, i, 017 };
371 ansi_render (ANSI * a, CRT_CA ca)
375 if ((ca.chr < VT102_CHARSET_SIZE) && (vt102_charset_c0[ca.chr]))
376 ca.chr = vt102_charset_c0[ca.chr];
378 if ((ca.chr >= 0x80) && (ca.chr < 0xa0))
381 ansi_set_attr (a, ca.attr);
382 ansi_set_color (a, ca.color);
385 utf8_emit (a->terminal, ca.chr);
387 ascii_emit (a->terminal, ca.chr);
391 /*Can't easily wrap round here as don't know size of destination screen*/
392 /*so invalidate the cached cursor position*/
394 if (a->pos.x >= a->size.x)
395 a->pos.x = ANSI_INVAL;
406 ansi_set_attr (a, CRT_ATTR_NORMAL);
407 ansi_set_color (a, CRT_COLOR_NORMAL);
409 a->terminal->xmit (a->terminal, "\033[2J", 4);
410 /*different emulators leave cursor in different places after cls differently*/
411 a->pos.x = ANSI_INVAL;
416 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
422 if (e.x != (CRT_COLS - 1))
426 if (s.y >= a->size.y)
429 ansi_showhide_cursor (a, 1);
430 i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
431 a->terminal->xmit (a->terminal, buf, i);
432 i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
433 a->terminal->xmit (a->terminal, buf, i);
434 a->terminal->xmit (a->terminal, "\033D", 2);
435 a->terminal->xmit (a->terminal, "\033[r", 3);
438 crt_erase (&a->crt, s, e, 1);
440 a->pos.x = ANSI_INVAL;
447 ansi_spot_scroll_up (ANSI * a, CRT * c)
451 l = c->sh.e.x - c->sh.s.x;
453 l *= sizeof (CRT_CA);
455 n = c->sh.e.y - c->sh.s.y;
456 p = CRT_ADDR_POS (&c->sh.s);
460 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
465 if (ansi_scroll_up (a, c->sh.s, c->sh.e))
468 n = c->sh.e.y - c->sh.s.y;
469 p = CRT_ADDR_POS (&c->sh.s);
473 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
477 c->sh.dir = 0; //FIXME: horrid hack
482 ansi_spot_scroll (ANSI * a, CRT * c)
488 /*we only care about up for the moment */
489 ansi_spot_scroll_up (a, c);
500 ansi_draw_line (ANSI * a, CRT_CA * cap, int y)
502 CRT_Pos p = { 0, y };
503 CRT_CA *acap = &a->crt.screen[CRT_ADDR_POS (&p)];
505 for (p.x = 0; p.x < a->crt.width; ++p.x)
507 if (p.x >= a->size.x)
510 if (crt_ca_cmp (*acap, *cap))
512 ansi_showhide_cursor (a, 1);
516 ansi_render (a, *acap);
525 ansi_resize_check (ANSI * a, int new_width)
528 if ((new_width && (new_width != a->crt.width))
529 || crt_pos_cmp (a->terminal->size, a->size))
532 terminal_getsize (a->terminal);
534 a->size = a->terminal->size;
536 a->pos.x = ANSI_INVAL;
537 a->hide_cursor = ANSI_INVAL;
542 a->crt.width = new_width;
544 // FIXME: -- echos back crap?
545 // a->terminal->xmit (a->terminal, "\033[c", 3);
547 // maybe - issue 132 column command if we're 132?
550 a->terminal->xmit (a->terminal, "\033=", 2);
551 a->terminal->xmit (a->terminal, "\033[?6l", 5);
552 a->terminal->xmit (a->terminal, "\033[r", 3);
555 a->terminal->xmit (a->terminal, "\033%G", 3);
559 a->terminal->xmit (a->terminal, "\033(B", 3);
560 a->terminal->xmit (a->terminal, "\033)0", 3);
561 a->terminal->xmit (a->terminal, "\017", 1);
567 #define HISTORY_GUESS_SCROLL 24 /*guess all 24 lines usually scroll */
568 /*if they haven't then ansi_draw will patch it up*/
571 ansi_history (ANSI * a, History * h)
575 /*Do we need to catch up on history?*/
577 if (a->history_ptr == h->wptr)
579 ansi_resize_check (a, 0);
581 if ((a->size.x < a->crt.width) || (a->size.y < CRT_ROWS))
584 ansi_force_attr_normal (a);
585 ansi_set_color (a, CRT_COLOR_NORMAL);
587 i = sprintf (buf, "\033[%d;%dr", 1, HISTORY_GUESS_SCROLL);
588 a->terminal->xmit (a->terminal, buf, i);
591 while (a->history_ptr != h->wptr)
594 History_ent *e = &h->lines[a->history_ptr];
596 HISTORY_INC (h, a->history_ptr);
601 /*If so write the line ot the top of the screen */
602 ansi_draw_line (a, e->line, 0);
605 /*Roll HISTORY_GUESS_SCROLL lines up putting the top line into the xterm's history */
608 /*Make extra lines a predictable colour */
609 ansi_set_color (a, CRT_COLOR_NORMAL);
611 ansi_showhide_cursor (a, 1);
612 i = sprintf (buf, "\033[%d;%dH", HISTORY_GUESS_SCROLL, 1);
613 a->terminal->xmit (a->terminal, buf, i);
614 a->terminal->xmit (a->terminal, "\033D", 2);
615 a->pos.x = ANSI_INVAL;
617 /*now do the same in our image of the screen */
626 for (s.y++; s.y < HISTORY_GUESS_SCROLL; s.y++, e.y++)
628 memcpy (&a->crt.screen[CRT_ADDR_POS (&e)],
629 &a->crt.screen[CRT_ADDR_POS (&s)],
630 sizeof (CRT_CA) * a->crt.width);
636 crt_erase (&a->crt, s, e, 1, CRT_COLOR_NORMAL);
641 a->terminal->xmit (a->terminal, "\033[r", 3);
642 a->pos.x = ANSI_INVAL;
650 ansi_draw (ANSI * a, CRT * c)
654 int hidden_cursor = 0;
656 ansi_resize_check (a, c->width);
659 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
661 if (p.y >= a->size.y)
664 ansi_draw_line (a, &c->screen[CRT_ADDR (p.y, 0)], p.y);
669 if ((c->width > a->size.x) || (CRT_ROWS > a->size.y))
671 char msg[1024]; // = "Window is too small";
677 sprintf (msg, "Window too small (%dx%d need %dx%d)", a->size.x,
678 a->size.y, c->width, CRT_ROWS);
680 ansi_showhide_cursor (a, 1);
681 ansi_set_attr (a, CRT_ATTR_REVERSE);
682 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
685 a->terminal->xmit (a->terminal, msg, i);
686 a->pos.x = ANSI_INVAL;
690 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
692 ansi_showhide_cursor (a, 1);
697 ansi_move (a, a->crt.pos);
699 a->crt.hide_cursor = c->hide_cursor;
700 ansi_showhide_cursor (a, a->crt.hide_cursor);
704 ansi_reset (ANSI * a, CRT * c)
707 ansi_draw (a, c ? c : &a->crt);
711 ansi_terminal_reset (ANSI * a)
713 CRT_Pos p = { 0, CRT_ROWS };
714 ansi_force_attr_normal (a);
720 ansi_flush_escape (ANSI * a, Context * c)
722 ANSI_Parser *p = &a->parser;
725 for (i = 0; i < p->escape_ptr; ++i)
727 keydis_key (c->k, c, p->escape_buf[i]);
735 ansi_parse_deckey (ANSI * a, Context * c)
737 ANSI_Parser *p = &a->parser;
738 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
740 ansi_flush_escape (a, c);
744 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
746 keydis_key (c->k, c, KEY_UP + (p->escape_buf[2] - 'A'));
748 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
750 keydis_key (c->k, c, KEY_154 + (p->escape_buf[2] - 'a'));
754 ansi_flush_escape (a, c);
762 ansi_parse_ansikey (ANSI * a, Context * c)
764 ANSI_Parser *p = &a->parser;
766 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
768 ansi_flush_escape (a, c);
771 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
773 keydis_key (c->k, c, KEY_180 + (p->escape_buf[2] - '0'));
777 ansi_flush_escape (a, c);
788 ansi_parse_escape (ANSI * a, Context * c)
790 ANSI_Parser *p = &a->parser;
791 switch (p->escape_ptr)
797 switch (p->escape_buf[1])
803 ansi_flush_escape (a, c);
807 switch (p->escape_buf[1])
810 ansi_parse_deckey (a, c);
813 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
814 ansi_parse_deckey (a, c);
817 ansi_flush_escape (a, c);
821 switch (p->escape_buf[1])
824 ansi_parse_ansikey (a, c);
827 ansi_flush_escape (a, c);
831 ansi_flush_escape (a, c);
837 ansi_check_escape (ANSI * a, Context * c)
839 ANSI_Parser *p = &a->parser;
840 struct timeval now, diff;
841 gettimeofday (&now, NULL);
842 timersub (&now, &p->last_escape, &diff);
845 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
846 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
854 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
855 ansi_flush_escape (a, c);
861 ansi_parse_char (ANSI * a, Context * c, int ch)
863 ANSI_Parser *p = &a->parser;
866 /*See if it's time to flush the escape*/
867 ansi_check_escape (a, c);
872 ansi_flush_escape (a, c);
876 gettimeofday (&p->last_escape, NULL);
881 p->escape_buf[p->escape_ptr++] = ch;
882 ansi_parse_escape (a, c);
886 keydis_key (c->k, c, ch);
892 ansi_parse (ANSI * a, Context * c, char *buf, int len)
895 ansi_parse_char (a, c, *(buf++));
899 ansi_dispatch (ANSI * a, Context * c)
904 ansi_check_escape (a, c);
910 red = a->terminal->recv (a->terminal, buf, sizeof (buf));
923 a->history_ptr = c->h->wptr;
924 HISTORY_INC (c->h, a->history_ptr);
931 ansi_parse (a, c, buf, red);
938 ansi_update (ANSI * a, Context * c)
940 ansi_history (a, c->h);
941 ansi_draw (a, &c->v->crt);
947 a->terminal_reset (a);
949 a->terminal->close (a->terminal);
956 ansi_new_from_terminal (TTY * t, int utf8)
960 ret = malloc (sizeof (ANSI));
961 memset (ret, 0, sizeof (ANSI));
966 ret->update = ansi_update;
967 ret->reset = ansi_reset;
968 ret->terminal_reset = ansi_terminal_reset;
969 ret->close = ansi_free;
970 ret->dispatch = ansi_dispatch;