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