Debian Codegraph @

plugin/plugingizmo/ttlgen.cc

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /***************************************************************************
3 * ttlgen.cc
4 *
5 * Fri Jul 15 09:27:19 CEST 2016
6 * Copyright 2016 Bent Bisballe Nyeng
7 * [email protected]
8 ****************************************************************************/
9
10 /*
11 * This file is part of PluginGizmo.
12 *
13 * PluginGizmo is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * PluginGizmo is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with PluginGizmo; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 */
27 #include <iostream>
28 #include <fstream>
29 #include <dlfcn.h>
30
31 #include "pluginlv2.h"
32
33 typedef PluginLV2* create_t();
34
35 enum class UIType
36 {
37 CocoaUI,
38 Gtk3UI,
39 GtkUI,
40 Qt4UI,
41 Qt5UI,
42 WindowsUI,
43 X11UI,
44 };
45
46 static void header(std::ostream& output)
47 {
48 output << "\
49 # LV2 Plugin\n\
50 # Copyright 2019 Bent Bisballe Nyeng <[email protected]>\n\
51 #\n\
52 # Permission to use, copy, modify, and/or distribute this software for any\n\
53 # purpose with or without fee is hereby granted, provided that the above\n\
54 # copyright notice and this permission notice appear in all copies.\n\
55 #\n\
56 # THIS SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES\n\
57 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\n\
58 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR\n\
59 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\n\
60 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\n\
61 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF\n\
62 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n\
63 ";
64 }
65
66 static void includes(std::ostream& output)
67 {
68 output << "\
69 @prefix doap: <http://usefulinc.com/ns/doap#> .\n\
70 @prefix foaf: <http://xmlns.com/foaf/0.1/> .\n\
71 @prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n\
72 @prefix atom: <http://lv2plug.in/ns/ext/atom#> .\n\
73 @prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n\
74 @prefix state: <http://lv2plug.in/ns/ext/state#> .\n\
75 @prefix pprops: <http://lv2plug.in/ns/ext/port-props#> .\n\
76 @prefix idpy: <http://harrisonconsoles.com/lv2/inlinedisplay#> .\n\
77 @prefix time: <http://lv2plug.in/ns/ext/time#> .\n\
78 @prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n\
79 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n\
80 ";
81 }
82
83 // If UIClass was not explicitly set already, try to autodetect it based on
84 // operating system.
85 #ifndef UIClass
86 #ifdef __linux__
87 #define UIClass "X11UI"
88 #elif _WIN32
89 #define UIClass "WindowsUI"
90 #elif __APPLE__
91 #define UIClass "CocoaUI"
92 #elif __FreeBSD__
93 #define UIClass "X11UI"
94 #elif __unix__
95 // All other unices (*BSD etc)
96 #define UIClass "X11UI"
97 #endif
98 #endif
99
100 static void ui(Plugin& plugin, const std::string& pluginfile, UIType uitype,
101 std::ostream& output)
102 {
103 if(!plugin.hasGUI())
104 {
105 return;
106 }
107
108 output << "<" << plugin.getURI() << "/lv2#ui>\n";
109
110 switch(uitype)
111 {
112 case UIType::CocoaUI:
113 output << " a ui:CocoaUI ;\n";
114 break;
115 case UIType::Gtk3UI:
116 output << " a ui:Gtk3UI ;\n";
117 break;
118 case UIType::GtkUI:
119 output << " a ui:GtkUI ;\n";
120 break;
121 case UIType::Qt4UI:
122 output << " a ui:Qt4UI ;\n";
123 break;
124 case UIType::Qt5UI:
125 output << " a ui:Qt5UI ;\n";
126 break;
127 case UIType::WindowsUI:
128 output << " a ui:WindowsUI ;\n";
129 break;
130 case UIType::X11UI:
131 output << " a ui:X11UI ;\n";
132 break;
133 }
134
135 output << "\
136 lv2:requiredFeature ui:resize ;\n\
137 lv2:extensionData ui:resize ;\n\
138 lv2:requiredFeature ui:idleInterface ;\n\
139 lv2:extensionData ui:idleInterface ;\n\
140 lv2:requiredFeature <http://lv2plug.in/ns/ext/instance-access> ;\n\
141 ui:binary <" << pluginfile << "> .\n";
142 }
143
144 static void ports(Plugin& plugin, std::ostream& output)
145 {
146 std::size_t port_index = 2;
147 for(std::size_t i = 0; i < plugin.getNumberOfMidiInputs(); ++i)
148 {
149 output << "\
150 , [\n\
151 a atom:AtomPort ,\n\
152 lv2:InputPort;\n\
153 atom:bufferType atom:Sequence ;\n\
154 atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ;\n\
155 lv2:index " << port_index << " ;\n\
156 lv2:symbol \"control\" ;\n\
157 lv2:name \"Control\"\n\
158 ]";
159 ++port_index;
160 }
161
162 for(std::size_t i = 0; i < plugin.getNumberOfMidiOutputs(); ++i)
163 {
164 output << "\
165 , [\n\
166 a atom:AtomPort ,\n\
167 lv2:OutputPort;\n\
168 atom:bufferType atom:Sequence ;\n\
169 atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ;\n\
170 lv2:index " << port_index << " ;\n\
171 lv2:symbol \"control\" ;\n\
172 lv2:name \"Control\"\n\
173 ]";
174 ++port_index;
175 }
176
177 std::size_t input_port_index = 1;
178 for(std::size_t i = 0; i < plugin.getNumberOfAudioInputs(); ++i)
179 {
180 output << "\
181 , [\n\
182 a lv2:AudioPort ,\n\
183 lv2:InputPort ;\n\
184 lv2:index " << port_index << " ;\n\
185 lv2:symbol \"in" << input_port_index << "\" ;\n\
186 lv2:name \"In" << input_port_index << "\"\n\
187 ]";
188 ++port_index;
189 ++input_port_index;
190 }
191
192 std::size_t output_port_index = 1;
193 for(std::size_t i = 0; i < plugin.getNumberOfAudioOutputs(); ++i)
194 {
195 output << "\
196 , [\n\
197 a lv2:AudioPort ,\n\
198 lv2:OutputPort ;\n\
199 lv2:index " << port_index << " ;\n\
200 lv2:symbol \"out" << output_port_index << "\" ;\n\
201 lv2:name \"Out" << output_port_index << "\"\n\
202 ]";
203 ++port_index;
204 ++output_port_index;
205 }
206 }
207
208 void usage(const char* app)
209 {
210 std::cout << "Usage: " << app << " <plugin binary> <output file> [uitype]\n";
211 std::cout << "where uitype can be one of:\n";
212 std::cout << " CocoaUI\n";
213 std::cout << " Gtk3UI\n";
214 std::cout << " GtkUI\n";
215 std::cout << " Qt4UI\n";
216 std::cout << " Qt5UI\n";
217 std::cout << " WindowsUI\n";
218 std::cout << " X11UI\n";
219 std::cout << "default is " UIClass "\n";
220 }
221
222 UIType fromString(const std::string& t)
223 {
224 if(t == "CocoaUI")
225 {
226 return UIType::CocoaUI;
227 }
228 if(t == "Gtk3UI")
229 {
230 return UIType::Gtk3UI;
231 }
232 if(t == "GtkUI")
233 {
234 return UIType::GtkUI;
235 }
236 if(t == "Qt4UI")
237 {
238 return UIType::Qt4UI;
239 }
240 if(t == "Qt5UI")
241 {
242 return UIType::Qt5UI;
243 }
244 if(t == "WindowsUI")
245 {
246 return UIType::WindowsUI;
247 }
248 if(t == "X11UI")
249 {
250 return UIType::X11UI;
251 }
252
253 std::cerr << "Bad uitype: '" << t << "'\n";
254 exit(1);
255 }
256
257 int main(int argc, char* argv[])
258 {
259 if(argc < 3 || argc > 4)
260 {
261 std::cerr << "Missing argument.\n";
262 usage(argv[0]);
263 return 1;
264 }
265 UIType uitype = UIType::X11UI;
266 if(argc == 4)
267 {
268 uitype = fromString(argv[3]);
269 }
270
271 std::string library = argv[1];
272 auto seppos = library.rfind("/");
273 std::string binary = library;
274 if(seppos != std::string::npos)
275 {
276 binary = library.substr(seppos + 1);
277 }
278
279 // load the plugin library
280 void* plugin = dlopen(library.data(), RTLD_LAZY);
281 if(!plugin)
282 {
283 std::cerr << "Cannot load library: " << dlerror() << std::endl;
284 std::cerr << "Library must have absolute path or prefix ./myplugin.so.\n";
285 return 1;
286 }
287
288 // reset errors
289 dlerror();
290
291 // load the symbols
292 create_t* create_plugin = (create_t*) dlsym(plugin, "createEffectInstance");
293 const char* dlsym_error = dlerror();
294 if(dlsym_error)
295 {
296 std::cerr << "Cannot load symbol create: " << dlsym_error << '\n';
297 return 1;
298 }
299
300 std::ofstream output(argv[2]);
301
302 // create an instance of the class
303 Plugin* p = create_plugin();
304
305 header(output);
306 output << std::endl;
307 includes(output);
308 output << std::endl;
309 ui(*p, binary, uitype, output);
310 output << std::endl;
311
312 output << "\
313 <" << p->getURI() << "/lv2>\n\
314 a lv2:Plugin ;\n\
315 lv2:binary <" << binary << "> ;\n \
316 a lv2:InstrumentPlugin ;\n\
317 doap:name \"" << p->getEffectName() << "\" ;\n\
318 doap:maintainer [\n\
319 foaf:name \"" << p->getVendorString() << "\" ;\n\
320 foaf:homepage <" << p->getHomepage() << "> ;\n\
321 ] ;\n\
322 doap:license <http://usefulinc.com/doap/licenses/gpl> ;\n\
323 ui:ui <" << p->getURI() << "/lv2#ui> ;\n\
324 doap:license <http://opensource.org/licenses/gpl-3.0> ;\n\
325 lv2:optionalFeature <http://lv2plug.in/ns/ext/uri-map> ;\n\
326 lv2:optionalFeature <http://lv2plug.in/ns/ext/event> ;\n\
327 lv2:optionalFeature idpy:queue_draw ;\n\
328 lv2:extensionData state:interface ;\n\
329 lv2:port [\n\
330 a lv2:InputPort, lv2:ControlPort ;\n\
331 lv2:index 0 ;\n\
332 lv2:symbol \"lv2_freewheel\" ;\n\
333 lv2:name \"Freewheel\" ;\n\
334 lv2:default 0.0 ;\n\
335 lv2:minimum 0.0 ;\n\
336 lv2:maximum 1.0 ;\n\
337 lv2:designation <http://lv2plug.in/ns/lv2core#freeWheeling> ;\n\
338 lv2:portProperty <http://lv2plug.in/ns/lv2core#freeWheeling> ;\n\
339 lv2:portProperty lv2:toggled ;\n\
340 lv2:portProperty pprops:hasStrictBounds;\n\
341 ] , [\n\
342 a lv2:OutputPort, lv2:ControlPort ;\n\
343 lv2:designation <http://lv2plug.in/ns/lv2core#latency>;\n\
344 lv2:index 1;\n\
345 lv2:symbol \"latency\";\n\
346 lv2:name \"Latency\";\n\
347 lv2:minimum 0;\n\
348 lv2:maximum 192000;\n\
349 lv2:portProperty lv2:reportsLatency, lv2:integer;\n\
350 ]";
351
352 ports(*p, output);
353
354 output << " .\n";
355
356 // FIXME: The plugin is never deleted, but since we're terminating now anyway
357 // it doesn't matter much...
358
359 // unload the plugin library
360 dlclose(plugin);
361
362 return 0;
363 }