Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

501 rinda
14 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. static int cucul_resize(cucul_canvas_t *, unsigned int, unsigned int);
  33. /** \brief Initialise a \e libcucul canvas.
  34. *
  35. * Initialise internal \e libcucul structures and the backend that will
  36. * be used for subsequent graphical operations. It must be the first
  37. * \e libcucul function to be called in a function. cucul_free_canvas()
  38. * should be called at the end of the program to free all allocated resources.
  39. *
  40. * Both the cursor and the canvas' handle are initialised at the top-left
  41. * corner.
  42. *
  43. * If an error occurs, NULL is returned and \b errno is set accordingly:
  44. * - \c ENOMEM Not enough memory for the requested canvas size.
  45. *
  46. * \param width The desired canvas width
  47. * \param height The desired canvas height
  48. * \return A libcucul canvas handle upon success, NULL if an error occurred.
  49. */
  50. cucul_canvas_t * cucul_create_canvas(unsigned int width, unsigned int height)
  51. {
  52. cucul_canvas_t *cv = malloc(sizeof(cucul_canvas_t));
  53. if(!cv)
  54. goto nomem;
  55. cv->refcount = 0;
  56. cv->autoinc = 0;
  57. cv->resize_callback = NULL;
  58. cv->resize_data = NULL;
  59. cv->frame = 0;
  60. cv->framecount = 1;
  61. cv->frames = malloc(sizeof(struct cucul_frame));
  62. if(!cv->frames)
  63. {
  64. free(cv);
  65. goto nomem;
  66. }
  67. cv->frames[0].width = cv->frames[0].height = 0;
  68. cv->frames[0].chars = NULL;
  69. cv->frames[0].attrs = NULL;
  70. cv->frames[0].x = cv->frames[0].y = 0;
  71. cv->frames[0].handlex = cv->frames[0].handley = 0;
  72. cv->frames[0].curattr = 0;
  73. cv->frames[0].name = strdup("frame#00000000");
  74. _cucul_load_frame_info(cv);
  75. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  76. cv->ff = NULL;
  77. if(cucul_resize(cv, width, height) < 0)
  78. {
  79. int saved_errno = geterrno();
  80. free(cv->frames[0].name);
  81. free(cv->frames);
  82. free(cv);
  83. seterrno(saved_errno);
  84. return NULL;
  85. }
  86. return cv;
  87. nomem:
  88. seterrno(ENOMEM);
  89. return NULL;
  90. }
  91. /** \brief Manage a canvas.
  92. *
  93. * Lock a canvas to prevent it from being resized. If non-NULL,
  94. * the \e callback function pointer will be called upon each
  95. * \e cucul_set_canvas_size call and if the returned value is zero, the
  96. * canvas resize request will be denied.
  97. *
  98. * This function is only useful for display drivers such as the \e libcaca
  99. * library.
  100. *
  101. * If an error occurs, -1 is returned and \b errno is set accordingly:
  102. * - \c EBUSY The canvas is already being managed.
  103. *
  104. * \param cv A libcucul canvas.
  105. * \param callback An optional callback function pointer.
  106. * \param p The argument to be passed to \e callback.
  107. * \return 0 in case of success, -1 if an error occurred.
  108. */
  109. int cucul_manage_canvas(cucul_canvas_t *cv, int (*callback)(void *), void *p)
  110. {
  111. if(cv->refcount)
  112. {
  113. seterrno(EBUSY);
  114. return -1;
  115. }
  116. cv->resize_callback = callback;
  117. cv->resize_data = p;
  118. cv->refcount = 1;
  119. return 0;
  120. }
  121. /** \brief Unmanage a canvas.
  122. *
  123. * Unlock a canvas previously locked by cucul_manage_canvas(). For safety
  124. * reasons, the callback and callback data arguments must be the same as for
  125. * the cucul_manage_canvas() call.
  126. *
  127. * This function is only useful for display drivers such as the \e libcaca
  128. * library.
  129. *
  130. * If an error occurs, -1 is returned and \b errno is set accordingly:
  131. * - \c EINVAL The canvas is not managed, or the callback arguments do
  132. * not match.
  133. *
  134. * \param cv A libcucul canvas.
  135. * \param callback The \e callback argument previously passed to
  136. cucul_manage_canvas().
  137. * \param p The \e p argument previously passed to cucul_manage_canvas().
  138. * \return 0 in case of success, -1 if an error occurred.
  139. */
  140. int cucul_unmanage_canvas(cucul_canvas_t *cv, int (*callback)(void *), void *p)
  141. {
  142. if(!cv->refcount
  143. || cv->resize_callback != callback || cv->resize_data != p)
  144. {
  145. seterrno(EINVAL);
  146. return -1;
  147. }
  148. cv->refcount = 0;
  149. return 0;
  150. }
  151. /** \brief Resize a canvas.
  152. *
  153. * Set the canvas' width and height, in character cells.
  154. *
  155. * The contents of the canvas are preserved to the extent of the new
  156. * canvas size. Newly allocated character cells at the right and/or at
  157. * the bottom of the canvas are filled with spaces.
  158. *
  159. * If as a result of the resize the cursor coordinates fall outside the
  160. * new canvas boundaries, they are readjusted. For instance, if the
  161. * current X cursor coordinate is 11 and the requested width is 10, the
  162. * new X cursor coordinate will be 10.
  163. *
  164. * It is an error to try to resize the canvas if an output driver has
  165. * been attached to the canvas using caca_create_display(). You need to
  166. * remove the output driver using caca_free_display() before you can change
  167. * the canvas size again. However, the caca output driver can cause a
  168. * canvas resize through user interaction. See the caca_event() documentation
  169. * for more about this.
  170. *
  171. * If an error occurs, -1 is returned and \b errno is set accordingly:
  172. * - \c EBUSY The canvas is in use by a display driver and cannot be resized.
  173. * - \c ENOMEM Not enough memory for the requested canvas size. If this
  174. * happens, the canvas handle becomes invalid and should not be used.
  175. *
  176. * \param cv A libcucul canvas.
  177. * \param width The desired canvas width.
  178. * \param height The desired canvas height.
  179. * \return 0 in case of success, -1 if an error occurred.
  180. */
  181. int cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  182. unsigned int height)
  183. {
  184. if(cv->refcount && cv->resize_callback
  185. && !cv->resize_callback(cv->resize_data))
  186. {
  187. seterrno(EBUSY);
  188. return -1;
  189. }
  190. return cucul_resize(cv, width, height);
  191. }
  192. /** \brief Get the canvas width.
  193. *
  194. * Return the current canvas' width, in character cells.
  195. *
  196. * This function never fails.
  197. *
  198. * \param cv A libcucul canvas.
  199. * \return The canvas width.
  200. */
  201. unsigned int cucul_get_canvas_width(cucul_canvas_t const *cv)
  202. {
  203. return cv->width;
  204. }
  205. /** \brief Get the canvas height.
  206. *
  207. * Returns the current canvas' height, in character cells.
  208. *
  209. * This function never fails.
  210. *
  211. * \param cv A libcucul canvas.
  212. * \return The canvas height.
  213. */
  214. unsigned int cucul_get_canvas_height(cucul_canvas_t const *cv)
  215. {
  216. return cv->height;
  217. }
  218. /** \brief Get the canvas character array.
  219. *
  220. * Return the current canvas' internal character array. The array elements
  221. * consist in native endian 32-bit Unicode values as returned by
  222. * cucul_get_char().
  223. *
  224. * This function is only useful for display drivers such as the \e libcaca
  225. * library.
  226. *
  227. * This function never fails.
  228. *
  229. * \param cv A libcucul canvas.
  230. * \return The canvas character array.
  231. */
  232. unsigned char const * cucul_get_canvas_chars(cucul_canvas_t const *cv)
  233. {
  234. return (unsigned char const *)cv->chars;
  235. }
  236. /** \brief Get the canvas attribute array.
  237. *
  238. * Returns the current canvas' internal attribute array. The array elements
  239. * consist in native endian 32-bit attribute values as returned by
  240. * cucul_get_attr().
  241. *
  242. * This function is only useful for display drivers such as the \e libcaca
  243. * library.
  244. *
  245. * This function never fails.
  246. *
  247. * \param cv A libcucul canvas.
  248. * \return The canvas attribute array.
  249. */
  250. unsigned char const * cucul_get_canvas_attrs(cucul_canvas_t const *cv)
  251. {
  252. return (unsigned char const *)cv->attrs;
  253. }
  254. /** \brief Uninitialise \e libcucul.
  255. *
  256. * Free all resources allocated by cucul_create_canvas(). After
  257. * this function has been called, no other \e libcucul functions may be
  258. * used unless a new call to cucul_create_canvas() is done.
  259. *
  260. * If an error occurs, -1 is returned and \b errno is set accordingly:
  261. * - \c EBUSY The canvas is in use by a display driver and cannot be freed.
  262. *
  263. * \param cv A libcucul canvas.
  264. * \return 0 in case of success, -1 if an error occurred.
  265. */
  266. int cucul_free_canvas(cucul_canvas_t *cv)
  267. {
  268. unsigned int f;
  269. if(cv->refcount)
  270. {
  271. seterrno(EBUSY);
  272. return -1;
  273. }
  274. for(f = 0; f < cv->framecount; f++)
  275. {
  276. free(cv->frames[f].chars);
  277. free(cv->frames[f].attrs);
  278. free(cv->frames[f].name);
  279. }
  280. cucul_canvas_set_figfont(cv, NULL);
  281. free(cv->frames);
  282. free(cv);
  283. return 0;
  284. }
  285. /** \brief Generate a random integer within a range.
  286. *
  287. * Generate a random integer within the given range.
  288. *
  289. * This function never fails.
  290. *
  291. * \param min The lower bound of the integer range.
  292. * \param max The upper bound of the integer range.
  293. * \return A random integer comprised between \p min and \p max - 1
  294. * (inclusive).
  295. */
  296. int cucul_rand(int min, int max)
  297. {
  298. static int need_init = 1;
  299. if(need_init)
  300. {
  301. srand(getpid() + time(NULL));
  302. need_init = 0;
  303. }
  304. return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
  305. }
  306. /** \brief Return the \e libcucul version.
  307. *
  308. * Return a read-only string with the \e libcucul version information.
  309. *
  310. * This function never fails.
  311. *
  312. * \return The \e libcucul version information.
  313. */
  314. char const * cucul_get_version(void)
  315. {
  316. return VERSION;
  317. }
  318. /*
  319. * XXX: The following functions are local.
  320. */
  321. int cucul_resize(cucul_canvas_t *cv, unsigned int width, unsigned int height)
  322. {
  323. unsigned int x, y, f, old_width, old_height, new_size, old_size;
  324. old_width = cv->width;
  325. old_height = cv->height;
  326. old_size = old_width * old_height;
  327. _cucul_save_frame_info(cv);
  328. cv->width = width;
  329. cv->height = height;
  330. new_size = width * height;
  331. /* Step 1: if new area is bigger, resize the memory area now. */
  332. if(new_size > old_size)
  333. {
  334. for(f = 0; f < cv->framecount; f++)
  335. {
  336. cv->frames[f].chars = realloc(cv->frames[f].chars,
  337. new_size * sizeof(uint32_t));
  338. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  339. new_size * sizeof(uint32_t));
  340. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  341. {
  342. seterrno(ENOMEM);
  343. return -1;
  344. }
  345. }
  346. }
  347. /* Step 2: move line data if necessary. */
  348. if(width == old_width)
  349. {
  350. /* Width did not change, which means we do not need to move data. */
  351. ;
  352. }
  353. else if(width > old_width)
  354. {
  355. /* New width is bigger than old width, which means we need to
  356. * copy lines starting from the bottom of the screen otherwise
  357. * we will overwrite information. */
  358. for(f = 0; f < cv->framecount; f++)
  359. {
  360. uint32_t *chars = cv->frames[f].chars;
  361. uint32_t *attrs = cv->frames[f].attrs;
  362. for(y = height < old_height ? height : old_height; y--; )
  363. {
  364. uint32_t attr = cv->frames[f].curattr;
  365. for(x = old_width; x--; )
  366. {
  367. chars[y * width + x] = chars[y * old_width + x];
  368. attrs[y * width + x] = attrs[y * old_width + x];
  369. }
  370. /* Zero the end of the line */
  371. for(x = width - old_width; x--; )
  372. {
  373. chars[y * width + old_width + x] = (uint32_t)' ';
  374. attrs[y * width + old_width + x] = attr;
  375. }
  376. }
  377. }
  378. }
  379. else
  380. {
  381. /* New width is smaller. Copy as many lines as possible. Ignore
  382. * the first line, it is already in place. */
  383. unsigned int lines = height < old_height ? height : old_height;
  384. for(f = 0; f < cv->framecount; f++)
  385. {
  386. uint32_t *chars = cv->frames[f].chars;
  387. uint32_t *attrs = cv->frames[f].attrs;
  388. for(y = 1; y < lines; y++)
  389. {
  390. for(x = 0; x < width; x++)
  391. {
  392. chars[y * width + x] = chars[y * old_width + x];
  393. attrs[y * width + x] = attrs[y * old_width + x];
  394. }
  395. }
  396. }
  397. }
  398. /* Step 3: fill the bottom of the new screen if necessary. */
  399. if(height > old_height)
  400. {
  401. for(f = 0; f < cv->framecount; f++)
  402. {
  403. uint32_t *chars = cv->frames[f].chars;
  404. uint32_t *attrs = cv->frames[f].attrs;
  405. uint32_t attr = cv->frames[f].curattr;
  406. /* Zero the bottom of the screen */
  407. for(x = (height - old_height) * width; x--; )
  408. {
  409. chars[old_height * width + x] = (uint32_t)' ';
  410. attrs[old_height * width + x] = attr;
  411. }
  412. }
  413. }
  414. /* Step 4: if new area is smaller, resize memory area now. */
  415. if(new_size < old_size)
  416. {
  417. for(f = 0; f < cv->framecount; f++)
  418. {
  419. cv->frames[f].chars = realloc(cv->frames[f].chars,
  420. new_size * sizeof(uint32_t));
  421. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  422. new_size * sizeof(uint32_t));
  423. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  424. {
  425. seterrno(ENOMEM);
  426. return -1;
  427. }
  428. }
  429. }
  430. /* Set new size */
  431. for(f = 0; f < cv->framecount; f++)
  432. {
  433. if(cv->frames[f].x > (int)width)
  434. cv->frames[f].x = width;
  435. if(cv->frames[f].y > (int)height)
  436. cv->frames[f].y = height;
  437. cv->frames[f].width = width;
  438. cv->frames[f].height = height;
  439. }
  440. /* Reset the current frame shortcuts */
  441. _cucul_load_frame_info(cv);
  442. return 0;
  443. }