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

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