Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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