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

547 рядки
16 KiB

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