source: libcaca/trunk/caca/time.c @ 1462

Last change on this file since 1462 was 1462, checked in by Sam Hocevar, 13 years ago
  • Bwarf, typo in the no warranty clause.
  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: time.c 1462 2006-12-12 01:53:54Z sam $
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/*
16 *  This file contains simple timer routines.
17 */
18
19#include "config.h"
20#include "common.h"
21
22#if !defined(__KERNEL__)
23#   include <stdlib.h>
24#   if defined(HAVE_SYS_TIME_H)
25#       include <sys/time.h>
26#   endif
27#   include <time.h>
28#   if defined(USE_WIN32)
29#       include <windows.h>
30#   endif
31#   if defined(HAVE_UNISTD_H)
32#       include <unistd.h>
33#   endif
34#endif
35
36#include "caca.h"
37#include "caca_internals.h"
38
39void _caca_sleep(unsigned int usec)
40{
41#if defined(HAVE_USLEEP)
42        usleep(usec);
43#elif defined(HAVE_SLEEP)
44        Sleep(usec / 1000);
45#else
46        /* SLEEP */
47#endif
48}
49
50unsigned int _caca_getticks(caca_timer_t *timer)
51{
52#if defined(HAVE_GETTIMEOFDAY)
53    struct timeval tv;
54#elif defined(USE_WIN32)
55    static __int64 freq = -1; /* FIXME: can this move to caca_context? */
56    unsigned __int64 usec;
57#endif
58    unsigned int ticks = 0;
59    int new_sec, new_usec;
60
61#if defined(HAVE_GETTIMEOFDAY)
62    gettimeofday(&tv, NULL);
63    new_sec = tv.tv_sec;
64    new_usec = tv.tv_usec;
65#elif defined(USE_WIN32)
66    if(freq == -1)
67    {
68        if(!QueryPerformanceFrequency((LARGE_INTEGER *)&freq))
69            freq = 0;
70    }
71
72    QueryPerformanceCounter((LARGE_INTEGER *)&usec);
73    new_sec = (int)(usec * 1000000 / freq / 1000000);
74    new_usec = (int)((usec * 1000000 / freq) % 1000000);
75#endif
76
77    if(timer->last_sec != 0)
78    {
79        /* If the delay was greater than 60 seconds, return 10 seconds
80         * otherwise we may overflow our ticks counter. */
81        if(new_sec >= timer->last_sec + 60)
82            ticks = 60 * 1000000;
83        else
84        {
85            ticks = (new_sec - timer->last_sec) * 1000000;
86            ticks += new_usec;
87            ticks -= timer->last_usec;
88        }
89    }
90
91    timer->last_sec = new_sec;
92    timer->last_usec = new_usec;
93
94    return ticks;
95}
96
Note: See TracBrowser for help on using the repository browser.