| 1 | /*
|
|---|
| 2 | * synergy -- mouse and keyboard sharing utility
|
|---|
| 3 | * Copyright (C) 2004 Chris Schoeneman
|
|---|
| 4 | *
|
|---|
| 5 | * This package is free software; you can redistribute it and/or
|
|---|
| 6 | * modify it under the terms of the GNU General Public License
|
|---|
| 7 | * found in the file COPYING that should have accompanied this file.
|
|---|
| 8 | *
|
|---|
| 9 | * This package is distributed in the hope that it will be useful,
|
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | * GNU General Public License for more details.
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | #ifndef CEVENTQUEUE_H
|
|---|
| 16 | #define CEVENTQUEUE_H
|
|---|
| 17 |
|
|---|
| 18 | #include "IEventQueue.h"
|
|---|
| 19 | #include "CEvent.h"
|
|---|
| 20 | #include "CPriorityQueue.h"
|
|---|
| 21 | #include "CStopwatch.h"
|
|---|
| 22 | #include "IArchMultithread.h"
|
|---|
| 23 | #include "stdmap.h"
|
|---|
| 24 | #include "stdset.h"
|
|---|
| 25 |
|
|---|
| 26 | //! Event queue
|
|---|
| 27 | /*!
|
|---|
| 28 | An event queue that implements the platform independent parts and
|
|---|
| 29 | delegates the platform dependent parts to a subclass.
|
|---|
| 30 | */
|
|---|
| 31 | class CEventQueue : public IEventQueue {
|
|---|
| 32 | public:
|
|---|
| 33 | CEventQueue();
|
|---|
| 34 | virtual ~CEventQueue();
|
|---|
| 35 |
|
|---|
| 36 | // IEventQueue overrides
|
|---|
| 37 | virtual void adoptBuffer(IEventQueueBuffer*);
|
|---|
| 38 | virtual bool getEvent(CEvent& event, double timeout = -1.0);
|
|---|
| 39 | virtual bool dispatchEvent(const CEvent& event);
|
|---|
| 40 | virtual void addEvent(const CEvent& event);
|
|---|
| 41 | virtual CEventQueueTimer*
|
|---|
| 42 | newTimer(double duration, void* target);
|
|---|
| 43 | virtual CEventQueueTimer*
|
|---|
| 44 | newOneShotTimer(double duration, void* target);
|
|---|
| 45 | virtual void deleteTimer(CEventQueueTimer*);
|
|---|
| 46 | virtual void adoptHandler(CEvent::Type type,
|
|---|
| 47 | void* target, IEventJob* handler);
|
|---|
| 48 | virtual void removeHandler(CEvent::Type type, void* target);
|
|---|
| 49 | virtual void removeHandlers(void* target);
|
|---|
| 50 | virtual CEvent::Type
|
|---|
| 51 | registerType(const char* name);
|
|---|
| 52 | virtual CEvent::Type
|
|---|
| 53 | registerTypeOnce(CEvent::Type& type, const char* name);
|
|---|
| 54 | virtual bool isEmpty() const;
|
|---|
| 55 | virtual IEventJob* getHandler(CEvent::Type type, void* target) const;
|
|---|
| 56 | virtual const char* getTypeName(CEvent::Type type);
|
|---|
| 57 |
|
|---|
| 58 | private:
|
|---|
| 59 | UInt32 saveEvent(const CEvent& event);
|
|---|
| 60 | CEvent removeEvent(UInt32 eventID);
|
|---|
| 61 | bool hasTimerExpired(CEvent& event);
|
|---|
| 62 | double getNextTimerTimeout() const;
|
|---|
| 63 |
|
|---|
| 64 | private:
|
|---|
| 65 | class CTimer {
|
|---|
| 66 | public:
|
|---|
| 67 | CTimer(CEventQueueTimer*, double timeout, double initialTime,
|
|---|
| 68 | void* target, bool oneShot);
|
|---|
| 69 | ~CTimer();
|
|---|
| 70 |
|
|---|
| 71 | void reset();
|
|---|
| 72 |
|
|---|
| 73 | CTimer& operator-=(double);
|
|---|
| 74 |
|
|---|
| 75 | operator double() const;
|
|---|
| 76 |
|
|---|
| 77 | bool isOneShot() const;
|
|---|
| 78 | CEventQueueTimer*
|
|---|
| 79 | getTimer() const;
|
|---|
| 80 | void* getTarget() const;
|
|---|
| 81 | void fillEvent(CTimerEvent&) const;
|
|---|
| 82 |
|
|---|
| 83 | bool operator<(const CTimer&) const;
|
|---|
| 84 |
|
|---|
| 85 | private:
|
|---|
| 86 | CEventQueueTimer* m_timer;
|
|---|
| 87 | double m_timeout;
|
|---|
| 88 | void* m_target;
|
|---|
| 89 | bool m_oneShot;
|
|---|
| 90 | double m_time;
|
|---|
| 91 | };
|
|---|
| 92 | typedef std::set<CEventQueueTimer*> CTimers;
|
|---|
| 93 | typedef CPriorityQueue<CTimer> CTimerQueue;
|
|---|
| 94 | typedef std::map<UInt32, CEvent> CEventTable;
|
|---|
| 95 | typedef std::vector<UInt32> CEventIDList;
|
|---|
| 96 | typedef std::map<CEvent::Type, const char*> CTypeMap;
|
|---|
| 97 | typedef std::map<CEvent::Type, IEventJob*> CTypeHandlerTable;
|
|---|
| 98 | typedef std::map<void*, CTypeHandlerTable> CHandlerTable;
|
|---|
| 99 |
|
|---|
| 100 | CArchMutex m_mutex;
|
|---|
| 101 |
|
|---|
| 102 | // registered events
|
|---|
| 103 | CEvent::Type m_nextType;
|
|---|
| 104 | CTypeMap m_typeMap;
|
|---|
| 105 |
|
|---|
| 106 | // buffer of events
|
|---|
| 107 | IEventQueueBuffer* m_buffer;
|
|---|
| 108 |
|
|---|
| 109 | // saved events
|
|---|
| 110 | CEventTable m_events;
|
|---|
| 111 | CEventIDList m_oldEventIDs;
|
|---|
| 112 |
|
|---|
| 113 | // timers
|
|---|
| 114 | CStopwatch m_time;
|
|---|
| 115 | CTimers m_timers;
|
|---|
| 116 | CTimerQueue m_timerQueue;
|
|---|
| 117 | CTimerEvent m_timerEvent;
|
|---|
| 118 |
|
|---|
| 119 | // event handlers
|
|---|
| 120 | CHandlerTable m_handlers;
|
|---|
| 121 | };
|
|---|
| 122 |
|
|---|
| 123 | #endif
|
|---|