1 | /* |
---|
2 | * libcaca 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 | |
---|
12 | /** \file time.c |
---|
13 | * \version \$Id: time.c 527 2006-03-06 08:32:40Z sam $ |
---|
14 | * \author Sam Hocevar <sam@zoy.org> |
---|
15 | * \brief Timer routines |
---|
16 | * |
---|
17 | * This file contains simple timer routines. |
---|
18 | */ |
---|
19 | |
---|
20 | #include "config.h" |
---|
21 | |
---|
22 | #include <stdlib.h> |
---|
23 | #if defined(HAVE_SYS_TIME_H) |
---|
24 | # include <sys/time.h> |
---|
25 | #endif |
---|
26 | #include <time.h> |
---|
27 | |
---|
28 | #if defined(USE_WIN32) |
---|
29 | # include <windows.h> |
---|
30 | #endif |
---|
31 | |
---|
32 | #if defined(HAVE_UNISTD_H) |
---|
33 | # include <unistd.h> |
---|
34 | #endif |
---|
35 | |
---|
36 | #include "caca.h" |
---|
37 | #include "caca_internals.h" |
---|
38 | |
---|
39 | void _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 | |
---|
50 | unsigned int _caca_getticks(struct caca_timer *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 | |
---|