2 * Copyright (c) 2001-2008, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
6 * Marc Flerackers (mflerackers@androme.be)
9 //! Functions and class to manage input devices.
19 #include <input_globals.h>
20 #include <InputServerTypes.h>
23 static BMessenger
*sInputServer
= NULL
;
27 find_input_device(const char *name
)
29 BMessage
command(IS_FIND_DEVICES
);
32 command
.AddString("device", name
);
34 status_t err
= _control_input_server_(&command
, &reply
);
39 BInputDevice
*dev
= new (std::nothrow
) BInputDevice
;
46 reply
.FindString("device", &device
);
47 reply
.FindInt32("type", &type
);
49 dev
->_SetNameAndType(device
, (input_device_type
)type
);
56 get_input_devices(BList
*list
)
60 BMessage
command(IS_FIND_DEVICES
);
63 status_t err
= _control_input_server_(&command
, &reply
);
72 while (reply
.FindString("device", i
, &name
) == B_OK
) {
73 reply
.FindInt32("type", i
++, &type
);
75 BInputDevice
*dev
= new (std::nothrow
) BInputDevice
;
77 dev
->_SetNameAndType(name
, (input_device_type
)type
);
87 watch_input_devices(BMessenger target
, bool start
)
89 BMessage
command(IS_WATCH_DEVICES
);
92 command
.AddMessenger("target", target
);
93 command
.AddBool("start", start
);
95 return _control_input_server_(&command
, &reply
);
99 BInputDevice::~BInputDevice()
106 BInputDevice::Name() const
113 BInputDevice::Type() const
120 BInputDevice::IsRunning() const
125 BMessage
command(IS_IS_DEVICE_RUNNING
);
128 command
.AddString("device", fName
);
130 return _control_input_server_(&command
, &reply
) == B_OK
;
135 BInputDevice::Start()
140 BMessage
command(IS_START_DEVICE
);
143 command
.AddString("device", fName
);
145 return _control_input_server_(&command
, &reply
);
155 BMessage
command(IS_STOP_DEVICE
);
158 command
.AddString("device", fName
);
160 return _control_input_server_(&command
, &reply
);
165 BInputDevice::Control(uint32 code
, BMessage
*message
)
170 BMessage
command(IS_CONTROL_DEVICES
);
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
);
182 reply
.FindMessage("message", message
);
189 BInputDevice::Start(input_device_type type
)
191 BMessage
command(IS_START_DEVICE
);
194 command
.AddInt32("type", type
);
196 return _control_input_server_(&command
, &reply
);
201 BInputDevice::Stop(input_device_type type
)
203 BMessage
command(IS_STOP_DEVICE
);
206 command
.AddInt32("type", type
);
208 return _control_input_server_(&command
, &reply
);
213 BInputDevice::Control(input_device_type type
, uint32 code
, BMessage
*message
)
215 BMessage
command(IS_CONTROL_DEVICES
);
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
);
227 reply
.FindMessage("message", message
);
233 BInputDevice::BInputDevice()
236 fType(B_UNDEFINED_DEVICE
)
242 BInputDevice::_SetNameAndType(const char *name
, input_device_type type
)
250 fName
= strdup(name
);
257 _control_input_server_(BMessage
*command
, BMessage
*reply
)
260 sInputServer
= new (std::nothrow
) BMessenger
;
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);
273 if (reply
->FindInt32("status", &err
) != B_OK
)