Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

time.c 2.2 KiB

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