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.
 
 
 
 
 
 

174 regels
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. #include "common.h"
  20. #if !defined(__KERNEL__)
  21. # include <stdlib.h>
  22. # include <string.h>
  23. #endif
  24. #include "cucul.h"
  25. #include "cucul_internals.h"
  26. #include "caca.h"
  27. #include "caca_internals.h"
  28. static int caca_init_driver(caca_display_t *dp);
  29. /** \brief Attach a caca graphical context to a cucul canvas.
  30. *
  31. * Create a graphical context using device-dependent features (ncurses for
  32. * terminals, an X11 window, a DOS command window...) that attaches to a
  33. * libcucul canvas. Everything that gets drawn in the libcucul canvas can
  34. * then be displayed by the libcaca driver.
  35. *
  36. * \param cv The cucul cavas.
  37. * \return The caca graphical context or NULL if an error occurred.
  38. */
  39. caca_display_t * caca_create_display(cucul_canvas_t * cv)
  40. {
  41. caca_display_t *dp = malloc(sizeof(caca_display_t));
  42. dp->cv = cv;
  43. if(caca_init_driver(dp))
  44. {
  45. free(dp);
  46. return NULL;
  47. }
  48. if(dp->drv.init_graphics(dp))
  49. {
  50. free(dp);
  51. return NULL;
  52. }
  53. /* Attached! */
  54. dp->cv->refcount++;
  55. /* Graphics stuff */
  56. dp->delay = 0;
  57. dp->rendertime = 0;
  58. /* Events stuff */
  59. #if defined(USE_SLANG) || defined(USE_NCURSES)
  60. dp->events.key_timer.last_sec = 0;
  61. dp->events.key_timer.last_usec = 0;
  62. dp->events.last_key_ticks = 0;
  63. dp->events.autorepeat_ticks = 0;
  64. dp->events.last_key_event.type = CACA_EVENT_NONE;
  65. #endif
  66. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
  67. dp->events.queue = 0;
  68. #endif
  69. dp->timer.last_sec = 0;
  70. dp->timer.last_usec = 0;
  71. dp->lastticks = 0;
  72. /* Mouse position */
  73. dp->mouse.x = dp->cv->width / 2;
  74. dp->mouse.y = dp->cv->height / 2;
  75. /* Resize events */
  76. dp->resize.resized = 0;
  77. return dp;
  78. }
  79. /** \brief Detach a caca graphical context from a cucul backend context.
  80. *
  81. * Detach a graphical context from its cucul backend and destroy it. The
  82. * libcucul canvas continues to exist and other graphical contexts can be
  83. * attached to it afterwards.
  84. *
  85. * \param dp The libcaca graphical context.
  86. */
  87. void caca_free_display(caca_display_t *dp)
  88. {
  89. dp->drv.end_graphics(dp);
  90. dp->cv->refcount--;
  91. free(dp);
  92. }
  93. /*
  94. * XXX: The following functions are local.
  95. */
  96. static int caca_init_driver(caca_display_t *dp)
  97. {
  98. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  99. char *var = getenv("CACA_DRIVER");
  100. /* If the environment variable was set, use it */
  101. if(var && *var)
  102. {
  103. #if defined(USE_WIN32)
  104. if(!strcasecmp(var, "win32")) return win32_install(dp);
  105. #endif
  106. #if defined(USE_CONIO)
  107. if(!strcasecmp(var, "conio")) return conio_install(dp);
  108. #endif
  109. #if defined(USE_X11)
  110. if(!strcasecmp(var, "x11")) return x11_install(dp);
  111. #endif
  112. #if defined(USE_GL)
  113. if(!strcasecmp(var, "gl")) return gl_install(dp);
  114. #endif
  115. if(!strcasecmp(var, "raw")) return raw_install(dp);
  116. #if defined(USE_SLANG)
  117. if(!strcasecmp(var, "slang")) return slang_install(dp);
  118. #endif
  119. #if defined(USE_NCURSES)
  120. if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
  121. #endif
  122. #if defined(USE_VGA)
  123. if(!strcasecmp(var, "vga")) return vga_install(dp);
  124. #endif
  125. return -1;
  126. }
  127. #endif
  128. #if defined(USE_WIN32)
  129. if(win32_install(dp) == 0) return 0;
  130. #endif
  131. #if defined(USE_CONIO)
  132. if(conio_install(dp) == 0) return 0;
  133. #endif
  134. #if defined(USE_VGA)
  135. if(vga_install(dp) == 0) return 0;
  136. #endif
  137. #if defined(USE_X11)
  138. if(x11_install(dp) == 0) return 0;
  139. #endif
  140. #if defined(USE_GL)
  141. if(gl_install(dp) == 0) return 0;
  142. #endif
  143. #if defined(USE_SLANG)
  144. if(slang_install(dp) == 0) return 0;
  145. #endif
  146. #if defined(USE_NCURSES)
  147. if(ncurses_install(dp) == 0) return 0;
  148. #endif
  149. return -1;
  150. }