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

456 строки
12 KiB

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