]> git.mxchange.org Git - flightgear.git/blob - Time/fg_timer.c
Initial revision.
[flightgear.git] / Time / fg_timer.c
1 /**************************************************************************
2  * fg_timer.c -- time handling routines
3  *
4  * Written by Curtis Olson, started June 1997.
5  *
6  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #include <signal.h>    /* for timer routines */
28 #include <stdio.h>     /* for printf() */
29 #include <time.h>      /* for get/setitimer */
30 #include <sys/timeb.h> /* for ftime() and struct timeb */
31
32
33 #include "fg_timer.h"
34
35
36 unsigned long int fgSimTime;
37 static struct itimerval t, ot;
38 static void (*callbackfunc)();
39
40
41 /* This routine catches the SIGALRM */
42 void fgTimerCatch() {
43     /* ignore any SIGALRM's until we come back from our EOM iteration */
44     signal(SIGALRM, SIG_IGN);
45
46     /* printf("In fgTimerCatch()\n"); */
47
48     callbackfunc();
49
50     signal(SIGALRM, fgTimerCatch);
51 }
52
53
54 /* this routine initializes the interval timer to generate a SIGALRM after
55  * the specified interval (dt) */
56 void fgTimerInit(float dt, void (*f)()) {
57     int terr;
58     int isec;
59     float usec;
60
61     callbackfunc = f;
62
63     isec = (int) dt;
64     usec = 1000000* (dt - (float) isec);
65
66     t.it_interval.tv_sec = isec;
67     t.it_interval.tv_usec = usec;
68     t.it_value.tv_sec = isec;
69     t.it_value.tv_usec = usec;
70     /* printf("fgTimerInit() called\n"); */
71     fgTimerCatch();   /* set up for SIGALRM signal catch */
72     terr = setitimer( ITIMER_REAL, &t, &ot );
73     if (terr) {
74         printf("Error returned from setitimer");
75         exit(0);
76     }
77 }
78
79
80 /* This function returns the number of milleseconds since the last
81    time it was called. */
82 int fgGetTimeInterval() {
83     static struct timeb last;
84     static struct timeb current;
85     static int inited = 0;
86
87     int interval;
88
89     if ( ! inited ) {
90         inited = 1;
91         ftime(&last);
92         interval = 0;
93     } else {
94         ftime(&current);
95         interval = 1000 * (current.time - last.time) + 
96             (current.millitm - last.millitm);
97     }
98
99     last = current;
100     return(interval);
101 }
102
103
104 /* $Log$
105 /* Revision 1.1  1997/06/16 19:24:20  curt
106 /* Initial revision.
107 /*
108  */