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.
 
 
 
 
 
 

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