2002-08-02 23:57:46 +04:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
#include "CStopwatch.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// CStopwatch
|
|
|
|
//
|
|
|
|
|
|
|
|
CStopwatch::CStopwatch(bool triggered) :
|
2002-06-10 20:49:46 +04:00
|
|
|
m_mark(0.0),
|
|
|
|
m_triggered(triggered),
|
|
|
|
m_stopped(triggered)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-06-10 20:49:46 +04:00
|
|
|
if (!triggered) {
|
2001-10-06 18:13:28 +04:00
|
|
|
m_mark = getClock();
|
2002-06-10 20:49:46 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CStopwatch::~CStopwatch()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
double
|
|
|
|
CStopwatch::reset()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
if (m_stopped) {
|
|
|
|
const double dt = m_mark;
|
|
|
|
m_mark = 0.0;
|
|
|
|
return dt;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const double t = getClock();
|
|
|
|
const double dt = t - m_mark;
|
|
|
|
m_mark = t;
|
|
|
|
return dt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
void
|
|
|
|
CStopwatch::stop()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-06-10 20:49:46 +04:00
|
|
|
if (m_stopped) {
|
2001-10-06 18:13:28 +04:00
|
|
|
return;
|
2002-06-10 20:49:46 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// save the elapsed time
|
|
|
|
m_mark = getClock() - m_mark;
|
|
|
|
m_stopped = true;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
void
|
|
|
|
CStopwatch::start()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
m_triggered = false;
|
2002-06-10 20:49:46 +04:00
|
|
|
if (!m_stopped) {
|
2001-10-06 18:13:28 +04:00
|
|
|
return;
|
2002-06-10 20:49:46 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// set the mark such that it reports the time elapsed at stop()
|
|
|
|
m_mark = getClock() - m_mark;
|
|
|
|
m_stopped = false;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
void
|
|
|
|
CStopwatch::setTrigger()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
stop();
|
|
|
|
m_triggered = true;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
double
|
|
|
|
CStopwatch::getTime()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
if (m_triggered) {
|
|
|
|
const double dt = m_mark;
|
|
|
|
start();
|
|
|
|
return dt;
|
|
|
|
}
|
2002-06-10 20:49:46 +04:00
|
|
|
else if (m_stopped) {
|
2001-10-06 18:13:28 +04:00
|
|
|
return m_mark;
|
2002-06-10 20:49:46 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return getClock() - m_mark;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CStopwatch::operator double()
|
|
|
|
{
|
|
|
|
return getTime();
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
bool
|
|
|
|
CStopwatch::isStopped() const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
return m_stopped;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
double
|
|
|
|
CStopwatch::getTime() const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-06-10 20:49:46 +04:00
|
|
|
if (m_stopped) {
|
2001-10-06 18:13:28 +04:00
|
|
|
return m_mark;
|
2002-06-10 20:49:46 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return getClock() - m_mark;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CStopwatch::operator double() const
|
|
|
|
{
|
|
|
|
return getTime();
|
|
|
|
}
|
|
|
|
|
2002-06-19 15:23:49 +04:00
|
|
|
#if WINDOWS_LIKE
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// avoid getting a lot a crap from mmsystem.h that we don't need
|
|
|
|
#define MMNODRV // Installable driver support
|
|
|
|
#define MMNOSOUND // Sound support
|
|
|
|
#define MMNOWAVE // Waveform support
|
|
|
|
#define MMNOMIDI // MIDI support
|
|
|
|
#define MMNOAUX // Auxiliary audio support
|
|
|
|
#define MMNOMIXER // Mixer support
|
|
|
|
#define MMNOJOY // Joystick support
|
|
|
|
#define MMNOMCI // MCI support
|
|
|
|
#define MMNOMMIO // Multimedia file I/O support
|
|
|
|
#define MMNOMMSYSTEM // General MMSYSTEM functions
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2001-10-06 18:13:28 +04:00
|
|
|
#include <windows.h>
|
|
|
|
#include <mmsystem.h>
|
|
|
|
|
|
|
|
typedef WINMMAPI DWORD (WINAPI *PTimeGetTime)(void);
|
|
|
|
|
|
|
|
static double s_freq = 0.0;
|
|
|
|
static HINSTANCE s_mmInstance = NULL;
|
|
|
|
static PTimeGetTime s_tgt = NULL;
|
|
|
|
|
|
|
|
//
|
|
|
|
// initialize local variables
|
|
|
|
//
|
|
|
|
|
|
|
|
class CStopwatchInit {
|
2002-04-29 18:40:01 +04:00
|
|
|
public:
|
2001-10-06 18:13:28 +04:00
|
|
|
CStopwatchInit();
|
|
|
|
~CStopwatchInit();
|
|
|
|
};
|
|
|
|
static CStopwatchInit s_init;
|
|
|
|
|
|
|
|
CStopwatchInit::CStopwatchInit()
|
|
|
|
{
|
|
|
|
LARGE_INTEGER freq;
|
|
|
|
if (QueryPerformanceFrequency(&freq) && freq.QuadPart != 0) {
|
|
|
|
s_freq = 1.0 / static_cast<double>(freq.QuadPart);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// load winmm.dll and get timeGetTime
|
|
|
|
s_mmInstance = LoadLibrary("winmm");
|
2002-06-10 20:49:46 +04:00
|
|
|
if (s_mmInstance) {
|
2001-10-06 18:13:28 +04:00
|
|
|
s_tgt = (PTimeGetTime)GetProcAddress(s_mmInstance, "timeGetTime");
|
2002-06-10 20:49:46 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CStopwatchInit::~CStopwatchInit()
|
|
|
|
{
|
2002-06-10 20:49:46 +04:00
|
|
|
if (s_mmInstance) {
|
2001-10-06 18:13:28 +04:00
|
|
|
FreeLibrary(reinterpret_cast<HMODULE>(s_mmInstance));
|
2002-06-10 20:49:46 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
double
|
|
|
|
CStopwatch::getClock() const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
// get time. we try three ways, in order of descending precision
|
|
|
|
if (s_freq != 0.0) {
|
|
|
|
LARGE_INTEGER c;
|
|
|
|
QueryPerformanceCounter(&c);
|
|
|
|
return s_freq * static_cast<double>(c.QuadPart);
|
|
|
|
}
|
|
|
|
else if (s_tgt) {
|
|
|
|
return 0.001 * static_cast<double>(s_tgt());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0.001 * static_cast<double>(GetTickCount());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-19 15:23:49 +04:00
|
|
|
#elif UNIX_LIKE
|
|
|
|
|
|
|
|
#if TIME_WITH_SYS_TIME
|
|
|
|
# include <sys/time.h>
|
|
|
|
# include <time.h>
|
|
|
|
#else
|
|
|
|
# if HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
# else
|
|
|
|
# include <time.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
double
|
|
|
|
CStopwatch::getClock() const
|
|
|
|
{
|
|
|
|
struct timeval t;
|
|
|
|
gettimeofday(&t, NULL);
|
|
|
|
return (double)t.tv_sec + 1.0e-6 * (double)t.tv_usec;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // UNIX_LIKE
|