1 /* 2 * Copyright 2005-2006, 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 filter_result Filter(BMessage* event, EventTarget** _target, 60 int32* _viewToken = NULL, BMessage* latestMouseMoved = NULL) = 0; 61 virtual void RemoveTarget(EventTarget* target); 62 }; 63 64 class EventDispatcher : public BLocker { 65 public: 66 EventDispatcher(); 67 ~EventDispatcher(); 68 69 status_t SetTo(EventStream* stream); 70 status_t InitCheck(); 71 72 void RemoveTarget(EventTarget& target); 73 74 bool AddListener(EventTarget& target, int32 token, 75 uint32 eventMask, uint32 options); 76 bool AddTemporaryListener(EventTarget& target, 77 int32 token, uint32 eventMask, uint32 options); 78 void RemoveListener(EventTarget& target, int32 token); 79 void RemoveTemporaryListener(EventTarget& target, int32 token); 80 81 void SetMouseFilter(EventFilter* filter); 82 void SetKeyboardFilter(EventFilter* filter); 83 84 void GetMouse(BPoint& where, int32& buttons); 85 void SendFakeMouseMoved(EventTarget& target, int32 viewToken); 86 87 bool HasCursorThread(); 88 void SetHWInterface(HWInterface* interface); 89 90 void SetDragMessage(BMessage& message, ServerBitmap* bitmap, 91 const BPoint& offsetFromCursor); 92 // the message should be delivered on the next 93 // "mouse up". 94 // if the mouse is not pressed, it should 95 // be delivered to the "current" target right away. 96 97 void SetDesktop(Desktop* desktop); 98 99 private: 100 status_t _Run(); 101 void _Unset(); 102 103 bool _SendMessage(BMessenger& messenger, BMessage* message, float importance); 104 105 bool _AddTokens(BMessage* message, EventTarget* target, uint32 eventMask, 106 BMessage* nextMouseMoved = NULL, int32* _viewToken = NULL); 107 void _RemoveTokens(BMessage* message); 108 void _SetFeedFocus(BMessage* message); 109 void _UnsetFeedFocus(BMessage* message); 110 111 void _SetMouseTarget(const BMessenger* messenger); 112 void _UnsetLastMouseTarget(); 113 114 bool _AddListener(EventTarget& target, int32 token, 115 uint32 eventMask, uint32 options, bool temporary); 116 void _RemoveTemporaryListeners(); 117 118 void _DeliverDragMessage(); 119 120 void _EventLoop(); 121 void _CursorLoop(); 122 123 static status_t _event_looper(void* dispatcher); 124 static status_t _cursor_looper(void* dispatcher); 125 126 private: 127 EventStream* fStream; 128 thread_id fThread; 129 thread_id fCursorThread; 130 131 EventTarget* fPreviousMouseTarget; 132 EventTarget* fFocus; 133 bool fSuspendFocus; 134 135 EventFilter* fMouseFilter; 136 EventFilter* fKeyboardFilter; 137 138 BObjectList<EventTarget> fTargets; 139 140 BMessage* fNextLatestMouseMoved; 141 BPoint fLastCursorPosition; 142 int32 fLastButtons; 143 144 BMessage fDragMessage; 145 bool fDraggingMessage; 146 BPoint fDragOffset; 147 ServerBitmap* fDragBitmap; 148 // NOTE: unfortunately, the EventDispatcher 149 // has to know what a ServerBitmap is... 150 // otherwise, linking the libs in the 151 // testenvironment is problematic, because 152 // the alternative is that HWInterface knows 153 // about BitmapManager 154 155 BLocker fCursorLock; 156 HWInterface* fHWInterface; 157 Desktop* fDesktop; 158 }; 159 160 #endif /* EVENT_DISPATCHER_H */ 161