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.
 
 
 
 
 
 

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