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  */
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  */
114  {
115  LightMutexHolder holder(_lock);
116  _connected_devices.remove_device(device);
117  }
118 
119  throw_event("disconnect-device", device);
120 }
121 
122 /**
123  * Polls the system to see if there are any new devices. In some
124  * implementations this is a no-op.
125  */
128 }
void init_libputil()
Initializes the library.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
get_device_class
Returns an identification of the general type of device.
Definition: inputDevice.h:242
This class keeps track of all the devices on a system, and sends out events when a device has been ho...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Manages a list of InputDevice objects, as returned by various InputDeviceManager methods.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void update()
Polls the system to see if there are any new devices.
void remove_device(InputDevice *device)
Called when a device has been removed, or when a device should otherwise no longer be tracked.
bool remove_device(InputDevice *device)
Removes the indicated InputDevice from the collection.
size_t size() const
Returns the number of devices in the collection.
This is a structure representing a single input device.
Definition: inputDevice.h:53
Similar to MutexHolder, but for a light mutex.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void add_device(InputDevice *device)
Adds a new InputDevice to the collection.
void add_device(InputDevice *device)
Called when a new device has been discovered.
InputDeviceSet get_devices() const
Description: Returns all currently connected devices.