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.

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