4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.13 2008/02/07 12:16:04 james
14 * *** empty log message ***
16 * Revision 1.12 2008/02/07 11:32:41 james
17 * *** empty log message ***
19 * Revision 1.11 2008/02/07 11:11:14 staffcvs
20 * *** empty log message ***
22 * Revision 1.10 2008/02/07 01:02:52 james
23 * *** empty log message ***
25 * Revision 1.9 2008/02/07 00:43:27 james
26 * *** empty log message ***
28 * Revision 1.8 2008/02/07 00:39:13 james
29 * *** empty log message ***
31 * Revision 1.7 2008/02/06 20:26:57 james
32 * *** empty log message ***
34 * Revision 1.6 2008/02/06 17:53:28 james
35 * *** empty log message ***
37 * Revision 1.5 2008/02/06 15:53:22 james
38 * *** empty log message ***
40 * Revision 1.4 2008/02/04 20:23:55 james
41 * *** empty log message ***
43 * Revision 1.3 2008/02/04 05:45:55 james
46 * Revision 1.2 2008/02/04 02:05:06 james
47 * *** empty log message ***
49 * Revision 1.1 2008/02/03 23:31:25 james
50 * *** empty log message ***
56 set_nonblocking (int fd)
59 arg = fcntl (fd, F_GETFL, arg);
61 fcntl (fd, F_SETFL, arg);
68 arg = fcntl (fd, F_GETFL, arg);
70 fcntl (fd, F_SETFL, arg);
75 ansi_read (ANSI * a, void *buf, int n)
79 set_nonblocking (a->fd);
80 red = read (a->fd, buf, n);
85 if ((red == -1) && (errno == EAGAIN))
94 ansi_write (ANSI * a, char *buf, int n)
97 write (a->fd, buf, n);
101 ansi_getsize (ANSI * a)
103 struct winsize sz = { 0 };
104 if (ioctl (a->fd, TIOCGWINSZ, &sz))
106 a->size.x = CRT_COLS;
107 a->size.y = CRT_ROWS;
111 a->size.x = sz.ws_col;
112 a->size.y = sz.ws_row;
119 ansi_move (ANSI * a, CRT_Pos p)
123 int dx = p.x - a->pos.x;
124 int dy = p.y - a->pos.y;
126 // a->pos.x = ANSI_INVAL;
128 if (a->pos.x != ANSI_INVAL)
138 ansi_write (a, "\033[C", 3);
142 ansi_write (a, "\033[D", 3);
146 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
147 ansi_write (a, buf, n);
154 ansi_write (a, "\033[A", 3);
158 ansi_write (a, "\033[B", 3);
162 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
163 ansi_write (a, buf, n);
167 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
168 ansi_write (a, buf, n);
175 ansi_write (a, "\033[E", 3);
179 ansi_write (a, "\033[F", 3);
183 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
184 ansi_write (a, buf, n);
188 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
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);
200 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
201 ansi_write (a, buf, n);
209 ansi_showhide_cursor (ANSI * a, int hide)
211 if (a->hide_cursor == hide)
216 ansi_write (a, "\033[?25l", 6);
220 ansi_write (a, "\033[?25h", 6);
223 a->hide_cursor = hide;
228 ansi_force_attr_normal (ANSI * a)
230 ansi_write (a, "\033[0m", 4);
231 a->attr = CRT_ATTR_NORMAL;
232 a->color = ANSI_INVAL;
236 ansi_set_color (ANSI * a, int color)
243 if ((a->color == ANSI_INVAL) || (color!=a->color)) {
244 fg=CRT_COLOR_FG(color);
245 bg=CRT_COLOR_BG(color);
247 if (fg & CRT_COLOR_INTENSITY) {
253 if (bg & CRT_COLOR_INTENSITY) {
259 i=sprintf(buf,"\033[%d;%dm",fg,bg);
260 fprintf(stderr,"Color set to %d %d %x\n",fg,bg,color);
262 ansi_write (a,buf,i);
268 ansi_set_attr (ANSI * a, int attr)
272 dif = attr ^ a->attr;
280 if (attr == CRT_ATTR_NORMAL)
282 ansi_force_attr_normal (a);
287 if (dif & CRT_ATTR_UNDERLINE)
289 if (attr & CRT_ATTR_UNDERLINE)
291 ansi_write (a, "\033[4m", 4);
295 ansi_write (a, "\033[24m", 5);
298 if (dif & CRT_ATTR_REVERSE)
300 if (attr & CRT_ATTR_REVERSE)
302 ansi_write (a, "\033[7m", 4);
306 ansi_write (a, "\033[27m", 5);
309 if (dif & CRT_ATTR_BOLD)
311 if (attr & CRT_ATTR_BOLD)
313 ansi_write (a, "\033[1m", 4);
317 ansi_write (a, "\033[21m", 5);
318 ansi_write (a, "\033[22m", 5);
326 ansi_render (ANSI * a, CRT_CA ca)
335 ansi_set_attr (a, ca.attr);
336 ansi_set_color(a,ca.color);
338 ansi_write (a, &ca.chr, 1);
342 /*Can't easily wrap round here as don't know size of destination screen*/
343 /*so invalidate the cached cursor position*/
345 if (a->pos.x >= CRT_COLS)
346 a->pos.x = ANSI_INVAL;
357 ansi_force_attr_normal (a);
358 ansi_set_color(a,CRT_COLOR_NORMAL);
360 ansi_write (a, "\033[2J", 4);
361 /*different emulators leave cursor in different places after cls differently*/
362 a->pos.x = ANSI_INVAL;
367 ansi_draw (ANSI * a, CRT * c)
371 int hidden_cursor = 0;
373 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
375 if (p.y >= a->size.y)
377 o = CRT_ADDR (p.y, 0);
378 for (p.x = 0; p.x < CRT_COLS; ++p.x, ++o)
380 if (p.x >= a->size.x)
382 if (crt_ca_cmp (a->crt.screen[o], c->screen[o]))
384 ansi_showhide_cursor (a, 1);
385 a->crt.screen[o] = c->screen[o];
388 ansi_render (a, a->crt.screen[o]);
394 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
396 char msg[] = "Window is too small";
400 ansi_showhide_cursor (a, 1);
401 ansi_set_attr (a, CRT_ATTR_REVERSE);
402 ansi_set_color(a,CRT_MAKE_COLOR(CRT_COLOR_WHITE,CRT_COLOR_RED));
405 ansi_write (a, msg, sizeof (msg));
406 a->pos.x = ANSI_INVAL;
410 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
412 ansi_showhide_cursor (a, 1);
417 ansi_move (a, a->crt.pos);
419 a->crt.hide_cursor = c->hide_cursor;
420 ansi_showhide_cursor (a, a->crt.hide_cursor);
424 ansi_reset (ANSI * a)
426 // FIXME: -- echos back crap?
427 // ansi_write (a, "\033[c", 3);
430 a->pos.x = ANSI_INVAL;
431 a->hide_cursor = ANSI_INVAL;
436 ansi_write (a, "\033=", 2);
437 ansi_draw (a, &a->crt);
441 ansi_flush_escape (ANSI * a, VT102 * v,TTY *t)
443 ANSI_Parser *p = &a->parser;
446 for (i = 0; i < p->escape_ptr; ++i)
448 vt102_send (v, p->escape_buf[i],t);
456 ansi_parse_deckey (ANSI * a, VT102 * v,TTY *t)
458 ANSI_Parser *p = &a->parser;
459 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
461 ansi_flush_escape (a, v,t);
465 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
467 vt102_send (v, KEY_UP + (p->escape_buf[2] - 'A'),t);
469 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
471 vt102_send (v, KEY_154 + (p->escape_buf[2] - 'a'),t);
475 ansi_flush_escape (a, v,t);
483 ansi_parse_ansikey (ANSI * a, VT102 * v,TTY *t)
485 ANSI_Parser *p = &a->parser;
487 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
489 ansi_flush_escape (a, v,t);
492 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
494 vt102_send (v, KEY_180 + (p->escape_buf[2] - '0'),t);
498 ansi_flush_escape (a, v,t);
509 ansi_parse_escape (ANSI * a, VT102 * v,TTY *t)
511 ANSI_Parser *p = &a->parser;
512 switch (p->escape_ptr)
518 switch (p->escape_buf[1])
524 ansi_flush_escape (a, v,t);
528 switch (p->escape_buf[1])
531 ansi_parse_deckey (a, v,t);
534 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
535 ansi_parse_deckey (a, v,t);
538 ansi_flush_escape (a, v,t);
542 switch (p->escape_buf[1])
545 ansi_parse_ansikey (a, v,t);
548 ansi_flush_escape (a, v,t);
552 ansi_flush_escape (a, v,t);
558 ansi_check_escape (ANSI * a, VT102 * v,TTY *t)
560 ANSI_Parser *p = &a->parser;
561 struct timeval now, diff;
562 gettimeofday (&now, NULL);
563 timersub (&now, &p->last_escape, &diff);
566 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
567 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
575 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
576 ansi_flush_escape (a, v,t);
582 ansi_parse_char (ANSI * a, int c, VT102 * v,TTY *t)
584 ANSI_Parser *p = &a->parser;
587 /*See if it's time to flush the escape*/
588 ansi_check_escape (a, v,t);
593 ansi_flush_escape (a, v,t);
597 gettimeofday (&p->last_escape, NULL);
602 p->escape_buf[p->escape_ptr++] = c;
603 ansi_parse_escape (a, v,t);
613 ansi_parse (ANSI * a, char *buf, int len, VT102 * v,TTY *t)
616 ansi_parse_char (a, *(buf++), v,t);
620 ansi_dispatch (ANSI * a, VT102 * v,TTY *t)
625 ansi_check_escape (a, v,t);
628 red = ansi_read (a, buf, sizeof (buf));
643 ansi_parse (a, buf, red, v,t);