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.
 
 
 
 
 
 

216 line
4.7 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. #if !defined(__KERNEL__)
  22. # include <stdlib.h>
  23. # include <string.h>
  24. #endif
  25. #include "cucul.h"
  26. #include "cucul_internals.h"
  27. #include "caca.h"
  28. #include "caca_internals.h"
  29. static int caca_init_driver(caca_t *kk);
  30. /** \brief Attach a caca graphical context to a cucul backend context.
  31. *
  32. * Create a graphical context using device-dependent features (ncurses for
  33. * terminals, an X11 window, a DOS command window...) that attaches to a
  34. * libcucul canvas. Everything that gets drawn in the libcucul canvas can
  35. * then be displayed by the libcaca driver.
  36. *
  37. * \param qq The cucul backend context.
  38. * \return The caca graphical context or NULL if an error occurred.
  39. */
  40. caca_t * caca_attach(cucul_t * qq)
  41. {
  42. caca_t *kk = malloc(sizeof(caca_t));
  43. kk->qq = qq;
  44. if(caca_init_driver(kk))
  45. {
  46. free(kk);
  47. return NULL;
  48. }
  49. if(kk->drv.init_graphics(kk))
  50. {
  51. free(kk);
  52. return NULL;
  53. }
  54. /* Attached! */
  55. kk->qq->refcount++;
  56. /* Graphics stuff */
  57. kk->delay = 0;
  58. kk->rendertime = 0;
  59. /* Events stuff */
  60. #if defined(USE_SLANG) || defined(USE_NCURSES)
  61. kk->events.key_timer.last_sec = 0;
  62. kk->events.key_timer.last_usec = 0;
  63. kk->events.last_key_ticks = 0;
  64. kk->events.autorepeat_ticks = 0;
  65. kk->events.last_key = 0;
  66. #endif
  67. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
  68. kk->events.queue = 0;
  69. #endif
  70. kk->timer.last_sec = 0;
  71. kk->timer.last_usec = 0;
  72. kk->lastticks = 0;
  73. /* Mouse position */
  74. kk->mouse.x = kk->qq->width / 2;
  75. kk->mouse.y = kk->qq->height / 2;
  76. /* Resize events */
  77. kk->resize.resized = 0;
  78. return kk;
  79. }
  80. /** \brief Detach a caca graphical context from a cucul backend context.
  81. *
  82. * Detach a graphical context from its cucul backend and destroy it. The
  83. * libcucul canvas continues to exist and other graphical contexts can be
  84. * attached to it afterwards.
  85. *
  86. * \param qq The caca graphical context.
  87. */
  88. void caca_detach(caca_t *kk)
  89. {
  90. kk->drv.end_graphics(kk);
  91. kk->qq->refcount--;
  92. free(kk);
  93. }
  94. /*
  95. * XXX: The following functions are local.
  96. */
  97. static int caca_init_driver(caca_t *kk)
  98. {
  99. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  100. char *var = getenv("CACA_DRIVER");
  101. /* If the environment variable was set, use it */
  102. if(var && *var)
  103. {
  104. #if defined(USE_WIN32)
  105. if(!strcasecmp(var, "win32"))
  106. win32_init_driver(kk);
  107. else
  108. #endif
  109. #if defined(USE_CONIO)
  110. if(!strcasecmp(var, "conio"))
  111. conio_init_driver(kk);
  112. else
  113. #endif
  114. #if defined(USE_X11)
  115. if(!strcasecmp(var, "x11"))
  116. x11_init_driver(kk);
  117. else
  118. #endif
  119. #if defined(USE_GL)
  120. if(!strcasecmp(var, "gl"))
  121. gl_init_driver(kk);
  122. else
  123. #endif
  124. #if defined(USE_SLANG)
  125. if(!strcasecmp(var, "slang"))
  126. slang_init_driver(kk);
  127. else
  128. #endif
  129. #if defined(USE_NCURSES)
  130. if(!strcasecmp(var, "ncurses"))
  131. ncurses_init_driver(kk);
  132. else
  133. #endif
  134. #if defined(USE_NETWORK)
  135. if(!strcasecmp(var, "network"))
  136. network_init_driver(kk);
  137. else
  138. #endif
  139. #if defined(USE_VGA)
  140. if(!strcasecmp(var, "vga"))
  141. vga_init_driver(kk);
  142. else
  143. #endif
  144. return -1;
  145. return 0;
  146. }
  147. #endif
  148. #if defined(USE_WIN32)
  149. win32_init_driver(kk);
  150. return 0;
  151. #endif
  152. #if defined(USE_CONIO)
  153. conio_init_driver(kk);
  154. return 0;
  155. #endif
  156. #if defined(USE_VGA)
  157. vga_init_driver(kk);
  158. return 0;
  159. #endif
  160. #if defined(USE_X11)
  161. #if defined(HAVE_GETENV)
  162. if(getenv("DISPLAY") && *(getenv("DISPLAY")))
  163. #endif
  164. {
  165. x11_init_driver(kk);
  166. return 0;
  167. }
  168. #endif
  169. #if defined(USE_GL)
  170. #if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
  171. if(getenv("DISPLAY") && *(getenv("DISPLAY")))
  172. #endif
  173. {
  174. gl_init_driver(kk);
  175. return 0;
  176. }
  177. #endif
  178. #if defined(USE_SLANG)
  179. slang_init_driver(kk);
  180. return 0;
  181. #endif
  182. #if defined(USE_NCURSES)
  183. ncurses_init_driver(kk);
  184. return 0;
  185. #endif
  186. #if defined(USE_NETWORK)
  187. network_init_driver(kk);
  188. return 0;
  189. #endif
  190. return -1;
  191. }