Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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