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.
 
 
 
 
 
 

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