xref: /haiku/src/kits/interface/Input.cpp (revision 24159a0c7d6d6dcba9f2a0c1a7c08d2c8167f21b)
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 #include <stdlib.h>
27 #include <string.h>
28 
29 #include <Input.h>
30 #include <List.h>
31 #include <Message.h>
32 
33 #include <input_globals.h>
34 #include <InputServerTypes.h>
35 
36 
37 static BMessenger *sInputServer = NULL;
38 
39 
40 BInputDevice *
41 find_input_device(const char *name)
42 {
43 	BMessage command(IS_FIND_DEVICES);
44 	BMessage reply;
45 
46 	command.AddString("device", name);
47 
48 	status_t err = _control_input_server_(&command, &reply);
49 
50 	if (err != B_OK)
51 		return NULL;
52 
53 	BInputDevice *dev = new BInputDevice;
54 
55 	const char *device;
56 	int32 type;
57 
58 	reply.FindString("device", &device);
59 	reply.FindInt32("type", &type);
60 
61 	dev->set_name_and_type(device, (input_device_type)type);
62 
63 	return dev;
64 }
65 
66 
67 status_t
68 get_input_devices(BList *list)
69 {
70 	list->MakeEmpty();
71 
72 	BMessage command(IS_FIND_DEVICES);
73 	BMessage reply;
74 
75 	status_t err = _control_input_server_(&command, &reply);
76 
77 	if (err != B_OK)
78 		return err;
79 
80 	const char *name;
81 	int32 type;
82 	int32 i = 0;
83 
84 	while (reply.FindString("device", i, &name) == B_OK) {
85 		reply.FindInt32("type", i++, &type);
86 
87 		BInputDevice *dev = new BInputDevice;
88 
89 		dev->set_name_and_type(name, (input_device_type)type);
90 
91 		list->AddItem(dev);
92 	}
93 
94 	return err;
95 }
96 
97 
98 status_t
99 watch_input_devices(BMessenger target, bool start)
100 {
101 	BMessage command(IS_WATCH_DEVICES);
102 	BMessage reply;
103 
104 	command.AddMessenger("target", target);
105 	command.AddBool("start", start);
106 
107 	return _control_input_server_(&command, &reply);
108 }
109 
110 
111 BInputDevice::~BInputDevice()
112 {
113 	free(fName);
114 }
115 
116 
117 const char *
118 BInputDevice::Name() const
119 {
120 	return fName;
121 }
122 
123 
124 input_device_type
125 BInputDevice::Type() const
126 {
127 	return fType;
128 }
129 
130 
131 bool
132 BInputDevice::IsRunning() const
133 {
134 	if (!fName)
135 		return false;
136 
137 	BMessage command(IS_IS_DEVICE_RUNNING);
138 	BMessage reply;
139 
140 	command.AddString("device", fName);
141 
142 	return _control_input_server_(&command, &reply) == B_OK;
143 }
144 
145 
146 status_t
147 BInputDevice::Start()
148 {
149 	if (!fName)
150 		return B_ERROR;
151 
152 	BMessage command(IS_START_DEVICE);
153 	BMessage reply;
154 
155 	command.AddString("device", fName);
156 
157 	return _control_input_server_(&command, &reply);
158 }
159 
160 
161 status_t
162 BInputDevice::Stop()
163 {
164 	if (!fName)
165 		return B_ERROR;
166 
167 	BMessage command(IS_STOP_DEVICE);
168 	BMessage reply;
169 
170 	command.AddString("device", fName);
171 
172 	return _control_input_server_(&command, &reply);
173 }
174 
175 
176 status_t
177 BInputDevice::Control(uint32 code, BMessage *message)
178 {
179 	if (!fName)
180 		return B_ERROR;
181 
182 	BMessage command(IS_CONTROL_DEVICES);
183 	BMessage reply;
184 
185 	command.AddString("device", fName);
186 	command.AddInt32("code", code);
187 	command.AddMessage("message", message);
188 
189 	message->MakeEmpty();
190 
191 	status_t err = _control_input_server_(&command, &reply);
192 
193 	if (err == B_OK)
194 		reply.FindMessage("message", message);
195 
196 	return err;
197 }
198 
199 
200 status_t
201 BInputDevice::Start(input_device_type type)
202 {
203 	BMessage command(IS_START_DEVICE);
204 	BMessage reply;
205 
206 	command.AddInt32("type", type);
207 
208 	return _control_input_server_(&command, &reply);
209 }
210 
211 
212 status_t
213 BInputDevice::Stop(input_device_type type)
214 {
215 	BMessage command(IS_STOP_DEVICE);
216 	BMessage reply;
217 
218 	command.AddInt32("type", type);
219 
220 	return _control_input_server_(&command, &reply);
221 }
222 
223 
224 status_t
225 BInputDevice::Control(input_device_type type, uint32 code,
226 									  BMessage *message)
227 {
228 	BMessage command(IS_CONTROL_DEVICES);
229 	BMessage reply;
230 
231 	command.AddInt32("type", type);
232 	command.AddInt32("code", code);
233 	command.AddMessage("message", message);
234 
235 	message->MakeEmpty();
236 
237 	status_t err = _control_input_server_(&command, &reply);
238 
239 	if (err == B_OK)
240 		reply.FindMessage("message", message);
241 
242 	return err;
243 }
244 
245 
246 BInputDevice::BInputDevice()
247 {
248 	fName = NULL;
249 	fType = B_UNDEFINED_DEVICE;
250 }
251 
252 
253 void
254 BInputDevice::set_name_and_type(const char *name, input_device_type type)
255 {
256 	if (fName) {
257 		free (fName);
258 		fName = NULL;
259 	}
260 
261 	if (name)
262 		fName = strdup(name);
263 
264 	fType = type;
265 }
266 
267 
268 status_t
269 _control_input_server_(BMessage *command, BMessage *reply)
270 {
271 	if (!sInputServer)
272 		sInputServer = new BMessenger;
273 
274 	if (!sInputServer->IsValid())
275 		*sInputServer = BMessenger("application/x-vnd.Be-input_server", -1, NULL);
276 
277 	status_t err = sInputServer->SendMessage(command, reply);
278 
279 	if (err != B_OK)
280 		return err;
281 
282 	if (reply->FindInt32("status", &err) != B_OK)
283 		return B_ERROR;
284 
285 	return err;
286 }
287