Panda3D

inputDevice.I
Go to the documentation of this file.
1 /**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file inputDevice.I
10 * @author rdb
11 * @date 2015-12-11
12 */
13
14 #include "config_device.h"
15
16 /**
17 *
18 */
19 INLINE InputDevice::
20 InputDevice() {
21 _button_events = new ButtonEventList;
22 _pointer_events = new PointerEventList;
23 }
24
25 /**
26 * Returns a human-readable name for the device. Not necessarily unique.
27 */
28 INLINE std::string InputDevice::
29 get_name() const {
30 LightMutexHolder holder(_lock);
31 return _name;
32 }
33
34 /**
35 * Returns a string containing the manufacturer of the device, if this
36 * information is known.
37 */
38 INLINE std::string InputDevice::
39 get_manufacturer() const {
40 LightMutexHolder holder(_lock);
41 return _manufacturer;
42 }
43
44 /**
45 * Returns a string containing the serial number of the device, if this
46 * information is known.
47 */
48 INLINE std::string InputDevice::
49 get_serial_number() const {
50 LightMutexHolder holder(_lock);
51 return _serial_number;
52 }
53
54 /**
55 * Returns a string containing the USB vendor ID of the device, if this
56 * information is known.
57 */
58 INLINE unsigned short InputDevice::
59 get_vendor_id() const {
60 LightMutexHolder holder(_lock);
61 return _vendor_id;
62 }
63
64 /**
65 * Returns a string containing the USB product ID of the device, if this
66 * information is known.
67 */
68 INLINE unsigned short InputDevice::
69 get_product_id() const {
70 LightMutexHolder holder(_lock);
71 return _product_id;
72 }
73
74 /**
75 * Returns true if the device is still connected and able to receive data,
76 * false otherwise. May return false positives.
77 */
78 INLINE bool InputDevice::
79 is_connected() const {
80 LightMutexHolder holder(_lock);
81 return _is_connected;
82 }
83
84 /**
85 * Returns an identification of the general type of device. If this could not
86 * be determined, returns DeviceClass.unknown.
87 */
88 INLINE InputDevice::DeviceClass InputDevice::
89 get_device_class() const {
90 LightMutexHolder holder(_lock);
91 return _device_class;
92 }
93
94 /**
95 * Returns true if the device supports the indicated feature.
96 */
97 INLINE bool InputDevice::
98 has_feature(Feature feature) const {
99 LightMutexHolder holder(_lock);
100 return (_features & (1 << (unsigned int)feature)) != 0;
101 }
102
103 /**
104 * Returns true if this is a pointing device.
105 */
106 INLINE bool InputDevice::
107 has_pointer() const {
108 return has_feature(Feature::pointer);
109 }
110
111 /**
112 * Returns true if the device has a physical keyboard designed for text entry.
113 */
114 INLINE bool InputDevice::
115 has_keyboard() const {
116 return has_feature(Feature::keyboard);
117 }
118
119 /**
120 * Returns true if the device features a tracker that can track position and/or
121 * orientation in 3D space.
122 */
123 INLINE bool InputDevice::
124 has_tracker() const {
125 return has_feature(Feature::tracker);
126 }
127
128 /**
129 * Returns true if the device has vibration motors that can be controlled by
130 * calling set_vibration().
131 */
132 INLINE bool InputDevice::
133 has_vibration() const {
134 return has_feature(Feature::vibration);
135 }
136
137 /**
138 * Returns true if the device may be able to provide information about its
139 * battery life.
140 */
141 INLINE bool InputDevice::
142 has_battery() const {
143 return has_feature(Feature::battery);
144 }
145
146 /**
147 * Returns the TrackerData associated with the input device's tracker. This
148 * only makes sense if has_tracker() also returns true.
149 */
150 INLINE TrackerData InputDevice::
151 get_tracker() const {
152 LightMutexHolder holder(_lock);
153 return _tracker_data;
154 }
155
156 /**
157 * Returns a rough indication of the battery level, ranging from 0 (completely
158 * empty battery) to the indicated max_level value.
159 */
160 INLINE InputDevice::BatteryData InputDevice::
161 get_battery() const {
162 LightMutexHolder holder(_lock);
163 return _battery_data;
164 }
165
166 /**
167 * Returns the number of buttons known to the device. This includes those
168 * buttons whose state has been seen, as well as buttons that have been
169 * associated with a ButtonHandle even if their state is unknown. This number
170 * may change as more buttons are discovered.
171 */
172 INLINE size_t InputDevice::
173 get_num_buttons() const {
174 LightMutexHolder holder(_lock);
175 return _buttons.size();
176 }
177
178 /**
179 * Associates the indicated ButtonHandle with the button of the indicated index
180 * number. When the given button index changes state, a corresponding
181 * ButtonEvent will be generated with the given ButtonHandle. Pass
182 * ButtonHandle::none() to turn off any association.
183 *
184 * It is not necessary to call this if you simply want to query the state of
185 * the various buttons by index number; this is only necessary in order to
186 * generate ButtonEvents when the buttons change state.
187 */
188 INLINE void InputDevice::
189 map_button(size_t index, ButtonHandle button) {
190 LightMutexHolder holder(_lock);
191 if (index >= _buttons.size()) {
192 _buttons.resize(index + 1, ButtonState());
193 }
194
195 _buttons[index].handle = button;
196 }
197
198 /**
199 * Returns the ButtonHandle that was previously associated with the given index
200 * number by a call to map_button(), or ButtonHandle::none() if no button
201 * was associated.
202 */
203 INLINE ButtonHandle InputDevice::
204 get_button_map(size_t index) const {
205 if (index < _buttons.size()) {
206 return _buttons[index].handle;
207 } else {
208 return ButtonHandle::none();
209 }
210 }
211
212 /**
213 * Returns true if the indicated button (identified by its index number) is
214 * currently known to be down, or false if it is up or unknown.
215 */
216 INLINE bool InputDevice::
217 is_button_pressed(size_t index) const {
218 if (index < _buttons.size()) {
219 return (_buttons[index]._state == S_down);
220 } else {
221 return false;
222 }
223 }
224
225 /**
226 * Returns true if the state of the indicated button is known, or false if we
227 * have never heard anything about this particular button.
228 */
229 INLINE bool InputDevice::
230 is_button_known(size_t index) const {
231 if (index < _buttons.size()) {
232 return _buttons[index]._state != S_unknown;
233 } else {
234 return false;
235 }
236 }
237
238 /**
239 * Returns the ButtonState that is set at the given index, or throw an assert
240 * if the index was not found in the list.
241 */
242 INLINE InputDevice::ButtonState InputDevice::
243 get_button(size_t index) const {
244 nassertr_always(index < _buttons.size(), ButtonState());
245 return _buttons[index];
246 }
247
248 /**
249 * Returns the first ButtonState found with the given axis, or throw an assert
250 * if the button handle was not found in the list.
251 */
252 INLINE InputDevice::ButtonState InputDevice::
253 find_button(ButtonHandle handle) const {
254 for (size_t i = 0; i < _buttons.size(); ++i) {
255 if (_buttons[i].handle == handle) {
256 return _buttons[i];
257 }
258 }
259 return ButtonState();
260 }
261
262 /**
263 * Returns the number of analog axes known to the InputDevice. This number
264 * may change as more axes are discovered.
265 */
266 INLINE size_t InputDevice::
267 get_num_axes() const {
268 return _axes.size();
269 }
270
271 /**
272 * Associates the indicated Axis with the axis of the indicated index
273 * number. Pass Axis::none to turn off any association.
274 *
275 * It is not necessary to call this if you simply want to query the state of
276 * the various axes by index number.
277 */
278 INLINE void InputDevice::
279 map_axis(size_t index, InputDevice::Axis axis) {
280 LightMutexHolder holder(_lock);
281 if (index >= _axes.size()) {
282 _axes.resize(index + 1, AxisState());
283 }
284
285 _axes[index].axis = axis;
286 }
287
288 /**
289 * Returns the current position of indicated analog axis (identified by its
290 * index number), or 0.0 if the axis is unknown. The normal range of a
291 * single axis is -1.0 to 1.0.
292 */
293 INLINE double InputDevice::
294 get_axis_value(size_t index) const {
295 if (index < _axes.size()) {
296 return _axes[index].value;
297 } else {
298 return 0.0;
299 }
300 }
301
302 /**
303 * Returns the axis state that is set at the given index, or throw an assert
304 * if the index was not found in the list.
305 */
306 INLINE InputDevice::AxisState InputDevice::
307 get_axis(size_t index) const {
308 nassertr_always(index < _axes.size(), AxisState());
309 return _axes[index];
310 }
311
312 /**
313 * Returns the first AnalogAxis found with the given axis, or throw an assert
314 * if the axis was not found in the list.
315 */
316 INLINE InputDevice::AxisState InputDevice::
317 find_axis(InputDevice::Axis axis) const {
318 for (size_t i = 0; i < _axes.size(); ++i) {
319 if (_axes[i].axis == axis) {
320 return _axes[i];
321 }
322 }
323 return AxisState();
324 }
325
326 /**
327 * Returns true if the state of the indicated analog axis is known, or false
328 * if we have never heard anything about this particular axis.
329 */
330 INLINE bool InputDevice::
331 is_axis_known(size_t index) const {
332 if (index < _axes.size()) {
333 return _axes[index].known;
334 } else {
335 return false;
336 }
337 }
338
339 /**
340 * Sets the strength of the vibration effect, if supported. The values are
341 * clamped to 0-1 range. The first value axes the low-frequency rumble
342 * motor, whereas the second axes the high-frequency motor, if present.
343 */
344 INLINE void InputDevice::
345 set_vibration(double strong, double weak) {
346 LightMutexHolder holder(_lock);
347 do_set_vibration(std::max(std::min(strong, 1.0), 0.0), std::max(std::min(weak, 1.0), 0.0));
348 }
349
350 /**
351 * Enables the generation of mouse-movement events.
352 */
353 INLINE void InputDevice::
354 enable_pointer_events() {
355 LightMutexHolder holder(_lock);
356 _enable_pointer_events = true;
357 }
358
359 /**
360 * Disables the generation of mouse-movement events.
361 */
362 INLINE void InputDevice::
363 disable_pointer_events() {
364 LightMutexHolder holder(_lock);
365 _enable_pointer_events = false;
366 _pointer_events.clear();
367 }
368
369 /**
370 * Called to indicate that the device supports the given feature.
371 * Assumes the lock is held.
372 */
373 INLINE void InputDevice::
374 enable_feature(Feature feature) {
375 _features |= (1 << (unsigned int)feature);
376 }
377
378 /**
379 * Called to indicate that the device has been disconnected or connected from
380 * its host.
381 */
382 INLINE void InputDevice::
383 set_connected(bool connected) {
384 LightMutexHolder holder(_lock);
385 _is_connected = connected;
386 }
387
388 /**
389 *
390 */
391 INLINE InputDevice::ButtonState::
392 ButtonState(ButtonHandle handle) :
393 handle(handle) {
394 }
395
396 /**
397 * True if the button state is currently known.
398 */
399 ALWAYS_INLINE bool InputDevice::ButtonState::
400 is_known() const {
401 return (_state != S_unknown);
402 }
403
404 /**
405 * True if the button is currently known to be pressed.
406 */
407 ALWAYS_INLINE bool InputDevice::ButtonState::
408 is_pressed() const {
409 return (_state == S_down);
410 }
ButtonEventList
Records a set of button events that happened recently.
Definition: buttonEventList.h:33
ButtonHandle
A ButtonHandle represents a single button from any device, including keyboard buttons and mouse butto...
Definition: buttonHandle.h:26
InputDevice::AxisState
Definition: inputDevice.h:164
InputDevice::BatteryData
Definition: inputDevice.h:180
InputDevice::ButtonState
Definition: inputDevice.h:145
InputDevice::ButtonState::is_pressed
is_pressed
True if the button is currently known to be pressed.
Definition: inputDevice.h:156
InputDevice::ButtonState::is_known
is_known
True if the button state is currently known.
Definition: inputDevice.h:155
InputDevice::is_connected
is_connected
Returns true if the device is still connected and able to receive data, false otherwise.
Definition: inputDevice.h:244
InputDevice::get_name
get_name
Returns a human-readable name for the device.
Definition: inputDevice.h:228
InputDevice::has_pointer
bool has_pointer() const
Returns true if this is a pointing device.
Definition: inputDevice.I:107
InputDevice::set_vibration
void set_vibration(double strong, double weak)
Sets the strength of the vibration effect, if supported.
Definition: inputDevice.I:345
InputDevice::get_product_id
get_product_id
Returns a string containing the USB product ID of the device, if this information is known.
Definition: inputDevice.h:240
InputDevice::get_vendor_id
get_vendor_id
Returns a string containing the USB vendor ID of the device, if this information is known.
Definition: inputDevice.h:237
InputDevice::has_battery
has_battery
Returns true if the device may be able to provide information about its battery life.
Definition: inputDevice.h:255
InputDevice::get_num_buttons
get_num_buttons
Returns the number of buttons known to the device.
Definition: inputDevice.h:258
InputDevice::has_vibration
bool has_vibration() const
Returns true if the device has vibration motors that can be controlled by calling set_vibration().
Definition: inputDevice.I:133
InputDevice::get_num_axes
get_num_axes
Returns the number of analog axes known to the InputDevice.
Definition: inputDevice.h:259
InputDevice::is_button_known
bool is_button_known(size_t index) const
Returns true if the state of the indicated button is known, or false if we have never heard anything ...
Definition: inputDevice.I:230
InputDevice::map_axis
void map_axis(size_t index, Axis axis)
Associates the indicated Axis with the axis of the indicated index number.
Definition: inputDevice.I:279
InputDevice::set_connected
void set_connected(bool connected)
Called to indicate that the device has been disconnected or connected from its host.
Definition: inputDevice.I:383
InputDevice::find_button
ButtonState find_button(ButtonHandle handle) const
Returns the first ButtonState found with the given axis, or throw an assert if the button handle was ...
Definition: inputDevice.I:253
InputDevice::has_keyboard
bool has_keyboard() const
Returns true if the device has a physical keyboard designed for text entry.
Definition: inputDevice.I:115
InputDevice::map_button
void map_button(size_t index, ButtonHandle handle)
Associates the indicated ButtonHandle with the button of the indicated index number.
Definition: inputDevice.I:189
InputDevice::get_axis
get_axis
Returns the axis state that is set at the given index, or throw an assert if the index was not found ...
Definition: inputDevice.h:259
InputDevice::has_tracker
has_tracker
Returns true if the device features a tracker that can track position and/or orientation in 3D space.
Definition: inputDevice.h:254
InputDevice::get_button
get_button
Returns the ButtonState that is set at the given index, or throw an assert if the index was not found...
Definition: inputDevice.h:258
InputDevice::find_axis
AxisState find_axis(Axis axis) const
Returns the first AnalogAxis found with the given axis, or throw an assert if the axis was not found ...
Definition: inputDevice.I:317
InputDevice::get_manufacturer
get_manufacturer
Returns a string containing the manufacturer of the device, if this information is known.
Definition: inputDevice.h:231
InputDevice::is_button_pressed
bool is_button_pressed(size_t index) const
Returns true if the indicated button (identified by its index number) is currently known to be down,...
Definition: inputDevice.I:217
InputDevice::get_button_map
ButtonHandle get_button_map(size_t index) const
Returns the ButtonHandle that was previously associated with the given index number by a call to map_...
Definition: inputDevice.I:204
InputDevice::get_battery
get_battery
Returns a rough indication of the battery level, ranging from 0 (completely empty battery) to the ind...
Definition: inputDevice.h:255
InputDevice::get_tracker
get_tracker
Returns the TrackerData associated with the input device's tracker.
Definition: inputDevice.h:254
InputDevice::get_serial_number
get_serial_number
Returns a string containing the serial number of the device, if this information is known.
Definition: inputDevice.h:234
InputDevice::has_feature
bool has_feature(Feature feature) const
Returns true if the device supports the indicated feature.
Definition: inputDevice.I:98
InputDevice::get_device_class
get_device_class
Returns an identification of the general type of device.
Definition: inputDevice.h:248
InputDevice::enable_pointer_events
void enable_pointer_events()
Enables the generation of mouse-movement events.
Definition: inputDevice.I:354
InputDevice::disable_pointer_events
void disable_pointer_events()
Disables the generation of mouse-movement events.
Definition: inputDevice.I:363
InputDevice::get_axis_value
double get_axis_value(size_t index) const
Returns the current position of indicated analog axis (identified by its index number),...
Definition: inputDevice.I:294
InputDevice::is_axis_known
bool is_axis_known(size_t index) const
Returns true if the state of the indicated analog axis is known, or false if we have never heard anyt...
Definition: inputDevice.I:331
LightMutexHolder
Similar to MutexHolder, but for a light mutex.
Definition: lightMutexHolder.h:25
PointerEventList
Records a set of pointer events that happened recently.
Definition: pointerEventList.h:33
TrackerData
Stores the kinds of data that a tracker might output.
Definition: trackerData.h:23
config_device.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.