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.

преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. # if defined(HAVE_ERRNO_H)
  24. # include <errno.h>
  25. # endif
  26. #endif
  27. #include "cucul.h"
  28. #include "cucul_internals.h"
  29. /** \brief Initialise a \e libcucul canvas.
  30. *
  31. * This function initialises internal \e libcucul structures and the backend
  32. * that will be used for subsequent graphical operations. It must be the
  33. * first \e libcucul function to be called in a function. cucul_free_canvas()
  34. * should be called at the end of the program to free all allocated resources.
  35. *
  36. * If one of the desired canvas coordinates is zero, a default canvas size
  37. * of 80x32 is used instead.
  38. *
  39. * If an error occurs, NULL is returned and \b errno is set accordingly:
  40. * - \c ENOMEM Not enough memory for the requested canvas size.
  41. *
  42. * \param width The desired canvas width
  43. * \param height The desired canvas height
  44. * \return A libcucul canvas handle upon success, NULL if an error occurred.
  45. */
  46. cucul_canvas_t * cucul_create_canvas(unsigned int width, unsigned int height)
  47. {
  48. cucul_canvas_t *cv = malloc(sizeof(cucul_canvas_t));
  49. int ret;
  50. if(!cv)
  51. goto nomem;
  52. cv->refcount = 0;
  53. cv->fgcolor = CUCUL_COLOR_LIGHTGRAY;
  54. cv->bgcolor = CUCUL_COLOR_BLACK;
  55. cv->width = cv->height = 0;
  56. cv->chars = NULL;
  57. cv->attr = NULL;
  58. cv->frame = 0;
  59. cv->framecount = 1;
  60. cv->allchars = malloc(sizeof(uint32_t *));
  61. if(!cv->allchars)
  62. {
  63. free(cv);
  64. goto nomem;
  65. }
  66. cv->allattr = malloc(sizeof(uint32_t *));
  67. if(!cv->allattr)
  68. {
  69. free(cv->allchars);
  70. free(cv);
  71. goto nomem;
  72. }
  73. cv->allchars[0] = NULL;
  74. cv->allattr[0] = NULL;
  75. /* Initialise to a default size. 80x32 is arbitrary but matches AAlib's
  76. * default X11 window. When a graphic driver attaches to us, it can set
  77. * a different size. */
  78. if(width && height)
  79. ret = _cucul_set_canvas_size(cv, width, height);
  80. else
  81. ret = _cucul_set_canvas_size(cv, 80, 32);
  82. if(ret < 0)
  83. {
  84. #if defined(HAVE_ERRNO_H)
  85. int saved_errno = errno;
  86. #endif
  87. free(cv->allattr);
  88. free(cv->allchars);
  89. free(cv);
  90. #if defined(HAVE_ERRNO_H)
  91. errno = saved_errno;
  92. #endif
  93. return NULL;
  94. }
  95. /* FIXME: this shouldn't happen here */
  96. _cucul_init_dither();
  97. return cv;
  98. nomem:
  99. #if defined(HAVE_ERRNO_H)
  100. errno = ENOMEM;
  101. #endif
  102. return NULL;
  103. }
  104. /** \brief Resize a canvas.
  105. *
  106. * This function sets the canvas width and height, in character cells.
  107. *
  108. * The contents of the canvas are preserved to the extent of the new
  109. * canvas size. Newly allocated character cells at the right and/or at
  110. * the bottom of the canvas are filled with spaces.
  111. *
  112. * It is an error to try to resize the canvas if an output driver has
  113. * been attached to the canvas using caca_create_display(). You need to
  114. * remove the output driver using caca_free_display() before you can change
  115. * the canvas size again. However, the caca output driver can cause a
  116. * canvas resize through user interaction. See the caca_event() documentation
  117. * for more about this.
  118. *
  119. * If an error occurs, -1 is returned and \b errno is set accordingly:
  120. * - \c EBUSY The canvas is in use by a display driver and cannot be resized.
  121. * - \c ENOMEM Not enough memory for the requested canvas size. If this
  122. * happens, the canvas handle becomes invalid and should not be used.
  123. *
  124. * \param cv A libcucul canvas
  125. * \param width The desired canvas width
  126. * \param height The desired canvas height
  127. * \return 0 in case of success, -1 if an error occurred.
  128. */
  129. int cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  130. unsigned int height)
  131. {
  132. if(cv->refcount)
  133. {
  134. #if defined(HAVE_ERRNO_H)
  135. errno = EBUSY;
  136. #endif
  137. return -1;
  138. }
  139. return _cucul_set_canvas_size(cv, width, height);
  140. }
  141. /** \brief Get the canvas width.
  142. *
  143. * This function returns the current canvas width, in character cells.
  144. *
  145. * This function never fails.
  146. *
  147. * \param cv A libcucul canvas
  148. * \return The canvas width.
  149. */
  150. unsigned int cucul_get_canvas_width(cucul_canvas_t *cv)
  151. {
  152. return cv->width;
  153. }
  154. /** \brief Get the canvas height.
  155. *
  156. * This function returns the current canvas height, in character cells.
  157. *
  158. * This function never fails.
  159. *
  160. * \param cv A libcucul canvas
  161. * \return The canvas height.
  162. */
  163. unsigned int cucul_get_canvas_height(cucul_canvas_t *cv)
  164. {
  165. return cv->height;
  166. }
  167. /** \brief Translate a colour index into the colour's name.
  168. *
  169. * This function translates a cucul_color enum into a human-readable
  170. * description string of the associated colour.
  171. *
  172. * This function never fails.
  173. *
  174. * \param color The colour value.
  175. * \return A static string containing the colour's name, or \c "unknown" if
  176. * the colour is unknown.
  177. */
  178. char const *cucul_get_color_name(unsigned int color)
  179. {
  180. static char const *color_names[] =
  181. {
  182. "black",
  183. "blue",
  184. "green",
  185. "cyan",
  186. "red",
  187. "magenta",
  188. "brown",
  189. "light gray",
  190. "dark gray",
  191. "light blue",
  192. "light green",
  193. "light cyan",
  194. "light red",
  195. "light magenta",
  196. "yellow",
  197. "white",
  198. };
  199. if(color < 0 || color > 15)
  200. return "unknown";
  201. return color_names[color];
  202. }
  203. /** \brief Uninitialise \e libcucul.
  204. *
  205. * This function frees all resources allocated by cucul_create_canvas(). After
  206. * cucul_free_canvas() has been called, no other \e libcucul functions may be
  207. * used unless a new call to cucul_create_canvas() is done.
  208. *
  209. * If an error occurs, -1 is returned and \b errno is set accordingly:
  210. * - \c EBUSY The canvas is in use by a display driver and cannot be freed.
  211. *
  212. * \param cv A libcucul canvas
  213. * \return 0 in case of success, -1 if an error occurred.
  214. */
  215. int cucul_free_canvas(cucul_canvas_t *cv)
  216. {
  217. unsigned int f;
  218. if(cv->refcount)
  219. {
  220. #if defined(HAVE_ERRNO_H)
  221. errno = EBUSY;
  222. #endif
  223. return -1;
  224. }
  225. /* FIXME: this shouldn't be here either (see above) */
  226. _cucul_end_dither();
  227. for(f = 0; f < cv->framecount; f++)
  228. {
  229. free(cv->allchars[f]);
  230. free(cv->allattr[f]);
  231. }
  232. free(cv);
  233. return 0;
  234. }
  235. /** \brief Generate a random integer within a range.
  236. *
  237. * This function never fails.
  238. *
  239. * \param min The lower bound of the integer range.
  240. * \param max The upper bound of the integer range.
  241. * \return A random integer comprised between \p min and \p max - 1
  242. * (inclusive).
  243. */
  244. int cucul_rand(int min, int max)
  245. {
  246. return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
  247. }
  248. /*
  249. * XXX: The following functions are local.
  250. */
  251. int _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  252. unsigned int height)
  253. {
  254. unsigned int x, y, f, old_width, old_height, new_size, old_size;
  255. old_width = cv->width;
  256. old_height = cv->height;
  257. old_size = old_width * old_height;
  258. cv->width = width;
  259. cv->height = height;
  260. new_size = width * height;
  261. /* Step 1: if new area is bigger, resize the memory area now. */
  262. if(new_size > old_size)
  263. {
  264. for(f = 0; f < cv->framecount; f++)
  265. {
  266. cv->allchars[f] = realloc(cv->allchars[f],
  267. new_size * sizeof(uint32_t));
  268. cv->allattr[f] = realloc(cv->allattr[f],
  269. new_size * sizeof(uint32_t));
  270. if(!cv->allchars[f] || !cv->allattr[f])
  271. {
  272. #if defined(HAVE_ERRNO_H)
  273. errno = ENOMEM;
  274. #endif
  275. return -1;
  276. }
  277. }
  278. }
  279. /* Step 2: move line data if necessary. */
  280. if(width == old_width)
  281. {
  282. /* Width did not change, which means we do not need to move data. */
  283. ;
  284. }
  285. else if(width > old_width)
  286. {
  287. /* New width is bigger than old width, which means we need to
  288. * copy lines starting from the bottom of the screen otherwise
  289. * we will overwrite information. */
  290. for(f = 0; f < cv->framecount; f++)
  291. {
  292. uint32_t *chars = cv->allchars[f];
  293. uint32_t *attr = cv->allattr[f];
  294. for(y = height < old_height ? height : old_height; y--; )
  295. {
  296. for(x = old_width; x--; )
  297. {
  298. chars[y * width + x] = chars[y * old_width + x];
  299. attr[y * width + x] = attr[y * old_width + x];
  300. }
  301. /* Zero the end of the line */
  302. for(x = width - old_width; x--; )
  303. chars[y * width + old_width + x] = (uint32_t)' ';
  304. memset(attr + y * width + old_width, 0,
  305. (width - old_width) * 4);
  306. }
  307. }
  308. }
  309. else
  310. {
  311. /* New width is smaller. Copy as many lines as possible. Ignore
  312. * the first line, it is already in place. */
  313. unsigned int lines = height < old_height ? height : old_height;
  314. for(f = 0; f < cv->framecount; f++)
  315. {
  316. uint32_t *chars = cv->allchars[f];
  317. uint32_t *attr = cv->allattr[f];
  318. for(y = 1; y < lines; y++)
  319. {
  320. for(x = 0; x < width; x++)
  321. {
  322. chars[y * width + x] = chars[y * old_width + x];
  323. attr[y * width + x] = attr[y * old_width + x];
  324. }
  325. }
  326. }
  327. }
  328. /* Step 3: fill the bottom of the new screen if necessary. */
  329. if(height > old_height)
  330. {
  331. for(f = 0; f < cv->framecount; f++)
  332. {
  333. uint32_t *chars = cv->allchars[f];
  334. uint32_t *attr = cv->allattr[f];
  335. /* Zero the bottom of the screen */
  336. for(x = (height - old_height) * width; x--; )
  337. chars[old_height * width + x] = (uint32_t)' ';
  338. memset(attr + old_height * width, 0,
  339. (height - old_height) * width * 4);
  340. }
  341. }
  342. /* Step 4: if new area is smaller, resize memory area now. */
  343. if(new_size <= old_size)
  344. {
  345. for(f = 0; f < cv->framecount; f++)
  346. {
  347. cv->allchars[f] = realloc(cv->allchars[f],
  348. new_size * sizeof(uint32_t));
  349. cv->allattr[f] = realloc(cv->allattr[f],
  350. new_size * sizeof(uint32_t));
  351. if(!cv->allchars[f] || !cv->allattr[f])
  352. {
  353. #if defined(HAVE_ERRNO_H)
  354. errno = ENOMEM;
  355. #endif
  356. return -1;
  357. }
  358. }
  359. }
  360. /* Reset the current frame shortcut */
  361. cv->chars = cv->allchars[cv->frame];
  362. cv->attr = cv->allattr[cv->frame];
  363. return 0;
  364. }