Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

515 rader
14 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  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 libcucul 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 "cucul.h"
  30. #include "cucul_internals.h"
  31. static int cucul_resize(cucul_canvas_t *, int, int);
  32. /** \brief Initialise a \e libcucul canvas.
  33. *
  34. * Initialise internal \e libcucul structures and the backend that will
  35. * be used for subsequent graphical operations. It must be the first
  36. * \e libcucul function to be called in a function. cucul_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 libcucul canvas handle upon success, NULL if an error occurred.
  49. */
  50. cucul_canvas_t * cucul_create_canvas(int width, int height)
  51. {
  52. cucul_canvas_t *cv;
  53. if(width < 0 || height < 0)
  54. {
  55. seterrno(EINVAL);
  56. return NULL;
  57. }
  58. cv = malloc(sizeof(cucul_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 cucul_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. _cucul_load_frame_info(cv);
  81. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  82. cv->ff = NULL;
  83. if(cucul_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 cucul_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 libcucul 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 cucul_manage_canvas(cucul_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 cucul_manage_canvas(). For safety
  130. * reasons, the callback and callback data arguments must be the same as for
  131. * the cucul_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 libcucul canvas.
  141. * \param callback The \e callback argument previously passed to
  142. cucul_manage_canvas().
  143. * \param p The \e p argument previously passed to cucul_manage_canvas().
  144. * \return 0 in case of success, -1 if an error occurred.
  145. */
  146. int cucul_unmanage_canvas(cucul_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 libcucul 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 cucul_set_canvas_size(cucul_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 cucul_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 libcucul canvas.
  210. * \return The canvas width.
  211. */
  212. int cucul_get_canvas_width(cucul_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 libcucul canvas.
  223. * \return The canvas height.
  224. */
  225. int cucul_get_canvas_height(cucul_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. * cucul_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 libcucul canvas.
  241. * \return The canvas character array.
  242. */
  243. uint8_t const * cucul_get_canvas_chars(cucul_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. * cucul_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 libcucul canvas.
  259. * \return The canvas attribute array.
  260. */
  261. uint8_t const * cucul_get_canvas_attrs(cucul_canvas_t const *cv)
  262. {
  263. return (uint8_t const *)cv->attrs;
  264. }
  265. /** \brief Uninitialise \e libcucul.
  266. *
  267. * Free all resources allocated by cucul_create_canvas(). After
  268. * this function has been called, no other \e libcucul functions may be
  269. * used unless a new call to cucul_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 libcucul canvas.
  275. * \return 0 in case of success, -1 if an error occurred.
  276. */
  277. int cucul_free_canvas(cucul_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. cucul_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 cucul_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. /** \brief Return the \e libcucul version.
  318. *
  319. * Return a read-only string with the \e libcucul version information.
  320. *
  321. * This function never fails.
  322. *
  323. * \return The \e libcucul version information.
  324. */
  325. char const * cucul_get_version(void)
  326. {
  327. return VERSION;
  328. }
  329. /*
  330. * XXX: The following functions are local.
  331. */
  332. int cucul_resize(cucul_canvas_t *cv, int width, int height)
  333. {
  334. int x, y, f, old_width, old_height, new_size, old_size;
  335. old_width = cv->width;
  336. old_height = cv->height;
  337. old_size = old_width * old_height;
  338. _cucul_save_frame_info(cv);
  339. cv->width = width;
  340. cv->height = height;
  341. new_size = width * height;
  342. /* Step 1: if new area is bigger, resize the memory area now. */
  343. if(new_size > old_size)
  344. {
  345. for(f = 0; f < cv->framecount; f++)
  346. {
  347. cv->frames[f].chars = realloc(cv->frames[f].chars,
  348. new_size * sizeof(uint32_t));
  349. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  350. new_size * sizeof(uint32_t));
  351. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  352. {
  353. seterrno(ENOMEM);
  354. return -1;
  355. }
  356. }
  357. }
  358. /* Step 2: move line data if necessary. */
  359. if(width == old_width)
  360. {
  361. /* Width did not change, which means we do not need to move data. */
  362. ;
  363. }
  364. else if(width > old_width)
  365. {
  366. /* New width is bigger than old width, which means we need to
  367. * copy lines starting from the bottom of the screen otherwise
  368. * we will overwrite information. */
  369. for(f = 0; f < cv->framecount; f++)
  370. {
  371. uint32_t *chars = cv->frames[f].chars;
  372. uint32_t *attrs = cv->frames[f].attrs;
  373. for(y = height < old_height ? height : old_height; y--; )
  374. {
  375. uint32_t attr = cv->frames[f].curattr;
  376. for(x = old_width; x--; )
  377. {
  378. chars[y * width + x] = chars[y * old_width + x];
  379. attrs[y * width + x] = attrs[y * old_width + x];
  380. }
  381. /* Zero the end of the line */
  382. for(x = width - old_width; x--; )
  383. {
  384. chars[y * width + old_width + x] = (uint32_t)' ';
  385. attrs[y * width + old_width + x] = attr;
  386. }
  387. }
  388. }
  389. }
  390. else
  391. {
  392. /* New width is smaller. Copy as many lines as possible. Ignore
  393. * the first line, it is already in place. */
  394. int lines = height < old_height ? height : old_height;
  395. for(f = 0; f < cv->framecount; f++)
  396. {
  397. uint32_t *chars = cv->frames[f].chars;
  398. uint32_t *attrs = cv->frames[f].attrs;
  399. for(y = 1; y < lines; y++)
  400. {
  401. for(x = 0; x < width; x++)
  402. {
  403. chars[y * width + x] = chars[y * old_width + x];
  404. attrs[y * width + x] = attrs[y * old_width + x];
  405. }
  406. }
  407. }
  408. }
  409. /* Step 3: fill the bottom of the new screen if necessary. */
  410. if(height > old_height)
  411. {
  412. for(f = 0; f < cv->framecount; f++)
  413. {
  414. uint32_t *chars = cv->frames[f].chars;
  415. uint32_t *attrs = cv->frames[f].attrs;
  416. uint32_t attr = cv->frames[f].curattr;
  417. /* Zero the bottom of the screen */
  418. for(x = (height - old_height) * width; x--; )
  419. {
  420. chars[old_height * width + x] = (uint32_t)' ';
  421. attrs[old_height * width + x] = attr;
  422. }
  423. }
  424. }
  425. /* Step 4: if new area is smaller, resize memory area now. */
  426. if(new_size < old_size)
  427. {
  428. for(f = 0; f < cv->framecount; f++)
  429. {
  430. cv->frames[f].chars = realloc(cv->frames[f].chars,
  431. new_size * sizeof(uint32_t));
  432. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  433. new_size * sizeof(uint32_t));
  434. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  435. {
  436. seterrno(ENOMEM);
  437. return -1;
  438. }
  439. }
  440. }
  441. /* Set new size */
  442. for(f = 0; f < cv->framecount; f++)
  443. {
  444. if(cv->frames[f].x > (int)width)
  445. cv->frames[f].x = width;
  446. if(cv->frames[f].y > (int)height)
  447. cv->frames[f].y = height;
  448. cv->frames[f].width = width;
  449. cv->frames[f].height = height;
  450. }
  451. /* Reset the current frame shortcuts */
  452. _cucul_load_frame_info(cv);
  453. return 0;
  454. }