xref: /haiku/src/kits/interface/Input.cpp (revision 51978af14a173e7fae0563b562be5603bc652aeb)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		Input.cpp
23 //	Author:			Marc Flerackers (mflerackers@androme.be)
24 //	Description:	Functions and class to manage input devices.
25 //------------------------------------------------------------------------------
26 
27 // Standard Includes -----------------------------------------------------------
28 #include <stdlib.h>
29 #include <string.h>
30 
31 // System Includes -------------------------------------------------------------
32 #include <Input.h>
33 #include <Message.h>
34 #include <Errors.h>
35 #include <List.h>
36 
37 // Project Includes ------------------------------------------------------------
38 
39 // Local Includes --------------------------------------------------------------
40 
41 // Local Defines ---------------------------------------------------------------
42 
43 // Globals ---------------------------------------------------------------------
44 
45 BMessenger *input_server=NULL;
46 
47 _IMPEXP_BE status_t _control_input_server_(BMessage *command, BMessage *reply);
48 
49 /*static bool PrintNameAndDelete(void *ptr)
50 {
51    if(ptr)
52    {
53       BInputDevice *dev = (BInputDevice *)ptr;
54 	  printf("* %s : %s\n", dev->Name(), dev->IsRunning() ? "running" : "not running");
55       delete dev;
56       return false;
57    }
58 
59    return true;
60 }
61 
62 void PrintDevices()
63 {
64 	BList devices;
65 
66 	status_t err = get_input_devices(&devices);
67 
68 	if(err != B_OK)
69 		printf("Error while getting input devices\n");
70 	else
71 	{
72 	   devices.DoForEach(PrintNameAndDelete);
73 	   devices.MakeEmpty();
74 	}
75 }*/
76 
77 //------------------------------------------------------------------------------
78 BInputDevice *find_input_device(const char *name)
79 {
80 	BMessage command('Ifdv');
81 	BMessage reply;
82 
83 	command.AddString("device", name);
84 
85 	status_t err = _control_input_server_(&command, &reply);
86 
87 	if (err != B_OK)
88 		return NULL;
89 
90 	BInputDevice *dev = new BInputDevice;
91 
92 	const char *device;
93 	int32 type;
94 
95 	reply.FindString("device", &device);
96 	reply.FindInt32("type", &type);
97 
98 	dev->set_name_and_type(device, (input_device_type)type);
99 
100 	return dev;
101 }
102 //------------------------------------------------------------------------------
103 status_t get_input_devices(BList *list)
104 {
105 	list->MakeEmpty();
106 
107 	BMessage command('Ifdv');
108 	BMessage reply;
109 
110 	status_t err = _control_input_server_(&command, &reply);
111 
112 	if (err != B_OK)
113 		return err;
114 
115 	const char *name;
116 	int32 type;
117 	int32 i = 0;
118 
119 	while (reply.FindString("device", i, &name) == B_OK)
120 	{
121 		reply.FindInt32("type", i++, &type);
122 
123 		BInputDevice *dev = new BInputDevice;
124 
125 		dev->set_name_and_type(name, (input_device_type)type);
126 
127 		list->AddItem(dev);
128 	}
129 
130 	return err;
131 }
132 //------------------------------------------------------------------------------
133 status_t watch_input_devices(BMessenger target, bool start)
134 {
135 	BMessage command('Iwdv');
136 	BMessage reply;
137 
138 	command.AddMessenger("target", target);
139 	command.AddBool("start", start);
140 
141 	return _control_input_server_(&command, &reply);
142 }
143 //------------------------------------------------------------------------------
144 BInputDevice::~BInputDevice()
145 {
146 	if (fName)
147 		free (fName);
148 }
149 //------------------------------------------------------------------------------
150 const char *BInputDevice::Name() const
151 {
152 	return fName;
153 }
154 //------------------------------------------------------------------------------
155 input_device_type BInputDevice::Type() const
156 {
157 	return fType;
158 }
159 //------------------------------------------------------------------------------
160 bool BInputDevice::IsRunning() const
161 {
162 	if (!fName)
163 		return false;
164 
165 	BMessage command('Idvr');
166 	BMessage reply;
167 
168 	command.AddString("device", fName);
169 
170 	return _control_input_server_(&command, &reply) == B_OK;
171 }
172 //------------------------------------------------------------------------------
173 status_t BInputDevice::Start()
174 {
175 	if (!fName)
176 		return B_ERROR;
177 
178 	BMessage command('Istd');
179 	BMessage reply;
180 
181 	command.AddString("device", fName);
182 
183 	return _control_input_server_(&command, &reply);
184 }
185 //------------------------------------------------------------------------------
186 status_t BInputDevice::Stop()
187 {
188 	if (!fName)
189 		return B_ERROR;
190 
191 	BMessage command('Ispd');
192 	BMessage reply;
193 
194 	command.AddString("device", fName);
195 
196 	return _control_input_server_(&command, &reply);
197 }
198 //------------------------------------------------------------------------------
199 status_t BInputDevice::Control(uint32 code, BMessage *message)
200 {
201 	if (!fName)
202 		return B_ERROR;
203 
204 	BMessage command('Icnd');
205 	BMessage reply;
206 
207 	command.AddString("device", fName);
208 	command.AddInt32("code", code);
209 	command.AddMessage("message", message);
210 
211 	message->MakeEmpty();
212 
213 	status_t err = _control_input_server_(&command, &reply);
214 
215 	if (err == B_OK)
216 		reply.FindMessage("message", message);
217 
218 	return err;
219 }
220 //------------------------------------------------------------------------------
221 status_t BInputDevice::Start(input_device_type type)
222 {
223 	BMessage command('Istd');
224 	BMessage reply;
225 
226 	command.AddInt32("type", type);
227 
228 	return _control_input_server_(&command, &reply);
229 }
230 //------------------------------------------------------------------------------
231 status_t BInputDevice::Stop(input_device_type type)
232 {
233 	BMessage command('Ispd');
234 	BMessage reply;
235 
236 	command.AddInt32("type", type);
237 
238 	return _control_input_server_(&command, &reply);
239 }
240 //------------------------------------------------------------------------------
241 status_t BInputDevice::Control(input_device_type type, uint32 code,
242 									  BMessage *message)
243 {
244 	BMessage command('Icnd');
245 	BMessage reply;
246 
247 	command.AddInt32("type", type);
248 	command.AddInt32("code", code);
249 	command.AddMessage("message", message);
250 
251 	message->MakeEmpty();
252 
253 	status_t err = _control_input_server_(&command, &reply);
254 
255 	if (err == B_OK)
256 		reply.FindMessage("message", message);
257 
258 	return err;
259 }
260 //------------------------------------------------------------------------------
261 BInputDevice::BInputDevice()
262 {
263 	fName = NULL;
264 	fType = B_UNDEFINED_DEVICE;
265 }
266 //------------------------------------------------------------------------------
267 void BInputDevice::set_name_and_type(const char *name, input_device_type type)
268 {
269 	if (fName)
270 	{
271 		free (fName);
272 		fName = NULL;
273 	}
274 
275 	if (name)
276 		fName = strdup(name);
277 
278 	fType = type;
279 }
280 //------------------------------------------------------------------------------
281 status_t _control_input_server_(BMessage *command, BMessage *reply)
282 {
283 	if (!input_server)
284 		input_server = new BMessenger;
285 
286 	if (!input_server->IsValid())
287 		*input_server = BMessenger("Application/x-vnd.Be-input_server", -1, NULL);
288 
289 	status_t err = input_server->SendMessage(command, reply);
290 
291 	if (err != B_OK)
292 		return err;
293 
294 	if (reply->FindInt32("status", err) != B_OK)
295 		return B_ERROR;
296 
297 	return err;
298 }
299 //------------------------------------------------------------------------------
300