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.
 
 
 
 
 
 

476 líneas
13 KiB

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