Panda3D
inputDeviceManager.cxx
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 inputDeviceManager.cxx
10  * @author rdb
11  * @date 2015-12-09
12  */
13 
14 #include "inputDeviceManager.h"
17 #include "winInputDeviceManager.h"
18 #include "throw_event.h"
19 #include "config_putil.h"
20 
21 InputDeviceManager *InputDeviceManager::_global_ptr = nullptr;
22 
23 /**
24  * Initializes the input device manager by scanning which devices are currently
25  * connected and setting up any platform-dependent structures necessary for
26  * listening for future device connect events.
27  */
28 InputDeviceManager::
29 InputDeviceManager() : _lock("InputDeviceManager") {
30 }
31 
32 /**
33  * Creates the global input manager.
34  */
35 void InputDeviceManager::
36 make_global_ptr() {
37  init_libputil();
38 
39 #ifdef _WIN32
40  _global_ptr = new WinInputDeviceManager;
41 #elif defined(__APPLE__)
42  _global_ptr = new IOKitInputDeviceManager;
43 #elif defined(PHAVE_LINUX_INPUT_H)
44  _global_ptr = new LinuxInputDeviceManager;
45 #else
46  _global_ptr = new InputDeviceManager;
47 #endif
48 }
49 
50 /**
51  * Description: Returns all currently connected devices.
52  */
54 get_devices() const {
55  InputDeviceSet devices;
56  LightMutexHolder holder(_lock);
57 
58  for (size_t i = 0; i < _connected_devices.size(); ++i) {
59  InputDevice *device = _connected_devices[i];
60  devices.add_device(device);
61  }
62 
63  return devices;
64 }
65 
66 /**
67  * Description: Returns all currently connected devices of the given device class.
68  */
70 get_devices(InputDevice::DeviceClass device_class) const {
71  InputDeviceSet devices;
72  LightMutexHolder holder(_lock);
73 
74  for (size_t i = 0; i < _connected_devices.size(); ++i) {
75  InputDevice *device = _connected_devices[i];
76  if (device->get_device_class() == device_class) {
77  devices.add_device(device);
78  }
79  }
80 
81  return devices;
82 }
83 
84 
85 /**
86  * Called when a new device has been discovered. This may also be used to
87  * register virtual devices.
88  *
89  * This causes a connect-device event to be thrown.
90  */
92 add_device(InputDevice *device) {
93  {
94  LightMutexHolder holder(_lock);
95  _connected_devices.add_device(device);
96 
97 #ifdef PHAVE_LINUX_INPUT_H
98  // If we had added it pending activity on the device, remove it
99  // from the list of inactive devices.
100  _inactive_devices.remove_device(device);
101 #endif
102  }
103  throw_event("connect-device", device);
104 }
105 
106 /**
107  * Called when a device has been removed, or when a device should otherwise no
108  * longer be tracked.
109  *
110  * This causes a disconnect-device event to be thrown.
111  */
113 remove_device(InputDevice *device) {
114  // We need to hold a reference, since remove_device decrements the refcount.
115  PT(InputDevice) device_ref = device;
116  {
117  LightMutexHolder holder(_lock);
118  _connected_devices.remove_device(device);
119  }
120 
121  throw_event("disconnect-device", device);
122 }
123 
124 /**
125  * Polls the system to see if there are any new devices. In some
126  * implementations this is a no-op.
127  */
129 update() {
130 }
inputDeviceManager.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
LightMutexHolder
Similar to MutexHolder, but for a light mutex.
Definition: lightMutexHolder.h:25
config_putil.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
throw_event.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
InputDeviceManager::remove_device
void remove_device(InputDevice *device)
Called when a device has been removed, or when a device should otherwise no longer be tracked.
Definition: inputDeviceManager.cxx:113
ioKitInputDeviceManager.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
InputDeviceSet::add_device
void add_device(InputDevice *device)
Adds a new InputDevice to the collection.
Definition: inputDeviceSet.cxx:45
init_libputil
void init_libputil()
Initializes the library.
Definition: config_putil.cxx:185
InputDevice::get_device_class
get_device_class
Returns an identification of the general type of device.
Definition: inputDevice.h:248
InputDevice
This is a structure representing a single input device.
Definition: inputDevice.h:53
InputDeviceManager
This class keeps track of all the devices on a system, and sends out events when a device has been ho...
Definition: inputDeviceManager.h:33
InputDeviceManager::update
virtual void update()
Polls the system to see if there are any new devices.
Definition: inputDeviceManager.cxx:129
InputDeviceSet::remove_device
bool remove_device(InputDevice *device)
Removes the indicated InputDevice from the collection.
Definition: inputDeviceSet.cxx:54
linuxInputDeviceManager.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
winInputDeviceManager.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
InputDeviceSet::size
size_t size() const
Returns the number of devices in the collection.
Definition: inputDeviceSet.I:34
InputDeviceSet
Manages a list of InputDevice objects, as returned by various InputDeviceManager methods.
Definition: inputDeviceSet.h:26
InputDeviceManager::add_device
void add_device(InputDevice *device)
Called when a new device has been discovered.
Definition: inputDeviceManager.cxx:92
InputDeviceManager::get_devices
InputDeviceSet get_devices() const
Description: Returns all currently connected devices.
Definition: inputDeviceManager.cxx:54