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.
 
 
 
 
 
 

173 rivejä
4.1 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains the main functions used by \e libcaca applications to
  15. * initialise the library, get the screen properties, set the framerate and
  16. * so on.
  17. */
  18. #include "config.h"
  19. #if !defined(__KERNEL__)
  20. # include <stdlib.h>
  21. # include <string.h>
  22. #endif
  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_event.type = CACA_EVENT_NONE;
  64. #endif
  65. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
  66. kk->events.queue = 0;
  67. #endif
  68. kk->timer.last_sec = 0;
  69. kk->timer.last_usec = 0;
  70. kk->lastticks = 0;
  71. /* Mouse position */
  72. kk->mouse.x = kk->qq->width / 2;
  73. kk->mouse.y = kk->qq->height / 2;
  74. /* Resize events */
  75. kk->resize.resized = 0;
  76. return kk;
  77. }
  78. /** \brief Detach a caca graphical context from a cucul backend context.
  79. *
  80. * Detach a graphical context from its cucul backend and destroy it. The
  81. * libcucul canvas continues to exist and other graphical contexts can be
  82. * attached to it afterwards.
  83. *
  84. * \param kk The libcaca graphical context.
  85. */
  86. void caca_detach(caca_t *kk)
  87. {
  88. kk->drv.end_graphics(kk);
  89. kk->qq->refcount--;
  90. free(kk);
  91. }
  92. /*
  93. * XXX: The following functions are local.
  94. */
  95. static int caca_init_driver(caca_t *kk)
  96. {
  97. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  98. char *var = getenv("CACA_DRIVER");
  99. /* If the environment variable was set, use it */
  100. if(var && *var)
  101. {
  102. #if defined(USE_WIN32)
  103. if(!strcasecmp(var, "win32")) return win32_install(kk);
  104. #endif
  105. #if defined(USE_CONIO)
  106. if(!strcasecmp(var, "conio")) return conio_install(kk);
  107. #endif
  108. #if defined(USE_X11)
  109. if(!strcasecmp(var, "x11")) return x11_install(kk);
  110. #endif
  111. #if defined(USE_GL)
  112. if(!strcasecmp(var, "gl")) return gl_install(kk);
  113. #endif
  114. if(!strcasecmp(var, "raw")) return raw_install(kk);
  115. #if defined(USE_SLANG)
  116. if(!strcasecmp(var, "slang")) return slang_install(kk);
  117. #endif
  118. #if defined(USE_NCURSES)
  119. if(!strcasecmp(var, "ncurses")) return ncurses_install(kk);
  120. #endif
  121. #if defined(USE_VGA)
  122. if(!strcasecmp(var, "vga")) return vga_install(kk);
  123. #endif
  124. return -1;
  125. }
  126. #endif
  127. #if defined(USE_WIN32)
  128. if(win32_install(kk) == 0) return 0;
  129. #endif
  130. #if defined(USE_CONIO)
  131. if(conio_install(kk) == 0) return 0;
  132. #endif
  133. #if defined(USE_VGA)
  134. if(vga_install(kk) == 0) return 0;
  135. #endif
  136. #if defined(USE_X11)
  137. if(x11_install(kk) == 0) return 0;
  138. #endif
  139. #if defined(USE_GL)
  140. if(gl_install(kk) == 0) return 0;
  141. #endif
  142. #if defined(USE_SLANG)
  143. if(slang_install(kk) == 0) return 0;
  144. #endif
  145. #if defined(USE_NCURSES)
  146. if(ncurses_install(kk) == 0) return 0;
  147. #endif
  148. return -1;
  149. }