25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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