您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

376 行
11 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. 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. * If as a result of the resize the cursor coordinates fall outside the
  96. * new canvas boundaries, they are readjusted. For instance, if the
  97. * current X cursor coordinate is 11 and the requested width is 10, the
  98. * new X cursor coordinate will be 10.
  99. *
  100. * It is an error to try to resize the canvas if an output driver has
  101. * been attached to the canvas using caca_create_display(). You need to
  102. * remove the output driver using caca_free_display() before you can change
  103. * the canvas size again. However, the caca output driver can cause a
  104. * canvas resize through user interaction. See the caca_event() documentation
  105. * for more about this.
  106. *
  107. * If an error occurs, -1 is returned and \b errno is set accordingly:
  108. * - \c EBUSY The canvas is in use by a display driver and cannot be resized.
  109. * - \c ENOMEM Not enough memory for the requested canvas size. If this
  110. * happens, the canvas handle becomes invalid and should not be used.
  111. *
  112. * \param cv A libcucul canvas
  113. * \param width The desired canvas width
  114. * \param height The desired canvas height
  115. * \return 0 in case of success, -1 if an error occurred.
  116. */
  117. int cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  118. unsigned int height)
  119. {
  120. if(cv->refcount)
  121. {
  122. seterrno(EBUSY);
  123. return -1;
  124. }
  125. return _cucul_set_canvas_size(cv, width, height);
  126. }
  127. /** \brief Get the canvas width.
  128. *
  129. * Return the current canvas' width, in character cells.
  130. *
  131. * This function never fails.
  132. *
  133. * \param cv A libcucul canvas
  134. * \return The canvas width.
  135. */
  136. unsigned int cucul_get_canvas_width(cucul_canvas_t *cv)
  137. {
  138. return cv->width;
  139. }
  140. /** \brief Get the canvas height.
  141. *
  142. * Returns the current canvas' height, in character cells.
  143. *
  144. * This function never fails.
  145. *
  146. * \param cv A libcucul canvas
  147. * \return The canvas height.
  148. */
  149. unsigned int cucul_get_canvas_height(cucul_canvas_t *cv)
  150. {
  151. return cv->height;
  152. }
  153. /** \brief Uninitialise \e libcucul.
  154. *
  155. * Free all resources allocated by cucul_create_canvas(). After
  156. * this function has been called, no other \e libcucul functions may be
  157. * used unless a new call to cucul_create_canvas() is done.
  158. *
  159. * If an error occurs, -1 is returned and \b errno is set accordingly:
  160. * - \c EBUSY The canvas is in use by a display driver and cannot be freed.
  161. *
  162. * \param cv A libcucul canvas
  163. * \return 0 in case of success, -1 if an error occurred.
  164. */
  165. int cucul_free_canvas(cucul_canvas_t *cv)
  166. {
  167. unsigned int f;
  168. if(cv->refcount)
  169. {
  170. seterrno(EBUSY);
  171. return -1;
  172. }
  173. for(f = 0; f < cv->framecount; f++)
  174. {
  175. free(cv->frames[f].chars);
  176. free(cv->frames[f].attrs);
  177. free(cv->frames[f].name);
  178. }
  179. free(cv->frames);
  180. free(cv);
  181. return 0;
  182. }
  183. /** \brief Generate a random integer within a range.
  184. *
  185. * Generate a random integer within the given range.
  186. *
  187. * This function never fails.
  188. *
  189. * \param min The lower bound of the integer range.
  190. * \param max The upper bound of the integer range.
  191. * \return A random integer comprised between \p min and \p max - 1
  192. * (inclusive).
  193. */
  194. int cucul_rand(int min, int max)
  195. {
  196. static int need_init = 1;
  197. if(need_init)
  198. {
  199. srand(getpid() + time(NULL));
  200. need_init = 0;
  201. }
  202. return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
  203. }
  204. /*
  205. * XXX: The following functions are local.
  206. */
  207. int _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  208. unsigned int height)
  209. {
  210. unsigned int x, y, f, old_width, old_height, new_size, old_size;
  211. old_width = cv->width;
  212. old_height = cv->height;
  213. old_size = old_width * old_height;
  214. _cucul_save_frame_info(cv);
  215. cv->width = width;
  216. cv->height = height;
  217. new_size = width * height;
  218. /* Step 1: if new area is bigger, resize the memory area now. */
  219. if(new_size > old_size)
  220. {
  221. for(f = 0; f < cv->framecount; f++)
  222. {
  223. cv->frames[f].chars = realloc(cv->frames[f].chars,
  224. new_size * sizeof(uint32_t));
  225. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  226. new_size * sizeof(uint32_t));
  227. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  228. {
  229. seterrno(ENOMEM);
  230. return -1;
  231. }
  232. }
  233. }
  234. /* Step 2: move line data if necessary. */
  235. if(width == old_width)
  236. {
  237. /* Width did not change, which means we do not need to move data. */
  238. ;
  239. }
  240. else if(width > old_width)
  241. {
  242. /* New width is bigger than old width, which means we need to
  243. * copy lines starting from the bottom of the screen otherwise
  244. * we will overwrite information. */
  245. for(f = 0; f < cv->framecount; f++)
  246. {
  247. uint32_t *chars = cv->frames[f].chars;
  248. uint32_t *attrs = cv->frames[f].attrs;
  249. for(y = height < old_height ? height : old_height; y--; )
  250. {
  251. uint32_t attr = cv->frames[f].curattr;
  252. for(x = old_width; x--; )
  253. {
  254. chars[y * width + x] = chars[y * old_width + x];
  255. attrs[y * width + x] = attrs[y * old_width + x];
  256. }
  257. /* Zero the end of the line */
  258. for(x = width - old_width; x--; )
  259. {
  260. chars[y * width + old_width + x] = (uint32_t)' ';
  261. attrs[y * width + old_width + x] = attr;
  262. }
  263. }
  264. }
  265. }
  266. else
  267. {
  268. /* New width is smaller. Copy as many lines as possible. Ignore
  269. * the first line, it is already in place. */
  270. unsigned int lines = height < old_height ? height : old_height;
  271. for(f = 0; f < cv->framecount; f++)
  272. {
  273. uint32_t *chars = cv->frames[f].chars;
  274. uint32_t *attrs = cv->frames[f].attrs;
  275. for(y = 1; y < lines; y++)
  276. {
  277. for(x = 0; x < width; x++)
  278. {
  279. chars[y * width + x] = chars[y * old_width + x];
  280. attrs[y * width + x] = attrs[y * old_width + x];
  281. }
  282. }
  283. }
  284. }
  285. /* Step 3: fill the bottom of the new screen if necessary. */
  286. if(height > old_height)
  287. {
  288. for(f = 0; f < cv->framecount; f++)
  289. {
  290. uint32_t *chars = cv->frames[f].chars;
  291. uint32_t *attrs = cv->frames[f].attrs;
  292. uint32_t attr = cv->frames[f].curattr;
  293. /* Zero the bottom of the screen */
  294. for(x = (height - old_height) * width; x--; )
  295. {
  296. chars[old_height * width + x] = (uint32_t)' ';
  297. attrs[old_height * width + x] = attr;
  298. }
  299. }
  300. }
  301. /* Step 4: if new area is smaller, resize memory area now. */
  302. if(new_size < old_size)
  303. {
  304. for(f = 0; f < cv->framecount; f++)
  305. {
  306. cv->frames[f].chars = realloc(cv->frames[f].chars,
  307. new_size * sizeof(uint32_t));
  308. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  309. new_size * sizeof(uint32_t));
  310. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  311. {
  312. seterrno(ENOMEM);
  313. return -1;
  314. }
  315. }
  316. }
  317. /* Set new size */
  318. for(f = 0; f < cv->framecount; f++)
  319. {
  320. if(cv->frames[f].x > (int)width)
  321. cv->frames[f].x = width;
  322. if(cv->frames[f].y > (int)height)
  323. cv->frames[f].y = height;
  324. cv->frames[f].width = width;
  325. cv->frames[f].height = height;
  326. }
  327. /* Reset the current frame shortcuts */
  328. _cucul_load_frame_info(cv);
  329. return 0;
  330. }