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) 2004 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:36:45 +00:00
|
|
|
#include "base/SimpleEventQueueBuffer.h"
|
|
|
|
|
#include "arch/Arch.h"
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "base/Stopwatch.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
class EventQueueTimer
|
|
|
|
|
{
|
|
|
|
|
};
|
2017-01-07 19:55:20 +00:00
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// SimpleEventQueueBuffer
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
SimpleEventQueueBuffer::SimpleEventQueueBuffer()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
m_queueMutex = ARCH->newMutex();
|
|
|
|
|
m_queueReadyCond = ARCH->newCondVar();
|
|
|
|
|
m_queueReady = false;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
SimpleEventQueueBuffer::~SimpleEventQueueBuffer()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
ARCH->closeCondVar(m_queueReadyCond);
|
|
|
|
|
ARCH->closeMutex(m_queueMutex);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void SimpleEventQueueBuffer::waitForEvent(double timeout)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
ArchMutexLock lock(m_queueMutex);
|
|
|
|
|
Stopwatch timer(true);
|
|
|
|
|
while (!m_queueReady) {
|
|
|
|
|
double timeLeft = timeout;
|
|
|
|
|
if (timeLeft >= 0.0) {
|
|
|
|
|
timeLeft -= timer.getTime();
|
|
|
|
|
if (timeLeft < 0.0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
ARCH->waitCondVar(m_queueReadyCond, m_queueMutex, timeLeft);
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-11 02:13:19 +00:00
|
|
|
IEventQueueBuffer::Type SimpleEventQueueBuffer::getEvent(Event &, uint32_t &dataID)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
ArchMutexLock lock(m_queueMutex);
|
|
|
|
|
if (!m_queueReady) {
|
|
|
|
|
return kNone;
|
|
|
|
|
}
|
|
|
|
|
dataID = m_queue.back();
|
|
|
|
|
m_queue.pop_back();
|
|
|
|
|
m_queueReady = !m_queue.empty();
|
|
|
|
|
return kUser;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-11 02:13:19 +00:00
|
|
|
bool SimpleEventQueueBuffer::addEvent(uint32_t dataID)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
ArchMutexLock lock(m_queueMutex);
|
|
|
|
|
m_queue.push_front(dataID);
|
|
|
|
|
if (!m_queueReady) {
|
|
|
|
|
m_queueReady = true;
|
|
|
|
|
ARCH->broadcastCondVar(m_queueReadyCond);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool SimpleEventQueueBuffer::isEmpty() const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
ArchMutexLock lock(m_queueMutex);
|
|
|
|
|
return !m_queueReady;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
EventQueueTimer *SimpleEventQueueBuffer::newTimer(double, bool) const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
return new EventQueueTimer;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void SimpleEventQueueBuffer::deleteTimer(EventQueueTimer *timer) const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
delete timer;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|