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 777 2006-04-16 18:28:47Z sam $ |
---|
7 | * |
---|
8 | * This library is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | * This file contains simple timer routines. |
---|
16 | */ |
---|
17 | |
---|
18 | #include "config.h" |
---|
19 | |
---|
20 | #if !defined(__KERNEL__) |
---|
21 | # include <stdlib.h> |
---|
22 | # if defined(HAVE_SYS_TIME_H) |
---|
23 | # include <sys/time.h> |
---|
24 | # endif |
---|
25 | # include <time.h> |
---|
26 | # if defined(USE_WIN32) |
---|
27 | # include <windows.h> |
---|
28 | # endif |
---|
29 | # if defined(HAVE_UNISTD_H) |
---|
30 | # include <unistd.h> |
---|
31 | # endif |
---|
32 | #endif |
---|
33 | |
---|
34 | #include "caca.h" |
---|
35 | #include "caca_internals.h" |
---|
36 | |
---|
37 | void _caca_sleep(unsigned int usec) |
---|
38 | { |
---|
39 | #if defined(HAVE_USLEEP) |
---|
40 | usleep(usec); |
---|
41 | #elif defined(HAVE_SLEEP) |
---|
42 | Sleep(usec / 1000); |
---|
43 | #else |
---|
44 | /* SLEEP */ |
---|
45 | #endif |
---|
46 | } |
---|
47 | |
---|
48 | unsigned int _caca_getticks(caca_timer_t *timer) |
---|
49 | { |
---|
50 | #if defined(HAVE_GETTIMEOFDAY) |
---|
51 | struct timeval tv; |
---|
52 | #elif defined(USE_WIN32) |
---|
53 | static __int64 freq = -1; /* FIXME: can this move to caca_context? */ |
---|
54 | unsigned __int64 usec; |
---|
55 | #endif |
---|
56 | unsigned int ticks = 0; |
---|
57 | int new_sec, new_usec; |
---|
58 | |
---|
59 | #if defined(HAVE_GETTIMEOFDAY) |
---|
60 | gettimeofday(&tv, NULL); |
---|
61 | new_sec = tv.tv_sec; |
---|
62 | new_usec = tv.tv_usec; |
---|
63 | #elif defined(USE_WIN32) |
---|
64 | if(freq == -1) |
---|
65 | { |
---|
66 | if(!QueryPerformanceFrequency((LARGE_INTEGER *)&freq)) |
---|
67 | freq = 0; |
---|
68 | } |
---|
69 | |
---|
70 | QueryPerformanceCounter((LARGE_INTEGER *)&usec); |
---|
71 | new_sec = (int)(usec * 1000000 / freq / 1000000); |
---|
72 | new_usec = (int)((usec * 1000000 / freq) % 1000000); |
---|
73 | #endif |
---|
74 | |
---|
75 | if(timer->last_sec != 0) |
---|
76 | { |
---|
77 | /* If the delay was greater than 60 seconds, return 10 seconds |
---|
78 | * otherwise we may overflow our ticks counter. */ |
---|
79 | if(new_sec >= timer->last_sec + 60) |
---|
80 | ticks = 60 * 1000000; |
---|
81 | else |
---|
82 | { |
---|
83 | ticks = (new_sec - timer->last_sec) * 1000000; |
---|
84 | ticks += new_usec; |
---|
85 | ticks -= timer->last_usec; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | timer->last_sec = new_sec; |
---|
90 | timer->last_usec = new_usec; |
---|
91 | |
---|
92 | return ticks; |
---|
93 | } |
---|
94 | |
---|