-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathOscInterface.cpp
More file actions
331 lines (291 loc) · 9.31 KB
/
Copy pathOscInterface.cpp
File metadata and controls
331 lines (291 loc) · 9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* OscInterface.cpp
*
* Copyright (c) 2010 Alexandre Quessy <alexandre@quessy.net>
* Copyright (c) 2010 Tristan Matthews <le.businessman@gmail.com>
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
* (c) 2020 Alexandre Quessy -- alexandre(@)quessy(.)net
* (c) 2026 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "OscInterface.h"
#include "MainWindow.h"
#include <QVariant>
#include <QColor>
#include <QPointF>
namespace mmp {
namespace {
/// Returns the source with the given id, or a null pointer, WITHOUT mutating
/// the manager (MappingManager::getSourceById() would insert a null entry for
/// an unknown id, since QMap::operator[] is non-const there).
Source::ptr findSourceById(MappingManager& manager, int id)
{
for (int i = 0; i < manager.nSources(); ++i)
{
Source::ptr source = manager.getSource(i);
if (!source.isNull() && static_cast<int>(source->getId()) == id)
return source;
}
return Source::ptr();
}
/// Resolves the source(s) targeted by an action (by id or by name pattern).
QVector<Source::ptr> resolveSources(MappingManager& manager, const OscAction& action)
{
if (action.selectByName)
return manager.getSourcesByNameRegExp(action.name);
QVector<Source::ptr> sources;
Source::ptr source = findSourceById(manager, action.id);
if (!source.isNull())
sources.push_back(source);
return sources;
}
/// Resolves the layer(s) targeted by an action (by id or by name pattern).
QVector<Layer::ptr> resolveLayers(MappingManager& manager, const OscAction& action)
{
if (action.selectByName)
return manager.getLayersByNameRegExp(action.name);
QVector<Layer::ptr> layers;
Layer::ptr layer = manager.getLayerById(action.id);
if (!layer.isNull())
layers.push_back(layer);
return layers;
}
} // namespace
OscInterface::OscInterface(
int listen_port, bool acceptFromNetwork) :
receiver_(listen_port, acceptFromNetwork),
messaging_queue_() {
receiving_enabled_ = true;
if (receiving_enabled_) {
qDebug() << "Listening osc.udp://localhost:" << listen_port;
// setup handler
QObject::connect(&receiver_, &OscReceiver::messageReceived, [=](const QString& oscAddress, const QVariantList& arguments) {
this->messageReceivedCb(oscAddress, arguments);
});
}
}
OscInterface::~OscInterface() {
// pass
}
void OscInterface::push_command(QVariantList command)
{
messaging_queue_.push(command);
}
void OscInterface::consume_commands(MainWindow &main_window)
{
bool success = true;
while (success)
{
QVariantList command;
success = messaging_queue_.try_pop(command);
if (success)
{
this->applyOscCommand(main_window, command);
}
}
}
void OscInterface::start()
{
}
void OscInterface::messageReceivedCb(const QString& oscAddress, const QVariantList& arguments) {
QVariantList command;
command.append(QVariant(oscAddress));
QString types = "";
for (int i = 0; i < arguments.count(); ++ i) {
QVariant argument = arguments[i];
QMetaType::Type type = static_cast<QMetaType::Type>(argument.typeId());
if (type == QMetaType::Int) {
types += "i";
} else if (type == QMetaType::Float) {
types += "f";
} else if (type == QMetaType::Double) {
types += "f";
} else if (type == QMetaType::QString) {
types += "s";
} else if (type == QMetaType::Bool) {
if (argument.toBool()) {
types += "T";
} else {
types += "F";
}
} else {
qDebug() << "Unhandled OSC argument type " << argument.typeName();
}
// TODO: implement other OSC types
}
command.append(QVariant(types));
for (int i = 0; i < arguments.size(); ++i)
{
QVariant argument = arguments[i];
command.append(argument);
}
this->push_command(command);
}
static void printCommand(QVariantList &command)
{
for (int i = 0; i < command.size(); ++i)
{
if (command.at(i).typeId() == QMetaType::Int)
{
qDebug() << command.at(i).toInt() << " ";
}
else if (command.at(i).typeId() == QMetaType::Double)
{
qDebug() << command.at(i).toDouble() << " ";
}
else if (command.at(i).typeId() == QMetaType::QString)
{
qDebug() << command.at(i).toString() << " ";
}
else
{
qDebug() << "(?) ";
}
}
qDebug() << Qt::endl;
}
void OscInterface::applyOscCommand(MainWindow &main_window, QVariantList & command) {
if (is_verbose())
{
std::cout << "OscInterface::applyOscCommand: Receive OSC: " << std::endl;
printCommand(command);
}
// The two first QVariant objects are: path, typeTags. The rest are the args.
if (command.size() < 2)
return;
if (command.at(0).typeId() != QMetaType::QString)
return;
if (command.at(1).typeId() != QMetaType::QString)
return;
const QString path = command.at(0).toString();
const QVariantList args = command.mid(2);
OscAction action = parseOscAction(path, args);
bool handled = applyAction(main_window, action);
if (!handled && is_verbose())
{
qDebug() << "OSC path could not be processed: " << path
<< (action.isValid() ? QString("(no matching target)") : action.error)
<< Qt::endl;
printCommand(command);
}
}
bool OscInterface::applyAction(MainWindow &main_window, const OscAction& action)
{
MappingManager& manager = main_window.getMappingManager();
switch (action.type)
{
case OscAction::Invalid:
return false;
// Global transport.
case OscAction::Quit:
if (main_window.isOscQuitAllowed()) { main_window.close(); return true; }
qWarning() << "Ignoring OSC /mapmap/quit: remote quit is disabled "
"(enable it in Preferences > OSC Setup).";
return false;
case OscAction::PlayAll: main_window.play(); return true;
case OscAction::PauseAll: main_window.pause(); return true;
case OscAction::RewindAll: main_window.rewind(); return true;
// Per-source commands.
case OscAction::SourcePlay:
case OscAction::SourcePause:
case OscAction::SourceRewind:
case OscAction::SourceProperty:
{
bool handled = false;
for (Source::ptr source : resolveSources(manager, action))
{
if (source.isNull())
continue;
switch (action.type)
{
case OscAction::SourcePlay: source->play(); handled = true; break;
case OscAction::SourcePause: source->pause(); handled = true; break;
case OscAction::SourceRewind: source->rewind(); handled = true; break;
case OscAction::SourceProperty: handled |= setElementProperty(source, action.property, action.value); break;
default: break;
}
}
return handled;
}
// Per-layer commands.
case OscAction::LayerProperty:
case OscAction::LayerMove:
case OscAction::LayerTranslate:
case OscAction::LayerVertex:
{
bool handled = false;
for (Layer::ptr layer : resolveLayers(manager, action))
{
if (layer.isNull())
continue;
if (action.type == OscAction::LayerProperty)
{
handled |= setElementProperty(layer, action.property, action.value);
continue;
}
// The shape that move/translate/vertex operate on.
MShape::ptr shape = (action.shapeRole == OscAction::InputShape)
? layer->getInputShape()
: layer->getShape();
if (shape.isNull())
continue;
switch (action.type)
{
case OscAction::LayerMove:
{
// Absolute: translate so the shape's center lands on (x, y).
const QPointF center = shape->getCenter();
shape->translate(QPointF(action.x - center.x(), action.y - center.y()));
handled = true;
break;
}
case OscAction::LayerTranslate:
shape->translate(QPointF(action.x, action.y));
handled = true;
break;
case OscAction::LayerVertex:
if (action.vertexIndex >= 0 && action.vertexIndex < shape->nVertices())
{
shape->setVertex(action.vertexIndex, action.x, action.y);
handled = true;
}
break;
default:
break;
}
}
return handled;
}
}
return false;
}
bool OscInterface::setElementProperty(const QSharedPointer<Element>& elem, const QString& property, const QVariant& value)
{
if (elem.isNull())
return false;
const QByteArray name = property.toUtf8();
// Colors arrive over OSC as strings ("#ff0000", "red", ...). Convert them to
// a QColor when the target property is a color, since QVariant won't do it.
const QVariant existing = elem->property(name.constData());
if (existing.isValid()
&& existing.typeId() == QMetaType::QColor
&& value.typeId() == QMetaType::QString)
{
return elem->setProperty(name.constData(), QColor(value.toString()));
}
return elem->setProperty(name.constData(), value);
}
}