You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

538 regels
16 KiB

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