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