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 1392 2006-11-13 23:42:43Z sam $ |
---|
7 | * |
---|
8 | * This library is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | * This file contains the libcaca SLang input and output driver |
---|
16 | */ |
---|
17 | |
---|
18 | #include "config.h" |
---|
19 | #include "common.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 "caca.h" |
---|
37 | #include "caca_internals.h" |
---|
38 | #include "cucul.h" |
---|
39 | #include "cucul_internals.h" |
---|
40 | |
---|
41 | /* |
---|
42 | * Global variables |
---|
43 | */ |
---|
44 | |
---|
45 | /* Tables generated by tools/optipal.c */ |
---|
46 | static int const slang_palette[2*16*16] = |
---|
47 | { |
---|
48 | 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, |
---|
49 | 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 0, 0, 8, |
---|
50 | 8, 7, 7, 8, 15, 7, 7, 15, 15, 9, 9, 15, 1, 9, 9, 1, |
---|
51 | 7, 9, 9, 7, 8, 1, 1, 8, 0, 1, 15, 10, 10, 15, 2, 10, |
---|
52 | 10, 2, 7, 10, 10, 7, 8, 2, 2, 8, 0, 2, 15, 11, 11, 15, |
---|
53 | 3, 11, 11, 3, 7, 11, 11, 7, 8, 3, 3, 8, 0, 3, 15, 12, |
---|
54 | 12, 15, 4, 12, 12, 4, 7, 12, 12, 7, 8, 4, 4, 8, 0, 4, |
---|
55 | 15, 13, 13, 15, 5, 13, 13, 5, 7, 13, 13, 7, 8, 5, 5, 8, |
---|
56 | 0, 5, 15, 14, 14, 15, 6, 14, 14, 6, 7, 14, 14, 7, 8, 6, |
---|
57 | 6, 8, 0, 6, 4, 6, 6, 4, 12, 14, 14, 12, 6, 2, 2, 6, |
---|
58 | 14, 10, 10, 14, 2, 3, 3, 2, 10, 11, 11, 10, 3, 1, 1, 3, |
---|
59 | 11, 9, 9, 11, 1, 5, 5, 1, 9, 13, 13, 9, 5, 4, 4, 5, |
---|
60 | 13, 12, 12, 13, 4, 14, 6, 12, 12, 6, 14, 4, 6, 10, 2, 14, |
---|
61 | 14, 2, 10, 6, 2, 11, 3, 10, 10, 3, 11, 2, 3, 9, 1, 11, |
---|
62 | 11, 1, 9, 3, 1, 13, 5, 9, 9, 5, 13, 1, 5, 12, 4, 13, |
---|
63 | 13, 4, 12, 5, 0, 7, 0, 15, 15, 8, 8, 15, 15, 1, 7, 1, |
---|
64 | 1, 6, 2, 5, 3, 4, 4, 3, 5, 2, 6, 1, 0, 0, 1, 1, |
---|
65 | 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 2, 2, 3, 3, |
---|
66 | 4, 4, 5, 5, 6, 6, 7, 7, 14, 9, 1, 15, 8, 9, 8, 8, |
---|
67 | 9, 9, 1, 7, 0, 9, 9, 8, 6, 9, 13, 10, 2, 15, 8, 10, |
---|
68 | 7, 2, 15, 2, 2, 7, 0, 10, 10, 8, 5, 10, 12, 11, 3, 15, |
---|
69 | 8, 11, 7, 3, 15, 3, 3, 7, 0, 11, 11, 8, 4, 11, 11, 12, |
---|
70 | 4, 15, 8, 12, 7, 4, 15, 4, 4, 7, 0, 12, 12, 8, 3, 12, |
---|
71 | 10, 13, 5, 15, 8, 13, 7, 5, 15, 5, 5, 7, 0, 13, 13, 8, |
---|
72 | 2, 13, 9, 14, 6, 15, 8, 14, 7, 6, 15, 6, 6, 7, 0, 14, |
---|
73 | 14, 8, 1, 14, 5, 6, 2, 4, 13, 14, 10, 12, 4, 2, 3, 6, |
---|
74 | 12, 10, 11, 14, 6, 3, 1, 2, 14, 11, 9, 10, 2, 1, 5, 3, |
---|
75 | 10, 9, 13, 11, 3, 5, 4, 1, 11, 13, 12, 9, 1, 4, 6, 5, |
---|
76 | 9, 12, 14, 13, 5, 14, 2, 12, 13, 6, 10, 4, 4, 10, 3, 14, |
---|
77 | 12, 2, 11, 6, 6, 11, 1, 10, 14, 3, 9, 2, 2, 9, 5, 11, |
---|
78 | 10, 1, 13, 3, 3, 13, 4, 9, 11, 5, 12, 1, 1, 12, 6, 13, |
---|
79 | 9, 4, 14, 5, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, |
---|
80 | }; |
---|
81 | |
---|
82 | static int const slang_assoc[16*16] = |
---|
83 | { |
---|
84 | 134, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
---|
85 | 28, 135, 214, 86, 219, 91, 133, 127, 26, 23, 240, 112, 245, 117, 141, 126, |
---|
86 | 37, 211, 142, 83, 206, 132, 78, 160, 35, 237, 32, 109, 232, 140, 104, 161, |
---|
87 | 46, 87, 82, 143, 131, 215, 210, 169, 44, 113, 108, 41, 139, 241, 236, 170, |
---|
88 | 55, 222, 203, 130, 144, 94, 75, 178, 53, 248, 229, 138, 50, 120, 101, 179, |
---|
89 | 64, 90, 129, 218, 95, 145, 223, 187, 62, 116, 137, 244, 121, 59, 249, 188, |
---|
90 | 73, 128, 79, 207, 74, 202, 146, 196, 71, 136, 105, 233, 100, 228, 68, 197, |
---|
91 | 122, 153, 162, 171, 180, 189, 198, 147, 16, 25, 34, 43, 52, 61, 70, 18, |
---|
92 | 15, 27, 36, 45, 54, 63, 72, 17, 151, 155, 164, 173, 182, 191, 200, 124, |
---|
93 | 154, 22, 238, 110, 243, 115, 156, 24, 150, 152, 216, 88, 221, 93, 148, 20, |
---|
94 | 163, 235, 31, 107, 230, 165, 102, 33, 159, 213, 250, 85, 208, 157, 80, 29, |
---|
95 | 172, 111, 106, 40, 174, 239, 234, 42, 168, 89, 84, 251, 166, 217, 212, 38, |
---|
96 | 181, 246, 227, 183, 49, 118, 99, 51, 177, 224, 205, 175, 252, 96, 77, 47, |
---|
97 | 190, 114, 192, 242, 119, 58, 247, 60, 186, 92, 184, 220, 97, 253, 225, 56, |
---|
98 | 199, 201, 103, 231, 98, 226, 67, 69, 195, 193, 81, 209, 76, 204, 254, 65, |
---|
99 | 123, 149, 158, 167, 176, 185, 194, 19, 125, 21, 30, 39, 48, 57, 66, 255, |
---|
100 | }; |
---|
101 | |
---|
102 | /* |
---|
103 | * Local functions |
---|
104 | */ |
---|
105 | static void slang_init_palette(void); |
---|
106 | static void slang_write_utf32(uint32_t); |
---|
107 | |
---|
108 | #if defined(HAVE_SIGNAL) |
---|
109 | static RETSIGTYPE sigwinch_handler(int); |
---|
110 | static caca_display_t *sigwinch_d; /* FIXME: we ought to get rid of this */ |
---|
111 | #endif |
---|
112 | #if defined(HAVE_GETENV) && defined(HAVE_PUTENV) |
---|
113 | static void slang_check_terminal(void); |
---|
114 | #endif |
---|
115 | |
---|
116 | static int slang_init_graphics(caca_display_t *dp) |
---|
117 | { |
---|
118 | #if defined(HAVE_GETENV) && defined(HAVE_PUTENV) |
---|
119 | slang_check_terminal(); |
---|
120 | #endif |
---|
121 | |
---|
122 | #if defined(HAVE_SIGNAL) |
---|
123 | sigwinch_d = dp; |
---|
124 | signal(SIGWINCH, sigwinch_handler); |
---|
125 | #endif |
---|
126 | |
---|
127 | _caca_set_term_title("caca for S-Lang"); |
---|
128 | |
---|
129 | /* Initialise slang library */ |
---|
130 | SLsig_block_signals(); |
---|
131 | SLtt_get_terminfo(); |
---|
132 | |
---|
133 | if(SLkp_init() == -1) |
---|
134 | { |
---|
135 | SLsig_unblock_signals(); |
---|
136 | return -1; |
---|
137 | } |
---|
138 | |
---|
139 | SLang_init_tty(-1, 0, 1); |
---|
140 | |
---|
141 | if(SLsmg_init_smg() == -1) |
---|
142 | { |
---|
143 | SLsig_unblock_signals(); |
---|
144 | return -1; |
---|
145 | } |
---|
146 | |
---|
147 | SLsig_unblock_signals(); |
---|
148 | |
---|
149 | SLsmg_cls(); |
---|
150 | SLtt_set_cursor_visibility(0); |
---|
151 | SLkp_define_keysym("\e[M", 1001); |
---|
152 | SLtt_set_mouse_mode(1, 0); |
---|
153 | SLsmg_refresh(); |
---|
154 | |
---|
155 | /* Disable scrolling so that hashmap scrolling optimization code |
---|
156 | * does not cause ugly refreshes due to slow terminals */ |
---|
157 | SLtt_Term_Cannot_Scroll = 1; |
---|
158 | |
---|
159 | slang_init_palette(); |
---|
160 | |
---|
161 | /* Disable alt charset support so that we get a chance to have all |
---|
162 | * 256 colour pairs */ |
---|
163 | SLtt_Has_Alt_Charset = 0; |
---|
164 | |
---|
165 | #ifdef HAVE_SLSMG_UTF8_ENABLE |
---|
166 | SLsmg_utf8_enable(1); /* 1 == force, 0 == disable, -1 == autodetect */ |
---|
167 | SLtt_utf8_enable(1); |
---|
168 | #endif |
---|
169 | |
---|
170 | _cucul_set_canvas_size(dp->cv, SLtt_Screen_Cols, SLtt_Screen_Rows); |
---|
171 | |
---|
172 | return 0; |
---|
173 | } |
---|
174 | |
---|
175 | static int slang_end_graphics(caca_display_t *dp) |
---|
176 | { |
---|
177 | _caca_set_term_title(""); |
---|
178 | SLtt_set_mouse_mode(0, 0); |
---|
179 | SLtt_set_cursor_visibility(1); |
---|
180 | SLang_reset_tty(); |
---|
181 | SLsmg_reset_smg(); |
---|
182 | |
---|
183 | return 0; |
---|
184 | } |
---|
185 | |
---|
186 | static int slang_set_display_title(caca_display_t *dp, char const *title) |
---|
187 | { |
---|
188 | _caca_set_term_title(title); |
---|
189 | |
---|
190 | return 0; |
---|
191 | } |
---|
192 | |
---|
193 | static unsigned int slang_get_display_width(caca_display_t *dp) |
---|
194 | { |
---|
195 | /* Fallback to a 6x10 font */ |
---|
196 | return dp->cv->width * 6; |
---|
197 | } |
---|
198 | |
---|
199 | static unsigned int slang_get_display_height(caca_display_t *dp) |
---|
200 | { |
---|
201 | /* Fallback to a 6x10 font */ |
---|
202 | return dp->cv->height * 10; |
---|
203 | } |
---|
204 | |
---|
205 | static void slang_display(caca_display_t *dp) |
---|
206 | { |
---|
207 | int x, y; |
---|
208 | uint32_t *attrs = dp->cv->attrs; |
---|
209 | uint32_t *chars = dp->cv->chars; |
---|
210 | for(y = 0; y < (int)dp->cv->height; y++) |
---|
211 | { |
---|
212 | SLsmg_gotorc(y, 0); |
---|
213 | for(x = dp->cv->width; x--; ) |
---|
214 | { |
---|
215 | uint32_t ch = *chars++; |
---|
216 | |
---|
217 | #if defined(OPTIMISE_SLANG_PALETTE) |
---|
218 | /* If foreground == background, just don't use this colour |
---|
219 | * pair, and print a space instead of the real character. |
---|
220 | * XXX: disabled, because I can't remember what it was |
---|
221 | * here for, and in cases where SLang does not render |
---|
222 | * bright backgrounds, it's just fucked up. */ |
---|
223 | #if 0 |
---|
224 | uint8_t fgcolor = cucul_attr_to_ansi_fg(*attrs); |
---|
225 | uint8_t bgcolor = cucul_attr_to_ansi_bg(*attrs); |
---|
226 | |
---|
227 | if(fgcolor >= 0x10) |
---|
228 | fgcolor = CUCUL_LIGHTGRAY; |
---|
229 | |
---|
230 | if(bgcolor >= 0x10) |
---|
231 | bgcolor = CUCUL_BLACK; /* FIXME: handle transparency */ |
---|
232 | |
---|
233 | if(fgcolor == bgcolor) |
---|
234 | { |
---|
235 | if(fgcolor == CUCUL_BLACK) |
---|
236 | fgcolor = CUCUL_WHITE; |
---|
237 | else if(fgcolor == CUCUL_WHITE |
---|
238 | || fgcolor <= CUCUL_LIGHTGRAY) |
---|
239 | fgcolor = CUCUL_BLACK; |
---|
240 | else |
---|
241 | fgcolor = CUCUL_WHITE; |
---|
242 | SLsmg_set_color(slang_assoc[fgcolor + 16 * bgcolor]); |
---|
243 | SLsmg_write_char(' '); |
---|
244 | attrs++; |
---|
245 | } |
---|
246 | else |
---|
247 | #endif |
---|
248 | { |
---|
249 | SLsmg_set_color(slang_assoc[cucul_attr_to_ansi(*attrs++)]); |
---|
250 | slang_write_utf32(ch); |
---|
251 | } |
---|
252 | #else |
---|
253 | SLsmg_set_color(cucul_attr_to_ansi(*attrs++)); |
---|
254 | slang_write_utf32(ch); |
---|
255 | #endif |
---|
256 | } |
---|
257 | } |
---|
258 | SLsmg_refresh(); |
---|
259 | } |
---|
260 | |
---|
261 | static void slang_handle_resize(caca_display_t *dp) |
---|
262 | { |
---|
263 | SLtt_get_screen_size(); |
---|
264 | dp->resize.w = SLtt_Screen_Cols; |
---|
265 | dp->resize.h = SLtt_Screen_Rows; |
---|
266 | |
---|
267 | if(dp->resize.w != dp->cv->width || dp->resize.h != dp->cv->height) |
---|
268 | SLsmg_reinit_smg(); |
---|
269 | } |
---|
270 | |
---|
271 | static int slang_get_event(caca_display_t *dp, caca_event_t *ev) |
---|
272 | { |
---|
273 | int intkey; |
---|
274 | |
---|
275 | if(!SLang_input_pending(0)) |
---|
276 | { |
---|
277 | ev->type = CACA_EVENT_NONE; |
---|
278 | return 0; |
---|
279 | } |
---|
280 | |
---|
281 | /* We first use SLang_getkey() to see whether Esc was pressed |
---|
282 | * alone, then (if it wasn't) we unget the key and use SLkp_getkey() |
---|
283 | * instead, so that escape sequences are interpreted. */ |
---|
284 | intkey = SLang_getkey(); |
---|
285 | |
---|
286 | if(intkey != 0x1b /* Esc */ || SLang_input_pending(0)) |
---|
287 | { |
---|
288 | SLang_ungetkey(intkey); |
---|
289 | intkey = SLkp_getkey(); |
---|
290 | } |
---|
291 | |
---|
292 | /* If the key was ASCII, return it immediately */ |
---|
293 | if(intkey < 0x7f) |
---|
294 | { |
---|
295 | ev->type = CACA_EVENT_KEY_PRESS; |
---|
296 | ev->data.key.ch = intkey; |
---|
297 | ev->data.key.utf32 = intkey; |
---|
298 | ev->data.key.utf8[0] = intkey; |
---|
299 | ev->data.key.utf8[1] = '\0'; |
---|
300 | return 1; |
---|
301 | } |
---|
302 | |
---|
303 | /* If the key was UTF-8, parse the whole sequence */ |
---|
304 | if(intkey >= 0x80 && intkey < 0x100) |
---|
305 | { |
---|
306 | int keys[7]; /* Necessary for ungetkey(); */ |
---|
307 | char utf8[7]; |
---|
308 | uint32_t utf32; |
---|
309 | unsigned int i, bytes = 0; |
---|
310 | |
---|
311 | keys[0] = intkey; |
---|
312 | utf8[0] = intkey; |
---|
313 | |
---|
314 | for(i = 1; i < 6; i++) |
---|
315 | { |
---|
316 | if(!SLang_input_pending(0)) |
---|
317 | break; |
---|
318 | keys[i] = SLang_getkey(); |
---|
319 | utf8[i] = (unsigned char)keys[i]; |
---|
320 | } |
---|
321 | |
---|
322 | utf8[i] = '\0'; |
---|
323 | utf32 = cucul_utf8_to_utf32(utf8, &bytes); |
---|
324 | |
---|
325 | while(i > bytes) |
---|
326 | SLang_ungetkey(keys[--i]); |
---|
327 | |
---|
328 | if(bytes) |
---|
329 | { |
---|
330 | ev->type = CACA_EVENT_KEY_PRESS; |
---|
331 | ev->data.key.ch = 0; |
---|
332 | ev->data.key.utf32 = utf32; |
---|
333 | strcpy(ev->data.key.ut |
---|