4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.15 2008/02/07 13:19:48 james
14 * *** empty log message ***
16 * Revision 1.14 2008/02/07 12:21:16 james
17 * *** empty log message ***
19 * Revision 1.13 2008/02/07 12:16:04 james
20 * *** empty log message ***
22 * Revision 1.12 2008/02/07 11:32:41 james
23 * *** empty log message ***
25 * Revision 1.11 2008/02/07 11:11:14 staffcvs
26 * *** empty log message ***
28 * Revision 1.10 2008/02/07 01:02:52 james
29 * *** empty log message ***
31 * Revision 1.9 2008/02/07 00:43:27 james
32 * *** empty log message ***
34 * Revision 1.8 2008/02/07 00:39:13 james
35 * *** empty log message ***
37 * Revision 1.7 2008/02/06 20:26:57 james
38 * *** empty log message ***
40 * Revision 1.6 2008/02/06 17:53:28 james
41 * *** empty log message ***
43 * Revision 1.5 2008/02/06 15:53:22 james
44 * *** empty log message ***
46 * Revision 1.4 2008/02/04 20:23:55 james
47 * *** empty log message ***
49 * Revision 1.3 2008/02/04 05:45:55 james
52 * Revision 1.2 2008/02/04 02:05:06 james
53 * *** empty log message ***
55 * Revision 1.1 2008/02/03 23:31:25 james
56 * *** empty log message ***
62 set_nonblocking (int fd)
65 arg = fcntl (fd, F_GETFL, arg);
67 fcntl (fd, F_SETFL, arg);
74 arg = fcntl (fd, F_GETFL, arg);
76 fcntl (fd, F_SETFL, arg);
81 ansi_read (ANSI * a, void *buf, int n)
85 set_nonblocking (a->fd);
86 red = read (a->fd, buf, n);
91 if ((red == -1) && (errno == EAGAIN))
100 ansi_write (ANSI * a, char *buf, int n)
102 set_blocking (a->fd);
103 write (a->fd, buf, n);
107 ansi_getsize (ANSI * a)
109 struct winsize sz = { 0 };
110 if (ioctl (a->fd, TIOCGWINSZ, &sz))
112 a->size.x = CRT_COLS;
113 a->size.y = CRT_ROWS;
117 a->size.x = sz.ws_col;
118 a->size.y = sz.ws_row;
125 ansi_move (ANSI * a, CRT_Pos p)
129 int dx = p.x - a->pos.x;
130 int dy = p.y - a->pos.y;
132 // a->pos.x = ANSI_INVAL;
134 if (a->pos.x != ANSI_INVAL)
144 ansi_write (a, "\033[C", 3);
148 ansi_write (a, "\033[D", 3);
152 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
153 ansi_write (a, buf, n);
160 ansi_write (a, "\033[A", 3);
164 ansi_write (a, "\033[B", 3);
168 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
169 ansi_write (a, buf, n);
173 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
174 ansi_write (a, buf, n);
181 ansi_write (a, "\033[E", 3);
185 ansi_write (a, "\033[F", 3);
189 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
190 ansi_write (a, buf, n);
194 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
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);
206 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
207 ansi_write (a, buf, n);
215 ansi_showhide_cursor (ANSI * a, int hide)
217 if (a->hide_cursor == hide)
222 ansi_write (a, "\033[?25l", 6);
226 ansi_write (a, "\033[?25h", 6);
229 a->hide_cursor = hide;
234 ansi_force_attr_normal (ANSI * a)
236 ansi_write (a, "\033[0m", 4);
237 a->attr = CRT_ATTR_NORMAL;
238 a->color = ANSI_INVAL;
242 ansi_set_color (ANSI * a, int color)
249 if ((a->color == ANSI_INVAL) || (color!=a->color)) {
250 fg=CRT_COLOR_FG(color);
251 bg=CRT_COLOR_BG(color);
253 if (fg & CRT_COLOR_INTENSITY) {
259 if (bg & CRT_COLOR_INTENSITY) {
265 i=sprintf(buf,"\033[%d;%dm",fg,bg);
267 fprintf(stderr,"Color set to %d %d %x\n",fg,bg,color);
270 ansi_write (a,buf,i);
276 ansi_set_attr (ANSI * a, int attr)
280 dif = attr ^ a->attr;
288 if (attr == CRT_ATTR_NORMAL)
290 ansi_force_attr_normal (a);
295 if (dif & CRT_ATTR_UNDERLINE)
297 if (attr & CRT_ATTR_UNDERLINE)
299 ansi_write (a, "\033[4m", 4);
303 ansi_write (a, "\033[24m", 5);
306 if (dif & CRT_ATTR_REVERSE)
308 if (attr & CRT_ATTR_REVERSE)
310 ansi_write (a, "\033[7m", 4);
314 ansi_write (a, "\033[27m", 5);
317 if (dif & CRT_ATTR_BOLD)
319 if (attr & CRT_ATTR_BOLD)
321 ansi_write (a, "\033[1m", 4);
325 ansi_write (a, "\033[21m", 5);
326 ansi_write (a, "\033[22m", 5);
334 ansi_render (ANSI * a, CRT_CA ca)
343 ansi_set_attr (a, ca.attr);
344 ansi_set_color(a,ca.color);
346 ansi_write (a, &ca.chr, 1);
350 /*Can't easily wrap round here as don't know size of destination screen*/
351 /*so invalidate the cached cursor position*/
353 if (a->pos.x >= a->size.x)
354 a->pos.x = ANSI_INVAL;
365 ansi_force_attr_normal (a);
366 ansi_set_color(a,CRT_COLOR_NORMAL);
368 ansi_write (a, "\033[2J", 4);
369 /*different emulators leave cursor in different places after cls differently*/
370 a->pos.x = ANSI_INVAL;
373 int ansi_scroll_up(ANSI *a,CRT_Pos s,CRT_Pos e)
378 if (e.x!=(CRT_COLS-1)) return -1;
380 if (s.y>=a->size.y) return -1;
382 ansi_showhide_cursor (a, 1);
383 i=sprintf(buf,"\033[%d;%dr",s.y+1,e.y+1);
385 i=sprintf(buf,"\033[%d;%dH",e.y+1,0);
387 ansi_write (a, "\033D",2);
388 ansi_write(a,"\033[r", 3);
396 void ansi_spot_scroll_up(ANSI * a, CRT * c)
400 l = c->sh.e.x - c->sh.s.x;
402 l *= sizeof (CRT_CA);
404 n = c->sh.e.y - c->sh.s.y;
405 p = CRT_ADDR_POS (&c->sh.s);
409 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
414 if (ansi_scroll_up(a,c->sh.s,c->sh.e)) return;
416 n = c->sh.e.y - c->sh.s.y;
417 p = CRT_ADDR_POS (&c->sh.s);
421 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
425 c->sh.dir=0; //FIXME: horrid hack
429 void ansi_spot_scroll(ANSI * a, CRT * c)
434 /*we only care about up for the moment */
435 ansi_spot_scroll_up(a,c);
446 ansi_draw (ANSI * a, CRT * c)
450 int hidden_cursor = 0;
452 ansi_spot_scroll(a,c);
454 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
456 if (p.y >= a->size.y)
458 o = CRT_ADDR (p.y, 0);
459 for (p.x = 0; p.x < CRT_COLS; ++p.x, ++o)
461 if (p.x >= a->size.x)
463 if (crt_ca_cmp (a->crt.screen[o], c->screen[o]))
465 ansi_showhide_cursor (a, 1);
466 a->crt.screen[o] = c->screen[o];
469 ansi_render (a, a->crt.screen[o]);
475 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
477 char msg[] = "Window is too small";
481 ansi_showhide_cursor (a, 1);
482 ansi_set_attr (a, CRT_ATTR_REVERSE);
483 ansi_set_color(a,CRT_MAKE_COLOR(CRT_COLOR_WHITE,CRT_COLOR_RED));
486 ansi_write (a, msg, sizeof (msg));
487 a->pos.x = ANSI_INVAL;
491 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
493 ansi_showhide_cursor (a, 1);
498 ansi_move (a, a->crt.pos);
500 a->crt.hide_cursor = c->hide_cursor;
501 ansi_showhide_cursor (a, a->crt.hide_cursor);
505 ansi_reset (ANSI * a)
507 // FIXME: -- echos back crap?
508 // ansi_write (a, "\033[c", 3);
511 a->pos.x = ANSI_INVAL;
512 a->hide_cursor = ANSI_INVAL;
517 ansi_write (a, "\033=", 2);
518 ansi_write (a, "\033[?6l", 5);
519 ansi_write (a, "\033[r", 3);
520 ansi_draw (a, &a->crt);
524 ansi_flush_escape (ANSI * a, VT102 * v,TTY *t)
526 ANSI_Parser *p = &a->parser;
529 for (i = 0; i < p->escape_ptr; ++i)
531 vt102_send (v, p->escape_buf[i],t);
539 ansi_parse_deckey (ANSI * a, VT102 * v,TTY *t)
541 ANSI_Parser *p = &a->parser;
542 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
544 ansi_flush_escape (a, v,t);
548 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
550 vt102_send (v, KEY_UP + (p->escape_buf[2] - 'A'),t);
552 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
554 vt102_send (v, KEY_154 + (p->escape_buf[2] - 'a'),t);
558 ansi_flush_escape (a, v,t);
566 ansi_parse_ansikey (ANSI * a, VT102 * v,TTY *t)
568 ANSI_Parser *p = &a->parser;
570 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
572 ansi_flush_escape (a, v,t);
575 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
577 vt102_send (v, KEY_180 + (p->escape_buf[2] - '0'),t);
581 ansi_flush_escape (a, v,t);
592 ansi_parse_escape (ANSI * a, VT102 * v,TTY *t)
594 ANSI_Parser *p = &a->parser;
595 switch (p->escape_ptr)
601 switch (p->escape_buf[1])
607 ansi_flush_escape (a, v,t);
611 switch (p->escape_buf[1])
614 ansi_parse_deckey (a, v,t);
617 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
618 ansi_parse_deckey (a, v,t);
621 ansi_flush_escape (a, v,t);
625 switch (p->escape_buf[1])
628 ansi_parse_ansikey (a, v,t);
631 ansi_flush_escape (a, v,t);
635 ansi_flush_escape (a, v,t);
641 ansi_check_escape (ANSI * a, VT102 * v,TTY *t)
643 ANSI_Parser *p = &a->parser;
644 struct timeval now, diff;
645 gettimeofday (&now, NULL);
646 timersub (&now, &p->last_escape, &diff);
649 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
650 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
658 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
659 ansi_flush_escape (a, v,t);
665 ansi_parse_char (ANSI * a, int c, VT102 * v,TTY *t)
667 ANSI_Parser *p = &a->parser;
670 /*See if it's time to flush the escape*/
671 ansi_check_escape (a, v,t);
676 ansi_flush_escape (a, v,t);
680 gettimeofday (&p->last_escape, NULL);
685 p->escape_buf[p->escape_ptr++] = c;
686 ansi_parse_escape (a, v,t);
696 ansi_parse (ANSI * a, char *buf, int len, VT102 * v,TTY *t)
699 ansi_parse_char (a, *(buf++), v,t);
703 ansi_dispatch (ANSI * a, VT102 * v,TTY *t)
708 ansi_check_escape (a, v,t);
711 red = ansi_read (a, buf, sizeof (buf));
726 ansi_parse (a, buf, red, v,t);