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.2  2008/02/04 02:05:06  james
14  * *** empty log message ***
15  *
16  * Revision 1.1  2008/02/03 23:31:25  james
17  * *** empty log message ***
18  *
19  */
20 #include "project.h"
21
22 void
23 ansi_write (ANSI * a, char *buf, int n)
24 {
25   write (a->fd, buf, n);
26 }
27
28
29
30 void
31 ansi_move (ANSI * a, CRT_Pos p)
32 {
33   char buf[16];
34   int n;
35   int dx = a->pos.x - p.x;
36   int dy = a->pos.y - p.y;
37
38
39   if (a->pos.x != ANSI_INVAL)
40     {
41
42       if ((!dx) && (!dy))
43         return;
44
45       if (!dy)
46         {
47           if (dx == 1)
48             {
49               ansi_write (a, "\033[C", 3);
50             }
51           else if (dx == -1)
52             {
53               ansi_write (a, "\033[D", 3);
54             }
55           else
56             {
57               n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
58               ansi_write (a, buf, n);
59             }
60         }
61       else if (!dx)
62         {
63           if (dy == -1)
64             {
65               ansi_write (a, "\033[A", 3);
66             }
67           else if (dy == 1)
68             {
69               ansi_write (a, "\033[B", 3);
70             }
71           else if (dy < 0)
72             {
73               n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
74               ansi_write (a, buf, n);
75             }
76           else
77             {
78               n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
79               ansi_write (a, buf, n);
80             }
81         }
82       else if (!p.x)
83         {
84           if (dy == 1)
85             {
86               ansi_write (a, "\033[E", 3);
87             }
88           else if (dy == -1)
89             {
90               ansi_write (a, "\033[F", 3);
91             }
92           else if (dy > 0)
93             {
94               n = snprintf (buf, sizeof (buf), "\033[%dE", -dy);
95               ansi_write (a, buf, n);
96             }
97           else
98             {
99               n = snprintf (buf, sizeof (buf), "\033[%dF", dy);
100               ansi_write (a, buf, n);
101             }
102         }
103       else
104         {
105           n = snprintf (buf, sizeof (buf), "\033[%d;%dHF", p.y + 1, p.x + 1);
106           ansi_write (a, buf, n);
107         }
108     }
109   else
110     {
111       n = snprintf (buf, sizeof (buf), "\033[%d;%dHF", p.y + 1, p.x + 1);
112       ansi_write (a, buf, n);
113     }
114
115   a->pos = p;
116 }
117
118
119 void
120 ansi_showhide_cursor (ANSI * a, int hide)
121 {
122   if (a->hide_cursor == hide)
123     return;
124
125   if (hide)
126     {
127       ansi_write (a, "\033[?25h", 6);
128     }
129   else
130     {
131       ansi_write (a, "\033[?25l", 6);
132     }
133
134   a->hide_cursor = hide;
135 }
136
137
138 void
139 ansi_force_attr_normal (ANSI * a)
140 {
141   ansi_write (a, "\033[0m", 4);
142   a->attr = CRT_ATTR_NORMAL;
143 }
144
145 void
146 ansi_set_attr (ANSI * a, int attr)
147 {
148   int dif;
149
150   dif = attr ^ a->attr;
151
152   if (!dif)
153     return;
154
155   if (attr == CRT_ATTR_NORMAL)
156     {
157       ansi_force_attr_normal (a);
158       return;
159     }
160
161   if (dif & CRT_ATTR_UNDERLINE)
162     {
163       if (attr & CRT_ATTR_UNDERLINE)
164         {
165           ansi_write (a, "\033[4m", 4);
166         }
167       else
168         {
169           ansi_write (a, "\033[24m", 5);
170         }
171     }
172   if (dif & CRT_ATTR_REVERSE)
173     {
174       if (attr & CRT_ATTR_REVERSE)
175         {
176           ansi_write (a, "\033[7m", 4);
177         }
178       else
179         {
180           ansi_write (a, "\033[27m", 5);
181         }
182     }
183   if (dif & CRT_ATTR_BOLD)
184     {
185       if (attr & CRT_ATTR_REVERSE)
186         {
187           ansi_write (a, "\033[1m", 4);
188         }
189       else
190         {
191           ansi_write (a, "\033[22m", 5);
192         }
193     }
194 }
195
196
197 void
198 ansi_render (ANSI * a, CRT_CA ca)
199 {
200   int dif;
201
202   if (ca.chr < 32)
203     ca.chr = ' ';
204   if (ca.chr > 126)
205     ca.chr = ' ';
206
207   ansi_set_attr (a, ca.attr);
208
209   ansi_write (a, &ca.chr, 1);
210
211   a->pos.x++;
212
213 /*Can't easily wrap round here as don't know size of destination screen*/
214 /*so invalidate the cached cursor position*/
215
216   if (a->pos.x >= CRT_COLS)
217     a->pos.x = ANSI_INVAL;
218
219 }
220
221 void
222 ansi_cls (ANSI * a)
223 {
224   CRT_Pos p = { 0 };
225
226   crt_cls (&a->crt);
227   ansi_force_attr_normal (a);
228   ansi_move (a, p);
229   ansi_write (a, "\033[2J", 4);
230 /*different emulators leave cursor in different places after cls differently*/
231   a->pos.x = ANSI_INVAL;
232 }
233
234
235 void
236 ansi_draw (ANSI * a, CRT * c)
237 {
238   CRT_Pos p;
239   int o;
240
241   ansi_showhide_cursor (a, 1);
242
243   for (p.y = 0; p.y < CRT_ROWS; ++p.y)
244     {
245       if (p.y >= a->size.y)
246         continue;
247       o = CRT_ADDR (p.y, 0);
248       for (p.x = 0; p.x < CRT_COLS; ++p.x, ++o)
249         {
250           if (p.x >= a->size.x)
251             continue;
252           if (crt_ca_cmp (a->crt.screen[o], c->screen[o]))
253             {
254               a->crt.screen[o] = c->screen[o];
255
256               ansi_move (a, p);
257               ansi_render (a, a->crt.screen[o]);
258             }
259         }
260     }
261
262   a->crt.pos = c->pos;
263   ansi_move (a, a->crt.pos);
264
265   a->crt.hide_cursor = c->hide_cursor;
266   ansi_showhide_cursor (a, a->crt.hide_cursor);
267 }
268
269 void
270 ansi_reset (ANSI * a)
271 {
272   ansi_write (a, "\033[c", 3);
273
274   a->pos.x = ANSI_INVAL;
275   a->hide_cursor = ANSI_INVAL;
276
277   crt_reset (&a->crt);
278
279   ansi_cls (a);
280   ansi_draw (a, &a->crt);
281 }