4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.21 2008/02/14 02:46:44 james
14 * *** empty log message ***
16 * Revision 1.20 2008/02/14 01:55:57 james
17 * *** empty log message ***
19 * Revision 1.19 2008/02/13 16:57:29 james
20 * *** empty log message ***
22 * Revision 1.18 2008/02/13 09:12:21 james
23 * *** empty log message ***
25 * Revision 1.17 2008/02/13 01:08:18 james
26 * *** empty log message ***
28 * Revision 1.16 2008/02/07 13:22:51 james
29 * *** empty log message ***
31 * Revision 1.15 2008/02/07 13:19:48 james
32 * *** empty log message ***
34 * Revision 1.14 2008/02/07 12:21:16 james
35 * *** empty log message ***
37 * Revision 1.13 2008/02/07 12:16:04 james
38 * *** empty log message ***
40 * Revision 1.12 2008/02/07 11:32:41 james
41 * *** empty log message ***
43 * Revision 1.11 2008/02/07 11:11:14 staffcvs
44 * *** empty log message ***
46 * Revision 1.10 2008/02/07 01:02:52 james
47 * *** empty log message ***
49 * Revision 1.9 2008/02/07 00:43:27 james
50 * *** empty log message ***
52 * Revision 1.8 2008/02/07 00:39:13 james
53 * *** empty log message ***
55 * Revision 1.7 2008/02/06 20:26:57 james
56 * *** empty log message ***
58 * Revision 1.6 2008/02/06 17:53:28 james
59 * *** empty log message ***
61 * Revision 1.5 2008/02/06 15:53:22 james
62 * *** empty log message ***
64 * Revision 1.4 2008/02/04 20:23:55 james
65 * *** empty log message ***
67 * Revision 1.3 2008/02/04 05:45:55 james
70 * Revision 1.2 2008/02/04 02:05:06 james
71 * *** empty log message ***
73 * Revision 1.1 2008/02/03 23:31:25 james
74 * *** empty log message ***
81 ansi_move (ANSI * a, CRT_Pos p)
85 int dx = p.x - a->pos.x;
86 int dy = p.y - a->pos.y;
88 // a->pos.x = ANSI_INVAL;
90 if (a->pos.x != ANSI_INVAL)
100 a->terminal->xmit (a->terminal, "\033[C", 3);
104 a->terminal->xmit (a->terminal, "\033[D", 3);
108 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
109 a->terminal->xmit (a->terminal, buf, n);
116 a->terminal->xmit (a->terminal, "\033[A", 3);
120 a->terminal->xmit (a->terminal, "\033[B", 3);
124 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
125 a->terminal->xmit (a->terminal, buf, n);
129 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
130 a->terminal->xmit (a->terminal, buf, n);
137 a->terminal->xmit (a->terminal, "\033[E", 3);
141 a->terminal->xmit (a->terminal, "\033[F", 3);
145 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
146 a->terminal->xmit (a->terminal, buf, n);
150 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
151 a->terminal->xmit (a->terminal, buf, n);
156 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
157 a->terminal->xmit (a->terminal, buf, n);
162 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
163 a->terminal->xmit (a->terminal, buf, n);
171 ansi_showhide_cursor (ANSI * a, int hide)
173 if (a->hide_cursor == hide)
178 a->terminal->xmit (a->terminal, "\033[?25l", 6);
182 a->terminal->xmit (a->terminal, "\033[?25h", 6);
185 a->hide_cursor = hide;
190 ansi_force_attr_normal (ANSI * a)
192 a->terminal->xmit (a->terminal, "\033[0m", 4);
193 a->attr = CRT_ATTR_NORMAL;
194 a->color = ANSI_INVAL;
198 ansi_set_color (ANSI * a, int color)
205 if ((a->color == ANSI_INVAL) || (color != a->color))
207 fg = CRT_COLOR_FG (color);
208 bg = CRT_COLOR_BG (color);
210 if (fg & CRT_COLOR_INTENSITY)
219 if (bg & CRT_COLOR_INTENSITY)
228 i = sprintf (buf, "\033[%d;%dm", fg, bg);
230 fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
233 a->terminal->xmit (a->terminal, buf, i);
239 ansi_set_attr (ANSI * a, int attr)
243 dif = attr ^ a->attr;
251 if (attr == CRT_ATTR_NORMAL)
253 ansi_force_attr_normal (a);
258 if (dif & CRT_ATTR_UNDERLINE)
260 if (attr & CRT_ATTR_UNDERLINE)
262 a->terminal->xmit (a->terminal, "\033[4m", 4);
266 a->terminal->xmit (a->terminal, "\033[24m", 5);
269 if (dif & CRT_ATTR_REVERSE)
271 if (attr & CRT_ATTR_REVERSE)
273 a->terminal->xmit (a->terminal, "\033[7m", 4);
277 a->terminal->xmit (a->terminal, "\033[27m", 5);
280 if (dif & CRT_ATTR_BOLD)
282 if (attr & CRT_ATTR_BOLD)
284 a->terminal->xmit (a->terminal, "\033[1m", 4);
288 a->terminal->xmit (a->terminal, "\033[21m", 5);
289 a->terminal->xmit (a->terminal, "\033[22m", 5);
297 ansi_render (ANSI * a, CRT_CA ca)
306 ansi_set_attr (a, ca.attr);
307 ansi_set_color (a, ca.color);
309 a->terminal->xmit (a->terminal, &ca.chr, 1);
313 /*Can't easily wrap round here as don't know size of destination screen*/
314 /*so invalidate the cached cursor position*/
316 if (a->pos.x >= a->size.x)
317 a->pos.x = ANSI_INVAL;
328 ansi_set_attr (a, CRT_ATTR_NORMAL);
329 ansi_set_color (a, CRT_COLOR_NORMAL);
331 a->terminal->xmit (a->terminal, "\033[2J", 4);
332 /*different emulators leave cursor in different places after cls differently*/
333 a->pos.x = ANSI_INVAL;
338 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
344 if (e.x != (CRT_COLS - 1))
348 if (s.y >= a->size.y)
351 ansi_showhide_cursor (a, 1);
352 i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
353 a->terminal->xmit (a->terminal, buf, i);
354 i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
355 a->terminal->xmit (a->terminal, buf, i);
356 a->terminal->xmit (a->terminal, "\033D", 2);
357 a->terminal->xmit (a->terminal, "\033[r", 3);
360 crt_erase (&a->crt, s, e, 1);
362 a->pos.x = ANSI_INVAL;
369 ansi_spot_scroll_up (ANSI * a, CRT * c)
373 l = c->sh.e.x - c->sh.s.x;
375 l *= sizeof (CRT_CA);
377 n = c->sh.e.y - c->sh.s.y;
378 p = CRT_ADDR_POS (&c->sh.s);
382 if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
387 if (ansi_scroll_up (a, c->sh.s, c->sh.e))
390 n = c->sh.e.y - c->sh.s.y;
391 p = CRT_ADDR_POS (&c->sh.s);
395 memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
399 c->sh.dir = 0; //FIXME: horrid hack
404 ansi_spot_scroll (ANSI * a, CRT * c)
410 /*we only care about up for the moment */
411 ansi_spot_scroll_up (a, c);
422 ansi_draw_line (ANSI * a, CRT_CA * cap, int y)
424 CRT_Pos p = { 0, y };
425 CRT_CA *acap = &a->crt.screen[CRT_ADDR_POS (&p)];
427 for (p.x = 0; p.x < CRT_COLS; ++p.x)
429 if (p.x >= a->size.x)
432 if (crt_ca_cmp (*acap, *cap))
434 ansi_showhide_cursor (a, 1);
438 ansi_render (a, *acap);
447 ansi_resize_check (ANSI * a)
450 if (!crt_pos_cmp (a->terminal->size, a->size))
453 terminal_getsize (a->terminal);
455 a->size = a->terminal->size;
457 a->pos.x = ANSI_INVAL;
458 a->hide_cursor = ANSI_INVAL;
462 // FIXME: -- echos back crap?
463 // a->terminal->xmit (a->terminal, "\033[c", 3);
466 a->terminal->xmit (a->terminal, "\033=", 2);
467 a->terminal->xmit (a->terminal, "\033[?6l", 5);
468 a->terminal->xmit (a->terminal, "\033[r", 3);
472 #define HISTORY_GUESS_SCROLL 24 /*guess all 24 lines usually scroll */
473 /*if they haven't then ansi_draw will patch it up*/
476 ansi_history (ANSI * a, History * h)
480 /*Do we need to catch up on history?*/
482 if (a->history_ptr == h->wptr)
484 ansi_resize_check (a);
486 if ((a->size.x < CRT_COLS) || (a->size.y < CRT_ROWS))
489 ansi_force_attr_normal (a);
490 ansi_set_color (a, CRT_COLOR_NORMAL);
492 i = sprintf (buf, "\033[%d;%dr", 1, HISTORY_GUESS_SCROLL);
493 a->terminal->xmit (a->terminal, buf, i);
496 while (a->history_ptr != h->wptr)
499 History_ent *e = &h->lines[a->history_ptr];
501 HISTORY_INC (h, a->history_ptr);
506 /*If so write the line ot the top of the screen */
507 ansi_draw_line (a, e->line, 0);
510 /*Roll HISTORY_GUESS_SCROLL lines up putting the top line into the xterm's history */
513 ansi_showhide_cursor (a, 1);
514 i = sprintf (buf, "\033[%d;%dH", HISTORY_GUESS_SCROLL, 1);
515 a->terminal->xmit (a->terminal, buf, i);
516 a->terminal->xmit (a->terminal, "\033D", 2);
517 a->pos.x = ANSI_INVAL;
519 /*now do the same in our image of the screen */
528 for (s.y++; s.y < HISTORY_GUESS_SCROLL; s.y++, e.y++)
530 memcpy (&a->crt.screen[CRT_ADDR_POS (&e)],
531 &a->crt.screen[CRT_ADDR_POS (&s)],
532 sizeof (CRT_CA) * CRT_COLS);
538 crt_erase (&a->crt, s, e, 1);
543 a->terminal->xmit (a->terminal, "\033[r", 3);
544 a->pos.x = ANSI_INVAL;
552 ansi_draw (ANSI * a, CRT * c)
556 int hidden_cursor = 0;
558 ansi_resize_check (a);
562 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
564 if (p.y >= a->size.y)
567 ansi_draw_line (a, &c->screen[CRT_ADDR (p.y, 0)], p.y);
572 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
574 char msg[1024]; // = "Window is too small";
580 sprintf (msg, "Window too small (%dx%d need %dx%d)", a->size.x,
581 a->size.y, CRT_COLS, CRT_ROWS);
583 ansi_showhide_cursor (a, 1);
584 ansi_set_attr (a, CRT_ATTR_REVERSE);
585 ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
588 a->terminal->xmit (a->terminal, msg, i);
589 a->pos.x = ANSI_INVAL;
593 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
595 ansi_showhide_cursor (a, 1);
600 ansi_move (a, a->crt.pos);
602 a->crt.hide_cursor = c->hide_cursor;
603 ansi_showhide_cursor (a, a->crt.hide_cursor);
607 ansi_reset (ANSI * a, CRT * c)
610 ansi_draw (a, c ? c : &a->crt);
614 ansi_terminal_reset (ANSI * a)
616 CRT_Pos p = { 0, CRT_ROWS };
617 ansi_force_attr_normal (a);
623 ansi_flush_escape (ANSI * a, Context * c)
625 ANSI_Parser *p = &a->parser;
628 for (i = 0; i < p->escape_ptr; ++i)
630 c->k->key (c->k, p->escape_buf[i]);
638 ansi_parse_deckey (ANSI * a, Context * c)
640 ANSI_Parser *p = &a->parser;
641 if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
643 ansi_flush_escape (a, c);
647 if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
649 c->k->key (c->k, KEY_UP + (p->escape_buf[2] - 'A'));
651 else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
653 c->k->key (c->k, KEY_154 + (p->escape_buf[2] - 'a'));
657 ansi_flush_escape (a, c);
665 ansi_parse_ansikey (ANSI * a, Context * c)
667 ANSI_Parser *p = &a->parser;
669 if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
671 ansi_flush_escape (a, c);
674 if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
676 c->k->key (c->k, KEY_180 + (p->escape_buf[2] - '0'));
680 ansi_flush_escape (a, c);
691 ansi_parse_escape (ANSI * a, Context * c)
693 ANSI_Parser *p = &a->parser;
694 switch (p->escape_ptr)
700 switch (p->escape_buf[1])
706 ansi_flush_escape (a, c);
710 switch (p->escape_buf[1])
713 ansi_parse_deckey (a, c);
716 if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
717 ansi_parse_deckey (a, c);
720 ansi_flush_escape (a, c);
724 switch (p->escape_buf[1])
727 ansi_parse_ansikey (a, c);
730 ansi_flush_escape (a, c);
734 ansi_flush_escape (a, c);
740 ansi_check_escape (ANSI * a, Context * c)
742 ANSI_Parser *p = &a->parser;
743 struct timeval now, diff;
744 gettimeofday (&now, NULL);
745 timersub (&now, &p->last_escape, &diff);
748 fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
749 p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
757 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
758 ansi_flush_escape (a, c);
764 ansi_parse_char (ANSI * a, Context * c, int ch)
766 ANSI_Parser *p = &a->parser;
769 /*See if it's time to flush the escape*/
770 ansi_check_escape (a, c);
775 ansi_flush_escape (a, c);
779 gettimeofday (&p->last_escape, NULL);
784 p->escape_buf[p->escape_ptr++] = ch;
785 ansi_parse_escape (a, c);
789 c->k->key (c->k, ch);
795 ansi_parse (ANSI * a, Context * c, char *buf, int len)
798 ansi_parse_char (a, c, *(buf++));
802 ansi_dispatch (ANSI * a, Context * c)
807 ansi_check_escape (a, c);
813 red = a->terminal->recv (a->terminal, buf, sizeof (buf));
826 a->history_ptr = c->h->wptr;
827 HISTORY_INC (c->h, a->history_ptr);
834 ansi_parse (a, c, buf, red);
841 ansi_update (ANSI * a, Context * c)
843 ansi_history (a, c->h);
844 ansi_draw (a, &c->v->crt);