4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.11 2008/02/07 11:11:14 staffcvs
14 * *** empty log message ***
16 * Revision 1.10 2008/02/07 01:02:52 james
17 * *** empty log message ***
19 * Revision 1.9 2008/02/07 00:43:27 james
20 * *** empty log message ***
22 * Revision 1.8 2008/02/07 00:39:13 james
23 * *** empty log message ***
25 * Revision 1.7 2008/02/06 20:26:57 james
26 * *** empty log message ***
28 * Revision 1.6 2008/02/06 17:53:28 james
29 * *** empty log message ***
31 * Revision 1.5 2008/02/06 15:53:22 james
32 * *** empty log message ***
34 * Revision 1.4 2008/02/04 20:23:55 james
35 * *** empty log message ***
37 * Revision 1.3 2008/02/04 05:45:55 james
40 * Revision 1.2 2008/02/04 02:05:06 james
41 * *** empty log message ***
43 * Revision 1.1 2008/02/03 23:31:25 james
44 * *** empty log message ***
50 set_nonblocking (int fd)
53 arg = fcntl (fd, F_GETFL, arg);
55 fcntl (fd, F_SETFL, arg);
62 arg = fcntl (fd, F_GETFL, arg);
64 fcntl (fd, F_SETFL, arg);
69 ansi_read (ANSI * a, void *buf, int n)
73 set_nonblocking (a->fd);
74 red = read (a->fd, buf, n);
79 if ((red == -1) && (errno == EAGAIN))
88 ansi_write (ANSI * a, char *buf, int n)
91 write (a->fd, buf, n);
95 ansi_getsize (ANSI * a)
97 struct winsize sz = { 0 };
98 if (ioctl (a->fd, TIOCGWINSZ, &sz))
100 a->size.x = CRT_COLS;
101 a->size.y = CRT_ROWS;
105 a->size.x = sz.ws_col;
106 a->size.y = sz.ws_row;
113 ansi_move (ANSI * a, CRT_Pos p)
117 int dx = p.x - a->pos.x;
118 int dy = p.y - a->pos.y;
120 // a->pos.x = ANSI_INVAL;
122 if (a->pos.x != ANSI_INVAL)
132 ansi_write (a, "\033[C", 3);
136 ansi_write (a, "\033[D", 3);
140 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
141 ansi_write (a, buf, n);
148 ansi_write (a, "\033[A", 3);
152 ansi_write (a, "\033[B", 3);
156 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
157 ansi_write (a, buf, n);
161 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
162 ansi_write (a, buf, n);
169 ansi_write (a, "\033[E", 3);
173 ansi_write (a, "\033[F", 3);
177 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
178 ansi_write (a, buf, n);
182 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
183 ansi_write (a, buf, n);
188 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
189 ansi_write (a, buf, n);
194 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
195 ansi_write (a, buf, n);
203 ansi_showhide_cursor (ANSI * a, int hide)
205 if (a->hide_cursor == hide)
210 ansi_write (a, "\033[?25l", 6);
214 ansi_write (a, "\033[?25h", 6);
217 a->hide_cursor = hide;
222 ansi_force_attr_normal (ANSI * a)
224 ansi_write (a, "\033[0m", 4);
225 a->attr = CRT_ATTR_NORMAL;
229 ansi_set_attr (ANSI * a, int attr)
233 dif = attr ^ a->attr;
240 if (attr == CRT_ATTR_NORMAL)
242 ansi_force_attr_normal (a);
246 if (dif & CRT_ATTR_UNDERLINE)
248 if (attr & CRT_ATTR_UNDERLINE)
250 ansi_write (a, "\033[4m", 4);
254 ansi_write (a, "\033[24m", 5);
257 if (dif & CRT_ATTR_REVERSE)
259 if (attr & CRT_ATTR_REVERSE)
261 ansi_write (a, "\033[7m", 4);
265 ansi_write (a, "\033[27m", 5);
268 if (dif & CRT_ATTR_BOLD)
270 if (attr & CRT_ATTR_BOLD)
272 ansi_write (a, "\033[1m", 4);
276 ansi_write (a, "\033[21m", 5);
277 ansi_write (a, "\033[22m", 5);
285 ansi_render (ANSI * a, CRT_CA ca)
294 ansi_set_attr (a, ca.attr);
296 ansi_write (a, &ca.chr, 1);
300 /*Can't easily wrap round here as don't know size of destination screen*/
301 /*so invalidate the cached cursor position*/
303 if (a->pos.x >= CRT_COLS)
304 a->pos.x = ANSI_INVAL;
315 ansi_force_attr_normal (a);
317 ansi_write (a, "\033[2J", 4);
318 /*different emulators leave cursor in different places after cls differently*/
319 a->pos.x = ANSI_INVAL;
324 ansi_draw (ANSI * a, CRT * c)
328 int hidden_cursor = 0;
330 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
332 if (p.y >= a->size.y)
334 o = CRT_ADDR (p.y, 0);
335 for (p.x = 0; p.x < CRT_COLS; ++p.x, ++o)
337 if (p.x >= a->size.x)
339 if (crt_ca_cmp (a->crt.screen[o], c->screen[o]))
341 ansi_showhide_cursor (a, 1);
342 a->crt.screen[o] = c->screen[o];
345 ansi_render (a, a->crt.screen[o]);
351 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
353 char msg[] = "Window is too small";
357 ansi_showhide_cursor (a, 1);
358 ansi_set_attr (a, CRT_ATTR_REVERSE);
361 ansi_write (a, msg, sizeof (msg));
362 a->pos.x = ANSI_INVAL;
366 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
368 ansi_showhide_cursor (a, 1);
373 ansi_move (a, a->crt.pos);
375 a->crt.hide_cursor = c->hide_cursor;
376 ansi_showhide_cursor (a, a->crt.hide_cursor);
380 ansi_reset (ANSI * a)
382 // FIXME: -- echos back crap?
383 // ansi_write (a, "\033[c", 3);
386 a->pos.x = ANSI_INVAL;
387 a->hide_cursor = ANSI_INVAL;
392 ansi_write (a, "\033=", 2);
393 ansi_draw (a, &a->crt);
397 ansi_flush_escape (ANSI * a, VT102 * v)
399 ANSI_Parser *p = &a->parser;
402 for (i = 0; i < p->escape_ptr; ++i)
404 vt102_send (v, p->escape_buf[i]);
412 ansi_parse_deckey (ANSI * a, VT102 * v)
414 ANSI_Parser *p = &a->parser;
415 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
417 ansi_flush_escape (a, v);
421 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
423 vt102_send (v, KEY_UP + (p->escape_buf[2] - 'A'));
425 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
427 vt102_send (v, KEY_154 + (p->escape_buf[2] - 'a'));
431 ansi_flush_escape (a, v);
439 ansi_parse_ansikey (ANSI * a, VT102 * v)
441 ANSI_Parser *p = &a->parser;
443 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
445 ansi_flush_escape (a, v);
448 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
450 vt102_send (v, KEY_180 + (p->escape_buf[2] - '0'));
454 ansi_flush_escape (a, v);
465 ansi_parse_escape (ANSI * a, VT102 * v)
467 ANSI_Parser *p = &a->parser;
468 switch (p->escape_ptr)
474 switch (p->escape_buf[1])
480 ansi_flush_escape (a, v);
484 switch (p->escape_buf[1])
487 ansi_parse_deckey (a, v);
490 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
491 ansi_parse_deckey (a, v);
494 ansi_flush_escape (a, v);
498 switch (p->escape_buf[1])
501 ansi_parse_ansikey (a, v);
504 ansi_flush_escape (a, v);
508 ansi_flush_escape (a, v);
514 ansi_check_escape (ANSI * a, VT102 * v)
516 ANSI_Parser *p = &a->parser;
517 struct timeval now, diff;
518 gettimeofday (&now, NULL);
519 timersub (&now, &p->last_escape, &diff);
522 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
523 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
531 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
532 ansi_flush_escape (a, v);
538 ansi_parse_char (ANSI * a, int c, VT102 * v)
540 ANSI_Parser *p = &a->parser;
543 /*See if it's time to flush the escape*/
544 ansi_check_escape (a, v);
549 ansi_flush_escape (a, v);
553 gettimeofday (&p->last_escape, NULL);
558 p->escape_buf[p->escape_ptr++] = c;
559 ansi_parse_escape (a, v);
569 ansi_parse (ANSI * a, char *buf, int len, VT102 * v)
572 ansi_parse_char (a, *(buf++), v);
576 ansi_dispatch (ANSI * a, VT102 * v)
581 ansi_check_escape (a, v);
584 red = ansi_read (a, buf, sizeof (buf));
599 ansi_parse (a, buf, red, v);