# LV2 Persist Extension # Copyright 2010-2011 David Robillard # Copyright 2010 Leonard Ritter # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. @prefix persist: . @prefix doap: . @prefix foaf: . @prefix lv2: . @prefix rdf: . @prefix rdfs: . @prefix xsd: . a lv2:Specification ; doap:name "LV2 Persist" ; doap:license ; doap:release [ doap:revision "0.3" ; doap:created "2011-03-29" ] ; doap:developer [ a foaf:Person ; foaf:name "Leonard Ritter" ; foaf:homepage ; ] ; doap:maintainer [ a foaf:Person ; foaf:name "David Robillard" ; foaf:homepage ; rdfs:seeAlso ] ; lv2:documentation """

This extension provides a mechanism for plugins to save and restore state across instances, allowing hosts to save configuration/state/data with a project or fully clone (i.e. make a deep copy of) a plugin instance.

This extension allows plugins to save private state data, i.e. data that is not contained in input ports. The motivating ideal is for the state of a plugin instance to be entirely described by port values (as with all LV2 plugins) and a key/value dictionary as defined by this extension. This mechanism is simple, yet sufficiently powerful to describe arbitrarily advanced state.

The state described by this extension is conceptually a single key/value dictionary. Keys are URIs, and values are typed-tagged blobs of any type. The plugin provides a save and restore method for saving and restoring state. To initiate a save or restore, the host calls these methods, passing a callback to be used for saving or restoring a key/value pair. This data is available to the host, allowing state to be easily used in many different way. The actual mechanism of saving and restoring state is completely abstract from the plugin's perspective.

Because the state is a simple dictionary, hosts and plugins can work with state easily (virtually all programming languages have an appropriate dictionary type available). Additionally, this format is simple and terse to serialise in many formats (e.g. any RDF syntax, JSON, XML, key/value databases such as BDB, etc.). In particular, state can be elegantly described in a plugin's Turtle description, which is useful for e.g. presets or default state. Note that these are simply possibilities; this extension defines only a few function prototypes and does not require the use of any particular syntax, data structure, file system, or other implementation detail. Hosts are free to work with plugin state in whatever way is most appropriate for that host.

This extension makes it possible for plugins to save private data, but state is not necessarily private, e.g. a plugin could have a public interface for inspecting and manipulating internal state, which would be saved using this extension. Plugins and extensions SHOULD express state changes as modifications to this key/value dictionary, and use meaningful types wherever possible. Extensions may define a dynamic mechanism for accessing plugin state, or conventional state keys likely to be useful to several implementations.

In pseudo code, a typical use case in a plugin is:

#define NS_EG   "http://example.org/"
#define NS_ATOM "http://lv2plug.in/ns/ext/atom#"
#define NS_XSD  "http://www.w3.org/2001/XMLSchema#"

static const char* const KEY_GREETING = "http://example.org/greeting";

LV2_Handle my_instantiate(...)
{
    MyPlugin*            plugin = ...;
    LV2_URI_Map_Feature* map    = ...;
    plugin->uri_greeting_key = map->uri_to_id(..., NULL, NS_EG "greeting-key");
    plugin->uri_xsd_string   = map->uri_to_id(..., NULL, NS_XSD "string");
    plugin->state->greeting  = strdup("Hello");
    return plugin;
}

void my_save(LV2_Handle                 instance,
             LV2_Persist_Store_Function store,
             void*                      callback_data)
{
    MyPlugin*   plugin   = (MyPlugin*)instance;
    const char* greeting = plugin->state->greeting;

    store(callback_data,
          plugin->uri_greeting_key,
          greeting,
          strlen(greeting) + 1,
          plugin->uri_xsd_string,
          LV2_PERSIST_IS_POD | LV2_PERSIST_IS_PORTABLE);
}

void my_restore(LV2_Handle                    instance,
                LV2_Persist_Retrieve_Function retrieve,
                void*                         callback_data)
{
    MyPlugin* plugin = (MyPlugin*)instance;

    size_t      size;
    uint32_t    type;
    uint32_t    flags;
    const char* greeting = retrieve(callback_data,
                                    plugin->uri_greeting_key,
                                    &size,
                                    &type,
                                    &flags);

    if (greeting) {
        free(plugin->state->greeting);
        plugin->state->greeting = strdup(greeting);
    } else {
        plugin->state->greeting = strdup("Hello");
    }
}

Similarly, a typical use case in a host is:

int store_callback(void*       callback_data,
                   uint32_t    key,
                   const void* value,
                   size_t      size,
                   uint32_t    type,
                   uint32_t    flags)
{
    if ((flags & LV2_PERSIST_IS_POD)) {
        /* Keeping state in memory only, if disk was involved then
           LV2_PERSIST_IS_PORTABLE would have to be checked as well.
        */
        Map* state_map = (Map*)callback_data;
        state_map->insert(key, Value(copy(value), size, type, pod));
        return 0;
    } else {
        return 1; /* Non-POD events are unsupported. */
    }
}

Map get_plugin_state(LV2_Handle instance)
{
    LV2_Persist* persist = instance.extension_data("http://lv2plug.in/ns/ext/persist");
    Map state_map;
    persist.save(instance, store_callback, &state_map);
    return state_map;
}
""" . persist:InstanceState a rdfs:Class ; rdfs:label "Plugin Instance State" ; rdfs:comment """ This class is used to express a plugin instance's state in RDF. The key/value properties of the instance form the predicate/object (respectively) of triples with a persist:InstanceState as the subject (see persist:instanceState for an example). This may be used wherever it is useful to express a plugin instance's state in RDF (e.g. for serialisation, storing in a model, or transmitting over a network). Note that this class is provided because it may be useful for hosts, plugins, or extensions that work with instance state, but its use is not required to support the LV2 Persist extension. """ . persist:instanceState a rdf:Property ; rdfs:range persist:InstanceState ; lv2:documentation """ Predicate to relate a plugin instance to an InstanceState. This may be used wherever the state of a particular plugin instance needs to be represented. Note that the domain of this property is unspecified, since LV2 does not define any RDF class for plugin instance. This predicate may be used wherever it makes sense to do so, e.g.:
@prefix eg: <http://example.org/> .

<plugininstance> persist:instanceState [
    eg:somekey "some value" ;
    eg:someotherkey "some other value" ;
    eg:favourite-number 2 .
]
Note that this property is provided because it may be useful for hosts, plugins, or extensions that work with instance state, but its use is not required to support the LV2 Persist extension. """ .