您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

204 行
4.9 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. # if defined(HAVE_ERRNO_H)
  24. # include <errno.h>
  25. # endif
  26. #endif
  27. #include "cucul.h"
  28. #include "cucul_internals.h"
  29. #include "caca.h"
  30. #include "caca_internals.h"
  31. static int caca_select_driver(caca_display_t *dp);
  32. /** \brief Attach a caca graphical context to a cucul canvas.
  33. *
  34. * Create a graphical context using device-dependent features (ncurses for
  35. * terminals, an X11 window, a DOS command window...) that attaches to a
  36. * libcucul canvas. Everything that gets drawn in the libcucul canvas can
  37. * then be displayed by the libcaca driver.
  38. *
  39. * If an error occurs, NULL is returned and \b errno is set accordingly:
  40. * - \c ENOMEM Not enough memory.
  41. * - \c ENODEV Graphical device could not be initialised.
  42. *
  43. * \param cv The cucul cavas.
  44. * \return The caca graphical context or NULL if an error occurred.
  45. */
  46. caca_display_t * caca_create_display(cucul_canvas_t * cv)
  47. {
  48. caca_display_t *dp = malloc(sizeof(caca_display_t));
  49. if(!dp)
  50. {
  51. #if defined(HAVE_ERRNO_H)
  52. errno = ENOMEM;
  53. #endif
  54. return NULL;
  55. }
  56. dp->cv = cv;
  57. if(caca_select_driver(dp))
  58. {
  59. free(dp);
  60. #if defined(HAVE_ERRNO_H)
  61. errno = ENODEV;
  62. #endif
  63. return NULL;
  64. }
  65. if(dp->drv.init_graphics(dp))
  66. {
  67. free(dp);
  68. #if defined(HAVE_ERRNO_H)
  69. errno = ENODEV;
  70. #endif
  71. return NULL;
  72. }
  73. /* Attached! */
  74. dp->cv->refcount++;
  75. /* Graphics stuff */
  76. dp->delay = 0;
  77. dp->rendertime = 0;
  78. /* Events stuff */
  79. #if defined(USE_SLANG) || defined(USE_NCURSES)
  80. dp->events.key_timer.last_sec = 0;
  81. dp->events.key_timer.last_usec = 0;
  82. dp->events.last_key_ticks = 0;
  83. dp->events.autorepeat_ticks = 0;
  84. dp->events.last_key_event.type = CACA_EVENT_NONE;
  85. #endif
  86. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
  87. dp->events.queue = 0;
  88. #endif
  89. dp->timer.last_sec = 0;
  90. dp->timer.last_usec = 0;
  91. dp->lastticks = 0;
  92. /* Mouse position */
  93. dp->mouse.x = dp->cv->width / 2;
  94. dp->mouse.y = dp->cv->height / 2;
  95. /* Resize events */
  96. dp->resize.resized = 0;
  97. return dp;
  98. }
  99. /** \brief Detach a caca graphical context from a cucul backend context.
  100. *
  101. * Detach a graphical context from its cucul backend and destroy it. The
  102. * libcucul canvas continues to exist and other graphical contexts can be
  103. * attached to it afterwards.
  104. *
  105. * This function never fails.
  106. *
  107. * \param dp The libcaca graphical context.
  108. * \return This function always returns 0.
  109. */
  110. int caca_free_display(caca_display_t *dp)
  111. {
  112. dp->drv.end_graphics(dp);
  113. dp->cv->refcount--;
  114. free(dp);
  115. return 0;
  116. }
  117. /*
  118. * XXX: The following functions are local.
  119. */
  120. static int caca_select_driver(caca_display_t *dp)
  121. {
  122. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  123. char *var = getenv("CACA_DRIVER");
  124. /* If the environment variable was set, use it */
  125. if(var && *var)
  126. {
  127. #if defined(USE_WIN32)
  128. if(!strcasecmp(var, "win32")) return win32_install(dp);
  129. #endif
  130. #if defined(USE_CONIO)
  131. if(!strcasecmp(var, "conio")) return conio_install(dp);
  132. #endif
  133. #if defined(USE_X11)
  134. if(!strcasecmp(var, "x11")) return x11_install(dp);
  135. #endif
  136. #if defined(USE_GL)
  137. if(!strcasecmp(var, "gl")) return gl_install(dp);
  138. #endif
  139. #if !defined(__KERNEL__)
  140. if(!strcasecmp(var, "raw")) return raw_install(dp);
  141. #endif
  142. #if defined(USE_SLANG)
  143. if(!strcasecmp(var, "slang")) return slang_install(dp);
  144. #endif
  145. #if defined(USE_NCURSES)
  146. if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
  147. #endif
  148. #if defined(USE_VGA)
  149. if(!strcasecmp(var, "vga")) return vga_install(dp);
  150. #endif
  151. return -1;
  152. }
  153. #endif
  154. #if defined(USE_WIN32)
  155. if(win32_install(dp) == 0) return 0;
  156. #endif
  157. #if defined(USE_CONIO)
  158. if(conio_install(dp) == 0) return 0;
  159. #endif
  160. #if defined(USE_VGA)
  161. if(vga_install(dp) == 0) return 0;
  162. #endif
  163. #if defined(USE_X11)
  164. if(x11_install(dp) == 0) return 0;
  165. #endif
  166. #if defined(USE_GL)
  167. if(gl_install(dp) == 0) return 0;
  168. #endif
  169. /* ncurses has a higher priority than slang because it has better colour
  170. * support across terminal types, despite being slightly slower. */
  171. #if defined(USE_NCURSES)
  172. if(ncurses_install(dp) == 0) return 0;
  173. #endif
  174. #if defined(USE_SLANG)
  175. if(slang_install(dp) == 0) return 0;
  176. #endif
  177. return -1;
  178. }