xref: /haiku/src/servers/app/EventDispatcher.h (revision 5e96d7d537fbec23bad4ae9b4c8e7b02e769f0c6)
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 		void _SendFakeMouseMoved(BMessage* message);
106 		bool _SendMessage(BMessenger& messenger, BMessage* message,
107 				float importance);
108 
109 		bool _AddTokens(BMessage* message, EventTarget* target,
110 				uint32 eventMask, BMessage* nextMouseMoved = NULL,
111 				int32* _viewToken = NULL);
112 		void _RemoveTokens(BMessage* message);
113 		void _SetFeedFocus(BMessage* message);
114 		void _UnsetFeedFocus(BMessage* message);
115 
116 		void _SetMouseTarget(const BMessenger* messenger);
117 		void _UnsetLastMouseTarget();
118 
119 		bool _AddListener(EventTarget& target, int32 token,
120 				uint32 eventMask, uint32 options, bool temporary);
121 		void _RemoveTemporaryListeners();
122 
123 		void _DeliverDragMessage();
124 
125 		void _EventLoop();
126 		void _CursorLoop();
127 
128 		static status_t _event_looper(void* dispatcher);
129 		static status_t _cursor_looper(void* dispatcher);
130 
131 	private:
132 		EventStream*	fStream;
133 		thread_id		fThread;
134 		thread_id		fCursorThread;
135 
136 		EventTarget*	fPreviousMouseTarget;
137 		EventTarget*	fFocus;
138 		bool			fSuspendFocus;
139 
140 		EventFilter*	fMouseFilter;
141 		EventFilter*	fKeyboardFilter;
142 
143 		BObjectList<EventTarget> fTargets;
144 
145 		BMessage*		fNextLatestMouseMoved;
146 		BPoint			fLastCursorPosition;
147 		int32			fLastButtons;
148 		bigtime_t		fLastUpdate;
149 
150 		BMessage		fDragMessage;
151 		bool			fDraggingMessage;
152 		BPoint			fDragOffset;
153 		ServerBitmap*	fDragBitmap;
154 			// NOTE: unfortunately, the EventDispatcher
155 			// has to know what a ServerBitmap is...
156 			// otherwise, linking the libs in the
157 			// testenvironment is problematic, because
158 			// the alternative is that HWInterface knows
159 			// about BitmapManager
160 
161 		BLocker			fCursorLock;
162 		HWInterface*	fHWInterface;
163 		Desktop*		fDesktop;
164 };
165 
166 #endif	/* EVENT_DISPATCHER_H */
167