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. #endif
  27. #ifdef _WIN32
  28. #define WIN32_LEAN_AND_MEAN
  29. #include <windows.h>
  30. #endif
  31. #include "caca.h"
  32. #include "caca_internals.h"
  33. static int caca_resize(caca_canvas_t *, int, int);
  34. /** \brief Initialise a \e libcaca canvas.
  35. *
  36. * Initialise internal \e libcaca structures and the backend that will
  37. * be used for subsequent graphical operations. It must be the first
  38. * \e libcaca function to be called in a function. caca_free_canvas()
  39. * should be called at the end of the program to free all allocated resources.
  40. *
  41. * Both the cursor and the canvas' handle are initialised at the top-left
  42. * corner.
  43. *
  44. * If an error occurs, NULL is returned and \b errno is set accordingly:
  45. * - \c EINVAL Specified width or height is invalid.
  46. * - \c EOVERFLOW Specified width and height overflowed.
  47. * - \c ENOMEM Not enough memory for the requested canvas size.
  48. *
  49. * \param width The desired canvas width
  50. * \param height The desired canvas height
  51. * \return A libcaca canvas handle upon success, NULL if an error occurred.
  52. */
  53. caca_canvas_t * caca_create_canvas(int width, int height)
  54. {
  55. caca_canvas_t *cv;
  56. if(width < 0 || height < 0)
  57. {
  58. seterrno(EINVAL);
  59. return NULL;
  60. }
  61. cv = malloc(sizeof(caca_canvas_t));
  62. if(!cv)
  63. goto nomem;
  64. cv->refcount = 0;
  65. cv->autoinc = 0;
  66. cv->resize_callback = NULL;
  67. cv->resize_data = NULL;
  68. cv->frame = 0;
  69. cv->framecount = 1;
  70. cv->frames = malloc(sizeof(struct caca_frame));
  71. if(!cv->frames)
  72. {
  73. free(cv);
  74. goto nomem;
  75. }
  76. cv->frames[0].width = cv->frames[0].height = 0;
  77. cv->frames[0].chars = NULL;
  78. cv->frames[0].attrs = NULL;
  79. cv->frames[0].x = cv->frames[0].y = 0;
  80. cv->frames[0].handlex = cv->frames[0].handley = 0;
  81. cv->frames[0].curattr = 0;
  82. cv->frames[0].name = strdup("frame#00000000");
  83. _caca_load_frame_info(cv);
  84. caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT);
  85. cv->ndirty = 0;
  86. cv->dirty_disabled = 0;
  87. cv->ff = NULL;
  88. if(caca_resize(cv, width, height) < 0)
  89. {
  90. int saved_errno = geterrno();
  91. free(cv->frames[0].name);
  92. free(cv->frames);
  93. free(cv);
  94. seterrno(saved_errno);
  95. return NULL;
  96. }
  97. return cv;
  98. nomem:
  99. seterrno(ENOMEM);
  100. return NULL;
  101. }
  102. /** \brief Manage a canvas.
  103. *
  104. * Lock a canvas to prevent it from being resized. If non-NULL,
  105. * the \e callback function pointer will be called upon each
  106. * \e caca_set_canvas_size call and if the returned value is zero, the
  107. * canvas resize request will be denied.
  108. *
  109. * This function is only useful for display drivers such as the \e libcaca
  110. * library.
  111. *
  112. * If an error occurs, -1 is returned and \b errno is set accordingly:
  113. * - \c EBUSY The canvas is already being managed.
  114. *
  115. * \param cv A libcaca canvas.
  116. * \param callback An optional callback function pointer.
  117. * \param p The argument to be passed to \e callback.
  118. * \return 0 in case of success, -1 if an error occurred.
  119. */
  120. int caca_manage_canvas(caca_canvas_t *cv, int (*callback)(void *), void *p)
  121. {
  122. if(cv->refcount)
  123. {
  124. seterrno(EBUSY);
  125. return -1;
  126. }
  127. cv->resize_callback = callback;
  128. cv->resize_data = p;
  129. cv->refcount = 1;
  130. return 0;
  131. }
  132. /** \brief unmanage a canvas.
  133. *
  134. * unlock a canvas previously locked by caca_manage_canvas(). for safety
  135. * reasons, the callback and callback data arguments must be the same as for
  136. * the caca_manage_canvas() call.
  137. *
  138. * this function is only useful for display drivers such as the \e libcaca
  139. * library.
  140. *
  141. * if an error occurs, -1 is returned and \b errno is set accordingly:
  142. * - \c einval the canvas is not managed, or the callback arguments do
  143. * not match.
  144. *
  145. * \param cv a libcaca canvas.
  146. * \param callback the \e callback argument previously passed to
  147. * caca_manage_canvas().
  148. * \param p the \e p argument previously passed to caca_manage_canvas().
  149. * \return 0 in case of success, -1 if an error occurred.
  150. */
  151. int caca_unmanage_canvas(caca_canvas_t *cv, int (*callback)(void *), void *p)
  152. {
  153. if(!cv->refcount
  154. || cv->resize_callback != callback || cv->resize_data != p)
  155. {
  156. seterrno(EINVAL);
  157. return -1;
  158. }
  159. cv->refcount = 0;
  160. return 0;
  161. }
  162. /** \brief Resize a canvas.
  163. *
  164. * Set the canvas' width and height, in character cells.
  165. *
  166. * The contents of the canvas are preserved to the extent of the new
  167. * canvas size. Newly allocated character cells at the right and/or at
  168. * the bottom of the canvas are filled with spaces.
  169. *
  170. * If as a result of the resize the cursor coordinates fall outside the
  171. * new canvas boundaries, they are readjusted. For instance, if the
  172. * current X cursor coordinate is 11 and the requested width is 10, the
  173. * new X cursor coordinate will be 10.
  174. *
  175. * It is an error to try to resize the canvas if an output driver has
  176. * been attached to the canvas using caca_create_display(). You need to
  177. * remove the output driver using caca_free_display() before you can change
  178. * the canvas size again. However, the caca output driver can cause a
  179. * canvas resize through user interaction. See the caca_event() documentation
  180. * for more about this.
  181. *
  182. * If an error occurs, -1 is returned and \b errno is set accordingly:
  183. * - \c EINVAL Specified width or height is invalid.
  184. * - \c EOVERFLOW Specified width and height overflowed.
  185. * - \c EBUSY The canvas is in use by a display driver and cannot be resized.
  186. * - \c ENOMEM Not enough memory for the requested canvas size. If this
  187. * happens, the canvas handle becomes invalid and should not be used.
  188. *
  189. * \param cv A libcaca canvas.
  190. * \param width The desired canvas width.
  191. * \param height The desired canvas height.
  192. * \return 0 in case of success, -1 if an error occurred.
  193. */
  194. int caca_set_canvas_size(caca_canvas_t *cv, int width, int height)
  195. {
  196. if(width < 0 || height < 0)
  197. {
  198. seterrno(EINVAL);
  199. return -1;
  200. }
  201. if(cv->refcount && cv->resize_callback
  202. && !cv->resize_callback(cv->resize_data))
  203. {
  204. seterrno(EBUSY);
  205. return -1;
  206. }
  207. return caca_resize(cv, width, height);
  208. }
  209. /** \brief Get the canvas width.
  210. *
  211. * Return the current canvas' width, in character cells.
  212. *
  213. * This function never fails.
  214. *
  215. * \param cv A libcaca canvas.
  216. * \return The canvas width.
  217. */
  218. int caca_get_canvas_width(caca_canvas_t const *cv)
  219. {
  220. return cv->width;
  221. }
  222. /** \brief Get the canvas height.
  223. *
  224. * Returns the current canvas' height, in character cells.
  225. *
  226. * This function never fails.
  227. *
  228. * \param cv A libcaca canvas.
  229. * \return The canvas height.
  230. */
  231. int caca_get_canvas_height(caca_canvas_t const *cv)
  232. {
  233. return cv->height;
  234. }
  235. /** \brief Get the canvas character array.
  236. *
  237. * Return the current canvas' internal character array. The array elements
  238. * consist in native endian 32-bit Unicode values as returned by
  239. * caca_get_char().
  240. *
  241. * This function is probably only useful for \e libcaca 's internal display
  242. * drivers.
  243. *
  244. * This function never fails.
  245. *
  246. * \param cv A libcaca canvas.
  247. * \return The canvas character array.
  248. */
  249. uint32_t const * caca_get_canvas_chars(caca_canvas_t const *cv)
  250. {
  251. return (uint32_t const *)cv->chars;
  252. }
  253. /** \brief Get the canvas attribute array.
  254. *
  255. * Returns the current canvas' internal attribute array. The array elements
  256. * consist in native endian 32-bit attribute values as returned by
  257. * caca_get_attr().
  258. *
  259. * This function is probably only useful for \e libcaca 's internal display
  260. * drivers.
  261. *
  262. * This function never fails.
  263. *
  264. * \param cv A libcaca canvas.
  265. * \return The canvas attribute array.
  266. */
  267. uint32_t const * caca_get_canvas_attrs(caca_canvas_t const *cv)
  268. {
  269. return (uint32_t const *)cv->attrs;
  270. }
  271. /** \brief Free a \e libcaca canvas.
  272. *
  273. * Free all resources allocated by caca_create_canvas(). The canvas
  274. * pointer becomes invalid and must no longer be used unless a new call
  275. * to caca_create_canvas() is made.
  276. *
  277. * If an error occurs, -1 is returned and \b errno is set accordingly:
  278. * - \c EBUSY The canvas is in use by a display driver and cannot be freed.
  279. *
  280. * \param cv A libcaca canvas.
  281. * \return 0 in case of success, -1 if an error occurred.
  282. */
  283. int caca_free_canvas(caca_canvas_t *cv)
  284. {
  285. int f;
  286. if(cv->refcount)
  287. {
  288. seterrno(EBUSY);
  289. return -1;
  290. }
  291. for(f = 0; f < cv->framecount; f++)
  292. {
  293. free(cv->frames[f].chars);
  294. free(cv->frames[f].attrs);
  295. free(cv->frames[f].name);
  296. }
  297. caca_canvas_set_figfont(cv, NULL);
  298. free(cv->frames);
  299. free(cv);
  300. return 0;
  301. }
  302. /** \brief Generate a random integer within a range.
  303. *
  304. * Generate a random integer within the given range.
  305. *
  306. * This function never fails.
  307. *
  308. * \param min The lower bound of the integer range.
  309. * \param max The upper bound of the integer range.
  310. * \return A random integer comprised between \p min and \p max - 1
  311. * (inclusive).
  312. */
  313. static caca_timer_t timer = {0, 0};
  314. int caca_rand(int min, int max)
  315. {
  316. static int need_init = 1;
  317. if(need_init)
  318. {
  319. #ifdef _WIN32
  320. srand(GetCurrentProcessId() + _caca_getticks(&timer));
  321. #else
  322. srand(getpid() + _caca_getticks(&timer));
  323. #endif
  324. need_init = 0;
  325. }
  326. return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
  327. }
  328. /*
  329. * XXX: The following functions are local.
  330. */
  331. int caca_resize(caca_canvas_t *cv, int width, int height)
  332. {
  333. int x, y, f, old_width, old_height, old_size;
  334. /* Check for overflow */
  335. int new_size = width * height;
  336. if (new_size < 0 || (width > 0 && new_size / width != height))
  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. }