| 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 IEVENTQUEUE_H | 
|---|
| 16 | #define IEVENTQUEUE_H | 
|---|
| 17 |  | 
|---|
| 18 | #include "IInterface.h" | 
|---|
| 19 | #include "CEvent.h" | 
|---|
| 20 |  | 
|---|
| 21 | #define EVENTQUEUE IEventQueue::getInstance() | 
|---|
| 22 |  | 
|---|
| 23 | class IEventJob; | 
|---|
| 24 | class IEventQueueBuffer; | 
|---|
| 25 |  | 
|---|
| 26 | // Opaque type for timer info.  This is defined by subclasses of | 
|---|
| 27 | // IEventQueueBuffer. | 
|---|
| 28 | class CEventQueueTimer; | 
|---|
| 29 |  | 
|---|
| 30 | //! Event queue interface | 
|---|
| 31 | /*! | 
|---|
| 32 | An event queue provides a queue of CEvents.  Clients can block waiting | 
|---|
| 33 | on any event becoming available at the head of the queue and can place | 
|---|
| 34 | new events at the end of the queue.  Clients can also add and remove | 
|---|
| 35 | timers which generate events periodically. | 
|---|
| 36 | */ | 
|---|
| 37 | class IEventQueue : public IInterface { | 
|---|
| 38 | public: | 
|---|
| 39 | class CTimerEvent { | 
|---|
| 40 | public: | 
|---|
| 41 | CEventQueueTimer*       m_timer;        //!< The timer | 
|---|
| 42 | UInt32                          m_count;        //!< Number of repeats | 
|---|
| 43 | }; | 
|---|
| 44 |  | 
|---|
| 45 | //! @name manipulators | 
|---|
| 46 | //@{ | 
|---|
| 47 |  | 
|---|
| 48 | //! Set the buffer | 
|---|
| 49 | /*! | 
|---|
| 50 | Replace the current event queue buffer.  Any queued events are | 
|---|
| 51 | discarded.  The queue takes ownership of the buffer. | 
|---|
| 52 | */ | 
|---|
| 53 | virtual void            adoptBuffer(IEventQueueBuffer*) = 0; | 
|---|
| 54 |  | 
|---|
| 55 | //! Remove event from queue | 
|---|
| 56 | /*! | 
|---|
| 57 | Returns the next event on the queue into \p event.  If no event is | 
|---|
| 58 | available then blocks for up to \p timeout seconds, or forever if | 
|---|
| 59 | \p timeout is negative.  Returns true iff an event was available. | 
|---|
| 60 | */ | 
|---|
| 61 | virtual bool            getEvent(CEvent& event, double timeout = -1.0) = 0; | 
|---|
| 62 |  | 
|---|
| 63 | //! Dispatch an event | 
|---|
| 64 | /*! | 
|---|
| 65 | Looks up the dispatcher for the event's target and invokes it. | 
|---|
| 66 | Returns true iff a dispatcher exists for the target. | 
|---|
| 67 | */ | 
|---|
| 68 | virtual bool            dispatchEvent(const CEvent& event) = 0; | 
|---|
| 69 |  | 
|---|
| 70 | //! Add event to queue | 
|---|
| 71 | /*! | 
|---|
| 72 | Adds \p event to the end of the queue. | 
|---|
| 73 | */ | 
|---|
| 74 | virtual void            addEvent(const CEvent& event) = 0; | 
|---|
| 75 |  | 
|---|
| 76 | //! Create a recurring timer | 
|---|
| 77 | /*! | 
|---|
| 78 | Creates and returns a timer.  An event is returned after \p duration | 
|---|
| 79 | seconds and the timer is reset to countdown again.  When a timer event | 
|---|
| 80 | is returned the data points to a \c CTimerEvent.  The client must pass | 
|---|
| 81 | the returned timer to \c deleteTimer() (whether or not the timer has | 
|---|
| 82 | expired) to release the timer.  The returned timer event uses the | 
|---|
| 83 | given \p target.  If \p target is NULL it uses the returned timer as | 
|---|
| 84 | the target. | 
|---|
| 85 |  | 
|---|
| 86 | Events for a single timer don't accumulate in the queue, even if the | 
|---|
| 87 | client reading events can't keep up.  Instead, the \c m_count member | 
|---|
| 88 | of the \c CTimerEvent indicates how many events for the timer would | 
|---|
| 89 | have been put on the queue since the last event for the timer was | 
|---|
| 90 | removed (or since the timer was added). | 
|---|
| 91 | */ | 
|---|
| 92 | virtual CEventQueueTimer* | 
|---|
| 93 | newTimer(double duration, void* target) = 0; | 
|---|
| 94 |  | 
|---|
| 95 | //! Create a one-shot timer | 
|---|
| 96 | /*! | 
|---|
| 97 | Creates and returns a one-shot timer.  An event is returned when | 
|---|
| 98 | the timer expires and the timer is removed from further handling. | 
|---|
| 99 | When a timer event is returned the data points to a \c CTimerEvent. | 
|---|
| 100 | The \m c_count member of the \c CTimerEvent is always 1.  The client | 
|---|
| 101 | must pass the returned timer to \c deleteTimer() (whether or not the | 
|---|
| 102 | timer has expired) to release the timer.  The returned timer event | 
|---|
| 103 | uses the given \p target.  If \p target is NULL it uses the returned | 
|---|
| 104 | timer as the target. | 
|---|
| 105 | */ | 
|---|
| 106 | virtual CEventQueueTimer* | 
|---|
| 107 | newOneShotTimer(double duration, | 
|---|
| 108 | void* target) = 0; | 
|---|
| 109 |  | 
|---|
| 110 | //! Destroy a timer | 
|---|
| 111 | /*! | 
|---|
| 112 | Destroys a previously created timer.  The timer is removed from the | 
|---|
| 113 | queue and will not generate event, even if the timer has expired. | 
|---|
| 114 | */ | 
|---|
| 115 | virtual void            deleteTimer(CEventQueueTimer*) = 0; | 
|---|
| 116 |  | 
|---|
| 117 | //! Register an event handler for an event type | 
|---|
| 118 | /*! | 
|---|
| 119 | Registers an event handler for \p type and \p target.  The \p handler | 
|---|
| 120 | is adopted.  Any existing handler for the type,target pair is deleted. | 
|---|
| 121 | \c dispatchEvent() will invoke \p handler for any event for \p target | 
|---|
| 122 | of type \p type.  If no such handler exists it will use the handler | 
|---|
| 123 | for \p target and type \p kUnknown if it exists. | 
|---|
| 124 | */ | 
|---|
| 125 | virtual void            adoptHandler(CEvent::Type type, | 
|---|
| 126 | void* target, IEventJob* handler) = 0; | 
|---|
| 127 |  | 
|---|
| 128 | //! Unregister an event handler for an event type | 
|---|
| 129 | /*! | 
|---|
| 130 | Unregisters an event handler for the \p type, \p target pair and | 
|---|
| 131 | deletes it. | 
|---|
| 132 | */ | 
|---|
| 133 | virtual void            removeHandler(CEvent::Type type, void* target) = 0; | 
|---|
| 134 |  | 
|---|
| 135 | //! Unregister all event handlers for an event target | 
|---|
| 136 | /*! | 
|---|
| 137 | Unregisters all event handlers for the \p target and deletes them. | 
|---|
| 138 | */ | 
|---|
| 139 | virtual void            removeHandlers(void* target) = 0; | 
|---|
| 140 |  | 
|---|
| 141 | //! Creates a new event type | 
|---|
| 142 | /*! | 
|---|
| 143 | Returns a unique event type id. | 
|---|
| 144 | */ | 
|---|
| 145 | virtual CEvent::Type | 
|---|
| 146 | registerType(const char* name) = 0; | 
|---|
| 147 |  | 
|---|
| 148 | //! Creates a new event type | 
|---|
| 149 | /*! | 
|---|
| 150 | If \p type contains \c kUnknown then it is set to a unique event | 
|---|
| 151 | type id otherwise it is left alone.  The final value of \p type | 
|---|
| 152 | is returned. | 
|---|
| 153 | */ | 
|---|
| 154 | virtual CEvent::Type | 
|---|
| 155 | registerTypeOnce(CEvent::Type& type, | 
|---|
| 156 | const char* name) = 0; | 
|---|
| 157 |  | 
|---|
| 158 | //@} | 
|---|
| 159 | //! @name accessors | 
|---|
| 160 | //@{ | 
|---|
| 161 |  | 
|---|
| 162 | //! Test if queue is empty | 
|---|
| 163 | /*! | 
|---|
| 164 | Returns true iff the queue has no events in it, including timer | 
|---|
| 165 | events. | 
|---|
| 166 | */ | 
|---|
| 167 | virtual bool            isEmpty() const = 0; | 
|---|
| 168 |  | 
|---|
| 169 | //! Get an event handler | 
|---|
| 170 | /*! | 
|---|
| 171 | Finds and returns the event handler for the \p type, \p target pair | 
|---|
| 172 | if it exists, otherwise it returns NULL. | 
|---|
| 173 | */ | 
|---|
| 174 | virtual IEventJob*      getHandler(CEvent::Type type, void* target) const = 0; | 
|---|
| 175 |  | 
|---|
| 176 | //! Get name for event | 
|---|
| 177 | /*! | 
|---|
| 178 | Returns the name for the event \p type.  This is primarily for | 
|---|
| 179 | debugging. | 
|---|
| 180 | */ | 
|---|
| 181 | virtual const char*     getTypeName(CEvent::Type type) = 0; | 
|---|
| 182 |  | 
|---|
| 183 | //! Get the system event type target | 
|---|
| 184 | /*! | 
|---|
| 185 | Returns the target to use for dispatching \c CEvent::kSystem events. | 
|---|
| 186 | */ | 
|---|
| 187 | static void*            getSystemTarget(); | 
|---|
| 188 |  | 
|---|
| 189 | //! Get the singleton instance | 
|---|
| 190 | /*! | 
|---|
| 191 | Returns the singleton instance of the event queue | 
|---|
| 192 | */ | 
|---|
| 193 | static IEventQueue*     getInstance(); | 
|---|
| 194 |  | 
|---|
| 195 | //@} | 
|---|
| 196 |  | 
|---|
| 197 | protected: | 
|---|
| 198 | //! @name manipulators | 
|---|
| 199 | //@{ | 
|---|
| 200 |  | 
|---|
| 201 | //! Set the singleton instance | 
|---|
| 202 | /*! | 
|---|
| 203 | Sets the singleton instance of the event queue | 
|---|
| 204 | */ | 
|---|
| 205 | static void                     setInstance(IEventQueue*); | 
|---|
| 206 |  | 
|---|
| 207 | //@} | 
|---|
| 208 |  | 
|---|
| 209 | private: | 
|---|
| 210 | static IEventQueue*     s_instance; | 
|---|
| 211 | }; | 
|---|
| 212 |  | 
|---|
| 213 | #endif | 
|---|