diff --git a/cmake/Libraries.cmake b/cmake/Libraries.cmake index b229e340b..a4fc40417 100644 --- a/cmake/Libraries.cmake +++ b/cmake/Libraries.cmake @@ -82,12 +82,10 @@ macro(configure_unix_libs) include(CheckSymbolExists) include(CheckCSourceCompiles) - check_include_files(sys/select.h HAVE_SYS_SELECT_H) check_include_files(sys/socket.h HAVE_SYS_SOCKET_H) check_include_files(sys/time.h HAVE_SYS_TIME_H) check_include_files(unistd.h HAVE_UNISTD_H) - check_function_exists(nanosleep HAVE_NANOSLEEP) check_function_exists(sigwait HAVE_POSIX_SIGWAIT) check_function_exists(inet_aton HAVE_INET_ATON) @@ -157,9 +155,6 @@ macro(configure_unix_libs) # For config.h, set some static values; it may be a good idea to make these # values dynamic for non-standard UNIX compilers. set(HAVE_PTHREAD_SIGNAL 1) - set(SELECT_TYPE_ARG1 int) - set(SELECT_TYPE_ARG234 " (fd_set *)") - set(SELECT_TYPE_ARG5 " (struct timeval *)") set(TIME_WITH_SYS_TIME 1) set(HAVE_SOCKLEN_T 1) diff --git a/src/lib/Config.h.in b/src/lib/Config.h.in index 05bef1971..286d69e83 100644 --- a/src/lib/Config.h.in +++ b/src/lib/Config.h.in @@ -13,9 +13,6 @@ /* Define if you have the `inet_aton` function. */ #cmakedefine HAVE_INET_ATON @HAVE_INET_ATON@ -/* Define if you have the `nanosleep` function. */ -#cmakedefine HAVE_NANOSLEEP @HAVE_NANOSLEEP@ - /* Define if you have a POSIX `sigwait` function. */ #cmakedefine HAVE_POSIX_SIGWAIT @HAVE_POSIX_SIGWAIT@ @@ -28,9 +25,6 @@ /* Define if your compiler defines socklen_t. */ #cmakedefine HAVE_SOCKLEN_T @HAVE_SOCKLEN_T@ -/* Define to 1 if you have the header file. */ -#cmakedefine HAVE_SYS_SELECT_H @HAVE_SYS_SELECT_H@ - /* Define to 1 if you have the header file. */ #cmakedefine HAVE_SYS_SOCKET_H @HAVE_SYS_SOCKET_H@ @@ -61,15 +55,6 @@ /* Define this if the XKB extension is available. */ #cmakedefine HAVE_XKB_EXTENSION @HAVE_XKB_EXTENSION@ -/* Define to the type of arg 1 for `select`. */ -#cmakedefine SELECT_TYPE_ARG1 @SELECT_TYPE_ARG1@ - -/* Define to the type of args 2, 3 and 4 for `select`. */ -#cmakedefine SELECT_TYPE_ARG234 @SELECT_TYPE_ARG234@ - -/* Define to the type of arg 5 for `select`. */ -#cmakedefine SELECT_TYPE_ARG5 @SELECT_TYPE_ARG5@ - /* Define to 1 if you can safely include both and . */ #cmakedefine TIME_WITH_SYS_TIME @TIME_WITH_SYS_TIME@ diff --git a/src/lib/arch/Arch.cpp b/src/lib/arch/Arch.cpp index 2e648dbe8..976858309 100644 --- a/src/lib/arch/Arch.cpp +++ b/src/lib/arch/Arch.cpp @@ -7,6 +7,8 @@ #include "arch/Arch.h" +#include + #if SYSAPI_WIN32 #include "arch/win32/ArchMiscWindows.h" #endif @@ -41,3 +43,12 @@ Arch *Arch::getInstance() assert(s_instance != nullptr); return s_instance; } + +void Arch::sleep(double timeout) +{ + ARCH->testCancelThread(); + if (timeout < 0.0) + return; + const auto msec = static_cast(timeout * 1000); + std::this_thread::sleep_for(std::chrono::milliseconds(msec)); +} diff --git a/src/lib/arch/Arch.h b/src/lib/arch/Arch.h index 60990a93d..d7d174801 100644 --- a/src/lib/arch/Arch.h +++ b/src/lib/arch/Arch.h @@ -33,7 +33,6 @@ #include "arch/win32/ArchLogWindows.h" #include "arch/win32/ArchMultithreadWindows.h" #include "arch/win32/ArchNetworkWinsock.h" -#include "arch/win32/ArchSleepWindows.h" #include "arch/win32/ArchTimeWindows.h" #elif SYSAPI_UNIX @@ -41,7 +40,6 @@ #include "arch/unix/ArchDaemonUnix.h" #include "arch/unix/ArchLogUnix.h" #include "arch/unix/ArchNetworkBSD.h" -#include "arch/unix/ArchSleepUnix.h" #include "arch/unix/ArchTimeUnix.h" #if HAVE_PTHREAD @@ -70,7 +68,6 @@ class Arch : public ARCH_DAEMON, public ARCH_LOG, public ARCH_MULTITHREAD, public ARCH_NETWORK, - public ARCH_SLEEP, public ArchString, public ARCH_TIME { @@ -102,6 +99,12 @@ public: s_instance = s; } + /** + * @brief blocks calling thread for timout seconds + * @param timeout - blocking time in seconds. if < 0 not blocked if == 0 then caller yields the CPU + */ + static void sleep(double timeout); + private: static Arch *s_instance; }; diff --git a/src/lib/arch/CMakeLists.txt b/src/lib/arch/CMakeLists.txt index b7a441d52..9bcf7ac3e 100644 --- a/src/lib/arch/CMakeLists.txt +++ b/src/lib/arch/CMakeLists.txt @@ -16,8 +16,6 @@ if(WIN32) win32/ArchMultithreadWindows.h win32/ArchNetworkWinsock.cpp win32/ArchNetworkWinsock.h - win32/ArchSleepWindows.cpp - win32/ArchSleepWindows.h win32/ArchTimeWindows.cpp win32/ArchTimeWindows.h win32/XArchWindows.cpp @@ -34,8 +32,6 @@ elseif(UNIX) unix/ArchMultithreadPosix.h unix/ArchNetworkBSD.cpp unix/ArchNetworkBSD.h - unix/ArchSleepUnix.cpp - unix/ArchSleepUnix.h unix/ArchTimeUnix.cpp unix/ArchTimeUnix.h unix/XArchUnix.cpp @@ -52,7 +48,6 @@ add_library(arch STATIC ${PLATFORM_CODE} IArchLog.h IArchMultithread.h IArchNetwork.h - IArchSleep.h ArchString.cpp ArchString.h IArchTime.h diff --git a/src/lib/arch/IArchSleep.h b/src/lib/arch/IArchSleep.h deleted file mode 100644 index 44aaeb9d1..000000000 --- a/src/lib/arch/IArchSleep.h +++ /dev/null @@ -1,34 +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 sleeping -/*! -This interface defines the sleep operations required by -deskflow. Each architecture must implement this interface. -*/ -class IArchSleep : public IInterface -{ -public: - //! @name manipulators - //@{ - - //! Sleep - /*! - Blocks the calling thread for \c timeout seconds. If - \c timeout < 0.0 then the call returns immediately. If \c timeout - == 0.0 then the calling thread yields the CPU. - - (cancellation point) - */ - virtual void sleep(double timeout) = 0; - - //@} -}; diff --git a/src/lib/arch/unix/ArchSleepUnix.cpp b/src/lib/arch/unix/ArchSleepUnix.cpp deleted file mode 100644 index 54328ae69..000000000 --- a/src/lib/arch/unix/ArchSleepUnix.cpp +++ /dev/null @@ -1,70 +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/ArchSleepUnix.h" - -#include "arch/Arch.h" - -#if TIME_WITH_SYS_TIME -#include -#include -#else -#if HAVE_SYS_TIME_H -#include -#else -#include -#endif -#endif -#if !HAVE_NANOSLEEP -#if HAVE_SYS_SELECT_H -#include -#endif -#if HAVE_SYS_TYPES_H -#include -#endif -#if HAVE_UNISTD_H -#include -#endif -#endif - -// -// ArchSleepUnix -// - -void ArchSleepUnix::sleep(double timeout) -{ - ARCH->testCancelThread(); - if (timeout < 0.0) { - return; - } - -#if HAVE_NANOSLEEP - // prep timeout - struct timespec t; - t.tv_sec = (long)timeout; - t.tv_nsec = (long)(1.0e+9 * (timeout - (double)t.tv_sec)); - - // wait - while (nanosleep(&t, &t) < 0) - ARCH->testCancelThread(); -#else - /* emulate nanosleep() with select() */ - double startTime = ARCH->time(); - double timeLeft = timeout; - while (timeLeft > 0.0) { - struct timeval timeout2; - timeout2.tv_sec = static_cast(timeLeft); - timeout2.tv_usec = static_cast(1.0e+6 * (timeLeft - timeout2.tv_sec)); - select( - (SELECT_TYPE_ARG1)0, SELECT_TYPE_ARG234 nullptr, SELECT_TYPE_ARG234 nullptr, SELECT_TYPE_ARG234 nullptr, - SELECT_TYPE_ARG5 & timeout2 - ); - ARCH->testCancelThread(); - timeLeft = timeout - (ARCH->time() - startTime); - } -#endif -} diff --git a/src/lib/arch/unix/ArchSleepUnix.h b/src/lib/arch/unix/ArchSleepUnix.h deleted file mode 100644 index 38bae8bcb..000000000 --- a/src/lib/arch/unix/ArchSleepUnix.h +++ /dev/null @@ -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/IArchSleep.h" - -#define ARCH_SLEEP ArchSleepUnix - -//! Unix implementation of IArchSleep -class ArchSleepUnix : public IArchSleep -{ -public: - ArchSleepUnix() = default; - ~ArchSleepUnix() override = default; - - // IArchSleep overrides - void sleep(double timeout) override; -}; diff --git a/src/lib/arch/win32/ArchSleepWindows.cpp b/src/lib/arch/win32/ArchSleepWindows.cpp deleted file mode 100644 index 0d606ca93..000000000 --- a/src/lib/arch/win32/ArchSleepWindows.cpp +++ /dev/null @@ -1,38 +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/ArchSleepWindows.h" -#include "arch/Arch.h" -#include "arch/win32/ArchMultithreadWindows.h" - -// -// ArchSleepWindows -// - -void ArchSleepWindows::sleep(double timeout) -{ - ARCH->testCancelThread(); - if (timeout < 0.0) { - return; - } - - // get the cancel event from the current thread. this only - // works if we're using the windows multithread object but - // this is windows so that's pretty certain; we'll get a - // link error if we're not, though. - ArchMultithreadWindows *mt = ArchMultithreadWindows::getInstance(); - if (mt != nullptr) { - HANDLE cancelEvent = mt->getCancelEventForCurrentThread(); - WaitForSingleObject(cancelEvent, (DWORD)(1000.0 * timeout)); - if (timeout == 0.0) { - Sleep(0); - } - } else { - Sleep((DWORD)(1000.0 * timeout)); - } - ARCH->testCancelThread(); -} diff --git a/src/lib/arch/win32/ArchSleepWindows.h b/src/lib/arch/win32/ArchSleepWindows.h deleted file mode 100644 index e0bf73a46..000000000 --- a/src/lib/arch/win32/ArchSleepWindows.h +++ /dev/null @@ -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/IArchSleep.h" - -#define ARCH_SLEEP ArchSleepWindows - -//! Win32 implementation of IArchSleep -class ArchSleepWindows : public IArchSleep -{ -public: - ArchSleepWindows() = default; - ~ArchSleepWindows() override = default; - - // IArchSleep overrides - void sleep(double timeout) override; -};