您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains the main functions used by \e libcaca applications
  16. * to initialise a drawing context.
  17. */
  18. #include "config.h"
  19. #if !defined(__KERNEL__)
  20. # include <stdio.h>
  21. # include <stdlib.h>
  22. # include <string.h>
  23. # include <time.h>
  24. # include <sys/types.h>
  25. # if defined(HAVE_UNISTD_H)
  26. # include <unistd.h>
  27. # endif
  28. #endif
  29. #include "caca.h"
  30. #include "caca_internals.h"
  31. static int caca_resize(caca_canvas_t *, int, int);
  32. /** \brief Initialise a \e libcaca canvas.
  33. *
  34. * Initialise internal \e libcaca structures and the backend that will
  35. * be used for subsequent graphical operations. It must be the first
  36. * \e libcaca function to be called in a function. caca_free_canvas()
  37. * should be called at the end of the program to free all allocated resources.
  38. *
  39. * Both the cursor and the canvas' handle are initialised at the top-left
  40. * corner.
  41. *
  42. * If an error occurs, NULL is returned and \b errno is set accordingly:
  43. * - \c EINVAL Specified width or height is invalid.
  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->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 only useful for display drivers such as the \e libcaca
  236. * library.
  237. *
  238. * This function never fails.
  239. *
  240. * \param cv A libcaca canvas.
  241. * \return The canvas character array.
  242. */
  243. uint8_t const * caca_get_canvas_chars(caca_canvas_t const *cv)
  244. {
  245. return (uint8_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 only useful for display drivers such as the \e libcaca
  254. * library.
  255. *
  256. * This function never fails.
  257. *
  258. * \param cv A libcaca canvas.
  259. * \return The canvas attribute array.
  260. */
  261. uint8_t const * caca_get_canvas_attrs(caca_canvas_t const *cv)
  262. {
  263. return (uint8_t const *)cv->attrs;
  264. }
  265. /** \brief Uninitialise \e libcaca.
  266. *
  267. * Free all resources allocated by caca_create_canvas(). After
  268. * this function has been called, no other \e libcaca functions may be
  269. * used unless a new call to caca_create_canvas() is done.
  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. int caca_rand(int min, int max)
  308. {
  309. static int need_init = 1;
  310. if(need_init)
  311. {
  312. srand(getpid() + time(NULL));
  313. need_init = 0;
  314. }
  315. return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
  316. }
  317. /*
  318. * XXX: The following functions are local.
  319. */
  320. int caca_resize(caca_canvas_t *cv, int width, int height)
  321. {
  322. int x, y, f, old_width, old_height, new_size, old_size;
  323. old_width = cv->width;
  324. old_height = cv->height;
  325. old_size = old_width * old_height;
  326. _caca_save_frame_info(cv);
  327. cv->width = width;
  328. cv->height = height;
  329. new_size = width * height;
  330. /* Step 1: if new area is bigger, resize the memory area now. */
  331. if(new_size > old_size)
  332. {
  333. for(f = 0; f < cv->framecount; f++)
  334. {
  335. cv->frames[f].chars = realloc(cv->frames[f].chars,
  336. new_size * sizeof(uint32_t));
  337. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  338. new_size * sizeof(uint32_t));
  339. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  340. {
  341. seterrno(ENOMEM);
  342. return -1;
  343. }
  344. }
  345. }
  346. /* Step 2: move line data if necessary. */
  347. if(width == old_width)
  348. {
  349. /* Width did not change, which means we do not need to move data. */
  350. ;
  351. }
  352. else if(width > old_width)
  353. {
  354. /* New width is bigger than old width, which means we need to
  355. * copy lines starting from the bottom of the screen otherwise
  356. * we will overwrite information. */
  357. for(f = 0; f < cv->framecount; f++)
  358. {
  359. uint32_t *chars = cv->frames[f].chars;
  360. uint32_t *attrs = cv->frames[f].attrs;
  361. for(y = height < old_height ? height : old_height; y--; )
  362. {
  363. uint32_t attr = cv->frames[f].curattr;
  364. for(x = old_width; x--; )
  365. {
  366. chars[y * width + x] = chars[y * old_width + x];
  367. attrs[y * width + x] = attrs[y * old_width + x];
  368. }
  369. /* Zero the end of the line */
  370. for(x = width - old_width; x--; )
  371. {
  372. chars[y * width + old_width + x] = (uint32_t)' ';
  373. attrs[y * width + old_width + x] = attr;
  374. }
  375. }
  376. }
  377. }
  378. else
  379. {
  380. /* New width is smaller. Copy as many lines as possible. Ignore
  381. * the first line, it is already in place. */
  382. int lines = height < old_height ? height : old_height;
  383. for(f = 0; f < cv->framecount; f++)
  384. {
  385. uint32_t *chars = cv->frames[f].chars;
  386. uint32_t *attrs = cv->frames[f].attrs;
  387. for(y = 1; y < lines; y++)
  388. {
  389. for(x = 0; x < width; x++)
  390. {
  391. chars[y * width + x] = chars[y * old_width + x];
  392. attrs[y * width + x] = attrs[y * old_width + x];
  393. }
  394. }
  395. }
  396. }
  397. /* Step 3: fill the bottom of the new screen if necessary. */
  398. if(height > old_height)
  399. {
  400. for(f = 0; f < cv->framecount; f++)
  401. {
  402. uint32_t *chars = cv->frames[f].chars;
  403. uint32_t *attrs = cv->frames[f].attrs;
  404. uint32_t attr = cv->frames[f].curattr;
  405. /* Zero the bottom of the screen */
  406. for(x = (height - old_height) * width; x--; )
  407. {
  408. chars[old_height * width + x] = (uint32_t)' ';
  409. attrs[old_height * width + x] = attr;
  410. }
  411. }
  412. }
  413. /* Step 4: if new area is smaller, resize memory area now. */
  414. if(new_size < old_size)
  415. {
  416. for(f = 0; f < cv->framecount; f++)
  417. {
  418. cv->frames[f].chars = realloc(cv->frames[f].chars,
  419. new_size * sizeof(uint32_t));
  420. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  421. new_size * sizeof(uint32_t));
  422. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  423. {
  424. seterrno(ENOMEM);
  425. return -1;
  426. }
  427. }
  428. }
  429. /* Set new size */
  430. for(f = 0; f < cv->framecount; f++)
  431. {
  432. if(cv->frames[f].x > (int)width)
  433. cv->frames[f].x = width;
  434. if(cv->frames[f].y > (int)height)
  435. cv->frames[f].y = height;
  436. cv->frames[f].width = width;
  437. cv->frames[f].height = height;
  438. }
  439. /* Reset the current frame shortcuts */
  440. _caca_load_frame_info(cv);
  441. return 0;
  442. }
  443. /*
  444. * XXX: The following functions are aliases.
  445. */
  446. cucul_canvas_t * cucul_create_canvas(int, int) CACA_ALIAS(caca_create_canvas);
  447. int cucul_manage_canvas(cucul_canvas_t *, int (*)(void *), void *)
  448. CACA_ALIAS(caca_manage_canvas);
  449. int cucul_unmanage_canvas(cucul_canvas_t *, int (*)(void *), void *)
  450. CACA_ALIAS(caca_unmanage_canvas);
  451. int cucul_set_canvas_size(cucul_canvas_t *, int, int)
  452. CACA_ALIAS(caca_set_canvas_size);
  453. int cucul_get_canvas_width(cucul_canvas_t const *)
  454. CACA_ALIAS(caca_get_canvas_width);
  455. int cucul_get_canvas_height(cucul_canvas_t const *)
  456. CACA_ALIAS(caca_get_canvas_height);
  457. uint8_t const * cucul_get_canvas_chars(cucul_canvas_t const *)
  458. CACA_ALIAS(caca_get_canvas_chars);
  459. uint8_t const * cucul_get_canvas_attrs(cucul_canvas_t const *)
  460. CACA_ALIAS(caca_get_canvas_attrs);
  461. int cucul_free_canvas(cucul_canvas_t *) CACA_ALIAS(caca_free_canvas);
  462. int cucul_rand(int, int) CACA_ALIAS(caca_rand);