Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

173 рядки
4.2 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_display_t *dp);
  28. /** \brief Attach a caca graphical context to a cucul canvas.
  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 cv The cucul cavas.
  36. * \return The caca graphical context or NULL if an error occurred.
  37. */
  38. caca_display_t * caca_create_display(cucul_canvas_t * cv)
  39. {
  40. caca_display_t *dp = malloc(sizeof(caca_display_t));
  41. dp->cv = cv;
  42. if(caca_init_driver(dp))
  43. {
  44. free(dp);
  45. return NULL;
  46. }
  47. if(dp->drv.init_graphics(dp))
  48. {
  49. free(dp);
  50. return NULL;
  51. }
  52. /* Attached! */
  53. dp->cv->refcount++;
  54. /* Graphics stuff */
  55. dp->delay = 0;
  56. dp->rendertime = 0;
  57. /* Events stuff */
  58. #if defined(USE_SLANG) || defined(USE_NCURSES)
  59. dp->events.key_timer.last_sec = 0;
  60. dp->events.key_timer.last_usec = 0;
  61. dp->events.last_key_ticks = 0;
  62. dp->events.autorepeat_ticks = 0;
  63. dp->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. dp->events.queue = 0;
  67. #endif
  68. dp->timer.last_sec = 0;
  69. dp->timer.last_usec = 0;
  70. dp->lastticks = 0;
  71. /* Mouse position */
  72. dp->mouse.x = dp->cv->width / 2;
  73. dp->mouse.y = dp->cv->height / 2;
  74. /* Resize events */
  75. dp->resize.resized = 0;
  76. return dp;
  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 dp The libcaca graphical context.
  85. */
  86. void caca_free_display(caca_display_t *dp)
  87. {
  88. dp->drv.end_graphics(dp);
  89. dp->cv->refcount--;
  90. free(dp);
  91. }
  92. /*
  93. * XXX: The following functions are local.
  94. */
  95. static int caca_init_driver(caca_display_t *dp)
  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(dp);
  104. #endif
  105. #if defined(USE_CONIO)
  106. if(!strcasecmp(var, "conio")) return conio_install(dp);
  107. #endif
  108. #if defined(USE_X11)
  109. if(!strcasecmp(var, "x11")) return x11_install(dp);
  110. #endif
  111. #if defined(USE_GL)
  112. if(!strcasecmp(var, "gl")) return gl_install(dp);
  113. #endif
  114. if(!strcasecmp(var, "raw")) return raw_install(dp);
  115. #if defined(USE_SLANG)
  116. if(!strcasecmp(var, "slang")) return slang_install(dp);
  117. #endif
  118. #if defined(USE_NCURSES)
  119. if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
  120. #endif
  121. #if defined(USE_VGA)
  122. if(!strcasecmp(var, "vga")) return vga_install(dp);
  123. #endif
  124. return -1;
  125. }
  126. #endif
  127. #if defined(USE_WIN32)
  128. if(win32_install(dp) == 0) return 0;
  129. #endif
  130. #if defined(USE_CONIO)
  131. if(conio_install(dp) == 0) return 0;
  132. #endif
  133. #if defined(USE_VGA)
  134. if(vga_install(dp) == 0) return 0;
  135. #endif
  136. #if defined(USE_X11)
  137. if(x11_install(dp) == 0) return 0;
  138. #endif
  139. #if defined(USE_GL)
  140. if(gl_install(dp) == 0) return 0;
  141. #endif
  142. #if defined(USE_SLANG)
  143. if(slang_install(dp) == 0) return 0;
  144. #endif
  145. #if defined(USE_NCURSES)
  146. if(ncurses_install(dp) == 0) return 0;
  147. #endif
  148. return -1;
  149. }