4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.25 2008/02/20 19:44:37 james
16 * Revision 1.24 2008/02/20 19:36:06 james
19 * Revision 1.23 2008/02/20 19:25:09 james
20 * *** empty log message ***
22 * Revision 1.22 2008/02/15 03:32:07 james
23 * *** empty log message ***
25 * Revision 1.21 2008/02/14 02:46:44 james
26 * *** empty log message ***
28 * Revision 1.20 2008/02/14 01:55:57 james
29 * *** empty log message ***
31 * Revision 1.19 2008/02/13 16:57:29 james
32 * *** empty log message ***
34 * Revision 1.18 2008/02/13 09:12:21 james
35 * *** empty log message ***
37 * Revision 1.17 2008/02/13 01:08:18 james
38 * *** empty log message ***
40 * Revision 1.16 2008/02/07 13:22:51 james
41 * *** empty log message ***
43 * Revision 1.15 2008/02/07 13:19:48 james
44 * *** empty log message ***
46 * Revision 1.14 2008/02/07 12:21:16 james
47 * *** empty log message ***
49 * Revision 1.13 2008/02/07 12:16:04 james
50 * *** empty log message ***
52 * Revision 1.12 2008/02/07 11:32:41 james
53 * *** empty log message ***
55 * Revision 1.11 2008/02/07 11:11:14 staffcvs
56 * *** empty log message ***
58 * Revision 1.10 2008/02/07 01:02:52 james
59 * *** empty log message ***
61 * Revision 1.9 2008/02/07 00:43:27 james
62 * *** empty log message ***
64 * Revision 1.8 2008/02/07 00:39:13 james
65 * *** empty log message ***
67 * Revision 1.7 2008/02/06 20:26:57 james
68 * *** empty log message ***
70 * Revision 1.6 2008/02/06 17:53:28 james
71 * *** empty log message ***
73 * Revision 1.5 2008/02/06 15:53:22 james
74 * *** empty log message ***
76 * Revision 1.4 2008/02/04 20:23:55 james
77 * *** empty log message ***
79 * Revision 1.3 2008/02/04 05:45:55 james
82 * Revision 1.2 2008/02/04 02:05:06 james
83 * *** empty log message ***
85 * Revision 1.1 2008/02/03 23:31:25 james
86 * *** empty log message ***
93 ansi_move (ANSI * a, CRT_Pos p)
97 int dx = p.x - a->pos.x;
98 int dy = p.y - a->pos.y;
100 // a->pos.x = ANSI_INVAL;
102 if (a->pos.x != ANSI_INVAL)
112 a->terminal->xmit (a->terminal, "\033[C", 3);
116 a->terminal->xmit (a->terminal, "\033[D", 3);
120 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
121 a->terminal->xmit (a->terminal, buf, n);
128 a->terminal->xmit (a->terminal, "\033[A", 3);
132 a->terminal->xmit (a->terminal, "\033[B", 3);
136 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
137 a->terminal->xmit (a->terminal, buf, n);
141 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
142 a->terminal->xmit (a->terminal, buf, n);
149 a->terminal->xmit (a->terminal, "\033[E", 3);
153 a->terminal->xmit (a->terminal, "\033[F", 3);
157 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
158 a->terminal->xmit (a->terminal, buf, n);
162 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
163 a->terminal->xmit (a->terminal, buf, n);
168 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
169 a->terminal->xmit (a->terminal, buf, n);
174 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
175 a->terminal->xmit (a->terminal, buf, n);
183 ansi_showhide_cursor (ANSI * a, int hide)
185 if (a->hide_cursor == hide)
190 a->terminal->xmit (a->terminal, "\033[?25l", 6);
194 a->terminal->xmit (a->terminal, "\033[?25h", 6);
197 a->hide_cursor = hide;
202 ansi_force_attr_normal (ANSI * a)
204 a->terminal->xmit (a->terminal, "\033[0m", 4);
205 a->attr = CRT_ATTR_NORMAL;
206 a->color = ANSI_INVAL;
210 ansi_set_color (ANSI * a, int color)
217 if ((a->color == ANSI_INVAL) || (color != a->color))
219 fg = CRT_COLOR_FG (color);
220 bg = CRT_COLOR_BG (color);
222 if (fg & CRT_COLOR_INTENSITY)
231 if (bg & CRT_COLOR_INTENSITY)
240 i = sprintf (buf, "\033[%d;%dm", fg, bg);
242 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
245 a->terminal->xmit (a->terminal, buf, i);
251 ansi_set_attr (ANSI * a, int attr)
255 dif = attr ^ a->attr;
263 if (attr == CRT_ATTR_NORMAL)
265 ansi_force_attr_normal (a);
270 if (dif & CRT_ATTR_UNDERLINE)
272 if (attr & CRT_ATTR_UNDERLINE)
274 a->terminal->xmit (a->terminal, "\033[4m", 4);
278 a->terminal->xmit (a->terminal, "\033[24m", 5);
281 if (dif & CRT_ATTR_REVERSE)
283 if (attr & CRT_ATTR_REVERSE)
285 a->terminal->xmit (a->terminal, "\033[7m", 4);
289 a->terminal->xmit (a->terminal, "\033[27m", 5);
292 if (dif & CRT_ATTR_BOLD)
294 if (attr & CRT_ATTR_BOLD)
296 a->terminal->xmit (a->terminal, "\033[1m", 4);
300 a->terminal->xmit (a->terminal, "\033[21m", 5);
301 a->terminal->xmit (a->terminal, "\033[22m", 5);
309 ansi_render (ANSI * a, CRT_CA ca)
318 ansi_set_attr (a, ca.attr);
319 ansi_set_color (a, ca.color);
321 a->terminal->xmit (a->terminal, &ca.chr, 1);
325 /*Can't easily wrap round here as don't know size of destination screen*/
326 /*so invalidate the cached cursor position*/
328 if (a->pos.x >= a->size.x)
329 a->pos.x = ANSI_INVAL;
340 ansi_set_attr (a, CRT_ATTR_NORMAL);
341 ansi_set_color (a, CRT_COLOR_NORMAL);
343 a->terminal->xmit (a->terminal, "\033[2J", 4);
344 /*different emulators leave cursor in different places after cls differently*/
345 a->pos.x = ANSI_INVAL;
350 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
356 if (e.x != (CRT_COLS - 1))
360 if (s.y >= a->size.y)
363 ansi_showhide_cursor (a, 1);
364 i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
365 a->terminal->xmit (a->terminal, buf, i);
366 i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
367 a->terminal->xmit (a->terminal, buf, i);
368 a->terminal->xmit (a->terminal, "\033D", 2);
369 a->terminal->xmit (a->terminal, "\033[r", 3);
372 crt_erase (&a->crt, s, e, 1);
374 a->pos.x = ANSI_INVAL;
381 ansi_spot_scroll_up (ANSI * a, CRT * c)
385 l = c->sh.e.x - c->sh.s.x;
387 l *= sizeof (CRT_CA);
389 n = c->sh.e.y - c->sh.s.y;
390 p = CRT_ADDR_POS (&c->sh.s);
394 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
399 if (ansi_scroll_up (a, c->sh.s, c->sh.e))
402 n = c->sh.e.y - c->sh.s.y;
403 p = CRT_ADDR_POS (&c->sh.s);
407 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
411 c->sh.dir = 0; //FIXME: horrid hack
416 ansi_spot_scroll (ANSI * a, CRT * c)
422 /*we only care about up for the moment */
423 ansi_spot_scroll_up (a, c);
434 ansi_draw_line (ANSI * a, CRT_CA * cap, int y)
436 CRT_Pos p = { 0, y };
437 CRT_CA *acap = &a->crt.screen[CRT_ADDR_POS (&p)];
439 for (p.x = 0; p.x < CRT_COLS; ++p.x)
441 if (p.x >= a->size.x)
444 if (crt_ca_cmp (*acap, *cap))
446 ansi_showhide_cursor (a, 1);
450 ansi_render (a, *acap);
459 ansi_resize_check (ANSI * a)
462 if (!crt_pos_cmp (a->terminal->size, a->size))
465 terminal_getsize (a->terminal);
467 a->size = a->terminal->size;
469 a->pos.x = ANSI_INVAL;
470 a->hide_cursor = ANSI_INVAL;
474 // FIXME: -- echos back crap?
475 // a->terminal->xmit (a->terminal, "\033[c", 3);
478 a->terminal->xmit (a->terminal, "\033=", 2);
479 a->terminal->xmit (a->terminal, "\033[?6l", 5);
480 a->terminal->xmit (a->terminal, "\033[r", 3);
484 #define HISTORY_GUESS_SCROLL 24 /*guess all 24 lines usually scroll */
485 /*if they haven't then ansi_draw will patch it up*/
488 ansi_history (ANSI * a, History * h)
492 /*Do we need to catch up on history?*/
494 if (a->history_ptr == h->wptr)
496 ansi_resize_check (a);
498 if ((a->size.x < CRT_COLS) || (a->size.y < CRT_ROWS))
501 ansi_force_attr_normal (a);
502 ansi_set_color (a, CRT_COLOR_NORMAL);
504 i = sprintf (buf, "\033[%d;%dr", 1, HISTORY_GUESS_SCROLL);
505 a->terminal->xmit (a->terminal, buf, i);
508 while (a->history_ptr != h->wptr)
511 History_ent *e = &h->lines[a->history_ptr];
513 HISTORY_INC (h, a->history_ptr);
518 /*If so write the line ot the top of the screen */
519 ansi_draw_line (a, e->line, 0);
522 /*Roll HISTORY_GUESS_SCROLL lines up putting the top line into the xterm's history */
525 ansi_showhide_cursor (a, 1);
526 i = sprintf (buf, "\033[%d;%dH", HISTORY_GUESS_SCROLL, 1);
527 a->terminal->xmit (a->terminal, buf, i);
528 a->terminal->xmit (a->terminal, "\033D", 2);
529 a->pos.x = ANSI_INVAL;
531 /*now do the same in our image of the screen */
540 for (s.y++; s.y < HISTORY_GUESS_SCROLL; s.y++, e.y++)
542 memcpy (&a->crt.screen[CRT_ADDR_POS (&e)],
543 &a->crt.screen[CRT_ADDR_POS (&s)],
544 sizeof (CRT_CA) * CRT_COLS);
550 crt_erase (&a->crt, s, e, 1);
555 a->terminal->xmit (a->terminal, "\033[r", 3);
556 a->pos.x = ANSI_INVAL;
564 ansi_draw (ANSI * a, CRT * c)
568 int hidden_cursor = 0;
570 ansi_resize_check (a);
574 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
576 if (p.y >= a->size.y)
579 ansi_draw_line (a, &c->screen[CRT_ADDR (p.y, 0)], p.y);
584 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
586 char msg[1024]; // = "Window is too small";
592 sprintf (msg, "Window too small (%dx%d need %dx%d)", a->size.x,
593 a->size.y, CRT_COLS, CRT_ROWS);
595 ansi_showhide_cursor (a, 1);
596 ansi_set_attr (a, CRT_ATTR_REVERSE);
597 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
600 a->terminal->xmit (a->terminal, msg, i);
601 a->pos.x = ANSI_INVAL;
605 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
607 ansi_showhide_cursor (a, 1);
612 ansi_move (a, a->crt.pos);
614 a->crt.hide_cursor = c->hide_cursor;
615 ansi_showhide_cursor (a, a->crt.hide_cursor);
619 ansi_reset (ANSI * a, CRT * c)
622 ansi_draw (a, c ? c : &a->crt);
626 ansi_terminal_reset (ANSI * a)
628 CRT_Pos p = { 0, CRT_ROWS };
629 ansi_force_attr_normal (a);
635 ansi_flush_escape (ANSI * a, Context * c)
637 ANSI_Parser *p = &a->parser;
640 for (i = 0; i < p->escape_ptr; ++i)
642 keydis_key (c->k, c, p->escape_buf[i]);
650 ansi_parse_deckey (ANSI * a, Context * c)
652 ANSI_Parser *p = &a->parser;
653 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
655 ansi_flush_escape (a, c);
659 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
661 keydis_key (c->k, c, KEY_UP + (p->escape_buf[2] - 'A'));
663 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
665 keydis_key (c->k, c, KEY_154 + (p->escape_buf[2] - 'a'));
669 ansi_flush_escape (a, c);
677 ansi_parse_ansikey (ANSI * a, Context * c)
679 ANSI_Parser *p = &a->parser;
681 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
683 ansi_flush_escape (a, c);
686 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
688 keydis_key (c->k, c, KEY_180 + (p->escape_buf[2] - '0'));
692 ansi_flush_escape (a, c);
703 ansi_parse_escape (ANSI * a, Context * c)
705 ANSI_Parser *p = &a->parser;
706 switch (p->escape_ptr)
712 switch (p->escape_buf[1])
718 ansi_flush_escape (a, c);
722 switch (p->escape_buf[1])
725 ansi_parse_deckey (a, c);
728 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
729 ansi_parse_deckey (a, c);
732 ansi_flush_escape (a, c);
736 switch (p->escape_buf[1])
739 ansi_parse_ansikey (a, c);
742 ansi_flush_escape (a, c);
746 ansi_flush_escape (a, c);
752 ansi_check_escape (ANSI * a, Context * c)
754 ANSI_Parser *p = &a->parser;
755 struct timeval now, diff;
756 gettimeofday (&now, NULL);
757 timersub (&now, &p->last_escape, &diff);
760 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
761 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
769 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
770 ansi_flush_escape (a, c);
776 ansi_parse_char (ANSI * a, Context * c, int ch)
778 ANSI_Parser *p = &a->parser;
781 /*See if it's time to flush the escape*/
782 ansi_check_escape (a, c);
787 ansi_flush_escape (a, c);
791 gettimeofday (&p->last_escape, NULL);
796 p->escape_buf[p->escape_ptr++] = ch;
797 ansi_parse_escape (a, c);
801 keydis_key (c->k, c, ch);
807 ansi_parse (ANSI * a, Context * c, char *buf, int len)
810 ansi_parse_char (a, c, *(buf++));
814 ansi_dispatch (ANSI * a, Context * c)
819 ansi_check_escape (a, c);
825 red = a->terminal->recv (a->terminal, buf, sizeof (buf));
838 a->history_ptr = c->h->wptr;
839 HISTORY_INC (c->h, a->history_ptr);
846 ansi_parse (a, c, buf, red);
853 ansi_update (ANSI * a, Context * c)
855 ansi_history (a, c->h);
856 ansi_draw (a, &c->v->crt);
859 static void ansi_free(ANSI *a)
861 a->terminal_reset(a);
863 a->terminal->close(a->terminal);
869 ANSI *ansi_new_from_terminal(ANSI *a,TTY *t)
873 ret=malloc(sizeof(ANSI));
874 memset(ret,0,sizeof(ANSI));
878 ret->update=ansi_update;
879 ret->reset=ansi_reset;
880 ret->terminal_reset=ansi_terminal_reset;
881 ret->close=ansi_free;