1 /* 2 * Copyright 2005-2007, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Axel Dörfler, axeld@pinc-software.de 7 */ 8 #ifndef EVENT_DISPATCHER_H 9 #define EVENT_DISPATCHER_H 10 11 12 #include <Locker.h> 13 #include <Message.h> 14 #include <MessageFilter.h> 15 #include <Messenger.h> 16 #include <ObjectList.h> 17 18 19 class Desktop; 20 class EventStream; 21 class HWInterface; 22 class ServerBitmap; 23 24 struct event_listener; 25 26 27 class EventTarget { 28 public: 29 EventTarget(); 30 ~EventTarget(); 31 32 void SetTo(const BMessenger& messenger); 33 BMessenger& Messenger() { return fMessenger; } 34 35 event_listener* FindListener(int32 token, int32* _index = NULL); 36 bool AddListener(int32 token, uint32 eventMask, uint32 options, 37 bool temporary); 38 void RemoveListener(event_listener* listener, bool temporary); 39 40 bool RemoveListener(int32 token); 41 bool RemoveTemporaryListener(int32 token); 42 void RemoveTemporaryListeners(); 43 44 bool IsEmpty() const { return fListeners.IsEmpty(); } 45 46 int32 CountListeners() const { return fListeners.CountItems(); } 47 event_listener* ListenerAt(int32 index) const 48 { return fListeners.ItemAt(index); } 49 50 private: 51 bool _RemoveTemporaryListener(event_listener* listener, int32 index); 52 53 BObjectList<event_listener> fListeners; 54 BMessenger fMessenger; 55 }; 56 57 class EventFilter { 58 public: 59 virtual ~EventFilter() {}; 60 virtual filter_result Filter(BMessage* event, EventTarget** _target, 61 int32* _viewToken = NULL, BMessage* latestMouseMoved = NULL) = 0; 62 virtual void RemoveTarget(EventTarget* target); 63 }; 64 65 class EventDispatcher : public BLocker { 66 public: 67 EventDispatcher(); 68 ~EventDispatcher(); 69 70 status_t SetTo(EventStream* stream); 71 status_t InitCheck(); 72 73 void RemoveTarget(EventTarget& target); 74 75 bool AddListener(EventTarget& target, int32 token, 76 uint32 eventMask, uint32 options); 77 bool AddTemporaryListener(EventTarget& target, 78 int32 token, uint32 eventMask, uint32 options); 79 void RemoveListener(EventTarget& target, int32 token); 80 void RemoveTemporaryListener(EventTarget& target, int32 token); 81 82 void SetMouseFilter(EventFilter* filter); 83 void SetKeyboardFilter(EventFilter* filter); 84 85 void GetMouse(BPoint& where, int32& buttons); 86 void SendFakeMouseMoved(EventTarget& target, int32 viewToken); 87 bigtime_t IdleTime(); 88 89 bool HasCursorThread(); 90 void SetHWInterface(HWInterface* interface); 91 92 void SetDragMessage(BMessage& message, ServerBitmap* bitmap, 93 const BPoint& offsetFromCursor); 94 // the message should be delivered on the next 95 // "mouse up". 96 // if the mouse is not pressed, it should 97 // be delivered to the "current" target right away. 98 99 void SetDesktop(Desktop* desktop); 100 101 private: 102 status_t _Run(); 103 void _Unset(); 104 105 bool _SendMessage(BMessenger& messenger, BMessage* message, 106 float importance); 107 108 bool _AddTokens(BMessage* message, EventTarget* target, uint32 eventMask, 109 BMessage* nextMouseMoved = NULL, int32* _viewToken = NULL); 110 void _RemoveTokens(BMessage* message); 111 void _SetFeedFocus(BMessage* message); 112 void _UnsetFeedFocus(BMessage* message); 113 114 void _SetMouseTarget(const BMessenger* messenger); 115 void _UnsetLastMouseTarget(); 116 117 bool _AddListener(EventTarget& target, int32 token, 118 uint32 eventMask, uint32 options, bool temporary); 119 void _RemoveTemporaryListeners(); 120 121 void _DeliverDragMessage(); 122 123 void _EventLoop(); 124 void _CursorLoop(); 125 126 static status_t _event_looper(void* dispatcher); 127 static status_t _cursor_looper(void* dispatcher); 128 129 private: 130 EventStream* fStream; 131 thread_id fThread; 132 thread_id fCursorThread; 133 134 EventTarget* fPreviousMouseTarget; 135 EventTarget* fFocus; 136 bool fSuspendFocus; 137 138 EventFilter* fMouseFilter; 139 EventFilter* fKeyboardFilter; 140 141 BObjectList<EventTarget> fTargets; 142 143 BMessage* fNextLatestMouseMoved; 144 BPoint fLastCursorPosition; 145 int32 fLastButtons; 146 bigtime_t fLastUpdate; 147 148 BMessage fDragMessage; 149 bool fDraggingMessage; 150 BPoint fDragOffset; 151 ServerBitmap* fDragBitmap; 152 // NOTE: unfortunately, the EventDispatcher 153 // has to know what a ServerBitmap is... 154 // otherwise, linking the libs in the 155 // testenvironment is problematic, because 156 // the alternative is that HWInterface knows 157 // about BitmapManager 158 159 BLocker fCursorLock; 160 HWInterface* fHWInterface; 161 Desktop* fDesktop; 162 }; 163 164 #endif /* EVENT_DISPATCHER_H */ 165