Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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