MCP: gui changes; more debugging for button bnding lookup
[ardour.git] / libs / surfaces / mackie / timer.h
1 /*
2   Copyright (C) 1998, 1999, 2000, 2007 John Anderson
3   
4   This program is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Library General Public License
6   as published by the Free Software Foundation; either version 2
7   of the License, or (at your option) any later version.
8   
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13   
14   You should have received a copy of the GNU Library General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18
19 #ifndef timer_h
20 #define timer_h
21
22 #ifdef _WIN32
23 #include "windows.h"
24 #else
25 #include <sys/time.h>
26 #endif
27
28 namespace Mackie
29 {
30
31 /**
32         millisecond timer class.
33 */
34 class Timer
35 {
36 public:
37         
38         /**
39                 start the timer running if true, or just create the
40                 object if false.
41         */
42         Timer( bool shouldStart = true )
43         {
44                 if ( shouldStart )
45                         start();
46         }
47         
48         /**
49                 Start the timer running. Return the current timestamp, in milliseconds
50         */
51         unsigned long start()
52         {
53 #ifdef _WIN32
54                 _start = (unsigned long)::GetTickCount();
55 #else
56                 gettimeofday ( &_start, 0 );
57 #endif
58                 running = true;
59 #ifdef _WIN32
60                 return _start;
61 #else
62                 return ( _start.tv_sec * 1000000 + _start.tv_usec ) / 1000;
63 #endif
64         }
65
66         /**
67                 returns the number of milliseconds since start
68                 also stops the timer running
69         */
70         unsigned long stop()
71         {
72 #ifdef _WIN32
73                 _stop = (unsigned long)::GetTickCount();
74 #else
75                 gettimeofday ( &_stop, 0 );
76 #endif
77                 running = false;
78                 return elapsed();
79         }
80
81         /**
82                 returns the number of milliseconds since start
83         */
84         unsigned long elapsed() const
85         {
86                 if ( running )
87                 {
88 #ifdef _WIN32
89                         DWORD current = ::GetTickCount();
90                         return current - _start;
91 #else
92                         struct timeval current;
93                         gettimeofday ( &current, 0 );
94                         return (
95                                 ( current.tv_sec * 1000000 + current.tv_usec ) - ( _start.tv_sec * 1000000 + _start.tv_usec )
96                         ) / 1000
97                         ;
98 #endif
99                 }
100                 else
101                 {
102 #ifdef _WIN32
103                         return _stop - _start;
104 #else
105                         return (
106                                 ( _stop.tv_sec * 1000000 + _stop.tv_usec ) - ( _start.tv_sec * 1000000 + _start.tv_usec )
107                         ) / 1000
108                         ;
109 #endif
110                 }
111         }
112         
113         /**
114                 Call stop and then start. Return the value from stop.
115         */
116         unsigned long restart()
117         {
118                 unsigned long retval = stop();
119                 start();
120                 return retval;
121         }
122
123 private:
124 #ifdef _WIN32
125         unsigned long _start;
126         unsigned long _stop;
127 #else
128         struct timeval _start;
129         struct timeval _stop;
130 #endif
131         bool running;
132 };
133
134 }
135
136 #endif