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