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.
 
 
 
 
 
 

544 lines
15 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright © 2002—2021 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://www.wtfpl.net/ 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. # include <limits.h>
  27. #endif
  28. #include "caca.h"
  29. #include "caca_internals.h"
  30. static int caca_resize(caca_canvas_t *, int, int);
  31. /** \brief Initialise a \e libcaca canvas.
  32. *
  33. * Initialise internal \e libcaca structures and the backend that will
  34. * be used for subsequent graphical operations. It must be the first
  35. * \e libcaca function to be called in a function. caca_free_canvas()
  36. * should be called at the end of the program to free all allocated resources.
  37. *
  38. * Both the cursor and the canvas' handle are initialised at the top-left
  39. * corner.
  40. *
  41. * If an error occurs, NULL is returned and \b errno is set accordingly:
  42. * - \c EINVAL Specified width or height is invalid.
  43. * - \c EOVERFLOW Specified width and height overflowed.
  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 EOVERFLOW Specified width and height overflowed.
  182. * - \c EBUSY The canvas is in use by a display driver and cannot be resized.
  183. * - \c ENOMEM Not enough memory for the requested canvas size. If this
  184. * happens, the canvas handle becomes invalid and should not be used.
  185. *
  186. * \param cv A libcaca canvas.
  187. * \param width The desired canvas width.
  188. * \param height The desired canvas height.
  189. * \return 0 in case of success, -1 if an error occurred.
  190. */
  191. int caca_set_canvas_size(caca_canvas_t *cv, int width, int height)
  192. {
  193. if(width < 0 || height < 0)
  194. {
  195. seterrno(EINVAL);
  196. return -1;
  197. }
  198. if(cv->refcount && cv->resize_callback
  199. && !cv->resize_callback(cv->resize_data))
  200. {
  201. seterrno(EBUSY);
  202. return -1;
  203. }
  204. return caca_resize(cv, width, height);
  205. }
  206. /** \brief Get the canvas width.
  207. *
  208. * Return the current canvas' width, in character cells.
  209. *
  210. * This function never fails.
  211. *
  212. * \param cv A libcaca canvas.
  213. * \return The canvas width.
  214. */
  215. int caca_get_canvas_width(caca_canvas_t const *cv)
  216. {
  217. return cv->width;
  218. }
  219. /** \brief Get the canvas height.
  220. *
  221. * Returns the current canvas' height, in character cells.
  222. *
  223. * This function never fails.
  224. *
  225. * \param cv A libcaca canvas.
  226. * \return The canvas height.
  227. */
  228. int caca_get_canvas_height(caca_canvas_t const *cv)
  229. {
  230. return cv->height;
  231. }
  232. /** \brief Get the canvas character array.
  233. *
  234. * Return the current canvas' internal character array. The array elements
  235. * consist in native endian 32-bit Unicode values as returned by
  236. * caca_get_char().
  237. *
  238. * This function is probably only useful for \e libcaca 's internal display
  239. * drivers.
  240. *
  241. * This function never fails.
  242. *
  243. * \param cv A libcaca canvas.
  244. * \return The canvas character array.
  245. */
  246. uint32_t const * caca_get_canvas_chars(caca_canvas_t const *cv)
  247. {
  248. return (uint32_t const *)cv->chars;
  249. }
  250. /** \brief Get the canvas attribute array.
  251. *
  252. * Returns the current canvas' internal attribute array. The array elements
  253. * consist in native endian 32-bit attribute values as returned by
  254. * caca_get_attr().
  255. *
  256. * This function is probably only useful for \e libcaca 's internal display
  257. * drivers.
  258. *
  259. * This function never fails.
  260. *
  261. * \param cv A libcaca canvas.
  262. * \return The canvas attribute array.
  263. */
  264. uint32_t const * caca_get_canvas_attrs(caca_canvas_t const *cv)
  265. {
  266. return (uint32_t const *)cv->attrs;
  267. }
  268. /** \brief Free a \e libcaca canvas.
  269. *
  270. * Free all resources allocated by caca_create_canvas(). The canvas
  271. * pointer becomes invalid and must no longer be used unless a new call
  272. * to caca_create_canvas() is made.
  273. *
  274. * If an error occurs, -1 is returned and \b errno is set accordingly:
  275. * - \c EBUSY The canvas is in use by a display driver and cannot be freed.
  276. *
  277. * \param cv A libcaca canvas.
  278. * \return 0 in case of success, -1 if an error occurred.
  279. */
  280. int caca_free_canvas(caca_canvas_t *cv)
  281. {
  282. int f;
  283. if(cv->refcount)
  284. {
  285. seterrno(EBUSY);
  286. return -1;
  287. }
  288. for(f = 0; f < cv->framecount; f++)
  289. {
  290. free(cv->frames[f].chars);
  291. free(cv->frames[f].attrs);
  292. free(cv->frames[f].name);
  293. }
  294. caca_canvas_set_figfont(cv, NULL);
  295. free(cv->frames);
  296. free(cv);
  297. return 0;
  298. }
  299. /** \brief Generate a random integer within a range.
  300. *
  301. * Generate a random integer within the given range.
  302. *
  303. * This function never fails.
  304. *
  305. * \param min The lower bound of the integer range.
  306. * \param max The upper bound of the integer range.
  307. * \return A random integer comprised between \p min and \p max - 1
  308. * (inclusive).
  309. */
  310. static caca_timer_t timer = {0, 0};
  311. int caca_rand(int min, int max)
  312. {
  313. static int need_init = 1;
  314. if(need_init)
  315. {
  316. srand(getpid() + _caca_getticks(&timer));
  317. need_init = 0;
  318. }
  319. return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
  320. }
  321. /*
  322. * XXX: The following functions are local.
  323. */
  324. int caca_resize(caca_canvas_t *cv, int width, int height)
  325. {
  326. int x, y, f, old_width, old_height, old_size;
  327. /* Check for overflow */
  328. if (width != 0 && height > INT_MAX / width)
  329. {
  330. seterrno(EOVERFLOW);
  331. return -1;
  332. }
  333. int new_size = width * height;
  334. /* Check for overflow when multiplying by sizeof(uint32_t) on 32-bit
  335. * systems */
  336. if (new_size > 0 && (size_t)new_size > SIZE_MAX / sizeof(uint32_t))
  337. {
  338. seterrno(EOVERFLOW);
  339. return -1;
  340. }
  341. old_width = cv->width;
  342. old_height = cv->height;
  343. old_size = old_width * old_height;
  344. _caca_save_frame_info(cv);
  345. /* Preload new width and height values into the canvas to optimise
  346. * dirty rectangle handling */
  347. cv->width = width;
  348. cv->height = height;
  349. /* If width or height is smaller (or both), we have the opportunity to
  350. * reduce or even remove dirty rectangles */
  351. if(width < old_width || height < old_height)
  352. _caca_clip_dirty_rect_list(cv);
  353. /* Step 1: if new area is bigger, resize the memory area now. */
  354. if(new_size > old_size)
  355. {
  356. for(f = 0; f < cv->framecount; f++)
  357. {
  358. cv->frames[f].chars = realloc(cv->frames[f].chars,
  359. new_size * sizeof(uint32_t));
  360. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  361. new_size * sizeof(uint32_t));
  362. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  363. {
  364. seterrno(ENOMEM);
  365. return -1;
  366. }
  367. }
  368. }
  369. /* Step 2: move line data if necessary. */
  370. if(width == old_width)
  371. {
  372. /* Width did not change, which means we do not need to move data. */
  373. ;
  374. }
  375. else if(width > old_width)
  376. {
  377. /* New width is bigger than old width, which means we need to
  378. * copy lines starting from the bottom of the screen otherwise
  379. * we will overwrite information. */
  380. for(f = 0; f < cv->framecount; f++)
  381. {
  382. uint32_t *chars = cv->frames[f].chars;
  383. uint32_t *attrs = cv->frames[f].attrs;
  384. for(y = height < old_height ? height : old_height; y--; )
  385. {
  386. uint32_t attr = cv->frames[f].curattr;
  387. for(x = old_width; x--; )
  388. {
  389. chars[y * width + x] = chars[y * old_width + x];
  390. attrs[y * width + x] = attrs[y * old_width + x];
  391. }
  392. /* Zero the end of the line */
  393. for(x = width - old_width; x--; )
  394. {
  395. chars[y * width + old_width + x] = (uint32_t)' ';
  396. attrs[y * width + old_width + x] = attr;
  397. }
  398. }
  399. }
  400. if(!cv->dirty_disabled)
  401. caca_add_dirty_rect(cv, old_width, 0,
  402. width - old_width, old_height);
  403. }
  404. else
  405. {
  406. /* New width is smaller. Copy as many lines as possible. Ignore
  407. * the first line, it is already in place. */
  408. int lines = height < old_height ? height : old_height;
  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. for(y = 1; y < lines; y++)
  414. {
  415. for(x = 0; x < width; x++)
  416. {
  417. chars[y * width + x] = chars[y * old_width + x];
  418. attrs[y * width + x] = attrs[y * old_width + x];
  419. }
  420. }
  421. }
  422. }
  423. /* Step 3: fill the bottom of the new screen if necessary. */
  424. if(height > old_height)
  425. {
  426. for(f = 0; f < cv->framecount; f++)
  427. {
  428. uint32_t *chars = cv->frames[f].chars;
  429. uint32_t *attrs = cv->frames[f].attrs;
  430. uint32_t attr = cv->frames[f].curattr;
  431. /* Zero the bottom of the screen */
  432. for(x = (height - old_height) * width; x--; )
  433. {
  434. chars[old_height * width + x] = (uint32_t)' ';
  435. attrs[old_height * width + x] = attr;
  436. }
  437. }
  438. if(!cv->dirty_disabled)
  439. caca_add_dirty_rect(cv, 0, old_height,
  440. old_width, height - old_height);
  441. }
  442. /* If both width and height are larger, there is a new dirty rectangle
  443. * that needs to be created in the lower right corner. */
  444. if(!cv->dirty_disabled &&
  445. width > old_width && height > old_height)
  446. caca_add_dirty_rect(cv, old_width, old_height,
  447. width - old_width, height - old_height);
  448. /* Step 4: if new area is smaller, resize memory area now. */
  449. if(new_size < old_size)
  450. {
  451. for(f = 0; f < cv->framecount; f++)
  452. {
  453. cv->frames[f].chars = realloc(cv->frames[f].chars,
  454. new_size * sizeof(uint32_t));
  455. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  456. new_size * sizeof(uint32_t));
  457. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  458. {
  459. seterrno(ENOMEM);
  460. return -1;
  461. }
  462. }
  463. }
  464. /* Set new size */
  465. for(f = 0; f < cv->framecount; f++)
  466. {
  467. if(cv->frames[f].x > (int)width)
  468. cv->frames[f].x = width;
  469. if(cv->frames[f].y > (int)height)
  470. cv->frames[f].y = height;
  471. cv->frames[f].width = width;
  472. cv->frames[f].height = height;
  473. }
  474. /* Reset the current frame shortcuts */
  475. _caca_load_frame_info(cv);
  476. return 0;
  477. }