Loading...
Searching...
No Matches
DeviceInterface.hpp
1#pragma once
2#include <Device/Node/DeviceNode.hpp>
3#include <Device/Protocol/DeviceSettings.hpp>
4
5#include <ossia/detail/callback_container.hpp>
6#include <ossia/network/base/value_callback.hpp>
7
8#include <ossia-qt/device_metatype.hpp>
9
10#include <nano_signal_slot.hpp>
11#include <score_lib_device_export.h>
12
13#include <verdigris>
14class QMenu;
15namespace score
16{
17struct DocumentContext;
18}
19namespace ossia
20{
21class value;
22}
23
24namespace ossia::net
25{
26class node_base;
27class parameter_base;
28class device_base;
29struct network_context;
30using network_context_ptr = std::shared_ptr<network_context>;
31}
32namespace State
33{
34struct Message;
35}
36namespace Device
37{
38struct FullAddressSettings;
39
40struct SCORE_LIB_DEVICE_EXPORT DeviceCapas
41{
42 bool canAddNode{true};
43 bool canRemoveNode{true};
44 bool canRenameNode{true};
45 bool canSetProperties{true};
46 bool canDisconnect{true};
47 bool canRefreshValue{true};
48 bool canRefreshTree{false};
49 bool asyncConnect{false};
50 bool canListen{true};
51 bool canSerialize{true};
52 bool canLearn{false};
53 bool hasCallbacks{true};
54};
55
56enum DeviceLogging : int8_t
57{
58 LogNothing,
59 LogUnfolded,
60 LogEverything
61};
62
63class SCORE_LIB_DEVICE_EXPORT DeviceInterface
64 : public QObject
65 , public Nano::Observer
66{
67 W_OBJECT(DeviceInterface)
68
69public:
71 virtual ~DeviceInterface();
72
73 const Device::DeviceSettings& settings() const noexcept;
74 const QString& name() const noexcept;
75
76 virtual void addNode(const Device::Node& n);
77
78 DeviceCapas capabilities() const noexcept;
79 virtual DeviceResources usedResources() const noexcept;
80
81 virtual void disconnect();
82 virtual bool reconnect() = 0;
83 virtual void recreate(const Device::Node&); // Argument is the node of the
84 // device, used for recreation
85 virtual bool connected() const;
86 W_INVOKABLE(connected)
87
88 void updateSettings(const Device::DeviceSettings&);
89
90 // Asks, and returns all the new addresses if the device can refresh itself
91 // Minuit-like.
92 // The addresses are not applied to the device, they have to be via a
93 // command!
94 virtual Device::Node refresh();
95 std::optional<ossia::value> refresh(const State::Address&);
96 void request(const Device::Node&);
97 void setListening(const State::Address&, bool);
98 void addToListening(const std::vector<State::Address>&);
99 std::vector<State::Address> listening() const;
100
101 virtual void addAddress(const Device::FullAddressSettings&);
102 virtual void updateAddress(
103 const State::Address& currentAddr, const Device::FullAddressSettings& newAddr);
104 virtual void removeNode(const State::Address&);
105
106 void sendMessage(const State::Address& addr, const ossia::value& v);
107
108 // Make a node from an inside path, if it has been added for instance.
109 Device::Node getNode(const State::Address&) const;
110 Device::Node getNodeWithoutChildren(const State::Address&) const;
111
112 bool isLogging() const;
113 void setLogging(DeviceLogging);
114
115 virtual ossia::net::device_base* getDevice() const = 0;
116
117 virtual bool isLearning() const;
118 virtual void setLearning(bool);
119
120 virtual QMimeData* mimeData() const;
121 virtual void setupContextMenu(QMenu&) const;
122
123 void nodeCreated(const ossia::net::node_base&);
124 void nodeRemoving(const ossia::net::node_base&);
125 void nodeRenamed(const ossia::net::node_base&, std::string);
126 void addressCreated(const ossia::net::parameter_base&);
127 void addressUpdated(const ossia::net::node_base&, ossia::string_view key);
128 void addressRemoved(const ossia::net::parameter_base& addr);
129
130 Nano::Signal<void(const State::Address&, const ossia::value&)> valueUpdated;
131
132public:
133 // These signals are emitted if a device changes from the inside
134 void pathAdded(const State::Address& arg_1)
135 E_SIGNAL(SCORE_LIB_DEVICE_EXPORT, pathAdded, arg_1)
136 void pathUpdated(
137 const State::Address& arg_1, // current address
138 const Device::AddressSettings& arg_2)
139 E_SIGNAL(SCORE_LIB_DEVICE_EXPORT, pathUpdated, arg_1, arg_2) // new data
140 void pathRemoved(const State::Address& arg_1)
141 E_SIGNAL(SCORE_LIB_DEVICE_EXPORT, pathRemoved, arg_1)
142
143 // In case the whole namespace changed?
144 void namespaceUpdated() E_SIGNAL(SCORE_LIB_DEVICE_EXPORT, namespaceUpdated)
145
146 // In case the device changed
147 void deviceChanged(ossia::net::device_base* old_dev, ossia::net::device_base* new_dev)
148 E_SIGNAL(SCORE_LIB_DEVICE_EXPORT, deviceChanged, old_dev, new_dev)
149
150 /* If logging is enabled, these two signals may be sent
151 * when something happens */
152 void logInbound(const QString& arg_1) const
153 E_SIGNAL(SCORE_LIB_DEVICE_EXPORT, logInbound, arg_1)
154 void logOutbound(const QString& arg_1) const
155 E_SIGNAL(SCORE_LIB_DEVICE_EXPORT, logOutbound, arg_1)
156
157 void connectionChanged(bool arg_1) const
158 E_SIGNAL(SCORE_LIB_DEVICE_EXPORT, connectionChanged, arg_1)
159
160protected:
161 Device::DeviceSettings m_settings;
162 DeviceCapas m_capas;
163
164 using callback_pair = std::pair<
165 ossia::net::parameter_base*,
166 ossia::callback_container<ossia::value_callback>::iterator>;
167 score::hash_map<State::Address, callback_pair> m_callbacks;
168
169 void removeListening_impl(ossia::net::node_base& node, State::Address addr);
170 void removeListening_impl(
171 ossia::net::node_base& node, State::Address addr, std::vector<State::Address>&);
172 void renameListening_impl(const State::Address& parent, const QString& newName);
173 void setLogging_impl(DeviceLogging) const;
174 void enableCallbacks();
175 void disableCallbacks();
176
177 // Refresh without handling callbacks
178 Device::Node simple_refresh();
179
180private:
181 DeviceLogging m_logging = DeviceLogging::LogNothing;
182 bool m_callbacksEnabled = false;
183};
184
185class SCORE_LIB_DEVICE_EXPORT OwningDeviceInterface : public DeviceInterface
186{
187public:
188 ~OwningDeviceInterface() override;
189 void replaceDevice(ossia::net::device_base*);
190 void releaseDevice();
191
192protected:
193 void disconnect() override;
194
195 using DeviceInterface::DeviceInterface;
196
197 ossia::net::device_base* getDevice() const final override { return m_dev.get(); }
198
199 std::shared_ptr<ossia::net::device_base> m_dev;
200 bool m_owned{true};
201};
202
203SCORE_LIB_DEVICE_EXPORT ossia::net::node_base*
204getNodeFromPath(const QStringList& path, ossia::net::device_base& dev);
205
206SCORE_LIB_DEVICE_EXPORT ossia::net::node_base*
207createNodeFromPath(const QStringList& path, ossia::net::device_base& dev);
208
209SCORE_LIB_DEVICE_EXPORT Device::Node ToDeviceExplorer(const ossia::net::node_base& node);
210
211SCORE_LIB_DEVICE_EXPORT ossia::net::node_base*
212findNodeFromPath(const Device::Node& path, ossia::net::device_base& dev);
213
214SCORE_LIB_DEVICE_EXPORT ossia::net::node_base*
215findNodeFromPath(const QStringList& path, ossia::net::device_base& dev);
216
217SCORE_LIB_DEVICE_EXPORT
218void releaseDevice(
219 ossia::net::network_context& ctx, std::unique_ptr<ossia::net::device_base> dev);
220SCORE_LIB_DEVICE_EXPORT
221void releaseDevice(
222 ossia::net::network_context& ctx, std::shared_ptr<ossia::net::device_base> dev);
223}
Definition DeviceInterface.hpp:66
Definition DeviceInterface.hpp:186
Manipulation of Devices from Qt.
Definition AddressSettings.cpp:14
Utilities for OSSIA data structures.
Definition DeviceInterface.hpp:33
Base toolkit upon which the software is built.
Definition Application.cpp:113
Definition AddressSettings.hpp:49
Definition DeviceInterface.hpp:41
Definition DeviceSettings.hpp:20
Definition AddressSettings.hpp:62
The Address struct.
Definition Address.hpp:58
The Message struct.
Definition Message.hpp:15