/* * 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 = NULL); virtual CEventQueueTimer* newOneShotTimer(double duration, void* target = NULL); virtual void deleteTimer(CEventQueueTimer*); virtual void adoptHandler(void* target, IEventJob* dispatcher); virtual void adoptHandler(CEvent::Type type, void* target, IEventJob* handler); virtual IEventJob* orphanHandler(void* target); virtual IEventJob* orphanHandler(CEvent::Type type, void* target); virtual void removeHandler(void* target); virtual void removeHandler(CEvent::Type type, void* target); virtual bool isEmpty() const; virtual IEventJob* getHandler(CEvent::Type type, void* target) const; private: void doAdoptHandler(CEvent::Type type, void* target, IEventJob* handler); IEventJob* doOrphanHandler(CEvent::Type type, void* target); UInt32 saveEvent(const CEvent& event); CEvent removeEvent(UInt32 eventID); bool hasTimerExpired(CEvent& event); double getNextTimerTimeout() const; private: class CTypeTarget { public: CTypeTarget(CEvent::Type type, void* target); ~CTypeTarget(); bool operator<(const CTypeTarget&) const; private: CEvent::Type m_type; void* m_target; }; class CTimer { public: CTimer(CEventQueueTimer*, double timeout, 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 CTimers; typedef CPriorityQueue CTimerQueue; typedef std::map CEventTable; typedef std::vector CEventIDList; typedef std::map CHandlerTable; CArchMutex m_mutex; IEventQueueBuffer* m_buffer; CEventTable m_events; CEventIDList m_oldEventIDs; CStopwatch m_time; CTimers m_timers; CTimerQueue m_timerQueue; CTimerEvent m_timerEvent; CHandlerTable m_handlers; }; #endif