xref: /haiku/src/servers/input/InputServer.h (revision fc7456e9b1ec38c941134ed6d01c438cf289381e)
1 /*
2  * Copyright 2001-2013 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef INPUT_SERVER_APP_H
6 #define INPUT_SERVER_APP_H
7 
8 
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 
13 //#define DEBUG 1
14 
15 #include <Application.h>
16 #include <Debug.h>
17 #include <FindDirectory.h>
18 #include <InputServerDevice.h>
19 #include <InputServerFilter.h>
20 #include <InputServerMethod.h>
21 #include <InterfaceDefs.h>
22 #include <Locker.h>
23 #include <Message.h>
24 #include <ObjectList.h>
25 #include <OS.h>
26 #include <Screen.h>
27 #include <StringList.h>
28 #include <SupportDefs.h>
29 
30 #include <shared_cursor_area.h>
31 
32 #include "AddOnManager.h"
33 #include "KeyboardSettings.h"
34 #include "MouseSettings.h"
35 #include "PathList.h"
36 
37 
38 #define INPUTSERVER_SIGNATURE "application/x-vnd.Be-input_server"
39 	// use this when target should replace R5 input_server
40 
41 typedef BObjectList<BMessage> EventList;
42 
43 class BottomlineWindow;
44 
45 class InputDeviceListItem {
46 	public:
47 		InputDeviceListItem(BInputServerDevice& serverDevice,
48 			const input_device_ref& device);
49 		~InputDeviceListItem();
50 
51 		void Start();
52 		void Stop();
53 		void Control(uint32 code, BMessage* message);
54 
55 		const char* Name() const { return fDevice.name; }
56 		input_device_type Type() const { return fDevice.type; }
57 		bool Running() const { return fRunning; }
58 
59 		bool HasName(const char* name) const;
60 		bool HasType(input_device_type type) const;
61 		bool Matches(const char* name, input_device_type type) const;
62 
63 		BInputServerDevice* ServerDevice() { return fServerDevice; }
64 
65 	private:
66 		BInputServerDevice* fServerDevice;
67 		input_device_ref   	fDevice;
68 		bool 				fRunning;
69 };
70 
71 namespace BPrivate {
72 
73 class DeviceAddOn {
74 public:
75 								DeviceAddOn(BInputServerDevice* device);
76 								~DeviceAddOn();
77 
78 			bool				HasPath(const char* path) const;
79 			status_t			AddPath(const char* path);
80 			status_t			RemovePath(const char* path);
81 			int32				CountPaths() const;
82 
83 			BInputServerDevice*	Device() { return fDevice; }
84 
85 private:
86 			BInputServerDevice*	fDevice;
87 			PathList			fMonitoredPaths;
88 };
89 
90 }	// namespace BPrivate
91 
92 class _BMethodAddOn_ {
93 	public:
94 		_BMethodAddOn_(BInputServerMethod *method, const char* name,
95 			const uchar* icon);
96 		~_BMethodAddOn_();
97 
98 		status_t SetName(const char* name);
99 		status_t SetIcon(const uchar* icon);
100 		status_t SetMenu(const BMenu* menu, const BMessenger& messenger);
101 		status_t MethodActivated(bool activate);
102 		status_t AddMethod();
103 		int32 Cookie() { return fCookie; }
104 
105 	private:
106 		BInputServerMethod* fMethod;
107 		char* fName;
108 		uchar fIcon[16*16*1];
109 		const BMenu* fMenu;
110 		BMessenger fMessenger;
111 		int32 fCookie;
112 };
113 
114 class KeymapMethod : public BInputServerMethod {
115 	public:
116 		KeymapMethod();
117 		~KeymapMethod();
118 };
119 
120 class InputServer : public BApplication {
121 	public:
122 		InputServer();
123 		virtual ~InputServer();
124 
125 		virtual void ArgvReceived(int32 argc, char** argv);
126 
127 		virtual bool QuitRequested();
128 		virtual void ReadyToRun();
129 		virtual void MessageReceived(BMessage* message);
130 
131 		void HandleSetMethod(BMessage* message);
132 		status_t HandleGetSetMouseType(BMessage* message, BMessage* reply);
133 		status_t HandleGetSetMouseAcceleration(BMessage* message, BMessage* reply);
134 		status_t HandleGetSetKeyRepeatDelay(BMessage* message, BMessage* reply);
135 		status_t HandleGetKeyInfo(BMessage* message, BMessage* reply);
136 		status_t HandleGetModifiers(BMessage* message, BMessage* reply);
137 		status_t HandleGetModifierKey(BMessage* message, BMessage* reply);
138 		status_t HandleSetModifierKey(BMessage* message, BMessage* reply);
139 		status_t HandleSetKeyboardLocks(BMessage* message, BMessage* reply);
140 		status_t HandleGetSetMouseSpeed(BMessage* message, BMessage* reply);
141 		status_t HandleSetMousePosition(BMessage* message, BMessage* reply);
142 		status_t HandleGetSetMouseMap(BMessage* message, BMessage* reply);
143 		status_t HandleGetSetKeyboardID(BMessage* message, BMessage* reply);
144 		status_t HandleGetSetClickSpeed(BMessage* message, BMessage* reply);
145 		status_t HandleGetSetKeyRepeatRate(BMessage* message, BMessage* reply);
146 		status_t HandleGetSetKeyMap(BMessage* message, BMessage* reply);
147 		status_t HandleFocusUnfocusIMAwareView(BMessage* message, BMessage* reply);
148 
149 		status_t EnqueueDeviceMessage(BMessage* message);
150 		status_t EnqueueMethodMessage(BMessage* message);
151 		status_t SetNextMethod(bool direction);
152 		void SetActiveMethod(BInputServerMethod* method);
153 		const BMessenger* MethodReplicant();
154 		void SetMethodReplicant(const BMessenger *replicant);
155 		bool EventLoopRunning();
156 
157 		status_t GetDeviceInfo(const char* name, input_device_type *_type,
158 					bool *_isRunning = NULL);
159 		status_t GetDeviceInfos(BMessage *msg);
160 		status_t UnregisterDevices(BInputServerDevice& serverDevice,
161 					input_device_ref** devices = NULL);
162 		status_t RegisterDevices(BInputServerDevice& serverDevice,
163 					input_device_ref** devices);
164 		status_t StartStopDevices(const char* name, input_device_type type,
165 					bool doStart);
166 		status_t StartStopDevices(BInputServerDevice& serverDevice, bool start);
167 		status_t ControlDevices(const char *name, input_device_type type,
168 					uint32 code, BMessage* message);
169 
170 		bool SafeMode();
171 
172 		::AddOnManager* AddOnManager() { return fAddOnManager; }
173 
174 		static BList gInputFilterList;
175 		static BLocker gInputFilterListLocker;
176 
177 		static BList gInputMethodList;
178 		static BLocker gInputMethodListLocker;
179 
180 		static KeymapMethod gKeymapMethod;
181 
182 		BRect& ScreenFrame() { return fFrame; }
183 
184 	private:
185 		typedef BApplication _inherited;
186 
187 		status_t _LoadKeymap();
188 		status_t _LoadSystemKeymap();
189 		status_t _SaveKeymap(bool isDefault = false);
190 		void _InitKeyboardMouseStates();
191 
192 		MouseSettings* _RunningMouseSettings();
193 		void _RunningMiceSettings(BList& settings);
194 		void _DeviceStarted(InputDeviceListItem& item);
195 		void _DeviceStopping(InputDeviceListItem& item);
196 		MouseSettings* _GetSettingsForMouse(BString mouseName);
197 		status_t _PostMouseControlMessage(int32 code, const BString& mouseName);
198 
199 		status_t _StartEventLoop();
200 		void _EventLoop();
201 		static status_t _EventLooper(void *arg);
202 
203 		void _UpdateMouseAndKeys(EventList& events);
204 		bool _SanitizeEvents(EventList& events);
205 		bool _MethodizeEvents(EventList& events);
206 		bool _FilterEvents(EventList& events);
207 		void _DispatchEvents(EventList& events);
208 
209 		void _FilterEvent(BInputServerFilter* filter, EventList& events,
210 					int32& index, int32& count);
211 		status_t _DispatchEvent(BMessage* event);
212 
213 		status_t _AcquireInput(BMessage& message, BMessage& reply);
214 		void _ReleaseInput(BMessage* message);
215 
216 	private:
217 		uint16			fKeyboardID;
218 
219 		BList			fInputDeviceList;
220 		BLocker 		fInputDeviceListLocker;
221 
222 		KeyboardSettings fKeyboardSettings;
223 		MultipleMouseSettings	fMouseSettings;
224 		MouseSettings	fDefaultMouseSettings;
225 		BStringList		fRunningMouseList;
226 		BLocker 		fRunningMouseListLocker;
227 
228 		BPoint			fMousePos;		// current mouse position
229 		key_info		fKeyInfo;		// current key info
230 		key_map			fKeys;			// current key_map
231 		char*			fChars;			// current keymap chars
232 		uint32			fCharsSize;		// current keymap char count
233 
234 		port_id      	fEventLooperPort;
235 
236 		::AddOnManager*	fAddOnManager;
237 
238 		BScreen			fScreen;
239 		BRect			fFrame;
240 
241 		BLocker			fEventQueueLock;
242 		EventList 		fEventQueue;
243 
244 		BInputServerMethod*	fActiveMethod;
245 		EventList			fMethodQueue;
246 		const BMessenger*	fReplicantMessenger;
247 		BottomlineWindow*	fInputMethodWindow;
248 		bool				fInputMethodAware;
249 
250 		sem_id 			fCursorSem;
251 		port_id			fAppServerPort;
252 		team_id			fAppServerTeam;
253 		area_id			fCursorArea;
254 		shared_cursor*	fCursorBuffer;
255 };
256 
257 extern InputServer* gInputServer;
258 
259 #if DEBUG >= 1
260 #	if DEBUG == 2
261 #		undef PRINT
262 		inline void _iprint(const char *fmt, ...) {
263 			FILE* log = fopen("/var/log/input_server.log", "a");
264 			char buf[1024];
265 			va_list ap;
266 			va_start(ap, fmt);
267 			vsprintf(buf, fmt, ap);
268 			va_end(ap);
269 			fputs(buf, log);
270 			fflush(log);
271 			fclose(log);
272         }
273 #		define PRINT(x)	_iprint x
274 #	else
275 #		undef PRINT
276 #		define PRINT(x)	SERIAL_PRINT(x)
277 #	endif
278 #	define PRINTERR(x)		PRINT(x)
279 #	define EXIT()          PRINT(("EXIT %s\n", __PRETTY_FUNCTION__))
280 #	define CALLED()        PRINT(("CALLED %s\n", __PRETTY_FUNCTION__))
281 #else
282 #	define EXIT()          ((void)0)
283 #	define CALLED()        ((void)0)
284 #	define PRINTERR(x)		SERIAL_PRINT(x)
285 #endif
286 
287 #endif	/* INPUT_SERVER_APP_H */
288