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.
 
 
 
 
 
 

375 lignes
10 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-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 libcucul applications
  15. * to initialise a drawing context.
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #if !defined(__KERNEL__)
  20. # include <stdio.h>
  21. # include <stdlib.h>
  22. # include <string.h>
  23. # include <time.h>
  24. # if defined(HAVE_ERRNO_H)
  25. # include <errno.h>
  26. # endif
  27. # include <sys/types.h>
  28. # if defined(HAVE_UNISTD_H)
  29. # include <unistd.h>
  30. # endif
  31. #endif
  32. #include "cucul.h"
  33. #include "cucul_internals.h"
  34. /** \brief Initialise a \e libcucul canvas.
  35. *
  36. * Initialise internal \e libcucul structures and the backend that will
  37. * be used for subsequent graphical operations. It must be the first
  38. * \e libcucul function to be called in a function. cucul_free_canvas()
  39. * should be called at the end of the program to free all allocated resources.
  40. *
  41. * If an error occurs, NULL is returned and \b errno is set accordingly:
  42. * - \c ENOMEM Not enough memory for the requested canvas size.
  43. *
  44. * \param width The desired canvas width
  45. * \param height The desired canvas height
  46. * \return A libcucul canvas handle upon success, NULL if an error occurred.
  47. */
  48. cucul_canvas_t * cucul_create_canvas(unsigned int width, unsigned int height)
  49. {
  50. cucul_canvas_t *cv = malloc(sizeof(cucul_canvas_t));
  51. if(!cv)
  52. goto nomem;
  53. cv->refcount = 0;
  54. cv->curattr = 0x00000000;
  55. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  56. cv->width = cv->height = 0;
  57. cv->chars = NULL;
  58. cv->attrs = NULL;
  59. cv->frame = 0;
  60. cv->framecount = 1;
  61. cv->allchars = malloc(sizeof(uint32_t *));
  62. if(!cv->allchars)
  63. {
  64. free(cv);
  65. goto nomem;
  66. }
  67. cv->allattrs = malloc(sizeof(uint32_t *));
  68. if(!cv->allattrs)
  69. {
  70. free(cv->allchars);
  71. free(cv);
  72. goto nomem;
  73. }
  74. cv->allchars[0] = NULL;
  75. cv->allattrs[0] = NULL;
  76. if(_cucul_set_canvas_size(cv, width, height) < 0)
  77. {
  78. #if defined(HAVE_ERRNO_H)
  79. int saved_errno = errno;
  80. #endif
  81. free(cv->allattrs);
  82. free(cv->allchars);
  83. free(cv);
  84. #if defined(HAVE_ERRNO_H)
  85. errno = saved_errno;
  86. #endif
  87. return NULL;
  88. }
  89. return cv;
  90. nomem:
  91. #if defined(HAVE_ERRNO_H)
  92. errno = ENOMEM;
  93. #endif
  94. return NULL;
  95. }
  96. /** \brief Resize a canvas.
  97. *
  98. * Set the canvas' width and height, in character cells.
  99. *
  100. * The contents of the canvas are preserved to the extent of the new
  101. * canvas size. Newly allocated character cells at the right and/or at
  102. * the bottom of the canvas are filled with spaces.
  103. *
  104. * It is an error to try to resize the canvas if an output driver has
  105. * been attached to the canvas using caca_create_display(). You need to
  106. * remove the output driver using caca_free_display() before you can change
  107. * the canvas size again. However, the caca output driver can cause a
  108. * canvas resize through user interaction. See the caca_event() documentation
  109. * for more about this.
  110. *
  111. * If an error occurs, -1 is returned and \b errno is set accordingly:
  112. * - \c EBUSY The canvas is in use by a display driver and cannot be resized.
  113. * - \c ENOMEM Not enough memory for the requested canvas size. If this
  114. * happens, the canvas handle becomes invalid and should not be used.
  115. *
  116. * \param cv A libcucul canvas
  117. * \param width The desired canvas width
  118. * \param height The desired canvas height
  119. * \return 0 in case of success, -1 if an error occurred.
  120. */
  121. int cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  122. unsigned int height)
  123. {
  124. if(cv->refcount)
  125. {
  126. #if defined(HAVE_ERRNO_H)
  127. errno = EBUSY;
  128. #endif
  129. return -1;
  130. }
  131. return _cucul_set_canvas_size(cv, width, height);
  132. }
  133. /** \brief Get the canvas width.
  134. *
  135. * Return the current canvas' width, in character cells.
  136. *
  137. * This function never fails.
  138. *
  139. * \param cv A libcucul canvas
  140. * \return The canvas width.
  141. */
  142. unsigned int cucul_get_canvas_width(cucul_canvas_t *cv)
  143. {
  144. return cv->width;
  145. }
  146. /** \brief Get the canvas height.
  147. *
  148. * Returns the current canvas' height, in character cells.
  149. *
  150. * This function never fails.
  151. *
  152. * \param cv A libcucul canvas
  153. * \return The canvas height.
  154. */
  155. unsigned int cucul_get_canvas_height(cucul_canvas_t *cv)
  156. {
  157. return cv->height;
  158. }
  159. /** \brief Uninitialise \e libcucul.
  160. *
  161. * Free all resources allocated by cucul_create_canvas(). After
  162. * this function has been called, no other \e libcucul functions may be
  163. * used unless a new call to cucul_create_canvas() is done.
  164. *
  165. * If an error occurs, -1 is returned and \b errno is set accordingly:
  166. * - \c EBUSY The canvas is in use by a display driver and cannot be freed.
  167. *
  168. * \param cv A libcucul canvas
  169. * \return 0 in case of success, -1 if an error occurred.
  170. */
  171. int cucul_free_canvas(cucul_canvas_t *cv)
  172. {
  173. unsigned int f;
  174. if(cv->refcount)
  175. {
  176. #if defined(HAVE_ERRNO_H)
  177. errno = EBUSY;
  178. #endif
  179. return -1;
  180. }
  181. for(f = 0; f < cv->framecount; f++)
  182. {
  183. free(cv->allchars[f]);
  184. free(cv->allattrs[f]);
  185. }
  186. free(cv->allchars);
  187. free(cv->allattrs);
  188. free(cv);
  189. return 0;
  190. }
  191. /** \brief Generate a random integer within a range.
  192. *
  193. * Generate a random integer within the given range.
  194. *
  195. * This function never fails.
  196. *
  197. * \param min The lower bound of the integer range.
  198. * \param max The upper bound of the integer range.
  199. * \return A random integer comprised between \p min and \p max - 1
  200. * (inclusive).
  201. */
  202. int cucul_rand(int min, int max)
  203. {
  204. static int need_init = 1;
  205. if(need_init)
  206. {
  207. srand(getpid() + time(NULL));
  208. need_init = 0;
  209. }
  210. return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
  211. }
  212. /*
  213. * XXX: The following functions are local.
  214. */
  215. int _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  216. unsigned int height)
  217. {
  218. unsigned int x, y, f, old_width, old_height, new_size, old_size;
  219. old_width = cv->width;
  220. old_height = cv->height;
  221. old_size = old_width * old_height;
  222. cv->width = width;
  223. cv->height = height;
  224. new_size = width * height;
  225. /* Step 1: if new area is bigger, resize the memory area now. */
  226. if(new_size > old_size)
  227. {
  228. for(f = 0; f < cv->framecount; f++)
  229. {
  230. cv->allchars[f] = realloc(cv->allchars[f],
  231. new_size * sizeof(uint32_t));
  232. cv->allattrs[f] = realloc(cv->allattrs[f],
  233. new_size * sizeof(uint32_t));
  234. if(new_size && (!cv->allchars[f] || !cv->allattrs[f]))
  235. {
  236. #if defined(HAVE_ERRNO_H)
  237. errno = ENOMEM;
  238. #endif
  239. return -1;
  240. }
  241. }
  242. }
  243. /* Step 2: move line data if necessary. */
  244. if(width == old_width)
  245. {
  246. /* Width did not change, which means we do not need to move data. */
  247. ;
  248. }
  249. else if(width > old_width)
  250. {
  251. /* New width is bigger than old width, which means we need to
  252. * copy lines starting from the bottom of the screen otherwise
  253. * we will overwrite information. */
  254. for(f = 0; f < cv->framecount; f++)
  255. {
  256. uint32_t *chars = cv->allchars[f];
  257. uint32_t *attrs = cv->allattrs[f];
  258. for(y = height < old_height ? height : old_height; y--; )
  259. {
  260. uint32_t attr = cv->curattr;
  261. for(x = old_width; x--; )
  262. {
  263. chars[y * width + x] = chars[y * old_width + x];
  264. attrs[y * width + x] = attrs[y * old_width + x];
  265. }
  266. /* Zero the end of the line */
  267. for(x = width - old_width; x--; )
  268. {
  269. chars[y * width + old_width + x] = (uint32_t)' ';
  270. attrs[y * width + old_width + x] = attr;
  271. }
  272. }
  273. }
  274. }
  275. else
  276. {
  277. /* New width is smaller. Copy as many lines as possible. Ignore
  278. * the first line, it is already in place. */
  279. unsigned int lines = height < old_height ? height : old_height;
  280. for(f = 0; f < cv->framecount; f++)
  281. {
  282. uint32_t *chars = cv->allchars[f];
  283. uint32_t *attrs = cv->allattrs[f];
  284. for(y = 1; y < lines; y++)
  285. {
  286. for(x = 0; x < width; x++)
  287. {
  288. chars[y * width + x] = chars[y * old_width + x];
  289. attrs[y * width + x] = attrs[y * old_width + x];
  290. }
  291. }
  292. }
  293. }
  294. /* Step 3: fill the bottom of the new screen if necessary. */
  295. if(height > old_height)
  296. {
  297. for(f = 0; f < cv->framecount; f++)
  298. {
  299. uint32_t *chars = cv->allchars[f];
  300. uint32_t *attrs = cv->allattrs[f];
  301. uint32_t attr = cv->curattr;
  302. /* Zero the bottom of the screen */
  303. for(x = (height - old_height) * width; x--; )
  304. {
  305. chars[old_height * width + x] = (uint32_t)' ';
  306. attrs[old_height * width + x] = attr;
  307. }
  308. }
  309. }
  310. /* Step 4: if new area is smaller, resize memory area now. */
  311. if(new_size <= old_size)
  312. {
  313. for(f = 0; f < cv->framecount; f++)
  314. {
  315. cv->allchars[f] = realloc(cv->allchars[f],
  316. new_size * sizeof(uint32_t));
  317. cv->allattrs[f] = realloc(cv->allattrs[f],
  318. new_size * sizeof(uint32_t));
  319. if(new_size && (!cv->allchars[f] || !cv->allattrs[f]))
  320. {
  321. #if defined(HAVE_ERRNO_H)
  322. errno = ENOMEM;
  323. #endif
  324. return -1;
  325. }
  326. }
  327. }
  328. /* Reset the current frame shortcut */
  329. cv->chars = cv->allchars[cv->frame];
  330. cv->attrs = cv->allattrs[cv->frame];
  331. return 0;
  332. }