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