Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

271 lignes
6.7 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("gl", p)
  36. # define x11_install(p) caca_plugin_install("x11", p)
  37. #endif
  38. static int caca_can_resize(caca_display_t *);
  39. static int caca_select_driver(caca_display_t *);
  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 an error occurs, NULL is returned and \b errno is set accordingly:
  51. * - \c ENOMEM Not enough memory.
  52. * - \c ENODEV Graphical device could not be initialised.
  53. *
  54. * \param cv The cucul cavas.
  55. * \return The caca graphical context or NULL if an error occurred.
  56. */
  57. caca_display_t * caca_create_display(cucul_canvas_t *cv)
  58. {
  59. caca_display_t *dp = malloc(sizeof(caca_display_t));
  60. if(!dp)
  61. {
  62. seterrno(ENOMEM);
  63. return NULL;
  64. }
  65. if(cucul_manage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp))
  66. {
  67. free(dp);
  68. seterrno(EBUSY);
  69. return NULL;
  70. }
  71. dp->cv = cv;
  72. #if defined(USE_PLUGINS)
  73. dp->plugin = NULL;
  74. #endif
  75. if(caca_select_driver(dp))
  76. {
  77. #if defined(USE_PLUGINS)
  78. if(dp->plugin)
  79. dlclose(dp->plugin);
  80. #endif
  81. cucul_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp);
  82. free(dp);
  83. seterrno(ENODEV);
  84. return NULL;
  85. }
  86. if(dp->drv.init_graphics(dp))
  87. {
  88. #if defined(USE_PLUGINS)
  89. if(dp->plugin)
  90. dlclose(dp->plugin);
  91. #endif
  92. cucul_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp);
  93. free(dp);
  94. seterrno(ENODEV);
  95. return NULL;
  96. }
  97. /* Graphics stuff */
  98. dp->delay = 0;
  99. dp->rendertime = 0;
  100. /* Events stuff */
  101. #if defined(USE_SLANG) || defined(USE_NCURSES)
  102. dp->events.key_timer.last_sec = 0;
  103. dp->events.key_timer.last_usec = 0;
  104. dp->events.last_key_ticks = 0;
  105. dp->events.autorepeat_ticks = 0;
  106. dp->events.last_key_event.type = CACA_EVENT_NONE;
  107. #endif
  108. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
  109. dp->events.queue = 0;
  110. #endif
  111. dp->timer.last_sec = 0;
  112. dp->timer.last_usec = 0;
  113. dp->lastticks = 0;
  114. /* Mouse position */
  115. dp->mouse.x = cucul_get_canvas_width(dp->cv) / 2;
  116. dp->mouse.y = cucul_get_canvas_height(dp->cv) / 2;
  117. /* Resize events */
  118. dp->resize.resized = 0;
  119. dp->resize.allow = 0;
  120. return dp;
  121. }
  122. /** \brief Detach a caca graphical context from a cucul backend context.
  123. *
  124. * Detach a graphical context from its cucul backend and destroy it. The
  125. * libcucul canvas continues to exist and other graphical contexts can be
  126. * attached to it afterwards.
  127. *
  128. * This function never fails.
  129. *
  130. * \param dp The libcaca graphical context.
  131. * \return This function always returns 0.
  132. */
  133. int caca_free_display(caca_display_t *dp)
  134. {
  135. dp->drv.end_graphics(dp);
  136. #if defined(USE_PLUGINS)
  137. if(dp->plugin)
  138. dlclose(dp->plugin);
  139. #endif
  140. cucul_unmanage_canvas(dp->cv, (int (*)(void *))caca_can_resize, (void *)dp);
  141. free(dp);
  142. return 0;
  143. }
  144. /*
  145. * XXX: The following functions are local.
  146. */
  147. static int caca_can_resize(caca_display_t *dp)
  148. {
  149. return dp->resize.allow;
  150. }
  151. static int caca_select_driver(caca_display_t *dp)
  152. {
  153. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  154. char *var = getenv("CACA_DRIVER");
  155. /* If the environment variable was set, use it */
  156. if(var && *var)
  157. {
  158. #if defined(USE_COCOA)
  159. if(!strcasecmp(var, "cocoa")) return cocoa_install(dp);
  160. #endif
  161. #if defined(USE_WIN32)
  162. if(!strcasecmp(var, "win32")) return win32_install(dp);
  163. #endif
  164. #if defined(USE_CONIO)
  165. if(!strcasecmp(var, "conio")) return conio_install(dp);
  166. #endif
  167. #if defined(USE_X11)
  168. if(!strcasecmp(var, "x11")) return x11_install(dp);
  169. #endif
  170. #if defined(USE_GL)
  171. if(!strcasecmp(var, "gl")) return gl_install(dp);
  172. #endif
  173. #if !defined(__KERNEL__)
  174. if(!strcasecmp(var, "raw")) return raw_install(dp);
  175. #endif
  176. #if defined(USE_SLANG)
  177. if(!strcasecmp(var, "slang")) return slang_install(dp);
  178. #endif
  179. #if defined(USE_NCURSES)
  180. if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
  181. #endif
  182. #if defined(USE_VGA)
  183. if(!strcasecmp(var, "vga")) return vga_install(dp);
  184. #endif
  185. return -1;
  186. }
  187. #endif
  188. #if defined(USE_COCOA)
  189. if(cocoa_install(dp) == 0) return 0;
  190. #endif
  191. #if defined(USE_WIN32)
  192. if(win32_install(dp) == 0) return 0;
  193. #endif
  194. #if defined(USE_CONIO)
  195. if(conio_install(dp) == 0) return 0;
  196. #endif
  197. #if defined(USE_VGA)
  198. if(vga_install(dp) == 0) return 0;
  199. #endif
  200. #if defined(USE_X11)
  201. if(x11_install(dp) == 0) return 0;
  202. #endif
  203. #if defined(USE_GL)
  204. if(gl_install(dp) == 0) return 0;
  205. #endif
  206. /* ncurses has a higher priority than slang because it has better colour
  207. * support across terminal types, despite being slightly slower. */
  208. #if defined(USE_NCURSES)
  209. if(ncurses_install(dp) == 0) return 0;
  210. #endif
  211. #if defined(USE_SLANG)
  212. if(slang_install(dp) == 0) return 0;
  213. #endif
  214. return -1;
  215. }
  216. #if defined(USE_PLUGINS)
  217. static int caca_plugin_install(char const *name, caca_display_t *dp)
  218. {
  219. char buf[512];
  220. int (*sym) (caca_display_t *);
  221. sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, name);
  222. dp->plugin = dlopen(buf, RTLD_NOW);
  223. if(!dp->plugin)
  224. {
  225. sprintf(buf, "lib%s_plugin.so", name);
  226. dp->plugin = dlopen(buf, RTLD_NOW);
  227. if(!dp->plugin)
  228. return -1;
  229. }
  230. sprintf(buf, "%s_install", name);
  231. sym = dlsym(dp->plugin, buf);
  232. if(!sym)
  233. {
  234. dlclose(dp->plugin);
  235. return -1;
  236. }
  237. return sym(dp);
  238. }
  239. #endif