summaryrefslogtreecommitdiff
path: root/ttlgen.cc
blob: 1e9c2575b6d8fb434bfeb49521091ae339a5d219 (plain)
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            ttlgen.cc
 *
 *  Fri Jul 15 09:27:19 CEST 2016
 *  Copyright 2016 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of PluginGizmo.
 *
 *  PluginGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  PluginGizmo 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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with PluginGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include <iostream>
#include <fstream>
#include <dlfcn.h>

#include "pluginlv2.h"

typedef PluginLV2* create_t();

enum class UIType
{
	CocoaUI,
	Gtk3UI,
	GtkUI,
	Qt4UI,
	Qt5UI,
	WindowsUI,
	X11UI,
};

static void header(std::ostream& output)
{
	output << "\
# LV2 Plugin\n\
# Copyright 2019 Bent Bisballe Nyeng <deva@aasimon.org>\n\
#\n\
# Permission to use, copy, modify, and/or distribute this software for any\n\
# purpose with or without fee is hereby granted, provided that the above\n\
# copyright notice and this permission notice appear in all copies.\n\
#\n\
# THIS SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES\n\
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\n\
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR\n\
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\n\
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\n\
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF\n\
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n\
";
}

static void includes(std::ostream& output)
{
	output << "\
@prefix doap:   <http://usefulinc.com/ns/doap#> .\n\
@prefix foaf:   <http://xmlns.com/foaf/0.1/> .\n\
@prefix lv2:    <http://lv2plug.in/ns/lv2core#> .\n\
@prefix atom:   <http://lv2plug.in/ns/ext/atom#> .\n\
@prefix ui:     <http://lv2plug.in/ns/extensions/ui#> .\n\
@prefix state:  <http://lv2plug.in/ns/ext/state#> .\n\
@prefix pprops: <http://lv2plug.in/ns/ext/port-props#> .\n\
@prefix idpy:   <http://harrisonconsoles.com/lv2/inlinedisplay#> .\n\
@prefix time:   <http://lv2plug.in/ns/ext/time#> .\n\
@prefix lv2:    <http://lv2plug.in/ns/lv2core#> .\n\
@prefix rdfs:   <http://www.w3.org/2000/01/rdf-schema#> .\n\
";
}

// If UIClass was not explicitly set already, try to autodetect it based on
// operating system.
#ifndef UIClass
	#ifdef __linux__
		#define UIClass "X11UI"
	#elif _WIN32
		#define UIClass "WindowsUI"
	#elif __APPLE__
		#define UIClass "CocoaUI"
	#elif __FreeBSD__
		#define UIClass "X11UI"
	#elif __unix__
		// All other unices (*BSD etc)
		#define UIClass "X11UI"
	#endif
#endif

static void ui(Plugin& plugin, const std::string& pluginfile, UIType uitype,
               std::ostream& output)
{
	if(!plugin.hasGUI())
	{
		return;
	}

	output << "<" << plugin.getURI() << "/lv2#ui>\n";

	switch(uitype)
	{
	case UIType::CocoaUI:
		output << "	a ui:CocoaUI ;\n";
		break;
	case UIType::Gtk3UI:
		output << "	a ui:Gtk3UI ;\n";
		break;
	case UIType::GtkUI:
		output << "	a ui:GtkUI ;\n";
		break;
	case UIType::Qt4UI:
		output << "	a ui:Qt4UI ;\n";
		break;
	case UIType::Qt5UI:
		output << "	a ui:Qt5UI ;\n";
		break;
	case UIType::WindowsUI:
		output << "	a ui:WindowsUI ;\n";
		break;
	case UIType::X11UI:
		output << "	a ui:X11UI ;\n";
		break;
	}

	output << "\
	lv2:requiredFeature ui:resize ;\n\
	lv2:extensionData ui:resize ;\n\
	lv2:requiredFeature ui:idleInterface ;\n\
	lv2:extensionData ui:idleInterface ;\n\
	lv2:requiredFeature <http://lv2plug.in/ns/ext/instance-access> ;\n\
	ui:binary <" << pluginfile << "> .\n";
}

static void ports(Plugin& plugin, std::ostream& output)
{
	std::size_t port_index = 2;
	for(std::size_t i = 0; i < plugin.getNumberOfMidiInputs(); ++i)
	{
		output << "\
 , [\n\
		a atom:AtomPort ,\n\
			lv2:InputPort;\n\
		atom:bufferType atom:Sequence ;\n\
		atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ;\n\
		lv2:index " << port_index << " ;\n\
		lv2:symbol \"control\" ;\n\
		lv2:name \"Control\"\n\
	]";
		++port_index;
	}

	for(std::size_t i = 0; i < plugin.getNumberOfMidiOutputs(); ++i)
	{
		output << "\
 , [\n\
		a atom:AtomPort ,\n\
			lv2:OutputPort;\n\
		atom:bufferType atom:Sequence ;\n\
		atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ;\n\
		lv2:index " << port_index << " ;\n\
		lv2:symbol \"control\" ;\n\
		lv2:name \"Control\"\n\
	]";
		++port_index;
	}

	std::size_t input_port_index = 1;
	for(std::size_t i = 0; i < plugin.getNumberOfAudioInputs(); ++i)
	{
		output << "\
 , [\n\
		a lv2:AudioPort ,\n\
			lv2:InputPort ;\n\
		lv2:index " << port_index << " ;\n\
		lv2:symbol \"in" << input_port_index << "\" ;\n\
		lv2:name \"In" << input_port_index << "\"\n\
	]";
		++port_index;
		++input_port_index;
	}

	std::size_t output_port_index = 1;
	for(std::size_t i = 0; i < plugin.getNumberOfAudioOutputs(); ++i)
	{
		output << "\
 , [\n\
		a lv2:AudioPort ,\n\
			lv2:OutputPort ;\n\
		lv2:index " << port_index << " ;\n\
		lv2:symbol \"out" << output_port_index << "\" ;\n\
		lv2:name \"Out" << output_port_index << "\"\n\
	]";
		++port_index;
		++output_port_index;
	}
}

void usage(const char* app)
{
	std::cout << "Usage: " << app << " <plugin binary> <output file> [uitype]\n";
	std::cout << "where uitype can be one of:\n";
	std::cout << " CocoaUI\n";
	std::cout << " Gtk3UI\n";
	std::cout << " GtkUI\n";
	std::cout << " Qt4UI\n";
	std::cout << " Qt5UI\n";
	std::cout << " WindowsUI\n";
	std::cout << " X11UI\n";
	std::cout << "default is " UIClass "\n";
}

UIType fromString(const std::string& t)
{
	if(t == "CocoaUI")
	{
		return UIType::CocoaUI;
	}
	if(t == "Gtk3UI")
	{
		return UIType::Gtk3UI;
	}
	if(t == "GtkUI")
	{
		return UIType::GtkUI;
	}
	if(t == "Qt4UI")
	{
		return UIType::Qt4UI;
	}
	if(t == "Qt5UI")
	{
		return UIType::Qt5UI;
	}
	if(t == "WindowsUI")
	{
		return UIType::WindowsUI;
	}
	if(t == "X11UI")
	{
		return UIType::X11UI;
	}

	std::cerr << "Bad uitype: '" << t << "'\n";
	exit(1);
}

int main(int argc, char* argv[])
{
	if(argc < 3 || argc > 4)
	{
		std::cerr << "Missing argument.\n";
		usage(argv[0]);
		return 1;
	}
	UIType uitype = UIType::X11UI;
	if(argc == 4)
	{
		uitype = fromString(argv[3]);
	}

	std::string library = argv[1];
	auto seppos = library.rfind("/");
	std::string binary = library;
	if(seppos != std::string::npos)
	{
		binary = library.substr(seppos + 1);
	}

	// load the plugin library
	void* plugin = dlopen(library.data(), RTLD_LAZY);
	if(!plugin)
	{
		std::cerr << "Cannot load library: " << dlerror() << std::endl;
		std::cerr << "Library must have absolute path or prefix ./myplugin.so.\n";
		return 1;
	}

	// reset errors
	dlerror();

	// load the symbols
	create_t* create_plugin = (create_t*) dlsym(plugin, "createEffectInstance");
	const char* dlsym_error = dlerror();
	if(dlsym_error)
	{
		std::cerr << "Cannot load symbol create: " << dlsym_error << '\n';
		return 1;
	}

	std::ofstream output(argv[2]);

	// create an instance of the class
	Plugin* p = create_plugin();

	header(output);
	output << std::endl;
	includes(output);
	output << std::endl;
	ui(*p, binary, uitype, output);
	output << std::endl;

	output << "\
<" << p->getURI() << "/lv2>\n\
	a lv2:Plugin ;\n\
	lv2:binary <" << binary << "> ;\n \
	a lv2:InstrumentPlugin ;\n\
	doap:name \"" << p->getEffectName() << "\" ;\n\
	doap:maintainer [\n\
		foaf:name \"" << p->getVendorString() << "\" ;\n\
		foaf:homepage <" << p->getHomepage() << "> ;\n\
	] ;\n\
	doap:license <http://usefulinc.com/doap/licenses/gpl> ;\n\
	ui:ui <" << p->getURI() << "/lv2#ui> ;\n\
	doap:license <http://opensource.org/licenses/gpl-3.0> ;\n\
	lv2:optionalFeature <http://lv2plug.in/ns/ext/uri-map> ;\n\
	lv2:optionalFeature <http://lv2plug.in/ns/ext/event> ;\n\
	lv2:optionalFeature idpy:queue_draw ;\n\
	lv2:extensionData state:interface ;\n\
	lv2:port [\n\
		a lv2:InputPort, lv2:ControlPort ;\n\
		lv2:index 0 ;\n\
		lv2:symbol \"lv2_freewheel\" ;\n\
		lv2:name \"Freewheel\" ;\n\
		lv2:default 0.0 ;\n\
		lv2:minimum 0.0 ;\n\
		lv2:maximum 1.0 ;\n\
		lv2:designation <http://lv2plug.in/ns/lv2core#freeWheeling> ;\n\
		lv2:portProperty <http://lv2plug.in/ns/lv2core#freeWheeling> ;\n\
		lv2:portProperty lv2:toggled ;\n\
		lv2:portProperty pprops:hasStrictBounds;\n\
	] , [\n\
		a lv2:OutputPort, lv2:ControlPort ;\n\
		lv2:designation <http://lv2plug.in/ns/lv2core#latency>;\n\
		lv2:index 1;\n\
		lv2:symbol \"latency\";\n\
		lv2:name \"Latency\";\n\
		lv2:minimum 0;\n\
		lv2:maximum 192000;\n\
		lv2:portProperty lv2:reportsLatency, lv2:integer;\n\
	]";

	ports(*p, output);

	output << " .\n";

	// FIXME: The plugin is never deleted, but since we're terminating now anyway
	// it doesn't matter much...

	// unload the plugin library
	dlclose(plugin);

	return 0;
}