Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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