2012-06-10 16:50:54 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
2014-11-02 12:12:05 +00:00
|
|
|
* Copyright (C) 2012 Synergy Si Ltd.
|
2012-09-04 02:09:56 +00:00
|
|
|
* 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
|
2015-05-03 02:33:52 +00:00
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
//
|
|
|
|
|
2014-11-11 13:51:47 +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
|
|
|
//
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
OSXEventQueueBuffer::OSXEventQueueBuffer(IEventQueue* events) :
|
2013-09-25 10:44:09 +00:00
|
|
|
m_event(NULL),
|
2014-02-25 15:03:43 +00:00
|
|
|
m_eventQueue(events),
|
2014-02-17 19:38:26 +00:00
|
|
|
m_carbonEventQueue(NULL)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
OSXEventQueueBuffer::~OSXEventQueueBuffer()
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
// release the last event
|
|
|
|
if (m_event != NULL) {
|
|
|
|
ReleaseEvent(m_event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 19:38:26 +00:00
|
|
|
void
|
2014-11-11 13:51:47 +00:00
|
|
|
OSXEventQueueBuffer::init()
|
2014-02-17 19:38:26 +00:00
|
|
|
{
|
|
|
|
m_carbonEventQueue = GetCurrentEventQueue();
|
|
|
|
}
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
void
|
2014-11-11 13:51:47 +00:00
|
|
|
OSXEventQueueBuffer::waitForEvent(double timeout)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
EventRef event;
|
|
|
|
ReceiveNextEvent(0, NULL, timeout, false, &event);
|
|
|
|
}
|
|
|
|
|
|
|
|
IEventQueueBuffer::Type
|
2014-11-11 13:51:47 +00:00
|
|
|
OSXEventQueueBuffer::getEvent(Event& event, UInt32& dataID)
|
2012-06-10 16:50:54 +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) {
|
2014-11-11 13:51:47 +00:00
|
|
|
event = Event(Event::kQuit);
|
2012-06-10 16:50:54 +00:00
|
|
|
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:
|
2014-11-11 13:51:47 +00:00
|
|
|
event = Event(Event::kSystem,
|
2013-06-29 22:02:04 +00:00
|
|
|
m_eventQueue->getSystemTarget(), &m_event);
|
2012-06-10 16:50:54 +00:00
|
|
|
return kSystem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-11-11 13:51:47 +00:00
|
|
|
OSXEventQueueBuffer::addEvent(UInt32 dataID)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
EventRef event;
|
2013-08-30 14:38:43 +00:00
|
|
|
OSStatus error = CreateEvent(
|
2012-06-10 16:50:54 +00:00
|
|
|
kCFAllocatorDefault,
|
|
|
|
'Syne',
|
|
|
|
dataID,
|
|
|
|
0,
|
|
|
|
kEventAttributeNone,
|
|
|
|
&event);
|
|
|
|
|
2014-02-17 19:38:26 +00:00
|
|
|
if (error == noErr) {
|
|
|
|
|
|
|
|
assert(m_carbonEventQueue != NULL);
|
|
|
|
|
|
|
|
error = PostEventToQueue(
|
|
|
|
m_carbonEventQueue,
|
|
|
|
event,
|
|
|
|
kEventPriorityStandard);
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
ReleaseEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error == noErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-11-11 13:51:47 +00:00
|
|
|
OSXEventQueueBuffer::isEmpty() const
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
EventRef event;
|
|
|
|
OSStatus status = ReceiveNextEvent(0, NULL, 0.0, false, &event);
|
|
|
|
return (status == eventLoopTimedOutErr);
|
|
|
|
}
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
EventQueueTimer*
|
|
|
|
OSXEventQueueBuffer::newTimer(double, bool) const
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
2014-11-11 13:51:47 +00:00
|
|
|
return new EventQueueTimer;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-11 13:51:47 +00:00
|
|
|
OSXEventQueueBuffer::deleteTimer(EventQueueTimer* timer) const
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
delete timer;
|
|
|
|
}
|