2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:55:14 +00:00
|
|
|
#include "arch/unix/ArchSleepUnix.h"
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "arch/Arch.h"
|
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
#if TIME_WITH_SYS_TIME
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#else
|
|
|
|
|
#if HAVE_SYS_TIME_H
|
|
|
|
|
#include <sys/time.h>
|
2012-06-10 16:50:54 +00:00
|
|
|
#else
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
#endif
|
2012-06-10 16:50:54 +00:00
|
|
|
#endif
|
|
|
|
|
#if !HAVE_NANOSLEEP
|
2024-07-02 19:07:06 +00:00
|
|
|
#if HAVE_SYS_SELECT_H
|
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
#endif
|
|
|
|
|
#if HAVE_SYS_TYPES_H
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#endif
|
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#endif
|
2012-06-10 16:50:54 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// ArchSleepUnix
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ArchSleepUnix::sleep(double timeout)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
ARCH->testCancelThread();
|
|
|
|
|
if (timeout < 0.0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
#if HAVE_NANOSLEEP
|
2024-07-02 19:07:06 +00:00
|
|
|
// prep timeout
|
|
|
|
|
struct timespec t;
|
|
|
|
|
t.tv_sec = (long)timeout;
|
|
|
|
|
t.tv_nsec = (long)(1.0e+9 * (timeout - (double)t.tv_sec));
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// wait
|
|
|
|
|
while (nanosleep(&t, &t) < 0)
|
|
|
|
|
ARCH->testCancelThread();
|
2012-06-10 16:50:54 +00:00
|
|
|
#else
|
2024-07-02 19:07:06 +00:00
|
|
|
/* emulate nanosleep() with select() */
|
|
|
|
|
double startTime = ARCH->time();
|
|
|
|
|
double timeLeft = timeout;
|
|
|
|
|
while (timeLeft > 0.0) {
|
|
|
|
|
struct timeval timeout2;
|
|
|
|
|
timeout2.tv_sec = static_cast<int>(timeLeft);
|
|
|
|
|
timeout2.tv_usec = static_cast<int>(1.0e+6 * (timeLeft - timeout2.tv_sec));
|
2024-07-16 13:36:44 +00:00
|
|
|
select(
|
2025-04-27 19:39:23 +00:00
|
|
|
(SELECT_TYPE_ARG1)0, SELECT_TYPE_ARG234 nullptr, SELECT_TYPE_ARG234 nullptr, SELECT_TYPE_ARG234 nullptr,
|
2024-10-17 18:04:35 +00:00
|
|
|
SELECT_TYPE_ARG5 & timeout2
|
|
|
|
|
);
|
2024-07-02 19:07:06 +00:00
|
|
|
ARCH->testCancelThread();
|
|
|
|
|
timeLeft = timeout - (ARCH->time() - startTime);
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
#endif
|
|
|
|
|
}
|