Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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