4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.12 2008/02/07 11:32:41 james
14 * *** empty log message ***
16 * Revision 1.11 2008/02/07 11:11:14 staffcvs
17 * *** empty log message ***
19 * Revision 1.10 2008/02/07 01:02:52 james
20 * *** empty log message ***
22 * Revision 1.9 2008/02/07 00:43:27 james
23 * *** empty log message ***
25 * Revision 1.8 2008/02/07 00:39:13 james
26 * *** empty log message ***
28 * Revision 1.7 2008/02/06 20:26:57 james
29 * *** empty log message ***
31 * Revision 1.6 2008/02/06 17:53:28 james
32 * *** empty log message ***
34 * Revision 1.5 2008/02/06 15:53:22 james
35 * *** empty log message ***
37 * Revision 1.4 2008/02/04 20:23:55 james
38 * *** empty log message ***
40 * Revision 1.3 2008/02/04 05:45:55 james
43 * Revision 1.2 2008/02/04 02:05:06 james
44 * *** empty log message ***
46 * Revision 1.1 2008/02/03 23:31:25 james
47 * *** empty log message ***
53 set_nonblocking (int fd)
56 arg = fcntl (fd, F_GETFL, arg);
58 fcntl (fd, F_SETFL, arg);
65 arg = fcntl (fd, F_GETFL, arg);
67 fcntl (fd, F_SETFL, arg);
72 ansi_read (ANSI * a, void *buf, int n)
76 set_nonblocking (a->fd);
77 red = read (a->fd, buf, n);
82 if ((red == -1) && (errno == EAGAIN))
91 ansi_write (ANSI * a, char *buf, int n)
94 write (a->fd, buf, n);
98 ansi_getsize (ANSI * a)
100 struct winsize sz = { 0 };
101 if (ioctl (a->fd, TIOCGWINSZ, &sz))
103 a->size.x = CRT_COLS;
104 a->size.y = CRT_ROWS;
108 a->size.x = sz.ws_col;
109 a->size.y = sz.ws_row;
116 ansi_move (ANSI * a, CRT_Pos p)
120 int dx = p.x - a->pos.x;
121 int dy = p.y - a->pos.y;
123 // a->pos.x = ANSI_INVAL;
125 if (a->pos.x != ANSI_INVAL)
135 ansi_write (a, "\033[C", 3);
139 ansi_write (a, "\033[D", 3);
143 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
144 ansi_write (a, buf, n);
151 ansi_write (a, "\033[A", 3);
155 ansi_write (a, "\033[B", 3);
159 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
160 ansi_write (a, buf, n);
164 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
165 ansi_write (a, buf, n);
172 ansi_write (a, "\033[E", 3);
176 ansi_write (a, "\033[F", 3);
180 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
181 ansi_write (a, buf, n);
185 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
186 ansi_write (a, buf, n);
191 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
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);
206 ansi_showhide_cursor (ANSI * a, int hide)
208 if (a->hide_cursor == hide)
213 ansi_write (a, "\033[?25l", 6);
217 ansi_write (a, "\033[?25h", 6);
220 a->hide_cursor = hide;
225 ansi_force_attr_normal (ANSI * a)
227 ansi_write (a, "\033[0m", 4);
228 a->attr = CRT_ATTR_NORMAL;
232 ansi_set_attr (ANSI * a, int attr)
236 dif = attr ^ a->attr;
243 if (attr == CRT_ATTR_NORMAL)
245 ansi_force_attr_normal (a);
249 if (dif & CRT_ATTR_UNDERLINE)
251 if (attr & CRT_ATTR_UNDERLINE)
253 ansi_write (a, "\033[4m", 4);
257 ansi_write (a, "\033[24m", 5);
260 if (dif & CRT_ATTR_REVERSE)
262 if (attr & CRT_ATTR_REVERSE)
264 ansi_write (a, "\033[7m", 4);
268 ansi_write (a, "\033[27m", 5);
271 if (dif & CRT_ATTR_BOLD)
273 if (attr & CRT_ATTR_BOLD)
275 ansi_write (a, "\033[1m", 4);
279 ansi_write (a, "\033[21m", 5);
280 ansi_write (a, "\033[22m", 5);
288 ansi_render (ANSI * a, CRT_CA ca)
297 ansi_set_attr (a, ca.attr);
299 ansi_write (a, &ca.chr, 1);
303 /*Can't easily wrap round here as don't know size of destination screen*/
304 /*so invalidate the cached cursor position*/
306 if (a->pos.x >= CRT_COLS)
307 a->pos.x = ANSI_INVAL;
318 ansi_force_attr_normal (a);
320 ansi_write (a, "\033[2J", 4);
321 /*different emulators leave cursor in different places after cls differently*/
322 a->pos.x = ANSI_INVAL;
327 ansi_draw (ANSI * a, CRT * c)
331 int hidden_cursor = 0;
333 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
335 if (p.y >= a->size.y)
337 o = CRT_ADDR (p.y, 0);
338 for (p.x = 0; p.x < CRT_COLS; ++p.x, ++o)
340 if (p.x >= a->size.x)
342 if (crt_ca_cmp (a->crt.screen[o], c->screen[o]))
344 ansi_showhide_cursor (a, 1);
345 a->crt.screen[o] = c->screen[o];
348 ansi_render (a, a->crt.screen[o]);
354 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
356 char msg[] = "Window is too small";
360 ansi_showhide_cursor (a, 1);
361 ansi_set_attr (a, CRT_ATTR_REVERSE);
364 ansi_write (a, msg, sizeof (msg));
365 a->pos.x = ANSI_INVAL;
369 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
371 ansi_showhide_cursor (a, 1);
376 ansi_move (a, a->crt.pos);
378 a->crt.hide_cursor = c->hide_cursor;
379 ansi_showhide_cursor (a, a->crt.hide_cursor);
383 ansi_reset (ANSI * a)
385 // FIXME: -- echos back crap?
386 // ansi_write (a, "\033[c", 3);
389 a->pos.x = ANSI_INVAL;
390 a->hide_cursor = ANSI_INVAL;
395 ansi_write (a, "\033=", 2);
396 ansi_draw (a, &a->crt);
400 ansi_flush_escape (ANSI * a, VT102 * v,TTY *t)
402 ANSI_Parser *p = &a->parser;
405 for (i = 0; i < p->escape_ptr; ++i)
407 vt102_send (v, p->escape_buf[i],t);
415 ansi_parse_deckey (ANSI * a, VT102 * v,TTY *t)
417 ANSI_Parser *p = &a->parser;
418 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
420 ansi_flush_escape (a, v,t);
424 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
426 vt102_send (v, KEY_UP + (p->escape_buf[2] - 'A'),t);
428 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
430 vt102_send (v, KEY_154 + (p->escape_buf[2] - 'a'),t);
434 ansi_flush_escape (a, v,t);
442 ansi_parse_ansikey (ANSI * a, VT102 * v,TTY *t)
444 ANSI_Parser *p = &a->parser;
446 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
448 ansi_flush_escape (a, v,t);
451 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
453 vt102_send (v, KEY_180 + (p->escape_buf[2] - '0'),t);
457 ansi_flush_escape (a, v,t);
468 ansi_parse_escape (ANSI * a, VT102 * v,TTY *t)
470 ANSI_Parser *p = &a->parser;
471 switch (p->escape_ptr)
477 switch (p->escape_buf[1])
483 ansi_flush_escape (a, v,t);
487 switch (p->escape_buf[1])
490 ansi_parse_deckey (a, v,t);
493 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
494 ansi_parse_deckey (a, v,t);
497 ansi_flush_escape (a, v,t);
501 switch (p->escape_buf[1])
504 ansi_parse_ansikey (a, v,t);
507 ansi_flush_escape (a, v,t);
511 ansi_flush_escape (a, v,t);
517 ansi_check_escape (ANSI * a, VT102 * v,TTY *t)
519 ANSI_Parser *p = &a->parser;
520 struct timeval now, diff;
521 gettimeofday (&now, NULL);
522 timersub (&now, &p->last_escape, &diff);
525 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
526 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
534 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
535 ansi_flush_escape (a, v,t);
541 ansi_parse_char (ANSI * a, int c, VT102 * v,TTY *t)
543 ANSI_Parser *p = &a->parser;
546 /*See if it's time to flush the escape*/
547 ansi_check_escape (a, v,t);
552 ansi_flush_escape (a, v,t);
556 gettimeofday (&p->last_escape, NULL);
561 p->escape_buf[p->escape_ptr++] = c;
562 ansi_parse_escape (a, v,t);
572 ansi_parse (ANSI * a, char *buf, int len, VT102 * v,TTY *t)
575 ansi_parse_char (a, *(buf++), v,t);
579 ansi_dispatch (ANSI * a, VT102 * v,TTY *t)
584 ansi_check_escape (a, v,t);
587 red = ansi_read (a, buf, sizeof (buf));
602 ansi_parse (a, buf, red, v,t);