Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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