25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

canvas.c 16 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2012 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 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. static caca_timer_t timer = {0, 0};
  308. int caca_rand(int min, int max)
  309. {
  310. static int need_init = 1;
  311. if(need_init)
  312. {
  313. srand(getpid() + _caca_getticks(&timer));
  314. need_init = 0;
  315. }
  316. return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
  317. }
  318. /*
  319. * XXX: The following functions are local.
  320. */
  321. int caca_resize(caca_canvas_t *cv, int width, int height)
  322. {
  323. int x, y, f, old_width, old_height, new_size, old_size;
  324. old_width = cv->width;
  325. old_height = cv->height;
  326. old_size = old_width * old_height;
  327. _caca_save_frame_info(cv);
  328. /* Preload new width and height values into the canvas to optimise
  329. * dirty rectangle handling */
  330. cv->width = width;
  331. cv->height = height;
  332. new_size = width * height;
  333. /* If width or height is smaller (or both), we have the opportunity to
  334. * reduce or even remove dirty rectangles */
  335. if(width < old_width || height < old_height)
  336. _caca_clip_dirty_rect_list(cv);
  337. /* Step 1: if new area is bigger, resize the memory area now. */
  338. if(new_size > old_size)
  339. {
  340. for(f = 0; f < cv->framecount; f++)
  341. {
  342. cv->frames[f].chars = realloc(cv->frames[f].chars,
  343. new_size * sizeof(uint32_t));
  344. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  345. new_size * sizeof(uint32_t));
  346. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  347. {
  348. seterrno(ENOMEM);
  349. return -1;
  350. }
  351. }
  352. }
  353. /* Step 2: move line data if necessary. */
  354. if(width == old_width)
  355. {
  356. /* Width did not change, which means we do not need to move data. */
  357. ;
  358. }
  359. else if(width > old_width)
  360. {
  361. /* New width is bigger than old width, which means we need to
  362. * copy lines starting from the bottom of the screen otherwise
  363. * we will overwrite information. */
  364. for(f = 0; f < cv->framecount; f++)
  365. {
  366. uint32_t *chars = cv->frames[f].chars;
  367. uint32_t *attrs = cv->frames[f].attrs;
  368. for(y = height < old_height ? height : old_height; y--; )
  369. {
  370. uint32_t attr = cv->frames[f].curattr;
  371. for(x = old_width; x--; )
  372. {
  373. chars[y * width + x] = chars[y * old_width + x];
  374. attrs[y * width + x] = attrs[y * old_width + x];
  375. }
  376. /* Zero the end of the line */
  377. for(x = width - old_width; x--; )
  378. {
  379. chars[y * width + old_width + x] = (uint32_t)' ';
  380. attrs[y * width + old_width + x] = attr;
  381. }
  382. }
  383. }
  384. if(!cv->dirty_disabled)
  385. caca_add_dirty_rect(cv, old_width, 0,
  386. width - old_width, old_height);
  387. }
  388. else
  389. {
  390. /* New width is smaller. Copy as many lines as possible. Ignore
  391. * the first line, it is already in place. */
  392. int lines = height < old_height ? height : old_height;
  393. for(f = 0; f < cv->framecount; f++)
  394. {
  395. uint32_t *chars = cv->frames[f].chars;
  396. uint32_t *attrs = cv->frames[f].attrs;
  397. for(y = 1; y < lines; y++)
  398. {
  399. for(x = 0; x < width; x++)
  400. {
  401. chars[y * width + x] = chars[y * old_width + x];
  402. attrs[y * width + x] = attrs[y * old_width + x];
  403. }
  404. }
  405. }
  406. }
  407. /* Step 3: fill the bottom of the new screen if necessary. */
  408. if(height > old_height)
  409. {
  410. for(f = 0; f < cv->framecount; f++)
  411. {
  412. uint32_t *chars = cv->frames[f].chars;
  413. uint32_t *attrs = cv->frames[f].attrs;
  414. uint32_t attr = cv->frames[f].curattr;
  415. /* Zero the bottom of the screen */
  416. for(x = (height - old_height) * width; x--; )
  417. {
  418. chars[old_height * width + x] = (uint32_t)' ';
  419. attrs[old_height * width + x] = attr;
  420. }
  421. }
  422. if(!cv->dirty_disabled)
  423. caca_add_dirty_rect(cv, 0, old_height,
  424. old_width, height - old_height);
  425. }
  426. /* If both width and height are larger, there is a new dirty rectangle
  427. * that needs to be created in the lower right corner. */
  428. if(!cv->dirty_disabled &&
  429. width > old_width && height > old_height)
  430. caca_add_dirty_rect(cv, old_width, old_height,
  431. width - old_width, height - old_height);
  432. /* Step 4: if new area is smaller, resize memory area now. */
  433. if(new_size < old_size)
  434. {
  435. for(f = 0; f < cv->framecount; f++)
  436. {
  437. cv->frames[f].chars = realloc(cv->frames[f].chars,
  438. new_size * sizeof(uint32_t));
  439. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  440. new_size * sizeof(uint32_t));
  441. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  442. {
  443. seterrno(ENOMEM);
  444. return -1;
  445. }
  446. }
  447. }
  448. /* Set new size */
  449. for(f = 0; f < cv->framecount; f++)
  450. {
  451. if(cv->frames[f].x > (int)width)
  452. cv->frames[f].x = width;
  453. if(cv->frames[f].y > (int)height)
  454. cv->frames[f].y = height;
  455. cv->frames[f].width = width;
  456. cv->frames[f].height = height;
  457. }
  458. /* Reset the current frame shortcuts */
  459. _caca_load_frame_info(cv);
  460. return 0;
  461. }
  462. /*
  463. * XXX: The following functions are aliases.
  464. */
  465. cucul_canvas_t * cucul_create_canvas(int, int) CACA_ALIAS(caca_create_canvas);
  466. int cucul_manage_canvas(cucul_canvas_t *, int (*)(void *), void *)
  467. CACA_ALIAS(caca_manage_canvas);
  468. int cucul_unmanage_canvas(cucul_canvas_t *, int (*)(void *), void *)
  469. CACA_ALIAS(caca_unmanage_canvas);
  470. int cucul_set_canvas_size(cucul_canvas_t *, int, int)
  471. CACA_ALIAS(caca_set_canvas_size);
  472. int cucul_get_canvas_width(cucul_canvas_t const *)
  473. CACA_ALIAS(caca_get_canvas_width);
  474. int cucul_get_canvas_height(cucul_canvas_t const *)
  475. CACA_ALIAS(caca_get_canvas_height);
  476. uint32_t const * cucul_get_canvas_chars(cucul_canvas_t const *)
  477. CACA_ALIAS(caca_get_canvas_chars);
  478. uint32_t const * cucul_get_canvas_attrs(cucul_canvas_t const *)
  479. CACA_ALIAS(caca_get_canvas_attrs);
  480. int cucul_free_canvas(cucul_canvas_t *) CACA_ALIAS(caca_free_canvas);
  481. int cucul_rand(int, int) CACA_ALIAS(caca_rand);