~ubuntu-branches/ubuntu/breezy/knocker/breezy

« back to all changes in this revision

Viewing changes to src/knocker_time.c

  • Committer: Bazaar Package Importer
  • Author(s): Pablo Lorenzzoni
  • Date: 2004-02-23 13:38:39 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040223133839-8nkw6632is003mrp
Tags: 0.7.1-2
* Upgraded Standard-Version
* Add URL to the description

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* knocker version 0.7.1
 
2
 * Release date: 24 May 2002
 
3
 *
 
4
 * Project homepage: http://knocker.sourceforge.net
 
5
 *
 
6
 * Copyright 2001,2002 Gabriele Giorgetti <g.gabriele79@genie.it>
 
7
 *
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation; either version 2 of the License, or
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program; if not, write to the Free Software
 
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
22
*/
 
23
 
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
 
 
27
#include "knocker_time.h"
 
28
 
 
29
#ifdef __WIN32__
 
30
#include <windows.h>
 
31
#else
 
32
#include <sys/time.h>
 
33
#include <signal.h>
 
34
#include <unistd.h>
 
35
#endif /* __WIN32__ */
 
36
 
 
37
#ifdef __WIN32__
 
38
static unsigned long timer_start;
 
39
#else
 
40
static struct timeval timer_start;
 
41
#endif
 
42
 
 
43
 
 
44
void knocker_time_start_timer (void)
 
45
{
 
46
#ifdef __WIN32__
 
47
  timer_start = GetTickCount ();
 
48
#else
 
49
  /* Set first ticks value */
 
50
  gettimeofday (&timer_start, NULL);
 
51
#endif
 
52
}
 
53
 
 
54
double knocker_time_get_ticks (void)
 
55
{
 
56
  double ticks;
 
57
#ifdef __WIN32__
 
58
  double timer_now;
 
59
  na timer_now = GetTickCount ();
 
60
  ticks = ((timer_now - timer_start));
 
61
 
 
62
#else
 
63
  struct timeval timer_now;
 
64
 
 
65
  gettimeofday (&timer_now, NULL);
 
66
  ticks = (timer_now.tv_sec - timer_start.tv_sec) * 1000 + (timer_now.tv_usec - timer_start.tv_usec) / 1000;
 
67
#endif
 
68
  return (ticks);
 
69
}
 
70
 
 
71
 
 
72
void knocker_time_delay (unsigned long ms)
 
73
{
 
74
#ifdef __WIN32__
 
75
 
 
76
#else
 
77
 
 
78
 
 
79
/* #if defined (HAVE_PAUSE) && defined (HAVE_NANOSLEEP) */
 
80
 
 
81
/*   
 
82
# ifdef HAVE_NANOSLEEP
 
83
 --------------------- UNTESTED !!!!!!! -------------------------- 
 
84
 
 
85
        struct timespec rqtp;
 
86
        unsigned long pause;
 
87
 
 
88
        pause = pause;
 
89
 
 
90
        rqtp.tv_sec = (time_t) pause / 1000;
 
91
 
 
92
        rqtp.tv_nsec = (long) (pause % 1000) * 1000000;
 
93
 
 
94
        (void) nanosleep  (&rqtp, NULL);
 
95
*/
 
96
 
 
97
#ifdef HAVE_USLEEP
 
98
 
 
99
 
 
100
#else
 
101
  double before, now;
 
102
 
 
103
  before = knocker_time_get_ticks ();
 
104
  now = before;
 
105
 
 
106
#ifdef DEBUG
 
107
  fprintf (stderr, "debug: function knocker_time_delay() called.\n");
 
108
  fprintf (stderr, "debug: timer is now = %f (it should be 0)\n", now);
 
109
  fprintf (stderr, "debug: delaying for %f milliseconds...", ms);
 
110
#endif
 
111
 
 
112
  /* Very bad do nothing delay loop */
 
113
  do
 
114
    {
 
115
      now = knocker_time_get_ticks ();
 
116
 
 
117
      if (now > before + ms)
 
118
        break;                  /* just to make sure we don't get stuck in this loop */
 
119
      /* if the test now <= before + ms fails        */
 
120
 
 
121
    }
 
122
  while (now <= before + ms);
 
123
 
 
124
#endif
 
125
 
 
126
 
 
127
#ifdef DEBUG
 
128
  fprintf (stderr, " done.\n");
 
129
  fprintf (stderr, "debug: timer is now = %f \n", now);
 
130
#endif
 
131
 
 
132
#endif /* ifdef __WIN32__ */
 
133
}