Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

driver_slang.c 9.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 <stdio.h> /* BUFSIZ */
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #if defined(HAVE_UNISTD_H)
  29. # include <unistd.h>
  30. #endif
  31. #include <stdarg.h>
  32. #if defined(HAVE_SIGNAL_H)
  33. # include <signal.h>
  34. #endif
  35. #include "caca.h"
  36. #include "caca_internals.h"
  37. #include "cucul.h"
  38. #include "cucul_internals.h"
  39. /*
  40. * Global variables
  41. */
  42. /* Tables generated by test/optipal.c */
  43. static int const slang_palette[2*16*16] =
  44. {
  45. 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0,
  46. 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 0, 0, 8,
  47. 8, 7, 7, 8, 15, 7, 7, 15, 15, 9, 9, 15, 1, 9, 9, 1,
  48. 7, 9, 9, 7, 8, 1, 1, 8, 0, 1, 15, 10, 10, 15, 2, 10,
  49. 10, 2, 7, 10, 10, 7, 8, 2, 2, 8, 0, 2, 15, 11, 11, 15,
  50. 3, 11, 11, 3, 7, 11, 11, 7, 8, 3, 3, 8, 0, 3, 15, 12,
  51. 12, 15, 4, 12, 12, 4, 7, 12, 12, 7, 8, 4, 4, 8, 0, 4,
  52. 15, 13, 13, 15, 5, 13, 13, 5, 7, 13, 13, 7, 8, 5, 5, 8,
  53. 0, 5, 15, 14, 14, 15, 6, 14, 14, 6, 7, 14, 14, 7, 8, 6,
  54. 6, 8, 0, 6, 4, 6, 6, 4, 12, 14, 14, 12, 6, 2, 2, 6,
  55. 14, 10, 10, 14, 2, 3, 3, 2, 10, 11, 11, 10, 3, 1, 1, 3,
  56. 11, 9, 9, 11, 1, 5, 5, 1, 9, 13, 13, 9, 5, 4, 4, 5,
  57. 13, 12, 12, 13, 4, 14, 6, 12, 12, 6, 14, 4, 6, 10, 2, 14,
  58. 14, 2, 10, 6, 2, 11, 3, 10, 10, 3, 11, 2, 3, 9, 1, 11,
  59. 11, 1, 9, 3, 1, 13, 5, 9, 9, 5, 13, 1, 5, 12, 4, 13,
  60. 13, 4, 12, 5, 0, 7, 0, 15, 15, 8, 8, 15, 15, 1, 7, 1,
  61. 1, 6, 2, 5, 3, 4, 4, 3, 5, 2, 6, 1, 0, 0, 1, 1,
  62. 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 2, 2, 3, 3,
  63. 4, 4, 5, 5, 6, 6, 7, 7, 14, 9, 1, 15, 8, 9, 8, 8,
  64. 9, 9, 1, 7, 0, 9, 9, 8, 6, 9, 13, 10, 2, 15, 8, 10,
  65. 7, 2, 15, 2, 2, 7, 0, 10, 10, 8, 5, 10, 12, 11, 3, 15,
  66. 8, 11, 7, 3, 15, 3, 3, 7, 0, 11, 11, 8, 4, 11, 11, 12,
  67. 4, 15, 8, 12, 7, 4, 15, 4, 4, 7, 0, 12, 12, 8, 3, 12,
  68. 10, 13, 5, 15, 8, 13, 7, 5, 15, 5, 5, 7, 0, 13, 13, 8,
  69. 2, 13, 9, 14, 6, 15, 8, 14, 7, 6, 15, 6, 6, 7, 0, 14,
  70. 14, 8, 1, 14, 5, 6, 2, 4, 13, 14, 10, 12, 4, 2, 3, 6,
  71. 12, 10, 11, 14, 6, 3, 1, 2, 14, 11, 9, 10, 2, 1, 5, 3,
  72. 10, 9, 13, 11, 3, 5, 4, 1, 11, 13, 12, 9, 1, 4, 6, 5,
  73. 9, 12, 14, 13, 5, 14, 2, 12, 13, 6, 10, 4, 4, 10, 3, 14,
  74. 12, 2, 11, 6, 6, 11, 1, 10, 14, 3, 9, 2, 2, 9, 5, 11,
  75. 10, 1, 13, 3, 3, 13, 4, 9, 11, 5, 12, 1, 1, 12, 6, 13,
  76. 9, 4, 14, 5, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15,
  77. };
  78. static int const slang_assoc[16*16] =
  79. {
  80. 134, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  81. 28, 135, 214, 86, 219, 91, 133, 127, 26, 23, 240, 112, 245, 117, 141, 126,
  82. 37, 211, 142, 83, 206, 132, 78, 160, 35, 237, 32, 109, 232, 140, 104, 161,
  83. 46, 87, 82, 143, 131, 215, 210, 169, 44, 113, 108, 41, 139, 241, 236, 170,
  84. 55, 222, 203, 130, 144, 94, 75, 178, 53, 248, 229, 138, 50, 120, 101, 179,
  85. 64, 90, 129, 218, 95, 145, 223, 187, 62, 116, 137, 244, 121, 59, 249, 188,
  86. 73, 128, 79, 207, 74, 202, 146, 196, 71, 136, 105, 233, 100, 228, 68, 197,
  87. 122, 153, 162, 171, 180, 189, 198, 147, 16, 25, 34, 43, 52, 61, 70, 18,
  88. 15, 27, 36, 45, 54, 63, 72, 17, 151, 155, 164, 173, 182, 191, 200, 124,
  89. 154, 22, 238, 110, 243, 115, 156, 24, 150, 152, 216, 88, 221, 93, 148, 20,
  90. 163, 235, 31, 107, 230, 165, 102, 33, 159, 213, 250, 85, 208, 157, 80, 29,
  91. 172, 111, 106, 40, 174, 239, 234, 42, 168, 89, 84, 251, 166, 217, 212, 38,
  92. 181, 246, 227, 183, 49, 118, 99, 51, 177, 224, 205, 175, 252, 96, 77, 47,
  93. 190, 114, 192, 242, 119, 58, 247, 60, 186, 92, 184, 220, 97, 253, 225, 56,
  94. 199, 201, 103, 231, 98, 226, 67, 69, 195, 193, 81, 209, 76, 204, 254, 65,
  95. 123, 149, 158, 167, 176, 185, 194, 19, 125, 21, 30, 39, 48, 57, 66, 255,
  96. };
  97. /*
  98. * Local functions
  99. */
  100. static void slang_init_palette(void);
  101. #if defined(HAVE_SIGNAL)
  102. static RETSIGTYPE sigwinch_handler(int);
  103. static caca_t *sigwinch_kk; /* FIXME: we ought to get rid of this */
  104. #endif
  105. static int slang_init_graphics(caca_t *kk)
  106. {
  107. #if defined(HAVE_SIGNAL)
  108. sigwinch_kk = kk;
  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. cucul_set_size(kk->qq, SLtt_Screen_Cols, SLtt_Screen_Rows);
  139. return 0;
  140. }
  141. static int slang_end_graphics(caca_t *kk)
  142. {
  143. SLtt_set_mouse_mode(0, 0);
  144. SLtt_set_cursor_visibility(1);
  145. SLang_reset_tty();
  146. SLsmg_reset_smg();
  147. return 0;
  148. }
  149. static int slang_set_window_title(caca_t *kk, char const *title)
  150. {
  151. /* FIXME */
  152. return 0;
  153. }
  154. static unsigned int slang_get_window_width(caca_t *kk)
  155. {
  156. /* Fallback to a 6x10 font */
  157. return kk->qq->width * 6;
  158. }
  159. static unsigned int slang_get_window_height(caca_t *kk)
  160. {
  161. /* Fallback to a 6x10 font */
  162. return kk->qq->height * 10;
  163. }
  164. static void slang_display(caca_t *kk)
  165. {
  166. int x, y;
  167. uint8_t *attr = kk->qq->attr;
  168. uint32_t *chars = kk->qq->chars;
  169. for(y = 0; y < (int)kk->qq->height; y++)
  170. {
  171. SLsmg_gotorc(y, 0);
  172. for(x = kk->qq->width; x--; )
  173. {
  174. #if defined(OPTIMISE_SLANG_PALETTE)
  175. /* If foreground == background, just don't use this colour
  176. * pair, and print a space instead of the real character. */
  177. uint8_t fgcolor = *attr & 0xf;
  178. uint8_t bgcolor = *attr >> 4;
  179. if(fgcolor != bgcolor)
  180. {
  181. SLsmg_set_color(slang_assoc[*attr++]);
  182. SLsmg_write_char(*chars++ & 0x7f);
  183. }
  184. else
  185. {
  186. if(fgcolor == CUCUL_COLOR_BLACK)
  187. fgcolor = CUCUL_COLOR_WHITE;
  188. else if(fgcolor == CUCUL_COLOR_WHITE
  189. || fgcolor <= CUCUL_COLOR_LIGHTGRAY)
  190. fgcolor = CUCUL_COLOR_BLACK;
  191. else
  192. fgcolor = CUCUL_COLOR_WHITE;
  193. SLsmg_set_color(slang_assoc[fgcolor + 16 * bgcolor]);
  194. SLsmg_write_char(' ');
  195. chars++;
  196. attr++;
  197. }
  198. #else
  199. SLsmg_set_color(*attr++);
  200. SLsmg_write_char(*chars++ & 0x7f);
  201. #endif
  202. }
  203. }
  204. SLsmg_refresh();
  205. }
  206. static void slang_handle_resize(caca_t *kk, unsigned int *new_width,
  207. unsigned int *new_height)
  208. {
  209. SLtt_get_screen_size();
  210. *new_width = SLtt_Screen_Cols;
  211. *new_height = SLtt_Screen_Rows;
  212. if(*new_width != kk->qq->width || *new_height != kk->qq->height)
  213. SLsmg_reinit_smg();
  214. }
  215. /*
  216. * XXX: following functions are local
  217. */
  218. static void slang_init_palette(void)
  219. {
  220. /* See SLang ref., 5.4.4. */
  221. static char *slang_colors[16] =
  222. {
  223. /* Standard colours */
  224. "black",
  225. "blue",
  226. "green",
  227. "cyan",
  228. "red",
  229. "magenta",
  230. "brown",
  231. "lightgray",
  232. /* Bright colours */
  233. "gray",
  234. "brightblue",
  235. "brightgreen",
  236. "brightcyan",
  237. "brightred",
  238. "brightmagenta",
  239. "yellow",
  240. "white",
  241. };
  242. #if defined(OPTIMISE_SLANG_PALETTE)
  243. int i;
  244. for(i = 0; i < 16 * 16; i++)
  245. SLtt_set_color(i, NULL, slang_colors[slang_palette[i * 2]],
  246. slang_colors[slang_palette[i * 2 + 1]]);
  247. #else
  248. int fg, bg;
  249. for(bg = 0; bg < 16; bg++)
  250. for(fg = 0; fg < 16; fg++)
  251. {
  252. int i = fg + 16 * bg;
  253. SLtt_set_color(i, NULL, slang_colors[fg], slang_colors[bg]);
  254. }
  255. #endif
  256. }
  257. #if defined(HAVE_SIGNAL)
  258. static RETSIGTYPE sigwinch_handler(int sig)
  259. {
  260. sigwinch_kk->resize_event = 1;
  261. signal(SIGWINCH, sigwinch_handler);;
  262. }
  263. #endif
  264. /*
  265. * Driver initialisation
  266. */
  267. void slang_init_driver(caca_t *kk)
  268. {
  269. kk->driver.driver = CACA_DRIVER_SLANG;
  270. kk->driver.init_graphics = slang_init_graphics;
  271. kk->driver.end_graphics = slang_end_graphics;
  272. kk->driver.set_window_title = slang_set_window_title;
  273. kk->driver.get_window_width = slang_get_window_width;
  274. kk->driver.get_window_height = slang_get_window_height;
  275. kk->driver.display = slang_display;
  276. kk->driver.handle_resize = slang_handle_resize;
  277. }
  278. #endif /* USE_SLANG */