Chris@66
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@66
|
2 /*
|
Chris@66
|
3 Copyright (c) 2020 Queen Mary, University of London
|
Chris@66
|
4
|
Chris@66
|
5 Permission is hereby granted, free of charge, to any person
|
Chris@66
|
6 obtaining a copy of this software and associated documentation
|
Chris@66
|
7 files (the "Software"), to deal in the Software without
|
Chris@66
|
8 restriction, including without limitation the rights to use, copy,
|
Chris@66
|
9 modify, merge, publish, distribute, sublicense, and/or sell copies
|
Chris@66
|
10 of the Software, and to permit persons to whom the Software is
|
Chris@66
|
11 furnished to do so, subject to the following conditions:
|
Chris@66
|
12
|
Chris@66
|
13 The above copyright notice and this permission notice shall be
|
Chris@66
|
14 included in all copies or substantial portions of the Software.
|
Chris@66
|
15
|
Chris@66
|
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
Chris@66
|
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
Chris@66
|
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
Chris@66
|
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
Chris@66
|
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
Chris@66
|
21 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
Chris@66
|
22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
Chris@66
|
23
|
Chris@66
|
24 Except as contained in this notice, the names of the Centre for
|
Chris@66
|
25 Digital Music and Queen Mary, University of London shall not be
|
Chris@66
|
26 used in advertising or otherwise to promote the sale, use or other
|
Chris@66
|
27 dealings in this Software without prior written authorization.
|
Chris@66
|
28 */
|
Chris@66
|
29
|
Chris@66
|
30 #include <vamp-hostsdk/PluginHostAdapter.h>
|
Chris@66
|
31
|
Chris@66
|
32 // Undocumented internal API
|
Chris@66
|
33 #include <vamp-hostsdk/../src/vamp-hostsdk/Files.h>
|
Chris@66
|
34
|
Chris@67
|
35 #ifdef _WIN32
|
Chris@67
|
36 #include <windows.h>
|
Chris@67
|
37 #include <process.h>
|
Chris@67
|
38 #include <io.h>
|
Chris@67
|
39 #else
|
Chris@67
|
40 #include <unistd.h>
|
Chris@67
|
41 #endif
|
Chris@67
|
42
|
Chris@67
|
43 #include <fcntl.h>
|
Chris@67
|
44
|
Chris@66
|
45 #include <iostream>
|
Chris@66
|
46 #include <string>
|
Chris@66
|
47
|
Chris@66
|
48 using namespace std;
|
Chris@66
|
49
|
Chris@66
|
50 void usage(const char *me)
|
Chris@66
|
51 {
|
Chris@66
|
52 cerr << endl;
|
Chris@66
|
53 cerr << "Usage: " << me << " <soname>" << endl;
|
Chris@66
|
54 cerr << endl;
|
Chris@66
|
55 cerr << "Loads the Vamp plugin library found in <soname> and lists\n"
|
Chris@66
|
56 << "the plugin versions found therein. Plugins are listed one per\n"
|
Chris@66
|
57 << "line, with the plugin id and version number separated by a colon."
|
Chris@66
|
58 << endl;
|
Chris@66
|
59 cerr << endl;
|
Chris@66
|
60 cerr << "Note that <soname> must be the path to a plugin library, not\n"
|
Chris@66
|
61 << "merely the name of the library. That is, no search path is used.\n"
|
Chris@66
|
62 << "The file path should be UTF-8 encoded (on all platforms)."
|
Chris@66
|
63 << endl;
|
Chris@66
|
64 cerr << endl;
|
Chris@66
|
65 cerr << "The return code is 0 if the plugin library was successfully\n"
|
Chris@66
|
66 << "loaded, otherwise 1. An error message may be printed to stderr\n"
|
Chris@66
|
67 << "in the latter case." << endl;
|
Chris@66
|
68 cerr << endl;
|
Chris@66
|
69 exit(2);
|
Chris@66
|
70 }
|
Chris@66
|
71
|
Chris@67
|
72 // We write our output to stdout, but want to ensure that the plugin
|
Chris@67
|
73 // doesn't write anything itself. To do this we open a null file
|
Chris@67
|
74 // descriptor and dup2() it into place of stdout in the gaps between
|
Chris@67
|
75 // our own output activity.
|
Chris@67
|
76
|
Chris@67
|
77 static int normalFd = -1;
|
Chris@67
|
78 static int suspendedFd = -1;
|
Chris@67
|
79
|
Chris@67
|
80 static void initFds()
|
Chris@67
|
81 {
|
Chris@67
|
82 #ifdef _WIN32
|
Chris@67
|
83 normalFd = _dup(1);
|
Chris@67
|
84 suspendedFd = _open("NUL", _O_WRONLY);
|
Chris@67
|
85 #else
|
Chris@67
|
86 normalFd = dup(1);
|
Chris@67
|
87 suspendedFd = open("/dev/null", O_WRONLY);
|
Chris@67
|
88 #endif
|
Chris@67
|
89
|
Chris@67
|
90 if (normalFd < 0 || suspendedFd < 0) {
|
Chris@67
|
91 throw std::runtime_error
|
Chris@67
|
92 ("Failed to initialise fds for stdio suspend/resume");
|
Chris@67
|
93 }
|
Chris@67
|
94 }
|
Chris@67
|
95
|
Chris@67
|
96 static void suspendOutput()
|
Chris@67
|
97 {
|
Chris@67
|
98 #ifdef _WIN32
|
Chris@67
|
99 _dup2(suspendedFd, 1);
|
Chris@67
|
100 #else
|
Chris@67
|
101 dup2(suspendedFd, 1);
|
Chris@67
|
102 #endif
|
Chris@67
|
103 }
|
Chris@67
|
104
|
Chris@67
|
105 static void resumeOutput()
|
Chris@67
|
106 {
|
Chris@67
|
107 fflush(stdout);
|
Chris@67
|
108 #ifdef _WIN32
|
Chris@67
|
109 _dup2(normalFd, 1);
|
Chris@67
|
110 #else
|
Chris@67
|
111 dup2(normalFd, 1);
|
Chris@67
|
112 #endif
|
Chris@67
|
113 }
|
Chris@67
|
114
|
Chris@66
|
115 int main(int argc, char **argv)
|
Chris@66
|
116 {
|
Chris@66
|
117 char *scooter = argv[0];
|
Chris@66
|
118 char *name = 0;
|
Chris@66
|
119 while (scooter && *scooter) {
|
Chris@66
|
120 if (*scooter == '/' || *scooter == '\\') name = ++scooter;
|
Chris@66
|
121 else ++scooter;
|
Chris@66
|
122 }
|
Chris@66
|
123 if (!name || !*name) name = argv[0];
|
Chris@66
|
124
|
Chris@66
|
125 if (argc != 2) usage(name);
|
Chris@66
|
126 if (string(argv[1]) == string("-?")) usage(name);
|
Chris@66
|
127
|
Chris@67
|
128 initFds();
|
Chris@67
|
129 suspendOutput();
|
Chris@67
|
130
|
Chris@66
|
131 string libraryPath(argv[1]);
|
Chris@66
|
132
|
Chris@66
|
133 void *handle = Files::loadLibrary(libraryPath);
|
Chris@66
|
134 if (!handle) {
|
Chris@66
|
135 cerr << "Unable to load library " << libraryPath << endl;
|
Chris@66
|
136 return 1;
|
Chris@66
|
137 }
|
Chris@66
|
138
|
Chris@66
|
139 VampGetPluginDescriptorFunction fn =
|
Chris@66
|
140 (VampGetPluginDescriptorFunction)Files::lookupInLibrary
|
Chris@66
|
141 (handle, "vampGetPluginDescriptor");
|
Chris@66
|
142
|
Chris@66
|
143 if (!fn) {
|
Chris@66
|
144 cerr << "No vampGetPluginDescriptor function found in library \""
|
Chris@66
|
145 << libraryPath << "\"" << endl;
|
Chris@66
|
146 Files::unloadLibrary(handle);
|
Chris@66
|
147 return 1;
|
Chris@66
|
148 }
|
Chris@66
|
149
|
Chris@66
|
150 int index = 0;
|
Chris@66
|
151 const VampPluginDescriptor *descriptor = 0;
|
Chris@66
|
152
|
Chris@66
|
153 while ((descriptor = fn(VAMP_API_VERSION, index))) {
|
Chris@67
|
154 resumeOutput();
|
Chris@66
|
155 cout << descriptor->identifier << ":"
|
Chris@66
|
156 << descriptor->pluginVersion << endl;
|
Chris@67
|
157 suspendOutput();
|
Chris@66
|
158 ++index;
|
Chris@66
|
159 }
|
Chris@66
|
160
|
Chris@66
|
161 Files::unloadLibrary(handle);
|
Chris@66
|
162
|
Chris@66
|
163 return 0;
|
Chris@66
|
164 }
|