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.
 
 
 
 
 
 

398 rivejä
12 KiB

  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. #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 test/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. #if defined(HAVE_SIGNAL)
  95. static RETSIGTYPE sigwinch_handler(int);
  96. static caca_t *sigwinch_kk; /* FIXME: we ought to get rid of this */
  97. #endif
  98. static int slang_init_graphics(caca_t *kk)
  99. {
  100. #if defined(HAVE_SIGNAL)
  101. sigwinch_kk = kk;
  102. signal(SIGWINCH, sigwinch_handler);
  103. #endif
  104. /* Initialise slang library */
  105. SLsig_block_signals();
  106. SLtt_get_terminfo();
  107. if(SLkp_init() == -1)
  108. {
  109. SLsig_unblock_signals();
  110. return -1;
  111. }
  112. SLang_init_tty(-1, 0, 1);
  113. if(SLsmg_init_smg() == -1)
  114. {
  115. SLsig_unblock_signals();
  116. return -1;
  117. }
  118. SLsig_unblock_signals();
  119. SLsmg_cls();
  120. SLtt_set_cursor_visibility(0);
  121. SLkp_define_keysym("\e[M", 1001);
  122. SLtt_set_mouse_mode(1, 0);
  123. SLsmg_refresh();
  124. /* Disable scrolling so that hashmap scrolling optimization code
  125. * does not cause ugly refreshes due to slow terminals */
  126. SLtt_Term_Cannot_Scroll = 1;
  127. slang_init_palette();
  128. /* Disable alt charset support so that we get a chance to have all
  129. * 256 colour pairs */
  130. SLtt_Has_Alt_Charset = 0;
  131. cucul_set_size(kk->qq, SLtt_Screen_Cols, SLtt_Screen_Rows);
  132. return 0;
  133. }
  134. static int slang_end_graphics(caca_t *kk)
  135. {
  136. SLtt_set_mouse_mode(0, 0);
  137. SLtt_set_cursor_visibility(1);
  138. SLang_reset_tty();
  139. SLsmg_reset_smg();
  140. return 0;
  141. }
  142. static int slang_set_window_title(caca_t *kk, char const *title)
  143. {
  144. /* FIXME */
  145. return 0;
  146. }
  147. static unsigned int slang_get_window_width(caca_t *kk)
  148. {
  149. /* Fallback to a 6x10 font */
  150. return kk->qq->width * 6;
  151. }
  152. static unsigned int slang_get_window_height(caca_t *kk)
  153. {
  154. /* Fallback to a 6x10 font */
  155. return kk->qq->height * 10;
  156. }
  157. static void slang_display(caca_t *kk)
  158. {
  159. int x, y;
  160. uint8_t *attr = kk->qq->attr;
  161. uint32_t *chars = kk->qq->chars;
  162. for(y = 0; y < (int)kk->qq->height; y++)
  163. {
  164. SLsmg_gotorc(y, 0);
  165. for(x = kk->qq->width; x--; )
  166. {
  167. #if defined(OPTIMISE_SLANG_PALETTE)
  168. /* If foreground == background, just don't use this colour
  169. * pair, and print a space instead of the real character. */
  170. uint8_t fgcolor = *attr & 0xf;
  171. uint8_t bgcolor = *attr >> 4;
  172. if(fgcolor != bgcolor)
  173. {
  174. SLsmg_set_color(slang_assoc[*attr++]);
  175. SLsmg_write_char(*chars++ & 0x7f);
  176. }
  177. else
  178. {
  179. if(fgcolor == CUCUL_COLOR_BLACK)
  180. fgcolor = CUCUL_COLOR_WHITE;
  181. else if(fgcolor == CUCUL_COLOR_WHITE
  182. || fgcolor <= CUCUL_COLOR_LIGHTGRAY)
  183. fgcolor = CUCUL_COLOR_BLACK;
  184. else
  185. fgcolor = CUCUL_COLOR_WHITE;
  186. SLsmg_set_color(slang_assoc[fgcolor + 16 * bgcolor]);
  187. SLsmg_write_char(' ');
  188. chars++;
  189. attr++;
  190. }
  191. #else
  192. SLsmg_set_color(*attr++);
  193. SLsmg_write_char(*chars++ & 0x7f);
  194. #endif
  195. }
  196. }
  197. SLsmg_refresh();
  198. }
  199. static void slang_handle_resize(caca_t *kk, unsigned int *new_width,
  200. unsigned int *new_height)
  201. {
  202. SLtt_get_screen_size();
  203. *new_width = SLtt_Screen_Cols;
  204. *new_height = SLtt_Screen_Rows;
  205. if(*new_width != kk->qq->width || *new_height != kk->qq->height)
  206. SLsmg_reinit_smg();
  207. }
  208. static unsigned int slang_get_event(caca_t *kk)
  209. {
  210. unsigned int event;
  211. int intkey;
  212. if(kk->resize_event)
  213. {
  214. kk->resize_event = 0;
  215. kk->resize = 1;
  216. return CACA_EVENT_RESIZE;
  217. }
  218. if(!SLang_input_pending(0))
  219. return CACA_EVENT_NONE;
  220. /* We first use SLang_getkey() to see whether Esc was pressed
  221. * alone, then (if it wasn't) we unget the key and use SLkp_getkey()
  222. * instead, so that escape sequences are interpreted. */
  223. intkey = SLang_getkey();
  224. if(intkey != 0x1b /* Esc */ || SLang_input_pending(0))
  225. {
  226. SLang_ungetkey(intkey);
  227. intkey = SLkp_getkey();
  228. }
  229. /* If the key was ASCII, return it immediately */
  230. if(intkey < 0x100)
  231. {
  232. return CACA_EVENT_KEY_PRESS | intkey;
  233. }
  234. if(intkey == 0x3e9)
  235. {
  236. int button = (SLang_getkey() - ' ' + 1) & 0xf;
  237. unsigned int x = SLang_getkey() - '!';
  238. unsigned int y = SLang_getkey() - '!';
  239. _push_event(kk, CACA_EVENT_MOUSE_PRESS | button);
  240. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | button);
  241. if(kk->mouse_x == x && kk->mouse_y == y)
  242. return _pop_event(kk);
  243. kk->mouse_x = x;
  244. kk->mouse_y = y;
  245. return CACA_EVENT_MOUSE_MOTION | (kk->mouse_x << 12) | kk->mouse_y;
  246. }
  247. event = CACA_EVENT_KEY_PRESS;
  248. switch(intkey)
  249. {
  250. case SL_KEY_UP: return event | CACA_KEY_UP;
  251. case SL_KEY_DOWN: return event | CACA_KEY_DOWN;
  252. case SL_KEY_LEFT: return event | CACA_KEY_LEFT;
  253. case SL_KEY_RIGHT: return event | CACA_KEY_RIGHT;
  254. case SL_KEY_IC: return event | CACA_KEY_INSERT;
  255. case SL_KEY_DELETE: return event | CACA_KEY_DELETE;
  256. case SL_KEY_HOME: return event | CACA_KEY_HOME;
  257. case SL_KEY_END: return event | CACA_KEY_END;
  258. case SL_KEY_PPAGE: return event | CACA_KEY_PAGEUP;
  259. case SL_KEY_NPAGE: return event | CACA_KEY_PAGEDOWN;
  260. case SL_KEY_F(1): return event | CACA_KEY_F1;
  261. case SL_KEY_F(2): return event | CACA_KEY_F2;
  262. case SL_KEY_F(3): return event | CACA_KEY_F3;
  263. case SL_KEY_F(4): return event | CACA_KEY_F4;
  264. case SL_KEY_F(5): return event | CACA_KEY_F5;
  265. case SL_KEY_F(6): return event | CACA_KEY_F6;
  266. case SL_KEY_F(7): return event | CACA_KEY_F7;
  267. case SL_KEY_F(8): return event | CACA_KEY_F8;
  268. case SL_KEY_F(9): return event | CACA_KEY_F9;
  269. case SL_KEY_F(10): return event | CACA_KEY_F10;
  270. case SL_KEY_F(11): return event | CACA_KEY_F11;
  271. case SL_KEY_F(12): return event | CACA_KEY_F12;
  272. }
  273. return CACA_EVENT_NONE;
  274. }
  275. /*
  276. * XXX: following functions are local
  277. */
  278. static void slang_init_palette(void)
  279. {
  280. /* See SLang ref., 5.4.4. */
  281. static char *slang_colors[16] =
  282. {
  283. /* Standard colours */
  284. "black",
  285. "blue",
  286. "green",
  287. "cyan",
  288. "red",
  289. "magenta",
  290. "brown",
  291. "lightgray",
  292. /* Bright colours */
  293. "gray",
  294. "brightblue",
  295. "brightgreen",
  296. "brightcyan",
  297. "brightred",
  298. "brightmagenta",
  299. "yellow",
  300. "white",
  301. };
  302. #if defined(OPTIMISE_SLANG_PALETTE)
  303. int i;
  304. for(i = 0; i < 16 * 16; i++)
  305. SLtt_set_color(i, NULL, slang_colors[slang_palette[i * 2]],
  306. slang_colors[slang_palette[i * 2 + 1]]);
  307. #else
  308. int fg, bg;
  309. for(bg = 0; bg < 16; bg++)
  310. for(fg = 0; fg < 16; fg++)
  311. {
  312. int i = fg + 16 * bg;
  313. SLtt_set_color(i, NULL, slang_colors[fg], slang_colors[bg]);
  314. }
  315. #endif
  316. }
  317. #if defined(HAVE_SIGNAL)
  318. static RETSIGTYPE sigwinch_handler(int sig)
  319. {
  320. sigwinch_kk->resize_event = 1;
  321. signal(SIGWINCH, sigwinch_handler);;
  322. }
  323. #endif
  324. /*
  325. * Driver initialisation
  326. */
  327. void slang_init_driver(caca_t *kk)
  328. {
  329. kk->driver.driver = CACA_DRIVER_SLANG;
  330. kk->driver.init_graphics = slang_init_graphics;
  331. kk->driver.end_graphics = slang_end_graphics;
  332. kk->driver.set_window_title = slang_set_window_title;
  333. kk->driver.get_window_width = slang_get_window_width;
  334. kk->driver.get_window_height = slang_get_window_height;
  335. kk->driver.display = slang_display;
  336. kk->driver.handle_resize = slang_handle_resize;
  337. kk->driver.get_event = slang_get_event;
  338. }
  339. #endif /* USE_SLANG */