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.
 
 
 
 
 
 

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