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.
 
 
 
 
 
 

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