Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

492 řádky
13 KiB

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