1 | /* |
---|
2 | * libcaca Colour ASCII-Art library |
---|
3 | * Copyright (c) 2002-2010 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://sam.zoy.org/wtfpl/COPYING for more details. |
---|
11 | */ |
---|
12 | |
---|
13 | /* |
---|
14 | * This file contains simple timer routines. |
---|
15 | */ |
---|
16 | |
---|
17 | #include "config.h" |
---|
18 | |
---|
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 | |
---|
33 | #include "caca.h" |
---|
34 | #include "caca_internals.h" |
---|
35 | |
---|
36 | void _caca_sleep(int usec) |
---|
37 | { |
---|
38 | #if defined(HAVE_USLEEP) |
---|
39 | usleep(usec); |
---|
40 | #elif defined(HAVE_SLEEP) |
---|
41 | Sleep((usec + 500) / 1000); |
---|
42 | #else |
---|
43 | /* SLEEP */ |
---|
44 | #endif |
---|
45 | } |
---|
46 | |
---|
47 | int _caca_getticks(caca_timer_t *timer) |
---|
48 | { |
---|
49 | #if defined(HAVE_GETTIMEOFDAY) |
---|
50 | struct timeval tv; |
---|
51 | #elif defined(USE_WIN32) |
---|
52 | static __int64 freq = -1; /* FIXME: can this move to caca_context? */ |
---|
53 | __int64 usec; |
---|
54 | #endif |
---|
55 | int ticks = 0; |
---|
56 | int new_sec, new_usec; |
---|
57 | |
---|
58 | #if defined(HAVE_GETTIMEOFDAY) |
---|
59 | gettimeofday(&tv, NULL); |
---|
60 | new_sec = tv.tv_sec; |
---|
61 | new_usec = tv.tv_usec; |
---|
62 | #elif defined(USE_WIN32) |
---|
63 | if(freq == -1) |
---|
64 | { |
---|
65 | if(!QueryPerformanceFrequency((LARGE_INTEGER *)&freq)) |
---|
66 | freq = 0; |
---|
67 | } |
---|
68 | |
---|
69 | QueryPerformanceCounter((LARGE_INTEGER *)&usec); |
---|
70 | new_sec = (int)(usec * 1000000 / freq / 1000000); |
---|
71 | new_usec = (int)((usec * 1000000 / freq) % 1000000); |
---|
72 | #endif |
---|
73 | |
---|
74 | if(timer->last_sec != 0) |
---|
75 | { |
---|
76 | /* If the delay was greater than 60 seconds, return 10 seconds |
---|
77 | * otherwise we may overflow our ticks counter. */ |
---|
78 | if(new_sec >= timer->last_sec + 60) |
---|
79 | ticks = 60 * 1000000; |
---|
80 | else |
---|
81 | { |
---|
82 | ticks = (new_sec - timer->last_sec) * 1000000; |
---|
83 | ticks += new_usec; |
---|
84 | ticks -= timer->last_usec; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | timer->last_sec = new_sec; |
---|
89 | timer->last_usec = new_usec; |
---|
90 | |
---|
91 | return ticks; |
---|
92 | } |
---|
93 | |
---|