chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / ansi.c
1 /*
2  * ansi.c:
3  *
4  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
5  * All rights reserved.
6  *
7  */
8
9 static char rcsid[] = "$Id$";
10
11 /*
12  * $Log$
13  * Revision 1.38  2008/03/02 10:27:24  james
14  * *** empty log message ***
15  *
16  * Revision 1.37  2008/02/29 22:50:29  james
17  * *** empty log message ***
18  *
19  * Revision 1.36  2008/02/28 22:00:42  james
20  * *** empty log message ***
21  *
22  * Revision 1.35  2008/02/28 16:57:51  james
23  * *** empty log message ***
24  *
25  * Revision 1.34  2008/02/27 09:42:53  james
26  * *** empty log message ***
27  *
28  * Revision 1.33  2008/02/27 09:42:21  james
29  * *** empty log message ***
30  *
31  * Revision 1.32  2008/02/26 23:56:12  james
32  * *** empty log message ***
33  *
34  * Revision 1.31  2008/02/26 23:23:17  james
35  * *** empty log message ***
36  *
37  * Revision 1.30  2008/02/24 00:42:53  james
38  * *** empty log message ***
39  *
40  * Revision 1.29  2008/02/23 11:48:37  james
41  * *** empty log message ***
42  *
43  * Revision 1.28  2008/02/22 17:07:00  james
44  * *** empty log message ***
45  *
46  * Revision 1.27  2008/02/20 22:54:22  staffcvs
47  * *** empty log message ***
48  *
49  * Revision 1.26  2008/02/20 20:16:07  james
50  * *** empty log message ***
51  *
52  * Revision 1.25  2008/02/20 19:44:37  james
53  * @@
54  *
55  * Revision 1.24  2008/02/20 19:36:06  james
56  * @@
57  *
58  * Revision 1.23  2008/02/20 19:25:09  james
59  * *** empty log message ***
60  *
61  * Revision 1.22  2008/02/15 03:32:07  james
62  * *** empty log message ***
63  *
64  * Revision 1.21  2008/02/14 02:46:44  james
65  * *** empty log message ***
66  *
67  * Revision 1.20  2008/02/14 01:55:57  james
68  * *** empty log message ***
69  *
70  * Revision 1.19  2008/02/13 16:57:29  james
71  * *** empty log message ***
72  *
73  * Revision 1.18  2008/02/13 09:12:21  james
74  * *** empty log message ***
75  *
76  * Revision 1.17  2008/02/13 01:08:18  james
77  * *** empty log message ***
78  *
79  * Revision 1.16  2008/02/07 13:22:51  james
80  * *** empty log message ***
81  *
82  * Revision 1.15  2008/02/07 13:19:48  james
83  * *** empty log message ***
84  *
85  * Revision 1.14  2008/02/07 12:21:16  james
86  * *** empty log message ***
87  *
88  * Revision 1.13  2008/02/07 12:16:04  james
89  * *** empty log message ***
90  *
91  * Revision 1.12  2008/02/07 11:32:41  james
92  * *** empty log message ***
93  *
94  * Revision 1.11  2008/02/07 11:11:14  staffcvs
95  * *** empty log message ***
96  *
97  * Revision 1.10  2008/02/07 01:02:52  james
98  * *** empty log message ***
99  *
100  * Revision 1.9  2008/02/07 00:43:27  james
101  * *** empty log message ***
102  *
103  * Revision 1.8  2008/02/07 00:39:13  james
104  * *** empty log message ***
105  *
106  * Revision 1.7  2008/02/06 20:26:57  james
107  * *** empty log message ***
108  *
109  * Revision 1.6  2008/02/06 17:53:28  james
110  * *** empty log message ***
111  *
112  * Revision 1.5  2008/02/06 15:53:22  james
113  * *** empty log message ***
114  *
115  * Revision 1.4  2008/02/04 20:23:55  james
116  * *** empty log message ***
117  *
118  * Revision 1.3  2008/02/04 05:45:55  james
119  * ::
120  *
121  * Revision 1.2  2008/02/04 02:05:06  james
122  * *** empty log message ***
123  *
124  * Revision 1.1  2008/02/03 23:31:25  james
125  * *** empty log message ***
126  *
127  */
128 #include "project.h"
129
130
131 static void
132 ansi_move (ANSI * a, CRT_Pos p)
133 {
134   char buf[16];
135   int n;
136   int dx = p.x - a->pos.x;
137   int dy = p.y - a->pos.y;
138
139 //  a->pos.x = ANSI_INVAL;
140
141   if (a->pos.x != ANSI_INVAL)
142     {
143
144       if ((!dx) && (!dy))
145         return;
146
147       if (!dy)
148         {
149           if (dx == 1)
150             {
151               a->terminal->xmit (a->terminal, "\033[C", 3);
152             }
153           else if (dx == -1)
154             {
155               a->terminal->xmit (a->terminal, "\033[D", 3);
156             }
157           else
158             {
159               n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
160               a->terminal->xmit (a->terminal, buf, n);
161             }
162         }
163       else if (!dx)
164         {
165           if (dy == -1)
166             {
167               a->terminal->xmit (a->terminal, "\033[A", 3);
168             }
169           else if (dy == 1)
170             {
171               a->terminal->xmit (a->terminal, "\033[B", 3);
172             }
173           else if (dy < 0)
174             {
175               n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
176               a->terminal->xmit (a->terminal, buf, n);
177             }
178           else
179             {
180               n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
181               a->terminal->xmit (a->terminal, buf, n);
182             }
183         }
184       else if (!p.x)
185         {
186           if (dy == 1)
187             {
188               a->terminal->xmit (a->terminal, "\033[E", 3);
189             }
190           else if (dy == -1)
191             {
192               a->terminal->xmit (a->terminal, "\033[F", 3);
193             }
194           else if (dy > 0)
195             {
196               n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
197               a->terminal->xmit (a->terminal, buf, n);
198             }
199           else
200             {
201               n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
202               a->terminal->xmit (a->terminal, buf, n);
203             }
204         }
205       else
206         {
207           n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
208           a->terminal->xmit (a->terminal, buf, n);
209         }
210     }
211   else
212     {
213       n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
214       a->terminal->xmit (a->terminal, buf, n);
215     }
216
217   a->pos = p;
218 }
219
220
221 static void
222 ansi_showhide_cursor (ANSI * a, int hide)
223 {
224   if (a->hide_cursor == hide)
225     return;
226
227   if (hide)
228     {
229       a->terminal->xmit (a->terminal, "\033[?25l", 6);
230     }
231   else
232     {
233       a->terminal->xmit (a->terminal, "\033[?25h", 6);
234     }
235
236   a->hide_cursor = hide;
237 }
238
239
240 static void
241 ansi_force_attr_normal (ANSI * a)
242 {
243   a->terminal->xmit (a->terminal, "\033[0m", 4);
244   a->attr = CRT_ATTR_NORMAL;
245   a->color = ANSI_INVAL;
246 }
247
248 static void
249 ansi_set_color (ANSI * a, int color)
250 {
251   int dif;
252   char buf[16];
253   int i;
254   int fg, bg;
255
256   if ((a->color == ANSI_INVAL) || (color != a->color))
257     {
258       fg = CRT_COLOR_FG (color);
259       bg = CRT_COLOR_BG (color);
260
261       if (fg & CRT_COLOR_INTENSITY)
262         {
263           fg += 90;
264         }
265       else
266         {
267           fg += 30;
268         }
269
270       if (bg & CRT_COLOR_INTENSITY)
271         {
272           bg += 100;
273         }
274       else
275         {
276           bg += 40;
277         }
278
279       i = sprintf (buf, "\033[%d;%dm", fg, bg);
280 #if 0
281       fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
282 #endif
283
284       a->terminal->xmit (a->terminal, buf, i);
285       a->color = color;
286     }
287 }
288
289 static void
290 ansi_set_attr (ANSI * a, int attr)
291 {
292   int dif;
293
294   dif = attr ^ a->attr;
295
296   if (!dif)
297     return;
298
299   a->attr = attr;
300
301 #if 0
302   if (attr == CRT_ATTR_NORMAL)
303     {
304       ansi_force_attr_normal (a);
305       return;
306     }
307 #endif
308
309   if (dif & CRT_ATTR_UNDERLINE)
310     {
311       if (attr & CRT_ATTR_UNDERLINE)
312         {
313           a->terminal->xmit (a->terminal, "\033[4m", 4);
314         }
315       else
316         {
317           a->terminal->xmit (a->terminal, "\033[24m", 5);
318         }
319     }
320   if (dif & CRT_ATTR_REVERSE)
321     {
322       if (attr & CRT_ATTR_REVERSE)
323         {
324           a->terminal->xmit (a->terminal, "\033[7m", 4);
325         }
326       else
327         {
328           a->terminal->xmit (a->terminal, "\033[27m", 5);
329         }
330     }
331   if (dif & CRT_ATTR_BOLD)
332     {
333       if (attr & CRT_ATTR_BOLD)
334         {
335           a->terminal->xmit (a->terminal, "\033[1m", 4);
336         }
337       else
338         {
339           a->terminal->xmit (a->terminal, "\033[21m", 5);
340           a->terminal->xmit (a->terminal, "\033[22m", 5);
341         }
342     }
343
344 }
345
346 static void
347 ascii_emit (TTY * t, uint32_t ch)
348 {
349   int i;
350
351 /*Some quick obvious subsititons for quotation marks*/
352   switch (ch)
353     {
354     case 0x2018:
355       ch = '`';
356       break;
357     case 0x2019:
358       ch = '\'';
359       break;
360     case 0x201c:
361     case 0x201d:
362       ch = '"';
363       break;
364     }
365
366 /*Short cut the easy stuff*/
367   if (ch < 0x7f)
368     {
369       uint8_t c = ch;
370       t->xmit (t, &c, 1);
371       return;
372     }
373
374   for (i = 0; i < VT102_CHARSET_SIZE; ++i)
375     {
376       if (vt102_charset_gl[i] == ch)
377         {
378           uint8_t c[3] = { 016, i, 017 };
379           t->xmit (t, &c, 3);
380           return;
381         }
382     }
383
384   t->xmit (t, "?", 1);
385
386 }
387
388 static void
389 ansi_render (ANSI * a, CRT_CA ca)
390 {
391   int dif;
392
393   if ((ca.chr < VT102_CHARSET_SIZE) && (vt102_charset_c0[ca.chr]))
394     ca.chr = vt102_charset_c0[ca.chr];
395
396   if ((ca.chr >= 0x80) && (ca.chr < 0xa0))
397     ca.chr = ' ';
398
399   ansi_set_attr (a, ca.attr);
400   ansi_set_color (a, ca.color);
401
402   if (a->utf8)
403     utf8_emit (a->terminal, ca.chr);
404   else
405     ascii_emit (a->terminal, ca.chr);
406
407   a->pos.x++;
408
409 /*Can't easily wrap round here as don't know size of destination screen*/
410 /*so invalidate the cached cursor position*/
411
412   if (a->pos.x >= a->size.x)
413     a->pos.x = ANSI_INVAL;
414
415 }
416
417 static void
418 ansi_cls (ANSI * a)
419 {
420   CRT_Pos p = { 0 };
421
422   crt_cls (&a->crt);
423
424   ansi_set_attr (a, CRT_ATTR_NORMAL);
425   ansi_set_color (a, CRT_COLOR_NORMAL);
426   ansi_move (a, p);
427   a->terminal->xmit (a->terminal, "\033[2J", 4);
428 /*different emulators leave cursor in different places after cls differently*/
429   a->pos.x = ANSI_INVAL;
430 }
431
432 #if 0
433 int
434 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
435 {
436   char buf[16];
437   int i;
438   if (s.x)
439     return -1;
440   if (e.x != (CRT_COLS - 1))
441     return -1;
442   if (s.y)
443     return -1;
444   if (s.y >= a->size.y)
445     return -1;
446
447   ansi_showhide_cursor (a, 1);
448   i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
449   a->terminal->xmit (a->terminal, buf, i);
450   i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
451   a->terminal->xmit (a->terminal, buf, i);
452   a->terminal->xmit (a->terminal, "\033D", 2);
453   a->terminal->xmit (a->terminal, "\033[r", 3);
454
455   s.y = e.y;
456   crt_erase (&a->crt, s, e, 1);
457
458   a->pos.x = ANSI_INVAL;
459
460   return 0;
461 }
462
463
464 static void
465 ansi_spot_scroll_up (ANSI * a, CRT * c)
466 {
467   int l, n, p;
468
469   l = c->sh.e.x - c->sh.s.x;
470   l++;
471   l *= sizeof (CRT_CA);
472
473   n = c->sh.e.y - c->sh.s.y;
474   p = CRT_ADDR_POS (&c->sh.s);
475
476   while (n--)
477     {
478       if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
479         return;
480       p += CRT_COLS;
481     }
482
483   if (ansi_scroll_up (a, c->sh.s, c->sh.e))
484     return;
485
486   n = c->sh.e.y - c->sh.s.y;
487   p = CRT_ADDR_POS (&c->sh.s);
488
489   while (n--)
490     {
491       memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
492       p += CRT_COLS;
493     }
494
495   c->sh.dir = 0;                //FIXME: horrid hack
496
497 }
498
499 static void
500 ansi_spot_scroll (ANSI * a, CRT * c)
501 {
502
503   switch (c->sh.dir)
504     {
505     case -1:
506 /*we only care about up for the moment */
507       ansi_spot_scroll_up (a, c);
508       break;
509     }
510
511   return;
512 }
513
514 #endif
515
516
517 static void
518 ansi_draw_line (ANSI * a, CRT_CA * cap, int y)
519 {
520   CRT_Pos p = { 0, y };
521   CRT_CA *acap = &a->crt.screen[CRT_ADDR_POS (&p)];
522
523   for (p.x = 0; p.x < a->crt.size.x; ++p.x)
524     {
525       if (p.x >= a->size.x)
526         continue;
527
528       if (crt_ca_cmp (*acap, *cap))
529         {
530           ansi_showhide_cursor (a, 1);
531           *acap = *cap;
532
533           ansi_move (a, p);
534           ansi_render (a, *acap);
535         }
536
537       acap++;
538       cap++;
539     }
540 }
541
542 static void
543 ansi_resize_check (ANSI * a, CRT_Pos * size)
544 {
545
546   if ((size && crt_pos_cmp (a->crt.size, *size))
547       || crt_pos_cmp (a->terminal->size, a->size))
548     {
549
550       terminal_getsize (a->terminal);
551
552       a->size = a->terminal->size;
553
554       a->pos.x = ANSI_INVAL;
555       a->hide_cursor = ANSI_INVAL;
556
557       crt_reset (&a->crt);
558
559       if (size)
560         a->crt.size = *size;
561
562 // FIXME: -- echos back crap?
563 //  a->terminal->xmit (a->terminal, "\033[c", 3);
564
565 // maybe - issue 132 column command if we're 132?
566
567       ansi_cls (a);
568       a->terminal->xmit (a->terminal, "\033=", 2);
569       a->terminal->xmit (a->terminal, "\033[?6l", 5);
570       a->terminal->xmit (a->terminal, "\033[r", 3);
571       if (a->utf8)
572         {
573           a->terminal->xmit (a->terminal, "\033%G", 3);
574         }
575       else
576         {
577           a->terminal->xmit (a->terminal, "\033(B", 3);
578           a->terminal->xmit (a->terminal, "\033)0", 3);
579           a->terminal->xmit (a->terminal, "\017", 1);
580         }
581
582     }
583 }
584
585 /*if they haven't then ansi_draw will patch it up*/
586
587 static void
588 ansi_history (ANSI * a, History * h)
589 {
590   char buf[32];
591   int i;
592   int guess_scroll;
593 /*Do we need to catch up on history?*/
594
595   if (a->history_ptr == h->wptr)
596     return;
597   ansi_resize_check (a, NULL);
598
599   if ((a->size.x < a->crt.size.x) || (a->size.y < a->crt.size.y))
600     return;
601
602   guess_scroll = a->crt.size.y - 1; /*Bototm line should be a status line */
603
604
605   ansi_force_attr_normal (a);
606   ansi_set_color (a, CRT_COLOR_NORMAL);
607
608   i = sprintf (buf, "\033[%d;%dr", 1, guess_scroll);
609   a->terminal->xmit (a->terminal, buf, i);
610
611
612   while (a->history_ptr != h->wptr)
613     {
614
615       History_ent *e = &h->lines[a->history_ptr];
616
617       HISTORY_INC (h, a->history_ptr);
618
619       if (!e->valid)
620         continue;
621
622       /*If so write the line ot the top of the screen */
623       ansi_draw_line (a, e->line, 0);
624
625
626       /*Roll guess_scroll lines up putting the top line into the xterm's history */
627
628
629       /*Make extra lines a predictable colour */
630       ansi_set_color (a, CRT_COLOR_NORMAL);
631
632       ansi_showhide_cursor (a, 1);
633       i = sprintf (buf, "\033[%d;%dH", guess_scroll, 1);
634       a->terminal->xmit (a->terminal, buf, i);
635       a->terminal->xmit (a->terminal, "\033D", 2);
636       a->pos.x = ANSI_INVAL;
637
638       /*now do the same in our image of the screen */
639
640       {
641         CRT_Pos s = { 0 }
642         , e =
643         {
644         0};
645
646         /*scroll lines up */
647         for (s.y++; s.y < guess_scroll; s.y++, e.y++)
648           {
649             memcpy (&a->crt.screen[CRT_ADDR_POS (&e)],
650                     &a->crt.screen[CRT_ADDR_POS (&s)],
651                     sizeof (CRT_CA) * a->crt.size.x);
652           }
653
654         /* erase new line */
655         s.y = e.y;
656         e.x = CRT_COLS - 1;
657         crt_erase (&a->crt, s, e, 1, CRT_COLOR_NORMAL);
658       }
659
660     }
661 /*reset margins*/
662   a->terminal->xmit (a->terminal, "\033[r", 3);
663   a->pos.x = ANSI_INVAL;
664
665 }
666
667
668
669
670 static void
671 ansi_draw (ANSI * a, CRT * c)
672 {
673   CRT_Pos p;
674   int o;
675   int hidden_cursor = 0;
676
677   ansi_resize_check (a, &c->size);
678
679
680   for (p.y = 0; p.y < a->crt.size.y; ++p.y)
681     {
682       if (p.y >= a->size.y)
683         continue;
684
685       ansi_draw_line (a, &c->screen[CRT_ADDR (p.y, 0)], p.y);
686
687     }
688
689
690   if ((c->size.x > a->size.x) || (c->size.y > a->size.y))
691     {
692       char msg[1024];           // = "Window is too small";
693       int i;
694       p.x = 0;
695       p.y = 0;
696
697       i =
698         sprintf (msg, "Window too small (%dx%d need %dx%d)", a->size.x,
699                  a->size.y, c->size.x, c->size.y);
700
701       ansi_showhide_cursor (a, 1);
702       ansi_set_attr (a, CRT_ATTR_REVERSE);
703       ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
704       ansi_move (a, p);
705
706       a->terminal->xmit (a->terminal, msg, i);
707       a->pos.x = ANSI_INVAL;
708     }
709
710
711   if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
712     {
713       ansi_showhide_cursor (a, 1);
714       return;
715     }
716
717   a->crt.pos = c->pos;
718   ansi_move (a, a->crt.pos);
719
720   a->crt.hide_cursor = c->hide_cursor;
721   ansi_showhide_cursor (a, a->crt.hide_cursor);
722 }
723
724 static void
725 ansi_reset (ANSI * a, CRT * c)
726 {
727   a->size.x = -1;
728   ansi_draw (a, c ? c : &a->crt);
729 }
730
731 static void
732 ansi_terminal_reset (ANSI * a)
733 {
734   CRT_Pos p = { 0, a->crt.size.y };
735   ansi_force_attr_normal (a);
736
737   ansi_move (a, p);
738 }
739
740
741 int ansi_key(ANSI *a,Context *c,int key)
742 {
743
744   if (!c->d)
745     return c->k->key (c->k, c, key);
746
747   cmd_show_status (c->d, c);
748
749
750   if (c->d->active) {
751         if (key == CMD_KEY) {
752     return cmd_deactivate (c->d, c);
753         }else {
754     return cmd_key (c->d, c,a, key);
755         }
756   } else if (key == CMD_KEY)
757     return cmd_activate (c->d, c);
758   }
759
760
761   return c->k->key (c->k, c, key);
762 }
763
764
765 static void
766 ansi_flush_escape (ANSI * a, Context * c)
767 {
768   ANSI_Parser *p = &a->parser;
769   int i;
770
771   for (i = 0; i < p->escape_ptr; ++i)
772     {
773       ansi_key (a, c, p->escape_buf[i]);
774     }
775
776   p->escape_ptr = 0;
777   p->in_escape = 0;
778 }
779
780 static void
781 ansi_parse_deckey (ANSI * a, Context * c)
782 {
783   ANSI_Parser *p = &a->parser;
784   if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
785     {
786       ansi_flush_escape (a, c);
787       return;
788     }
789
790   if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
791     {
792       ansi_key (a, c, KEY_UP + (p->escape_buf[2] - 'A'));
793     }
794   else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
795     {
796       ansi_key (a, c, KEY_154 + (p->escape_buf[2] - 'a'));
797     }
798   else
799     {
800       ansi_flush_escape (a, c);
801       return;
802     }
803   p->in_escape = 0;
804   p->escape_ptr = 0;
805 }
806
807 static void
808 ansi_parse_ansikey (ANSI * a, Context * c)
809 {
810   ANSI_Parser *p = &a->parser;
811
812   if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
813     {
814       ansi_flush_escape (a, c);
815       return;
816     }
817   if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
818     {
819       ansi_key (a, c, KEY_180 + (p->escape_buf[2] - '0'));
820     }
821   else
822     {
823       ansi_flush_escape (a, c);
824       return;
825     }
826
827   p->in_escape = 0;
828   p->escape_ptr = 0;
829 }
830
831
832
833 static void
834 ansi_parse_escape (ANSI * a, Context * c)
835 {
836   ANSI_Parser *p = &a->parser;
837   switch (p->escape_ptr)
838     {
839     case 0:
840     case 1:
841       return;
842     case 2:
843       switch (p->escape_buf[1])
844         {
845         case '[':
846         case 'O':
847           break;
848         default:
849           ansi_flush_escape (a, c);
850         }
851       break;
852     case 3:
853       switch (p->escape_buf[1])
854         {
855         case 'O':
856           ansi_parse_deckey (a, c);
857           break;
858         case '[':
859           if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
860             ansi_parse_deckey (a, c);
861           break;
862         default:
863           ansi_flush_escape (a, c);
864         }
865       break;
866     case 4:
867       switch (p->escape_buf[1])
868         {
869         case '[':
870           ansi_parse_ansikey (a, c);
871           break;
872         default:
873           ansi_flush_escape (a, c);
874         }
875       break;
876     case 5:
877       ansi_flush_escape (a, c);
878     }
879 }
880
881
882 static void
883 ansi_check_escape (ANSI * a, Context * c)
884 {
885   ANSI_Parser *p = &a->parser;
886   struct timeval now, diff;
887   gettimeofday (&now, NULL);
888   timersub (&now, &p->last_escape, &diff);
889
890 #if 0
891   fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
892            p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
893 #endif
894
895   if (!p->in_escape)
896     return;
897
898
899   /*Time up? */
900   if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
901     ansi_flush_escape (a, c);
902
903 }
904
905
906 static void
907 ansi_parse_char (ANSI * a, Context * c, int ch)
908 {
909   ANSI_Parser *p = &a->parser;
910
911
912 /*See if it's time to flush the escape*/
913   ansi_check_escape (a, c);
914
915   if (ch == 033)
916     {
917       if (p->in_escape)
918         ansi_flush_escape (a, c);
919
920       p->in_escape++;
921       p->escape_ptr = 0;
922       gettimeofday (&p->last_escape, NULL);
923     }
924
925   if (p->in_escape)
926     {
927       p->escape_buf[p->escape_ptr++] = ch;
928       ansi_parse_escape (a, c);
929     }
930   else
931     {
932       ansi_key (a, c, ch);
933     }
934
935 }
936
937 static void
938 ansi_parse (ANSI * a, Context * c, char *buf, int len)
939 {
940   while (len--)
941     ansi_parse_char (a, c, *(buf++));
942 }
943
944 int
945 ansi_dispatch (ANSI * a, Context * c)
946 {
947   char buf[1024];
948   int red;
949
950   ansi_check_escape (a, c);
951
952
953   if (!a->terminal)
954     return 0;
955
956   red = a->terminal->recv (a->terminal, buf, sizeof (buf));
957   if (red <= 0)
958     return red;
959
960 #if 0
961   if (*buf == 3)
962     return -1;
963 #endif
964
965 #if 0
966   if (*buf == 2)
967     {
968 #if  0
969       a->history_ptr = c->h->wptr;
970       HISTORY_INC (c->h, a->history_ptr);
971 #endif
972       return -1;
973     }
974 #endif
975
976
977   ansi_parse (a, c, buf, red);
978
979   return 0;
980 }
981
982
983 static void
984 ansi_update (ANSI * a, Context * c)
985 {
986   ansi_history (a, c->h);
987   ansi_draw (a, &c->v->crt);
988   tty_length(a->terminal,c->v->crt.size.y);
989 }
990
991 static void
992 ansi_free (ANSI * a)
993 {
994   a->terminal_reset (a);
995   if (a->terminal)
996     a->terminal->close (a->terminal);
997
998   free (a);
999
1000 }
1001
1002 ANSI *
1003 ansi_new_from_terminal (TTY * t, int utf8)
1004 {
1005   ANSI *ret;
1006
1007   ret = malloc (sizeof (ANSI));
1008   memset (ret, 0, sizeof (ANSI));
1009
1010   ret->terminal = t;
1011
1012   ret->utf8 = utf8;
1013   ret->update = ansi_update;
1014   ret->reset = ansi_reset;
1015   ret->terminal_reset = ansi_terminal_reset;
1016   ret->close = ansi_free;
1017   ret->dispatch = ansi_dispatch;
1018
1019   return ret;
1020 }