Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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