Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * libcaca ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file driver_slang.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief SLang driver
  15. *
  16. * This file contains the libcaca SLang input and output driver
  17. */
  18. #include "config.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 test/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_t *sigwinch_kk; /* 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_t *kk)
  105. {
  106. #if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
  107. slang_check_terminal();
  108. #endif
  109. #if defined(HAVE_SIGNAL)
  110. sigwinch_kk = kk;
  111. signal(SIGWINCH, sigwinch_handler);
  112. #endif
  113. /* Initialise slang library */
  114. SLsig_block_signals();
  115. SLtt_get_terminfo();
  116. if(SLkp_init() == -1)
  117. {
  118. SLsig_unblock_signals();
  119. return -1;
  120. }
  121. SLang_init_tty(-1, 0, 1);
  122. if(SLsmg_init_smg() == -1)
  123. {
  124. SLsig_unblock_signals();
  125. return -1;
  126. }
  127. SLsig_unblock_signals();
  128. SLsmg_cls();
  129. SLtt_set_cursor_visibility(0);
  130. SLkp_define_keysym("\e[M", 1001);
  131. SLtt_set_mouse_mode(1, 0);
  132. SLsmg_refresh();
  133. /* Disable scrolling so that hashmap scrolling optimization code
  134. * does not cause ugly refreshes due to slow terminals */
  135. SLtt_Term_Cannot_Scroll = 1;
  136. slang_init_palette();
  137. /* Disable alt charset support so that we get a chance to have all
  138. * 256 colour pairs */
  139. SLtt_Has_Alt_Charset = 0;
  140. #ifdef HAVE_SLSMG_UTF8_ENABLE
  141. SLsmg_utf8_enable(1); /* 1 == force, 0 == disable, -1 == autodetect */
  142. SLtt_utf8_enable(1);
  143. #endif
  144. _cucul_set_size(kk->qq, SLtt_Screen_Cols, SLtt_Screen_Rows);
  145. return 0;
  146. }
  147. static int slang_end_graphics(caca_t *kk)
  148. {
  149. SLtt_set_mouse_mode(0, 0);
  150. SLtt_set_cursor_visibility(1);
  151. SLang_reset_tty();
  152. SLsmg_reset_smg();
  153. return 0;
  154. }
  155. static int slang_set_window_title(caca_t *kk, char const *title)
  156. {
  157. /* FIXME */
  158. return 0;
  159. }
  160. static unsigned int slang_get_window_width(caca_t *kk)
  161. {
  162. /* Fallback to a 6x10 font */
  163. return kk->qq->width * 6;
  164. }
  165. static unsigned int slang_get_window_height(caca_t *kk)
  166. {
  167. /* Fallback to a 6x10 font */
  168. return kk->qq->height * 10;
  169. }
  170. static void slang_display(caca_t *kk)
  171. {
  172. int x, y;
  173. uint8_t *attr = kk->qq->attr;
  174. uint32_t *chars = kk->qq->chars;
  175. for(y = 0; y < (int)kk->qq->height; y++)
  176. {
  177. SLsmg_gotorc(y, 0);
  178. for(x = kk->qq->width; x--; )
  179. {
  180. uint32_t c = *chars++;
  181. #if defined(OPTIMISE_SLANG_PALETTE)
  182. uint8_t fgcolor = *attr & 0xf;
  183. uint8_t bgcolor = *attr >> 4;
  184. /* If foreground == background, just don't use this colour
  185. * pair, and print a space instead of the real character. */
  186. if(fgcolor != bgcolor)
  187. {
  188. SLsmg_set_color(slang_assoc[*attr++]);
  189. slang_write_utf32(c);
  190. }
  191. else
  192. {
  193. if(fgcolor == CUCUL_COLOR_BLACK)
  194. fgcolor = CUCUL_COLOR_WHITE;
  195. else if(fgcolor == CUCUL_COLOR_WHITE
  196. || fgcolor <= CUCUL_COLOR_LIGHTGRAY)
  197. fgcolor = CUCUL_COLOR_BLACK;
  198. else
  199. fgcolor = CUCUL_COLOR_WHITE;
  200. SLsmg_set_color(slang_assoc[fgcolor + 16 * bgcolor]);
  201. SLsmg_write_char(' ');
  202. attr++;
  203. }
  204. #else
  205. SLsmg_set_color(*attr++);
  206. slang_write_utf32(c);
  207. #endif
  208. }
  209. }
  210. SLsmg_refresh();
  211. }
  212. static void slang_handle_resize(caca_t *kk)
  213. {
  214. SLtt_get_screen_size();
  215. kk->resize.w = SLtt_Screen_Cols;
  216. kk->resize.h = SLtt_Screen_Rows;
  217. if(kk->resize.w != kk->qq->width || kk->resize.h != kk->qq->height)
  218. SLsmg_reinit_smg();
  219. }
  220. static unsigned int slang_get_event(caca_t *kk)
  221. {
  222. unsigned int event;
  223. int intkey;
  224. if(!SLang_input_pending(0))
  225. return CACA_EVENT_NONE;
  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. return CACA_EVENT_KEY_PRESS | intkey;
  239. }
  240. if(intkey == 0x3e9)
  241. {
  242. int button = (SLang_getkey() - ' ' + 1) & 0xf;
  243. unsigned int x = SLang_getkey() - '!';
  244. unsigned int y = SLang_getkey() - '!';
  245. _push_event(kk, CACA_EVENT_MOUSE_PRESS | button);
  246. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | button);
  247. if(kk->mouse.x == x && kk->mouse.y == y)
  248. return _pop_event(kk);
  249. kk->mouse.x = x;
  250. kk->mouse.y = y;
  251. return CACA_EVENT_MOUSE_MOTION | (kk->mouse.x << 12) | kk->mouse.y;
  252. }
  253. event = CACA_EVENT_KEY_PRESS;
  254. switch(intkey)
  255. {
  256. case SL_KEY_UP: return event | CACA_KEY_UP;
  257. case SL_KEY_DOWN: return event | CACA_KEY_DOWN;
  258. case SL_KEY_LEFT: return event | CACA_KEY_LEFT;
  259. case SL_KEY_RIGHT: return event | CACA_KEY_RIGHT;
  260. case SL_KEY_IC: return event | CACA_KEY_INSERT;
  261. case SL_KEY_DELETE: return event | CACA_KEY_DELETE;
  262. case SL_KEY_HOME: return event | CACA_KEY_HOME;
  263. case SL_KEY_END: return event | CACA_KEY_END;
  264. case SL_KEY_PPAGE: return event | CACA_KEY_PAGEUP;
  265. case SL_KEY_NPAGE: return event | CACA_KEY_PAGEDOWN;
  266. case SL_KEY_F(1): return event | CACA_KEY_F1;
  267. case SL_KEY_F(2): return event | CACA_KEY_F2;
  268. case SL_KEY_F(3): return event | CACA_KEY_F3;
  269. case SL_KEY_F(4): return event | CACA_KEY_F4;
  270. case SL_KEY_F(5): return event | CACA_KEY_F5;
  271. case SL_KEY_F(6): return event | CACA_KEY_F6;
  272. case SL_KEY_F(7): return event | CACA_KEY_F7;
  273. case SL_KEY_F(8): return event | CACA_KEY_F8;
  274. case SL_KEY_F(9): return event | CACA_KEY_F9;
  275. case SL_KEY_F(10): return event | CACA_KEY_F10;
  276. case SL_KEY_F(11): return event | CACA_KEY_F11;
  277. case SL_KEY_F(12): return event | CACA_KEY_F12;
  278. }
  279. return CACA_EVENT_NONE;
  280. }
  281. /*
  282. * XXX: following functions are local
  283. */
  284. static void slang_init_palette(void)
  285. {
  286. /* See SLang ref., 5.4.4. */
  287. static char *slang_colors[16] =
  288. {
  289. /* Standard colours */
  290. "black",
  291. "blue",
  292. "green",
  293. "cyan",
  294. "red",
  295. "magenta",
  296. "brown",
  297. "lightgray",
  298. /* Bright colours */
  299. "gray",
  300. "brightblue",
  301. "brightgreen",
  302. "brightcyan",
  303. "brightred",
  304. "brightmagenta",
  305. "yellow",
  306. "white",
  307. };
  308. #if defined(OPTIMISE_SLANG_PALETTE)
  309. int i;
  310. for(i = 0; i < 16 * 16; i++)
  311. SLtt_set_color(i, NULL, slang_colors[slang_palette[i * 2]],
  312. slang_colors[slang_palette[i * 2 + 1]]);
  313. #else
  314. int fg, bg;
  315. for(bg = 0; bg < 16; bg++)
  316. for(fg = 0; fg < 16; fg++)
  317. {
  318. int i = fg + 16 * bg;
  319. SLtt_set_color(i, NULL, slang_colors[fg], slang_colors[bg]);
  320. }
  321. #endif
  322. }
  323. static void slang_write_utf32(uint32_t c)
  324. {
  325. static const uint8_t mark[7] =
  326. {
  327. 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
  328. };
  329. char buf[10], *parser;
  330. int bytes;
  331. if(c < 0x80)
  332. {
  333. SLsmg_write_char(c);
  334. return;
  335. }
  336. #ifdef HAVE_SLSMG_UTF8_ENABLE
  337. bytes = (c < 0x800) ? 2 : (c < 0x10000) ? 3 : 4;
  338. buf[bytes] = '\0';
  339. parser = buf + bytes;
  340. switch(bytes)
  341. {
  342. case 4: *--parser = (c | 0x80) & 0xbf; c >>= 6;
  343. case 3: *--parser = (c | 0x80) & 0xbf; c >>= 6;
  344. case 2: *--parser = (c | 0x80) & 0xbf; c >>= 6;
  345. }
  346. *--parser = c | mark[bytes];
  347. SLsmg_write_string(buf);
  348. #else
  349. SLsmg_write_char(' ');
  350. #endif
  351. }
  352. #if defined(HAVE_SIGNAL)
  353. static RETSIGTYPE sigwinch_handler(int sig)
  354. {
  355. sigwinch_kk->resize.resized = 1;
  356. signal(SIGWINCH, sigwinch_handler);
  357. }
  358. #endif
  359. #if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
  360. static void slang_check_terminal(void)
  361. {
  362. char *term, *colorterm, *other;
  363. term = getenv("TERM");
  364. colorterm = getenv("COLORTERM");
  365. if(term && !strcmp(term, "xterm"))
  366. {
  367. /* If we are using gnome-terminal, it's really a 16 colour terminal */
  368. if(colorterm && !strcmp(colorterm, "gnome-terminal"))
  369. {
  370. (void)putenv("TERM=xterm-16color");
  371. return;
  372. }
  373. /* Ditto if we are using Konsole */
  374. other = getenv("KONSOLE_DCOP_SESSION");
  375. if(other)
  376. {
  377. (void)putenv("TERM=xterm-16color");
  378. return;
  379. }
  380. }
  381. }
  382. #endif
  383. /*
  384. * Driver initialisation
  385. */
  386. void slang_init_driver(caca_t *kk)
  387. {
  388. kk->drv.driver = CACA_DRIVER_SLANG;
  389. kk->drv.init_graphics = slang_init_graphics;
  390. kk->drv.end_graphics = slang_end_graphics;
  391. kk->drv.set_window_title = slang_set_window_title;
  392. kk->drv.get_window_width = slang_get_window_width;
  393. kk->drv.get_window_height = slang_get_window_height;
  394. kk->drv.display = slang_display;
  395. kk->drv.handle_resize = slang_handle_resize;
  396. kk->drv.get_event = slang_get_event;
  397. }
  398. #endif /* USE_SLANG */