Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

180 lignes
4.3 KiB

  1. /*
  2. * libcaca Colour 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_event.type = CACA_EVENT_NONE;
  66. #endif
  67. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
  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")) return win32_install(kk);
  106. #endif
  107. #if defined(USE_CONIO)
  108. if(!strcasecmp(var, "conio")) return conio_install(kk);
  109. #endif
  110. #if defined(USE_X11)
  111. if(!strcasecmp(var, "x11")) return x11_install(kk);
  112. #endif
  113. #if defined(USE_GL)
  114. if(!strcasecmp(var, "gl")) return gl_install(kk);
  115. #endif
  116. #if defined(USE_SLANG)
  117. if(!strcasecmp(var, "slang")) return slang_install(kk);
  118. #endif
  119. #if defined(USE_NCURSES)
  120. if(!strcasecmp(var, "ncurses")) return ncurses_install(kk);
  121. #endif
  122. #if defined(USE_NETWORK)
  123. if(!strcasecmp(var, "network")) return network_install(kk);
  124. #endif
  125. #if defined(USE_VGA)
  126. if(!strcasecmp(var, "vga")) return vga_install(kk);
  127. #endif
  128. return -1;
  129. }
  130. #endif
  131. #if defined(USE_WIN32)
  132. if(win32_install(kk) == 0) return 0;
  133. #endif
  134. #if defined(USE_CONIO)
  135. if(conio_install(kk) == 0) return 0;
  136. #endif
  137. #if defined(USE_VGA)
  138. if(vga_install(kk) == 0) return 0;
  139. #endif
  140. #if defined(USE_X11)
  141. if(x11_install(kk) == 0) return 0;
  142. #endif
  143. #if defined(USE_GL)
  144. if(gl_install(kk) == 0) return 0;
  145. #endif
  146. #if defined(USE_SLANG)
  147. if(slang_install(kk) == 0) return 0;
  148. #endif
  149. #if defined(USE_NCURSES)
  150. if(ncurses_install(kk) == 0) return 0;
  151. #endif
  152. #if defined(USE_NETWORK)
  153. if(network_install(kk) == 0) return 0;
  154. #endif
  155. return -1;
  156. }