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.
 
 
 
 
 
 

537 rivejä
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. old_width = cv->width;
  335. old_height = cv->height;
  336. old_size = old_width * old_height;
  337. _caca_save_frame_info(cv);
  338. /* Preload new width and height values into the canvas to optimise
  339. * dirty rectangle handling */
  340. cv->width = width;
  341. cv->height = height;
  342. /* If width or height is smaller (or both), we have the opportunity to
  343. * reduce or even remove dirty rectangles */
  344. if(width < old_width || height < old_height)
  345. _caca_clip_dirty_rect_list(cv);
  346. /* Step 1: if new area is bigger, resize the memory area now. */
  347. if(new_size > old_size)
  348. {
  349. for(f = 0; f < cv->framecount; f++)
  350. {
  351. cv->frames[f].chars = realloc(cv->frames[f].chars,
  352. new_size * sizeof(uint32_t));
  353. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  354. new_size * sizeof(uint32_t));
  355. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  356. {
  357. seterrno(ENOMEM);
  358. return -1;
  359. }
  360. }
  361. }
  362. /* Step 2: move line data if necessary. */
  363. if(width == old_width)
  364. {
  365. /* Width did not change, which means we do not need to move data. */
  366. ;
  367. }
  368. else if(width > old_width)
  369. {
  370. /* New width is bigger than old width, which means we need to
  371. * copy lines starting from the bottom of the screen otherwise
  372. * we will overwrite information. */
  373. for(f = 0; f < cv->framecount; f++)
  374. {
  375. uint32_t *chars = cv->frames[f].chars;
  376. uint32_t *attrs = cv->frames[f].attrs;
  377. for(y = height < old_height ? height : old_height; y--; )
  378. {
  379. uint32_t attr = cv->frames[f].curattr;
  380. for(x = old_width; x--; )
  381. {
  382. chars[y * width + x] = chars[y * old_width + x];
  383. attrs[y * width + x] = attrs[y * old_width + x];
  384. }
  385. /* Zero the end of the line */
  386. for(x = width - old_width; x--; )
  387. {
  388. chars[y * width + old_width + x] = (uint32_t)' ';
  389. attrs[y * width + old_width + x] = attr;
  390. }
  391. }
  392. }
  393. if(!cv->dirty_disabled)
  394. caca_add_dirty_rect(cv, old_width, 0,
  395. width - old_width, old_height);
  396. }
  397. else
  398. {
  399. /* New width is smaller. Copy as many lines as possible. Ignore
  400. * the first line, it is already in place. */
  401. int lines = height < old_height ? height : old_height;
  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. for(y = 1; y < lines; y++)
  407. {
  408. for(x = 0; x < width; x++)
  409. {
  410. chars[y * width + x] = chars[y * old_width + x];
  411. attrs[y * width + x] = attrs[y * old_width + x];
  412. }
  413. }
  414. }
  415. }
  416. /* Step 3: fill the bottom of the new screen if necessary. */
  417. if(height > old_height)
  418. {
  419. for(f = 0; f < cv->framecount; f++)
  420. {
  421. uint32_t *chars = cv->frames[f].chars;
  422. uint32_t *attrs = cv->frames[f].attrs;
  423. uint32_t attr = cv->frames[f].curattr;
  424. /* Zero the bottom of the screen */
  425. for(x = (height - old_height) * width; x--; )
  426. {
  427. chars[old_height * width + x] = (uint32_t)' ';
  428. attrs[old_height * width + x] = attr;
  429. }
  430. }
  431. if(!cv->dirty_disabled)
  432. caca_add_dirty_rect(cv, 0, old_height,
  433. old_width, height - old_height);
  434. }
  435. /* If both width and height are larger, there is a new dirty rectangle
  436. * that needs to be created in the lower right corner. */
  437. if(!cv->dirty_disabled &&
  438. width > old_width && height > old_height)
  439. caca_add_dirty_rect(cv, old_width, old_height,
  440. width - old_width, height - old_height);
  441. /* Step 4: if new area is smaller, resize memory area now. */
  442. if(new_size < old_size)
  443. {
  444. for(f = 0; f < cv->framecount; f++)
  445. {
  446. cv->frames[f].chars = realloc(cv->frames[f].chars,
  447. new_size * sizeof(uint32_t));
  448. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  449. new_size * sizeof(uint32_t));
  450. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  451. {
  452. seterrno(ENOMEM);
  453. return -1;
  454. }
  455. }
  456. }
  457. /* Set new size */
  458. for(f = 0; f < cv->framecount; f++)
  459. {
  460. if(cv->frames[f].x > (int)width)
  461. cv->frames[f].x = width;
  462. if(cv->frames[f].y > (int)height)
  463. cv->frames[f].y = height;
  464. cv->frames[f].width = width;
  465. cv->frames[f].height = height;
  466. }
  467. /* Reset the current frame shortcuts */
  468. _caca_load_frame_info(cv);
  469. return 0;
  470. }