deskflow/src/lib/platform/OSXEventQueueBuffer.cpp

102 lines
2.6 KiB
C++
Raw Normal View History

2012-06-10 16:50:54 +00:00
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Stephen Jensen <sjensen313@proton.me>
* SPDX-FileCopyrightText: (C) 2012 - 2025 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
*/
#include "platform/OSXEventQueueBuffer.h"
#include "base/Event.h"
#include "base/IEventQueue.h"
#include "base/Log.h"
2012-06-10 16:50:54 +00:00
//
2014-11-11 13:51:47 +00:00
// EventQueueTimer
2012-06-10 16:50:54 +00:00
//
class EventQueueTimer
{
};
2012-06-10 16:50:54 +00:00
//
2014-11-11 13:51:47 +00:00
// OSXEventQueueBuffer
2012-06-10 16:50:54 +00:00
//
OSXEventQueueBuffer::OSXEventQueueBuffer(IEventQueue *events) : m_eventQueue(events)
{
// Initialization is now managed using modern constructs
2012-06-10 16:50:54 +00:00
}
OSXEventQueueBuffer::~OSXEventQueueBuffer()
{
// No explicit clean-up needed as GCD and STL handle resource management
2012-06-10 16:50:54 +00:00
}
void OSXEventQueueBuffer::init()
{
// No initialization needed for GCD-based implementation
2014-02-17 19:38:26 +00:00
}
void OSXEventQueueBuffer::waitForEvent(double timeout)
{
std::unique_lock<std::mutex> lock(m_mutex);
if (m_dataQueue.empty()) {
auto duration = std::chrono::duration<double>(timeout);
LOG_DEBUG2("waiting for event, timeout: %f seconds", timeout);
m_cond.wait_for(lock, duration, [this] { return !m_dataQueue.empty(); });
} else {
LOG_DEBUG2("found events in the queue");
}
2012-06-10 16:50:54 +00:00
}
2025-01-11 02:13:19 +00:00
IEventQueueBuffer::Type OSXEventQueueBuffer::getEvent(Event &event, uint32_t &dataID)
{
std::unique_lock<std::mutex> lock(m_mutex);
if (m_dataQueue.empty()) {
LOG_DEBUG2("no events in queue");
return kNone;
}
dataID = m_dataQueue.front();
m_dataQueue.pop();
lock.unlock(); // Unlock early to allow other threads to proceed
LOG_DEBUG2("handled user event with dataID: %u", dataID);
return kUser;
}
2025-01-11 02:13:19 +00:00
bool OSXEventQueueBuffer::addEvent(uint32_t dataID)
{
// Use GCD to dispatch event addition on the main queue
dispatch_async(dispatch_get_main_queue(), ^{
std::lock_guard<std::mutex> lock(this->m_mutex);
LOG_DEBUG2("adding user event with dataID: %u", dataID);
this->m_dataQueue.push(dataID);
this->m_cond.notify_one();
LOG_DEBUG2("user event added to queue, dataID=%u", dataID);
});
// Always return true since dispatch_async does not fail under normal conditions
return true;
2012-06-10 16:50:54 +00:00
}
bool OSXEventQueueBuffer::isEmpty() const
{
std::lock_guard<std::mutex> lock(m_mutex);
bool empty = m_dataQueue.empty();
LOG_DEBUG2("queue is %s", empty ? "empty" : "not empty");
return empty;
2012-06-10 16:50:54 +00:00
}
EventQueueTimer *OSXEventQueueBuffer::newTimer(double, bool) const
{
return new EventQueueTimer;
2012-06-10 16:50:54 +00:00
}
void OSXEventQueueBuffer::deleteTimer(EventQueueTimer *timer) const
{
delete timer;
2012-06-10 16:50:54 +00:00
}