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