4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.30 2008/02/24 00:42:53 james
14 * *** empty log message ***
16 * Revision 1.29 2008/02/23 11:48:37 james
17 * *** empty log message ***
19 * Revision 1.28 2008/02/22 17:07:00 james
20 * *** empty log message ***
22 * Revision 1.27 2008/02/20 22:54:22 staffcvs
23 * *** empty log message ***
25 * Revision 1.26 2008/02/20 20:16:07 james
26 * *** empty log message ***
28 * Revision 1.25 2008/02/20 19:44:37 james
31 * Revision 1.24 2008/02/20 19:36:06 james
34 * Revision 1.23 2008/02/20 19:25:09 james
35 * *** empty log message ***
37 * Revision 1.22 2008/02/15 03:32:07 james
38 * *** empty log message ***
40 * Revision 1.21 2008/02/14 02:46:44 james
41 * *** empty log message ***
43 * Revision 1.20 2008/02/14 01:55:57 james
44 * *** empty log message ***
46 * Revision 1.19 2008/02/13 16:57:29 james
47 * *** empty log message ***
49 * Revision 1.18 2008/02/13 09:12:21 james
50 * *** empty log message ***
52 * Revision 1.17 2008/02/13 01:08:18 james
53 * *** empty log message ***
55 * Revision 1.16 2008/02/07 13:22:51 james
56 * *** empty log message ***
58 * Revision 1.15 2008/02/07 13:19:48 james
59 * *** empty log message ***
61 * Revision 1.14 2008/02/07 12:21:16 james
62 * *** empty log message ***
64 * Revision 1.13 2008/02/07 12:16:04 james
65 * *** empty log message ***
67 * Revision 1.12 2008/02/07 11:32:41 james
68 * *** empty log message ***
70 * Revision 1.11 2008/02/07 11:11:14 staffcvs
71 * *** empty log message ***
73 * Revision 1.10 2008/02/07 01:02:52 james
74 * *** empty log message ***
76 * Revision 1.9 2008/02/07 00:43:27 james
77 * *** empty log message ***
79 * Revision 1.8 2008/02/07 00:39:13 james
80 * *** empty log message ***
82 * Revision 1.7 2008/02/06 20:26:57 james
83 * *** empty log message ***
85 * Revision 1.6 2008/02/06 17:53:28 james
86 * *** empty log message ***
88 * Revision 1.5 2008/02/06 15:53:22 james
89 * *** empty log message ***
91 * Revision 1.4 2008/02/04 20:23:55 james
92 * *** empty log message ***
94 * Revision 1.3 2008/02/04 05:45:55 james
97 * Revision 1.2 2008/02/04 02:05:06 james
98 * *** empty log message ***
100 * Revision 1.1 2008/02/03 23:31:25 james
101 * *** empty log message ***
108 ansi_move (ANSI * a, CRT_Pos p)
112 int dx = p.x - a->pos.x;
113 int dy = p.y - a->pos.y;
115 // a->pos.x = ANSI_INVAL;
117 if (a->pos.x != ANSI_INVAL)
127 a->terminal->xmit (a->terminal, "\033[C", 3);
131 a->terminal->xmit (a->terminal, "\033[D", 3);
135 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
136 a->terminal->xmit (a->terminal, buf, n);
143 a->terminal->xmit (a->terminal, "\033[A", 3);
147 a->terminal->xmit (a->terminal, "\033[B", 3);
151 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
152 a->terminal->xmit (a->terminal, buf, n);
156 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
157 a->terminal->xmit (a->terminal, buf, n);
164 a->terminal->xmit (a->terminal, "\033[E", 3);
168 a->terminal->xmit (a->terminal, "\033[F", 3);
172 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
173 a->terminal->xmit (a->terminal, buf, n);
177 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
178 a->terminal->xmit (a->terminal, buf, n);
183 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
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);
198 ansi_showhide_cursor (ANSI * a, int hide)
200 if (a->hide_cursor == hide)
205 a->terminal->xmit (a->terminal, "\033[?25l", 6);
209 a->terminal->xmit (a->terminal, "\033[?25h", 6);
212 a->hide_cursor = hide;
217 ansi_force_attr_normal (ANSI * a)
219 a->terminal->xmit (a->terminal, "\033[0m", 4);
220 a->attr = CRT_ATTR_NORMAL;
221 a->color = ANSI_INVAL;
225 ansi_set_color (ANSI * a, int color)
232 if ((a->color == ANSI_INVAL) || (color != a->color))
234 fg = CRT_COLOR_FG (color);
235 bg = CRT_COLOR_BG (color);
237 if (fg & CRT_COLOR_INTENSITY)
246 if (bg & CRT_COLOR_INTENSITY)
255 i = sprintf (buf, "\033[%d;%dm", fg, bg);
257 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
260 a->terminal->xmit (a->terminal, buf, i);
266 ansi_set_attr (ANSI * a, int attr)
270 dif = attr ^ a->attr;
278 if (attr == CRT_ATTR_NORMAL)
280 ansi_force_attr_normal (a);
285 if (dif & CRT_ATTR_UNDERLINE)
287 if (attr & CRT_ATTR_UNDERLINE)
289 a->terminal->xmit (a->terminal, "\033[4m", 4);
293 a->terminal->xmit (a->terminal, "\033[24m", 5);
296 if (dif & CRT_ATTR_REVERSE)
298 if (attr & CRT_ATTR_REVERSE)
300 a->terminal->xmit (a->terminal, "\033[7m", 4);
304 a->terminal->xmit (a->terminal, "\033[27m", 5);
307 if (dif & CRT_ATTR_BOLD)
309 if (attr & CRT_ATTR_BOLD)
311 a->terminal->xmit (a->terminal, "\033[1m", 4);
315 a->terminal->xmit (a->terminal, "\033[21m", 5);
316 a->terminal->xmit (a->terminal, "\033[22m", 5);
323 ansi_emit_noutf8 (TTY * t, uint32_t ch)
325 uint8_t c = (ch > 0x7f) ? '?' : ch;
330 ansi_render (ANSI * a, CRT_CA ca)
336 if ((ca.chr > 0x7e) && (ca.chr < 0xa0))
339 ansi_set_attr (a, ca.attr);
340 ansi_set_color (a, ca.color);
343 utf8_emit (a->terminal, ca.chr);
345 ansi_emit_noutf8 (a->terminal, ca.chr);
349 /*Can't easily wrap round here as don't know size of destination screen*/
350 /*so invalidate the cached cursor position*/
352 if (a->pos.x >= a->size.x)
353 a->pos.x = ANSI_INVAL;
364 ansi_set_attr (a, CRT_ATTR_NORMAL);
365 ansi_set_color (a, CRT_COLOR_NORMAL);
367 a->terminal->xmit (a->terminal, "\033[2J", 4);
368 /*different emulators leave cursor in different places after cls differently*/
369 a->pos.x = ANSI_INVAL;
374 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
380 if (e.x != (CRT_COLS - 1))
384 if (s.y >= a->size.y)
387 ansi_showhide_cursor (a, 1);
388 i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
389 a->terminal->xmit (a->terminal, buf, i);
390 i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
391 a->terminal->xmit (a->terminal, buf, i);
392 a->terminal->xmit (a->terminal, "\033D", 2);
393 a->terminal->xmit (a->terminal, "\033[r", 3);
396 crt_erase (&a->crt, s, e, 1);
398 a->pos.x = ANSI_INVAL;
405 ansi_spot_scroll_up (ANSI * a, CRT * c)
409 l = c->sh.e.x - c->sh.s.x;
411 l *= sizeof (CRT_CA);
413 n = c->sh.e.y - c->sh.s.y;
414 p = CRT_ADDR_POS (&c->sh.s);
418 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
423 if (ansi_scroll_up (a, c->sh.s, c->sh.e))
426 n = c->sh.e.y - c->sh.s.y;
427 p = CRT_ADDR_POS (&c->sh.s);
431 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
435 c->sh.dir = 0; //FIXME: horrid hack
440 ansi_spot_scroll (ANSI * a, CRT * c)
446 /*we only care about up for the moment */
447 ansi_spot_scroll_up (a, c);
458 ansi_draw_line (ANSI * a, CRT_CA * cap, int y)
460 CRT_Pos p = { 0, y };
461 CRT_CA *acap = &a->crt.screen[CRT_ADDR_POS (&p)];
463 for (p.x = 0; p.x < CRT_COLS; ++p.x)
465 if (p.x >= a->size.x)
468 if (crt_ca_cmp (*acap, *cap))
470 ansi_showhide_cursor (a, 1);
474 ansi_render (a, *acap);
483 ansi_resize_check (ANSI * a)
486 if (!crt_pos_cmp (a->terminal->size, a->size))
489 terminal_getsize (a->terminal);
491 a->size = a->terminal->size;
493 a->pos.x = ANSI_INVAL;
494 a->hide_cursor = ANSI_INVAL;
498 // FIXME: -- echos back crap?
499 // a->terminal->xmit (a->terminal, "\033[c", 3);
502 a->terminal->xmit (a->terminal, "\033=", 2);
503 a->terminal->xmit (a->terminal, "\033[?6l", 5);
504 a->terminal->xmit (a->terminal, "\033[r", 3);
508 #define HISTORY_GUESS_SCROLL 24 /*guess all 24 lines usually scroll */
509 /*if they haven't then ansi_draw will patch it up*/
512 ansi_history (ANSI * a, History * h)
516 /*Do we need to catch up on history?*/
518 if (a->history_ptr == h->wptr)
520 ansi_resize_check (a);
522 if ((a->size.x < CRT_COLS) || (a->size.y < CRT_ROWS))
525 ansi_force_attr_normal (a);
526 ansi_set_color (a, CRT_COLOR_NORMAL);
528 i = sprintf (buf, "\033[%d;%dr", 1, HISTORY_GUESS_SCROLL);
529 a->terminal->xmit (a->terminal, buf, i);
532 while (a->history_ptr != h->wptr)
535 History_ent *e = &h->lines[a->history_ptr];
537 HISTORY_INC (h, a->history_ptr);
542 /*If so write the line ot the top of the screen */
543 ansi_draw_line (a, e->line, 0);
546 /*Roll HISTORY_GUESS_SCROLL lines up putting the top line into the xterm's history */
549 /*Make extra lines a predictable colour */
550 ansi_set_color (a, CRT_COLOR_NORMAL);
552 ansi_showhide_cursor (a, 1);
553 i = sprintf (buf, "\033[%d;%dH", HISTORY_GUESS_SCROLL, 1);
554 a->terminal->xmit (a->terminal, buf, i);
555 a->terminal->xmit (a->terminal, "\033D", 2);
556 a->pos.x = ANSI_INVAL;
558 /*now do the same in our image of the screen */
567 for (s.y++; s.y < HISTORY_GUESS_SCROLL; s.y++, e.y++)
569 memcpy (&a->crt.screen[CRT_ADDR_POS (&e)],
570 &a->crt.screen[CRT_ADDR_POS (&s)],
571 sizeof (CRT_CA) * CRT_COLS);
577 crt_erase (&a->crt, s, e, 1, CRT_COLOR_NORMAL);
582 a->terminal->xmit (a->terminal, "\033[r", 3);
583 a->pos.x = ANSI_INVAL;
591 ansi_draw (ANSI * a, CRT * c)
595 int hidden_cursor = 0;
597 ansi_resize_check (a);
601 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
603 if (p.y >= a->size.y)
606 ansi_draw_line (a, &c->screen[CRT_ADDR (p.y, 0)], p.y);
611 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
613 char msg[1024]; // = "Window is too small";
619 sprintf (msg, "Window too small (%dx%d need %dx%d)", a->size.x,
620 a->size.y, CRT_COLS, CRT_ROWS);
622 ansi_showhide_cursor (a, 1);
623 ansi_set_attr (a, CRT_ATTR_REVERSE);
624 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
627 a->terminal->xmit (a->terminal, msg, i);
628 a->pos.x = ANSI_INVAL;
632 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
634 ansi_showhide_cursor (a, 1);
639 ansi_move (a, a->crt.pos);
641 a->crt.hide_cursor = c->hide_cursor;
642 ansi_showhide_cursor (a, a->crt.hide_cursor);
646 ansi_reset (ANSI * a, CRT * c)
649 ansi_draw (a, c ? c : &a->crt);
653 ansi_terminal_reset (ANSI * a)
655 CRT_Pos p = { 0, CRT_ROWS };
656 ansi_force_attr_normal (a);
662 ansi_flush_escape (ANSI * a, Context * c)
664 ANSI_Parser *p = &a->parser;
667 for (i = 0; i < p->escape_ptr; ++i)
669 keydis_key (c->k, c, p->escape_buf[i]);
677 ansi_parse_deckey (ANSI * a, Context * c)
679 ANSI_Parser *p = &a->parser;
680 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
682 ansi_flush_escape (a, c);
686 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
688 keydis_key (c->k, c, KEY_UP + (p->escape_buf[2] - 'A'));
690 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
692 keydis_key (c->k, c, KEY_154 + (p->escape_buf[2] - 'a'));
696 ansi_flush_escape (a, c);
704 ansi_parse_ansikey (ANSI * a, Context * c)
706 ANSI_Parser *p = &a->parser;
708 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
710 ansi_flush_escape (a, c);
713 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
715 keydis_key (c->k, c, KEY_180 + (p->escape_buf[2] - '0'));
719 ansi_flush_escape (a, c);
730 ansi_parse_escape (ANSI * a, Context * c)
732 ANSI_Parser *p = &a->parser;
733 switch (p->escape_ptr)
739 switch (p->escape_buf[1])
745 ansi_flush_escape (a, c);
749 switch (p->escape_buf[1])
752 ansi_parse_deckey (a, c);
755 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
756 ansi_parse_deckey (a, c);
759 ansi_flush_escape (a, c);
763 switch (p->escape_buf[1])
766 ansi_parse_ansikey (a, c);
769 ansi_flush_escape (a, c);
773 ansi_flush_escape (a, c);
779 ansi_check_escape (ANSI * a, Context * c)
781 ANSI_Parser *p = &a->parser;
782 struct timeval now, diff;
783 gettimeofday (&now, NULL);
784 timersub (&now, &p->last_escape, &diff);
787 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
788 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
796 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
797 ansi_flush_escape (a, c);
803 ansi_parse_char (ANSI * a, Context * c, int ch)
805 ANSI_Parser *p = &a->parser;
808 /*See if it's time to flush the escape*/
809 ansi_check_escape (a, c);
814 ansi_flush_escape (a, c);
818 gettimeofday (&p->last_escape, NULL);
823 p->escape_buf[p->escape_ptr++] = ch;
824 ansi_parse_escape (a, c);
828 keydis_key (c->k, c, ch);
834 ansi_parse (ANSI * a, Context * c, char *buf, int len)
837 ansi_parse_char (a, c, *(buf++));
841 ansi_dispatch (ANSI * a, Context * c)
846 ansi_check_escape (a, c);
852 red = a->terminal->recv (a->terminal, buf, sizeof (buf));
865 a->history_ptr = c->h->wptr;
866 HISTORY_INC (c->h, a->history_ptr);
873 ansi_parse (a, c, buf, red);
880 ansi_update (ANSI * a, Context * c)
882 ansi_history (a, c->h);
883 ansi_draw (a, &c->v->crt);
889 a->terminal_reset (a);
891 a->terminal->close (a->terminal);
898 ansi_new_from_terminal (TTY * t, int utf8)
902 ret = malloc (sizeof (ANSI));
903 memset (ret, 0, sizeof (ANSI));
908 ret->update = ansi_update;
909 ret->reset = ansi_reset;
910 ret->terminal_reset = ansi_terminal_reset;
911 ret->close = ansi_free;
912 ret->dispatch = ansi_dispatch;