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

272 行
6.7 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. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains the main functions used by \e libcaca applications to
  16. * initialise the library, get the screen properties, set the framerate and
  17. * so on.
  18. */
  19. #include "config.h"
  20. #include "common.h"
  21. #if !defined(__KERNEL__)
  22. # include <stdlib.h>
  23. # include <string.h>
  24. # include <stdio.h>
  25. # if defined(USE_PLUGINS)
  26. # if defined(HAVE_DLFCN_H)
  27. # include <dlfcn.h>
  28. # endif
  29. # endif
  30. #endif
  31. #include "cucul.h"
  32. #include "cucul_internals.h"
  33. #include "caca.h"
  34. #include "caca_internals.h"
  35. #if defined(USE_PLUGINS)
  36. # define gl_install(p) caca_plugin_install("gl", p)
  37. # define x11_install(p) caca_plugin_install("x11", p)
  38. #endif
  39. static int caca_can_resize(caca_display_t *);
  40. static int caca_select_driver(caca_display_t *);
  41. #if defined(USE_PLUGINS)
  42. static int caca_plugin_install(char const *, caca_display_t *);
  43. #endif
  44. /** \brief Attach a caca graphical context to a cucul canvas.
  45. *
  46. * Create a graphical context using device-dependent features (ncurses for
  47. * terminals, an X11 window, a DOS command window...) that attaches to a
  48. * libcucul canvas. Everything that gets drawn in the libcucul canvas can
  49. * then be displayed by the libcaca driver.
  50. *
  51. * If an error occurs, NULL is returned and \b errno is set accordingly:
  52. * - \c ENOMEM Not enough memory.
  53. * - \c ENODEV Graphical device could not be initialised.
  54. *
  55. * \param cv The cucul cavas.
  56. * \return The caca graphical context or NULL if an error occurred.
  57. */
  58. caca_display_t * caca_create_display(cucul_canvas_t *cv)
  59. {
  60. caca_display_t *dp = malloc(sizeof(caca_display_t));
  61. if(!dp)
  62. {
  63. seterrno(ENOMEM);
  64. return NULL;
  65. }
  66. if(cucul_manage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp))
  67. {
  68. free(dp);
  69. seterrno(EBUSY);
  70. return NULL;
  71. }
  72. dp->cv = cv;
  73. #if defined(USE_PLUGINS)
  74. dp->plugin = NULL;
  75. #endif
  76. if(caca_select_driver(dp))
  77. {
  78. #if defined(USE_PLUGINS)
  79. if(dp->plugin)
  80. dlclose(dp->plugin);
  81. #endif
  82. cucul_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp);
  83. free(dp);
  84. seterrno(ENODEV);
  85. return NULL;
  86. }
  87. if(dp->drv.init_graphics(dp))
  88. {
  89. #if defined(USE_PLUGINS)
  90. if(dp->plugin)
  91. dlclose(dp->plugin);
  92. #endif
  93. cucul_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp);
  94. free(dp);
  95. seterrno(ENODEV);
  96. return NULL;
  97. }
  98. /* Graphics stuff */
  99. dp->delay = 0;
  100. dp->rendertime = 0;
  101. /* Events stuff */
  102. #if defined(USE_SLANG) || defined(USE_NCURSES)
  103. dp->events.key_timer.last_sec = 0;
  104. dp->events.key_timer.last_usec = 0;
  105. dp->events.last_key_ticks = 0;
  106. dp->events.autorepeat_ticks = 0;
  107. dp->events.last_key_event.type = CACA_EVENT_NONE;
  108. #endif
  109. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
  110. dp->events.queue = 0;
  111. #endif
  112. dp->timer.last_sec = 0;
  113. dp->timer.last_usec = 0;
  114. dp->lastticks = 0;
  115. /* Mouse position */
  116. dp->mouse.x = dp->cv->width / 2;
  117. dp->mouse.y = dp->cv->height / 2;
  118. /* Resize events */
  119. dp->resize.resized = 0;
  120. dp->resize.allow = 0;
  121. return dp;
  122. }
  123. /** \brief Detach a caca graphical context from a cucul backend context.
  124. *
  125. * Detach a graphical context from its cucul backend and destroy it. The
  126. * libcucul canvas continues to exist and other graphical contexts can be
  127. * attached to it afterwards.
  128. *
  129. * This function never fails.
  130. *
  131. * \param dp The libcaca graphical context.
  132. * \return This function always returns 0.
  133. */
  134. int caca_free_display(caca_display_t *dp)
  135. {
  136. dp->drv.end_graphics(dp);
  137. #if defined(USE_PLUGINS)
  138. if(dp->plugin)
  139. dlclose(dp->plugin);
  140. #endif
  141. cucul_unmanage_canvas(dp->cv, (int (*)(void *))caca_can_resize, (void *)dp);
  142. free(dp);
  143. return 0;
  144. }
  145. /*
  146. * XXX: The following functions are local.
  147. */
  148. static int caca_can_resize(caca_display_t *dp)
  149. {
  150. return dp->resize.allow;
  151. }
  152. static int caca_select_driver(caca_display_t *dp)
  153. {
  154. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  155. char *var = getenv("CACA_DRIVER");
  156. /* If the environment variable was set, use it */
  157. if(var && *var)
  158. {
  159. #if defined(USE_COCOA)
  160. if(!strcasecmp(var, "cocoa")) return cocoa_install(dp);
  161. #endif
  162. #if defined(USE_WIN32)
  163. if(!strcasecmp(var, "win32")) return win32_install(dp);
  164. #endif
  165. #if defined(USE_CONIO)
  166. if(!strcasecmp(var, "conio")) return conio_install(dp);
  167. #endif
  168. #if defined(USE_X11)
  169. if(!strcasecmp(var, "x11")) return x11_install(dp);
  170. #endif
  171. #if defined(USE_GL)
  172. if(!strcasecmp(var, "gl")) return gl_install(dp);
  173. #endif
  174. #if !defined(__KERNEL__)
  175. if(!strcasecmp(var, "raw")) return raw_install(dp);
  176. #endif
  177. #if defined(USE_SLANG)
  178. if(!strcasecmp(var, "slang")) return slang_install(dp);
  179. #endif
  180. #if defined(USE_NCURSES)
  181. if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
  182. #endif
  183. #if defined(USE_VGA)
  184. if(!strcasecmp(var, "vga")) return vga_install(dp);
  185. #endif
  186. return -1;
  187. }
  188. #endif
  189. #if defined(USE_COCOA)
  190. if(cocoa_install(dp) == 0) return 0;
  191. #endif
  192. #if defined(USE_WIN32)
  193. if(win32_install(dp) == 0) return 0;
  194. #endif
  195. #if defined(USE_CONIO)
  196. if(conio_install(dp) == 0) return 0;
  197. #endif
  198. #if defined(USE_VGA)
  199. if(vga_install(dp) == 0) return 0;
  200. #endif
  201. #if defined(USE_X11)
  202. if(x11_install(dp) == 0) return 0;
  203. #endif
  204. #if defined(USE_GL)
  205. if(gl_install(dp) == 0) return 0;
  206. #endif
  207. /* ncurses has a higher priority than slang because it has better colour
  208. * support across terminal types, despite being slightly slower. */
  209. #if defined(USE_NCURSES)
  210. if(ncurses_install(dp) == 0) return 0;
  211. #endif
  212. #if defined(USE_SLANG)
  213. if(slang_install(dp) == 0) return 0;
  214. #endif
  215. return -1;
  216. }
  217. #if defined(USE_PLUGINS)
  218. static int caca_plugin_install(char const *name, caca_display_t *dp)
  219. {
  220. char buf[512];
  221. int (*sym) (caca_display_t *);
  222. sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, name);
  223. dp->plugin = dlopen(buf, RTLD_NOW);
  224. if(!dp->plugin)
  225. {
  226. sprintf(buf, "lib%s_plugin.so", name);
  227. dp->plugin = dlopen(buf, RTLD_NOW);
  228. if(!dp->plugin)
  229. return -1;
  230. }
  231. sprintf(buf, "%s_install", name);
  232. sym = dlsym(dp->plugin, buf);
  233. if(!sym)
  234. {
  235. dlclose(dp->plugin);
  236. return -1;
  237. }
  238. return sym(dp);
  239. }
  240. #endif