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.
 
 
 
 
 
 

482 satır
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. #include "common.h"
  20. #if !defined(__KERNEL__)
  21. # include <stdio.h>
  22. # include <stdlib.h>
  23. # include <string.h>
  24. # include <time.h>
  25. # include <sys/types.h>
  26. # if defined(HAVE_UNISTD_H)
  27. # include <unistd.h>
  28. # endif
  29. #endif
  30. #include "cucul.h"
  31. #include "cucul_internals.h"
  32. static int cucul_resize(cucul_canvas_t *, unsigned int, unsigned int);
  33. /** \brief Initialise a \e libcucul canvas.
  34. *
  35. * Initialise internal \e libcucul structures and the backend that will
  36. * be used for subsequent graphical operations. It must be the first
  37. * \e libcucul function to be called in a function. cucul_free_canvas()
  38. * should be called at the end of the program to free all allocated resources.
  39. *
  40. * Both the cursor and the canvas' handle are initialised at the top-left
  41. * corner.
  42. *
  43. * If an error occurs, NULL is returned and \b errno is set accordingly:
  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(unsigned int width, unsigned int height)
  51. {
  52. cucul_canvas_t *cv = malloc(sizeof(cucul_canvas_t));
  53. if(!cv)
  54. goto nomem;
  55. cv->refcount = 0;
  56. cv->autoinc = 0;
  57. cv->resize_callback = NULL;
  58. cv->resize_data = NULL;
  59. cv->frame = 0;
  60. cv->framecount = 1;
  61. cv->frames = malloc(sizeof(struct cucul_frame));
  62. if(!cv->frames)
  63. {
  64. free(cv);
  65. goto nomem;
  66. }
  67. cv->frames[0].width = cv->frames[0].height = 0;
  68. cv->frames[0].chars = NULL;
  69. cv->frames[0].attrs = NULL;
  70. cv->frames[0].x = cv->frames[0].y = 0;
  71. cv->frames[0].handlex = cv->frames[0].handley = 0;
  72. cv->frames[0].curattr = 0;
  73. cv->frames[0].name = strdup("frame#00000000");
  74. _cucul_load_frame_info(cv);
  75. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  76. if(cucul_resize(cv, width, height) < 0)
  77. {
  78. int saved_errno = geterrno();
  79. free(cv->frames[0].name);
  80. free(cv->frames);
  81. free(cv);
  82. seterrno(saved_errno);
  83. return NULL;
  84. }
  85. return cv;
  86. nomem:
  87. seterrno(ENOMEM);
  88. return NULL;
  89. }
  90. /** \brief Manage a canvas.
  91. *
  92. * Lock a canvas to prevent it from being resized. If non-NULL,
  93. * the \e callback function pointer will be called upon each
  94. * \e cucul_set_canvas_size call and if the returned value is zero, the
  95. * canvas resize request will be denied.
  96. *
  97. * This function is only useful for display drivers such as the \e libcaca
  98. * library.
  99. *
  100. * If an error occurs, -1 is returned and \b errno is set accordingly:
  101. * - \c EBUSY The canvas is already being managed.
  102. *
  103. * \param cv A libcucul canvas.
  104. * \param callback An optional callback function pointer.
  105. * \param p The argument to be passed to \e callback.
  106. * \return 0 in case of success, -1 if an error occurred.
  107. */
  108. int cucul_manage_canvas(cucul_canvas_t *cv, int (*callback)(void *), void *p)
  109. {
  110. if(cv->refcount)
  111. {
  112. seterrno(EBUSY);
  113. return -1;
  114. }
  115. cv->refcount = 1;
  116. return 0;
  117. }
  118. /** \brief Unmanage a canvas.
  119. *
  120. * Unlock a canvas previously locked by cucul_manage_canvas(). For safety
  121. * reasons, the callback and callback data arguments must be the same as for
  122. * the cucul_manage_canvas() call.
  123. *
  124. * This function is only useful for display drivers such as the \e libcaca
  125. * library.
  126. *
  127. * If an error occurs, -1 is returned and \b errno is set accordingly:
  128. * - \c EINVAL The canvas is not managed, or the callback arguments do
  129. * not match.
  130. *
  131. * \param cv A libcucul canvas.
  132. * \param callback The \e callback argument previously passed to
  133. cucul_manage_canvas().
  134. * \param p The \e p argument previously passed to cucul_manage_canvas().
  135. * \return 0 in case of success, -1 if an error occurred.
  136. */
  137. int cucul_unmanage_canvas(cucul_canvas_t *cv, int (*callback)(void *), void *p)
  138. {
  139. if(!cv->refcount
  140. || cv->resize_callback != callback || cv->resize_data != p)
  141. {
  142. seterrno(EINVAL);
  143. return -1;
  144. }
  145. cv->refcount = 0;
  146. return 0;
  147. }
  148. /** \brief Resize a canvas.
  149. *
  150. * Set the canvas' width and height, in character cells.
  151. *
  152. * The contents of the canvas are preserved to the extent of the new
  153. * canvas size. Newly allocated character cells at the right and/or at
  154. * the bottom of the canvas are filled with spaces.
  155. *
  156. * If as a result of the resize the cursor coordinates fall outside the
  157. * new canvas boundaries, they are readjusted. For instance, if the
  158. * current X cursor coordinate is 11 and the requested width is 10, the
  159. * new X cursor coordinate will be 10.
  160. *
  161. * It is an error to try to resize the canvas if an output driver has
  162. * been attached to the canvas using caca_create_display(). You need to
  163. * remove the output driver using caca_free_display() before you can change
  164. * the canvas size again. However, the caca output driver can cause a
  165. * canvas resize through user interaction. See the caca_event() documentation
  166. * for more about this.
  167. *
  168. * If an error occurs, -1 is returned and \b errno is set accordingly:
  169. * - \c EBUSY The canvas is in use by a display driver and cannot be resized.
  170. * - \c ENOMEM Not enough memory for the requested canvas size. If this
  171. * happens, the canvas handle becomes invalid and should not be used.
  172. *
  173. * \param cv A libcucul canvas.
  174. * \param width The desired canvas width.
  175. * \param height The desired canvas height.
  176. * \return 0 in case of success, -1 if an error occurred.
  177. */
  178. int cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  179. unsigned int height)
  180. {
  181. if(cv->refcount && cv->resize_callback
  182. && !cv->resize_callback(cv->resize_data))
  183. {
  184. seterrno(EBUSY);
  185. return -1;
  186. }
  187. return cucul_resize(cv, width, height);
  188. }
  189. /** \brief Get the canvas width.
  190. *
  191. * Return the current canvas' width, in character cells.
  192. *
  193. * This function never fails.
  194. *
  195. * \param cv A libcucul canvas.
  196. * \return The canvas width.
  197. */
  198. unsigned int cucul_get_canvas_width(cucul_canvas_t const *cv)
  199. {
  200. return cv->width;
  201. }
  202. /** \brief Get the canvas height.
  203. *
  204. * Returns the current canvas' height, in character cells.
  205. *
  206. * This function never fails.
  207. *
  208. * \param cv A libcucul canvas.
  209. * \return The canvas height.
  210. */
  211. unsigned int cucul_get_canvas_height(cucul_canvas_t const *cv)
  212. {
  213. return cv->height;
  214. }
  215. /** \brief Get the canvas character array.
  216. *
  217. * Return the current canvas' internal character array. The array elements
  218. * consist in native endian 32-bit Unicode values as returned by
  219. * cucul_get_char().
  220. *
  221. * This function is only useful for display drivers such as the \e libcaca
  222. * library.
  223. *
  224. * This function never fails.
  225. *
  226. * \param cv A libcucul canvas.
  227. * \return The canvas character array.
  228. */
  229. unsigned char const * cucul_get_canvas_chars(cucul_canvas_t const *cv)
  230. {
  231. return (unsigned char const *)cv->chars;
  232. }
  233. /** \brief Get the canvas attribute array.
  234. *
  235. * Returns the current canvas' internal attribute array. The array elements
  236. * consist in native endian 32-bit attribute values as returned by
  237. * cucul_get_attr().
  238. *
  239. * This function is only useful for display drivers such as the \e libcaca
  240. * library.
  241. *
  242. * This function never fails.
  243. *
  244. * \param cv A libcucul canvas.
  245. * \return The canvas attribute array.
  246. */
  247. unsigned char const * cucul_get_canvas_attrs(cucul_canvas_t const *cv)
  248. {
  249. return (unsigned char const *)cv->attrs;
  250. }
  251. /** \brief Uninitialise \e libcucul.
  252. *
  253. * Free all resources allocated by cucul_create_canvas(). After
  254. * this function has been called, no other \e libcucul functions may be
  255. * used unless a new call to cucul_create_canvas() is done.
  256. *
  257. * If an error occurs, -1 is returned and \b errno is set accordingly:
  258. * - \c EBUSY The canvas is in use by a display driver and cannot be freed.
  259. *
  260. * \param cv A libcucul canvas.
  261. * \return 0 in case of success, -1 if an error occurred.
  262. */
  263. int cucul_free_canvas(cucul_canvas_t *cv)
  264. {
  265. unsigned int f;
  266. if(cv->refcount)
  267. {
  268. seterrno(EBUSY);
  269. return -1;
  270. }
  271. for(f = 0; f < cv->framecount; f++)
  272. {
  273. free(cv->frames[f].chars);
  274. free(cv->frames[f].attrs);
  275. free(cv->frames[f].name);
  276. }
  277. free(cv->frames);
  278. free(cv);
  279. return 0;
  280. }
  281. /** \brief Generate a random integer within a range.
  282. *
  283. * Generate a random integer within the given range.
  284. *
  285. * This function never fails.
  286. *
  287. * \param min The lower bound of the integer range.
  288. * \param max The upper bound of the integer range.
  289. * \return A random integer comprised between \p min and \p max - 1
  290. * (inclusive).
  291. */
  292. int cucul_rand(int min, int max)
  293. {
  294. static int need_init = 1;
  295. if(need_init)
  296. {
  297. srand(getpid() + time(NULL));
  298. need_init = 0;
  299. }
  300. return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
  301. }
  302. /*
  303. * XXX: The following functions are local.
  304. */
  305. int cucul_resize(cucul_canvas_t *cv, unsigned int width, unsigned int height)
  306. {
  307. unsigned int x, y, f, old_width, old_height, new_size, old_size;
  308. old_width = cv->width;
  309. old_height = cv->height;
  310. old_size = old_width * old_height;
  311. _cucul_save_frame_info(cv);
  312. cv->width = width;
  313. cv->height = height;
  314. new_size = width * height;
  315. /* Step 1: if new area is bigger, resize the memory area now. */
  316. if(new_size > old_size)
  317. {
  318. for(f = 0; f < cv->framecount; f++)
  319. {
  320. cv->frames[f].chars = realloc(cv->frames[f].chars,
  321. new_size * sizeof(uint32_t));
  322. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  323. new_size * sizeof(uint32_t));
  324. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  325. {
  326. seterrno(ENOMEM);
  327. return -1;
  328. }
  329. }
  330. }
  331. /* Step 2: move line data if necessary. */
  332. if(width == old_width)
  333. {
  334. /* Width did not change, which means we do not need to move data. */
  335. ;
  336. }
  337. else if(width > old_width)
  338. {
  339. /* New width is bigger than old width, which means we need to
  340. * copy lines starting from the bottom of the screen otherwise
  341. * we will overwrite information. */
  342. for(f = 0; f < cv->framecount; f++)
  343. {
  344. uint32_t *chars = cv->frames[f].chars;
  345. uint32_t *attrs = cv->frames[f].attrs;
  346. for(y = height < old_height ? height : old_height; y--; )
  347. {
  348. uint32_t attr = cv->frames[f].curattr;
  349. for(x = old_width; x--; )
  350. {
  351. chars[y * width + x] = chars[y * old_width + x];
  352. attrs[y * width + x] = attrs[y * old_width + x];
  353. }
  354. /* Zero the end of the line */
  355. for(x = width - old_width; x--; )
  356. {
  357. chars[y * width + old_width + x] = (uint32_t)' ';
  358. attrs[y * width + old_width + x] = attr;
  359. }
  360. }
  361. }
  362. }
  363. else
  364. {
  365. /* New width is smaller. Copy as many lines as possible. Ignore
  366. * the first line, it is already in place. */
  367. unsigned int lines = height < old_height ? height : old_height;
  368. for(f = 0; f < cv->framecount; f++)
  369. {
  370. uint32_t *chars = cv->frames[f].chars;
  371. uint32_t *attrs = cv->frames[f].attrs;
  372. for(y = 1; y < lines; y++)
  373. {
  374. for(x = 0; x < width; x++)
  375. {
  376. chars[y * width + x] = chars[y * old_width + x];
  377. attrs[y * width + x] = attrs[y * old_width + x];
  378. }
  379. }
  380. }
  381. }
  382. /* Step 3: fill the bottom of the new screen if necessary. */
  383. if(height > old_height)
  384. {
  385. for(f = 0; f < cv->framecount; f++)
  386. {
  387. uint32_t *chars = cv->frames[f].chars;
  388. uint32_t *attrs = cv->frames[f].attrs;
  389. uint32_t attr = cv->frames[f].curattr;
  390. /* Zero the bottom of the screen */
  391. for(x = (height - old_height) * width; x--; )
  392. {
  393. chars[old_height * width + x] = (uint32_t)' ';
  394. attrs[old_height * width + x] = attr;
  395. }
  396. }
  397. }
  398. /* Step 4: if new area is smaller, resize memory area now. */
  399. if(new_size < old_size)
  400. {
  401. for(f = 0; f < cv->framecount; f++)
  402. {
  403. cv->frames[f].chars = realloc(cv->frames[f].chars,
  404. new_size * sizeof(uint32_t));
  405. cv->frames[f].attrs = realloc(cv->frames[f].attrs,
  406. new_size * sizeof(uint32_t));
  407. if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
  408. {
  409. seterrno(ENOMEM);
  410. return -1;
  411. }
  412. }
  413. }
  414. /* Set new size */
  415. for(f = 0; f < cv->framecount; f++)
  416. {
  417. if(cv->frames[f].x > (int)width)
  418. cv->frames[f].x = width;
  419. if(cv->frames[f].y > (int)height)
  420. cv->frames[f].y = height;
  421. cv->frames[f].width = width;
  422. cv->frames[f].height = height;
  423. }
  424. /* Reset the current frame shortcuts */
  425. _cucul_load_frame_info(cv);
  426. return 0;
  427. }