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.
 
 
 
 
 
 

256 regels
5.9 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->mouse_x = kk->qq->width / 2;
  72. kk->mouse_y = kk->qq->height / 2;
  73. kk->resize = 0;
  74. kk->resize_event = 0;
  75. return kk;
  76. }
  77. /** \brief Detach a caca graphical context from a cucul backend context.
  78. *
  79. * Detach a graphical context from its cucul backend and destroy it. The
  80. * libcucul canvas continues to exist and other graphical contexts can be
  81. * attached to it afterwards.
  82. *
  83. * \param qq The caca graphical context.
  84. */
  85. void caca_detach(caca_t *kk)
  86. {
  87. kk->driver.end_graphics(kk);
  88. kk->qq->refcount--;
  89. free(kk);
  90. }
  91. /*
  92. * XXX: The following functions are local.
  93. */
  94. static int caca_init_driver(caca_t *kk)
  95. {
  96. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  97. char *var = getenv("CACA_DRIVER");
  98. /* If the environment variable was set, use it */
  99. if(var && *var)
  100. {
  101. #if defined(USE_WIN32)
  102. if(!strcasecmp(var, "win32"))
  103. win32_init_driver(kk);
  104. else
  105. #endif
  106. #if defined(USE_CONIO)
  107. if(!strcasecmp(var, "conio"))
  108. conio_init_driver(kk);
  109. else
  110. #endif
  111. #if defined(USE_X11)
  112. if(!strcasecmp(var, "x11"))
  113. x11_init_driver(kk);
  114. else
  115. #endif
  116. #if defined(USE_GL)
  117. if(!strcasecmp(var, "gl"))
  118. gl_init_driver(kk);
  119. else
  120. #endif
  121. #if defined(USE_SLANG)
  122. if(!strcasecmp(var, "slang"))
  123. slang_init_driver(kk);
  124. else
  125. #endif
  126. #if defined(USE_NCURSES)
  127. if(!strcasecmp(var, "ncurses"))
  128. ncurses_init_driver(kk);
  129. else
  130. #endif
  131. return -1;
  132. return 0;
  133. }
  134. #endif
  135. #if defined(USE_WIN32)
  136. win32_init_driver(kk);
  137. return 0;
  138. #endif
  139. #if defined(USE_CONIO)
  140. conio_init_driver(kk);
  141. return 0;
  142. #endif
  143. #if defined(USE_X11)
  144. #if defined(HAVE_GETENV)
  145. if(getenv("DISPLAY") && *(getenv("DISPLAY")))
  146. #endif
  147. {
  148. x11_init_driver(kk);
  149. return 0;
  150. }
  151. #endif
  152. #if defined(USE_GL)
  153. #if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
  154. if(getenv("DISPLAY") && *(getenv("DISPLAY")))
  155. #endif
  156. {
  157. gl_init_driver(kk);
  158. return 0;
  159. }
  160. #endif
  161. #if defined(USE_SLANG)
  162. slang_init_driver(kk);
  163. return 0;
  164. #endif
  165. #if defined(USE_NCURSES)
  166. slang_init_driver(kk);
  167. return 0;
  168. #endif
  169. return -1;
  170. }
  171. static void caca_check_terminal(caca_t *kk)
  172. {
  173. #if defined(HAVE_GETENV) && defined(HAVE_PUTENV) && \
  174. (defined(USE_SLANG) || defined(USE_NCURSES))
  175. char *term, *colorterm, *other;
  176. #endif
  177. #if defined(USE_SLANG)
  178. if(kk->driver.driver != CACA_DRIVER_SLANG)
  179. #endif
  180. #if defined(USE_NCURSES)
  181. if(kk->driver.driver != CACA_DRIVER_NCURSES)
  182. #endif
  183. return;
  184. #if defined(HAVE_GETENV) && defined(HAVE_PUTENV) && \
  185. (defined(USE_SLANG) || defined(USE_NCURSES))
  186. term = getenv("TERM");
  187. colorterm = getenv("COLORTERM");
  188. if(term && !strcmp(term, "xterm"))
  189. {
  190. /* If we are using gnome-terminal, it's really a 16 colour terminal */
  191. if(colorterm && !strcmp(colorterm, "gnome-terminal"))
  192. {
  193. #if defined(USE_NCURSES)
  194. if(kk->driver.driver == CACA_DRIVER_NCURSES)
  195. {
  196. SCREEN *screen;
  197. screen = newterm("xterm-16color", stdout, stdin);
  198. if(screen == NULL)
  199. return;
  200. endwin();
  201. }
  202. #endif
  203. (void)putenv("TERM=xterm-16color");
  204. return;
  205. }
  206. /* Ditto if we are using Konsole */
  207. other = getenv("KONSOLE_DCOP_SESSION");
  208. if(other)
  209. {
  210. #if defined(USE_NCURSES)
  211. if(kk->driver.driver == CACA_DRIVER_NCURSES)
  212. {
  213. SCREEN *screen;
  214. screen = newterm("xterm-16color", stdout, stdin);
  215. if(screen == NULL)
  216. return;
  217. endwin();
  218. }
  219. #endif
  220. (void)putenv("TERM=xterm-16color");
  221. return;
  222. }
  223. }
  224. #endif
  225. }