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.
 
 
 
 
 
 

107 lines
2.6 KiB

  1. /*
  2. * libcaca ASCII-Art library
  3. * Copyright (c) 2002, 2003, 2004 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19. * 02111-1307 USA
  20. */
  21. /** \file time.c
  22. * \version \$Id$
  23. * \author Sam Hocevar <sam@zoy.org>
  24. * \brief Timer routines
  25. *
  26. * This file contains simple timer routines.
  27. */
  28. #include "config.h"
  29. #include <stdlib.h>
  30. #if defined(HAVE_SYS_TIME_H)
  31. # include <sys/time.h>
  32. #endif
  33. #include <time.h>
  34. #if defined(USE_WIN32)
  35. # include <windows.h>
  36. #endif
  37. #if defined(HAVE_UNISTD_H)
  38. # include <unistd.h>
  39. #endif
  40. #include "caca.h"
  41. #include "caca_internals.h"
  42. void _caca_sleep(unsigned int usec)
  43. {
  44. #if defined(HAVE_USLEEP)
  45. usleep(usec);
  46. #elif defined(HAVE_SLEEP)
  47. Sleep(usec / 1000);
  48. #else
  49. SLEEP
  50. #endif
  51. }
  52. unsigned int _caca_getticks(struct caca_timer *timer)
  53. {
  54. #if defined(HAVE_GETTIMEOFDAY)
  55. struct timeval tv;
  56. #elif defined(USE_WIN32)
  57. static __int64 freq = -1;
  58. unsigned __int64 usec;
  59. #endif
  60. unsigned int ticks = 0;
  61. int new_sec, new_usec;
  62. #if defined(HAVE_GETTIMEOFDAY)
  63. gettimeofday(&tv, NULL);
  64. new_sec = tv.tv_sec;
  65. new_usec = tv.tv_usec;
  66. #elif defined(USE_WIN32)
  67. if(freq == -1)
  68. {
  69. if(!QueryPerformanceFrequency((LARGE_INTEGER *)&freq))
  70. freq = 0;
  71. }
  72. QueryPerformanceCounter((LARGE_INTEGER *)&usec);
  73. new_sec = (int)(usec * 1000000 / freq / 1000000);
  74. new_usec = (int)((usec * 1000000 / freq) % 1000000);
  75. #endif
  76. if(timer->last_sec != 0)
  77. {
  78. /* If the delay was greater than 60 seconds, return 10 seconds
  79. * otherwise we may overflow our ticks counter. */
  80. if(new_sec >= timer->last_sec + 60)
  81. ticks = 60 * 1000000;
  82. else
  83. {
  84. ticks = (new_sec - timer->last_sec) * 1000000;
  85. ticks += new_usec;
  86. ticks -= timer->last_usec;
  87. }
  88. }
  89. timer->last_sec = new_sec;
  90. timer->last_usec = new_usec;
  91. return ticks;
  92. }