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.
 
 
 
 
 
 

262 lines
7.3 KiB

  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 character and string drawing functions.
  16. */
  17. #include "config.h"
  18. #if !defined(__KERNEL__)
  19. # include <stdio.h>
  20. # include <stdlib.h>
  21. # include <string.h>
  22. #endif
  23. #include "cucul.h"
  24. #include "caca.h"
  25. #include "caca_internals.h"
  26. /** \brief Set the display title.
  27. *
  28. * If libcaca runs in a window, try to change its title. This works with
  29. * the ncurses, S-Lang, OpenGL, X11 and Win32 drivers.
  30. *
  31. * If an error occurs, -1 is returned and \b errno is set accordingly:
  32. * - \c ENOSYS Display driver does not support setting the window title.
  33. *
  34. * \param dp The libcaca display context.
  35. * \param title The desired display title.
  36. * \return 0 upon success, -1 if an error occurred.
  37. */
  38. int caca_set_display_title(caca_display_t *dp, char const *title)
  39. {
  40. int ret = dp->drv.set_display_title(dp, title);
  41. if(ret)
  42. seterrno(ENOSYS);
  43. return ret;
  44. }
  45. /** \brief Get the display width.
  46. *
  47. * If libcaca runs in a window, get the usable window width. This value can
  48. * be used for aspect ratio calculation. If libcaca does not run in a window
  49. * or if there is no way to know the font size, most drivers will assume a
  50. * 6x10 font is being used. Note that the units are not necessarily pixels.
  51. *
  52. * This function never fails.
  53. *
  54. * \param dp The libcaca display context.
  55. * \return The display width.
  56. */
  57. unsigned int caca_get_display_width(caca_display_t const *dp)
  58. {
  59. return dp->drv.get_display_width(dp);
  60. }
  61. /** \brief Get the display height.
  62. *
  63. * If libcaca runs in a window, get the usable window height. This value can
  64. * be used for aspect ratio calculation. If libcaca does not run in a window
  65. * or if there is no way to know the font size, assume a 6x10 font is being
  66. * used. Note that the units are not necessarily pixels.
  67. *
  68. * This function never fails.
  69. *
  70. * \param dp The libcaca display context.
  71. * \return The display height.
  72. */
  73. unsigned int caca_get_display_height(caca_display_t const *dp)
  74. {
  75. return dp->drv.get_display_height(dp);
  76. }
  77. /** \brief Set the refresh delay.
  78. *
  79. * Set the refresh delay in microseconds. The refresh delay is used by
  80. * caca_refresh_display() to achieve constant framerate. See the
  81. * caca_refresh_display() documentation for more details.
  82. *
  83. * If the argument is zero, constant framerate is disabled. This is the
  84. * default behaviour.
  85. *
  86. * This function never fails.
  87. *
  88. * \param dp The libcaca display context.
  89. * \param usec The refresh delay in microseconds.
  90. * \return This function always returns 0.
  91. */
  92. int caca_set_display_time(caca_display_t *dp, unsigned int usec)
  93. {
  94. dp->delay = usec;
  95. return 0;
  96. }
  97. /** \brief Get the display's average rendering time.
  98. *
  99. * Get the average rendering time, which is the average measured time
  100. * between two caca_refresh_display() calls, in microseconds. If constant
  101. * framerate was activated by calling caca_set_display_time(), the average
  102. * rendering time will be close to the requested delay even if the real
  103. * rendering time was shorter.
  104. *
  105. * This function never fails.
  106. *
  107. * \param dp The libcaca display context.
  108. * \return The render time in microseconds.
  109. */
  110. unsigned int caca_get_display_time(caca_display_t const *dp)
  111. {
  112. return dp->rendertime;
  113. }
  114. /** \brief Flush pending changes and redraw the screen.
  115. *
  116. * Flush all graphical operations and print them to the display device.
  117. * Nothing will show on the screen until this function is called.
  118. *
  119. * If caca_set_display_time() was called with a non-zero value,
  120. * caca_refresh_display() will use that value to achieve constant
  121. * framerate: if two consecutive calls to caca_refresh_display() are within
  122. * a time range shorter than the value set with caca_set_display_time(),
  123. * the second call will be delayed before performing the screen refresh.
  124. *
  125. * This function never fails.
  126. *
  127. * \param dp The libcaca display context.
  128. * \return This function always returns 0.
  129. */
  130. int caca_refresh_display(caca_display_t *dp)
  131. {
  132. #if !defined(_DOXYGEN_SKIP_ME)
  133. #define IDLE_USEC 5000
  134. #endif
  135. int ticks = dp->lastticks + _caca_getticks(&dp->timer);
  136. dp->drv.display(dp);
  137. /* Once the display is finished, we can ack resizes */
  138. if(dp->resize.resized)
  139. {
  140. dp->resize.resized = 0;
  141. _caca_handle_resize(dp);
  142. }
  143. /* Wait until dp->delay + time of last call */
  144. ticks += _caca_getticks(&dp->timer);
  145. for(ticks += _caca_getticks(&dp->timer);
  146. ticks + IDLE_USEC < (int)dp->delay;
  147. ticks += _caca_getticks(&dp->timer))
  148. {
  149. _caca_sleep(IDLE_USEC);
  150. }
  151. /* Update the sliding mean of the render time */
  152. dp->rendertime = (7 * dp->rendertime + ticks) / 8;
  153. dp->lastticks = ticks - dp->delay;
  154. /* If we drifted too much, it's bad, bad, bad. */
  155. if(dp->lastticks > (int)dp->delay)
  156. dp->lastticks = 0;
  157. return 0;
  158. }
  159. /** \brief Show or hide the cursor.
  160. *
  161. * Show or hide the cursor, for devices that support such a feature.
  162. *
  163. * If an error occurs, -1 is returned and \b errno is set accordingly:
  164. * - \c ENOSYS Display driver does not support showing the cursor.
  165. *
  166. * \param dp The libcaca display context.
  167. * \param flag 0 hides the cursor, 1 shows the system's default cursor
  168. * (usually a white rectangle). Other values are reserved for
  169. * future use.
  170. * \return 0 upon success, -1 if an error occurred.
  171. */
  172. int caca_set_cursor(caca_display_t *dp, int flag)
  173. {
  174. if(!dp->drv.set_cursor)
  175. {
  176. seterrno(ENOSYS);
  177. return -1;
  178. }
  179. dp->drv.set_cursor(dp, flag);
  180. return 0;
  181. }
  182. /** \brief Show or hide the mouse pointer.
  183. *
  184. * Show or hide the mouse pointer. This function works with the ncurses,
  185. * S-Lang and X11 drivers.
  186. *
  187. * If an error occurs, -1 is returned and \b errno is set accordingly:
  188. * - \c ENOSYS Display driver does not support hiding the mouse pointer.
  189. *
  190. * \param dp The libcaca display context.
  191. * \param flag 0 hides the pointer, 1 shows the system's default pointer
  192. * (usually an arrow). Other values are reserved for future use.
  193. * \return 0 upon success, -1 if an error occurred.
  194. */
  195. int caca_set_mouse(caca_display_t *dp, int flag)
  196. {
  197. if(!dp->drv.set_mouse)
  198. {
  199. seterrno(ENOSYS);
  200. return -1;
  201. }
  202. dp->drv.set_mouse(dp, flag);
  203. return 0;
  204. }
  205. /*
  206. * XXX: following functions are local
  207. */
  208. void _caca_handle_resize(caca_display_t *dp)
  209. {
  210. dp->drv.handle_resize(dp);
  211. /* Tell libcucul we changed size */
  212. if(dp->resize.w != cucul_get_canvas_width(dp->cv)
  213. || dp->resize.h != cucul_get_canvas_height(dp->cv))
  214. {
  215. dp->resize.allow = 1;
  216. cucul_set_canvas_size(dp->cv, dp->resize.w, dp->resize.h);
  217. dp->resize.allow = 0;
  218. }
  219. }
  220. void _caca_set_term_title(char const *str)
  221. {
  222. #if defined(HAVE_GETENV)
  223. char *term;
  224. term = getenv("TERM");
  225. if(!term || !strcmp(term, "linux"))
  226. return;
  227. #endif
  228. fprintf(stdout, "\x1b]0;%s\x07", str);
  229. fflush(stdout);
  230. }