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.
 
 
 
 
 
 

476 rivejä
12 KiB

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