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