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ů.
 
 
 
 
 
 

253 řádky
5.8 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 caca.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief Main \e libcaca functions
  15. *
  16. * This file contains the main functions used by \e libcaca applications to
  17. * initialise the library, get the screen properties, set the framerate and
  18. * so on.
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "cucul.h"
  24. #include "cucul_internals.h"
  25. #include "caca.h"
  26. #include "caca_internals.h"
  27. static int caca_init_driver(caca_t *kk);
  28. static void caca_check_terminal(caca_t *kk);
  29. /** \brief Attach a caca graphical context to a cucul backend context.
  30. *
  31. * Create a graphical context using device-dependent features (ncurses for
  32. * terminals, an X11 window, a DOS command window...) that attaches to a
  33. * libcucul canvas. Everything that gets drawn in the libcucul canvas can
  34. * then be displayed by the libcaca driver.
  35. *
  36. * \param qq The cucul backend context.
  37. * \return The caca graphical context or NULL if an error occurred.
  38. */
  39. caca_t * caca_attach(cucul_t * qq)
  40. {
  41. caca_t *kk = malloc(sizeof(caca_t));
  42. kk->qq = qq;
  43. if(caca_init_driver(kk))
  44. {
  45. free(kk);
  46. return NULL;
  47. }
  48. /* Only needed for slang and ncurses */
  49. caca_check_terminal(kk);
  50. if(kk->driver.init_graphics(kk))
  51. {
  52. free(kk);
  53. return NULL;
  54. }
  55. /* Attached! */
  56. kk->qq->refcount++;
  57. /* Graphics stuff */
  58. kk->delay = 0;
  59. kk->rendertime = 0;
  60. /* Events stuff */
  61. #if defined(USE_SLANG) || defined(USE_NCURSES)
  62. kk->events.key_timer.last_sec = 0;
  63. kk->events.key_timer.last_usec = 0;
  64. kk->events.last_key_ticks = 0;
  65. kk->events.autorepeat_ticks = 0;
  66. kk->events.last_key = 0;
  67. #endif
  68. kk->timer.last_sec = 0;
  69. kk->timer.last_usec = 0;
  70. kk->lastticks = 0;
  71. kk->resize = 0;
  72. kk->resize_event = 0;
  73. return kk;
  74. }
  75. /** \brief Detach a caca graphical context from a cucul backend context.
  76. *
  77. * Detach a graphical context from its cucul backend and destroy it. The
  78. * libcucul canvas continues to exist and other graphical contexts can be
  79. * attached to it afterwards.
  80. *
  81. * \param qq The caca graphical context.
  82. */
  83. void caca_detach(caca_t *kk)
  84. {
  85. kk->driver.end_graphics(kk);
  86. kk->qq->refcount--;
  87. free(kk);
  88. }
  89. /*
  90. * XXX: The following functions are local.
  91. */
  92. static int caca_init_driver(caca_t *kk)
  93. {
  94. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  95. char *var = getenv("CACA_DRIVER");
  96. /* If the environment variable was set, use it */
  97. if(var && *var)
  98. {
  99. #if defined(USE_WIN32)
  100. if(!strcasecmp(var, "win32"))
  101. win32_init_driver(kk);
  102. else
  103. #endif
  104. #if defined(USE_CONIO)
  105. if(!strcasecmp(var, "conio"))
  106. conio_init_driver(kk);
  107. else
  108. #endif
  109. #if defined(USE_X11)
  110. if(!strcasecmp(var, "x11"))
  111. x11_init_driver(kk);
  112. else
  113. #endif
  114. #if defined(USE_GL)
  115. if(!strcasecmp(var, "gl"))
  116. gl_init_driver(kk);
  117. else
  118. #endif
  119. #if defined(USE_SLANG)
  120. if(!strcasecmp(var, "slang"))
  121. slang_init_driver(kk);
  122. else
  123. #endif
  124. #if defined(USE_NCURSES)
  125. if(!strcasecmp(var, "ncurses"))
  126. ncurses_init_driver(kk);
  127. else
  128. #endif
  129. return -1;
  130. return 0;
  131. }
  132. #endif
  133. #if defined(USE_WIN32)
  134. win32_init_driver(kk);
  135. return 0;
  136. #endif
  137. #if defined(USE_CONIO)
  138. conio_init_driver(kk);
  139. return 0;
  140. #endif
  141. #if defined(USE_X11)
  142. #if defined(HAVE_GETENV)
  143. if(getenv("DISPLAY") && *(getenv("DISPLAY")))
  144. #endif
  145. {
  146. x11_init_driver(kk);
  147. return 0;
  148. }
  149. #endif
  150. #if defined(USE_GL)
  151. #if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
  152. if(getenv("DISPLAY") && *(getenv("DISPLAY")))
  153. #endif
  154. {
  155. gl_init_driver(kk);
  156. return 0;
  157. }
  158. #endif
  159. #if defined(USE_SLANG)
  160. slang_init_driver(kk);
  161. return 0;
  162. #endif
  163. #if defined(USE_NCURSES)
  164. slang_init_driver(kk);
  165. return 0;
  166. #endif
  167. return -1;
  168. }
  169. static void caca_check_terminal(caca_t *kk)
  170. {
  171. #if defined(HAVE_GETENV) && defined(HAVE_PUTENV) && \
  172. (defined(USE_SLANG) || defined(USE_NCURSES))
  173. char *term, *colorterm, *other;
  174. #endif
  175. #if defined(USE_SLANG)
  176. if(kk->driver.driver != CACA_DRIVER_SLANG)
  177. #endif
  178. #if defined(USE_NCURSES)
  179. if(kk->driver.driver != CACA_DRIVER_NCURSES)
  180. #endif
  181. return;
  182. #if defined(HAVE_GETENV) && defined(HAVE_PUTENV) && \
  183. (defined(USE_SLANG) || defined(USE_NCURSES))
  184. term = getenv("TERM");
  185. colorterm = getenv("COLORTERM");
  186. if(term && !strcmp(term, "xterm"))
  187. {
  188. /* If we are using gnome-terminal, it's really a 16 colour terminal */
  189. if(colorterm && !strcmp(colorterm, "gnome-terminal"))
  190. {
  191. #if defined(USE_NCURSES)
  192. if(kk->driver.driver == CACA_DRIVER_NCURSES)
  193. {
  194. SCREEN *screen;
  195. screen = newterm("xterm-16color", stdout, stdin);
  196. if(screen == NULL)
  197. return;
  198. endwin();
  199. }
  200. #endif
  201. (void)putenv("TERM=xterm-16color");
  202. return;
  203. }
  204. /* Ditto if we are using Konsole */
  205. other = getenv("KONSOLE_DCOP_SESSION");
  206. if(other)
  207. {
  208. #if defined(USE_NCURSES)
  209. if(kk->driver.driver == CACA_DRIVER_NCURSES)
  210. {
  211. SCREEN *screen;
  212. screen = newterm("xterm-16color", stdout, stdin);
  213. if(screen == NULL)
  214. return;
  215. endwin();
  216. }
  217. #endif
  218. (void)putenv("TERM=xterm-16color");
  219. return;
  220. }
  221. }
  222. #endif
  223. }