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ů.
 
 
 
 
 
 

497 řádky
13 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright © 2006—2021 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 private.
  265. */
  266. extern void *_caca_alloc2d(size_t width, size_t height, size_t elem_size)
  267. {
  268. if (width == 0 || height == 0 || elem_size == 0 || SIZE_MAX / width / height < elem_size)
  269. return NULL;
  270. return malloc(width * height * elem_size);
  271. }
  272. /*
  273. * XXX: The following functions are local.
  274. */
  275. static int caca_can_resize(caca_display_t *dp)
  276. {
  277. return dp->resize.allow;
  278. }
  279. static int caca_install_driver(caca_display_t *dp, char const *driver)
  280. {
  281. #if defined(USE_PLUGINS)
  282. dp->plugin = NULL;
  283. #endif
  284. if(caca_select_driver(dp, driver))
  285. {
  286. #if defined(USE_PLUGINS)
  287. if(dp->plugin)
  288. dlclose(dp->plugin);
  289. #endif
  290. return -1;
  291. }
  292. if(dp->drv.init_graphics(dp))
  293. {
  294. #if defined(USE_PLUGINS)
  295. if(dp->plugin)
  296. dlclose(dp->plugin);
  297. #endif
  298. return -1;
  299. }
  300. /* Graphics stuff */
  301. dp->delay = 0;
  302. dp->rendertime = 0;
  303. /* Events stuff */
  304. #if defined(USE_SLANG) || defined(USE_NCURSES)
  305. dp->events.key_timer.last_sec = 0;
  306. dp->events.key_timer.last_usec = 0;
  307. dp->events.last_key_ticks = 0;
  308. dp->events.autorepeat_ticks = 0;
  309. dp->events.last_key_event.type = CACA_EVENT_NONE;
  310. #endif
  311. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL) || defined(USE_SDL)
  312. dp->events.queue = 0;
  313. #endif
  314. dp->timer.last_sec = 0;
  315. dp->timer.last_usec = 0;
  316. #if defined PROF
  317. _caca_init_stat(&dp->display_stat, "dp[%p] disp_sys time", dp);
  318. _caca_init_stat(&dp->wait_stat, "dp[%p] disp_wait time", dp);
  319. _caca_init_stat(&dp->ev_sys_stat, "dp[%p] ev_sys time", dp);
  320. _caca_init_stat(&dp->ev_wait_stat, "dp[%p] ev_wait time", dp);
  321. #endif
  322. dp->lastticks = 0;
  323. /* Mouse position */
  324. dp->mouse.x = caca_get_canvas_width(dp->cv) / 2;
  325. dp->mouse.y = caca_get_canvas_height(dp->cv) / 2;
  326. /* Resize events */
  327. dp->resize.resized = 0;
  328. dp->resize.allow = 0;
  329. return 0;
  330. }
  331. static int caca_uninstall_driver(caca_display_t *dp)
  332. {
  333. dp->drv.end_graphics(dp);
  334. #if defined(USE_PLUGINS)
  335. if(dp->plugin)
  336. dlclose(dp->plugin);
  337. #endif
  338. #if defined PROF
  339. _caca_fini_stat(&dp->display_stat);
  340. _caca_fini_stat(&dp->wait_stat);
  341. _caca_fini_stat(&dp->ev_wait_stat);
  342. _caca_fini_stat(&dp->ev_sys_stat);
  343. #endif
  344. return 0;
  345. }
  346. static int caca_select_driver(caca_display_t *dp, char const *driver)
  347. {
  348. char const *var = driver;
  349. #if defined(HAVE_GETENV)
  350. if(!var)
  351. var = getenv("CACA_DRIVER");
  352. #endif
  353. #if defined(HAVE_STRCASECMP)
  354. /* If the environment variable was set, use it */
  355. if(var && *var)
  356. {
  357. #if defined(USE_COCOA)
  358. if(!strcasecmp(var, "cocoa")) return cocoa_install(dp);
  359. #endif
  360. #if defined(USE_WIN32)
  361. if(!strcasecmp(var, "win32")) return win32_install(dp);
  362. #endif
  363. #if defined(USE_CONIO)
  364. if(!strcasecmp(var, "conio")) return conio_install(dp);
  365. #endif
  366. #if defined(USE_X11)
  367. if(!strcasecmp(var, "x11")) return x11_install(dp);
  368. #endif
  369. #if defined(USE_GL)
  370. if(!strcasecmp(var, "gl")) return gl_install(dp);
  371. #endif
  372. #if defined(USE_SDL)
  373. if(!strcasecmp(var, "sdl")) return sdl_install(dp);
  374. #endif
  375. #if !defined(__KERNEL__)
  376. if(!strcasecmp(var, "raw")) return raw_install(dp);
  377. #endif
  378. #if defined(USE_SLANG)
  379. if(!strcasecmp(var, "slang")) return slang_install(dp);
  380. #endif
  381. #if defined(USE_NCURSES)
  382. if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
  383. #endif
  384. #if defined(USE_VGA)
  385. if(!strcasecmp(var, "vga")) return vga_install(dp);
  386. #endif
  387. #if !defined(__KERNEL__)
  388. if(!strcasecmp(var, "null")) return null_install(dp);
  389. #endif
  390. return -1;
  391. }
  392. #endif
  393. #if defined(USE_COCOA)
  394. if(cocoa_install(dp) == 0) return 0;
  395. #endif
  396. #if defined(USE_WIN32)
  397. if(win32_install(dp) == 0) return 0;
  398. #endif
  399. #if defined(USE_CONIO)
  400. if(conio_install(dp) == 0) return 0;
  401. #endif
  402. #if defined(USE_VGA)
  403. if(vga_install(dp) == 0) return 0;
  404. #endif
  405. #if defined(USE_X11)
  406. if(x11_install(dp) == 0) return 0;
  407. #endif
  408. #if defined(USE_GL)
  409. if(gl_install(dp) == 0) return 0;
  410. #endif
  411. #if defined(USE_SDL)
  412. if(sdl_install(dp) == 0) return 0;
  413. #endif
  414. /* ncurses has a higher priority than slang because it has better colour
  415. * support across terminal types, despite being slightly slower. */
  416. #if defined(USE_NCURSES)
  417. if(ncurses_install(dp) == 0) return 0;
  418. #endif
  419. #if defined(USE_SLANG)
  420. if(slang_install(dp) == 0) return 0;
  421. #endif
  422. /* Of course we don't try "raw" or "null" if the user did not
  423. * specifically ask for them. */
  424. return -1;
  425. }
  426. #if defined(USE_PLUGINS)
  427. static int caca_plugin_install(caca_display_t *dp, char const *driver)
  428. {
  429. char buf[512];
  430. int (*sym) (caca_display_t *);
  431. sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, driver);
  432. dp->plugin = dlopen(buf, RTLD_NOW);
  433. if(!dp->plugin)
  434. {
  435. sprintf(buf, "lib%s_plugin.so", driver);
  436. dp->plugin = dlopen(buf, RTLD_NOW);
  437. if(!dp->plugin)
  438. return -1;
  439. }
  440. sprintf(buf, "%s_install", driver);
  441. sym = dlsym(dp->plugin, buf);
  442. if(!sym)
  443. {
  444. dlclose(dp->plugin);
  445. return -1;
  446. }
  447. return sym(dp);
  448. }
  449. #endif