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