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.

graphics.c 7.8 KiB

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