xref: /haiku/src/servers/input/InputServer.h (revision 93aeb8c3bc3f13cb1f282e3e749258a23790d947)
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 UnregisterDevices(BInputServerDevice& serverDevice,
139 					input_device_ref** devices = NULL);
140 		status_t RegisterDevices(BInputServerDevice& serverDevice,
141 					input_device_ref** devices);
142 		status_t StartStopDevices(const char* name, input_device_type type,
143 					bool doStart);
144 		status_t StartStopDevices(BInputServerDevice& serverDevice, bool start);
145 		status_t ControlDevices(const char *name, input_device_type type,
146 					uint32 code, BMessage* message);
147 
148 		bool DoMouseAcceleration(int32*, int32*);
149 		bool SetMousePos(long*, long*, long, long);
150 		bool SetMousePos(long*, long*, BPoint);
151 		bool SetMousePos(long*, long*, float, float);
152 
153 		bool SafeMode();
154 
155 		static BList gInputFilterList;
156 		static BLocker gInputFilterListLocker;
157 
158 		static BList gInputMethodList;
159 		static BLocker gInputMethodListLocker;
160 
161 		static DeviceManager gDeviceManager;
162 
163 		static KeymapMethod gKeymapMethod;
164 
165 		BRect& ScreenFrame() { return fFrame; }
166 
167 	private:
168 		typedef BApplication _inherited;
169 
170 		status_t _LoadKeymap();
171 		status_t _LoadSystemKeymap();
172 		void _InitKeyboardMouseStates();
173 
174 		status_t _StartEventLoop();
175 		void _EventLoop();
176 		static status_t _EventLooper(void *arg);
177 
178 		bool _SanitizeEvents(EventList& events);
179 		bool _MethodizeEvents(EventList& events);
180 		bool _FilterEvents(EventList& events);
181 		void _DispatchEvents(EventList& events);
182 
183 		void _FilterEvent(BInputServerFilter* filter, EventList& events,
184 					int32& index, int32& count);
185 		status_t _DispatchEvent(BMessage* event);
186 
187 		status_t _AcquireInput(BMessage& message, BMessage& reply);
188 		void _ReleaseInput(BMessage* message);
189 
190 		InputDeviceListItem* _FindInputDeviceListItem(BInputServerDevice& device);
191 
192 	private:
193 		bool 			fEventLoopRunning;
194 		bool 			fSafeMode;
195 		port_id 		fEventPort;
196 
197 		uint16			fKeyboardID;
198 
199 		BList			fInputDeviceList;
200 		BLocker 		fInputDeviceListLocker;
201 
202 		KeyboardSettings fKeyboardSettings;
203 		MouseSettings	fMouseSettings;
204 
205 		BPoint			fMousePos;		// current mouse position
206 		key_info		fKeyInfo;		// current key info
207 		key_map			fKeys;			// current key_map
208 		char*			fChars;			// current keymap chars
209 		uint32			fCharsSize;		// current keymap char count
210 
211 		port_id      	fEventLooperPort;
212 
213 		AddOnManager*	fAddOnManager;
214 
215 		BScreen			fScreen;
216 		BRect			fFrame;
217 
218 		BLocker			fEventQueueLock;
219 		EventList 		fEventQueue;
220 
221 		BInputServerMethod*	fActiveMethod;
222 		EventList			fMethodQueue;
223 		const BMessenger*	fReplicantMessenger;
224 		BottomlineWindow*	fInputMethodWindow;
225 		bool				fInputMethodAware;
226 
227 		sem_id 			fCursorSem;
228 		port_id			fAppServerPort;
229 		area_id			fCursorArea;
230 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
231 		shared_cursor*	fCursorBuffer;
232 #else
233 		uint32*			fCursorBuffer;
234 #endif
235 
236 #if DEBUG == 2
237 	public:
238 		static FILE *sLogFile;
239 #endif
240 };
241 
242 extern InputServer* gInputServer;
243 
244 #if DEBUG >= 1
245 #	if DEBUG == 2
246 #		undef PRINT
247         inline void _iprint(const char *fmt, ...) { char buf[1024]; va_list ap; va_start(ap, fmt); vsprintf(buf, fmt, ap); va_end(ap); \
248                 fputs(buf, InputServer::sLogFile); fflush(InputServer::sLogFile); }
249 #		define PRINT(x)	_iprint x
250 #	else
251 #		undef PRINT
252 #		define PRINT(x)	SERIAL_PRINT(x)
253 #	endif
254 #	define PRINTERR(x)		PRINT(x)
255 #	define EXIT()          PRINT(("EXIT %s\n", __PRETTY_FUNCTION__))
256 #	define CALLED()        PRINT(("CALLED %s\n", __PRETTY_FUNCTION__))
257 #else
258 #	define EXIT()          ((void)0)
259 #	define CALLED()        ((void)0)
260 #	define PRINTERR(x)		SERIAL_PRINT(x)
261 #endif
262 
263 #endif	/* INPUT_SERVER_APP_H */
264