| 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://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_SLEEP) |
|---|
| 39 | Sleep((usec + 500) / 1000); |
|---|
| 40 | #elif defined(HAVE_USLEEP) |
|---|
| 41 | usleep(usec); |
|---|
| 42 | #else |
|---|
| 43 | /* SLEEP */ |
|---|
| 44 | #endif |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | int _caca_getticks(caca_timer_t *timer) |
|---|
| 48 | { |
|---|
| 49 | #if defined(USE_WIN32) |
|---|
| 50 | LARGE_INTEGER tmp; |
|---|
| 51 | static double freq = -1.0; /* FIXME: can this move to caca_context? */ |
|---|
| 52 | double seconds; |
|---|
| 53 | #elif defined(HAVE_GETTIMEOFDAY) |
|---|
| 54 | struct timeval tv; |
|---|
| 55 | #endif |
|---|
| 56 | int ticks = 0; |
|---|
| 57 | int new_sec, new_usec; |
|---|
| 58 | |
|---|
| 59 | #if defined(USE_WIN32) |
|---|
| 60 | if (freq < 0.0) |
|---|
| 61 | { |
|---|
| 62 | if(!QueryPerformanceFrequency(&tmp)) |
|---|
| 63 | freq = 0.0; |
|---|
| 64 | else |
|---|
| 65 | freq = 1.0 / (double)tmp.QuadPart; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | QueryPerformanceCounter(&tmp); |
|---|
| 69 | seconds = freq * (double)tmp.QuadPart; |
|---|
| 70 | new_sec = (int)seconds; |
|---|
| 71 | new_usec = (int)((seconds - new_sec) * 1000000.0); |
|---|
| 72 | #elif defined(HAVE_GETTIMEOFDAY) |
|---|
| 73 | gettimeofday(&tv, NULL); |
|---|
| 74 | new_sec = tv.tv_sec; |
|---|
| 75 | new_usec = tv.tv_usec; |
|---|
| 76 | #endif |
|---|
| 77 | |
|---|
| 78 | if(timer->last_sec != 0) |
|---|
| 79 | { |
|---|
| 80 | /* If the delay was greater than 60 seconds, return 10 seconds |
|---|
| 81 | * otherwise we may overflow our ticks counter. */ |
|---|
| 82 | if(new_sec >= timer->last_sec + 60) |
|---|
| 83 | ticks = 60 * 1000000; |
|---|
| 84 | else |
|---|
| 85 | { |
|---|
| 86 | ticks = (new_sec - timer->last_sec) * 1000000; |
|---|
| 87 | ticks += new_usec; |
|---|
| 88 | ticks -= timer->last_usec; |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | timer->last_sec = new_sec; |
|---|
| 93 | timer->last_usec = new_usec; |
|---|
| 94 | |
|---|
| 95 | return ticks; |
|---|
| 96 | } |
|---|
| 97 | |
|---|