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.
 
 
 
 
 
 

97 lines
2.2 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 simple timer routines.
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #if !defined(__KERNEL__)
  20. # include <stdlib.h>
  21. # if defined(HAVE_SYS_TIME_H)
  22. # include <sys/time.h>
  23. # endif
  24. # include <time.h>
  25. # if defined(USE_WIN32)
  26. # include <windows.h>
  27. # endif
  28. # if defined(HAVE_UNISTD_H)
  29. # include <unistd.h>
  30. # endif
  31. #endif
  32. #include "caca.h"
  33. #include "caca_internals.h"
  34. void _caca_sleep(unsigned int usec)
  35. {
  36. #if defined(HAVE_USLEEP)
  37. usleep(usec);
  38. #elif defined(HAVE_SLEEP)
  39. Sleep(usec / 1000);
  40. #else
  41. /* SLEEP */
  42. #endif
  43. }
  44. unsigned int _caca_getticks(caca_timer_t *timer)
  45. {
  46. #if defined(HAVE_GETTIMEOFDAY)
  47. struct timeval tv;
  48. #elif defined(USE_WIN32)
  49. static __int64 freq = -1; /* FIXME: can this move to caca_context? */
  50. unsigned __int64 usec;
  51. #endif
  52. unsigned int ticks = 0;
  53. int new_sec, new_usec;
  54. #if defined(HAVE_GETTIMEOFDAY)
  55. gettimeofday(&tv, NULL);
  56. new_sec = tv.tv_sec;
  57. new_usec = tv.tv_usec;
  58. #elif defined(USE_WIN32)
  59. if(freq == -1)
  60. {
  61. if(!QueryPerformanceFrequency((LARGE_INTEGER *)&freq))
  62. freq = 0;
  63. }
  64. QueryPerformanceCounter((LARGE_INTEGER *)&usec);
  65. new_sec = (int)(usec * 1000000 / freq / 1000000);
  66. new_usec = (int)((usec * 1000000 / freq) % 1000000);
  67. #endif
  68. if(timer->last_sec != 0)
  69. {
  70. /* If the delay was greater than 60 seconds, return 10 seconds
  71. * otherwise we may overflow our ticks counter. */
  72. if(new_sec >= timer->last_sec + 60)
  73. ticks = 60 * 1000000;
  74. else
  75. {
  76. ticks = (new_sec - timer->last_sec) * 1000000;
  77. ticks += new_usec;
  78. ticks -= timer->last_usec;
  79. }
  80. }
  81. timer->last_sec = new_sec;
  82. timer->last_usec = new_usec;
  83. return ticks;
  84. }