No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

460 líneas
12 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. #if !defined(__KERNEL__)
  21. # include <stdlib.h>
  22. # include <string.h>
  23. # include <stdio.h>
  24. # if defined(USE_PLUGINS)
  25. # if defined(HAVE_DLFCN_H)
  26. # include <dlfcn.h>
  27. # endif
  28. # endif
  29. #endif
  30. #include "caca.h"
  31. #include "caca_internals.h"
  32. #if defined(USE_PLUGINS)
  33. # define gl_install(p) caca_plugin_install(p, "gl")
  34. # define x11_install(p) caca_plugin_install(p, "x11")
  35. #endif
  36. static int caca_can_resize(caca_display_t *);
  37. static int caca_install_driver(caca_display_t *, char const *);
  38. static int caca_uninstall_driver(caca_display_t *);
  39. static int caca_select_driver(caca_display_t *, char const *);
  40. #if defined(USE_PLUGINS)
  41. static int caca_plugin_install(caca_display_t *, char const *);
  42. #endif
  43. /** \brief Attach a caca graphical context to a caca 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. * libcaca canvas. Everything that gets drawn in the libcaca canvas can
  48. * then be displayed by the libcaca driver.
  49. *
  50. * If no caca 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. * See also caca_create_display_with_driver().
  55. *
  56. * If an error occurs, NULL is returned and \b errno is set accordingly:
  57. * - \c ENOMEM Not enough memory.
  58. * - \c ENODEV Graphical device could not be initialised.
  59. *
  60. * \param cv The caca canvas or NULL to create a canvas automatically.
  61. * \return The caca graphical context or NULL if an error occurred.
  62. */
  63. caca_display_t * caca_create_display(caca_canvas_t *cv)
  64. {
  65. return caca_create_display_with_driver(cv, NULL);
  66. }
  67. /** \brief Attach a specific caca graphical context to a caca canvas.
  68. *
  69. * Create a graphical context using device-dependent features (ncurses for
  70. * terminals, an X11 window, a DOS command window...) that attaches to a
  71. * libcaca canvas. Everything that gets drawn in the libcaca canvas can
  72. * then be displayed by the libcaca driver.
  73. *
  74. * If no caca canvas is provided, a new one is created. Its handle can be
  75. * retrieved using caca_get_canvas() and it is automatically destroyed when
  76. * caca_free_display() is called.
  77. *
  78. * If no driver name is provided, \e libcaca will try to autodetect the best
  79. * output driver it can.
  80. *
  81. * See also caca_create_display().
  82. *
  83. * If an error occurs, NULL is returned and \b errno is set accordingly:
  84. * - \c ENOMEM Not enough memory.
  85. * - \c ENODEV Graphical device could not be initialised.
  86. *
  87. * \param cv The caca canvas or NULL to create a canvas automatically.
  88. * \param driver A string describing the desired output driver or NULL to
  89. * choose the best driver automatically.
  90. * \return The caca graphical context or NULL if an error occurred.
  91. */
  92. caca_display_t * caca_create_display_with_driver(caca_canvas_t *cv,
  93. char const *driver)
  94. {
  95. caca_display_t *dp = malloc(sizeof(caca_display_t));
  96. if(!dp)
  97. {
  98. seterrno(ENOMEM);
  99. return NULL;
  100. }
  101. if((dp->autorelease = (cv == NULL)))
  102. {
  103. cv = caca_create_canvas(0, 0);
  104. }
  105. dp->cv = cv;
  106. if(caca_manage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp))
  107. {
  108. if(dp->autorelease)
  109. caca_free_canvas(dp->cv);
  110. free(dp);
  111. seterrno(EBUSY);
  112. return NULL;
  113. }
  114. if(caca_install_driver(dp, driver))
  115. {
  116. caca_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp);
  117. if(dp->autorelease)
  118. caca_free_canvas(dp->cv);
  119. free(dp);
  120. seterrno(ENODEV);
  121. return NULL;
  122. }
  123. return dp;
  124. }
  125. /** \brief Get available display drivers
  126. *
  127. * Return a list of available display drivers. The list is a NULL-terminated
  128. * array of strings, interleaving a string containing the internal value for
  129. * the display driver, and a string containing the natural language
  130. * description for that driver.
  131. *
  132. * This function never fails.
  133. *
  134. * \return An array of strings.
  135. */
  136. char const * const * caca_get_display_driver_list(void)
  137. {
  138. static char const * const list[] =
  139. {
  140. #if defined(USE_COCOA)
  141. "cocoa", "Mac OS X Cocoa",
  142. #endif
  143. #if defined(USE_WIN32)
  144. "win32", "Windows console",
  145. #endif
  146. #if defined(USE_CONIO)
  147. "conio", "MS-DOS conio",
  148. #endif
  149. #if defined(USE_X11)
  150. "x11", "X11 graphical window",
  151. #endif
  152. #if defined(USE_GL)
  153. "gl", "OpenGL window",
  154. #endif
  155. #if defined(USE_SLANG)
  156. "slang", "S-Lang console library",
  157. #endif
  158. #if defined(USE_NCURSES)
  159. "ncurses", "ncurses console library",
  160. #endif
  161. #if defined(USE_VGA)
  162. "vga", "direct VGA memory",
  163. #endif
  164. #if !defined(__KERNEL__)
  165. "raw", "raw libcaca output",
  166. #endif
  167. NULL, NULL
  168. };
  169. return list;
  170. }
  171. /** \brief Return a caca graphical context's current output driver.
  172. *
  173. * Return the given display's current output driver.
  174. *
  175. * This function never fails.
  176. *
  177. * \param dp The caca display.
  178. * \return A static string.
  179. */
  180. char const * caca_get_display_driver(caca_display_t *dp)
  181. {
  182. return dp->drv.driver;
  183. }
  184. /** \brief Set the output driver.
  185. *
  186. * Dynamically change the given display's output driver.
  187. *
  188. * FIXME: decide what to do in case of failure
  189. *
  190. * \param dp The caca display.
  191. * \param driver A string describing the desired output driver or NULL to
  192. * choose the best driver automatically.
  193. * \return 0 in case of success, -1 if an error occurred.
  194. */
  195. int caca_set_display_driver(caca_display_t *dp, char const *driver)
  196. {
  197. caca_uninstall_driver(dp);
  198. if(caca_install_driver(dp, driver))
  199. {
  200. seterrno(ENODEV);
  201. return -1;
  202. }
  203. return 0;
  204. }
  205. /** \brief Detach a caca graphical context from a caca backend context.
  206. *
  207. * Detach a graphical context from its caca backend and destroy it. The
  208. * libcaca canvas continues to exist and other graphical contexts can be
  209. * attached to it afterwards.
  210. *
  211. * If the caca canvas was automatically created by caca_create_display(),
  212. * it is automatically destroyed and any handle to it becomes invalid.
  213. *
  214. * This function never fails.
  215. *
  216. * \param dp The libcaca graphical context.
  217. * \return This function always returns 0.
  218. */
  219. int caca_free_display(caca_display_t *dp)
  220. {
  221. caca_uninstall_driver(dp);
  222. caca_unmanage_canvas(dp->cv, (int (*)(void *))caca_can_resize, (void *)dp);
  223. if(dp->autorelease)
  224. caca_free_canvas(dp->cv);
  225. free(dp);
  226. return 0;
  227. }
  228. /** \brief Get the canvas attached to a caca graphical context.
  229. *
  230. * Return a handle on the \e caca_canvas_t object that was either attached
  231. * or created by caca_create_display().
  232. *
  233. * This function never fails.
  234. *
  235. * \param dp The libcaca graphical context.
  236. * \return The libcaca canvas.
  237. */
  238. caca_canvas_t * caca_get_canvas(caca_display_t *dp)
  239. {
  240. return dp->cv;
  241. }
  242. /** \brief Return the \e libcaca version.
  243. *
  244. * Return a read-only string with the \e libcaca version information.
  245. *
  246. * This function never fails.
  247. *
  248. * \return The \e libcaca version information.
  249. */
  250. char const * caca_get_version(void)
  251. {
  252. return PACKAGE_VERSION;
  253. }
  254. /*
  255. * XXX: The following functions are local.
  256. */
  257. static int caca_can_resize(caca_display_t *dp)
  258. {
  259. return dp->resize.allow;
  260. }
  261. static int caca_install_driver(caca_display_t *dp, char const *driver)
  262. {
  263. #if defined(USE_PLUGINS)
  264. dp->plugin = NULL;
  265. #endif
  266. if(caca_select_driver(dp, driver))
  267. {
  268. #if defined(USE_PLUGINS)
  269. if(dp->plugin)
  270. dlclose(dp->plugin);
  271. #endif
  272. return -1;
  273. }
  274. if(dp->drv.init_graphics(dp))
  275. {
  276. #if defined(USE_PLUGINS)
  277. if(dp->plugin)
  278. dlclose(dp->plugin);
  279. #endif
  280. return -1;
  281. }
  282. /* Graphics stuff */
  283. dp->delay = 0;
  284. dp->rendertime = 0;
  285. /* Events stuff */
  286. #if defined(USE_SLANG) || defined(USE_NCURSES)
  287. dp->events.key_timer.last_sec = 0;
  288. dp->events.key_timer.last_usec = 0;
  289. dp->events.last_key_ticks = 0;
  290. dp->events.autorepeat_ticks = 0;
  291. dp->events.last_key_event.type = CACA_EVENT_NONE;
  292. #endif
  293. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
  294. dp->events.queue = 0;
  295. #endif
  296. dp->timer.last_sec = 0;
  297. dp->timer.last_usec = 0;
  298. dp->lastticks = 0;
  299. /* Mouse position */
  300. dp->mouse.x = caca_get_canvas_width(dp->cv) / 2;
  301. dp->mouse.y = caca_get_canvas_height(dp->cv) / 2;
  302. /* Resize events */
  303. dp->resize.resized = 0;
  304. dp->resize.allow = 0;
  305. return 0;
  306. }
  307. static int caca_uninstall_driver(caca_display_t *dp)
  308. {
  309. dp->drv.end_graphics(dp);
  310. #if defined(USE_PLUGINS)
  311. if(dp->plugin)
  312. dlclose(dp->plugin);
  313. #endif
  314. return 0;
  315. }
  316. static int caca_select_driver(caca_display_t *dp, char const *driver)
  317. {
  318. char const *var = driver;
  319. #if defined(HAVE_GETENV)
  320. if(!var)
  321. var = getenv("CACA_DRIVER");
  322. #endif
  323. #if defined(HAVE_STRCASECMP)
  324. /* If the environment variable was set, use it */
  325. if(var && *var)
  326. {
  327. #if defined(USE_COCOA)
  328. if(!strcasecmp(var, "cocoa")) return cocoa_install(dp);
  329. #endif
  330. #if defined(USE_WIN32)
  331. if(!strcasecmp(var, "win32")) return win32_install(dp);
  332. #endif
  333. #if defined(USE_CONIO)
  334. if(!strcasecmp(var, "conio")) return conio_install(dp);
  335. #endif
  336. #if defined(USE_X11)
  337. if(!strcasecmp(var, "x11")) return x11_install(dp);
  338. #endif
  339. #if defined(USE_GL)
  340. if(!strcasecmp(var, "gl")) return gl_install(dp);
  341. #endif
  342. #if !defined(__KERNEL__)
  343. if(!strcasecmp(var, "raw")) return raw_install(dp);
  344. #endif
  345. #if defined(USE_SLANG)
  346. if(!strcasecmp(var, "slang")) return slang_install(dp);
  347. #endif
  348. #if defined(USE_NCURSES)
  349. if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
  350. #endif
  351. #if defined(USE_VGA)
  352. if(!strcasecmp(var, "vga")) return vga_install(dp);
  353. #endif
  354. return -1;
  355. }
  356. #endif
  357. #if defined(USE_COCOA)
  358. if(cocoa_install(dp) == 0) return 0;
  359. #endif
  360. #if defined(USE_WIN32)
  361. if(win32_install(dp) == 0) return 0;
  362. #endif
  363. #if defined(USE_CONIO)
  364. if(conio_install(dp) == 0) return 0;
  365. #endif
  366. #if defined(USE_VGA)
  367. if(vga_install(dp) == 0) return 0;
  368. #endif
  369. #if defined(USE_X11)
  370. if(x11_install(dp) == 0) return 0;
  371. #endif
  372. #if defined(USE_GL)
  373. if(gl_install(dp) == 0) return 0;
  374. #endif
  375. /* ncurses has a higher priority than slang because it has better colour
  376. * support across terminal types, despite being slightly slower. */
  377. #if defined(USE_NCURSES)
  378. if(ncurses_install(dp) == 0) return 0;
  379. #endif
  380. #if defined(USE_SLANG)
  381. if(slang_install(dp) == 0) return 0;
  382. #endif
  383. return -1;
  384. }
  385. #if defined(USE_PLUGINS)
  386. static int caca_plugin_install(caca_display_t *dp, char const *driver)
  387. {
  388. char buf[512];
  389. int (*sym) (caca_display_t *);
  390. sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, driver);
  391. dp->plugin = dlopen(buf, RTLD_NOW);
  392. if(!dp->plugin)
  393. {
  394. sprintf(buf, "lib%s_plugin.so", driver);
  395. dp->plugin = dlopen(buf, RTLD_NOW);
  396. if(!dp->plugin)
  397. return -1;
  398. }
  399. sprintf(buf, "%s_install", driver);
  400. sym = dlsym(dp->plugin, buf);
  401. if(!sym)
  402. {
  403. dlclose(dp->plugin);
  404. return -1;
  405. }
  406. return sym(dp);
  407. }
  408. #endif
  409. /*
  410. * XXX: The following functions are aliases.
  411. */
  412. char const * cucul_get_version(void) CACA_ALIAS(caca_get_version);