Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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