4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.14 2008/02/07 12:21:16 james
14 * *** empty log message ***
16 * Revision 1.13 2008/02/07 12:16:04 james
17 * *** empty log message ***
19 * Revision 1.12 2008/02/07 11:32:41 james
20 * *** empty log message ***
22 * Revision 1.11 2008/02/07 11:11:14 staffcvs
23 * *** empty log message ***
25 * Revision 1.10 2008/02/07 01:02:52 james
26 * *** empty log message ***
28 * Revision 1.9 2008/02/07 00:43:27 james
29 * *** empty log message ***
31 * Revision 1.8 2008/02/07 00:39:13 james
32 * *** empty log message ***
34 * Revision 1.7 2008/02/06 20:26:57 james
35 * *** empty log message ***
37 * Revision 1.6 2008/02/06 17:53:28 james
38 * *** empty log message ***
40 * Revision 1.5 2008/02/06 15:53:22 james
41 * *** empty log message ***
43 * Revision 1.4 2008/02/04 20:23:55 james
44 * *** empty log message ***
46 * Revision 1.3 2008/02/04 05:45:55 james
49 * Revision 1.2 2008/02/04 02:05:06 james
50 * *** empty log message ***
52 * Revision 1.1 2008/02/03 23:31:25 james
53 * *** empty log message ***
59 set_nonblocking (int fd)
62 arg = fcntl (fd, F_GETFL, arg);
64 fcntl (fd, F_SETFL, arg);
71 arg = fcntl (fd, F_GETFL, arg);
73 fcntl (fd, F_SETFL, arg);
78 ansi_read (ANSI * a, void *buf, int n)
82 set_nonblocking (a->fd);
83 red = read (a->fd, buf, n);
88 if ((red == -1) && (errno == EAGAIN))
97 ansi_write (ANSI * a, char *buf, int n)
100 write (a->fd, buf, n);
104 ansi_getsize (ANSI * a)
106 struct winsize sz = { 0 };
107 if (ioctl (a->fd, TIOCGWINSZ, &sz))
109 a->size.x = CRT_COLS;
110 a->size.y = CRT_ROWS;
114 a->size.x = sz.ws_col;
115 a->size.y = sz.ws_row;
122 ansi_move (ANSI * a, CRT_Pos p)
126 int dx = p.x - a->pos.x;
127 int dy = p.y - a->pos.y;
129 // a->pos.x = ANSI_INVAL;
131 if (a->pos.x != ANSI_INVAL)
141 ansi_write (a, "\033[C", 3);
145 ansi_write (a, "\033[D", 3);
149 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
150 ansi_write (a, buf, n);
157 ansi_write (a, "\033[A", 3);
161 ansi_write (a, "\033[B", 3);
165 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
166 ansi_write (a, buf, n);
170 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
171 ansi_write (a, buf, n);
178 ansi_write (a, "\033[E", 3);
182 ansi_write (a, "\033[F", 3);
186 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
187 ansi_write (a, buf, n);
191 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
192 ansi_write (a, buf, n);
197 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
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);
212 ansi_showhide_cursor (ANSI * a, int hide)
214 if (a->hide_cursor == hide)
219 ansi_write (a, "\033[?25l", 6);
223 ansi_write (a, "\033[?25h", 6);
226 a->hide_cursor = hide;
231 ansi_force_attr_normal (ANSI * a)
233 ansi_write (a, "\033[0m", 4);
234 a->attr = CRT_ATTR_NORMAL;
235 a->color = ANSI_INVAL;
239 ansi_set_color (ANSI * a, int color)
246 if ((a->color == ANSI_INVAL) || (color!=a->color)) {
247 fg=CRT_COLOR_FG(color);
248 bg=CRT_COLOR_BG(color);
250 if (fg & CRT_COLOR_INTENSITY) {
256 if (bg & CRT_COLOR_INTENSITY) {
262 i=sprintf(buf,"\033[%d;%dm",fg,bg);
264 fprintf(stderr,"Color set to %d %d %x\n",fg,bg,color);
267 ansi_write (a,buf,i);
273 ansi_set_attr (ANSI * a, int attr)
277 dif = attr ^ a->attr;
285 if (attr == CRT_ATTR_NORMAL)
287 ansi_force_attr_normal (a);
292 if (dif & CRT_ATTR_UNDERLINE)
294 if (attr & CRT_ATTR_UNDERLINE)
296 ansi_write (a, "\033[4m", 4);
300 ansi_write (a, "\033[24m", 5);
303 if (dif & CRT_ATTR_REVERSE)
305 if (attr & CRT_ATTR_REVERSE)
307 ansi_write (a, "\033[7m", 4);
311 ansi_write (a, "\033[27m", 5);
314 if (dif & CRT_ATTR_BOLD)
316 if (attr & CRT_ATTR_BOLD)
318 ansi_write (a, "\033[1m", 4);
322 ansi_write (a, "\033[21m", 5);
323 ansi_write (a, "\033[22m", 5);
331 ansi_render (ANSI * a, CRT_CA ca)
340 ansi_set_attr (a, ca.attr);
341 ansi_set_color(a,ca.color);
343 ansi_write (a, &ca.chr, 1);
347 /*Can't easily wrap round here as don't know size of destination screen*/
348 /*so invalidate the cached cursor position*/
350 if (a->pos.x >= CRT_COLS)
351 a->pos.x = ANSI_INVAL;
362 ansi_force_attr_normal (a);
363 ansi_set_color(a,CRT_COLOR_NORMAL);
365 ansi_write (a, "\033[2J", 4);
366 /*different emulators leave cursor in different places after cls differently*/
367 a->pos.x = ANSI_INVAL;
372 ansi_draw (ANSI * a, CRT * c)
376 int hidden_cursor = 0;
378 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
380 if (p.y >= a->size.y)
382 o = CRT_ADDR (p.y, 0);
383 for (p.x = 0; p.x < CRT_COLS; ++p.x, ++o)
385 if (p.x >= a->size.x)
387 if (crt_ca_cmp (a->crt.screen[o], c->screen[o]))
389 ansi_showhide_cursor (a, 1);
390 a->crt.screen[o] = c->screen[o];
393 ansi_render (a, a->crt.screen[o]);
399 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
401 char msg[] = "Window is too small";
405 ansi_showhide_cursor (a, 1);
406 ansi_set_attr (a, CRT_ATTR_REVERSE);
407 ansi_set_color(a,CRT_MAKE_COLOR(CRT_COLOR_WHITE,CRT_COLOR_RED));
410 ansi_write (a, msg, sizeof (msg));
411 a->pos.x = ANSI_INVAL;
415 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
417 ansi_showhide_cursor (a, 1);
422 ansi_move (a, a->crt.pos);
424 a->crt.hide_cursor = c->hide_cursor;
425 ansi_showhide_cursor (a, a->crt.hide_cursor);
429 ansi_reset (ANSI * a)
431 // FIXME: -- echos back crap?
432 // ansi_write (a, "\033[c", 3);
435 a->pos.x = ANSI_INVAL;
436 a->hide_cursor = ANSI_INVAL;
441 ansi_write (a, "\033=", 2);
442 ansi_draw (a, &a->crt);
446 ansi_flush_escape (ANSI * a, VT102 * v,TTY *t)
448 ANSI_Parser *p = &a->parser;
451 for (i = 0; i < p->escape_ptr; ++i)
453 vt102_send (v, p->escape_buf[i],t);
461 ansi_parse_deckey (ANSI * a, VT102 * v,TTY *t)
463 ANSI_Parser *p = &a->parser;
464 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
466 ansi_flush_escape (a, v,t);
470 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
472 vt102_send (v, KEY_UP + (p->escape_buf[2] - 'A'),t);
474 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
476 vt102_send (v, KEY_154 + (p->escape_buf[2] - 'a'),t);
480 ansi_flush_escape (a, v,t);
488 ansi_parse_ansikey (ANSI * a, VT102 * v,TTY *t)
490 ANSI_Parser *p = &a->parser;
492 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
494 ansi_flush_escape (a, v,t);
497 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
499 vt102_send (v, KEY_180 + (p->escape_buf[2] - '0'),t);
503 ansi_flush_escape (a, v,t);
514 ansi_parse_escape (ANSI * a, VT102 * v,TTY *t)
516 ANSI_Parser *p = &a->parser;
517 switch (p->escape_ptr)
523 switch (p->escape_buf[1])
529 ansi_flush_escape (a, v,t);
533 switch (p->escape_buf[1])
536 ansi_parse_deckey (a, v,t);
539 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
540 ansi_parse_deckey (a, v,t);
543 ansi_flush_escape (a, v,t);
547 switch (p->escape_buf[1])
550 ansi_parse_ansikey (a, v,t);
553 ansi_flush_escape (a, v,t);
557 ansi_flush_escape (a, v,t);
563 ansi_check_escape (ANSI * a, VT102 * v,TTY *t)
565 ANSI_Parser *p = &a->parser;
566 struct timeval now, diff;
567 gettimeofday (&now, NULL);
568 timersub (&now, &p->last_escape, &diff);
571 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
572 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
580 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
581 ansi_flush_escape (a, v,t);
587 ansi_parse_char (ANSI * a, int c, VT102 * v,TTY *t)
589 ANSI_Parser *p = &a->parser;
592 /*See if it's time to flush the escape*/
593 ansi_check_escape (a, v,t);
598 ansi_flush_escape (a, v,t);
602 gettimeofday (&p->last_escape, NULL);
607 p->escape_buf[p->escape_ptr++] = c;
608 ansi_parse_escape (a, v,t);
618 ansi_parse (ANSI * a, char *buf, int len, VT102 * v,TTY *t)
621 ansi_parse_char (a, *(buf++), v,t);
625 ansi_dispatch (ANSI * a, VT102 * v,TTY *t)
630 ansi_check_escape (a, v,t);
633 red = ansi_read (a, buf, sizeof (buf));
648 ansi_parse (a, buf, red, v,t);