4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.17 2008/02/13 01:08:18 james
14 * *** empty log message ***
16 * Revision 1.16 2008/02/07 13:22:51 james
17 * *** empty log message ***
19 * Revision 1.15 2008/02/07 13:19:48 james
20 * *** empty log message ***
22 * Revision 1.14 2008/02/07 12:21:16 james
23 * *** empty log message ***
25 * Revision 1.13 2008/02/07 12:16:04 james
26 * *** empty log message ***
28 * Revision 1.12 2008/02/07 11:32:41 james
29 * *** empty log message ***
31 * Revision 1.11 2008/02/07 11:11:14 staffcvs
32 * *** empty log message ***
34 * Revision 1.10 2008/02/07 01:02:52 james
35 * *** empty log message ***
37 * Revision 1.9 2008/02/07 00:43:27 james
38 * *** empty log message ***
40 * Revision 1.8 2008/02/07 00:39:13 james
41 * *** empty log message ***
43 * Revision 1.7 2008/02/06 20:26:57 james
44 * *** empty log message ***
46 * Revision 1.6 2008/02/06 17:53:28 james
47 * *** empty log message ***
49 * Revision 1.5 2008/02/06 15:53:22 james
50 * *** empty log message ***
52 * Revision 1.4 2008/02/04 20:23:55 james
53 * *** empty log message ***
55 * Revision 1.3 2008/02/04 05:45:55 james
58 * Revision 1.2 2008/02/04 02:05:06 james
59 * *** empty log message ***
61 * Revision 1.1 2008/02/03 23:31:25 james
62 * *** empty log message ***
68 set_nonblocking (int fd)
71 arg = fcntl (fd, F_GETFL, arg);
73 fcntl (fd, F_SETFL, arg);
80 arg = fcntl (fd, F_GETFL, arg);
82 fcntl (fd, F_SETFL, arg);
87 ansi_move (ANSI * a, CRT_Pos p)
91 int dx = p.x - a->pos.x;
92 int dy = p.y - a->pos.y;
94 // a->pos.x = ANSI_INVAL;
96 if (a->pos.x != ANSI_INVAL)
106 a->terminal->xmit (a->terminal, "\033[C", 3);
110 a->terminal->xmit (a->terminal, "\033[D", 3);
114 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
115 a->terminal->xmit (a->terminal, buf, n);
122 a->terminal->xmit (a->terminal, "\033[A", 3);
126 a->terminal->xmit (a->terminal, "\033[B", 3);
130 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
131 a->terminal->xmit (a->terminal, buf, n);
135 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
136 a->terminal->xmit (a->terminal, buf, n);
143 a->terminal->xmit (a->terminal, "\033[E", 3);
147 a->terminal->xmit (a->terminal, "\033[F", 3);
151 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
152 a->terminal->xmit (a->terminal, buf, n);
156 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
157 a->terminal->xmit (a->terminal, buf, n);
162 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
163 a->terminal->xmit (a->terminal, buf, n);
168 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
169 a->terminal->xmit (a->terminal, buf, n);
177 ansi_showhide_cursor (ANSI * a, int hide)
179 if (a->hide_cursor == hide)
184 a->terminal->xmit (a->terminal, "\033[?25l", 6);
188 a->terminal->xmit (a->terminal, "\033[?25h", 6);
191 a->hide_cursor = hide;
196 ansi_force_attr_normal (ANSI * a)
198 a->terminal->xmit (a->terminal, "\033[0m", 4);
199 a->attr = CRT_ATTR_NORMAL;
200 a->color = ANSI_INVAL;
204 ansi_set_color (ANSI * a, int color)
211 if ((a->color == ANSI_INVAL) || (color != a->color))
213 fg = CRT_COLOR_FG (color);
214 bg = CRT_COLOR_BG (color);
216 if (fg & CRT_COLOR_INTENSITY)
225 if (bg & CRT_COLOR_INTENSITY)
234 i = sprintf (buf, "\033[%d;%dm", fg, bg);
236 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
239 a->terminal->xmit (a->terminal, buf, i);
245 ansi_set_attr (ANSI * a, int attr)
249 dif = attr ^ a->attr;
257 if (attr == CRT_ATTR_NORMAL)
259 ansi_force_attr_normal (a);
264 if (dif & CRT_ATTR_UNDERLINE)
266 if (attr & CRT_ATTR_UNDERLINE)
268 a->terminal->xmit (a->terminal, "\033[4m", 4);
272 a->terminal->xmit (a->terminal, "\033[24m", 5);
275 if (dif & CRT_ATTR_REVERSE)
277 if (attr & CRT_ATTR_REVERSE)
279 a->terminal->xmit (a->terminal, "\033[7m", 4);
283 a->terminal->xmit (a->terminal, "\033[27m", 5);
286 if (dif & CRT_ATTR_BOLD)
288 if (attr & CRT_ATTR_BOLD)
290 a->terminal->xmit (a->terminal, "\033[1m", 4);
294 a->terminal->xmit (a->terminal, "\033[21m", 5);
295 a->terminal->xmit (a->terminal, "\033[22m", 5);
303 ansi_render (ANSI * a, CRT_CA ca)
312 ansi_set_attr (a, ca.attr);
313 ansi_set_color (a, ca.color);
315 a->terminal->xmit (a->terminal, &ca.chr, 1);
319 /*Can't easily wrap round here as don't know size of destination screen*/
320 /*so invalidate the cached cursor position*/
322 if (a->pos.x >= a->size.x)
323 a->pos.x = ANSI_INVAL;
334 ansi_force_attr_normal (a);
335 ansi_set_color (a, CRT_COLOR_NORMAL);
337 a->terminal->xmit (a->terminal, "\033[2J", 4);
338 /*different emulators leave cursor in different places after cls differently*/
339 a->pos.x = ANSI_INVAL;
344 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
350 if (e.x != (CRT_COLS - 1))
354 if (s.y >= a->size.y)
357 ansi_showhide_cursor (a, 1);
358 i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
359 a->terminal->xmit (a->terminal, buf, i);
360 i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
361 a->terminal->xmit (a->terminal, buf, i);
362 a->terminal->xmit (a->terminal, "\033D", 2);
363 a->terminal->xmit (a->terminal, "\033[r", 3);
366 crt_erase (&a->crt, s, e, 1);
368 a->pos.x = ANSI_INVAL;
375 ansi_spot_scroll_up (ANSI * a, CRT * c)
379 l = c->sh.e.x - c->sh.s.x;
381 l *= sizeof (CRT_CA);
383 n = c->sh.e.y - c->sh.s.y;
384 p = CRT_ADDR_POS (&c->sh.s);
388 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
393 if (ansi_scroll_up (a, c->sh.s, c->sh.e))
396 n = c->sh.e.y - c->sh.s.y;
397 p = CRT_ADDR_POS (&c->sh.s);
401 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
405 c->sh.dir = 0; //FIXME: horrid hack
410 ansi_spot_scroll (ANSI * a, CRT * c)
416 /*we only care about up for the moment */
417 ansi_spot_scroll_up (a, c);
428 ansi_draw (ANSI * a, CRT * c)
432 int hidden_cursor = 0;
434 if (crt_pos_cmp(a->terminal.size,a->size)) {
437 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
439 if (p.y >= a->size.y)
441 o = CRT_ADDR (p.y, 0);
442 for (p.x = 0; p.x < CRT_COLS; ++p.x, ++o)
444 if (p.x >= a->size.x)
446 if (crt_ca_cmp (a->crt.screen[o], c->screen[o]))
448 ansi_showhide_cursor (a, 1);
449 a->crt.screen[o] = c->screen[o];
452 ansi_render (a, a->crt.screen[o]);
458 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
460 char msg[] = "Window is too small";
464 ansi_showhide_cursor (a, 1);
465 ansi_set_attr (a, CRT_ATTR_REVERSE);
466 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
469 a->terminal->xmit (a->terminal, msg, sizeof (msg));
470 a->pos.x = ANSI_INVAL;
474 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
476 ansi_showhide_cursor (a, 1);
481 ansi_move (a, a->crt.pos);
483 a->crt.hide_cursor = c->hide_cursor;
484 ansi_showhide_cursor (a, a->crt.hide_cursor);
488 ansi_reset (ANSI * a)
490 // FIXME: -- echos back crap?
491 // a->terminal->xmit (a->terminal, "\033[c", 3);
493 terminal_getsize(a->terminal);
495 a->size=a->terminal->size;
497 a->pos.x = ANSI_INVAL;
498 a->hide_cursor = ANSI_INVAL;
503 a->terminal->xmit (a->terminal, "\033=", 2);
504 a->terminal->xmit (a->terminal, "\033[?6l", 5);
505 a->terminal->xmit (a->terminal, "\033[r", 3);
506 ansi_draw (a, &a->crt);
510 ansi_flush_escape (ANSI * a, Context *c)
512 ANSI_Parser *p = &a->parser;
515 for (i = 0; i < p->escape_ptr; ++i)
517 vt102_send (c->v, p->escape_buf[i], c->t);
525 ansi_parse_deckey (ANSI * a, Context *c)
527 ANSI_Parser *p = &a->parser;
528 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
530 ansi_flush_escape (a, c);
534 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
536 vt102_send (c->v, KEY_UP + (p->escape_buf[2] - 'A'), c->t);
538 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
540 vt102_send (c->v, KEY_154 + (p->escape_buf[2] - 'a'), c->t);
544 ansi_flush_escape (a,c);
552 ansi_parse_ansikey (ANSI * a,Context *c)
554 ANSI_Parser *p = &a->parser;
556 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
558 ansi_flush_escape (a, c);
561 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
563 vt102_send (c->v, KEY_180 + (p->escape_buf[2] - '0'), c->t);
567 ansi_flush_escape (a,c);
578 ansi_parse_escape (ANSI * a,Context *c)
580 ANSI_Parser *p = &a->parser;
581 switch (p->escape_ptr)
587 switch (p->escape_buf[1])
593 ansi_flush_escape (a,c);
597 switch (p->escape_buf[1])
600 ansi_parse_deckey (a, c);
603 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
604 ansi_parse_deckey (a,c);
607 ansi_flush_escape (a,c);
611 switch (p->escape_buf[1])
614 ansi_parse_ansikey (a, c);
617 ansi_flush_escape (a,c);
621 ansi_flush_escape (a,c);
627 ansi_check_escape (ANSI * a, Context *c)
629 ANSI_Parser *p = &a->parser;
630 struct timeval now, diff;
631 gettimeofday (&now, NULL);
632 timersub (&now, &p->last_escape, &diff);
635 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
636 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
644 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
645 ansi_flush_escape (a,c);
651 ansi_parse_char (ANSI * a,Context *c, int ch)
653 ANSI_Parser *p = &a->parser;
656 /*See if it's time to flush the escape*/
657 ansi_check_escape (a, c);
662 ansi_flush_escape (a,c);
666 gettimeofday (&p->last_escape, NULL);
671 p->escape_buf[p->escape_ptr++] = ch;
672 ansi_parse_escape (a,c);
676 vt102_send (c->v, ch, c->t);
682 ansi_parse (ANSI * a,Context *c, char *buf, int len)
685 ansi_parse_char (a,c, *(buf++));
689 ansi_dispatch (ANSI * a, Context *c)
694 ansi_check_escape (a, c);
697 red = a->terminal->recv(a->terminal,buf,sizeof(buf));
712 ansi_parse (a,c, buf, red );