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

19 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 "caca.h"
  33. #include "caca_internals.h"
  34. #if defined(USE_PLUGINS)
  35. # define gl_install(p) caca_plugin_install("gl", p)
  36. # define x11_install(p) caca_plugin_install("x11", p)
  37. #endif
  38. static int caca_can_resize(caca_display_t *);
  39. static int caca_select_driver(caca_display_t *);
  40. #if defined(USE_PLUGINS)
  41. static int caca_plugin_install(char const *, caca_display_t *);
  42. #endif
  43. /** \brief Attach a caca graphical context to a cucul canvas.
  44. *
  45. * Create a graphical context using device-dependent features (ncurses for
  46. * terminals, an X11 window, a DOS command window...) that attaches to a
  47. * libcucul canvas. Everything that gets drawn in the libcucul canvas can
  48. * then be displayed by the libcaca driver.
  49. *
  50. * If no cucul canvas is provided, a new one is created. Its handle can be
  51. * retrieved using caca_get_canvas() and it is automatically destroyed when
  52. * caca_free_display() is called.
  53. *
  54. * If an error occurs, NULL is returned and \b errno is set accordingly:
  55. * - \c ENOMEM Not enough memory.
  56. * - \c ENODEV Graphical device could not be initialised.
  57. *
  58. * \param cv The cucul cavas.
  59. * \return The caca graphical context or NULL if an error occurred.
  60. */
  61. caca_display_t * caca_create_display(cucul_canvas_t *cv)
  62. {
  63. caca_display_t *dp = malloc(sizeof(caca_display_t));
  64. if(!dp)
  65. {
  66. seterrno(ENOMEM);
  67. return NULL;
  68. }
  69. if((dp->autorelease = (cv == NULL)))
  70. {
  71. cv = cucul_create_canvas(0, 0);
  72. }
  73. dp->cv = cv;
  74. if(cucul_manage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp))
  75. {
  76. if(dp->autorelease)
  77. cucul_free_canvas(dp->cv);
  78. free(dp);
  79. seterrno(EBUSY);
  80. return NULL;
  81. }
  82. #if defined(USE_PLUGINS)
  83. dp->plugin = NULL;
  84. #endif
  85. if(caca_select_driver(dp))
  86. {
  87. #if defined(USE_PLUGINS)
  88. if(dp->plugin)
  89. dlclose(dp->plugin);
  90. #endif
  91. cucul_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp);
  92. if(dp->autorelease)
  93. cucul_free_canvas(dp->cv);
  94. free(dp);
  95. seterrno(ENODEV);
  96. return NULL;
  97. }
  98. if(dp->drv.init_graphics(dp))
  99. {
  100. #if defined(USE_PLUGINS)
  101. if(dp->plugin)
  102. dlclose(dp->plugin);
  103. #endif
  104. cucul_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp);
  105. if(dp->autorelease)
  106. cucul_free_canvas(dp->cv);
  107. free(dp);
  108. seterrno(ENODEV);
  109. return NULL;
  110. }
  111. /* Graphics stuff */
  112. dp->delay = 0;
  113. dp->rendertime = 0;
  114. /* Events stuff */
  115. #if defined(USE_SLANG) || defined(USE_NCURSES)
  116. dp->events.key_timer.last_sec = 0;
  117. dp->events.key_timer.last_usec = 0;
  118. dp->events.last_key_ticks = 0;
  119. dp->events.autorepeat_ticks = 0;
  120. dp->events.last_key_event.type = CACA_EVENT_NONE;
  121. #endif
  122. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
  123. dp->events.queue = 0;
  124. #endif
  125. dp->timer.last_sec = 0;
  126. dp->timer.last_usec = 0;
  127. dp->lastticks = 0;
  128. /* Mouse position */
  129. dp->mouse.x = cucul_get_canvas_width(dp->cv) / 2;
  130. dp->mouse.y = cucul_get_canvas_height(dp->cv) / 2;
  131. /* Resize events */
  132. dp->resize.resized = 0;
  133. dp->resize.allow = 0;
  134. return dp;
  135. }
  136. /** \brief Detach a caca graphical context from a cucul backend context.
  137. *
  138. * Detach a graphical context from its cucul backend and destroy it. The
  139. * libcucul canvas continues to exist and other graphical contexts can be
  140. * attached to it afterwards.
  141. *
  142. * If the cucul canvas was automatically created by caca_create_display(),
  143. * it is automatically destroyed and any handle to it becomes invalid.
  144. *
  145. * This function never fails.
  146. *
  147. * \param dp The libcaca graphical context.
  148. * \return This function always returns 0.
  149. */
  150. int caca_free_display(caca_display_t *dp)
  151. {
  152. dp->drv.end_graphics(dp);
  153. #if defined(USE_PLUGINS)
  154. if(dp->plugin)
  155. dlclose(dp->plugin);
  156. #endif
  157. cucul_unmanage_canvas(dp->cv, (int (*)(void *))caca_can_resize, (void *)dp);
  158. if(dp->autorelease)
  159. cucul_free_canvas(dp->cv);
  160. free(dp);
  161. return 0;
  162. }
  163. /** \brief Get the canvas attached to a caca graphical context.
  164. *
  165. * Return a handle on the \e cucul_canvas_t object that was either attached
  166. * or created by caca_create_display().
  167. *
  168. * This function never fails.
  169. *
  170. * \param dp The libcaca graphical context.
  171. * \return The libcucul canvas.
  172. */
  173. cucul_canvas_t * caca_get_canvas(caca_display_t *dp)
  174. {
  175. return dp->cv;
  176. }
  177. /** \brief Return the \e libcaca version.
  178. *
  179. * Return a read-only string with the \e libcaca version information.
  180. *
  181. * This function never fails.
  182. *
  183. * \return The \e libcaca version information.
  184. */
  185. char const * caca_get_version(void)
  186. {
  187. return VERSION;
  188. }
  189. /*
  190. * XXX: The following functions are local.
  191. */
  192. static int caca_can_resize(caca_display_t *dp)
  193. {
  194. return dp->resize.allow;
  195. }
  196. static int caca_select_driver(caca_display_t *dp)
  197. {
  198. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  199. char *var = getenv("CACA_DRIVER");
  200. /* If the environment variable was set, use it */
  201. if(var && *var)
  202. {
  203. #if defined(USE_COCOA)
  204. if(!strcasecmp(var, "cocoa")) return cocoa_install(dp);
  205. #endif
  206. #if defined(USE_WIN32)
  207. if(!strcasecmp(var, "win32")) return win32_install(dp);
  208. #endif
  209. #if defined(USE_CONIO)
  210. if(!strcasecmp(var, "conio")) return conio_install(dp);
  211. #endif
  212. #if defined(USE_X11)
  213. if(!strcasecmp(var, "x11")) return x11_install(dp);
  214. #endif
  215. #if defined(USE_GL)
  216. if(!strcasecmp(var, "gl")) return gl_install(dp);
  217. #endif
  218. #if !defined(__KERNEL__)
  219. if(!strcasecmp(var, "raw")) return raw_install(dp);
  220. #endif
  221. #if defined(USE_SLANG)
  222. if(!strcasecmp(var, "slang")) return slang_install(dp);
  223. #endif
  224. #if defined(USE_NCURSES)
  225. if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
  226. #endif
  227. #if defined(USE_VGA)
  228. if(!strcasecmp(var, "vga")) return vga_install(dp);
  229. #endif
  230. return -1;
  231. }
  232. #endif
  233. #if defined(USE_COCOA)
  234. if(cocoa_install(dp) == 0) return 0;
  235. #endif
  236. #if defined(USE_WIN32)
  237. if(win32_install(dp) == 0) return 0;
  238. #endif
  239. #if defined(USE_CONIO)
  240. if(conio_install(dp) == 0) return 0;
  241. #endif
  242. #if defined(USE_VGA)
  243. if(vga_install(dp) == 0) return 0;
  244. #endif
  245. #if defined(USE_X11)
  246. if(x11_install(dp) == 0) return 0;
  247. #endif
  248. #if defined(USE_GL)
  249. if(gl_install(dp) == 0) return 0;
  250. #endif
  251. /* ncurses has a higher priority than slang because it has better colour
  252. * support across terminal types, despite being slightly slower. */
  253. #if defined(USE_NCURSES)
  254. if(ncurses_install(dp) == 0) return 0;
  255. #endif
  256. #if defined(USE_SLANG)
  257. if(slang_install(dp) == 0) return 0;
  258. #endif
  259. return -1;
  260. }
  261. #if defined(USE_PLUGINS)
  262. static int caca_plugin_install(char const *name, caca_display_t *dp)
  263. {
  264. char buf[512];
  265. int (*sym) (caca_display_t *);
  266. sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, name);
  267. dp->plugin = dlopen(buf, RTLD_NOW);
  268. if(!dp->plugin)
  269. {
  270. sprintf(buf, "lib%s_plugin.so", name);
  271. dp->plugin = dlopen(buf, RTLD_NOW);
  272. if(!dp->plugin)
  273. return -1;
  274. }
  275. sprintf(buf, "%s_install", name);
  276. sym = dlsym(dp->plugin, buf);
  277. if(!sym)
  278. {
  279. dlclose(dp->plugin);
  280. return -1;
  281. }
  282. return sym(dp);
  283. }
  284. #endif