Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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