No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

537 líneas
15 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. 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. caca_add_dirty_rect(cv, old_width, 0, width - old_width, old_height);
  379. }
  380. else
  381. {
  382. /* New width is smaller. Copy as many lines as possible. Ignore
  383. * the first line, it is already in place. */
  384. int lines = height < old_height ? height : old_height;
  385. for(f = 0; f < cv->framecount; f++)
  386. {
  387. uint32_t *chars = cv->frames[f].chars;
  388. uint32_t *attrs = cv->frames[f].attrs;
  389. for(y = 1; y < lines; y++)
  390. {
  391. for(x = 0; x < width; x++)
  392. {
  393. chars[y * width + x] = chars[y * old_width + x];
  394. attrs[y * width + x] = attrs[y * old_width + x];
  395. }
  396. }
  397. }
  398. }
  399. /* Step 3: fill the bottom of the new screen if necessary. */
  400. if(height > old_height)
  401. {
  402. for(f = 0; f < cv->framecount; f++)
  403. {
  404. uint32_t *chars = cv->frames[f].chars;
  405. uint32_t *attrs = cv->frames[f].attrs;
  406. uint32_t attr = cv->frames[f].curattr;
  407. /* Zero the bottom of the screen */
  408. for(x = (height - old_height) * width; x--; )
  409. {
  410. chars[old_height * width + x] = (uint32_t)' ';
  411. attrs[old_height * width + x] = attr;
  412. }
  413. }
  414. caca_add_dirty_rect(cv, 0, old_height, old_width, height - old_height);
  415. }
  416. /* XXX: technically we should not worry about the dirty rectangle in
  417. * the bottom-right corner, because we only handle one dirty rectangle,
  418. * but in case the API changes later, we make sure this is handled. */
  419. if(width > old_width && height > old_height)
  420. caca_add_dirty_rect(cv, old_width, old_height,
  421. width - old_width, height - old_height);
  422. /* Step 4: if new area is smaller, resize memory area now. */
  423. if(new_size < old_size)
  424. {
  425. for(f = 0; f < cv->framecount; f++)
  426. {
  427. cv->frames[f].chars = realloc(cv->frames[f].chars,
  428. new_size * sizeof(uint32_t));
  429. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  430. new_size * sizeof(uint32_t));
  431. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  432. {
  433. seterrno(ENOMEM);
  434. return -1;
  435. }
  436. }
  437. }
  438. /* Set new size */
  439. for(f = 0; f < cv->framecount; f++)
  440. {
  441. if(cv->frames[f].x > (int)width)
  442. cv->frames[f].x = width;
  443. if(cv->frames[f].y > (int)height)
  444. cv->frames[f].y = height;
  445. cv->frames[f].width = width;
  446. cv->frames[f].height = height;
  447. }
  448. /* Reset the current frame shortcuts */
  449. _caca_load_frame_info(cv);
  450. return 0;
  451. }
  452. /*
  453. * XXX: The following functions are aliases.
  454. */
  455. cucul_canvas_t * cucul_create_canvas(int, int) CACA_ALIAS(caca_create_canvas);
  456. int cucul_manage_canvas(cucul_canvas_t *, int (*)(void *), void *)
  457. CACA_ALIAS(caca_manage_canvas);
  458. int cucul_unmanage_canvas(cucul_canvas_t *, int (*)(void *), void *)
  459. CACA_ALIAS(caca_unmanage_canvas);
  460. int cucul_set_canvas_size(cucul_canvas_t *, int, int)
  461. CACA_ALIAS(caca_set_canvas_size);
  462. int cucul_get_canvas_width(cucul_canvas_t const *)
  463. CACA_ALIAS(caca_get_canvas_width);
  464. int cucul_get_canvas_height(cucul_canvas_t const *)
  465. CACA_ALIAS(caca_get_canvas_height);
  466. uint8_t const * cucul_get_canvas_chars(cucul_canvas_t const *)
  467. CACA_ALIAS(caca_get_canvas_chars);
  468. uint8_t const * cucul_get_canvas_attrs(cucul_canvas_t const *)
  469. CACA_ALIAS(caca_get_canvas_attrs);
  470. int cucul_free_canvas(cucul_canvas_t *) CACA_ALIAS(caca_free_canvas);
  471. int cucul_rand(int, int) CACA_ALIAS(caca_rand);