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