4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.10 2008/02/07 01:02:52 james
14 * *** empty log message ***
16 * Revision 1.9 2008/02/07 00:43:27 james
17 * *** empty log message ***
19 * Revision 1.8 2008/02/07 00:39:13 james
20 * *** empty log message ***
22 * Revision 1.7 2008/02/06 20:26:57 james
23 * *** empty log message ***
25 * Revision 1.6 2008/02/06 17:53:28 james
26 * *** empty log message ***
28 * Revision 1.5 2008/02/06 15:53:22 james
29 * *** empty log message ***
31 * Revision 1.4 2008/02/04 20:23:55 james
32 * *** empty log message ***
34 * Revision 1.3 2008/02/04 05:45:55 james
37 * Revision 1.2 2008/02/04 02:05:06 james
38 * *** empty log message ***
40 * Revision 1.1 2008/02/03 23:31:25 james
41 * *** empty log message ***
47 set_nonblocking (int fd)
50 arg = fcntl (fd, F_GETFL, arg);
52 fcntl (fd, F_SETFL, arg);
59 arg = fcntl (fd, F_GETFL, arg);
61 fcntl (fd, F_SETFL, arg);
66 ansi_read (ANSI * a, void *buf, int n)
70 set_nonblocking (a->fd);
71 red = read (a->fd, buf, n);
76 if ((red == -1) && (errno == EAGAIN))
85 ansi_write (ANSI * a, char *buf, int n)
88 write (a->fd, buf, n);
92 ansi_getsize (ANSI * a)
94 struct winsize sz = { 0 };
95 if (ioctl (a->fd, TIOCGWINSZ, &sz))
102 a->size.x = sz.ws_col;
103 a->size.y = sz.ws_row;
110 ansi_move (ANSI * a, CRT_Pos p)
114 int dx = p.x - a->pos.x;
115 int dy = p.y - a->pos.y;
117 // a->pos.x = ANSI_INVAL;
119 if (a->pos.x != ANSI_INVAL)
129 ansi_write (a, "\033[C", 3);
133 ansi_write (a, "\033[D", 3);
137 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
138 ansi_write (a, buf, n);
145 ansi_write (a, "\033[A", 3);
149 ansi_write (a, "\033[B", 3);
153 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
154 ansi_write (a, buf, n);
158 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
159 ansi_write (a, buf, n);
166 ansi_write (a, "\033[E", 3);
170 ansi_write (a, "\033[F", 3);
174 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
175 ansi_write (a, buf, n);
179 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
180 ansi_write (a, buf, n);
185 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
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);
200 ansi_showhide_cursor (ANSI * a, int hide)
202 if (a->hide_cursor == hide)
207 ansi_write (a, "\033[?25l", 6);
211 ansi_write (a, "\033[?25h", 6);
214 a->hide_cursor = hide;
219 ansi_force_attr_normal (ANSI * a)
221 ansi_write (a, "\033[0m", 4);
222 a->attr = CRT_ATTR_NORMAL;
226 ansi_set_attr (ANSI * a, int attr)
230 dif = attr ^ a->attr;
237 if (attr == CRT_ATTR_NORMAL)
239 ansi_force_attr_normal (a);
243 if (dif & CRT_ATTR_UNDERLINE)
245 if (attr & CRT_ATTR_UNDERLINE)
247 ansi_write (a, "\033[4m", 4);
251 ansi_write (a, "\033[24m", 5);
254 if (dif & CRT_ATTR_REVERSE)
256 if (attr & CRT_ATTR_REVERSE)
258 ansi_write (a, "\033[7m", 4);
262 ansi_write (a, "\033[27m", 5);
265 if (dif & CRT_ATTR_BOLD)
267 if (attr & CRT_ATTR_BOLD)
269 ansi_write (a, "\033[1m", 4);
273 ansi_write (a, "\033[21m", 5);
274 ansi_write (a, "\033[22m", 5);
282 ansi_render (ANSI * a, CRT_CA ca)
291 ansi_set_attr (a, ca.attr);
293 ansi_write (a, &ca.chr, 1);
297 /*Can't easily wrap round here as don't know size of destination screen*/
298 /*so invalidate the cached cursor position*/
300 if (a->pos.x >= CRT_COLS)
301 a->pos.x = ANSI_INVAL;
312 ansi_force_attr_normal (a);
314 ansi_write (a, "\033[2J", 4);
315 /*different emulators leave cursor in different places after cls differently*/
316 a->pos.x = ANSI_INVAL;
321 ansi_draw (ANSI * a, CRT * c)
325 int hidden_cursor = 0;
327 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
329 if (p.y >= a->size.y)
331 o = CRT_ADDR (p.y, 0);
332 for (p.x = 0; p.x < CRT_COLS; ++p.x, ++o)
334 if (p.x >= a->size.x)
336 if (crt_ca_cmp (a->crt.screen[o], c->screen[o]))
338 ansi_showhide_cursor (a, 1);
339 a->crt.screen[o] = c->screen[o];
342 ansi_render (a, a->crt.screen[o]);
348 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
350 char msg[] = "Window is too small";
354 ansi_showhide_cursor (a, 1);
355 ansi_set_attr (a, CRT_ATTR_REVERSE);
358 ansi_write (a, msg, sizeof (msg));
359 a->pos.x = ANSI_INVAL;
363 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
365 ansi_showhide_cursor (a, 1);
370 ansi_move (a, a->crt.pos);
372 a->crt.hide_cursor = c->hide_cursor;
373 ansi_showhide_cursor (a, a->crt.hide_cursor);
377 ansi_reset (ANSI * a)
379 // FIXME: -- echos back crap?
380 // ansi_write (a, "\033[c", 3);
383 a->pos.x = ANSI_INVAL;
384 a->hide_cursor = ANSI_INVAL;
389 ansi_write (a, "\033=", 2);
390 ansi_draw (a, &a->crt);
394 ansi_flush_escape (ANSI * a, VT102 * v)
396 ANSI_Parser *p = &a->parser;
399 for (i = 0; i < p->escape_ptr; ++i)
401 vt102_send (v, p->escape_buf[i]);
409 ansi_parse_deckey (ANSI * a, VT102 * v)
411 ANSI_Parser *p = &a->parser;
412 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
414 ansi_flush_escape (a, v);
418 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
420 vt102_send (v, KEY_UP + (p->escape_buf[2] - 'A'));
422 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
424 vt102_send (v, KEY_154 + (p->escape_buf[2] - 'a'));
428 ansi_flush_escape (a, v);
436 ansi_parse_ansikey (ANSI * a, VT102 * v)
438 ANSI_Parser *p = &a->parser;
440 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
442 ansi_flush_escape (a, v);
445 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
447 vt102_send (v, KEY_180 + (p->escape_buf[2] - '0'));
451 ansi_flush_escape (a, v);
462 ansi_parse_escape (ANSI * a, VT102 * v)
464 ANSI_Parser *p = &a->parser;
465 switch (p->escape_ptr)
471 switch (p->escape_buf[1])
477 ansi_flush_escape (a, v);
481 switch (p->escape_buf[1])
484 ansi_parse_deckey (a, v);
487 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
488 ansi_parse_deckey (a, v);
491 ansi_flush_escape (a, v);
495 switch (p->escape_buf[1])
498 ansi_parse_ansikey (a, v);
501 ansi_flush_escape (a, v);
505 ansi_flush_escape (a, v);
511 ansi_check_escape (ANSI * a, VT102 * v)
513 ANSI_Parser *p = &a->parser;
514 struct timeval now, diff;
515 gettimeofday (&now, NULL);
516 timersub (&now, &p->last_escape, &diff);
519 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
520 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
528 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
529 ansi_flush_escape (a, v);
535 ansi_parse_char (ANSI * a, int c, VT102 * v)
537 ANSI_Parser *p = &a->parser;
540 /*See if it's time to flush the escape*/
541 ansi_check_escape (a, v);
546 ansi_flush_escape (a, v);
550 gettimeofday (&p->last_escape, NULL);
555 p->escape_buf[p->escape_ptr++] = c;
556 ansi_parse_escape (a, v);
566 ansi_parse (ANSI * a, char *buf, int len, VT102 * v)
569 ansi_parse_char (a, *(buf++), v);
573 ansi_dispatch (ANSI * a, VT102 * v)
578 ansi_check_escape (a, v);
581 red = ansi_read (a, buf, sizeof (buf));
590 ansi_parse (a, buf, red, v);