Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

18 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 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(!strcasecmp(var, "raw")) return raw_install(kk);
  117. #if defined(USE_SLANG)
  118. if(!strcasecmp(var, "slang")) return slang_install(kk);
  119. #endif
  120. #if defined(USE_NCURSES)
  121. if(!strcasecmp(var, "ncurses")) return ncurses_install(kk);
  122. #endif
  123. #if defined(USE_VGA)
  124. if(!strcasecmp(var, "vga")) return vga_install(kk);
  125. #endif
  126. return -1;
  127. }
  128. #endif
  129. #if defined(USE_WIN32)
  130. if(win32_install(kk) == 0) return 0;
  131. #endif
  132. #if defined(USE_CONIO)
  133. if(conio_install(kk) == 0) return 0;
  134. #endif
  135. #if defined(USE_VGA)
  136. if(vga_install(kk) == 0) return 0;
  137. #endif
  138. #if defined(USE_X11)
  139. if(x11_install(kk) == 0) return 0;
  140. #endif
  141. #if defined(USE_GL)
  142. if(gl_install(kk) == 0) return 0;
  143. #endif
  144. #if defined(USE_SLANG)
  145. if(slang_install(kk) == 0) return 0;
  146. #endif
  147. #if defined(USE_NCURSES)
  148. if(ncurses_install(kk) == 0) return 0;
  149. #endif
  150. return -1;
  151. }