Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

223 righe
6.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; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains character and string drawing functions.
  15. */
  16. #include "config.h"
  17. #include "common.h"
  18. #if !defined(__KERNEL__)
  19. # if defined(HAVE_ERRNO_H)
  20. # include <errno.h>
  21. # endif
  22. #endif
  23. #include "caca.h"
  24. #include "caca_internals.h"
  25. #include "cucul.h"
  26. #include "cucul_internals.h"
  27. /** \brief Set the display title.
  28. *
  29. * If libcaca runs in a window, try to change its title. This works with
  30. * the OpenGL, X11 and Win32 drivers.
  31. *
  32. * If an error occurs, -1 is returned and \b errno is set accordingly:
  33. * - \c ENOSYS Display driver does not support setting the window title.
  34. *
  35. * \param dp The libcaca display context.
  36. * \param title The desired display title.
  37. * \return 0 upon success, -1 if an error occurred.
  38. */
  39. int caca_set_display_title(caca_display_t *dp, char const *title)
  40. {
  41. int ret = dp->drv.set_display_title(dp, title);
  42. #if defined(HAVE_ERRNO_H)
  43. if(ret)
  44. errno = ENOSYS;
  45. #endif
  46. return ret;
  47. }
  48. /** \brief Get the display width.
  49. *
  50. * If libcaca runs in a window, get the usable window width. This value can
  51. * be used for aspect ratio calculation. If libcaca does not run in a window
  52. * or if there is no way to know the font size, most drivers will assume a
  53. * 6x10 font is being used. Note that the units are not necessarily pixels.
  54. *
  55. * This function never fails.
  56. *
  57. * \param dp The libcaca display context.
  58. * \return The display width.
  59. */
  60. unsigned int caca_get_display_width(caca_display_t *dp)
  61. {
  62. return dp->drv.get_display_width(dp);
  63. }
  64. /** \brief Get the display height.
  65. *
  66. * If libcaca runs in a window, get the usable window height. This value can
  67. * be used for aspect ratio calculation. If libcaca does not run in a window
  68. * or if there is no way to know the font size, assume a 6x10 font is being
  69. * used. Note that the units are not necessarily pixels.
  70. *
  71. * This function never fails.
  72. *
  73. * \param dp The libcaca display context.
  74. * \return The display height.
  75. */
  76. unsigned int caca_get_display_height(caca_display_t *dp)
  77. {
  78. return dp->drv.get_display_height(dp);
  79. }
  80. /** \brief Set the refresh delay.
  81. *
  82. * This function sets the refresh delay in microseconds. The refresh delay
  83. * is used by caca_refresh_display() to achieve constant framerate. See the
  84. * caca_refresh_display() documentation for more details.
  85. *
  86. * If the argument is zero, constant framerate is disabled. This is the
  87. * default behaviour.
  88. *
  89. * This function never fails.
  90. *
  91. * \param dp The libcaca display context.
  92. * \param usec The refresh delay in microseconds.
  93. * \return This function always returns 0.
  94. */
  95. int caca_set_display_time(caca_display_t *dp, unsigned int usec)
  96. {
  97. dp->delay = usec;
  98. return 0;
  99. }
  100. /** \brief Get the display's average rendering time.
  101. *
  102. * This function returns the average rendering time, which is the average
  103. * measured time between two caca_refresh_display() calls, in microseconds. If
  104. * constant framerate was activated by calling caca_set_display_time(), the
  105. * average rendering time will not be considerably shorter than the requested
  106. * delay even if the real rendering time was shorter.
  107. *
  108. * This function never fails.
  109. *
  110. * \param dp The libcaca display context.
  111. * \return The render time in microseconds.
  112. */
  113. unsigned int caca_get_display_time(caca_display_t *dp)
  114. {
  115. return dp->rendertime;
  116. }
  117. /** \brief Flush pending changes and redraw the screen.
  118. *
  119. * This function flushes all graphical operations and prints them to the
  120. * screen. Nothing will show on the screen until caca_refresh_display() is
  121. * called.
  122. *
  123. * If caca_set_display_time() was called with a non-zero value,
  124. * caca_refresh_display() will use that value to achieve constant
  125. * framerate: if two consecutive calls to caca_refresh_display() are within
  126. * a time range shorter than the value set with caca_set_display_time(),
  127. * the second call will be delayed before performing the screen refresh.
  128. *
  129. * This function never fails.
  130. *
  131. * \param dp The libcaca display context.
  132. * \return This function always returns 0.
  133. */
  134. int caca_refresh_display(caca_display_t *dp)
  135. {
  136. #if !defined(_DOXYGEN_SKIP_ME)
  137. #define IDLE_USEC 5000
  138. #endif
  139. int ticks = dp->lastticks + _caca_getticks(&dp->timer);
  140. dp->drv.display(dp);
  141. /* Once the display is finished, we can ack resizes */
  142. if(dp->resize.resized)
  143. {
  144. dp->resize.resized = 0;
  145. _caca_handle_resize(dp);
  146. }
  147. /* Wait until dp->delay + time of last call */
  148. ticks += _caca_getticks(&dp->timer);
  149. for(ticks += _caca_getticks(&dp->timer);
  150. ticks + IDLE_USEC < (int)dp->delay;
  151. ticks += _caca_getticks(&dp->timer))
  152. {
  153. _caca_sleep(IDLE_USEC);
  154. }
  155. /* Update the sliding mean of the render time */
  156. dp->rendertime = (7 * dp->rendertime + ticks) / 8;
  157. dp->lastticks = ticks - dp->delay;
  158. /* If we drifted too much, it's bad, bad, bad. */
  159. if(dp->lastticks > (int)dp->delay)
  160. dp->lastticks = 0;
  161. return 0;
  162. }
  163. /** \brief Show or hide the mouse pointer.
  164. *
  165. * This function shows or hides the mouse pointer, for devices that
  166. * support it.
  167. *
  168. * If an error occurs, -1 is returned and \b errno is set accordingly:
  169. * - \c ENOSYS Display driver does not support hiding the mouse pointer.
  170. *
  171. * \param dp The libcaca display context.
  172. * \param flag 0 hides the pointer, 1 shows the system's default pointer
  173. * (usually an arrow). Other values are reserved for future use.
  174. * \return 0 upon success, -1 if an error occurred.
  175. */
  176. int caca_set_mouse(caca_display_t *dp, int flag)
  177. {
  178. if(!dp->drv.set_mouse)
  179. {
  180. #if defined(HAVE_ERRNO_H)
  181. errno = ENOSYS;
  182. #endif
  183. return -1;
  184. }
  185. dp->drv.set_mouse(dp, flag);
  186. return 0;
  187. }
  188. /*
  189. * XXX: following functions are local
  190. */
  191. void _caca_handle_resize(caca_display_t *dp)
  192. {
  193. dp->drv.handle_resize(dp);
  194. /* Tell libcucul we changed size */
  195. if(dp->resize.w != dp->cv->width || dp->resize.h != dp->cv->height)
  196. _cucul_set_canvas_size(dp->cv, dp->resize.w, dp->resize.h);
  197. }