File: CEventQueue.h

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 (123 lines) | stat: -rw-r--r-- 3,399 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
/*
 * 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.
 */

#ifndef CEVENTQUEUE_H
#define CEVENTQUEUE_H

#include "IEventQueue.h"
#include "CEvent.h"
#include "CPriorityQueue.h"
#include "CStopwatch.h"
#include "IArchMultithread.h"
#include "stdmap.h"
#include "stdset.h"

//! Event queue
/*!
An event queue that implements the platform independent parts and
delegates the platform dependent parts to a subclass.
*/
class CEventQueue : public IEventQueue {
public:
	CEventQueue();
	virtual ~CEventQueue();

	// IEventQueue overrides
	virtual void		adoptBuffer(IEventQueueBuffer*);
	virtual bool		getEvent(CEvent& event, double timeout = -1.0);
	virtual bool		dispatchEvent(const CEvent& event);
	virtual void		addEvent(const CEvent& event);
	virtual CEventQueueTimer*
						newTimer(double duration, void* target);
	virtual CEventQueueTimer*
						newOneShotTimer(double duration, void* target);
	virtual void		deleteTimer(CEventQueueTimer*);
	virtual void		adoptHandler(CEvent::Type type,
							void* target, IEventJob* handler);
	virtual void		removeHandler(CEvent::Type type, void* target);
	virtual void		removeHandlers(void* target);
	virtual CEvent::Type
						registerType(const char* name);
	virtual CEvent::Type
						registerTypeOnce(CEvent::Type& type, const char* name);
	virtual bool		isEmpty() const;
	virtual IEventJob*	getHandler(CEvent::Type type, void* target) const;
	virtual const char*	getTypeName(CEvent::Type type);

private:
	UInt32				saveEvent(const CEvent& event);
	CEvent				removeEvent(UInt32 eventID);
	bool				hasTimerExpired(CEvent& event);
	double				getNextTimerTimeout() const;

private:
	class CTimer {
	public:
		CTimer(CEventQueueTimer*, double timeout, double initialTime,
							void* target, bool oneShot);
		~CTimer();

		void			reset();

		CTimer&			operator-=(double);

						operator double() const;

		bool			isOneShot() const;
		CEventQueueTimer*
						getTimer() const;
		void*			getTarget() const;
		void			fillEvent(CTimerEvent&) const;

		bool			operator<(const CTimer&) const;

	private:
		CEventQueueTimer*	m_timer;
		double				m_timeout;
		void*				m_target;
		bool				m_oneShot;
		double				m_time;
	};
	typedef std::set<CEventQueueTimer*> CTimers;
	typedef CPriorityQueue<CTimer> CTimerQueue;
	typedef std::map<UInt32, CEvent> CEventTable;
	typedef std::vector<UInt32> CEventIDList;
	typedef std::map<CEvent::Type, const char*> CTypeMap;
	typedef std::map<CEvent::Type, IEventJob*> CTypeHandlerTable;
	typedef std::map<void*, CTypeHandlerTable> CHandlerTable;

	CArchMutex			m_mutex;

	// registered events
	CEvent::Type		m_nextType;
	CTypeMap			m_typeMap;

	// buffer of events
	IEventQueueBuffer*	m_buffer;

	// saved events
	CEventTable			m_events;
	CEventIDList		m_oldEventIDs;

	// timers
	CStopwatch			m_time;
	CTimers				m_timers;
	CTimerQueue			m_timerQueue;
	CTimerEvent			m_timerEvent;

	// event handlers
	CHandlerTable		m_handlers;
};

#endif