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