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