[331] | 1 | /* |
---|
| 2 | * libcaca ASCII-Art library |
---|
| 3 | * Copyright (c) 2002, 2003, 2004 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 GNU Lesser General Public |
---|
| 8 | * License as published by the Free Software Foundation; either |
---|
| 9 | * version 2 of the License, or (at your option) any later version. |
---|
| 10 | * |
---|
| 11 | * This library is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 14 | * Lesser General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU Lesser General Public |
---|
| 17 | * License along with this library; if not, write to the Free Software |
---|
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
| 19 | * 02111-1307 USA |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | /** \file time.c |
---|
| 23 | * \version \$Id: time.c 331 2004-01-09 09:51:53Z sam $ |
---|
| 24 | * \author Sam Hocevar <sam@zoy.org> |
---|
| 25 | * \brief Timer routines |
---|
| 26 | * |
---|
| 27 | * This file contains simple timer routines. |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #include "config.h" |
---|
| 31 | |
---|
| 32 | #include <stdlib.h> |
---|
| 33 | #include <sys/time.h> |
---|
| 34 | #include <time.h> |
---|
| 35 | |
---|
| 36 | #include "caca.h" |
---|
| 37 | #include "caca_internals.h" |
---|
| 38 | |
---|
| 39 | unsigned int _caca_getticks(struct caca_timer *timer) |
---|
| 40 | { |
---|
| 41 | struct timeval tv; |
---|
| 42 | unsigned int ticks = 0; |
---|
| 43 | |
---|
| 44 | gettimeofday(&tv, NULL); |
---|
| 45 | |
---|
| 46 | if(timer->last_sec != 0) |
---|
| 47 | { |
---|
| 48 | /* If the delay was greater than 60 seconds, return 10 seconds |
---|
| 49 | * otherwise we may overflow our ticks counter. */ |
---|
| 50 | if(tv.tv_sec >= timer->last_sec + 60) |
---|
| 51 | ticks = 60 * 1000000; |
---|
| 52 | else |
---|
| 53 | { |
---|
| 54 | ticks = (tv.tv_sec - timer->last_sec) * 1000000; |
---|
| 55 | ticks += tv.tv_usec; |
---|
| 56 | ticks -= timer->last_usec; |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | timer->last_sec = tv.tv_sec; |
---|
| 61 | timer->last_usec = tv.tv_usec; |
---|
| 62 | |
---|
| 63 | return ticks; |
---|
| 64 | } |
---|
| 65 | |
---|