Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

192 Zeilen
4.2 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. kk->mouse_x = kk->qq->width / 2;
  69. kk->mouse_y = kk->qq->height / 2;
  70. kk->resize = 0;
  71. kk->resize_event = 0;
  72. return kk;
  73. }
  74. /** \brief Detach a caca graphical context from a cucul backend context.
  75. *
  76. * Detach a graphical context from its cucul backend and destroy it. The
  77. * libcucul canvas continues to exist and other graphical contexts can be
  78. * attached to it afterwards.
  79. *
  80. * \param qq The caca graphical context.
  81. */
  82. void caca_detach(caca_t *kk)
  83. {
  84. kk->drv.end_graphics(kk);
  85. kk->qq->refcount--;
  86. free(kk);
  87. }
  88. /*
  89. * XXX: The following functions are local.
  90. */
  91. static int caca_init_driver(caca_t *kk)
  92. {
  93. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  94. char *var = getenv("CACA_DRIVER");
  95. /* If the environment variable was set, use it */
  96. if(var && *var)
  97. {
  98. #if defined(USE_WIN32)
  99. if(!strcasecmp(var, "win32"))
  100. win32_init_driver(kk);
  101. else
  102. #endif
  103. #if defined(USE_CONIO)
  104. if(!strcasecmp(var, "conio"))
  105. conio_init_driver(kk);
  106. else
  107. #endif
  108. #if defined(USE_X11)
  109. if(!strcasecmp(var, "x11"))
  110. x11_init_driver(kk);
  111. else
  112. #endif
  113. #if defined(USE_GL)
  114. if(!strcasecmp(var, "gl"))
  115. gl_init_driver(kk);
  116. else
  117. #endif
  118. #if defined(USE_SLANG)
  119. if(!strcasecmp(var, "slang"))
  120. slang_init_driver(kk);
  121. else
  122. #endif
  123. #if defined(USE_NCURSES)
  124. if(!strcasecmp(var, "ncurses"))
  125. ncurses_init_driver(kk);
  126. else
  127. #endif
  128. return -1;
  129. return 0;
  130. }
  131. #endif
  132. #if defined(USE_WIN32)
  133. win32_init_driver(kk);
  134. return 0;
  135. #endif
  136. #if defined(USE_CONIO)
  137. conio_init_driver(kk);
  138. return 0;
  139. #endif
  140. #if defined(USE_X11)
  141. #if defined(HAVE_GETENV)
  142. if(getenv("DISPLAY") && *(getenv("DISPLAY")))
  143. #endif
  144. {
  145. x11_init_driver(kk);
  146. return 0;
  147. }
  148. #endif
  149. #if defined(USE_GL)
  150. #if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
  151. if(getenv("DISPLAY") && *(getenv("DISPLAY")))
  152. #endif
  153. {
  154. gl_init_driver(kk);
  155. return 0;
  156. }
  157. #endif
  158. #if defined(USE_SLANG)
  159. slang_init_driver(kk);
  160. return 0;
  161. #endif
  162. #if defined(USE_NCURSES)
  163. slang_init_driver(kk);
  164. return 0;
  165. #endif
  166. return -1;
  167. }