forked from falkTX/dssi-vst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remotevstclient.cpp
441 lines (346 loc) · 10.9 KB
/
remotevstclient.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// -*- c-basic-offset: 4 -*-
/*
dssi-vst: a DSSI plugin wrapper for VST effects and instruments
Copyright 2012-2013 Filipe Coelho
Copyright 2010-2011 Kristian Amlie
Copyright 2004-2010 Chris Cannam
*/
#include "remotevstclient.h"
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <fcntl.h>
#include <iostream>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <cstdio>
#include <stdlib.h>
#include "rdwrops.h"
#include "paths.h"
RemoteVSTClient::RemoteVSTClient(std::string dllName, bool showGUI) :
RemotePluginClient()
{
pid_t child;
std::string arg = dllName + "," + getFileIdentifiers();
if (showGUI) arg = "-g " + arg;
const char *argStr = arg.c_str();
// We want to run the dssi-vst-server script, which runs wine
// dssi-vst-server.exe.so. We expect to find this script in the
// same subdirectory of a directory in the DSSI_PATH as a host
// would look for the GUI for this plugin: one called dssi-vst.
// See also RemoteVSTClient::queryPlugins below.
std::vector<std::string> dssiPath = Paths::getPath
("DSSI_PATH", "/usr/local/lib/dssi:/usr/lib/dssi", "/.dssi");
bool found = false;
std::string sought;
for (size_t i = 0; i < dssiPath.size(); ++i) {
std::string subDir = dssiPath[i] + "/dssi-vst";
std::string fileName = subDir + "/dssi-vst-server.exe";
DIR *directory = opendir(subDir.c_str());
if (!directory) {
sought += " " + fileName;
continue;
}
closedir(directory);
struct stat st;
if (stat(fileName.c_str(), &st)) {
sought += " " + fileName;
continue;
}
if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) ||
!(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
sought += " " + fileName;
std::cerr << "RemoteVSTClient: file " << fileName
<< " exists but can't be executed" << std::endl;
continue;
}
found = true;
std::cerr << "RemoteVSTClient: executing "
<< fileName << " " << argStr << std::endl;
const char* fileNameStr = fileName.c_str();
if ((child = vfork()) < 0) {
cleanup();
throw((std::string)"Fork failed");
} else if (child == 0) { // child process
if (execlp(fileNameStr, fileNameStr, argStr, NULL)) {
// vfork() docs say you shouldn't call a function here,
// but it seems to work for me.
perror("Exec failed");
_exit(1);
}
}
break;
}
if (!found) {
cleanup();
throw(std::string("Failed to find dssi-vst-server.exe [tried:" +
sought + "]"));
} else {
syncStartup();
}
}
RemoteVSTClient::~RemoteVSTClient()
{
for (int i = 0; i < 3; ++i) {
if (waitpid(-1, NULL, WNOHANG)) break;
sleep(1);
}
}
bool
RemoteVSTClient::addFromFd(int fd, PluginRecord &rec)
{
char buffer[64];
try {
tryRead(fd, buffer, 64);
rec.dllName = buffer;
} catch (RemotePluginClosedException) {
// This is acceptable here; it probably just means we're done
return false;
}
tryRead(fd, buffer, 64);
rec.pluginName = buffer;
// std::cerr << "Plugin " << rec.pluginName << std::endl;
tryRead(fd, buffer, 64);
rec.vendorName = buffer;
tryRead(fd, &rec.isSynth, sizeof(bool));
tryRead(fd, &rec.hasGUI, sizeof(bool));
tryRead(fd, &rec.inputs, sizeof(int));
tryRead(fd, &rec.outputs, sizeof(int));
tryRead(fd, &rec.parameters, sizeof(int));
// std::cerr << rec.parameters << " parameters" << std::endl;
for (int i = 0; i < rec.parameters; ++i) {
try {
tryRead(fd, buffer, 64);
} catch (RemotePluginClosedException) {
return false; // plugin check failed
}
rec.parameterNames.push_back(std::string(buffer));
float f;
tryRead(fd, &f, sizeof(float));
rec.parameterDefaults.push_back(f);
// std::cerr << "Parameter " << i << ": name " << buffer << ", default " << f << std::endl;
}
tryRead(fd, &rec.programs, sizeof(int));
// std::cerr << rec.programs << " programs" << std::endl;
for (int i = 0; i < rec.programs; ++i) {
tryRead(fd, buffer, 64);
rec.programNames.push_back(std::string(buffer));
// std::cerr << "Program " << i << ": name " << buffer << std::endl;
}
return true;
}
void
RemoteVSTClient::queryPlugins(std::vector<PluginRecord> &plugins)
{
// First check whether there are any DLLs in the same VST path as
// the scanner uses. If not, we know immediately there are no
// plugins and we don't need to run the (Wine-based) scanner.
// If there are, but they all have up-to-date cache files, then
// we can just read those and again not have to run the scanner.
std::vector<std::string> vstPath = Paths::getPath
("VST_PATH", "/usr/local/lib/vst:/usr/lib/vst", "/vst");
bool haveDll = false;
bool haveAllCaches = false;
bool haveCacheDir = false;
int version = int(RemotePluginVersion * 1000);
char *home = getenv("HOME");
std::string cacheDir = std::string(home) + "/.dssi-vst";
DIR *test = opendir(cacheDir.c_str());
if (test) {
haveCacheDir = true;
haveAllCaches = true; // until proven otherwise
closedir(test);
}
for (size_t i = 0; i < vstPath.size(); ++i) {
std::string vstDir = vstPath[i];
DIR *directory = opendir(vstDir.c_str());
if (!directory) continue;
struct dirent *entry;
while ((entry = readdir(directory))) {
std::string libname = entry->d_name;
if (libname[0] != '.' &&
libname.length() >= 5 &&
(libname.substr(libname.length() - 4) == ".dll" ||
libname.substr(libname.length() - 4) == ".DLL")) {
haveDll = true;
if (!haveCacheDir) break;
if (haveAllCaches) {
std::string cacheFileName = cacheDir + "/" + libname + ".cache";
struct stat st;
if (stat(cacheFileName.c_str(), &st)) {
haveAllCaches = false;
} else {
int testfd = open(cacheFileName.c_str(), O_RDONLY);
int testVersion = 0;
if (testfd < 0 ||
read(testfd, &testVersion, sizeof(int)) != sizeof(int) ||
testVersion != version) {
haveAllCaches = false;
}
if (testfd >= 0) close(testfd);
}
}
}
}
closedir(directory);
if (haveDll && !haveAllCaches) break;
}
if (!haveDll) return;
if (haveCacheDir && haveAllCaches) {
std::cerr << "RemoteVSTClient: all cache files are up-to-date, "
<< "not running scanner" << std::endl;
for (size_t i = 0; i < vstPath.size(); ++i) {
std::string vstDir = vstPath[i];
DIR *directory = opendir(vstDir.c_str());
if (!directory) continue;
struct dirent *entry;
while ((entry = readdir(directory))) {
std::string libname = entry->d_name;
if (libname[0] != '.' &&
libname.length() >= 5 &&
(libname.substr(libname.length() - 4) == ".dll" ||
libname.substr(libname.length() - 4) == ".DLL")) {
std::string cacheFileName = cacheDir + "/" + libname + ".cache";
int fd = -1;
int testVersion = 0;
if ((fd = open(cacheFileName.c_str(), O_RDONLY)) < 0) continue;
if (read(fd, &testVersion, sizeof(int)) != sizeof(int) ||
testVersion != version) {
close(fd);
continue;
}
PluginRecord rec;
if (addFromFd(fd, rec)) {
plugins.push_back(rec);
}
close(fd);
}
}
}
return;
}
char fifoFile[60];
sprintf(fifoFile, "/tmp/rplugin_qry_XXXXXX");
if (mkstemp(fifoFile) < 0) {
throw((std::string)"Failed to obtain temporary filename");
}
unlink(fifoFile);
if (mkfifo(fifoFile, 0666)) { //!!! what permission is correct here?
perror(fifoFile);
throw((std::string)"Failed to create FIFO");
}
// We open the fd nonblocking, then start the scanner, then wait
// to see whether the scanner starts sending anything on it. If
// no input is available after a certain time, give up.
int fd = -1;
if ((fd = open(fifoFile, O_RDONLY | O_NONBLOCK)) < 0) {
unlink(fifoFile);
throw((std::string)"Failed to open FIFO");
}
// We want to run the dssi-vst-scanner script, which runs wine
// dssi-vst-scanner.exe.so. We expect to find this script in the
// same subdirectory of a directory in the DSSI_PATH as a host
// would look for the GUI for this plugin: one called dssi-vst.
// See also the RemoteVSTClient constructor above.
std::vector<std::string> dssiPath = Paths::getPath
("DSSI_PATH", "/usr/local/lib/dssi:/usr/lib/dssi", "/.dssi");
bool found = false;
pid_t child;
std::string sought;
for (size_t i = 0; i < dssiPath.size(); ++i) {
std::string subDir = dssiPath[i] + "/dssi-vst";
std::string fileName = subDir + "/dssi-vst-scanner.exe";
DIR *directory = opendir(subDir.c_str());
if (!directory) {
sought += " " + fileName;
continue;
}
closedir(directory);
struct stat st;
if (stat(fileName.c_str(), &st)) {
sought += " " + fileName;
continue;
}
if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) ||
!(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
sought += " " + fileName;
std::cerr << "RemoteVSTClient: file " << fileName
<< " exists but can't be executed" << std::endl;
continue;
}
found = true;
std::cerr << "RemoteVSTClient: executing "
<< fileName << " " << fifoFile << std::endl;
if ((child = fork()) < 0) {
unlink(fifoFile);
throw((std::string)"Fork failed");
} else if (child == 0) { // child process
if (execlp(fileName.c_str(), fileName.c_str(), fifoFile, NULL)) {
perror("Exec failed");
unlink(fifoFile);
exit(1);
}
}
break;
}
if (!found) {
unlink(fifoFile);
throw(std::string("Failed to find dssi-vst-scanner.exe [tried:" +
sought + "]"));
}
struct pollfd pfd;
pfd.fd = fd;
pfd.events = POLLIN;
int sec;
int timeout = 40;
for (sec = 0; sec < timeout; ++sec) {
int rv = poll(&pfd, 1, 1000);
if (rv < 0) {
if (errno == EINTR || errno == EAGAIN) {
// try again
sleep(1);
continue;
} else {
close(fd);
unlink(fifoFile);
throw ((std::string)"Plugin scanner startup failed.");
}
} else if (rv > 0) {
break;
}
}
if (sec >= timeout) {
close(fd);
unlink(fifoFile);
throw ((std::string)"Plugin scanner timed out on startup.");
}
try {
int version = 0;
tryRead(fd, &version, sizeof(int));
if (version != int(RemotePluginVersion * 1000)) {
std::cerr << "RemoteVSTClient: Remote plugin version reported as "
<< version << ", I expected "
<< (RemotePluginVersion * 1000) << std::endl;
throw ((std::string)"Plugin scanner version mismatch");
}
while (1) {
PluginRecord rec;
if (!addFromFd(fd, rec)) break; // done
plugins.push_back(rec);
}
} catch (std::string s) {
std::cerr << s << std::endl;
} catch (RemotePluginClosedException) {
std::cerr << "Plugin scanner exited unexpectedly." << std::endl;
}
close(fd);
unlink(fifoFile);
for (int i = 0; i < 3; ++i) {
if (waitpid(-1, NULL, WNOHANG)) break;
sleep(1);
}
}