Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 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 Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file time.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief Timer routines
  15. *
  16. * This file contains simple timer routines.
  17. */
  18. #include "config.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(struct caca_timer *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. }