xref: /haiku/src/servers/input/InputServer.h (revision 58481f0f6ef1a61ba07283f012cafbc2ed874ead)
1 /*
2  * Copyright 2001-2008, 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 <SupportDefs.h>
28 
29 #include <shared_cursor_area.h>
30 
31 #include "AddOnManager.h"
32 #include "KeyboardSettings.h"
33 #include "MouseSettings.h"
34 #include "PathList.h"
35 
36 
37 #define INPUTSERVER_SIGNATURE "application/x-vnd.Be-input_server"
38 	// use this when target should replace R5 input_server
39 
40 typedef BObjectList<BMessage> EventList;
41 
42 class BottomlineWindow;
43 
44 class InputDeviceListItem {
45 	public:
46 		InputDeviceListItem(BInputServerDevice& serverDevice,
47 			const input_device_ref& device);
48 		~InputDeviceListItem();
49 
50 		void Start();
51 		void Stop();
52 		void Control(uint32 code, BMessage* message);
53 
54 		const char* Name() const { return fDevice.name; }
55 		input_device_type Type() const { return fDevice.type; }
56 		bool Running() const { return fRunning; }
57 
58 		bool HasName(const char* name) const;
59 		bool HasType(input_device_type type) const;
60 		bool Matches(const char* name, input_device_type type) const;
61 
62 		BInputServerDevice* ServerDevice() { return fServerDevice; }
63 
64 	private:
65 		BInputServerDevice* fServerDevice;
66 		input_device_ref   	fDevice;
67 		bool 				fRunning;
68 };
69 
70 namespace BPrivate {
71 
72 class DeviceAddOn {
73 public:
74 								DeviceAddOn(BInputServerDevice* device);
75 								~DeviceAddOn();
76 
77 			bool				HasPath(const char* path) const;
78 			status_t			AddPath(const char* path);
79 			status_t			RemovePath(const char* path);
80 			int32				CountPaths() const;
81 
82 			BInputServerDevice*	Device() { return fDevice; }
83 
84 private:
85 			BInputServerDevice*	fDevice;
86 			PathList			fMonitoredPaths;
87 };
88 
89 }	// namespace BPrivate
90 
91 class _BMethodAddOn_ {
92 	public:
93 		_BMethodAddOn_(BInputServerMethod *method, const char* name,
94 			const uchar* icon);
95 		~_BMethodAddOn_();
96 
97 		status_t SetName(const char* name);
98 		status_t SetIcon(const uchar* icon);
99 		status_t SetMenu(const BMenu* menu, const BMessenger& messenger);
100 		status_t MethodActivated(bool activate);
101 		status_t AddMethod();
102 
103 	private:
104 		BInputServerMethod* fMethod;
105 		char* fName;
106 		uchar fIcon[16*16*1];
107 		const BMenu* fMenu;
108 		BMessenger fMessenger;
109 };
110 
111 class KeymapMethod : public BInputServerMethod {
112 	public:
113 		KeymapMethod();
114 		~KeymapMethod();
115 };
116 
117 class InputServer : public BApplication {
118 	public:
119 		InputServer();
120 		virtual ~InputServer();
121 
122 		virtual void ArgvReceived(int32 argc, char** argv);
123 
124 		virtual bool QuitRequested();
125 		virtual void ReadyToRun();
126 		virtual void MessageReceived(BMessage* message);
127 
128 		void HandleSetMethod(BMessage* message);
129 		status_t HandleGetSetMouseType(BMessage* message, BMessage* reply);
130 		status_t HandleGetSetMouseAcceleration(BMessage* message, BMessage* reply);
131 		status_t HandleGetSetKeyRepeatDelay(BMessage* message, BMessage* reply);
132 		status_t HandleGetKeyInfo(BMessage* message, BMessage* reply);
133 		status_t HandleGetModifiers(BMessage* message, BMessage* reply);
134 		status_t HandleGetModifierKey(BMessage* message, BMessage* reply);
135 		status_t HandleSetModifierKey(BMessage* message, BMessage* reply);
136 		status_t HandleSetKeyboardLocks(BMessage* message, BMessage* reply);
137 		status_t HandleGetSetMouseSpeed(BMessage* message, BMessage* reply);
138 		status_t HandleSetMousePosition(BMessage* message, BMessage* reply);
139 		status_t HandleGetSetMouseMap(BMessage* message, BMessage* reply);
140 		status_t HandleGetKeyboardID(BMessage* message, BMessage* reply);
141 		status_t HandleGetSetClickSpeed(BMessage* message, BMessage* reply);
142 		status_t HandleGetSetKeyRepeatRate(BMessage* message, BMessage* reply);
143 		status_t HandleGetSetKeyMap(BMessage* message, BMessage* reply);
144 		status_t HandleFocusUnfocusIMAwareView(BMessage* message, BMessage* reply);
145 
146 		status_t EnqueueDeviceMessage(BMessage* message);
147 		status_t EnqueueMethodMessage(BMessage* message);
148 		status_t SetNextMethod(bool direction);
149 		void SetActiveMethod(BInputServerMethod* method);
150 		const BMessenger* MethodReplicant();
151 		void SetMethodReplicant(const BMessenger *replicant);
152 		bool EventLoopRunning();
153 
154 		status_t GetDeviceInfo(const char* name, input_device_type *_type,
155 					bool *_isRunning = NULL);
156 		status_t GetDeviceInfos(BMessage *msg);
157 		status_t UnregisterDevices(BInputServerDevice& serverDevice,
158 					input_device_ref** devices = NULL);
159 		status_t RegisterDevices(BInputServerDevice& serverDevice,
160 					input_device_ref** devices);
161 		status_t StartStopDevices(const char* name, input_device_type type,
162 					bool doStart);
163 		status_t StartStopDevices(BInputServerDevice& serverDevice, bool start);
164 		status_t ControlDevices(const char *name, input_device_type type,
165 					uint32 code, BMessage* message);
166 
167 		bool DoMouseAcceleration(int32*, int32*);
168 		bool SetMousePos(long*, long*, long, long);
169 		bool SetMousePos(long*, long*, BPoint);
170 		bool SetMousePos(long*, long*, float, float);
171 
172 		bool SafeMode();
173 
174 		::AddOnManager* AddOnManager() { return fAddOnManager; }
175 
176 		static BList gInputFilterList;
177 		static BLocker gInputFilterListLocker;
178 
179 		static BList gInputMethodList;
180 		static BLocker gInputMethodListLocker;
181 
182 		static KeymapMethod gKeymapMethod;
183 
184 		BRect& ScreenFrame() { return fFrame; }
185 
186 	private:
187 		typedef BApplication _inherited;
188 
189 		status_t _LoadKeymap();
190 		status_t _LoadSystemKeymap();
191 		status_t _SaveKeymap(bool isDefault = false);
192 		void _InitKeyboardMouseStates();
193 
194 		status_t _StartEventLoop();
195 		void _EventLoop();
196 		static status_t _EventLooper(void *arg);
197 
198 		void _UpdateMouseAndKeys(EventList& events);
199 		bool _SanitizeEvents(EventList& events);
200 		bool _MethodizeEvents(EventList& events);
201 		bool _FilterEvents(EventList& events);
202 		void _DispatchEvents(EventList& events);
203 
204 		void _FilterEvent(BInputServerFilter* filter, EventList& events,
205 					int32& index, int32& count);
206 		status_t _DispatchEvent(BMessage* event);
207 
208 		status_t _AcquireInput(BMessage& message, BMessage& reply);
209 		void _ReleaseInput(BMessage* message);
210 
211 	private:
212 		bool 			fEventLoopRunning;
213 		bool 			fSafeMode;
214 		port_id 		fEventPort;
215 
216 		uint16			fKeyboardID;
217 
218 		BList			fInputDeviceList;
219 		BLocker 		fInputDeviceListLocker;
220 
221 		KeyboardSettings fKeyboardSettings;
222 		MouseSettings	fMouseSettings;
223 
224 		BPoint			fMousePos;		// current mouse position
225 		key_info		fKeyInfo;		// current key info
226 		key_map			fKeys;			// current key_map
227 		char*			fChars;			// current keymap chars
228 		uint32			fCharsSize;		// current keymap char count
229 
230 		port_id      	fEventLooperPort;
231 
232 		::AddOnManager*	fAddOnManager;
233 
234 		BScreen			fScreen;
235 		BRect			fFrame;
236 
237 		BLocker			fEventQueueLock;
238 		EventList 		fEventQueue;
239 
240 		BInputServerMethod*	fActiveMethod;
241 		EventList			fMethodQueue;
242 		const BMessenger*	fReplicantMessenger;
243 		BottomlineWindow*	fInputMethodWindow;
244 		bool				fInputMethodAware;
245 
246 		sem_id 			fCursorSem;
247 		port_id			fAppServerPort;
248 		area_id			fCursorArea;
249 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
250 		shared_cursor*	fCursorBuffer;
251 #else
252 		uint32*			fCursorBuffer;
253 #endif
254 
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