forked from mapmapteam/mapmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOscInterface.cpp
252 lines (234 loc) · 7.02 KB
/
OscInterface.cpp
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
/*
* OscInterface.cpp
*
* Copyright (c) 2010 Alexandre Quessy <[email protected]>
* Copyright (c) 2010 Tristan Matthews <[email protected]>
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 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/>.
*/
#ifdef HAVE_OSC
#include "OscInterface.h"
#include "MainWindow.h"
#include <QVariant>
OscInterface::OscInterface(
// MainWindow* owner,
const std::string &listen_port) :
receiver_(listen_port),
// owner_(owner),
messaging_queue_()
{
//if (listen_port != OSC_PORT_NONE)
receiving_enabled_ = true;
if (receiving_enabled_)
{
std::cout << "Listening osc.udp://localhost:" << listen_port << std::endl;
// receiver_.addHandler("/ping", "", ping_cb, this);
// receiver_.addHandler("/pong", "", pong_cb, this);
//receiver_.addHandler("/image/path", "ss", image_path_cb, this);
receiver_.addHandler(NULL, NULL, genericHandler, this);
}
}
OscInterface::~OscInterface()
{
// pass
}
/**
* Handles /pong. Does nothing.
*/
int OscInterface::pong_cb(
const char *path,
const char * /*types*/,
lo_arg ** /*argv*/,
int /*argc*/,
void * /*data*/,
void *user_data)
{
OscInterface* context = static_cast<OscInterface*>(user_data);
if (context->is_verbose())
std::cout << "Got " << path << std::endl;
return 0;
}
/**
* Handles /ping. Does nothing.
*/
int OscInterface::ping_cb(
const char *path,
const char * /*types*/,
lo_arg ** /*argv*/,
int /*argc*/,
void * /*data*/,
void *user_data)
{
OscInterface* context = static_cast<OscInterface*>(user_data);
if (context->is_verbose())
std::cout << "Got " << path << std::endl;
return 0;
}
void OscInterface::push_command(QVariantList command)
{
messaging_queue_.push(command);
}
/**
* Takes action!
*
* Should be called when it's time to take action, before rendering a frame, for example.
*/
void OscInterface::consume_commands(MainWindow &main_window)
{
bool success = true;
while (success)
{
QVariantList command;
success = messaging_queue_.try_pop(command);
if (success)
{
//if (is_verbose())
// std::cout << __FUNCTION__ << ": apply " <<
// command.first().toString().toStdString() << std::endl;
this->applyOscCommand(main_window, command);
}
}
}
/**
* Starts listening if enabled
*/
void OscInterface::start()
{
if (receiving_enabled_)
{
// start a thread to try and subscribe us
receiver_.listen(); // start listening in separate thread
}
}
/* catch any incoming messages and display them. returning 1 means that the
* * message has not been fully handled and the server should try other methods */
int OscInterface::genericHandler(const char *path,
const char *types, lo_arg **argv,
int argc, void * /*data*/, void * user_data)
{
OscInterface* context = static_cast<OscInterface*>(user_data);
QVariantList message;
message.append(QVariant(QString(path)));
message.append(QVariant(QString(types)));
for (int i = 0; i < argc; ++i)
{
switch (types[i])
{
case 'i':
message.append(QVariant(argv[i]->i));
break;
case 'f':
message.append(QVariant((double) argv[i]->f));
break;
case 's':
message.append(QVariant(QString(static_cast<const char *>(&argv[i]->s))));
break;
case 'd':
message.append(QVariant((double) argv[i]->d));
break;
case 'T':
message.append(QVariant(true));
break;
case 'F':
message.append(QVariant(false));
break;
default:
break;
}
}
context->push_command(message);
return 0; // handled
}
static void printCommand(QVariantList &command)
{
for (int i = 0; i < command.size(); ++i)
{
if (command.at(i).type() == QVariant::Int)
{
std::cout << command.at(i).toInt() << " ";
}
else if (command.at(i).type() == QVariant::Double)
{
std::cout << command.at(i).toDouble() << " ";
}
else if (command.at(i).type() == QVariant::String)
{
std::cout << command.at(i).toString().toStdString() << " ";
}
else
{
std::cout << "(?) ";
}
}
std::cout << std::endl;
std::cout.flush();
}
void OscInterface::applyOscCommand(MainWindow &main_window, QVariantList & command)
{
Q_UNUSED(main_window);
bool VERBOSE = true;
if (VERBOSE)
{
std::cout << "OscInterface::applyOscCommand: Receive OSC: " << std::endl;
printCommand(command);
}
// The two first QVariant objects are: path, typeTags
if (command.size() < 2)
return;
if (command.at(0).type() != QVariant::String)
return;
if (command.at(1).type() != QVariant::String)
return;
std::string path = command.at(0).toString().toStdString();
std::string typetags = command.at(1).toString().toStdString();
// Handle all OSC messages here
if (path == "/mapmap/paint/media/load" && typetags == "is")
{
int paint_id = command.at(2).toInt();
std::string image_uri = command.at(3).toString().toStdString();
//std::cout << "load /mapmap/paint/media/load " << paint_id << " " << image_uri << std::endl;
main_window.setTextureUri(paint_id, image_uri);
}
else if (path == "/mapmap/paint/media/rate" && typetags == "if")
{
int paint_id = command.at(2).toInt();
float rate = command.at(3).toDouble();
//std::cout << "load /mapmap/paint/media/load " << paint_id << " " << image_uri << std::endl;
main_window.setTextureRate(paint_id, rate);
}
else if (path == "/mapmap/mapping/visible" && typetags == "ii")
{
int mappingId = command.at(2).toInt();
int visible = command.at(3).toInt();
//std::cout << "Visibility of MappingId " << mappingId << " set to " << visible << std::endl;
main_window.setMappingItemVisibility(mappingId, visible ? true : false);
main_window.setMappingVisible(mappingId, visible ? true : false);
}
else
{
std::cout << "Unhandled OSC message: ";
printCommand(command);
}
//else if (path == "/add/quad")
// addQuad();
//else if (path == "/add/triangle")
// addTriangle();
//else if (path == "/project/save")
// save();
//else if (path == "/project/open")
// open();
}
#endif // HAVE_OSC