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 "platform/OSXEventQueueBuffer.h"
|
|
|
|
|
|
|
|
|
|
#include "base/Event.h"
|
|
|
|
|
#include "base/IEventQueue.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
|
|
|
//
|
|
|
|
|
|
2024-10-17 18:04:35 +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
|
|
|
//
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
OSXEventQueueBuffer::OSXEventQueueBuffer(IEventQueue *events)
|
2024-07-16 13:36:44 +00:00
|
|
|
: m_event(NULL),
|
|
|
|
|
m_eventQueue(events),
|
2024-10-17 18:04:35 +00:00
|
|
|
m_carbonEventQueue(NULL)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
OSXEventQueueBuffer::~OSXEventQueueBuffer()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// release the last event
|
|
|
|
|
if (m_event != NULL) {
|
|
|
|
|
ReleaseEvent(m_event);
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void OSXEventQueueBuffer::init()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
m_carbonEventQueue = GetCurrentEventQueue();
|
2014-02-17 19:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void OSXEventQueueBuffer::waitForEvent(double timeout)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
EventRef event;
|
|
|
|
|
ReceiveNextEvent(0, NULL, timeout, false, &event);
|
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)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// release the previous event
|
|
|
|
|
if (m_event != NULL) {
|
|
|
|
|
ReleaseEvent(m_event);
|
|
|
|
|
m_event = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the next event
|
|
|
|
|
OSStatus error = ReceiveNextEvent(0, NULL, 0.0, true, &m_event);
|
|
|
|
|
|
|
|
|
|
// handle the event
|
|
|
|
|
if (error == eventLoopQuitErr) {
|
|
|
|
|
event = Event(Event::kQuit);
|
|
|
|
|
return kSystem;
|
|
|
|
|
} else if (error != noErr) {
|
|
|
|
|
return kNone;
|
|
|
|
|
} else {
|
2025-01-11 02:13:19 +00:00
|
|
|
uint32_t eventClass = GetEventClass(m_event);
|
2024-07-02 19:07:06 +00:00
|
|
|
switch (eventClass) {
|
|
|
|
|
case 'Syne':
|
|
|
|
|
dataID = GetEventKind(m_event);
|
|
|
|
|
return kUser;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
event = Event(Event::kSystem, m_eventQueue->getSystemTarget(), &m_event);
|
|
|
|
|
return kSystem;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2025-01-11 02:13:19 +00:00
|
|
|
bool OSXEventQueueBuffer::addEvent(uint32_t dataID)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
EventRef event;
|
2024-10-17 18:04:35 +00:00
|
|
|
OSStatus error = CreateEvent(kCFAllocatorDefault, 'Syne', dataID, 0, kEventAttributeNone, &event);
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
if (error == noErr) {
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
assert(m_carbonEventQueue != NULL);
|
|
|
|
|
|
|
|
|
|
error = PostEventToQueue(m_carbonEventQueue, event, kEventPriorityStandard);
|
|
|
|
|
|
|
|
|
|
ReleaseEvent(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (error == noErr);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool OSXEventQueueBuffer::isEmpty() const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
EventRef event;
|
|
|
|
|
OSStatus status = ReceiveNextEvent(0, NULL, 0.0, false, &event);
|
|
|
|
|
return (status == eventLoopTimedOutErr);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
EventQueueTimer *OSXEventQueueBuffer::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 OSXEventQueueBuffer::deleteTimer(EventQueueTimer *timer) const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
delete timer;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|