Debian Codegraph @

olad/plugin_api/DeviceManager.h

raw
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 *
16 * DeviceManager.h
17 * Interface to the DeviceManager class
18 * Copyright (C) 2005 Simon Newton
19 */
20
21 #ifndef OLAD_PLUGIN_API_DEVICEMANAGER_H_
22 #define OLAD_PLUGIN_API_DEVICEMANAGER_H_
23
24 #include <map>
25 #include <set>
26 #include <string>
27 #include <vector>
28 #include "ola/base/Macro.h"
29 #include "ola/timecode/TimeCode.h"
30 #include "olad/Device.h"
31 #include "olad/Preferences.h"
32
33 namespace ola {
34
35 // pair a device with it's alias
36 class device_alias_pair {
37 public:
38 unsigned int alias;
39 AbstractDevice *device;
40
41 device_alias_pair(unsigned int alias, AbstractDevice *device)
42 : alias(alias), device(device) {}
43 };
44
45 bool operator <(const device_alias_pair& left, const device_alias_pair &right);
46
47 /**
48 * @brief Keeps track of OLA's devices.
49 *
50 * Devices can be identified in one of two ways, by device-id or by alias.
51 * device-ids are strings and are persistent across restarting olad and reload
52 * the plugins. device-ids are the keys used in preference containers to
53 * identify devices.
54 *
55 * Device aliases are unsigned integers and are only valid for the lifetime of
56 * the DeviceManager object. Device aliases are used by users when patching /
57 * controlling a device. since '1' is easier to understand / type
58 * than 5-02050016. If a device is registered, then unregistered, then
59 * registered again, it'll have the same device alias.
60 */
61 class DeviceManager {
62 public:
63 /**
64 * @brief Create a new DeviceManager.
65 * @param prefs_factory the PreferencesFactory to use, ownership is not
66 * transferred.
67 * @param port_manager the PortManager to use, ownership is not transferred.
68 */
69 DeviceManager(PreferencesFactory *prefs_factory,
70 class PortManager *port_manager);
71
72 /**
73 * @brief Destructor.
74 */
75 ~DeviceManager();
76
77 /**
78 * @brief Register a device.
79 * @param device The device to register, ownership is not transferred.
80 * @returns true on success, false on failure.
81 *
82 * During registration, any saved port patchings for this device are restored.
83 */
84 bool RegisterDevice(AbstractDevice *device);
85
86 /**
87 * @brief Unregister a device by id.
88 * @param device_id the id of the device to remove
89 * @returns true on success, false on failure
90 */
91 bool UnregisterDevice(const std::string &device_id);
92
93 /**
94 * @brief Unregister a device by pointer.
95 * @param device a pointer to the device.
96 * @returns true on success, false on failure
97 */
98 bool UnregisterDevice(const AbstractDevice *device);
99
100 /**
101 * @brief Return the number of devices.
102 * @returns the number of devices.
103 */
104 unsigned int DeviceCount() const;
105
106 /**
107 * @brief Return a list of all devices and their aliases.
108 * @returns a vector of device_alias_pairs.
109 */
110 std::vector<device_alias_pair> Devices() const;
111
112 /**
113 * @brief Lookup a device using the device alias.
114 * @returns a pointer to the device or NULL if the device wasn't found.
115 */
116 AbstractDevice *GetDevice(unsigned int alias) const;
117
118 /**
119 * @brief Lookup a device using the device id.
120 * @param unique_id the device id of the device.
121 * @returns a device_alias_pair, if the device isn't found the alias is set to
122 * MISSING_DEVICE_ALIAS and the device pointer is NULL.
123 */
124 device_alias_pair GetDevice(const std::string &unique_id) const;
125
126 /**
127 * @brief Remove all devices.
128 */
129 void UnregisterAllDevices();
130
131
132 /**
133 * @brief Send timecode to all ports which support timecode.
134 * @param timecode the TimeCode information.
135 */
136 void SendTimeCode(const ola::timecode::TimeCode &timecode);
137
138 static const unsigned int MISSING_DEVICE_ALIAS;
139
140 private:
141 typedef std::map<std::string, device_alias_pair> DeviceIdMap;
142 typedef std::map<unsigned int, AbstractDevice*> DeviceAliasMap;
143
144 Preferences *m_port_preferences;
145 class PortManager *m_port_manager;
146
147 DeviceIdMap m_devices;
148 DeviceAliasMap m_alias_map;
149
150 unsigned int m_next_device_alias;
151 std::set<class OutputPort*> m_timecode_ports;
152
153 void ReleaseDevice(const AbstractDevice *device);
154 void RestoreDevicePortSettings(AbstractDevice *device);
155
156 template <class PortClass>
157 void SavePortPatchings(const std::vector<PortClass*> &ports) const;
158
159 void SavePortPriority(const Port &port) const;
160 void RestorePortPriority(Port *port) const;
161
162 template <class PortClass>
163 void RestorePortSettings(const std::vector<PortClass*> &ports) const;
164
165 static const char PORT_PREFERENCES[];
166 static const unsigned int FIRST_DEVICE_ALIAS = 1;
167 static const char PRIORITY_VALUE_SUFFIX[];
168 static const char PRIORITY_MODE_SUFFIX[];
169
170 DISALLOW_COPY_AND_ASSIGN(DeviceManager);
171 };
172 } // namespace ola
173 #endif // OLAD_PLUGIN_API_DEVICEMANAGER_H_