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