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.

преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. #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 EOVERFLOW Specified width and height overflowed.
  43. * - \c ENOMEM Not enough memory for the requested canvas size.
  44. *
  45. * \param width The desired canvas width
  46. * \param height The desired canvas height
  47. * \return A libcaca canvas handle upon success, NULL if an error occurred.
  48. */
  49. caca_canvas_t * caca_create_canvas(int width, int height)
  50. {
  51. caca_canvas_t *cv;
  52. if(width < 0 || height < 0)
  53. {
  54. seterrno(EINVAL);
  55. return NULL;
  56. }
  57. cv = malloc(sizeof(caca_canvas_t));
  58. if(!cv)
  59. goto nomem;
  60. cv->refcount = 0;
  61. cv->autoinc = 0;
  62. cv->resize_callback = NULL;
  63. cv->resize_data = NULL;
  64. cv->frame = 0;
  65. cv->framecount = 1;
  66. cv->frames = malloc(sizeof(struct caca_frame));
  67. if(!cv->frames)
  68. {
  69. free(cv);
  70. goto nomem;
  71. }
  72. cv->frames[0].width = cv->frames[0].height = 0;
  73. cv->frames[0].chars = NULL;
  74. cv->frames[0].attrs = NULL;
  75. cv->frames[0].x = cv->frames[0].y = 0;
  76. cv->frames[0].handlex = cv->frames[0].handley = 0;
  77. cv->frames[0].curattr = 0;
  78. cv->frames[0].name = strdup("frame#00000000");
  79. _caca_load_frame_info(cv);
  80. caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT);
  81. cv->ndirty = 0;
  82. cv->dirty_disabled = 0;
  83. cv->ff = NULL;
  84. if(caca_resize(cv, width, height) < 0)
  85. {
  86. int saved_errno = geterrno();
  87. free(cv->frames[0].name);
  88. free(cv->frames);
  89. free(cv);
  90. seterrno(saved_errno);
  91. return NULL;
  92. }
  93. return cv;
  94. nomem:
  95. seterrno(ENOMEM);
  96. return NULL;
  97. }
  98. /** \brief Manage a canvas.
  99. *
  100. * Lock a canvas to prevent it from being resized. If non-NULL,
  101. * the \e callback function pointer will be called upon each
  102. * \e caca_set_canvas_size call and if the returned value is zero, the
  103. * canvas resize request will be denied.
  104. *
  105. * This function is only useful for display drivers such as the \e libcaca
  106. * library.
  107. *
  108. * If an error occurs, -1 is returned and \b errno is set accordingly:
  109. * - \c EBUSY The canvas is already being managed.
  110. *
  111. * \param cv A libcaca canvas.
  112. * \param callback An optional callback function pointer.
  113. * \param p The argument to be passed to \e callback.
  114. * \return 0 in case of success, -1 if an error occurred.
  115. */
  116. int caca_manage_canvas(caca_canvas_t *cv, int (*callback)(void *), void *p)
  117. {
  118. if(cv->refcount)
  119. {
  120. seterrno(EBUSY);
  121. return -1;
  122. }
  123. cv->resize_callback = callback;
  124. cv->resize_data = p;
  125. cv->refcount = 1;
  126. return 0;
  127. }
  128. /** \brief unmanage a canvas.
  129. *
  130. * unlock a canvas previously locked by caca_manage_canvas(). for safety
  131. * reasons, the callback and callback data arguments must be the same as for
  132. * the caca_manage_canvas() call.
  133. *
  134. * this function is only useful for display drivers such as the \e libcaca
  135. * library.
  136. *
  137. * if an error occurs, -1 is returned and \b errno is set accordingly:
  138. * - \c einval the canvas is not managed, or the callback arguments do
  139. * not match.
  140. *
  141. * \param cv a libcaca canvas.
  142. * \param callback the \e callback argument previously passed to
  143. * caca_manage_canvas().
  144. * \param p the \e p argument previously passed to caca_manage_canvas().
  145. * \return 0 in case of success, -1 if an error occurred.
  146. */
  147. int caca_unmanage_canvas(caca_canvas_t *cv, int (*callback)(void *), void *p)
  148. {
  149. if(!cv->refcount
  150. || cv->resize_callback != callback || cv->resize_data != p)
  151. {
  152. seterrno(EINVAL);
  153. return -1;
  154. }
  155. cv->refcount = 0;
  156. return 0;
  157. }
  158. /** \brief Resize a canvas.
  159. *
  160. * Set the canvas' width and height, in character cells.
  161. *
  162. * The contents of the canvas are preserved to the extent of the new
  163. * canvas size. Newly allocated character cells at the right and/or at
  164. * the bottom of the canvas are filled with spaces.
  165. *
  166. * If as a result of the resize the cursor coordinates fall outside the
  167. * new canvas boundaries, they are readjusted. For instance, if the
  168. * current X cursor coordinate is 11 and the requested width is 10, the
  169. * new X cursor coordinate will be 10.
  170. *
  171. * It is an error to try to resize the canvas if an output driver has
  172. * been attached to the canvas using caca_create_display(). You need to
  173. * remove the output driver using caca_free_display() before you can change
  174. * the canvas size again. However, the caca output driver can cause a
  175. * canvas resize through user interaction. See the caca_event() documentation
  176. * for more about this.
  177. *
  178. * If an error occurs, -1 is returned and \b errno is set accordingly:
  179. * - \c EINVAL Specified width or height is invalid.
  180. * - \c EOVERFLOW Specified width and height overflowed.
  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 probably only useful for \e libcaca 's internal display
  238. * drivers.
  239. *
  240. * This function never fails.
  241. *
  242. * \param cv A libcaca canvas.
  243. * \return The canvas character array.
  244. */
  245. uint32_t const * caca_get_canvas_chars(caca_canvas_t const *cv)
  246. {
  247. return (uint32_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 probably only useful for \e libcaca 's internal display
  256. * drivers.
  257. *
  258. * This function never fails.
  259. *
  260. * \param cv A libcaca canvas.
  261. * \return The canvas attribute array.
  262. */
  263. uint32_t const * caca_get_canvas_attrs(caca_canvas_t const *cv)
  264. {
  265. return (uint32_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. static caca_timer_t timer = {0, 0};
  310. int caca_rand(int min, int max)
  311. {
  312. static int need_init = 1;
  313. if(need_init)
  314. {
  315. srand(getpid() + _caca_getticks(&timer));
  316. need_init = 0;
  317. }
  318. return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
  319. }
  320. /*
  321. * XXX: The following functions are local.
  322. */
  323. int caca_resize(caca_canvas_t *cv, int width, int height)
  324. {
  325. int x, y, f, old_width, old_height, old_size;
  326. /* Check for overflow */
  327. int new_size = width * height;
  328. if (new_size < 0 || (width > 0 && new_size / width != height))
  329. {
  330. seterrno(EOVERFLOW);
  331. return -1;
  332. }
  333. old_width = cv->width;
  334. old_height = cv->height;
  335. old_size = old_width * old_height;
  336. _caca_save_frame_info(cv);
  337. /* Preload new width and height values into the canvas to optimise
  338. * dirty rectangle handling */
  339. cv->width = width;
  340. cv->height = height;
  341. /* If width or height is smaller (or both), we have the opportunity to
  342. * reduce or even remove dirty rectangles */
  343. if(width < old_width || height < old_height)
  344. _caca_clip_dirty_rect_list(cv);
  345. /* Step 1: if new area is bigger, resize the memory area now. */
  346. if(new_size > old_size)
  347. {
  348. for(f = 0; f < cv->framecount; f++)
  349. {
  350. cv->frames[f].chars = realloc(cv->frames[f].chars,
  351. new_size * sizeof(uint32_t));
  352. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  353. new_size * sizeof(uint32_t));
  354. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  355. {
  356. seterrno(ENOMEM);
  357. return -1;
  358. }
  359. }
  360. }
  361. /* Step 2: move line data if necessary. */
  362. if(width == old_width)
  363. {
  364. /* Width did not change, which means we do not need to move data. */
  365. ;
  366. }
  367. else if(width > old_width)
  368. {
  369. /* New width is bigger than old width, which means we need to
  370. * copy lines starting from the bottom of the screen otherwise
  371. * we will overwrite information. */
  372. for(f = 0; f < cv->framecount; f++)
  373. {
  374. uint32_t *chars = cv->frames[f].chars;
  375. uint32_t *attrs = cv->frames[f].attrs;
  376. for(y = height < old_height ? height : old_height; y--; )
  377. {
  378. uint32_t attr = cv->frames[f].curattr;
  379. for(x = old_width; x--; )
  380. {
  381. chars[y * width + x] = chars[y * old_width + x];
  382. attrs[y * width + x] = attrs[y * old_width + x];
  383. }
  384. /* Zero the end of the line */
  385. for(x = width - old_width; x--; )
  386. {
  387. chars[y * width + old_width + x] = (uint32_t)' ';
  388. attrs[y * width + old_width + x] = attr;
  389. }
  390. }
  391. }
  392. if(!cv->dirty_disabled)
  393. caca_add_dirty_rect(cv, old_width, 0,
  394. width - old_width, old_height);
  395. }
  396. else
  397. {
  398. /* New width is smaller. Copy as many lines as possible. Ignore
  399. * the first line, it is already in place. */
  400. int lines = height < old_height ? height : old_height;
  401. for(f = 0; f < cv->framecount; f++)
  402. {
  403. uint32_t *chars = cv->frames[f].chars;
  404. uint32_t *attrs = cv->frames[f].attrs;
  405. for(y = 1; y < lines; y++)
  406. {
  407. for(x = 0; x < width; x++)
  408. {
  409. chars[y * width + x] = chars[y * old_width + x];
  410. attrs[y * width + x] = attrs[y * old_width + x];
  411. }
  412. }
  413. }
  414. }
  415. /* Step 3: fill the bottom of the new screen if necessary. */
  416. if(height > old_height)
  417. {
  418. for(f = 0; f < cv->framecount; f++)
  419. {
  420. uint32_t *chars = cv->frames[f].chars;
  421. uint32_t *attrs = cv->frames[f].attrs;
  422. uint32_t attr = cv->frames[f].curattr;
  423. /* Zero the bottom of the screen */
  424. for(x = (height - old_height) * width; x--; )
  425. {
  426. chars[old_height * width + x] = (uint32_t)' ';
  427. attrs[old_height * width + x] = attr;
  428. }
  429. }
  430. if(!cv->dirty_disabled)
  431. caca_add_dirty_rect(cv, 0, old_height,
  432. old_width, height - old_height);
  433. }
  434. /* If both width and height are larger, there is a new dirty rectangle
  435. * that needs to be created in the lower right corner. */
  436. if(!cv->dirty_disabled &&
  437. width > old_width && height > old_height)
  438. caca_add_dirty_rect(cv, old_width, old_height,
  439. width - old_width, height - old_height);
  440. /* Step 4: if new area is smaller, resize memory area now. */
  441. if(new_size < old_size)
  442. {
  443. for(f = 0; f < cv->framecount; f++)
  444. {
  445. cv->frames[f].chars = realloc(cv->frames[f].chars,
  446. new_size * sizeof(uint32_t));
  447. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  448. new_size * sizeof(uint32_t));
  449. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  450. {
  451. seterrno(ENOMEM);
  452. return -1;
  453. }
  454. }
  455. }
  456. /* Set new size */
  457. for(f = 0; f < cv->framecount; f++)
  458. {
  459. if(cv->frames[f].x > (int)width)
  460. cv->frames[f].x = width;
  461. if(cv->frames[f].y > (int)height)
  462. cv->frames[f].y = height;
  463. cv->frames[f].width = width;
  464. cv->frames[f].height = height;
  465. }
  466. /* Reset the current frame shortcuts */
  467. _caca_load_frame_info(cv);
  468. return 0;
  469. }