File: COSXEventQueueBuffer.cpp

package info (click to toggle)
synergy 1.3.1-5
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 4,664 kB
  • ctags: 5,482
  • sloc: cpp: 46,292; sh: 3,392; makefile: 938; ansic: 82
file content (124 lines) | stat: -rw-r--r-- 2,514 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * synergy -- mouse and keyboard sharing utility
 * Copyright (C) 2004 Chris Schoeneman
 * 
 * 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 COPYING that should have accompanied this file.
 * 
 * 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.
 */

#include "COSXEventQueueBuffer.h"
#include "CEvent.h"
#include "IEventQueue.h"

//
// CEventQueueTimer
//

class CEventQueueTimer { };

//
// COSXEventQueueBuffer
//

COSXEventQueueBuffer::COSXEventQueueBuffer() :
	m_event(NULL)
{
	// do nothing
}

COSXEventQueueBuffer::~COSXEventQueueBuffer()
{
	// release the last event
	if (m_event != NULL) {
		ReleaseEvent(m_event);
	}
}

void
COSXEventQueueBuffer::waitForEvent(double timeout)
{
	EventRef event;
	ReceiveNextEvent(0, NULL, timeout, false, &event);
}

IEventQueueBuffer::Type
COSXEventQueueBuffer::getEvent(CEvent& 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 = CEvent(CEvent::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 = CEvent(CEvent::kSystem,
						IEventQueue::getSystemTarget(), &m_event);
			return kSystem;
		}
	}
}

bool
COSXEventQueueBuffer::addEvent(UInt32 dataID)
{
	EventRef event;
	OSStatus error = CreateEvent( 
							kCFAllocatorDefault,
							'Syne', 
							dataID,
							0,
							kEventAttributeNone,
							&event);

	if (error == noErr) {
		error = PostEventToQueue(GetMainEventQueue(), event, 
							kEventPriorityStandard);
		ReleaseEvent(event);
	}
	
	return (error == noErr);
}

bool
COSXEventQueueBuffer::isEmpty() const
{
	EventRef event;
	OSStatus status = ReceiveNextEvent(0, NULL, 0.0, false, &event);
	return (status == eventLoopTimedOutErr);
}

CEventQueueTimer*
COSXEventQueueBuffer::newTimer(double, bool) const
{
	return new CEventQueueTimer;
}

void
COSXEventQueueBuffer::deleteTimer(CEventQueueTimer* timer) const
{
	delete timer;
}