deskflow/src/lib/platform/OSXEventQueueBuffer.cpp

127 lines
2.8 KiB
C++
Raw Normal View History

2012-06-10 16:50:54 +00:00
/*
* Deskflow -- mouse and keyboard sharing utility
2016-09-07 14:24:00 +00:00
* Copyright (C) 2012-2016 Symless Ltd.
* Copyright (C) 2004 Chris Schoeneman
*
2012-06-10 16:50:54 +00:00
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
*
2012-06-10 16:50:54 +00:00
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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
//
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_event(NULL),
m_eventQueue(events),
m_carbonEventQueue(NULL)
{
// do nothing
2012-06-10 16:50:54 +00:00
}
OSXEventQueueBuffer::~OSXEventQueueBuffer()
{
// release the last event
if (m_event != NULL) {
ReleaseEvent(m_event);
}
2012-06-10 16:50:54 +00:00
}
void OSXEventQueueBuffer::init()
{
m_carbonEventQueue = GetCurrentEventQueue();
2014-02-17 19:38:26 +00:00
}
void OSXEventQueueBuffer::waitForEvent(double timeout)
{
EventRef event;
ReceiveNextEvent(0, NULL, timeout, false, &event);
2012-06-10 16:50:54 +00:00
}
IEventQueueBuffer::Type OSXEventQueueBuffer::getEvent(Event &event, UInt32 &dataID)
{
// 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 {
UInt32 eventClass = GetEventClass(m_event);
switch (eventClass) {
case 'Syne':
dataID = GetEventKind(m_event);
return kUser;
default:
event = Event(Event::kSystem, m_eventQueue->getSystemTarget(), &m_event);
return kSystem;
}
}
}
bool OSXEventQueueBuffer::addEvent(UInt32 dataID)
{
EventRef event;
OSStatus error = CreateEvent(kCFAllocatorDefault, 'Syne', dataID, 0, kEventAttributeNone, &event);
if (error == noErr) {
2012-06-10 16:50:54 +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
}
bool OSXEventQueueBuffer::isEmpty() const
{
EventRef event;
OSStatus status = ReceiveNextEvent(0, NULL, 0.0, false, &event);
return (status == eventLoopTimedOutErr);
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
}