1 /* $Id$
2  *  Provides compiler-independent sleep() implementation with precisious
3  *  to milliseconds
4  *
5  *  Latest version may be foind on http://husky.sourceforge.net
6  *
7  *  Written by Scott Dudley
8  *  777 Downing St.    FidoNet     1:249/106
9  *  Kingston, Ont.     Internet    sjd@f106.n249.z1.fidonet.org
10  *  Canada  K7M 5N3    BBS         1-613-634-3058, V.32bis
11  *
12  *  Copyright 1989-1994 by SCI Communications.
13  *  Copyright 1997-2003 Husky Developers Team.
14  *
15  * HUSKYLIB: common defines, types and functions for HUSKY
16  *
17  * This is part of The HUSKY Fidonet Software project:
18  * see http://husky.sourceforge.net for details
19  *
20  *
21  * HUSKYLIB is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU Lesser General Public
23  * License as published by the Free Software Foundation; either
24  * version 2 of the License, or (at your option) any later version.
25  *
26  * HUSKYLIB is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29  * General Public License for more details.
30  *
31  * You should have received a copy of the GNU Lesser General Public
32  * License along with this library; see file COPYING. If not, write to the
33  * Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34  *
35  * See also http://www.gnu.org, license may be found here.
36  */
37 
38 /* standard headers */
39 #include <stdlib.h>
40 #include <time.h>
41 
42 #if !defined(_MSC_VER)
43 #include <sys/time.h>
44 #endif
45 
46 /* huskylib: compiler.h */
47 #include <compiler.h>
48 
49 
50 /* compiler-dependent headers */
51 #ifdef HAS_UNISTD_H
52 #  include <unistd.h>
53 #endif
54 
55 #if defined(__DOS__) || defined(__DPMI__)
56 #include <dos.h>
57 #endif
58 
59 
60 /* huskylib headers */
61 #define DLLEXPORT
62 #include <huskyext.h>
63 
64 /* huskylib headers */
65 #include <huskylib.h>
66 
67 
68 #if defined(HAS_sleep)
tdelay(int msecs)69 void _fast tdelay(int msecs)
70 {
71     mysleep(msecs);
72 }
73 #else
74 # error "'sleep' not defined!"
75 #endif
76 
77 
78 #if defined(__WIN32__) || defined(__MINGW32__)
husky_SetTimer(hs_time * timer_ctx)79 void husky_SetTimer(hs_time *timer_ctx)
80 {
81     dword now;
82     now = GetTickCount();
83     timer_ctx->sec = now / 1000;
84     timer_ctx->msec = now % 1000;
85 }
86 
husky_GetTimer(hs_time * timer_ctx)87 dword husky_GetTimer(hs_time *timer_ctx)
88 {
89     dword now;
90     dword diff;
91     now = GetTickCount();
92     diff = (((now/1000) - timer_ctx->sec) * 1000) + ((now % 1000) - timer_ctx->msec);
93     return diff;
94 }
95 
96 #elif defined (__UNIX__) || defined(__BEOS__) || defined(__DJGPP__) || defined(__CYGWIN__) || defined(__EMX__)
husky_SetTimer(hs_time * timer_ctx)97 void husky_SetTimer(hs_time *timer_ctx)
98 {
99     struct timeval now;
100     gettimeofday(&now, NULL);
101     timer_ctx->sec = now.tv_sec;
102     timer_ctx->msec = now.tv_usec / 1000;
103 }
104 
husky_GetTimer(hs_time * timer_ctx)105 dword husky_GetTimer(hs_time *timer_ctx)
106 {
107     struct timeval now;
108     dword diff;
109     gettimeofday(&now, NULL);
110     diff = ((now.tv_sec - timer_ctx->sec) * 1000) + ((now.tv_usec / 1000) - timer_ctx->msec);
111     return diff;
112 }
113 
114 #else
115 # error Unknown OS (husky_*Timer)
116 #endif
117