Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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