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.
 
 
 
 
 
 

543 lines
16 KiB

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