tcp: Fix 64 bit build with debugging features enabled.
[haiku.git] / src / kits / interface / Input.cpp
blob6b7d7230ac32338c01e0f72c4e45bef08a6c139e
1 /*
2 * Copyright (c) 2001-2008, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
5 * Authors:
6 * Marc Flerackers (mflerackers@androme.be)
7 */
9 //! Functions and class to manage input devices.
11 #include <stdlib.h>
12 #include <string.h>
13 #include <new>
15 #include <Input.h>
16 #include <List.h>
17 #include <Message.h>
19 #include <input_globals.h>
20 #include <InputServerTypes.h>
23 static BMessenger *sInputServer = NULL;
26 BInputDevice *
27 find_input_device(const char *name)
29 BMessage command(IS_FIND_DEVICES);
30 BMessage reply;
32 command.AddString("device", name);
34 status_t err = _control_input_server_(&command, &reply);
36 if (err != B_OK)
37 return NULL;
39 BInputDevice *dev = new (std::nothrow) BInputDevice;
40 if (dev == NULL)
41 return NULL;
43 const char *device;
44 int32 type;
46 reply.FindString("device", &device);
47 reply.FindInt32("type", &type);
49 dev->_SetNameAndType(device, (input_device_type)type);
51 return dev;
55 status_t
56 get_input_devices(BList *list)
58 list->MakeEmpty();
60 BMessage command(IS_FIND_DEVICES);
61 BMessage reply;
63 status_t err = _control_input_server_(&command, &reply);
65 if (err != B_OK)
66 return err;
68 const char *name;
69 int32 type;
70 int32 i = 0;
72 while (reply.FindString("device", i, &name) == B_OK) {
73 reply.FindInt32("type", i++, &type);
75 BInputDevice *dev = new (std::nothrow) BInputDevice;
76 if (dev != NULL) {
77 dev->_SetNameAndType(name, (input_device_type)type);
78 list->AddItem(dev);
82 return err;
86 status_t
87 watch_input_devices(BMessenger target, bool start)
89 BMessage command(IS_WATCH_DEVICES);
90 BMessage reply;
92 command.AddMessenger("target", target);
93 command.AddBool("start", start);
95 return _control_input_server_(&command, &reply);
99 BInputDevice::~BInputDevice()
101 free(fName);
105 const char *
106 BInputDevice::Name() const
108 return fName;
112 input_device_type
113 BInputDevice::Type() const
115 return fType;
119 bool
120 BInputDevice::IsRunning() const
122 if (!fName)
123 return false;
125 BMessage command(IS_IS_DEVICE_RUNNING);
126 BMessage reply;
128 command.AddString("device", fName);
130 return _control_input_server_(&command, &reply) == B_OK;
134 status_t
135 BInputDevice::Start()
137 if (!fName)
138 return B_ERROR;
140 BMessage command(IS_START_DEVICE);
141 BMessage reply;
143 command.AddString("device", fName);
145 return _control_input_server_(&command, &reply);
149 status_t
150 BInputDevice::Stop()
152 if (!fName)
153 return B_ERROR;
155 BMessage command(IS_STOP_DEVICE);
156 BMessage reply;
158 command.AddString("device", fName);
160 return _control_input_server_(&command, &reply);
164 status_t
165 BInputDevice::Control(uint32 code, BMessage *message)
167 if (!fName)
168 return B_ERROR;
170 BMessage command(IS_CONTROL_DEVICES);
171 BMessage reply;
173 command.AddString("device", fName);
174 command.AddInt32("code", code);
175 command.AddMessage("message", message);
177 message->MakeEmpty();
179 status_t err = _control_input_server_(&command, &reply);
181 if (err == B_OK)
182 reply.FindMessage("message", message);
184 return err;
188 status_t
189 BInputDevice::Start(input_device_type type)
191 BMessage command(IS_START_DEVICE);
192 BMessage reply;
194 command.AddInt32("type", type);
196 return _control_input_server_(&command, &reply);
200 status_t
201 BInputDevice::Stop(input_device_type type)
203 BMessage command(IS_STOP_DEVICE);
204 BMessage reply;
206 command.AddInt32("type", type);
208 return _control_input_server_(&command, &reply);
212 status_t
213 BInputDevice::Control(input_device_type type, uint32 code, BMessage *message)
215 BMessage command(IS_CONTROL_DEVICES);
216 BMessage reply;
218 command.AddInt32("type", type);
219 command.AddInt32("code", code);
220 command.AddMessage("message", message);
222 message->MakeEmpty();
224 status_t err = _control_input_server_(&command, &reply);
226 if (err == B_OK)
227 reply.FindMessage("message", message);
229 return err;
233 BInputDevice::BInputDevice()
235 fName(NULL),
236 fType(B_UNDEFINED_DEVICE)
241 void
242 BInputDevice::_SetNameAndType(const char *name, input_device_type type)
244 if (fName) {
245 free(fName);
246 fName = NULL;
249 if (name)
250 fName = strdup(name);
252 fType = type;
256 status_t
257 _control_input_server_(BMessage *command, BMessage *reply)
259 if (!sInputServer) {
260 sInputServer = new (std::nothrow) BMessenger;
261 if (!sInputServer)
262 return B_NO_MEMORY;
265 if (!sInputServer->IsValid())
266 *sInputServer = BMessenger("application/x-vnd.Be-input_server", -1, NULL);
268 status_t err = sInputServer->SendMessage(command, reply, 5000000LL, 5000000LL);
270 if (err != B_OK)
271 return err;
273 if (reply->FindInt32("status", &err) != B_OK)
274 return B_ERROR;
276 return err;