4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.24 2008/02/20 19:36:06 james
16 * Revision 1.23 2008/02/20 19:25:09 james
17 * *** empty log message ***
19 * Revision 1.22 2008/02/15 03:32:07 james
20 * *** empty log message ***
22 * Revision 1.21 2008/02/14 02:46:44 james
23 * *** empty log message ***
25 * Revision 1.20 2008/02/14 01:55:57 james
26 * *** empty log message ***
28 * Revision 1.19 2008/02/13 16:57:29 james
29 * *** empty log message ***
31 * Revision 1.18 2008/02/13 09:12:21 james
32 * *** empty log message ***
34 * Revision 1.17 2008/02/13 01:08:18 james
35 * *** empty log message ***
37 * Revision 1.16 2008/02/07 13:22:51 james
38 * *** empty log message ***
40 * Revision 1.15 2008/02/07 13:19:48 james
41 * *** empty log message ***
43 * Revision 1.14 2008/02/07 12:21:16 james
44 * *** empty log message ***
46 * Revision 1.13 2008/02/07 12:16:04 james
47 * *** empty log message ***
49 * Revision 1.12 2008/02/07 11:32:41 james
50 * *** empty log message ***
52 * Revision 1.11 2008/02/07 11:11:14 staffcvs
53 * *** empty log message ***
55 * Revision 1.10 2008/02/07 01:02:52 james
56 * *** empty log message ***
58 * Revision 1.9 2008/02/07 00:43:27 james
59 * *** empty log message ***
61 * Revision 1.8 2008/02/07 00:39:13 james
62 * *** empty log message ***
64 * Revision 1.7 2008/02/06 20:26:57 james
65 * *** empty log message ***
67 * Revision 1.6 2008/02/06 17:53:28 james
68 * *** empty log message ***
70 * Revision 1.5 2008/02/06 15:53:22 james
71 * *** empty log message ***
73 * Revision 1.4 2008/02/04 20:23:55 james
74 * *** empty log message ***
76 * Revision 1.3 2008/02/04 05:45:55 james
79 * Revision 1.2 2008/02/04 02:05:06 james
80 * *** empty log message ***
82 * Revision 1.1 2008/02/03 23:31:25 james
83 * *** empty log message ***
90 ansi_move (ANSI * a, CRT_Pos p)
94 int dx = p.x - a->pos.x;
95 int dy = p.y - a->pos.y;
97 // a->pos.x = ANSI_INVAL;
99 if (a->pos.x != ANSI_INVAL)
109 a->terminal->xmit (a->terminal, "\033[C", 3);
113 a->terminal->xmit (a->terminal, "\033[D", 3);
117 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
118 a->terminal->xmit (a->terminal, buf, n);
125 a->terminal->xmit (a->terminal, "\033[A", 3);
129 a->terminal->xmit (a->terminal, "\033[B", 3);
133 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
134 a->terminal->xmit (a->terminal, buf, n);
138 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
139 a->terminal->xmit (a->terminal, buf, n);
146 a->terminal->xmit (a->terminal, "\033[E", 3);
150 a->terminal->xmit (a->terminal, "\033[F", 3);
154 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
155 a->terminal->xmit (a->terminal, buf, n);
159 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
160 a->terminal->xmit (a->terminal, buf, n);
165 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
166 a->terminal->xmit (a->terminal, buf, n);
171 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
172 a->terminal->xmit (a->terminal, buf, n);
180 ansi_showhide_cursor (ANSI * a, int hide)
182 if (a->hide_cursor == hide)
187 a->terminal->xmit (a->terminal, "\033[?25l", 6);
191 a->terminal->xmit (a->terminal, "\033[?25h", 6);
194 a->hide_cursor = hide;
199 ansi_force_attr_normal (ANSI * a)
201 a->terminal->xmit (a->terminal, "\033[0m", 4);
202 a->attr = CRT_ATTR_NORMAL;
203 a->color = ANSI_INVAL;
207 ansi_set_color (ANSI * a, int color)
214 if ((a->color == ANSI_INVAL) || (color != a->color))
216 fg = CRT_COLOR_FG (color);
217 bg = CRT_COLOR_BG (color);
219 if (fg & CRT_COLOR_INTENSITY)
228 if (bg & CRT_COLOR_INTENSITY)
237 i = sprintf (buf, "\033[%d;%dm", fg, bg);
239 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
242 a->terminal->xmit (a->terminal, buf, i);
248 ansi_set_attr (ANSI * a, int attr)
252 dif = attr ^ a->attr;
260 if (attr == CRT_ATTR_NORMAL)
262 ansi_force_attr_normal (a);
267 if (dif & CRT_ATTR_UNDERLINE)
269 if (attr & CRT_ATTR_UNDERLINE)
271 a->terminal->xmit (a->terminal, "\033[4m", 4);
275 a->terminal->xmit (a->terminal, "\033[24m", 5);
278 if (dif & CRT_ATTR_REVERSE)
280 if (attr & CRT_ATTR_REVERSE)
282 a->terminal->xmit (a->terminal, "\033[7m", 4);
286 a->terminal->xmit (a->terminal, "\033[27m", 5);
289 if (dif & CRT_ATTR_BOLD)
291 if (attr & CRT_ATTR_BOLD)
293 a->terminal->xmit (a->terminal, "\033[1m", 4);
297 a->terminal->xmit (a->terminal, "\033[21m", 5);
298 a->terminal->xmit (a->terminal, "\033[22m", 5);
306 ansi_render (ANSI * a, CRT_CA ca)
315 ansi_set_attr (a, ca.attr);
316 ansi_set_color (a, ca.color);
318 a->terminal->xmit (a->terminal, &ca.chr, 1);
322 /*Can't easily wrap round here as don't know size of destination screen*/
323 /*so invalidate the cached cursor position*/
325 if (a->pos.x >= a->size.x)
326 a->pos.x = ANSI_INVAL;
337 ansi_set_attr (a, CRT_ATTR_NORMAL);
338 ansi_set_color (a, CRT_COLOR_NORMAL);
340 a->terminal->xmit (a->terminal, "\033[2J", 4);
341 /*different emulators leave cursor in different places after cls differently*/
342 a->pos.x = ANSI_INVAL;
347 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
353 if (e.x != (CRT_COLS - 1))
357 if (s.y >= a->size.y)
360 ansi_showhide_cursor (a, 1);
361 i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
362 a->terminal->xmit (a->terminal, buf, i);
363 i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
364 a->terminal->xmit (a->terminal, buf, i);
365 a->terminal->xmit (a->terminal, "\033D", 2);
366 a->terminal->xmit (a->terminal, "\033[r", 3);
369 crt_erase (&a->crt, s, e, 1);
371 a->pos.x = ANSI_INVAL;
378 ansi_spot_scroll_up (ANSI * a, CRT * c)
382 l = c->sh.e.x - c->sh.s.x;
384 l *= sizeof (CRT_CA);
386 n = c->sh.e.y - c->sh.s.y;
387 p = CRT_ADDR_POS (&c->sh.s);
391 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
396 if (ansi_scroll_up (a, c->sh.s, c->sh.e))
399 n = c->sh.e.y - c->sh.s.y;
400 p = CRT_ADDR_POS (&c->sh.s);
404 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
408 c->sh.dir = 0; //FIXME: horrid hack
413 ansi_spot_scroll (ANSI * a, CRT * c)
419 /*we only care about up for the moment */
420 ansi_spot_scroll_up (a, c);
431 ansi_draw_line (ANSI * a, CRT_CA * cap, int y)
433 CRT_Pos p = { 0, y };
434 CRT_CA *acap = &a->crt.screen[CRT_ADDR_POS (&p)];
436 for (p.x = 0; p.x < CRT_COLS; ++p.x)
438 if (p.x >= a->size.x)
441 if (crt_ca_cmp (*acap, *cap))
443 ansi_showhide_cursor (a, 1);
447 ansi_render (a, *acap);
456 ansi_resize_check (ANSI * a)
459 if (!crt_pos_cmp (a->terminal->size, a->size))
462 terminal_getsize (a->terminal);
464 a->size = a->terminal->size;
466 a->pos.x = ANSI_INVAL;
467 a->hide_cursor = ANSI_INVAL;
471 // FIXME: -- echos back crap?
472 // a->terminal->xmit (a->terminal, "\033[c", 3);
475 a->terminal->xmit (a->terminal, "\033=", 2);
476 a->terminal->xmit (a->terminal, "\033[?6l", 5);
477 a->terminal->xmit (a->terminal, "\033[r", 3);
481 #define HISTORY_GUESS_SCROLL 24 /*guess all 24 lines usually scroll */
482 /*if they haven't then ansi_draw will patch it up*/
485 ansi_history (ANSI * a, History * h)
489 /*Do we need to catch up on history?*/
491 if (a->history_ptr == h->wptr)
493 ansi_resize_check (a);
495 if ((a->size.x < CRT_COLS) || (a->size.y < CRT_ROWS))
498 ansi_force_attr_normal (a);
499 ansi_set_color (a, CRT_COLOR_NORMAL);
501 i = sprintf (buf, "\033[%d;%dr", 1, HISTORY_GUESS_SCROLL);
502 a->terminal->xmit (a->terminal, buf, i);
505 while (a->history_ptr != h->wptr)
508 History_ent *e = &h->lines[a->history_ptr];
510 HISTORY_INC (h, a->history_ptr);
515 /*If so write the line ot the top of the screen */
516 ansi_draw_line (a, e->line, 0);
519 /*Roll HISTORY_GUESS_SCROLL lines up putting the top line into the xterm's history */
522 ansi_showhide_cursor (a, 1);
523 i = sprintf (buf, "\033[%d;%dH", HISTORY_GUESS_SCROLL, 1);
524 a->terminal->xmit (a->terminal, buf, i);
525 a->terminal->xmit (a->terminal, "\033D", 2);
526 a->pos.x = ANSI_INVAL;
528 /*now do the same in our image of the screen */
537 for (s.y++; s.y < HISTORY_GUESS_SCROLL; s.y++, e.y++)
539 memcpy (&a->crt.screen[CRT_ADDR_POS (&e)],
540 &a->crt.screen[CRT_ADDR_POS (&s)],
541 sizeof (CRT_CA) * CRT_COLS);
547 crt_erase (&a->crt, s, e, 1);
552 a->terminal->xmit (a->terminal, "\033[r", 3);
553 a->pos.x = ANSI_INVAL;
561 ansi_draw (ANSI * a, CRT * c)
565 int hidden_cursor = 0;
567 ansi_resize_check (a);
571 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
573 if (p.y >= a->size.y)
576 ansi_draw_line (a, &c->screen[CRT_ADDR (p.y, 0)], p.y);
581 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
583 char msg[1024]; // = "Window is too small";
589 sprintf (msg, "Window too small (%dx%d need %dx%d)", a->size.x,
590 a->size.y, CRT_COLS, CRT_ROWS);
592 ansi_showhide_cursor (a, 1);
593 ansi_set_attr (a, CRT_ATTR_REVERSE);
594 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
597 a->terminal->xmit (a->terminal, msg, i);
598 a->pos.x = ANSI_INVAL;
602 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
604 ansi_showhide_cursor (a, 1);
609 ansi_move (a, a->crt.pos);
611 a->crt.hide_cursor = c->hide_cursor;
612 ansi_showhide_cursor (a, a->crt.hide_cursor);
616 ansi_reset (ANSI * a, CRT * c)
619 ansi_draw (a, c ? c : &a->crt);
623 ansi_terminal_reset (ANSI * a)
625 CRT_Pos p = { 0, CRT_ROWS };
626 ansi_force_attr_normal (a);
632 ansi_flush_escape (ANSI * a, Context * c)
634 ANSI_Parser *p = &a->parser;
637 for (i = 0; i < p->escape_ptr; ++i)
639 keydis_key (c->k, c, p->escape_buf[i]);
647 ansi_parse_deckey (ANSI * a, Context * c)
649 ANSI_Parser *p = &a->parser;
650 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
652 ansi_flush_escape (a, c);
656 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
658 keydis_key (c->k, c, KEY_UP + (p->escape_buf[2] - 'A'));
660 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
662 keydis_key (c->k, c, KEY_154 + (p->escape_buf[2] - 'a'));
666 ansi_flush_escape (a, c);
674 ansi_parse_ansikey (ANSI * a, Context * c)
676 ANSI_Parser *p = &a->parser;
678 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
680 ansi_flush_escape (a, c);
683 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
685 keydis_key (c->k, c, KEY_180 + (p->escape_buf[2] - '0'));
689 ansi_flush_escape (a, c);
700 ansi_parse_escape (ANSI * a, Context * c)
702 ANSI_Parser *p = &a->parser;
703 switch (p->escape_ptr)
709 switch (p->escape_buf[1])
715 ansi_flush_escape (a, c);
719 switch (p->escape_buf[1])
722 ansi_parse_deckey (a, c);
725 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
726 ansi_parse_deckey (a, c);
729 ansi_flush_escape (a, c);
733 switch (p->escape_buf[1])
736 ansi_parse_ansikey (a, c);
739 ansi_flush_escape (a, c);
743 ansi_flush_escape (a, c);
749 ansi_check_escape (ANSI * a, Context * c)
751 ANSI_Parser *p = &a->parser;
752 struct timeval now, diff;
753 gettimeofday (&now, NULL);
754 timersub (&now, &p->last_escape, &diff);
757 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
758 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
766 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
767 ansi_flush_escape (a, c);
773 ansi_parse_char (ANSI * a, Context * c, int ch)
775 ANSI_Parser *p = &a->parser;
778 /*See if it's time to flush the escape*/
779 ansi_check_escape (a, c);
784 ansi_flush_escape (a, c);
788 gettimeofday (&p->last_escape, NULL);
793 p->escape_buf[p->escape_ptr++] = ch;
794 ansi_parse_escape (a, c);
798 keydis_key (c->k, c, ch);
804 ansi_parse (ANSI * a, Context * c, char *buf, int len)
807 ansi_parse_char (a, c, *(buf++));
811 ansi_dispatch (ANSI * a, Context * c)
816 ansi_check_escape (a, c);
822 red = a->terminal->recv (a->terminal, buf, sizeof (buf));
835 a->history_ptr = c->h->wptr;
836 HISTORY_INC (c->h, a->history_ptr);
843 ansi_parse (a, c, buf, red);
850 ansi_update (ANSI * a, Context * c)
852 ansi_history (a, c->h);
853 ansi_draw (a, &c->v->crt);
856 static void ansi_free(ANSI *a)
858 a->terminal_reset(a);
860 a->terminal->close(a->terminal);
866 ANSI *ansi_new_from_terminal(ANSI *a,TTY *t)
870 ret=malloc(sizeof(ANSI));
871 memset(ret,0,sizeof(ANSI));
875 ret->update=ansi_update;
876 ret->reset=ansi_reset;
877 ret->terminal_reset=ansi_terminal_reset;
878 ret->close=ansi_free;