refactor: Unify ArchTime classes into one method

Idea from https://github.com/input-leap/input-leap/pull/1464'
This commit is contained in:
sithlord48 2025-06-11 23:18:35 -04:00 committed by Nick Bolton
parent 36b4bb09bc
commit a5522729aa
8 changed files with 15 additions and 193 deletions

View file

@ -52,3 +52,10 @@ void Arch::sleep(double timeout)
const auto msec = static_cast<uint64_t>(timeout * 1000); const auto msec = static_cast<uint64_t>(timeout * 1000);
std::this_thread::sleep_for(std::chrono::milliseconds(msec)); std::this_thread::sleep_for(std::chrono::milliseconds(msec));
} }
double Arch::time()
{
auto sinceEpoch = std::chrono::steady_clock::now().time_since_epoch();
auto uSecSinceEpoch = std::chrono::duration_cast<std::chrono::microseconds>(sinceEpoch).count();
return uSecSinceEpoch / 1000000.0;
}

View file

@ -33,14 +33,12 @@
#include "arch/win32/ArchLogWindows.h" #include "arch/win32/ArchLogWindows.h"
#include "arch/win32/ArchMultithreadWindows.h" #include "arch/win32/ArchMultithreadWindows.h"
#include "arch/win32/ArchNetworkWinsock.h" #include "arch/win32/ArchNetworkWinsock.h"
#include "arch/win32/ArchTimeWindows.h"
#elif SYSAPI_UNIX #elif SYSAPI_UNIX
#include "arch/unix/ArchDaemonUnix.h" #include "arch/unix/ArchDaemonUnix.h"
#include "arch/unix/ArchLogUnix.h" #include "arch/unix/ArchLogUnix.h"
#include "arch/unix/ArchNetworkBSD.h" #include "arch/unix/ArchNetworkBSD.h"
#include "arch/unix/ArchTimeUnix.h"
#if HAVE_PTHREAD #if HAVE_PTHREAD
#include "arch/unix/ArchMultithreadPosix.h" #include "arch/unix/ArchMultithreadPosix.h"
@ -64,12 +62,7 @@ to each method to those implementations. Clients should use the
exactly one of these objects before attempting to call any method, exactly one of these objects before attempting to call any method,
typically at the beginning of \c main(). typically at the beginning of \c main().
*/ */
class Arch : public ARCH_DAEMON, class Arch : public ARCH_DAEMON, public ARCH_LOG, public ARCH_MULTITHREAD, public ARCH_NETWORK, public ArchString
public ARCH_LOG,
public ARCH_MULTITHREAD,
public ARCH_NETWORK,
public ArchString,
public ARCH_TIME
{ {
public: public:
Arch(); Arch();
@ -105,6 +98,13 @@ public:
*/ */
static void sleep(double timeout); static void sleep(double timeout);
/**
* @brief time
* @return Returns the number of seconds since some arbitrary starting time.
* This should return as high a precision as reasonable.
*/
static double time();
private: private:
static Arch *s_instance; static Arch *s_instance;
}; };

View file

@ -16,8 +16,6 @@ if(WIN32)
win32/ArchMultithreadWindows.h win32/ArchMultithreadWindows.h
win32/ArchNetworkWinsock.cpp win32/ArchNetworkWinsock.cpp
win32/ArchNetworkWinsock.h win32/ArchNetworkWinsock.h
win32/ArchTimeWindows.cpp
win32/ArchTimeWindows.h
win32/XArchWindows.cpp win32/XArchWindows.cpp
win32/XArchWindows.h win32/XArchWindows.h
) )
@ -32,8 +30,6 @@ elseif(UNIX)
unix/ArchMultithreadPosix.h unix/ArchMultithreadPosix.h
unix/ArchNetworkBSD.cpp unix/ArchNetworkBSD.cpp
unix/ArchNetworkBSD.h unix/ArchNetworkBSD.h
unix/ArchTimeUnix.cpp
unix/ArchTimeUnix.h
unix/XArchUnix.cpp unix/XArchUnix.cpp
unix/XArchUnix.h unix/XArchUnix.h
) )
@ -50,7 +46,6 @@ add_library(arch STATIC ${PLATFORM_CODE}
IArchNetwork.h IArchNetwork.h
ArchString.cpp ArchString.cpp
ArchString.h ArchString.h
IArchTime.h
XArch.h XArch.h
) )

View file

@ -1,31 +0,0 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#pragma once
#include "common/IInterface.h"
//! Interface for architecture dependent time operations
/*!
This interface defines the time operations required by
deskflow. Each architecture must implement this interface.
*/
class IArchTime : public IInterface
{
public:
//! @name manipulators
//@{
//! Get the current time
/*!
Returns the number of seconds since some arbitrary starting time.
This should return as high a precision as reasonable.
*/
virtual double time() = 0;
//@}
};

View file

@ -1,30 +0,0 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#include "arch/unix/ArchTimeUnix.h"
#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
//
// ArchTimeUnix
//
double ArchTimeUnix::time()
{
struct timeval t;
gettimeofday(&t, nullptr);
return (double)t.tv_sec + 1.0e-6 * (double)t.tv_usec;
}

View file

@ -1,23 +0,0 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#pragma once
#include "arch/IArchTime.h"
#define ARCH_TIME ArchTimeUnix
//! Generic Unix implementation of IArchTime
class ArchTimeUnix : public IArchTime
{
public:
ArchTimeUnix() = default;
~ArchTimeUnix() override = default;
// IArchTime overrides
double time() override;
};

View file

@ -1,73 +0,0 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#include "arch/win32/ArchTimeWindows.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#define MMNODRV // Disable: Installable driver support
#define MMNOSOUND // Disable: Sound support
#define MMNOWAVE // Disable: Waveform support
#define MMNOMIDI // Disable: MIDI support
#define MMNOAUX // Disable: Auxiliary audio support
#define MMNOMIXER // Disable: Mixer support
#define MMNOJOY // Disable: Joystick support
#define MMNOMCI // Disable: MCI support
#define MMNOMMIO // Disable: Multimedia file I/O support
#define MMNOMMSYSTEM // Disable: General MMSYSTEM functions
#include <MMSystem.h>
typedef WINMMAPI DWORD(WINAPI *PTimeGetTime)(void);
static double s_freq = 0.0;
static HINSTANCE s_mmInstance = nullptr;
static PTimeGetTime s_tgt = nullptr;
//
// ArchTimeWindows
//
ArchTimeWindows::ArchTimeWindows()
{
assert(s_freq == 0.0 || s_mmInstance == nullptr);
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");
if (s_mmInstance != nullptr) {
s_tgt = (PTimeGetTime)GetProcAddress(s_mmInstance, "timeGetTime");
}
}
}
ArchTimeWindows::~ArchTimeWindows()
{
s_freq = 0.0;
if (s_mmInstance == nullptr) {
FreeLibrary(static_cast<HMODULE>(s_mmInstance));
s_tgt = nullptr;
s_mmInstance = nullptr;
}
}
double ArchTimeWindows::time()
{
// 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 != nullptr) {
return 0.001 * static_cast<double>(s_tgt());
} else {
return 0.001 * static_cast<double>(GetTickCount());
}
}

View file

@ -1,23 +0,0 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#pragma once
#include "arch/IArchTime.h"
#define ARCH_TIME ArchTimeWindows
//! Win32 implementation of IArchTime
class ArchTimeWindows : public IArchTime
{
public:
ArchTimeWindows();
~ArchTimeWindows() override;
// IArchTime overrides
double time() override;
};