You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 19 година
пре 19 година
пре 21 година
пре 19 година
пре 19 година
пре 19 година
пре 19 година
пре 19 година
пре 19 година
пре 19 година
пре 19 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains the main functions used by \e libcaca applications to
  15. * initialise the library, get the screen properties, set the framerate and
  16. * so on.
  17. */
  18. #include "config.h"
  19. #include "common.h"
  20. #if !defined(__KERNEL__)
  21. # include <stdlib.h>
  22. # include <string.h>
  23. # include <stdio.h>
  24. # if defined(HAVE_ERRNO_H)
  25. # include <errno.h>
  26. # endif
  27. # if defined(USE_PLUGINS)
  28. # if defined(HAVE_DLFCN_H)
  29. # include <dlfcn.h>
  30. # endif
  31. # endif
  32. #endif
  33. #include "cucul.h"
  34. #include "cucul_internals.h"
  35. #include "caca.h"
  36. #include "caca_internals.h"
  37. #if defined(USE_PLUGINS)
  38. # define gl_install(p) caca_plugin_install("gl", p)
  39. # define x11_install(p) caca_plugin_install("x11", p)
  40. #endif
  41. static int caca_select_driver(caca_display_t *);
  42. #if defined(USE_PLUGINS)
  43. static int caca_plugin_install(char const *, caca_display_t *);
  44. #endif
  45. /** \brief Attach a caca graphical context to a cucul canvas.
  46. *
  47. * Create a graphical context using device-dependent features (ncurses for
  48. * terminals, an X11 window, a DOS command window...) that attaches to a
  49. * libcucul canvas. Everything that gets drawn in the libcucul canvas can
  50. * then be displayed by the libcaca driver.
  51. *
  52. * If an error occurs, NULL is returned and \b errno is set accordingly:
  53. * - \c ENOMEM Not enough memory.
  54. * - \c ENODEV Graphical device could not be initialised.
  55. *
  56. * \param cv The cucul cavas.
  57. * \return The caca graphical context or NULL if an error occurred.
  58. */
  59. caca_display_t * caca_create_display(cucul_canvas_t * cv)
  60. {
  61. caca_display_t *dp = malloc(sizeof(caca_display_t));
  62. if(!dp)
  63. {
  64. #if defined(HAVE_ERRNO_H)
  65. errno = ENOMEM;
  66. #endif
  67. return NULL;
  68. }
  69. dp->cv = cv;
  70. #if defined(USE_PLUGINS)
  71. dp->plugin = NULL;
  72. #endif
  73. if(caca_select_driver(dp))
  74. {
  75. #if defined(USE_PLUGINS)
  76. if(dp->plugin)
  77. dlclose(dp->plugin);
  78. #endif
  79. free(dp);
  80. #if defined(HAVE_ERRNO_H)
  81. errno = ENODEV;
  82. #endif
  83. return NULL;
  84. }
  85. if(dp->drv.init_graphics(dp))
  86. {
  87. #if defined(USE_PLUGINS)
  88. if(dp->plugin)
  89. dlclose(dp->plugin);
  90. #endif
  91. free(dp);
  92. #if defined(HAVE_ERRNO_H)
  93. errno = ENODEV;
  94. #endif
  95. return NULL;
  96. }
  97. /* Attached! */
  98. dp->cv->refcount++;
  99. /* Graphics stuff */
  100. dp->delay = 0;
  101. dp->rendertime = 0;
  102. /* Events stuff */
  103. #if defined(USE_SLANG) || defined(USE_NCURSES)
  104. dp->events.key_timer.last_sec = 0;
  105. dp->events.key_timer.last_usec = 0;
  106. dp->events.last_key_ticks = 0;
  107. dp->events.autorepeat_ticks = 0;
  108. dp->events.last_key_event.type = CACA_EVENT_NONE;
  109. #endif
  110. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
  111. dp->events.queue = 0;
  112. #endif
  113. dp->timer.last_sec = 0;
  114. dp->timer.last_usec = 0;
  115. dp->lastticks = 0;
  116. /* Mouse position */
  117. dp->mouse.x = dp->cv->width / 2;
  118. dp->mouse.y = dp->cv->height / 2;
  119. /* Resize events */
  120. dp->resize.resized = 0;
  121. return dp;
  122. }
  123. /** \brief Detach a caca graphical context from a cucul backend context.
  124. *
  125. * Detach a graphical context from its cucul backend and destroy it. The
  126. * libcucul canvas continues to exist and other graphical contexts can be
  127. * attached to it afterwards.
  128. *
  129. * This function never fails.
  130. *
  131. * \param dp The libcaca graphical context.
  132. * \return This function always returns 0.
  133. */
  134. int caca_free_display(caca_display_t *dp)
  135. {
  136. dp->drv.end_graphics(dp);
  137. #if defined(USE_PLUGINS)
  138. if(dp->plugin)
  139. dlclose(dp->plugin);
  140. #endif
  141. dp->cv->refcount--;
  142. free(dp);
  143. return 0;
  144. }
  145. /*
  146. * XXX: The following functions are local.
  147. */
  148. static int caca_select_driver(caca_display_t *dp)
  149. {
  150. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  151. char *var = getenv("CACA_DRIVER");
  152. /* If the environment variable was set, use it */
  153. if(var && *var)
  154. {
  155. #if defined(USE_WIN32)
  156. if(!strcasecmp(var, "win32")) return win32_install(dp);
  157. #endif
  158. #if defined(USE_CONIO)
  159. if(!strcasecmp(var, "conio")) return conio_install(dp);
  160. #endif
  161. #if defined(USE_X11)
  162. if(!strcasecmp(var, "x11")) return x11_install(dp);
  163. #endif
  164. #if defined(USE_GL)
  165. if(!strcasecmp(var, "gl")) return gl_install(dp);
  166. #endif
  167. #if !defined(__KERNEL__)
  168. if(!strcasecmp(var, "raw")) return raw_install(dp);
  169. #endif
  170. #if defined(USE_SLANG)
  171. if(!strcasecmp(var, "slang")) return slang_install(dp);
  172. #endif
  173. #if defined(USE_NCURSES)
  174. if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
  175. #endif
  176. #if defined(USE_VGA)
  177. if(!strcasecmp(var, "vga")) return vga_install(dp);
  178. #endif
  179. return -1;
  180. }
  181. #endif
  182. #if defined(USE_WIN32)
  183. if(win32_install(dp) == 0) return 0;
  184. #endif
  185. #if defined(USE_CONIO)
  186. if(conio_install(dp) == 0) return 0;
  187. #endif
  188. #if defined(USE_VGA)
  189. if(vga_install(dp) == 0) return 0;
  190. #endif
  191. #if defined(USE_X11)
  192. if(x11_install(dp) == 0) return 0;
  193. #endif
  194. #if defined(USE_GL)
  195. if(gl_install(dp) == 0) return 0;
  196. #endif
  197. /* ncurses has a higher priority than slang because it has better colour
  198. * support across terminal types, despite being slightly slower. */
  199. #if defined(USE_NCURSES)
  200. if(ncurses_install(dp) == 0) return 0;
  201. #endif
  202. #if defined(USE_SLANG)
  203. if(slang_install(dp) == 0) return 0;
  204. #endif
  205. return -1;
  206. }
  207. #if defined(USE_PLUGINS)
  208. static int caca_plugin_install(char const *name, caca_display_t *dp)
  209. {
  210. char buf[512];
  211. int (*sym) (caca_display_t *);
  212. sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, name);
  213. dp->plugin = dlopen(buf, RTLD_NOW);
  214. if(!dp->plugin)
  215. {
  216. sprintf(buf, "lib%s_plugin.so", name);
  217. dp->plugin = dlopen(buf, RTLD_NOW);
  218. if(!dp->plugin)
  219. return -1;
  220. }
  221. sprintf(buf, "%s_install", name);
  222. sym = dlsym(dp->plugin, buf);
  223. if(!sym)
  224. {
  225. dlclose(dp->plugin);
  226. return -1;
  227. }
  228. return sym(dp);
  229. }
  230. #endif