1 | /* |
---|
2 | * libcaca Colour ASCII-Art library |
---|
3 | * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: driver_slang.c 2299 2008-04-19 12:42:50Z sam $ |
---|
7 | * |
---|
8 | * This library is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | * This file contains the libcaca SLang input and output driver |
---|
17 | */ |
---|
18 | |
---|
19 | #include "config.h" |
---|
20 | |
---|
21 | #if defined(USE_SLANG) |
---|
22 | |
---|
23 | #if defined(HAVE_SLANG_SLANG_H) |
---|
24 | # include <slang/slang.h> |
---|
25 | #else |
---|
26 | # include <slang.h> |
---|
27 | #endif |
---|
28 | |
---|
29 | #include <stdlib.h> |
---|
30 | #include <string.h> |
---|
31 | |
---|
32 | #if defined(HAVE_SIGNAL_H) |
---|
33 | # include <signal.h> |
---|
34 | #endif |
---|
35 | |
---|
36 | #include "cucul.h" |
---|
37 | #include "caca.h" |
---|
38 | #include "caca_internals.h" |
---|
39 | |
---|
40 | /* |
---|
41 | * Global variables |
---|
42 | */ |
---|
43 | |
---|
44 | /* Tables generated by tools/optipal.c */ |
---|
45 | static int const slang_palette[2*16*16] = |
---|
46 | { |
---|
47 | 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, |
---|
48 | 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 0, 0, 8, |
---|
49 | 8, 7, 7, 8, 15, 7, 7, 15, 15, 9, 9, 15, 1, 9, 9, 1, |
---|
50 | 7, 9, 9, 7, 8, 1, 1, 8, 0, 1, 15, 10, 10, 15, 2, 10, |
---|
51 | 10, 2, 7, 10, 10, 7, 8, 2, 2, 8, 0, 2, 15, 11, 11, 15, |
---|
52 | 3, 11, 11, 3, 7, 11, 11, 7, 8, 3, 3, 8, 0, 3, 15, 12, |
---|
53 | 12, 15, 4, 12, 12, 4, 7, 12, 12, 7, 8, 4, 4, 8, 0, 4, |
---|
54 | 15, 13, 13, 15, 5, 13, 13, 5, 7, 13, 13, 7, 8, 5, 5, 8, |
---|
55 | 0, 5, 15, 14, 14, 15, 6, 14, 14, 6, 7, 14, 14, 7, 8, 6, |
---|
56 | 6, 8, 0, 6, 4, 6, 6, 4, 12, 14, 14, 12, 6, 2, 2, 6, |
---|
57 | 14, 10, 10, 14, 2, 3, 3, 2, 10, 11, 11, 10, 3, 1, 1, 3, |
---|
58 | 11, 9, 9, 11, 1, 5, 5, 1, 9, 13, 13, 9, 5, 4, 4, 5, |
---|
59 | 13, 12, 12, 13, 4, 14, 6, 12, 12, 6, 14, 4, 6, 10, 2, 14, |
---|
60 | 14, 2, 10, 6, 2, 11, 3, 10, 10, 3, 11, 2, 3, 9, 1, 11, |
---|
61 | 11, 1, 9, 3, 1, 13, 5, 9, 9, 5, 13, 1, 5, 12, 4, 13, |
---|
62 | 13, 4, 12, 5, 0, 7, 0, 15, 15, 8, 8, 15, 15, 1, 7, 1, |
---|
63 | 1, 6, 2, 5, 3, 4, 4, 3, 5, 2, 6, 1, 0, 0, 1, 1, |
---|
64 | 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 2, 2, 3, 3, |
---|
65 | 4, 4, 5, 5, 6, 6, 7, 7, 14, 9, 1, 15, 8, 9, 8, 8, |
---|
66 | 9, 9, 1, 7, 0, 9, 9, 8, 6, 9, 13, 10, 2, 15, 8, 10, |
---|
67 | 7, 2, 15, 2, 2, 7, 0, 10, 10, 8, 5, 10, 12, 11, 3, 15, |
---|
68 | 8, 11, 7, 3, 15, 3, 3, 7, 0, 11, 11, 8, 4, 11, 11, 12, |
---|
69 | 4, 15, 8, 12, 7, 4, 15, 4, 4, 7, 0, 12, 12, 8, 3, 12, |
---|
70 | 10, 13, 5, 15, 8, 13, 7, 5, 15, 5, 5, 7, 0, 13, 13, 8, |
---|
71 | 2, 13, 9, 14, 6, 15, 8, 14, 7, 6, 15, 6, 6, 7, 0, 14, |
---|
72 | 14, 8, 1, 14, 5, 6, 2, 4, 13, 14, 10, 12, 4, 2, 3, 6, |
---|
73 | 12, 10, 11, 14, 6, 3, 1, 2, 14, 11, 9, 10, 2, 1, 5, 3, |
---|
74 | 10, 9, 13, 11, 3, 5, 4, 1, 11, 13, 12, 9, 1, 4, 6, 5, |
---|
75 | 9, 12, 14, 13, 5, 14, 2, 12, 13, 6, 10, 4, 4, 10, 3, 14, |
---|
76 | 12, 2, 11, 6, 6, 11, 1, 10, 14, 3, 9, 2, 2, 9, 5, 11, |
---|
77 | 10, 1, 13, 3, 3, 13, 4, 9, 11, 5, 12, 1, 1, 12, 6, 13, |
---|
78 | 9, 4, 14, 5, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, |
---|
79 | }; |
---|
80 | |
---|
81 | static int const slang_assoc[16*16] = |
---|
82 | { |
---|
83 | 134, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
---|
84 | 28, 135, 214, 86, 219, 91, 133, 127, 26, 23, 240, 112, 245, 117, 141, 126, |
---|
85 | 37, 211, 142, 83, 206, 132, 78, 160, 35, 237, 32, 109, 232, 140, 104, 161, |
---|
86 | 46, 87, 82, 143, 131, 215, 210, 169, 44, 113, 108, 41, 139, 241, 236, 170, |
---|
87 | 55, 222, 203, 130, 144, 94, 75, 178, 53, 248, 229, 138, 50, 120, 101, 179, |
---|
88 | 64, 90, 129, 218, 95, 145, 223, 187, 62, 116, 137, 244, 121, 59, 249, 188, |
---|
89 | 73, 128, 79, 207, 74, 202, 146, 196, 71, 136, 105, 233, 100, 228, 68, 197, |
---|
90 | 122, 153, 162, 171, 180, 189, 198, 147, 16, 25, 34, 43, 52, 61, 70, 18, |
---|
91 | 15, 27, 36, 45, 54, 63, 72, 17, 151, 155, 164, 173, 182, 191, 200, 124, |
---|
92 | 154, 22, 238, 110, 243, 115, 156, 24, 150, 152, 216, 88, 221, 93, 148, 20, |
---|
93 | 163, 235, 31, 107, 230, 165, 102, 33, 159, 213, 250, 85, 208, 157, 80, 29, |
---|
94 | 172, 111, 106, 40, 174, 239, 234, 42, 168, 89, 84, 251, 166, 217, 212, 38, |
---|
95 | 181, 246, 227, 183, 49, 118, 99, 51, 177, 224, 205, 175, 252, 96, 77, 47, |
---|
96 | 190, 114, 192, 242, 119, 58, 247, 60, 186, 92, 184, 220, 97, 253, 225, 56, |
---|
97 | 199, 201, 103, 231, 98, 226, 67, 69, 195, 193, 81, 209, 76, 204, 254, 65, |
---|
98 | 123, 149, 158, 167, 176, 185, 194, 19, 125, 21, 30, 39, 48, 57, 66, 255, |
---|
99 | }; |
---|
100 | |
---|
101 | /* |
---|
102 | * Local functions |
---|
103 | */ |
---|
104 | static void slang_init_palette(void); |
---|
105 | static void slang_write_utf32(uint32_t); |
---|
106 | |
---|
107 | #if defined(HAVE_SIGNAL) |
---|
108 | static RETSIGTYPE sigwinch_handler(int); |
---|
109 | static caca_display_t *sigwinch_d; /* FIXME: we ought to get rid of this */ |
---|
110 | #endif |
---|
111 | #if defined(HAVE_GETENV) && defined(HAVE_PUTENV) |
---|
112 | static void slang_install_terminal(caca_display_t *); |
---|
113 | static void slang_uninstall_terminal(caca_display_t *); |
---|
114 | #endif |
---|
115 | |
---|
116 | struct driver_private |
---|
117 | { |
---|
118 | char *term; |
---|
119 | }; |
---|
120 | |
---|
121 | static int slang_init_graphics(caca_display_t *dp) |
---|
122 | { |
---|
123 | dp->drv.p = malloc(sizeof(struct driver_private)); |
---|
124 | |
---|
125 | #if defined(HAVE_GETENV) && defined(HAVE_PUTENV) |
---|
126 | slang_install_terminal(dp); |
---|
127 | #endif |
---|
128 | |
---|
129 | #if defined(HAVE_SIGNAL) |
---|
130 | sigwinch_d = dp; |
---|
131 | signal(SIGWINCH, sigwinch_handler); |
---|
132 | #endif |
---|
133 | |
---|
134 | _caca_set_term_title("caca for S-Lang"); |
---|
135 | |
---|
136 | /* Initialise slang library */ |
---|
137 | SLsig_block_signals(); |
---|
138 | SLtt_get_terminfo(); |
---|
139 | |
---|
140 | if(SLkp_init() == -1) |
---|
141 | { |
---|
142 | SLsig_unblock_signals(); |
---|
143 | return -1; |
---|
144 | } |
---|
145 | |
---|
146 | SLang_init_tty(-1, 0, 1); |
---|
147 | |
---|
148 | if(SLsmg_init_smg() == -1) |
---|
149 | { |
---|
150 | SLsig_unblock_signals(); |
---|
151 | return -1; |
---|
152 | } |
---|
153 | |
---|
154 | SLsig_unblock_signals(); |
---|
155 | |
---|
156 | SLsmg_cls(); |
---|
157 | SLtt_set_cursor_visibility(0); |
---|
158 | SLkp_define_keysym("\e[M", 1001); |
---|
159 | SLtt_set_mouse_mode(1, 0); |
---|
160 | SLsmg_refresh(); |
---|
161 | |
---|
162 | /* Disable scrolling so that hashmap scrolling optimization code |
---|
163 | * does not cause ugly refreshes due to slow terminals */ |
---|
164 | SLtt_Term_Cannot_Scroll = 1; |
---|
165 | |
---|
166 | slang_init_palette(); |
---|
167 | |
---|
168 | #if defined(VMS) || defined(REAL_UNIX_SYSTEM) |
---|
169 | /* Disable alt charset support so that we get a chance to have all |
---|
170 | * 256 colour pairs */ |
---|
171 | SLtt_Has_Alt_Charset = 0; |
---|
172 | #endif |
---|
173 | |
---|
174 | #ifdef HAVE_SLSMG_UTF8_ENABLE |
---|
175 | SLsmg_utf8_enable(1); /* 1 == force, 0 == disable, -1 == autodetect */ |
---|
176 | SLtt_utf8_enable(1); |
---|
177 | #endif |
---|
178 | |
---|
179 | dp->resize.allow = 1; |
---|
180 | cucul_set_canvas_size(dp->cv, SLtt_Screen_Cols, SLtt_Screen_Rows); |
---|
181 | dp->resize.allow = 0; |
---|
182 | |
---|
183 | return 0; |
---|
184 | } |
---|
185 | |
---|
186 | static int slang_end_graphics(caca_display_t *dp) |
---|
187 | { |
---|
188 | _caca_set_term_title(""); |
---|
189 | SLtt_set_mouse_mode(0, 0); |
---|
190 | SLtt_set_cursor_visibility(1); |
---|
191 | SLang_reset_tty(); |
---|
192 | SLsmg_reset_smg(); |
---|
193 | |
---|
194 | #if defined HAVE_GETENV && defined HAVE_PUTENV |
---|
195 | slang_uninstall_terminal(dp); |
---|
196 | #endif |
---|
197 | |
---|
198 | free(dp->drv.p); |
---|
199 | |
---|
200 | return 0; |
---|
201 | } |
---|
202 | |
---|
203 | static int slang_set_display_title(caca_display_t *dp, char const *title) |
---|
204 | { |
---|
205 | _caca_set_term_title(title); |
---|
206 | |
---|
207 | return 0; |
---|
208 | } |
---|
209 | |
---|
210 | static unsigned int slang_get_display_width(caca_display_t const *dp) |
---|
211 | { |
---|
212 | /* Fallback to a 6x10 font */ |
---|
213 | return cucul_get_canvas_width(dp->cv) * 6; |
---|
214 | } |
---|
215 | |
---|
216 | static unsigned int slang_get_display_height(caca_display_t const *dp) |
---|
217 | { |
---|
218 | /* Fallback to a 6x10 font */ |
---|
219 | return cucul_get_canvas_height(dp->cv) * 10; |
---|
220 | } |
---|
221 | |
---|
222 | static void slang_display(caca_display_t *dp) |
---|
223 | { |
---|
224 | uint32_t const *cvchars = (uint32_t const *)cucul_get_canvas_chars(dp->cv); |
---|
225 | uint32_t const *cvattrs = (uint32_t const *)cucul_get_canvas_attrs(dp->cv); |
---|
226 | unsigned int width = cucul_get_canvas_width(dp->cv); |
---|
227 | unsigned int height = cucul_get_canvas_height(dp->cv); |
---|
228 | int x, y; |
---|
229 | |
---|
230 | for(y = 0; y < (int)height; y++) |
---|
231 | { |
---|
232 | SLsmg_gotorc(y, 0); |
---|
233 | for(x = width; x--; ) |
---|
234 | { |
---|
235 | uint32_t ch = *cvchars++; |
---|
236 | |
---|
237 | #if defined(OPTIMISE_SLANG_PALETTE) |
---|
238 | /* If foreground == background, just don't use this colour |
---|
239 | * pair, and print a space instead of the real character. |
---|
240 | * XXX: disabled, because I can't remember what it was |
---|
241 | * here for, and in cases where SLang does not render |
---|
242 | * bright backgrounds, it's just fucked up. */ |
---|
243 | #if 0 |
---|
244 | uint8_t fgcolor = cucul_attr_to_ansi_fg(*cvattrs); |
---|
245 | uint8_t bgcolor = cucul_attr_to_ansi_bg(*cvattrs); |
---|
246 | |
---|
247 | if(fgcolor >= 0x10) |
---|
248 | fgcolor = CUCUL_LIGHTGRAY; |
---|
249 | |
---|
250 | if(bgcolor >= 0x10) |
---|
251 | bgcolor = CUCUL_BLACK; /* FIXME: handle transparency */ |
---|
252 | |
---|
253 | if(fgcolor == bgcolor) |
---|
254 | { |
---|
255 | if(fgcolor == CUCUL_BLACK) |
---|
256 | fgcolor = CUCUL_WHITE; |
---|
257 | else if(fgcolor == CUCUL_WHITE |
---|
258 | || fgcolor <= CUCUL_LIGHTGRAY) |
---|
259 | fgcolor = CUCUL_BLACK; |
---|
260 | else |
---|
261 | fgcolor = CUCUL_WHITE; |
---|
262 | SLsmg_set_color(slang_assoc[fgcolor + 16 * bgcolor]); |
---|
263 | SLsmg_write_char(' '); |
---|
264 | cvattrs++; |
---|
265 | } |
---|
266 | else |
---|
267 | #endif |
---|
268 | { |
---|
269 | SLsmg_set_color(slang_assoc[cucul_attr_to_ansi(*cvattrs++)]); |
---|
270 | slang_write_utf32(ch); |
---|
271 | } |
---|
272 | #else |
---|
273 | SLsmg_set_color(cucul_attr_to_ansi(*cvattrs++)); |
---|
274 | slang_write_utf32(ch); |
---|
275 | #endif |
---|
276 | } |
---|
277 | } |
---|
278 | SLsmg_gotorc(cucul_get_cursor_y(dp->cv), cucul_get_cursor_x(dp->cv)); |
---|
279 | SLsmg_refresh(); |
---|
280 | } |
---|
281 | |
---|
282 | static void slang_handle_resize(caca_display_t *dp) |
---|
283 | { |
---|
284 | SLtt_get_screen_size(); |
---|
285 | dp->resize.w = SLtt_Screen_Cols; |
---|
286 | dp->resize.h = SLtt_Screen_Rows; |
---|
287 | |
---|
288 | if(dp->resize.w != cucul_get_canvas_width(dp->cv) |
---|
289 | || dp->resize.h != cucul_get_canvas_height(dp->cv)) |
---|
290 | SLsmg_reinit_smg(); |
---|
291 | } |
---|
292 | |
---|
293 | static int slang_get_event(caca_display_t *dp, caca_privevent_t *ev) |
---|
294 | { |
---|
295 | int intkey; |
---|
296 | |
---|
297 | if(!SLang_input_pending(0)) |
---|
298 | { |
---|
299 | ev->type = CACA_EVENT_NONE; |
---|
300 | return 0; |
---|
301 | } |
---|
302 | |
---|
303 | /* We first use SLang_getkey() to see whether Esc was pressed |
---|
304 | * alone, then (if it wasn't) we unget the key and use SLkp_getkey() |
---|
305 | * instead, so that escape sequences are interpreted. */ |
---|
306 | intkey = SLang_getkey(); |
---|
307 | |
---|
308 | if(intkey != 0x1b /* Esc */ || SLang_input_pending(0)) |
---|
309 | { |
---|
310 | SLang_ungetkey(intkey); |
---|
311 | intkey = SLkp_getkey(); |
---|
312 | } |
---|
313 | |
---|
314 | /* If the key was ASCII, return it immediately */ |
---|
315 | if(intkey < 0x7f) |
---|
316 | { |
---|
317 | ev->type = CACA_EVENT_KEY_PRESS; |
---|
318 | ev->data.key.ch = intkey; |
---|
319 | ev->data.key.utf32 = intkey; |
---|
320 | ev->data.key.utf8[0] = intkey; |
---|
321 | ev->data.key.utf8[1] = '\0'; |
---|
322 | return 1; |
---|
323 | } |
---|
324 | |
---|
325 | /* If the key was UTF-8, parse the whole sequence */ |
---|
326 | if(intkey >= 0x80 && intkey < 0x100) |
---|
327 | { |
---|
328 | int keys[7]; /* Necessary for ungetkey(); */ |
---|
329 | char utf8[7]; |
---|
330 | uint32_t utf32; |
---|
331 | unsigned int i, bytes = 0; |
---|
332 | |
---|
333 | keys[0] = intkey; |
---|
334 | utf8[0] = intkey; |
---|
335 | |
---|
336 | for(i = 1; i < 6; i++) |
---|
337 | { |
---|
338 | if(!SLang_input_pending(0)) |
---|
339 | break; |
---|
340 | keys[i] = SLang_getkey(); |
---|
341 | utf8[i] = (unsigned char)keys[i]; |
---|
342 | } |
---|
343 | |
---|
344 | utf8[i] = '\0'; |
---|
345 | utf32 = cucul_utf8_to_utf32(utf8, &bytes); |
---|
346 | |
---|
347 | while(i > bytes) |
---|
348 | SLang_ungetkey(keys[--i]); |
---|
349 | |
---|
350 | if(bytes) |
---|
351 | { |
---|
352 | ev->type = CACA_EVENT_KEY_PRESS; |
---|
353 | ev->data.key.ch = 0; |
---|
354 | ev->data.key.utf32 = utf32; |
---|
355 | strcpy(ev->data.key.utf8, utf8); |
---|
356 | return 1; |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | if(intkey == 0x3e9) |
---|
361 | { |
---|
362 | int button = (SLang_getkey() - ' ' + 1) & 0xf; |
---|
363 | unsigned int x = SLang_getkey() - '!'; |
---|
364 | unsigned int y = SLang_getkey() - '!'; |
---|
365 | |
---|
366 | ev->data.mouse.button = button; |
---|
367 | ev->type = CACA_EVENT_MOUSE_PRESS; |
---|
368 | _push_event(dp, ev); |
---|
369 | ev->type = CACA_EVENT_MOUSE_RELEASE; |
---|
370 | _push_event(dp, ev); |
---|
371 | |
---|
372 | if(dp->mouse.x == x && dp->mouse.y == y) |
---|
373 | return _pop_event(dp, ev); |
---|
374 | |
---|
375 | dp->mouse.x = x; |
---|
376 | dp->mouse.y = y; |
---|
377 | |
---|
378 | ev->type = CACA_EVENT_MOUSE_MOTION; |
---|
379 | ev->data.mouse.x = dp->mouse.x; |
---|
380 | ev->data.mouse.y = dp->mouse.y; |
---|
381 | return 1; |
---|
382 | } |
---|
383 | |
---|
384 | switch(intkey) |
---|
385 | { |
---|
386 | case SL_KEY_UP: ev->data.key.ch = CACA_KEY_UP; break; |
---|
387 | case SL_KEY_DOWN: ev->data.key.ch = CACA_KEY_DOWN; break; |
---|
388 | case SL_KEY_LEFT: ev->data.key.ch = CACA_KEY_LEFT; break; |
---|
389 | case SL_KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break; |
---|
390 | |
---|
391 | case SL_KEY_IC: ev->data.key.ch = CACA_KEY_INSERT; break; |
---|
392 | case SL_KEY_DELETE: ev->data.key.ch = CACA_KEY_DELETE; break; |
---|
393 | case 0x7f: |
---|
394 | case SL_KEY_BACKSPACE: ev->data.key.ch = CACA_KEY_BACKSPACE; break; |
---|
395 | case SL_KEY_HOME: ev->data.key.ch = CACA_KEY_HOME; break; |
---|
396 | case SL_KEY_END: ev->data.key.ch = CACA_KEY_END; break; |
---|
397 | case SL_KEY_PPAGE: ev->data.key.ch = CACA_KEY_PAGEUP; break; |
---|
398 | case SL_KEY_NPAGE: ev->data.key.ch = CACA_KEY_PAGEDOWN; break; |
---|
399 | |
---|
400 | case SL_KEY_F(1): ev->data.key.ch = CACA_KEY_F1; break; |
---|
401 | case SL_KEY_F(2): ev->data.key.ch = CACA_KEY_F2; break; |
---|
402 | case SL_KEY_F(3): ev->data.key.ch = CACA_KEY_F3; break; |
---|
403 | case SL_KEY_F(4): ev->data.key.ch = CACA_KEY_F4; break; |
---|
404 | case SL_KEY_F(5): ev->data.key.ch = CACA_KEY_F5; break; |
---|
405 | case SL_KEY_F(6): ev->data.key.ch = CACA_KEY_F6; break; |
---|
406 | case SL_KEY_F(7): ev->data.key.ch = CACA_KEY_F7; break; |
---|
407 | case SL_KEY_F(8): ev->data.key.ch = CACA_KEY_F8; break; |
---|
408 | case SL_KEY_F(9): ev->data.key.ch = CACA_KEY_F9; break; |
---|
409 | case SL_KEY_F(10): ev->data.key.ch = CACA_KEY_F10; break; |
---|
410 | case SL_KEY_F(11): ev->data.key.ch = CACA_KEY_F11; break; |
---|
411 | case SL_KEY_F(12): ev->data.key.ch = CACA_KEY_F12; break; |
---|
412 | |
---|
413 | default: |
---|
414 | /* Unknown key */ |
---|
415 | ev->type = CACA_EVENT_NONE; return 0; |
---|
416 | } |
---|
417 | |
---|
418 | ev->type = CACA_EVENT_KEY_PRESS; |
---|
419 | ev->data.key.utf32 = 0; |
---|
420 | ev->data.key.utf8[0] = '\0'; |
---|
421 | return 1; |
---|
422 | } |
---|
423 | |
---|
424 | static void slang_set_cursor(caca_display_t *dp, int flags) |
---|
425 | { |
---|
426 | SLtt_set_cursor_visibility(flags ? 1 : 0); |
---|
427 | } |
---|
428 | |
---|
429 | /* |
---|
430 | * XXX: following functions are local |
---|
431 | */ |
---|
432 | |
---|
433 | static void slang_init_palette(void) |
---|
434 | { |
---|
435 | /* See SLang ref., 5.4.4. */ |
---|
436 | static char *slang_colors[16] = |
---|
437 | { |
---|
438 | /* Standard colours */ |
---|
439 | "black", |
---|
440 | "blue", |
---|
441 | "green", |
---|
442 | "cyan", |
---|
443 | "red", |
---|
444 | "magenta", |
---|
445 | "brown", |
---|
446 | "lightgray", |
---|
447 | /* Bright colours */ |
---|
448 | "gray", |
---|
449 | "brightblue", |
---|
450 | "brightgreen", |
---|
451 | "brightcyan", |
---|
452 | "brightred", |
---|
453 | "brightmagenta", |
---|
454 | "yellow", |
---|
455 | "white", |
---|
456 | }; |
---|
457 | |
---|
458 | #if defined(OPTIMISE_SLANG_PALETTE) |
---|
459 | int i; |
---|
460 | |
---|
461 | for(i = 0; i < 16 * 16; i++) |
---|
462 | SLtt_set_color(i, NULL, slang_colors[slang_palette[i * 2]], |
---|
463 | slang_colors[slang_palette[i * 2 + 1]]); |
---|
464 | #else |
---|
465 | int fg, bg; |
---|
466 | |
---|
467 | for(bg = 0; bg < 16; bg++) |
---|
468 | for(fg = 0; fg < 16; fg++) |
---|
469 | { |
---|
470 | int i = fg + 16 * bg; |
---|
471 | SLtt_set_color(i, NULL, slang_colors[fg], slang_colors[bg]); |
---|
472 | } |
---|
473 | #endif |
---|
474 | } |
---|
475 | |
---|
476 | static void slang_write_utf32(uint32_t ch) |
---|
477 | { |
---|
478 | #ifdef HAVE_SLSMG_UTF8_ENABLE |
---|
479 | char buf[10]; |
---|
480 | int bytes; |
---|
481 | #else |
---|
482 | char ascii; |
---|
483 | #endif |
---|
484 | |
---|
485 | if(ch == CUCUL_MAGIC_FULLWIDTH) |
---|
486 | return; |
---|
487 | |
---|
488 | #ifdef HAVE_SLSMG_UTF8_ENABLE |
---|
489 | bytes = cucul_utf32_to_utf8(buf, ch); |
---|
490 | buf[bytes] = '\0'; |
---|
491 | SLsmg_write_string(buf); |
---|
492 | #else |
---|
493 | ascii = cucul_utf32_to_ascii(ch); |
---|
494 | SLsmg_write_char(ascii); |
---|
495 | if(cucul_utf32_is_fullwidth(ch)) |
---|
496 | SLsmg_write_char(ascii); |
---|
497 | #endif |
---|
498 | } |
---|
499 | |
---|
500 | #if defined(HAVE_SIGNAL) |
---|
501 | static RETSIGTYPE sigwinch_handler(int sig) |
---|
502 | { |
---|
503 | sigwinch_d->resize.resized = 1; |
---|
504 | |
---|
505 | signal(SIGWINCH, sigwinch_handler); |
---|
506 | } |
---|
507 | #endif |
---|
508 | |
---|
509 | #if defined(HAVE_GETENV) && defined(HAVE_PUTENV) |
---|
510 | static void slang_install_terminal(caca_display_t *dp) |
---|
511 | { |
---|
512 | char *term, *colorterm; |
---|
513 | |
---|
514 | dp->drv.p->term = NULL; |
---|
515 | |
---|
516 | term = getenv("TERM"); |
---|
517 | colorterm = getenv("COLORTERM"); |
---|
518 | |
---|
519 | if(!term || strcmp(term, "xterm")) |
---|
520 | return; |
---|
521 | |
---|
522 | /* If we are using gnome-terminal, it's really a 16 colour terminal. |
---|
523 | * Ditto if we are using xfce4-terminal, or Konsole. */ |
---|
524 | if((colorterm && (!strcmp(colorterm, "gnome-terminal") |
---|
525 | || !strcmp(colorterm, "Terminal"))) |
---|
526 | || getenv("KONSOLE_DCOP_SESSION")) |
---|
527 | { |
---|
528 | (void)putenv("TERM=xterm-16color"); |
---|
529 | dp->drv.p->term = strdup(term); |
---|
530 | return; |
---|
531 | } |
---|
532 | } |
---|
533 | |
---|
534 | static void slang_uninstall_terminal(caca_display_t *dp) |
---|
535 | { |
---|
536 | /* Needs to be persistent because we use putenv() */ |
---|
537 | static char termenv[1024]; |
---|
538 | |
---|
539 | if(!dp->drv.p->term) |
---|
540 | return; |
---|
541 | |
---|
542 | snprintf(termenv, 1023, "TERM=%s", dp->drv.p->term); |
---|
543 | free(dp->drv.p->term); |
---|
544 | (void)putenv(termenv); |
---|
545 | } |
---|
546 | #endif |
---|
547 | |
---|
548 | /* |
---|
549 | * Driver initialisation |
---|
550 | */ |
---|
551 | |
---|
552 | int slang_install(caca_display_t *dp) |
---|
553 | { |
---|
554 | dp->drv.id = CACA_DRIVER_SLANG; |
---|
555 | dp->drv.driver = "slang"; |
---|
556 | |
---|
557 | dp->drv.init_graphics = slang_init_graphics; |
---|
558 | dp->drv.end_graphics = slang_end_graphics; |
---|
559 | dp->drv.set_display_title = slang_set_display_title; |
---|
560 | dp->drv.get_display_width = slang_get_display_width; |
---|
561 | dp->drv.get_display_height = slang_get_display_height; |
---|
562 | dp->drv.display = slang_display; |
---|
563 | dp->drv.handle_resize = slang_handle_resize; |
---|
564 | dp->drv.get_event = slang_get_event; |
---|
565 | dp->drv.set_mouse = NULL; |
---|
566 | dp->drv.set_cursor = slang_set_cursor; |
---|
567 | |
---|
568 | return 0; |
---|
569 | } |
---|
570 | |
---|
571 | #endif /* USE_SLANG */ |
---|
572 | |
---|