4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.28 2008/02/22 17:07:00 james
14 * *** empty log message ***
16 * Revision 1.27 2008/02/20 22:54:22 staffcvs
17 * *** empty log message ***
19 * Revision 1.26 2008/02/20 20:16:07 james
20 * *** empty log message ***
22 * Revision 1.25 2008/02/20 19:44:37 james
25 * Revision 1.24 2008/02/20 19:36:06 james
28 * Revision 1.23 2008/02/20 19:25:09 james
29 * *** empty log message ***
31 * Revision 1.22 2008/02/15 03:32:07 james
32 * *** empty log message ***
34 * Revision 1.21 2008/02/14 02:46:44 james
35 * *** empty log message ***
37 * Revision 1.20 2008/02/14 01:55:57 james
38 * *** empty log message ***
40 * Revision 1.19 2008/02/13 16:57:29 james
41 * *** empty log message ***
43 * Revision 1.18 2008/02/13 09:12:21 james
44 * *** empty log message ***
46 * Revision 1.17 2008/02/13 01:08:18 james
47 * *** empty log message ***
49 * Revision 1.16 2008/02/07 13:22:51 james
50 * *** empty log message ***
52 * Revision 1.15 2008/02/07 13:19:48 james
53 * *** empty log message ***
55 * Revision 1.14 2008/02/07 12:21:16 james
56 * *** empty log message ***
58 * Revision 1.13 2008/02/07 12:16:04 james
59 * *** empty log message ***
61 * Revision 1.12 2008/02/07 11:32:41 james
62 * *** empty log message ***
64 * Revision 1.11 2008/02/07 11:11:14 staffcvs
65 * *** empty log message ***
67 * Revision 1.10 2008/02/07 01:02:52 james
68 * *** empty log message ***
70 * Revision 1.9 2008/02/07 00:43:27 james
71 * *** empty log message ***
73 * Revision 1.8 2008/02/07 00:39:13 james
74 * *** empty log message ***
76 * Revision 1.7 2008/02/06 20:26:57 james
77 * *** empty log message ***
79 * Revision 1.6 2008/02/06 17:53:28 james
80 * *** empty log message ***
82 * Revision 1.5 2008/02/06 15:53:22 james
83 * *** empty log message ***
85 * Revision 1.4 2008/02/04 20:23:55 james
86 * *** empty log message ***
88 * Revision 1.3 2008/02/04 05:45:55 james
91 * Revision 1.2 2008/02/04 02:05:06 james
92 * *** empty log message ***
94 * Revision 1.1 2008/02/03 23:31:25 james
95 * *** empty log message ***
102 ansi_move (ANSI * a, CRT_Pos p)
106 int dx = p.x - a->pos.x;
107 int dy = p.y - a->pos.y;
109 // a->pos.x = ANSI_INVAL;
111 if (a->pos.x != ANSI_INVAL)
121 a->terminal->xmit (a->terminal, "\033[C", 3);
125 a->terminal->xmit (a->terminal, "\033[D", 3);
129 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
130 a->terminal->xmit (a->terminal, buf, n);
137 a->terminal->xmit (a->terminal, "\033[A", 3);
141 a->terminal->xmit (a->terminal, "\033[B", 3);
145 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
146 a->terminal->xmit (a->terminal, buf, n);
150 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
151 a->terminal->xmit (a->terminal, buf, n);
158 a->terminal->xmit (a->terminal, "\033[E", 3);
162 a->terminal->xmit (a->terminal, "\033[F", 3);
166 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
167 a->terminal->xmit (a->terminal, buf, n);
171 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
172 a->terminal->xmit (a->terminal, buf, n);
177 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
178 a->terminal->xmit (a->terminal, buf, n);
183 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
184 a->terminal->xmit (a->terminal, buf, n);
192 ansi_showhide_cursor (ANSI * a, int hide)
194 if (a->hide_cursor == hide)
199 a->terminal->xmit (a->terminal, "\033[?25l", 6);
203 a->terminal->xmit (a->terminal, "\033[?25h", 6);
206 a->hide_cursor = hide;
211 ansi_force_attr_normal (ANSI * a)
213 a->terminal->xmit (a->terminal, "\033[0m", 4);
214 a->attr = CRT_ATTR_NORMAL;
215 a->color = ANSI_INVAL;
219 ansi_set_color (ANSI * a, int color)
226 if ((a->color == ANSI_INVAL) || (color != a->color))
228 fg = CRT_COLOR_FG (color);
229 bg = CRT_COLOR_BG (color);
231 if (fg & CRT_COLOR_INTENSITY)
240 if (bg & CRT_COLOR_INTENSITY)
249 i = sprintf (buf, "\033[%d;%dm", fg, bg);
251 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
254 a->terminal->xmit (a->terminal, buf, i);
260 ansi_set_attr (ANSI * a, int attr)
264 dif = attr ^ a->attr;
272 if (attr == CRT_ATTR_NORMAL)
274 ansi_force_attr_normal (a);
279 if (dif & CRT_ATTR_UNDERLINE)
281 if (attr & CRT_ATTR_UNDERLINE)
283 a->terminal->xmit (a->terminal, "\033[4m", 4);
287 a->terminal->xmit (a->terminal, "\033[24m", 5);
290 if (dif & CRT_ATTR_REVERSE)
292 if (attr & CRT_ATTR_REVERSE)
294 a->terminal->xmit (a->terminal, "\033[7m", 4);
298 a->terminal->xmit (a->terminal, "\033[27m", 5);
301 if (dif & CRT_ATTR_BOLD)
303 if (attr & CRT_ATTR_BOLD)
305 a->terminal->xmit (a->terminal, "\033[1m", 4);
309 a->terminal->xmit (a->terminal, "\033[21m", 5);
310 a->terminal->xmit (a->terminal, "\033[22m", 5);
318 ansi_render (ANSI * a, CRT_CA ca)
327 ansi_set_attr (a, ca.attr);
328 ansi_set_color (a, ca.color);
330 a->terminal->xmit (a->terminal, &ca.chr, 1);
334 /*Can't easily wrap round here as don't know size of destination screen*/
335 /*so invalidate the cached cursor position*/
337 if (a->pos.x >= a->size.x)
338 a->pos.x = ANSI_INVAL;
349 ansi_set_attr (a, CRT_ATTR_NORMAL);
350 ansi_set_color (a, CRT_COLOR_NORMAL);
352 a->terminal->xmit (a->terminal, "\033[2J", 4);
353 /*different emulators leave cursor in different places after cls differently*/
354 a->pos.x = ANSI_INVAL;
359 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
365 if (e.x != (CRT_COLS - 1))
369 if (s.y >= a->size.y)
372 ansi_showhide_cursor (a, 1);
373 i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
374 a->terminal->xmit (a->terminal, buf, i);
375 i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
376 a->terminal->xmit (a->terminal, buf, i);
377 a->terminal->xmit (a->terminal, "\033D", 2);
378 a->terminal->xmit (a->terminal, "\033[r", 3);
381 crt_erase (&a->crt, s, e, 1);
383 a->pos.x = ANSI_INVAL;
390 ansi_spot_scroll_up (ANSI * a, CRT * c)
394 l = c->sh.e.x - c->sh.s.x;
396 l *= sizeof (CRT_CA);
398 n = c->sh.e.y - c->sh.s.y;
399 p = CRT_ADDR_POS (&c->sh.s);
403 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
408 if (ansi_scroll_up (a, c->sh.s, c->sh.e))
411 n = c->sh.e.y - c->sh.s.y;
412 p = CRT_ADDR_POS (&c->sh.s);
416 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
420 c->sh.dir = 0; //FIXME: horrid hack
425 ansi_spot_scroll (ANSI * a, CRT * c)
431 /*we only care about up for the moment */
432 ansi_spot_scroll_up (a, c);
443 ansi_draw_line (ANSI * a, CRT_CA * cap, int y)
445 CRT_Pos p = { 0, y };
446 CRT_CA *acap = &a->crt.screen[CRT_ADDR_POS (&p)];
448 for (p.x = 0; p.x < CRT_COLS; ++p.x)
450 if (p.x >= a->size.x)
453 if (crt_ca_cmp (*acap, *cap))
455 ansi_showhide_cursor (a, 1);
459 ansi_render (a, *acap);
468 ansi_resize_check (ANSI * a)
471 if (!crt_pos_cmp (a->terminal->size, a->size))
474 terminal_getsize (a->terminal);
476 a->size = a->terminal->size;
478 a->pos.x = ANSI_INVAL;
479 a->hide_cursor = ANSI_INVAL;
483 // FIXME: -- echos back crap?
484 // a->terminal->xmit (a->terminal, "\033[c", 3);
487 a->terminal->xmit (a->terminal, "\033=", 2);
488 a->terminal->xmit (a->terminal, "\033[?6l", 5);
489 a->terminal->xmit (a->terminal, "\033[r", 3);
493 #define HISTORY_GUESS_SCROLL 24 /*guess all 24 lines usually scroll */
494 /*if they haven't then ansi_draw will patch it up*/
497 ansi_history (ANSI * a, History * h)
501 /*Do we need to catch up on history?*/
503 if (a->history_ptr == h->wptr)
505 ansi_resize_check (a);
507 if ((a->size.x < CRT_COLS) || (a->size.y < CRT_ROWS))
510 ansi_force_attr_normal (a);
511 ansi_set_color (a, CRT_COLOR_NORMAL);
513 i = sprintf (buf, "\033[%d;%dr", 1, HISTORY_GUESS_SCROLL);
514 a->terminal->xmit (a->terminal, buf, i);
517 while (a->history_ptr != h->wptr)
520 History_ent *e = &h->lines[a->history_ptr];
522 HISTORY_INC (h, a->history_ptr);
527 /*If so write the line ot the top of the screen */
528 ansi_draw_line (a, e->line, 0);
531 /*Roll HISTORY_GUESS_SCROLL lines up putting the top line into the xterm's history */
534 /*Make extra lines a predictable colour*/
535 ansi_set_color (a, CRT_COLOR_NORMAL);
537 ansi_showhide_cursor (a, 1);
538 i = sprintf (buf, "\033[%d;%dH", HISTORY_GUESS_SCROLL, 1);
539 a->terminal->xmit (a->terminal, buf, i);
540 a->terminal->xmit (a->terminal, "\033D", 2);
541 a->pos.x = ANSI_INVAL;
543 /*now do the same in our image of the screen */
552 for (s.y++; s.y < HISTORY_GUESS_SCROLL; s.y++, e.y++)
554 memcpy (&a->crt.screen[CRT_ADDR_POS (&e)],
555 &a->crt.screen[CRT_ADDR_POS (&s)],
556 sizeof (CRT_CA) * CRT_COLS);
562 crt_erase (&a->crt, s, e, 1,CRT_COLOR_NORMAL);
567 a->terminal->xmit (a->terminal, "\033[r", 3);
568 a->pos.x = ANSI_INVAL;
576 ansi_draw (ANSI * a, CRT * c)
580 int hidden_cursor = 0;
582 ansi_resize_check (a);
586 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
588 if (p.y >= a->size.y)
591 ansi_draw_line (a, &c->screen[CRT_ADDR (p.y, 0)], p.y);
596 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
598 char msg[1024]; // = "Window is too small";
604 sprintf (msg, "Window too small (%dx%d need %dx%d)", a->size.x,
605 a->size.y, CRT_COLS, CRT_ROWS);
607 ansi_showhide_cursor (a, 1);
608 ansi_set_attr (a, CRT_ATTR_REVERSE);
609 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
612 a->terminal->xmit (a->terminal, msg, i);
613 a->pos.x = ANSI_INVAL;
617 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
619 ansi_showhide_cursor (a, 1);
624 ansi_move (a, a->crt.pos);
626 a->crt.hide_cursor = c->hide_cursor;
627 ansi_showhide_cursor (a, a->crt.hide_cursor);
631 ansi_reset (ANSI * a, CRT * c)
634 ansi_draw (a, c ? c : &a->crt);
638 ansi_terminal_reset (ANSI * a)
640 CRT_Pos p = { 0, CRT_ROWS };
641 ansi_force_attr_normal (a);
647 ansi_flush_escape (ANSI * a, Context * c)
649 ANSI_Parser *p = &a->parser;
652 for (i = 0; i < p->escape_ptr; ++i)
654 keydis_key (c->k, c, p->escape_buf[i]);
662 ansi_parse_deckey (ANSI * a, Context * c)
664 ANSI_Parser *p = &a->parser;
665 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
667 ansi_flush_escape (a, c);
671 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
673 keydis_key (c->k, c, KEY_UP + (p->escape_buf[2] - 'A'));
675 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
677 keydis_key (c->k, c, KEY_154 + (p->escape_buf[2] - 'a'));
681 ansi_flush_escape (a, c);
689 ansi_parse_ansikey (ANSI * a, Context * c)
691 ANSI_Parser *p = &a->parser;
693 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
695 ansi_flush_escape (a, c);
698 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
700 keydis_key (c->k, c, KEY_180 + (p->escape_buf[2] - '0'));
704 ansi_flush_escape (a, c);
715 ansi_parse_escape (ANSI * a, Context * c)
717 ANSI_Parser *p = &a->parser;
718 switch (p->escape_ptr)
724 switch (p->escape_buf[1])
730 ansi_flush_escape (a, c);
734 switch (p->escape_buf[1])
737 ansi_parse_deckey (a, c);
740 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
741 ansi_parse_deckey (a, c);
744 ansi_flush_escape (a, c);
748 switch (p->escape_buf[1])
751 ansi_parse_ansikey (a, c);
754 ansi_flush_escape (a, c);
758 ansi_flush_escape (a, c);
764 ansi_check_escape (ANSI * a, Context * c)
766 ANSI_Parser *p = &a->parser;
767 struct timeval now, diff;
768 gettimeofday (&now, NULL);
769 timersub (&now, &p->last_escape, &diff);
772 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
773 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
781 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
782 ansi_flush_escape (a, c);
788 ansi_parse_char (ANSI * a, Context * c, int ch)
790 ANSI_Parser *p = &a->parser;
793 /*See if it's time to flush the escape*/
794 ansi_check_escape (a, c);
799 ansi_flush_escape (a, c);
803 gettimeofday (&p->last_escape, NULL);
808 p->escape_buf[p->escape_ptr++] = ch;
809 ansi_parse_escape (a, c);
813 keydis_key (c->k, c, ch);
819 ansi_parse (ANSI * a, Context * c, char *buf, int len)
822 ansi_parse_char (a, c, *(buf++));
826 ansi_dispatch (ANSI * a, Context * c)
831 ansi_check_escape (a, c);
837 red = a->terminal->recv (a->terminal, buf, sizeof (buf));
850 a->history_ptr = c->h->wptr;
851 HISTORY_INC (c->h, a->history_ptr);
858 ansi_parse (a, c, buf, red);
865 ansi_update (ANSI * a, Context * c)
867 ansi_history (a, c->h);
868 ansi_draw (a, &c->v->crt);
874 a->terminal_reset (a);
876 a->terminal->close (a->terminal);
883 ansi_new_from_terminal (TTY * t)
887 ret = malloc (sizeof (ANSI));
888 memset (ret, 0, sizeof (ANSI));
892 ret->update = ansi_update;
893 ret->reset = ansi_reset;
894 ret->terminal_reset = ansi_terminal_reset;
895 ret->close = ansi_free;
896 ret->dispatch = ansi_dispatch;