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.
 
 
 
 
 
 

213 lines
4.6 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. kk->timer.last_sec = 0;
  68. kk->timer.last_usec = 0;
  69. kk->lastticks = 0;
  70. /* Mouse position */
  71. kk->mouse.x = kk->qq->width / 2;
  72. kk->mouse.y = kk->qq->height / 2;
  73. /* Resize events */
  74. kk->resize.resized = 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->drv.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. #if defined(USE_NETWORK)
  132. if(!strcasecmp(var, "network"))
  133. network_init_driver(kk);
  134. else
  135. #endif
  136. #if defined(USE_VGA)
  137. if(!strcasecmp(var, "vga"))
  138. vga_init_driver(kk);
  139. else
  140. #endif
  141. return -1;
  142. return 0;
  143. }
  144. #endif
  145. #if defined(USE_WIN32)
  146. win32_init_driver(kk);
  147. return 0;
  148. #endif
  149. #if defined(USE_CONIO)
  150. conio_init_driver(kk);
  151. return 0;
  152. #endif
  153. #if defined(USE_VGA)
  154. vga_init_driver(kk);
  155. return 0;
  156. #endif
  157. #if defined(USE_X11)
  158. #if defined(HAVE_GETENV)
  159. if(getenv("DISPLAY") && *(getenv("DISPLAY")))
  160. #endif
  161. {
  162. x11_init_driver(kk);
  163. return 0;
  164. }
  165. #endif
  166. #if defined(USE_GL)
  167. #if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
  168. if(getenv("DISPLAY") && *(getenv("DISPLAY")))
  169. #endif
  170. {
  171. gl_init_driver(kk);
  172. return 0;
  173. }
  174. #endif
  175. #if defined(USE_SLANG)
  176. slang_init_driver(kk);
  177. return 0;
  178. #endif
  179. #if defined(USE_NCURSES)
  180. slang_init_driver(kk);
  181. return 0;
  182. #endif
  183. #if defined(USE_NETWORK)
  184. network_init_driver(kk);
  185. return 0;
  186. #endif
  187. return -1;
  188. }