xref: /haiku/src/servers/input/InputServer.h (revision 909af08f4328301fbdef1ffb41f566c3b5bec0c7)
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 <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 		int32 Cookie() { return fCookie; }
103 
104 	private:
105 		BInputServerMethod* fMethod;
106 		char* fName;
107 		uchar fIcon[16*16*1];
108 		const BMenu* fMenu;
109 		BMessenger fMessenger;
110 		int32 fCookie;
111 };
112 
113 class KeymapMethod : public BInputServerMethod {
114 	public:
115 		KeymapMethod();
116 		~KeymapMethod();
117 };
118 
119 class InputServer : public BApplication {
120 	public:
121 		InputServer();
122 		virtual ~InputServer();
123 
124 		virtual void ArgvReceived(int32 argc, char** argv);
125 
126 		virtual bool QuitRequested();
127 		virtual void ReadyToRun();
128 		virtual void MessageReceived(BMessage* message);
129 
130 		void HandleSetMethod(BMessage* message);
131 		status_t HandleGetSetMouseType(BMessage* message, BMessage* reply);
132 		status_t HandleGetSetMouseAcceleration(BMessage* message, BMessage* reply);
133 		status_t HandleGetSetKeyRepeatDelay(BMessage* message, BMessage* reply);
134 		status_t HandleGetKeyInfo(BMessage* message, BMessage* reply);
135 		status_t HandleGetModifiers(BMessage* message, BMessage* reply);
136 		status_t HandleGetModifierKey(BMessage* message, BMessage* reply);
137 		status_t HandleSetModifierKey(BMessage* message, BMessage* reply);
138 		status_t HandleSetKeyboardLocks(BMessage* message, BMessage* reply);
139 		status_t HandleGetSetMouseSpeed(BMessage* message, BMessage* reply);
140 		status_t HandleSetMousePosition(BMessage* message, BMessage* reply);
141 		status_t HandleGetSetMouseMap(BMessage* message, BMessage* reply);
142 		status_t HandleGetSetKeyboardID(BMessage* message, BMessage* reply);
143 		status_t HandleGetSetClickSpeed(BMessage* message, BMessage* reply);
144 		status_t HandleGetSetKeyRepeatRate(BMessage* message, BMessage* reply);
145 		status_t HandleGetSetKeyMap(BMessage* message, BMessage* reply);
146 		status_t HandleFocusUnfocusIMAwareView(BMessage* message, BMessage* reply);
147 
148 		status_t EnqueueDeviceMessage(BMessage* message);
149 		status_t EnqueueMethodMessage(BMessage* message);
150 		status_t SetNextMethod(bool direction);
151 		void SetActiveMethod(BInputServerMethod* method);
152 		const BMessenger* MethodReplicant();
153 		void SetMethodReplicant(const BMessenger *replicant);
154 		bool EventLoopRunning();
155 
156 		status_t GetDeviceInfo(const char* name, input_device_type *_type,
157 					bool *_isRunning = NULL);
158 		status_t GetDeviceInfos(BMessage *msg);
159 		status_t UnregisterDevices(BInputServerDevice& serverDevice,
160 					input_device_ref** devices = NULL);
161 		status_t RegisterDevices(BInputServerDevice& serverDevice,
162 					input_device_ref** devices);
163 		status_t StartStopDevices(const char* name, input_device_type type,
164 					bool doStart);
165 		status_t StartStopDevices(BInputServerDevice& serverDevice, bool start);
166 		status_t ControlDevices(const char *name, input_device_type type,
167 					uint32 code, BMessage* message);
168 
169 		bool SafeMode();
170 
171 		::AddOnManager* AddOnManager() { return fAddOnManager; }
172 
173 		static BList gInputFilterList;
174 		static BLocker gInputFilterListLocker;
175 
176 		static BList gInputMethodList;
177 		static BLocker gInputMethodListLocker;
178 
179 		static KeymapMethod gKeymapMethod;
180 
181 		BRect& ScreenFrame() { return fFrame; }
182 
183 	private:
184 		typedef BApplication _inherited;
185 
186 		status_t _LoadKeymap();
187 		status_t _LoadSystemKeymap();
188 		status_t _SaveKeymap(bool isDefault = false);
189 		void _InitKeyboardMouseStates();
190 
191 		MouseSettings* _RunningMouseSettings();
192 		void _RunningMiceSettings(BList& settings);
193 		MouseSettings* _GetSettingsForMouse(BString mouseName);
194 		status_t _PostMouseControlMessage(int32 code, const BString& mouseName);
195 
196 		status_t _StartEventLoop();
197 		void _EventLoop();
198 		static status_t _EventLooper(void *arg);
199 
200 		void _UpdateMouseAndKeys(EventList& events);
201 		bool _SanitizeEvents(EventList& events);
202 		bool _MethodizeEvents(EventList& events);
203 		bool _FilterEvents(EventList& events);
204 		void _DispatchEvents(EventList& events);
205 
206 		void _FilterEvent(BInputServerFilter* filter, EventList& events,
207 					int32& index, int32& count);
208 		status_t _DispatchEvent(BMessage* event);
209 
210 		status_t _AcquireInput(BMessage& message, BMessage& reply);
211 		void _ReleaseInput(BMessage* message);
212 
213 	private:
214 		uint16			fKeyboardID;
215 
216 		BList			fInputDeviceList;
217 		BLocker 		fInputDeviceListLocker;
218 
219 		KeyboardSettings fKeyboardSettings;
220 		MultipleMouseSettings	fMouseSettings;
221 		MouseSettings	fDefaultMouseSettings;
222 
223 		BPoint			fMousePos;		// current mouse position
224 		key_info		fKeyInfo;		// current key info
225 		key_map			fKeys;			// current key_map
226 		char*			fChars;			// current keymap chars
227 		uint32			fCharsSize;		// current keymap char count
228 
229 		port_id      	fEventLooperPort;
230 
231 		::AddOnManager*	fAddOnManager;
232 
233 		BScreen			fScreen;
234 		BRect			fFrame;
235 
236 		BLocker			fEventQueueLock;
237 		EventList 		fEventQueue;
238 
239 		BInputServerMethod*	fActiveMethod;
240 		EventList			fMethodQueue;
241 		const BMessenger*	fReplicantMessenger;
242 		BottomlineWindow*	fInputMethodWindow;
243 		bool				fInputMethodAware;
244 
245 		sem_id 			fCursorSem;
246 		port_id			fAppServerPort;
247 		team_id			fAppServerTeam;
248 		area_id			fCursorArea;
249 		shared_cursor*	fCursorBuffer;
250 };
251 
252 extern InputServer* gInputServer;
253 
254 #if DEBUG >= 1
255 #	if DEBUG == 2
256 #		undef PRINT
257 		inline void _iprint(const char *fmt, ...) {
258 			FILE* log = fopen("/var/log/input_server.log", "a");
259 			char buf[1024];
260 			va_list ap;
261 			va_start(ap, fmt);
262 			vsprintf(buf, fmt, ap);
263 			va_end(ap);
264 			fputs(buf, log);
265 			fflush(log);
266 			fclose(log);
267         }
268 #		define PRINT(x)	_iprint x
269 #	else
270 #		undef PRINT
271 #		define PRINT(x)	SERIAL_PRINT(x)
272 #	endif
273 #	define PRINTERR(x)		PRINT(x)
274 #	define EXIT()          PRINT(("EXIT %s\n", __PRETTY_FUNCTION__))
275 #	define CALLED()        PRINT(("CALLED %s\n", __PRETTY_FUNCTION__))
276 #else
277 #	define EXIT()          ((void)0)
278 #	define CALLED()        ((void)0)
279 #	define PRINTERR(x)		SERIAL_PRINT(x)
280 #endif
281 
282 #endif	/* INPUT_SERVER_APP_H */
283