diff options
-rw-r--r-- | slv2/Makefile.am | 2 | ||||
-rw-r--r-- | slv2/lv2-gtk2gui.h | 212 | ||||
-rw-r--r-- | slv2/plugin.h | 26 | ||||
-rw-r--r-- | slv2/pluginguiinstance.h | 125 | ||||
-rw-r--r-- | slv2/slv2.h | 1 | ||||
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/plugin.c | 39 | ||||
-rw-r--r-- | src/pluginguiinstance.c | 168 | ||||
-rw-r--r-- | src/slv2_internal.h | 9 | ||||
-rw-r--r-- | utils/lv2_inspect.c | 21 |
10 files changed, 592 insertions, 12 deletions
diff --git a/slv2/Makefile.am b/slv2/Makefile.am index 8e57eb1..e0a9c0e 100644 --- a/slv2/Makefile.am +++ b/slv2/Makefile.am @@ -2,9 +2,11 @@ slv2includedir = $(includedir)/slv2 slv2include_HEADERS = \ lv2.h \ + lv2-gtk2gui.h \ plugin.h \ pluginclass.h \ pluginclasses.h \ + pluginguiinstance.h \ plugininstance.h \ plugins.h \ port.h \ diff --git a/slv2/lv2-gtk2gui.h b/slv2/lv2-gtk2gui.h new file mode 100644 index 0000000..fd57d28 --- /dev/null +++ b/slv2/lv2-gtk2gui.h @@ -0,0 +1,212 @@ +/************************************************************************ + * + * GTK2 in-process UI extension for LV2 + * + * Copyright (C) 2006 Lars Luthman <lars.luthman@gmail.com> + * + * Based on lv2.h, which is + * + * Copyright (C) 2000-2002 Richard W.E. Furse, Paul Barton-Davis, Stefan + * Westerfeld + * Copyright (C) 2006 Steve Harris, Dave Robillard. + * + * This header 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 2.1 of the License, + * or (at your option) any later version. + * + * This header 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 this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA. + * + ***********************************************************************/ + +/** @file + This extension defines an interface that can be used in LV2 plugins and + hosts to create GTK2 GUIs for plugins. The GUIs will be plugins that + will reside in shared object files in a LV2 bundle and be referenced in + the RDF file using the triples +<pre> + <http://my.plugin> <http://ll-plugins.nongnu.org/lv2/ext/gtk2gui#gui> <http://my.plugingui> + <http://my.plugingui> <http://ll-plugins.nongnu.org/lv2/ext/gtk2gui#binary> <mygui.so> +</pre> + where <http://my.plugin> is the URI of the plugin, <http://my.plugingui> is + the URI of the plugin GUI and <mygui.so> is the relative URI to the shared + object file. While it is possible to have the plugin GUI and the plugin in + the same shared object file it is probably a good idea to keep them + separate so that hosts that don't want GUIs don't have to load the GUI code. + + It's entirely possible to have multiple GUIs for the same plugin, or to have + the GUI for a plugin in a different bundle from the actual plugin - this + way people other than the plugin author can write plugin GUIs independently + without editing the original plugin bundle. If a GUI is in a separate bundle + the first triple above should be in that bundle's manifest.ttl file so that + hosts can find the GUI when scanning the manifests. + + Note that the process that loads the shared object file containing the GUI + code and the process that loads the shared object file containing the + actual plugin implementation does not have to be the same. There are many + valid reasons for having the plugin and the GUI in different processes, or + even on different machines. This means that you can _not_ use singletons + and global variables and expect them to refer to the same objects in the + GUI and the actual plugin. The function callback interface defined in this + header is all you can expect to work. + + Since the LV2 specification itself allows for extensions that may add + new types of data and configuration parameters that plugin authors may + want to control with a GUI, this extension allows for meta-extensions that + can extend the interface between the GUI and the host. See the + instantiate() and extension_data() callback pointers for more details. + + Note that this extension is NOT a Host Feature. There is no way for a plugin + to know whether the host that loads it supports GUIs or not, and the plugin + must ALWAYS work without the GUI (although it may be rather useless unless + it has been configured using the GUI in a previous session). + + GUIs written to this specification do not need to be threadsafe - the + functions defined below may only be called in the same thread as the GTK + main loop is running in. +*/ + +#ifndef LV2_GTK2GUI_H +#define LV2_GTK2GUI_H + +#include "lv2.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +/* The original header includes <gtk/gtkwidget.h>, but we don't want + libslv2 to depend on GTK+ so we forward declare the _GtkWidget struct + instead. */ +struct _GtkWidget; + + + +/** This handle indicates a particular instance of a GUI. + It is valid to compare this to NULL (0 for C++) but otherwise the + host MUST not attempt to interpret it. The GUI plugin may use it to + reference internal instance data. */ +typedef void* LV2UI_Handle; + + +/** This handle indicates a particular plugin instance, provided by the host. + It is valid to compare this to NULL (0 for C++) but otherwise the + GUI plugin MUST not attempt to interpret it. The host may use it to + reference internal instance data. */ +typedef void* LV2UI_Controller; + + +/** This is the type of the host-provided function that changes the value + of a control rate float input port in a plugin instance. A function + pointer of this type will be provided to the GUI bu the host in the + instantiate() function. */ +typedef void (*LV2UI_Set_Control_Function)(LV2UI_Controller controller, + uint32_t port, + float value); + + +typedef struct _LV2UI_Descriptor { + + /** The URI for this GUI (not for the plugin it controls). */ + const char* URI; + + /** Create a new GUI object and return a handle to it. This function works + similarly to the instantiate() member in LV2_Descriptor, with the + additions that the URI for the plugin that this GUI is for is passed + as a parameter, a function pointer and a controller handle are passed to + allow the plugin to change control port values in the plugin + (control_function and controller) and a pointer to a GtkWidget pointer + is passed, which the GUI plugin should set to point to a newly created + widget which will be the main GUI for the plugin. + + The features array works like the one in the instantiate() member + in LV2_Descriptor, except that the URIs should be denoted with the triples + <pre> + <http://my.plugingui> <http://ll-plugins.nongnu.org/lv2/dev/gtk2gui#optionalFeature> <http://my.guifeature> + </pre> + or + <pre> + <http://my.plugingui> <http://ll-plugins.nongnu.org/lv2/dev/gtk2gui#requiredFeature> <http://my.guifeature> + </pre> + in the RDF file, instead of the lv2:optionalFeature or lv2:requiredFeature + that is used by host features. These features are associated with the GUI, + not with the plugin - they are not actually LV2 Host Features, they just + use the same data structure. + + The same rules apply for these features as for normal host features - + if a feature is listed as required in the RDF file and the host does not + support it, it must not load the GUI. + */ + LV2UI_Handle (*instantiate)(const struct _LV2UI_Descriptor* descriptor, + const char* plugin_uri, + const char* bundle_path, + LV2UI_Set_Control_Function control_function, + LV2UI_Controller controller, + struct _GtkWidget** widget, + const LV2_Host_Feature** features); + + + /** Destroy the GUI object and the associated widget. + */ + void (*cleanup)(LV2UI_Handle gui); + + /** Tell the GUI that a control port value has changed. This member may be + set to NULL if the GUI is not interested in control port changes. + */ + void (*set_control)(LV2UI_Handle gui, + uint32_t port, + float value); + + /** Returns a data structure associated with an extension URI, for example + a struct containing additional function pointers. Avoid returning + function pointers directly since standard C++ has no valid way of + casting a void* to a function pointer. This member may be set to NULL + if the GUI is not interested in supporting any extensions. This is similar + to the extension_data() member in LV2_Descriptor. + */ + void* (*extension_data)(LV2UI_Handle gui, + const char* uri); + +} LV2UI_Descriptor; + + + +/** Accessing a plugin GUI. + + A plugin programmer must include a function called "lv2ui_descriptor" + with the following function prototype within the shared object + file. This function will have C-style linkage (if you are using + C++ this is taken care of by the 'extern "C"' clause at the top of + the file). This function will be accessed by the GUI host using the + @c dlsym() function and called to get a LV2UI_UIDescriptor for the + wanted plugin. + + Just like lv2_descriptor(), this function takes an index parameter. The + index should only be used for enumeration and not as any sort of ID number - + the host should just iterate from 0 and upwards until the function returns + NULL, or a descriptor with an URI matching the one the host is looking for + is returned. +*/ +const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index); + + +/** This is the type of the lv2ui_descriptor() function. */ +typedef const LV2UI_Descriptor* (*LV2UI_DescriptorFunction)(uint32_t index); + + + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/slv2/plugin.h b/slv2/plugin.h index 4d53bb3..9dc53e7 100644 --- a/slv2/plugin.h +++ b/slv2/plugin.h @@ -321,6 +321,32 @@ slv2_plugin_get_port_by_symbol(SLV2Plugin plugin, const char* symbol); +/** Get a list of all GUIs available for this plugin. + * + * Note this returns the URI of the GUI, and not the path/URI to its shared + * library, use slv2_plugin_gui_get_library_uri with the values returned + * here for that. + * + * \param plugin The plugin to get the GUIs for. + * + * Time = Query + */ +SLV2Values +slv2_plugin_get_guis(SLV2Plugin plugin); + + +/** Get the URI for a GUI library. + * + * \param plugin The plugin that the GUI is for. + * \param gui A GUI identifier as returned by slv2_plugin_get_guis(). + * + * Time = Query + */ +SLV2Value +slv2_plugin_gui_get_library_uri(SLV2Plugin plugin, + SLV2Value gui); + + /** @} */ #ifdef __cplusplus diff --git a/slv2/pluginguiinstance.h b/slv2/pluginguiinstance.h new file mode 100644 index 0000000..82628a7 --- /dev/null +++ b/slv2/pluginguiinstance.h @@ -0,0 +1,125 @@ +/* SLV2 + * Copyright (C) 2007 Dave Robillard <http://drobilla.net> + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This library 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 General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __SLV2_PLUGINGUIINSTANCE_H__ +#define __SLV2_PLUGINGUIINSTANCE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <assert.h> +#include <slv2/lv2-gtk2gui.h> +#include <slv2/plugin.h> + +/** \defgroup lib Plugin GUI library access + * + * An SLV2GUIInstance is an instantiated GUI for a SLV2Plugin. GUI instances + * are loaded from dynamically loaded libraries. These functions interact + * with the GUI code in the binary library only, they do not read data files + * in any way. + * + * @{ + */ + + +typedef struct _GUIInstanceImpl* SLV2GUIInstanceImpl; + + +struct _GtkWidget; + + +/** Instance of a plugin GUI. + * + * All details are in hidden in the pimpl member to avoid making the + * implementation a part of the ABI. + */ +typedef struct _GUIInstance { + SLV2GUIInstanceImpl pimpl; ///< Private implementation +}* SLV2GUIInstance; + + + +/** Instantiate a plugin GUI. + * + * The returned object represents shared library objects loaded into memory, + * it must be cleaned up with slv2_gui_instance_free when no longer + * needed. + * + * \a plugin is not modified or directly referenced by the returned object + * (instances store only a copy of the plugin's URI). + * + * \a host_features NULL-terminated array of features the host supports. + * NULL may be passed if the host supports no additional features (unlike + * the LV2 specification - SLV2 takes care of it). + * + * \return NULL if instantiation failed. + */ +SLV2GUIInstance +slv2_plugin_gui_instantiate(SLV2Plugin plugin, + SLV2Value gui, + LV2UI_Set_Control_Function control_function, + LV2UI_Controller controller, + const LV2_Host_Feature** host_features); + + +/** Free a plugin GUI instance. + * + * \a instance is invalid after this call. + */ +void +slv2_gui_instance_free(SLV2GUIInstance instance); + + +/** Get the GTK+ 2.0 widget for the GUI instance. + */ +struct _GtkWidget* +slv2_gui_instance_get_widget(SLV2GUIInstance instance); + + +/** Get the LV2UI_Descriptor of the plugin GUI instance. + * + * Normally hosts should not need to access the LV2UI_Descriptor directly, + * use the slv2_gui_instance_* functions. + * + * The returned descriptor is shared and must not be deleted. + */ +const LV2UI_Descriptor* +slv2_gui_instance_get_descriptor(SLV2GUIInstance instance); + + +/** Get the LV2UI_Handle of the plugin GUI instance. + * + * Normally hosts should not need to access the LV2UI_Handle directly, + * use the slv2_gui_instance_* functions. + * + * The returned handle is shared and must not be deleted. + */ +LV2_Handle +slv2_gui_instance_get_handle(SLV2GUIInstance instance); + + +/** @} */ + +#ifdef __cplusplus +} +#endif + + +#endif /* __SLV2_PLUGINGUIINSTANCE_H__ */ + diff --git a/slv2/slv2.h b/slv2/slv2.h index 9573e9c..e5c2b07 100644 --- a/slv2/slv2.h +++ b/slv2/slv2.h @@ -29,6 +29,7 @@ extern "C" { #include <slv2/plugin.h> #include <slv2/port.h> #include <slv2/plugins.h> +#include <slv2/pluginguiinstance.h> #include <slv2/plugininstance.h> #include <slv2/value.h> #include <slv2/values.h> diff --git a/src/Makefile.am b/src/Makefile.am index c7c70f6..43543b5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,6 +8,7 @@ libslv2_la_SOURCES = \ plugin.c \ pluginclass.c \ pluginclasses.c \ + pluginguiinstance.c \ plugininstance.c \ plugins.c \ port.c \ diff --git a/src/plugin.c b/src/plugin.c index 7dba104..1d63978 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -541,3 +541,42 @@ slv2_plugin_get_port_by_symbol(SLV2Plugin p, return NULL; } + +SLV2Values +slv2_plugin_get_guis(SLV2Plugin plugin) +{ + if (!plugin->rdf) + slv2_plugin_load(plugin); + + return slv2_plugin_get_value(plugin, SLV2_URI, + "http://ll-plugins.nongnu.org/lv2/ext/gtk2gui#gui"); +} + + +SLV2Value +slv2_plugin_gui_get_library_uri(SLV2Plugin plugin, + SLV2Value gui) +{ + if (!plugin->rdf) + slv2_plugin_load(plugin); + + SLV2Values values = slv2_plugin_get_value_for_subject(plugin, gui, SLV2_URI, + "http://ll-plugins.nongnu.org/lv2/ext/gtk2gui#binary"); + + if (!values || slv2_values_size(values) == 0) { + slv2_values_free(values); + return NULL; + } + + SLV2Value value = slv2_values_get_at(values, 0); + if (!slv2_value_is_uri(value)) { + slv2_values_free(values); + return NULL; + } + + value = slv2_value_duplicate(value); + slv2_values_free(values); + + return value; +} + diff --git a/src/pluginguiinstance.c b/src/pluginguiinstance.c new file mode 100644 index 0000000..985f253 --- /dev/null +++ b/src/pluginguiinstance.c @@ -0,0 +1,168 @@ +/* SLV2 + * Copyright (C) 2007 Dave Robillard <http://drobilla.net> + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This library 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 General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#define _XOPEN_SOURCE 500 + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <assert.h> +#include <dlfcn.h> +#include <slv2/types.h> +#include <slv2/plugin.h> +#include <slv2/pluginguiinstance.h> +#include <slv2/util.h> +#include "slv2_internal.h" + + +SLV2GUIInstance +slv2_plugin_gui_instantiate(SLV2Plugin plugin, + SLV2Value gui, + LV2UI_Set_Control_Function control_function, + LV2UI_Controller controller, + const LV2_Host_Feature** host_features) +{ + struct _GUIInstance* result = NULL; + + bool local_host_features = (host_features == NULL); + if (local_host_features) { + host_features = malloc(sizeof(LV2_Host_Feature)); + host_features[0] = NULL; + } + + const char* const lib_uri = slv2_value_as_uri(slv2_plugin_gui_get_library_uri(plugin, gui)); + const char* const lib_path = slv2_uri_to_path(lib_uri); + + if (!lib_path) + return NULL; + + dlerror(); + void* lib = dlopen(lib_path, RTLD_NOW); + if (!lib) { + fprintf(stderr, "Unable to open GUI library %s (%s)\n", lib_path, dlerror()); + return NULL; + } + + LV2UI_DescriptorFunction df = dlsym(lib, "lv2ui_descriptor"); + + if (!df) { + fprintf(stderr, "Could not find symbol 'lv2ui_descriptor', " + "%s is not a LV2 plugin GUI.\n", lib_path); + dlclose(lib); + return NULL; + } else { + // Search for plugin by URI + + // FIXME: Kluge to get bundle path (containing directory of binary) + char* bundle_path = strdup(plugin->binary_uri); + char* const bundle_path_end = strrchr(bundle_path, '/'); + if (bundle_path_end) + *(bundle_path_end+1) = '\0'; + printf("GUI bundle path: %s\n", bundle_path); + + for (uint32_t i=0; 1; ++i) { + + const LV2UI_Descriptor* ld = df(i); + + if (!ld) { + fprintf(stderr, "Did not find GUI %s in %s\n", + slv2_value_as_uri(gui), lib_path); + dlclose(lib); + break; // return NULL + } else if (!strcmp(ld->URI, slv2_value_as_uri(gui))) { + + printf("Found GUI %s at index %u in:\n\t%s\n\n", + librdf_uri_as_string(plugin->plugin_uri), i, lib_path); + + assert(ld->instantiate); + + // Create SLV2GUIInstance to return + result = malloc(sizeof(struct _GUIInstance)); + struct _GUIInstanceImpl* impl = malloc(sizeof(struct _GUIInstanceImpl)); + impl->lv2ui_descriptor = ld; + impl->lv2ui_handle = ld->instantiate(ld, + slv2_plugin_get_uri(plugin), + (char*)bundle_path, + control_function, + controller, + (struct _GtkWidget**)&impl->widget, + host_features); + impl->lib_handle = lib; + result->pimpl = impl; + break; + } + } + + free(bundle_path); + } + + assert(result); + assert(slv2_plugin_get_num_ports(plugin) > 0); + + // Failed to instantiate + if (result->pimpl->lv2ui_handle == NULL) { + //printf("Failed to instantiate %s\n", plugin->plugin_uri); + free(result); + return NULL; + } + + // Failed to create a widget, but still got a handle - this means that + // the plugin is buggy + if (result->pimpl->widget == NULL) { + slv2_gui_instance_free(result); + return NULL; + } + + if (local_host_features) + free(host_features); + + return result; +} + + +void +slv2_gui_instance_free(SLV2GUIInstance instance) +{ + struct _GUIInstance* i = (struct _GUIInstance*)instance; + i->pimpl->lv2ui_descriptor->cleanup(i->pimpl->lv2ui_handle); + i->pimpl->lv2ui_descriptor = NULL; + dlclose(i->pimpl->lib_handle); + i->pimpl->lib_handle = NULL; + free(i->pimpl); + i->pimpl = NULL; + free(i); +} + + +struct _GtkWidget* +slv2_gui_instance_get_widget(SLV2GUIInstance instance) { + return instance->pimpl->widget; +} + + +const LV2UI_Descriptor* +slv2_gui_instance_get_descriptor(SLV2GUIInstance instance) { + return instance->pimpl->lv2ui_descriptor; +} + + +LV2_Handle +slv2_gui_instance_get_handle(SLV2GUIInstance instance) { + return instance->pimpl->lv2ui_handle; +} + diff --git a/src/slv2_internal.h b/src/slv2_internal.h index 31913ad..4d20e4f 100644 --- a/src/slv2_internal.h +++ b/src/slv2_internal.h @@ -28,6 +28,7 @@ extern "C" { #include <inttypes.h> #include <librdf.h> #include <slv2/types.h> +#include <slv2/lv2-gtk2gui.h> @@ -97,6 +98,14 @@ struct _InstanceImpl { }; +/* ********* GUI Instance ********* */ +struct _GUIInstanceImpl { + void* lib_handle; + const LV2UI_Descriptor* lv2ui_descriptor; + LV2UI_Handle lv2ui_handle; + void* widget; +}; + /* ********* Plugin Class ********* */ diff --git a/utils/lv2_inspect.c b/utils/lv2_inspect.c index 03f4e14..a7a5bf0 100644 --- a/utils/lv2_inspect.c +++ b/utils/lv2_inspect.c @@ -109,21 +109,18 @@ print_plugin(SLV2Plugin p) printf("\tBinary: %s\n\n", slv2_plugin_get_library_uri(p)); - SLV2Values gui = slv2_plugin_get_value(p, SLV2_URI, - "http://ll-plugins.nongnu.org/lv2/ext/gtk2gui#gui"); + SLV2Values gui = slv2_plugin_get_guis(p); if (slv2_values_size(gui) > 0) { printf("\tGUI:\n"); for (unsigned i=0; i < slv2_values_size(gui); ++i) { printf("\t\t%s\n", slv2_value_as_string(slv2_values_get_at(gui, i))); - SLV2Values binary = slv2_plugin_get_value_for_subject(p, - slv2_values_get_at(gui, i), - SLV2_URI, - "http://ll-plugins.nongnu.org/lv2/ext/gtk2gui#binary"); + SLV2Value binary = slv2_plugin_gui_get_library_uri(p, + slv2_values_get_at(gui, i)); - if (slv2_values_size(binary) > 0) - printf("\t\t\tBinary: %s\n", slv2_value_as_uri(slv2_values_get_at(binary, 0))); - - slv2_values_free(binary); + if (binary) + printf("\t\t\tBinary: %s\n", slv2_value_as_uri(binary)); + + slv2_value_free(binary); } } slv2_values_free(gui); @@ -158,8 +155,8 @@ print_plugin(SLV2Plugin p) for (unsigned i=0; i < slv2_values_size(v); ++i) printf("\t\t%s\n", slv2_value_as_uri(slv2_values_get_at(v, i))); slv2_values_free(v); - - + + /* Ports */ uint32_t num_ports = slv2_plugin_get_num_ports(p); |