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