您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

98 行
2.2 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2012 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This library is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What the Fuck You Want
  9. * to Public License, Version 2, as published by Sam Hocevar. See
  10. * http://www.wtfpl.net/ for more details.
  11. */
  12. /*
  13. * This file contains simple timer routines.
  14. */
  15. #include "config.h"
  16. #if !defined(__KERNEL__)
  17. # include <stdlib.h>
  18. # if defined(HAVE_SYS_TIME_H)
  19. # include <sys/time.h>
  20. # endif
  21. # include <time.h>
  22. # if defined(USE_WIN32)
  23. # include <windows.h>
  24. # endif
  25. # if defined(HAVE_UNISTD_H)
  26. # include <unistd.h>
  27. # endif
  28. #endif
  29. #include "caca.h"
  30. #include "caca_internals.h"
  31. void _caca_sleep(int usec)
  32. {
  33. #if defined(HAVE_SLEEP)
  34. Sleep((usec + 500) / 1000);
  35. #elif defined(HAVE_USLEEP)
  36. usleep(usec);
  37. #else
  38. /* SLEEP */
  39. #endif
  40. }
  41. int _caca_getticks(caca_timer_t *timer)
  42. {
  43. #if defined(USE_WIN32)
  44. LARGE_INTEGER tmp;
  45. static double freq = -1.0; /* FIXME: can this move to caca_context? */
  46. double seconds;
  47. #elif defined(HAVE_GETTIMEOFDAY)
  48. struct timeval tv;
  49. #endif
  50. int ticks = 0;
  51. int new_sec, new_usec;
  52. #if defined(USE_WIN32)
  53. if (freq < 0.0)
  54. {
  55. if(!QueryPerformanceFrequency(&tmp))
  56. freq = 0.0;
  57. else
  58. freq = 1.0 / (double)tmp.QuadPart;
  59. }
  60. QueryPerformanceCounter(&tmp);
  61. seconds = freq * (double)tmp.QuadPart;
  62. new_sec = (int)seconds;
  63. new_usec = (int)((seconds - new_sec) * 1000000.0);
  64. #elif defined(HAVE_GETTIMEOFDAY)
  65. gettimeofday(&tv, NULL);
  66. new_sec = tv.tv_sec;
  67. new_usec = tv.tv_usec;
  68. #endif
  69. if(timer->last_sec != 0)
  70. {
  71. /* If the delay was greater than 60 seconds, return 10 seconds
  72. * otherwise we may overflow our ticks counter. */
  73. if(new_sec >= timer->last_sec + 60)
  74. ticks = 60 * 1000000;
  75. else
  76. {
  77. ticks = (new_sec - timer->last_sec) * 1000000;
  78. ticks += new_usec;
  79. ticks -= timer->last_usec;
  80. }
  81. }
  82. timer->last_sec = new_sec;
  83. timer->last_usec = new_usec;
  84. return ticks;
  85. }