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.

пре 19 година
пре 19 година
пре 21 година
пре 18 година
пре 19 година
пре 19 година
пре 19 година
пре 19 година
пре 19 година
пре 19 година
пре 19 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. /** \brief Get available display drivers
  190. *
  191. * Return a list of available display drivers. The list is a NULL-terminated
  192. * array of strings, interleaving a string containing the internal value for
  193. * the display driver, and a string containing the natural language
  194. * description for that driver.
  195. *
  196. * This function never fails.
  197. *
  198. * \param dp Display object.
  199. * \return An array of strings.
  200. */
  201. char const * const * caca_get_display_driver_list(caca_display_t *dp)
  202. {
  203. static char const * const list[] =
  204. {
  205. "none", "No antialiasing",
  206. "prefilter", "Prefilter antialiasing",
  207. #if defined(USE_COCOA)
  208. "cocoa", "Mac OS X Cocoa",
  209. #endif
  210. #if defined(USE_WIN32)
  211. "win32", "Windows console",
  212. #endif
  213. #if defined(USE_CONIO)
  214. "conio", "MS-DOS conio",
  215. #endif
  216. #if defined(USE_X11)
  217. "x11", "X11 graphical window",
  218. #endif
  219. #if defined(USE_GL)
  220. "gl", "OpenGL window",
  221. #endif
  222. #if !defined(__KERNEL__)
  223. "raw", "raw libcaca output",
  224. #endif
  225. #if defined(USE_SLANG)
  226. "slang", "S-Lang console library",
  227. #endif
  228. #if defined(USE_NCURSES)
  229. "ncurses", "ncurses console library",
  230. #endif
  231. #if defined(USE_VGA)
  232. "vga", "direct VGA memory",
  233. #endif
  234. NULL, NULL
  235. };
  236. return list;
  237. }
  238. /*
  239. * XXX: The following functions are local.
  240. */
  241. static int caca_can_resize(caca_display_t *dp)
  242. {
  243. return dp->resize.allow;
  244. }
  245. static int caca_select_driver(caca_display_t *dp)
  246. {
  247. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  248. char *var = getenv("CACA_DRIVER");
  249. /* If the environment variable was set, use it */
  250. if(var && *var)
  251. {
  252. #if defined(USE_COCOA)
  253. if(!strcasecmp(var, "cocoa")) return cocoa_install(dp);
  254. #endif
  255. #if defined(USE_WIN32)
  256. if(!strcasecmp(var, "win32")) return win32_install(dp);
  257. #endif
  258. #if defined(USE_CONIO)
  259. if(!strcasecmp(var, "conio")) return conio_install(dp);
  260. #endif
  261. #if defined(USE_X11)
  262. if(!strcasecmp(var, "x11")) return x11_install(dp);
  263. #endif
  264. #if defined(USE_GL)
  265. if(!strcasecmp(var, "gl")) return gl_install(dp);
  266. #endif
  267. #if !defined(__KERNEL__)
  268. if(!strcasecmp(var, "raw")) return raw_install(dp);
  269. #endif
  270. #if defined(USE_SLANG)
  271. if(!strcasecmp(var, "slang")) return slang_install(dp);
  272. #endif
  273. #if defined(USE_NCURSES)
  274. if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
  275. #endif
  276. #if defined(USE_VGA)
  277. if(!strcasecmp(var, "vga")) return vga_install(dp);
  278. #endif
  279. return -1;
  280. }
  281. #endif
  282. #if defined(USE_COCOA)
  283. if(cocoa_install(dp) == 0) return 0;
  284. #endif
  285. #if defined(USE_WIN32)
  286. if(win32_install(dp) == 0) return 0;
  287. #endif
  288. #if defined(USE_CONIO)
  289. if(conio_install(dp) == 0) return 0;
  290. #endif
  291. #if defined(USE_VGA)
  292. if(vga_install(dp) == 0) return 0;
  293. #endif
  294. #if defined(USE_X11)
  295. if(x11_install(dp) == 0) return 0;
  296. #endif
  297. #if defined(USE_GL)
  298. if(gl_install(dp) == 0) return 0;
  299. #endif
  300. /* ncurses has a higher priority than slang because it has better colour
  301. * support across terminal types, despite being slightly slower. */
  302. #if defined(USE_NCURSES)
  303. if(ncurses_install(dp) == 0) return 0;
  304. #endif
  305. #if defined(USE_SLANG)
  306. if(slang_install(dp) == 0) return 0;
  307. #endif
  308. return -1;
  309. }
  310. #if defined(USE_PLUGINS)
  311. static int caca_plugin_install(char const *name, caca_display_t *dp)
  312. {
  313. char buf[512];
  314. int (*sym) (caca_display_t *);
  315. sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, name);
  316. dp->plugin = dlopen(buf, RTLD_NOW);
  317. if(!dp->plugin)
  318. {
  319. sprintf(buf, "lib%s_plugin.so", name);
  320. dp->plugin = dlopen(buf, RTLD_NOW);
  321. if(!dp->plugin)
  322. return -1;
  323. }
  324. sprintf(buf, "%s_install", name);
  325. sym = dlsym(dp->plugin, buf);
  326. if(!sym)
  327. {
  328. dlclose(dp->plugin);
  329. return -1;
  330. }
  331. return sym(dp);
  332. }
  333. #endif