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

364 строки
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->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].x = cv->frames[0].y = 0;
  71. cv->frames[0].handlex = cv->frames[0].handley = 0;
  72. cv->frames[0].curattr = cv->curattr;
  73. if(_cucul_set_canvas_size(cv, width, height) < 0)
  74. {
  75. int saved_errno = geterrno();
  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. }
  172. free(cv->frames);
  173. free(cv);
  174. return 0;
  175. }
  176. /** \brief Generate a random integer within a range.
  177. *
  178. * Generate a random integer within the given range.
  179. *
  180. * This function never fails.
  181. *
  182. * \param min The lower bound of the integer range.
  183. * \param max The upper bound of the integer range.
  184. * \return A random integer comprised between \p min and \p max - 1
  185. * (inclusive).
  186. */
  187. int cucul_rand(int min, int max)
  188. {
  189. static int need_init = 1;
  190. if(need_init)
  191. {
  192. srand(getpid() + time(NULL));
  193. need_init = 0;
  194. }
  195. return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
  196. }
  197. /*
  198. * XXX: The following functions are local.
  199. */
  200. int _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  201. unsigned int height)
  202. {
  203. unsigned int x, y, f, old_width, old_height, new_size, old_size;
  204. old_width = cv->width;
  205. old_height = cv->height;
  206. old_size = old_width * old_height;
  207. cv->width = width;
  208. cv->height = height;
  209. new_size = width * height;
  210. /* Step 1: if new area is bigger, resize the memory area now. */
  211. if(new_size > old_size)
  212. {
  213. for(f = 0; f < cv->framecount; f++)
  214. {
  215. cv->frames[f].chars = realloc(cv->frames[f].chars,
  216. new_size * sizeof(uint32_t));
  217. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  218. new_size * sizeof(uint32_t));
  219. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  220. {
  221. seterrno(ENOMEM);
  222. return -1;
  223. }
  224. }
  225. }
  226. /* Step 2: move line data if necessary. */
  227. if(width == old_width)
  228. {
  229. /* Width did not change, which means we do not need to move data. */
  230. ;
  231. }
  232. else if(width > old_width)
  233. {
  234. /* New width is bigger than old width, which means we need to
  235. * copy lines starting from the bottom of the screen otherwise
  236. * we will overwrite information. */
  237. for(f = 0; f < cv->framecount; f++)
  238. {
  239. uint32_t *chars = cv->frames[f].chars;
  240. uint32_t *attrs = cv->frames[f].attrs;
  241. for(y = height < old_height ? height : old_height; y--; )
  242. {
  243. uint32_t attr = cv->frames[f].curattr;
  244. for(x = old_width; x--; )
  245. {
  246. chars[y * width + x] = chars[y * old_width + x];
  247. attrs[y * width + x] = attrs[y * old_width + x];
  248. }
  249. /* Zero the end of the line */
  250. for(x = width - old_width; x--; )
  251. {
  252. chars[y * width + old_width + x] = (uint32_t)' ';
  253. attrs[y * width + old_width + x] = attr;
  254. }
  255. }
  256. }
  257. }
  258. else
  259. {
  260. /* New width is smaller. Copy as many lines as possible. Ignore
  261. * the first line, it is already in place. */
  262. unsigned int lines = height < old_height ? height : old_height;
  263. for(f = 0; f < cv->framecount; f++)
  264. {
  265. uint32_t *chars = cv->frames[f].chars;
  266. uint32_t *attrs = cv->frames[f].attrs;
  267. for(y = 1; y < lines; y++)
  268. {
  269. for(x = 0; x < width; x++)
  270. {
  271. chars[y * width + x] = chars[y * old_width + x];
  272. attrs[y * width + x] = attrs[y * old_width + x];
  273. }
  274. }
  275. }
  276. }
  277. /* Step 3: fill the bottom of the new screen if necessary. */
  278. if(height > old_height)
  279. {
  280. for(f = 0; f < cv->framecount; f++)
  281. {
  282. uint32_t *chars = cv->frames[f].chars;
  283. uint32_t *attrs = cv->frames[f].attrs;
  284. uint32_t attr = cv->frames[f].curattr;
  285. /* Zero the bottom of the screen */
  286. for(x = (height - old_height) * width; x--; )
  287. {
  288. chars[old_height * width + x] = (uint32_t)' ';
  289. attrs[old_height * width + x] = attr;
  290. }
  291. }
  292. }
  293. /* Step 4: if new area is smaller, resize memory area now. */
  294. if(new_size < old_size)
  295. {
  296. for(f = 0; f < cv->framecount; f++)
  297. {
  298. cv->frames[f].chars = realloc(cv->frames[f].chars,
  299. new_size * sizeof(uint32_t));
  300. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  301. new_size * sizeof(uint32_t));
  302. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  303. {
  304. seterrno(ENOMEM);
  305. return -1;
  306. }
  307. }
  308. }
  309. /* Set new size */
  310. for(f = 0; f < cv->framecount; f++)
  311. {
  312. cv->frames[f].width = width;
  313. cv->frames[f].height = height;
  314. }
  315. /* Reset the current frame shortcuts */
  316. cv->chars = cv->frames[cv->frame].chars;
  317. cv->attrs = cv->frames[cv->frame].attrs;
  318. return 0;
  319. }