選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

202 行
4.4 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. /** \brief Attach a caca graphical context to a cucul backend context.
  29. *
  30. * Create a graphical context using device-dependent features (ncurses for
  31. * terminals, an X11 window, a DOS command window...) that attaches to a
  32. * libcucul canvas. Everything that gets drawn in the libcucul canvas can
  33. * then be displayed by the libcaca driver.
  34. *
  35. * \param qq The cucul backend context.
  36. * \return The caca graphical context or NULL if an error occurred.
  37. */
  38. caca_t * caca_attach(cucul_t * qq)
  39. {
  40. caca_t *kk = malloc(sizeof(caca_t));
  41. kk->qq = qq;
  42. if(caca_init_driver(kk))
  43. {
  44. free(kk);
  45. return NULL;
  46. }
  47. if(kk->drv.init_graphics(kk))
  48. {
  49. free(kk);
  50. return NULL;
  51. }
  52. /* Attached! */
  53. kk->qq->refcount++;
  54. /* Graphics stuff */
  55. kk->delay = 0;
  56. kk->rendertime = 0;
  57. /* Events stuff */
  58. #if defined(USE_SLANG) || defined(USE_NCURSES)
  59. kk->events.key_timer.last_sec = 0;
  60. kk->events.key_timer.last_usec = 0;
  61. kk->events.last_key_ticks = 0;
  62. kk->events.autorepeat_ticks = 0;
  63. kk->events.last_key = 0;
  64. #endif
  65. kk->timer.last_sec = 0;
  66. kk->timer.last_usec = 0;
  67. kk->lastticks = 0;
  68. /* Mouse position */
  69. kk->mouse.x = kk->qq->width / 2;
  70. kk->mouse.y = kk->qq->height / 2;
  71. /* Resize events */
  72. kk->resize.resized = 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->drv.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. #if defined(USE_NETWORK)
  130. if(!strcasecmp(var, "network"))
  131. network_init_driver(kk);
  132. else
  133. #endif
  134. return -1;
  135. return 0;
  136. }
  137. #endif
  138. #if defined(USE_WIN32)
  139. win32_init_driver(kk);
  140. return 0;
  141. #endif
  142. #if defined(USE_CONIO)
  143. conio_init_driver(kk);
  144. return 0;
  145. #endif
  146. #if defined(USE_X11)
  147. #if defined(HAVE_GETENV)
  148. if(getenv("DISPLAY") && *(getenv("DISPLAY")))
  149. #endif
  150. {
  151. x11_init_driver(kk);
  152. return 0;
  153. }
  154. #endif
  155. #if defined(USE_GL)
  156. #if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
  157. if(getenv("DISPLAY") && *(getenv("DISPLAY")))
  158. #endif
  159. {
  160. gl_init_driver(kk);
  161. return 0;
  162. }
  163. #endif
  164. #if defined(USE_SLANG)
  165. slang_init_driver(kk);
  166. return 0;
  167. #endif
  168. #if defined(USE_NCURSES)
  169. slang_init_driver(kk);
  170. return 0;
  171. #endif
  172. #if defined(USE_NETWORK)
  173. network_init_driver(kk);
  174. return 0;
  175. #endif
  176. return -1;
  177. }