4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.16 2008/02/07 13:22:51 james
14 * *** empty log message ***
16 * Revision 1.15 2008/02/07 13:19:48 james
17 * *** empty log message ***
19 * Revision 1.14 2008/02/07 12:21:16 james
20 * *** empty log message ***
22 * Revision 1.13 2008/02/07 12:16:04 james
23 * *** empty log message ***
25 * Revision 1.12 2008/02/07 11:32:41 james
26 * *** empty log message ***
28 * Revision 1.11 2008/02/07 11:11:14 staffcvs
29 * *** empty log message ***
31 * Revision 1.10 2008/02/07 01:02:52 james
32 * *** empty log message ***
34 * Revision 1.9 2008/02/07 00:43:27 james
35 * *** empty log message ***
37 * Revision 1.8 2008/02/07 00:39:13 james
38 * *** empty log message ***
40 * Revision 1.7 2008/02/06 20:26:57 james
41 * *** empty log message ***
43 * Revision 1.6 2008/02/06 17:53:28 james
44 * *** empty log message ***
46 * Revision 1.5 2008/02/06 15:53:22 james
47 * *** empty log message ***
49 * Revision 1.4 2008/02/04 20:23:55 james
50 * *** empty log message ***
52 * Revision 1.3 2008/02/04 05:45:55 james
55 * Revision 1.2 2008/02/04 02:05:06 james
56 * *** empty log message ***
58 * Revision 1.1 2008/02/03 23:31:25 james
59 * *** empty log message ***
65 set_nonblocking (int fd)
68 arg = fcntl (fd, F_GETFL, arg);
70 fcntl (fd, F_SETFL, arg);
77 arg = fcntl (fd, F_GETFL, arg);
79 fcntl (fd, F_SETFL, arg);
84 ansi_read (ANSI * a, void *buf, int n)
88 set_nonblocking (a->fd);
89 red = read (a->fd, buf, n);
94 if ((red == -1) && (errno == EAGAIN))
103 ansi_write (ANSI * a, char *buf, int n)
105 set_blocking (a->fd);
106 write (a->fd, buf, n);
110 ansi_getsize (ANSI * a)
112 struct winsize sz = { 0 };
113 if (ioctl (a->fd, TIOCGWINSZ, &sz))
115 a->size.x = CRT_COLS;
116 a->size.y = CRT_ROWS;
120 a->size.x = sz.ws_col;
121 a->size.y = sz.ws_row;
128 ansi_move (ANSI * a, CRT_Pos p)
132 int dx = p.x - a->pos.x;
133 int dy = p.y - a->pos.y;
135 // a->pos.x = ANSI_INVAL;
137 if (a->pos.x != ANSI_INVAL)
147 ansi_write (a, "\033[C", 3);
151 ansi_write (a, "\033[D", 3);
155 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
156 ansi_write (a, buf, n);
163 ansi_write (a, "\033[A", 3);
167 ansi_write (a, "\033[B", 3);
171 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
172 ansi_write (a, buf, n);
176 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
177 ansi_write (a, buf, n);
184 ansi_write (a, "\033[E", 3);
188 ansi_write (a, "\033[F", 3);
192 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
193 ansi_write (a, buf, n);
197 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
198 ansi_write (a, buf, n);
203 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
204 ansi_write (a, buf, n);
209 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
210 ansi_write (a, buf, n);
218 ansi_showhide_cursor (ANSI * a, int hide)
220 if (a->hide_cursor == hide)
225 ansi_write (a, "\033[?25l", 6);
229 ansi_write (a, "\033[?25h", 6);
232 a->hide_cursor = hide;
237 ansi_force_attr_normal (ANSI * a)
239 ansi_write (a, "\033[0m", 4);
240 a->attr = CRT_ATTR_NORMAL;
241 a->color = ANSI_INVAL;
245 ansi_set_color (ANSI * a, int color)
252 if ((a->color == ANSI_INVAL) || (color != a->color))
254 fg = CRT_COLOR_FG (color);
255 bg = CRT_COLOR_BG (color);
257 if (fg & CRT_COLOR_INTENSITY)
266 if (bg & CRT_COLOR_INTENSITY)
275 i = sprintf (buf, "\033[%d;%dm", fg, bg);
277 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
280 ansi_write (a, buf, i);
286 ansi_set_attr (ANSI * a, int attr)
290 dif = attr ^ a->attr;
298 if (attr == CRT_ATTR_NORMAL)
300 ansi_force_attr_normal (a);
305 if (dif & CRT_ATTR_UNDERLINE)
307 if (attr & CRT_ATTR_UNDERLINE)
309 ansi_write (a, "\033[4m", 4);
313 ansi_write (a, "\033[24m", 5);
316 if (dif & CRT_ATTR_REVERSE)
318 if (attr & CRT_ATTR_REVERSE)
320 ansi_write (a, "\033[7m", 4);
324 ansi_write (a, "\033[27m", 5);
327 if (dif & CRT_ATTR_BOLD)
329 if (attr & CRT_ATTR_BOLD)
331 ansi_write (a, "\033[1m", 4);
335 ansi_write (a, "\033[21m", 5);
336 ansi_write (a, "\033[22m", 5);
344 ansi_render (ANSI * a, CRT_CA ca)
353 ansi_set_attr (a, ca.attr);
354 ansi_set_color (a, ca.color);
356 ansi_write (a, &ca.chr, 1);
360 /*Can't easily wrap round here as don't know size of destination screen*/
361 /*so invalidate the cached cursor position*/
363 if (a->pos.x >= a->size.x)
364 a->pos.x = ANSI_INVAL;
375 ansi_force_attr_normal (a);
376 ansi_set_color (a, CRT_COLOR_NORMAL);
378 ansi_write (a, "\033[2J", 4);
379 /*different emulators leave cursor in different places after cls differently*/
380 a->pos.x = ANSI_INVAL;
384 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
390 if (e.x != (CRT_COLS - 1))
394 if (s.y >= a->size.y)
397 ansi_showhide_cursor (a, 1);
398 i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
399 ansi_write (a, buf, i);
400 i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
401 ansi_write (a, buf, i);
402 ansi_write (a, "\033D", 2);
403 ansi_write (a, "\033[r", 3);
406 crt_erase (&a->crt, s, e, 1);
408 a->pos.x = ANSI_INVAL;
415 ansi_spot_scroll_up (ANSI * a, CRT * c)
419 l = c->sh.e.x - c->sh.s.x;
421 l *= sizeof (CRT_CA);
423 n = c->sh.e.y - c->sh.s.y;
424 p = CRT_ADDR_POS (&c->sh.s);
428 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
433 if (ansi_scroll_up (a, c->sh.s, c->sh.e))
436 n = c->sh.e.y - c->sh.s.y;
437 p = CRT_ADDR_POS (&c->sh.s);
441 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
445 c->sh.dir = 0; //FIXME: horrid hack
450 ansi_spot_scroll (ANSI * a, CRT * c)
456 /*we only care about up for the moment */
457 ansi_spot_scroll_up (a, c);
468 ansi_draw (ANSI * a, CRT * c)
472 int hidden_cursor = 0;
474 ansi_spot_scroll (a, c);
476 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
478 if (p.y >= a->size.y)
480 o = CRT_ADDR (p.y, 0);
481 for (p.x = 0; p.x < CRT_COLS; ++p.x, ++o)
483 if (p.x >= a->size.x)
485 if (crt_ca_cmp (a->crt.screen[o], c->screen[o]))
487 ansi_showhide_cursor (a, 1);
488 a->crt.screen[o] = c->screen[o];
491 ansi_render (a, a->crt.screen[o]);
497 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
499 char msg[] = "Window is too small";
503 ansi_showhide_cursor (a, 1);
504 ansi_set_attr (a, CRT_ATTR_REVERSE);
505 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
508 ansi_write (a, msg, sizeof (msg));
509 a->pos.x = ANSI_INVAL;
513 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
515 ansi_showhide_cursor (a, 1);
520 ansi_move (a, a->crt.pos);
522 a->crt.hide_cursor = c->hide_cursor;
523 ansi_showhide_cursor (a, a->crt.hide_cursor);
527 ansi_reset (ANSI * a)
529 // FIXME: -- echos back crap?
530 // ansi_write (a, "\033[c", 3);
533 a->pos.x = ANSI_INVAL;
534 a->hide_cursor = ANSI_INVAL;
539 ansi_write (a, "\033=", 2);
540 ansi_write (a, "\033[?6l", 5);
541 ansi_write (a, "\033[r", 3);
542 ansi_draw (a, &a->crt);
546 ansi_flush_escape (ANSI * a, VT102 * v, TTY * t)
548 ANSI_Parser *p = &a->parser;
551 for (i = 0; i < p->escape_ptr; ++i)
553 vt102_send (v, p->escape_buf[i], t);
561 ansi_parse_deckey (ANSI * a, VT102 * v, TTY * t)
563 ANSI_Parser *p = &a->parser;
564 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
566 ansi_flush_escape (a, v, t);
570 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
572 vt102_send (v, KEY_UP + (p->escape_buf[2] - 'A'), t);
574 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
576 vt102_send (v, KEY_154 + (p->escape_buf[2] - 'a'), t);
580 ansi_flush_escape (a, v, t);
588 ansi_parse_ansikey (ANSI * a, VT102 * v, TTY * t)
590 ANSI_Parser *p = &a->parser;
592 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
594 ansi_flush_escape (a, v, t);
597 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
599 vt102_send (v, KEY_180 + (p->escape_buf[2] - '0'), t);
603 ansi_flush_escape (a, v, t);
614 ansi_parse_escape (ANSI * a, VT102 * v, TTY * t)
616 ANSI_Parser *p = &a->parser;
617 switch (p->escape_ptr)
623 switch (p->escape_buf[1])
629 ansi_flush_escape (a, v, t);
633 switch (p->escape_buf[1])
636 ansi_parse_deckey (a, v, t);
639 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
640 ansi_parse_deckey (a, v, t);
643 ansi_flush_escape (a, v, t);
647 switch (p->escape_buf[1])
650 ansi_parse_ansikey (a, v, t);
653 ansi_flush_escape (a, v, t);
657 ansi_flush_escape (a, v, t);
663 ansi_check_escape (ANSI * a, VT102 * v, TTY * t)
665 ANSI_Parser *p = &a->parser;
666 struct timeval now, diff;
667 gettimeofday (&now, NULL);
668 timersub (&now, &p->last_escape, &diff);
671 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
672 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
680 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
681 ansi_flush_escape (a, v, t);
687 ansi_parse_char (ANSI * a, int c, VT102 * v, TTY * t)
689 ANSI_Parser *p = &a->parser;
692 /*See if it's time to flush the escape*/
693 ansi_check_escape (a, v, t);
698 ansi_flush_escape (a, v, t);
702 gettimeofday (&p->last_escape, NULL);
707 p->escape_buf[p->escape_ptr++] = c;
708 ansi_parse_escape (a, v, t);
712 vt102_send (v, c, t);
718 ansi_parse (ANSI * a, char *buf, int len, VT102 * v, TTY * t)
721 ansi_parse_char (a, *(buf++), v, t);
725 ansi_dispatch (ANSI * a, VT102 * v, TTY * t)
730 ansi_check_escape (a, v, t);
733 red = ansi_read (a, buf, sizeof (buf));
748 ansi_parse (a, buf, red, v, t);