4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.18 2008/02/13 09:12:21 james
14 * *** empty log message ***
16 * Revision 1.17 2008/02/13 01:08:18 james
17 * *** empty log message ***
19 * Revision 1.16 2008/02/07 13:22:51 james
20 * *** empty log message ***
22 * Revision 1.15 2008/02/07 13:19:48 james
23 * *** empty log message ***
25 * Revision 1.14 2008/02/07 12:21:16 james
26 * *** empty log message ***
28 * Revision 1.13 2008/02/07 12:16:04 james
29 * *** empty log message ***
31 * Revision 1.12 2008/02/07 11:32:41 james
32 * *** empty log message ***
34 * Revision 1.11 2008/02/07 11:11:14 staffcvs
35 * *** empty log message ***
37 * Revision 1.10 2008/02/07 01:02:52 james
38 * *** empty log message ***
40 * Revision 1.9 2008/02/07 00:43:27 james
41 * *** empty log message ***
43 * Revision 1.8 2008/02/07 00:39:13 james
44 * *** empty log message ***
46 * Revision 1.7 2008/02/06 20:26:57 james
47 * *** empty log message ***
49 * Revision 1.6 2008/02/06 17:53:28 james
50 * *** empty log message ***
52 * Revision 1.5 2008/02/06 15:53:22 james
53 * *** empty log message ***
55 * Revision 1.4 2008/02/04 20:23:55 james
56 * *** empty log message ***
58 * Revision 1.3 2008/02/04 05:45:55 james
61 * Revision 1.2 2008/02/04 02:05:06 james
62 * *** empty log message ***
64 * Revision 1.1 2008/02/03 23:31:25 james
65 * *** empty log message ***
72 ansi_move (ANSI * a, CRT_Pos p)
76 int dx = p.x - a->pos.x;
77 int dy = p.y - a->pos.y;
79 // a->pos.x = ANSI_INVAL;
81 if (a->pos.x != ANSI_INVAL)
91 a->terminal->xmit (a->terminal, "\033[C", 3);
95 a->terminal->xmit (a->terminal, "\033[D", 3);
99 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
100 a->terminal->xmit (a->terminal, buf, n);
107 a->terminal->xmit (a->terminal, "\033[A", 3);
111 a->terminal->xmit (a->terminal, "\033[B", 3);
115 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
116 a->terminal->xmit (a->terminal, buf, n);
120 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
121 a->terminal->xmit (a->terminal, buf, n);
128 a->terminal->xmit (a->terminal, "\033[E", 3);
132 a->terminal->xmit (a->terminal, "\033[F", 3);
136 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
137 a->terminal->xmit (a->terminal, buf, n);
141 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
142 a->terminal->xmit (a->terminal, buf, n);
147 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
148 a->terminal->xmit (a->terminal, buf, n);
153 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
154 a->terminal->xmit (a->terminal, buf, n);
162 ansi_showhide_cursor (ANSI * a, int hide)
164 if (a->hide_cursor == hide)
169 a->terminal->xmit (a->terminal, "\033[?25l", 6);
173 a->terminal->xmit (a->terminal, "\033[?25h", 6);
176 a->hide_cursor = hide;
181 ansi_force_attr_normal (ANSI * a)
183 a->terminal->xmit (a->terminal, "\033[0m", 4);
184 a->attr = CRT_ATTR_NORMAL;
185 a->color = ANSI_INVAL;
189 ansi_set_color (ANSI * a, int color)
196 if ((a->color == ANSI_INVAL) || (color != a->color))
198 fg = CRT_COLOR_FG (color);
199 bg = CRT_COLOR_BG (color);
201 if (fg & CRT_COLOR_INTENSITY)
210 if (bg & CRT_COLOR_INTENSITY)
219 i = sprintf (buf, "\033[%d;%dm", fg, bg);
221 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
224 a->terminal->xmit (a->terminal, buf, i);
230 ansi_set_attr (ANSI * a, int attr)
234 dif = attr ^ a->attr;
242 if (attr == CRT_ATTR_NORMAL)
244 ansi_force_attr_normal (a);
249 if (dif & CRT_ATTR_UNDERLINE)
251 if (attr & CRT_ATTR_UNDERLINE)
253 a->terminal->xmit (a->terminal, "\033[4m", 4);
257 a->terminal->xmit (a->terminal, "\033[24m", 5);
260 if (dif & CRT_ATTR_REVERSE)
262 if (attr & CRT_ATTR_REVERSE)
264 a->terminal->xmit (a->terminal, "\033[7m", 4);
268 a->terminal->xmit (a->terminal, "\033[27m", 5);
271 if (dif & CRT_ATTR_BOLD)
273 if (attr & CRT_ATTR_BOLD)
275 a->terminal->xmit (a->terminal, "\033[1m", 4);
279 a->terminal->xmit (a->terminal, "\033[21m", 5);
280 a->terminal->xmit (a->terminal, "\033[22m", 5);
288 ansi_render (ANSI * a, CRT_CA ca)
297 ansi_set_attr (a, ca.attr);
298 ansi_set_color (a, ca.color);
300 a->terminal->xmit (a->terminal, &ca.chr, 1);
304 /*Can't easily wrap round here as don't know size of destination screen*/
305 /*so invalidate the cached cursor position*/
307 if (a->pos.x >= a->size.x)
308 a->pos.x = ANSI_INVAL;
319 ansi_force_attr_normal (a);
320 ansi_set_color (a, CRT_COLOR_NORMAL);
322 a->terminal->xmit (a->terminal, "\033[2J", 4);
323 /*different emulators leave cursor in different places after cls differently*/
324 a->pos.x = ANSI_INVAL;
329 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
335 if (e.x != (CRT_COLS - 1))
339 if (s.y >= a->size.y)
342 ansi_showhide_cursor (a, 1);
343 i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
344 a->terminal->xmit (a->terminal, buf, i);
345 i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
346 a->terminal->xmit (a->terminal, buf, i);
347 a->terminal->xmit (a->terminal, "\033D", 2);
348 a->terminal->xmit (a->terminal, "\033[r", 3);
351 crt_erase (&a->crt, s, e, 1);
353 a->pos.x = ANSI_INVAL;
360 ansi_spot_scroll_up (ANSI * a, CRT * c)
364 l = c->sh.e.x - c->sh.s.x;
366 l *= sizeof (CRT_CA);
368 n = c->sh.e.y - c->sh.s.y;
369 p = CRT_ADDR_POS (&c->sh.s);
373 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
378 if (ansi_scroll_up (a, c->sh.s, c->sh.e))
381 n = c->sh.e.y - c->sh.s.y;
382 p = CRT_ADDR_POS (&c->sh.s);
386 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
390 c->sh.dir = 0; //FIXME: horrid hack
395 ansi_spot_scroll (ANSI * a, CRT * c)
401 /*we only care about up for the moment */
402 ansi_spot_scroll_up (a, c);
413 ansi_draw (ANSI * a, CRT * c)
417 int hidden_cursor = 0;
419 if (crt_pos_cmp (a->terminal->size, a->size))
421 terminal_getsize (a->terminal);
423 a->size = a->terminal->size;
425 a->pos.x = ANSI_INVAL;
426 a->hide_cursor = ANSI_INVAL;
430 // FIXME: -- echos back crap?
431 // a->terminal->xmit (a->terminal, "\033[c", 3);
434 a->terminal->xmit (a->terminal, "\033=", 2);
435 a->terminal->xmit (a->terminal, "\033[?6l", 5);
436 a->terminal->xmit (a->terminal, "\033[r", 3);
441 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
443 if (p.y >= a->size.y)
445 o = CRT_ADDR (p.y, 0);
446 for (p.x = 0; p.x < CRT_COLS; ++p.x, ++o)
448 if (p.x >= a->size.x)
450 if (crt_ca_cmp (a->crt.screen[o], c->screen[o]))
452 ansi_showhide_cursor (a, 1);
453 a->crt.screen[o] = c->screen[o];
456 ansi_render (a, a->crt.screen[o]);
462 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
464 char msg[] = "Window is too small";
468 ansi_showhide_cursor (a, 1);
469 ansi_set_attr (a, CRT_ATTR_REVERSE);
470 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
473 a->terminal->xmit (a->terminal, msg, sizeof (msg));
474 a->pos.x = ANSI_INVAL;
478 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
480 ansi_showhide_cursor (a, 1);
485 ansi_move (a, a->crt.pos);
487 a->crt.hide_cursor = c->hide_cursor;
488 ansi_showhide_cursor (a, a->crt.hide_cursor);
492 ansi_reset (ANSI * a)
495 ansi_draw (a, &a->crt);
499 ansi_flush_escape (ANSI * a, Context * c)
501 ANSI_Parser *p = &a->parser;
504 for (i = 0; i < p->escape_ptr; ++i)
506 vt102_send (c, p->escape_buf[i]);
514 ansi_parse_deckey (ANSI * a, Context * c)
516 ANSI_Parser *p = &a->parser;
517 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
519 ansi_flush_escape (a, c);
523 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
525 vt102_send (c, KEY_UP + (p->escape_buf[2] - 'A'));
527 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
529 vt102_send (c, KEY_154 + (p->escape_buf[2] - 'a'));
533 ansi_flush_escape (a, c);
541 ansi_parse_ansikey (ANSI * a, Context * c)
543 ANSI_Parser *p = &a->parser;
545 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
547 ansi_flush_escape (a, c);
550 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
552 vt102_send (c, KEY_180 + (p->escape_buf[2] - '0'));
556 ansi_flush_escape (a, c);
567 ansi_parse_escape (ANSI * a, Context * c)
569 ANSI_Parser *p = &a->parser;
570 switch (p->escape_ptr)
576 switch (p->escape_buf[1])
582 ansi_flush_escape (a, c);
586 switch (p->escape_buf[1])
589 ansi_parse_deckey (a, c);
592 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
593 ansi_parse_deckey (a, c);
596 ansi_flush_escape (a, c);
600 switch (p->escape_buf[1])
603 ansi_parse_ansikey (a, c);
606 ansi_flush_escape (a, c);
610 ansi_flush_escape (a, c);
616 ansi_check_escape (ANSI * a, Context * c)
618 ANSI_Parser *p = &a->parser;
619 struct timeval now, diff;
620 gettimeofday (&now, NULL);
621 timersub (&now, &p->last_escape, &diff);
624 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
625 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
633 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
634 ansi_flush_escape (a, c);
640 ansi_parse_char (ANSI * a, Context * c, int ch)
642 ANSI_Parser *p = &a->parser;
645 /*See if it's time to flush the escape*/
646 ansi_check_escape (a, c);
651 ansi_flush_escape (a, c);
655 gettimeofday (&p->last_escape, NULL);
660 p->escape_buf[p->escape_ptr++] = ch;
661 ansi_parse_escape (a, c);
671 ansi_parse (ANSI * a, Context * c, char *buf, int len)
674 ansi_parse_char (a, c, *(buf++));
678 ansi_dispatch (ANSI * a, Context * c)
683 ansi_check_escape (a, c);
685 red = a->terminal->recv (a->terminal, buf, sizeof (buf));
700 ansi_parse (a, c, buf, red);