2004-01-24 16:09:25 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2004-02-08 17:07:11 +00:00
|
|
|
virtual void adoptBuffer(IEventQueueBuffer*);
|
2004-01-24 16:09:25 +00:00
|
|
|
virtual bool getEvent(CEvent& event, double timeout = -1.0);
|
|
|
|
virtual bool dispatchEvent(const CEvent& event);
|
|
|
|
virtual void addEvent(const CEvent& event);
|
|
|
|
virtual CEventQueueTimer*
|
2004-02-14 14:04:36 +00:00
|
|
|
newTimer(double duration, void* target);
|
2004-01-24 16:09:25 +00:00
|
|
|
virtual CEventQueueTimer*
|
2004-02-14 14:04:36 +00:00
|
|
|
newOneShotTimer(double duration, void* target);
|
2004-01-24 16:09:25 +00:00
|
|
|
virtual void deleteTimer(CEventQueueTimer*);
|
2004-02-01 21:09:22 +00:00
|
|
|
virtual void adoptHandler(CEvent::Type type,
|
|
|
|
void* target, IEventJob* handler);
|
|
|
|
virtual void removeHandler(CEvent::Type type, void* target);
|
2004-09-29 21:59:26 +00:00
|
|
|
virtual void removeHandlers(void* target);
|
2004-02-14 14:04:36 +00:00
|
|
|
virtual CEvent::Type
|
|
|
|
registerType(const char* name);
|
|
|
|
virtual CEvent::Type
|
|
|
|
registerTypeOnce(CEvent::Type& type, const char* name);
|
2004-01-24 16:09:25 +00:00
|
|
|
virtual bool isEmpty() const;
|
2004-02-01 21:09:22 +00:00
|
|
|
virtual IEventJob* getHandler(CEvent::Type type, void* target) const;
|
2004-02-14 14:04:36 +00:00
|
|
|
virtual const char* getTypeName(CEvent::Type type);
|
2004-01-24 16:09:25 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
UInt32 saveEvent(const CEvent& event);
|
2004-02-08 17:07:11 +00:00
|
|
|
CEvent removeEvent(UInt32 eventID);
|
2004-01-24 16:09:25 +00:00
|
|
|
bool hasTimerExpired(CEvent& event);
|
|
|
|
double getNextTimerTimeout() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
class CTimer {
|
|
|
|
public:
|
2004-02-14 14:04:36 +00:00
|
|
|
CTimer(CEventQueueTimer*, double timeout, double initialTime,
|
|
|
|
void* target, bool oneShot);
|
2004-01-24 16:09:25 +00:00
|
|
|
~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;
|
2004-02-14 14:04:36 +00:00
|
|
|
typedef std::map<CEvent::Type, const char*> CTypeMap;
|
2004-09-29 21:59:26 +00:00
|
|
|
typedef std::map<CEvent::Type, IEventJob*> CTypeHandlerTable;
|
|
|
|
typedef std::map<void*, CTypeHandlerTable> CHandlerTable;
|
2004-01-24 16:09:25 +00:00
|
|
|
|
|
|
|
CArchMutex m_mutex;
|
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
// registered events
|
|
|
|
CEvent::Type m_nextType;
|
|
|
|
CTypeMap m_typeMap;
|
|
|
|
|
|
|
|
// buffer of events
|
2004-02-08 17:07:11 +00:00
|
|
|
IEventQueueBuffer* m_buffer;
|
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
// saved events
|
2004-01-24 16:09:25 +00:00
|
|
|
CEventTable m_events;
|
|
|
|
CEventIDList m_oldEventIDs;
|
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
// timers
|
2004-01-24 16:09:25 +00:00
|
|
|
CStopwatch m_time;
|
|
|
|
CTimers m_timers;
|
|
|
|
CTimerQueue m_timerQueue;
|
|
|
|
CTimerEvent m_timerEvent;
|
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
// event handlers
|
2004-01-24 16:09:25 +00:00
|
|
|
CHandlerTable m_handlers;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|