4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.19 2008/02/13 16:57:29 james
14 * *** empty log message ***
16 * Revision 1.18 2008/02/13 09:12:21 james
17 * *** empty log message ***
19 * Revision 1.17 2008/02/13 01:08:18 james
20 * *** empty log message ***
22 * Revision 1.16 2008/02/07 13:22:51 james
23 * *** empty log message ***
25 * Revision 1.15 2008/02/07 13:19:48 james
26 * *** empty log message ***
28 * Revision 1.14 2008/02/07 12:21:16 james
29 * *** empty log message ***
31 * Revision 1.13 2008/02/07 12:16:04 james
32 * *** empty log message ***
34 * Revision 1.12 2008/02/07 11:32:41 james
35 * *** empty log message ***
37 * Revision 1.11 2008/02/07 11:11:14 staffcvs
38 * *** empty log message ***
40 * Revision 1.10 2008/02/07 01:02:52 james
41 * *** empty log message ***
43 * Revision 1.9 2008/02/07 00:43:27 james
44 * *** empty log message ***
46 * Revision 1.8 2008/02/07 00:39:13 james
47 * *** empty log message ***
49 * Revision 1.7 2008/02/06 20:26:57 james
50 * *** empty log message ***
52 * Revision 1.6 2008/02/06 17:53:28 james
53 * *** empty log message ***
55 * Revision 1.5 2008/02/06 15:53:22 james
56 * *** empty log message ***
58 * Revision 1.4 2008/02/04 20:23:55 james
59 * *** empty log message ***
61 * Revision 1.3 2008/02/04 05:45:55 james
64 * Revision 1.2 2008/02/04 02:05:06 james
65 * *** empty log message ***
67 * Revision 1.1 2008/02/03 23:31:25 james
68 * *** empty log message ***
75 ansi_move (ANSI * a, CRT_Pos p)
79 int dx = p.x - a->pos.x;
80 int dy = p.y - a->pos.y;
82 // a->pos.x = ANSI_INVAL;
84 if (a->pos.x != ANSI_INVAL)
94 a->terminal->xmit (a->terminal, "\033[C", 3);
98 a->terminal->xmit (a->terminal, "\033[D", 3);
102 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
103 a->terminal->xmit (a->terminal, buf, n);
110 a->terminal->xmit (a->terminal, "\033[A", 3);
114 a->terminal->xmit (a->terminal, "\033[B", 3);
118 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
119 a->terminal->xmit (a->terminal, buf, n);
123 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
124 a->terminal->xmit (a->terminal, buf, n);
131 a->terminal->xmit (a->terminal, "\033[E", 3);
135 a->terminal->xmit (a->terminal, "\033[F", 3);
139 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
140 a->terminal->xmit (a->terminal, buf, n);
144 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
145 a->terminal->xmit (a->terminal, buf, n);
150 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
151 a->terminal->xmit (a->terminal, buf, n);
156 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
157 a->terminal->xmit (a->terminal, buf, n);
165 ansi_showhide_cursor (ANSI * a, int hide)
167 if (a->hide_cursor == hide)
172 a->terminal->xmit (a->terminal, "\033[?25l", 6);
176 a->terminal->xmit (a->terminal, "\033[?25h", 6);
179 a->hide_cursor = hide;
184 ansi_force_attr_normal (ANSI * a)
186 a->terminal->xmit (a->terminal, "\033[0m", 4);
187 a->attr = CRT_ATTR_NORMAL;
188 a->color = ANSI_INVAL;
192 ansi_set_color (ANSI * a, int color)
199 if ((a->color == ANSI_INVAL) || (color != a->color))
201 fg = CRT_COLOR_FG (color);
202 bg = CRT_COLOR_BG (color);
204 if (fg & CRT_COLOR_INTENSITY)
213 if (bg & CRT_COLOR_INTENSITY)
222 i = sprintf (buf, "\033[%d;%dm", fg, bg);
224 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
227 a->terminal->xmit (a->terminal, buf, i);
233 ansi_set_attr (ANSI * a, int attr)
237 dif = attr ^ a->attr;
245 if (attr == CRT_ATTR_NORMAL)
247 ansi_force_attr_normal (a);
252 if (dif & CRT_ATTR_UNDERLINE)
254 if (attr & CRT_ATTR_UNDERLINE)
256 a->terminal->xmit (a->terminal, "\033[4m", 4);
260 a->terminal->xmit (a->terminal, "\033[24m", 5);
263 if (dif & CRT_ATTR_REVERSE)
265 if (attr & CRT_ATTR_REVERSE)
267 a->terminal->xmit (a->terminal, "\033[7m", 4);
271 a->terminal->xmit (a->terminal, "\033[27m", 5);
274 if (dif & CRT_ATTR_BOLD)
276 if (attr & CRT_ATTR_BOLD)
278 a->terminal->xmit (a->terminal, "\033[1m", 4);
282 a->terminal->xmit (a->terminal, "\033[21m", 5);
283 a->terminal->xmit (a->terminal, "\033[22m", 5);
291 ansi_render (ANSI * a, CRT_CA ca)
300 ansi_set_attr (a, ca.attr);
301 ansi_set_color (a, ca.color);
303 a->terminal->xmit (a->terminal, &ca.chr, 1);
307 /*Can't easily wrap round here as don't know size of destination screen*/
308 /*so invalidate the cached cursor position*/
310 if (a->pos.x >= a->size.x)
311 a->pos.x = ANSI_INVAL;
322 ansi_set_attr (a, CRT_ATTR_NORMAL);
323 ansi_set_color (a, CRT_COLOR_NORMAL);
325 a->terminal->xmit (a->terminal, "\033[2J", 4);
326 /*different emulators leave cursor in different places after cls differently*/
327 a->pos.x = ANSI_INVAL;
332 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
338 if (e.x != (CRT_COLS - 1))
342 if (s.y >= a->size.y)
345 ansi_showhide_cursor (a, 1);
346 i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
347 a->terminal->xmit (a->terminal, buf, i);
348 i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
349 a->terminal->xmit (a->terminal, buf, i);
350 a->terminal->xmit (a->terminal, "\033D", 2);
351 a->terminal->xmit (a->terminal, "\033[r", 3);
354 crt_erase (&a->crt, s, e, 1);
356 a->pos.x = ANSI_INVAL;
363 ansi_spot_scroll_up (ANSI * a, CRT * c)
367 l = c->sh.e.x - c->sh.s.x;
369 l *= sizeof (CRT_CA);
371 n = c->sh.e.y - c->sh.s.y;
372 p = CRT_ADDR_POS (&c->sh.s);
376 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
381 if (ansi_scroll_up (a, c->sh.s, c->sh.e))
384 n = c->sh.e.y - c->sh.s.y;
385 p = CRT_ADDR_POS (&c->sh.s);
389 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
393 c->sh.dir = 0; //FIXME: horrid hack
398 ansi_spot_scroll (ANSI * a, CRT * c)
404 /*we only care about up for the moment */
405 ansi_spot_scroll_up (a, c);
416 ansi_draw_line (ANSI * a, CRT_CA * cap, int y)
418 CRT_Pos p = { 0, y };
419 CRT_CA *acap = &a->crt.screen[CRT_ADDR_POS (&p)];
421 for (p.x = 0; p.x < CRT_COLS; ++p.x)
423 if (p.x >= a->size.x)
426 if (crt_ca_cmp (*acap, *cap))
428 ansi_showhide_cursor (a, 1);
432 ansi_render (a, *acap);
441 ansi_resize_check (ANSI * a)
444 if (!crt_pos_cmp (a->terminal->size, a->size))
447 terminal_getsize (a->terminal);
449 a->size = a->terminal->size;
451 a->pos.x = ANSI_INVAL;
452 a->hide_cursor = ANSI_INVAL;
456 // FIXME: -- echos back crap?
457 // a->terminal->xmit (a->terminal, "\033[c", 3);
460 a->terminal->xmit (a->terminal, "\033=", 2);
461 a->terminal->xmit (a->terminal, "\033[?6l", 5);
462 a->terminal->xmit (a->terminal, "\033[r", 3);
466 #define HISTORY_GUESS_SCROLL 24 /*guess all 24 lines usually scroll */
467 /*if they haven't then ansi_draw will patch it up*/
470 ansi_history (ANSI * a, History * h)
474 /*Do we need to catch up on history?*/
476 if (a->history_ptr == h->wptr)
478 ansi_resize_check (a);
480 if ((a->size.x < CRT_COLS) || (a->size.y < CRT_ROWS))
483 ansi_force_attr_normal (a);
484 ansi_set_color (a, CRT_COLOR_NORMAL);
486 i = sprintf (buf, "\033[%d;%dr", 1, HISTORY_GUESS_SCROLL);
487 a->terminal->xmit (a->terminal, buf, i);
490 while (a->history_ptr != h->wptr)
493 History_ent *e = &h->lines[a->history_ptr];
495 HISTORY_INC (h, a->history_ptr);
500 /*If so write the line ot the top of the screen */
501 ansi_draw_line (a, e->line, 0);
504 /*Roll HISTORY_GUESS_SCROLL lines up putting the top line into the xterm's history */
507 ansi_showhide_cursor (a, 1);
508 i = sprintf (buf, "\033[%d;%dH", HISTORY_GUESS_SCROLL, 1);
509 a->terminal->xmit (a->terminal, buf, i);
510 a->terminal->xmit (a->terminal, "\033D", 2);
511 a->pos.x = ANSI_INVAL;
513 /*now do the same in our image of the screen */
522 for (s.y++; s.y < HISTORY_GUESS_SCROLL; s.y++, e.y++)
524 memcpy (&a->crt.screen[CRT_ADDR_POS (&e)],
525 &a->crt.screen[CRT_ADDR_POS (&s)],
526 sizeof (CRT_CA) * CRT_COLS);
532 crt_erase (&a->crt, s, e, 1);
537 a->terminal->xmit (a->terminal, "\033[r", 3);
538 a->pos.x = ANSI_INVAL;
546 ansi_draw (ANSI * a, CRT * c)
550 int hidden_cursor = 0;
552 ansi_resize_check (a);
556 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
558 if (p.y >= a->size.y)
561 ansi_draw_line (a, &c->screen[CRT_ADDR (p.y, 0)], p.y);
566 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
568 char msg[1024]; // = "Window is too small";
574 sprintf (msg, "Window too small (%dx%d need %dx%d)", a->size.x,
575 a->size.y, CRT_COLS, CRT_ROWS);
577 ansi_showhide_cursor (a, 1);
578 ansi_set_attr (a, CRT_ATTR_REVERSE);
579 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
582 a->terminal->xmit (a->terminal, msg, i);
583 a->pos.x = ANSI_INVAL;
587 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
589 ansi_showhide_cursor (a, 1);
594 ansi_move (a, a->crt.pos);
596 a->crt.hide_cursor = c->hide_cursor;
597 ansi_showhide_cursor (a, a->crt.hide_cursor);
601 ansi_reset (ANSI * a, CRT * c)
604 ansi_draw (a, c ? c : &a->crt);
608 ansi_terminal_reset (ANSI * a)
610 CRT_Pos p = { 0, CRT_ROWS };
611 ansi_force_attr_normal (a);
617 ansi_flush_escape (ANSI * a, Context * c)
619 ANSI_Parser *p = &a->parser;
622 for (i = 0; i < p->escape_ptr; ++i)
624 vt102_send (c, p->escape_buf[i]);
632 ansi_parse_deckey (ANSI * a, Context * c)
634 ANSI_Parser *p = &a->parser;
635 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
637 ansi_flush_escape (a, c);
641 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
643 vt102_send (c, KEY_UP + (p->escape_buf[2] - 'A'));
645 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
647 vt102_send (c, KEY_154 + (p->escape_buf[2] - 'a'));
651 ansi_flush_escape (a, c);
659 ansi_parse_ansikey (ANSI * a, Context * c)
661 ANSI_Parser *p = &a->parser;
663 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
665 ansi_flush_escape (a, c);
668 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
670 vt102_send (c, KEY_180 + (p->escape_buf[2] - '0'));
674 ansi_flush_escape (a, c);
685 ansi_parse_escape (ANSI * a, Context * c)
687 ANSI_Parser *p = &a->parser;
688 switch (p->escape_ptr)
694 switch (p->escape_buf[1])
700 ansi_flush_escape (a, c);
704 switch (p->escape_buf[1])
707 ansi_parse_deckey (a, c);
710 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
711 ansi_parse_deckey (a, c);
714 ansi_flush_escape (a, c);
718 switch (p->escape_buf[1])
721 ansi_parse_ansikey (a, c);
724 ansi_flush_escape (a, c);
728 ansi_flush_escape (a, c);
734 ansi_check_escape (ANSI * a, Context * c)
736 ANSI_Parser *p = &a->parser;
737 struct timeval now, diff;
738 gettimeofday (&now, NULL);
739 timersub (&now, &p->last_escape, &diff);
742 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
743 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
751 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
752 ansi_flush_escape (a, c);
758 ansi_parse_char (ANSI * a, Context * c, int ch)
760 ANSI_Parser *p = &a->parser;
763 /*See if it's time to flush the escape*/
764 ansi_check_escape (a, c);
769 ansi_flush_escape (a, c);
773 gettimeofday (&p->last_escape, NULL);
778 p->escape_buf[p->escape_ptr++] = ch;
779 ansi_parse_escape (a, c);
789 ansi_parse (ANSI * a, Context * c, char *buf, int len)
792 ansi_parse_char (a, c, *(buf++));
796 ansi_dispatch (ANSI * a, Context * c)
801 ansi_check_escape (a, c);
807 red = a->terminal->recv (a->terminal, buf, sizeof (buf));
819 ansi_reset (a, NULL);
824 a->history_ptr = c->h->wptr;
825 HISTORY_INC (c->h, a->history_ptr);
831 ansi_parse (a, c, buf, red);
838 ansi_update (ANSI * a, Context * c)
840 ansi_history (a, c->h);
841 ansi_draw (a, &c->v->crt);