Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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