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
|
// -*- c-basic-offset: 4 -*-
/*
dssi-vst: a DSSI plugin wrapper for VST effects and instruments
Copyright 2004-2008 Chris Cannam
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <sys/un.h>
#include <sys/poll.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <lo/lo.h>
#include <lo/lo_lowlevel.h>
#include "rdwrops.h"
static int debugLevel = 3;
static bool ready = false;
static bool exiting = false;
static lo_server oscserver = 0;
static char *hosturl = 0;
static lo_address hostaddr = 0;
static char *hosthostname = 0;
static char *hostport = 0;
static char *hostpath = 0;
static char *fifoFile = 0;
static int fifoFd = -1;
using std::cout;
using std::cerr;
using std::endl;
#include "remoteplugin.h" // for RemotePluginVersion
void
osc_error(int num, const char *msg, const char *path)
{
cerr << "Error: liblo server error " << num
<< " in path \"" << (path ? path : "(null)")
<< "\": " << msg << endl;
}
int
debug_handler(const char *path, const char *types, lo_arg **argv,
int argc, void *data, void *user_data)
{
int i;
cerr << "Warning: unhandled OSC message in GUI:" << endl;
for (i = 0; i < argc; ++i) {
cerr << "arg " << i << ": type '" << types[i] << "': ";
lo_arg_pp((lo_type)types[i], argv[i]);
cerr << endl;
}
cerr << "(path is <" << path << ">)" << endl;
return 1;
}
int
program_handler(const char *path, const char *types, lo_arg **argv,
int argc, void *data, void *user_data)
{
cerr << "dssi-vst_gui: program_handler" << endl;
return 0;
}
int
configure_handler(const char *path, const char *types, lo_arg **argv,
int argc, void *data, void *user_data)
{
cerr << "dssi-vst_gui: configure_handler, returning 0" << endl;
return 0;
}
int
show_handler(const char *path, const char *types, lo_arg **argv,
int argc, void *data, void *user_data)
{
cerr << "dssi-vst_gui: show_handler" << endl;
lo_send(hostaddr,
(std::string(hostpath) + "/configure").c_str(),
"ss",
"guiVisible",
fifoFile);
return 0;
}
int
hide_handler(const char *path, const char *types, lo_arg **argv,
int argc, void *data, void *user_data)
{
cerr << "dssi-vst_gui: hide_handler" << endl;
lo_send(hostaddr,
(std::string(hostpath) + "/configure").c_str(),
"ss",
"guiVisible",
"");
return 0;
}
int
quit_handler(const char *path, const char *types, lo_arg **argv,
int argc, void *data, void *user_data)
{
cerr << "quit_handler" << endl;
exiting = true;
return 0;
}
int
control_handler(const char *path, const char *types, lo_arg **argv,
int argc, void *data, void *user_data)
{
static int count = 0;
cerr << "dssi-vst_gui: control_handler " << count++ << endl;
return 0;
}
void
readFromPlugin()
{
cerr << "dssi-vst_gui: something to read from plugin" << endl;
try {
RemotePluginOpcode opcode = RemotePluginNoOpcode;
tryRead(fifoFd, &opcode, sizeof(RemotePluginOpcode));
switch (opcode) {
case RemotePluginIsReady:
ready = true;
break;
case RemotePluginSetParameter:
{
int port = readInt(fifoFd);
float value = readFloat(fifoFd);
cerr << "dssi-vst_gui: sending (" << port << "," << value << ") to host" << endl;
lo_send(hostaddr,
(std::string(hostpath) + "/control").c_str(),
"if", port, value);
break;
}
case RemotePluginTerminate:
cerr << "dssi-vst_gui: asked to terminate" << endl;
lo_send(hostaddr,
(std::string(hostpath) + "/exiting").c_str(),
"");
exiting = true;
break;
default:
std::cerr << "WARNING: dssi-vst_gui: unexpected opcode "
<< opcode << std::endl;
break;
}
} catch (...) { }
}
int
main(int argc, char **argv)
{
cout << "DSSI VST plugin GUI controller v" << RemotePluginVersion << endl;
cout << "Copyright (c) 2004-2008 Chris Cannam" << endl;
char *pluginlibname = 0;
char *label = 0;
char *friendlyname = 0;
if (argc != 5) {
cerr << "Usage: dssi-vst_gui <osc url> <plugin.so> <label> <friendlyname>" << endl;
exit(2);
}
hosturl = argv[1];
pluginlibname = argv[2];
label = argv[3];
friendlyname = argv[4];
if (!hosturl || !hosturl[0] ||
!pluginlibname || !pluginlibname[0] ||
!label || !label[0] ||
!friendlyname || !friendlyname[0]) {
cerr << "Usage: dssi-vst_gui <osc url> <plugin.so> <label> <friendlyname>" << endl;
exit(2);
}
char tmpFileBase[60];
sprintf(tmpFileBase, "/tmp/rplugin_gui_XXXXXX");
if (mkstemp(tmpFileBase) < 0) {
cerr << "Failed to obtain temporary filename" << endl;
exit(1);
}
fifoFile = strdup(tmpFileBase);
unlink(fifoFile);
if (mkfifo(fifoFile, 0666)) { //!!! what permission is correct here?
perror(fifoFile);
cerr << "Failed to create FIFO" << endl;
exit(1);
}
if ((fifoFd = open(fifoFile, O_RDONLY | O_NONBLOCK)) < 0) {
perror(fifoFile);
cerr << "Failed to open FIFO" << endl;
unlink(fifoFile);
exit(1);
}
oscserver = lo_server_new(NULL, osc_error);
lo_server_add_method(oscserver, "/dssi/control", "if", control_handler, 0);
lo_server_add_method(oscserver, "/dssi/program", "ii", program_handler, 0);
lo_server_add_method(oscserver, "/dssi/configure", "ss", configure_handler, 0);
lo_server_add_method(oscserver, "/dssi/show", "", show_handler, 0);
lo_server_add_method(oscserver, "/dssi/hide", "", hide_handler, 0);
lo_server_add_method(oscserver, "/dssi/quit", "", quit_handler, 0);
lo_server_add_method(oscserver, NULL, NULL, debug_handler, 0);
hosthostname = lo_url_get_hostname(hosturl);
hostport = lo_url_get_port(hosturl);
hostpath = lo_url_get_path(hosturl);
cout << "created lo server (url is " << lo_server_get_url(oscserver) << ") - update path is " << std::string(hostpath) << "/update" << endl;
hostaddr = lo_address_new(hosthostname, hostport);
lo_send(hostaddr,
(std::string(hostpath) + "/update").c_str(),
"s",
(std::string(lo_server_get_url(oscserver)) + "dssi").c_str());
exiting = false;
bool idle = true;
struct timeval tv;
gettimeofday(&tv, NULL);
while (!exiting) {
bool idleHere = true;
struct pollfd pfd;
pfd.fd = fifoFd;
pfd.events = POLLIN;
if (poll(&pfd, 1, 0) > 0) {
readFromPlugin();
idleHere = false;
}
if (lo_server_recv_noblock(oscserver, idle ? 30 : 0)) {
idleHere = false;
}
idle = idleHere;
if (!ready) {
struct timeval tv1;
gettimeofday(&tv1, NULL);
if (tv1.tv_sec > tv.tv_sec + 15) {
cerr << "dssi-vst_gui: No contact from plugin -- timed out on startup" << endl;
lo_send(hostaddr,
(std::string(hostpath) + "/exiting").c_str(),
"");
break;
}
}
}
if (debugLevel > 0) {
cerr << "dssi-vst_gui[1]: exiting" << endl;
}
close(fifoFd);
unlink(fifoFile);
return 0;
}
|