1 /*
2   Copyright 2010-2016 David Robillard <http://drobilla.net>
3   Copyright 2010 Leonard Ritter <paniq@paniq.org>
4   Copyright 2018 Ethan Reker <http://cutthroughrecordings.com>
5 
6   Permission to use, copy, modify, and/or distribute this software for any
7   purpose with or without fee is hereby granted, provided that the above
8   copyright notice and this permission notice appear in all copies.
9 
10   THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 
19 /**
20    @defgroup state State
21 
22    An interface for LV2 plugins to save and restore state, see
23    <http://lv2plug.in/ns/ext/state> for details.
24 
25    @{
26 */
27 module dplug.lv2.state;
28 
29 version(LV2):
30 
31 nothrow @nogc:
32 
33 import core.stdc.stddef;
34 import core.stdc.stdint;
35 
36 import dplug.lv2.lv2;
37 
38 enum LV2_STATE_URI    = "http://lv2plug.in/ns/ext/state";  ///< http://lv2plug.in/ns/ext/state
39 enum LV2_STATE_PREFIX = LV2_STATE_URI ~ "#";                 ///< http://lv2plug.in/ns/ext/state#
40 
41 enum LV2_STATE__State             = LV2_STATE_PREFIX ~ "State";              ///< http://lv2plug.in/ns/ext/state#State
42 enum LV2_STATE__interface         = LV2_STATE_PREFIX ~ "interface";          ///< http://lv2plug.in/ns/ext/state#interface
43 enum LV2_STATE__loadDefaultState  = LV2_STATE_PREFIX ~ "loadDefaultState";   ///< http://lv2plug.in/ns/ext/state#loadDefaultState
44 enum LV2_STATE__makePath          = LV2_STATE_PREFIX ~ "makePath";           ///< http://lv2plug.in/ns/ext/state#makePath
45 enum LV2_STATE__mapPath           = LV2_STATE_PREFIX ~ "mapPath";            ///< http://lv2plug.in/ns/ext/state#mapPath
46 enum LV2_STATE__state             = LV2_STATE_PREFIX ~ "state";              ///< http://lv2plug.in/ns/ext/state#state
47 enum LV2_STATE__threadSafeRestore = LV2_STATE_PREFIX ~ "threadSafeRestore";  ///< http://lv2plug.in/ns/ext/state#threadSafeRestore
48 
49 alias LV2_State_Handle           = void*; ///b< Opaque handle for state save/restore
50 alias LV2_State_Map_Path_Handle  = void*; ///< Opaque handle for state:mapPath feature
51 alias LV2_State_Make_Path_Handle = void*; ///< Opaque handle for state:makePath feature
52 
53 /**
54    Flags describing value characteristics.
55 
56    These flags are used along with the value's type URI to determine how to
57    (de-)serialise the value data, or whether it is even possible to do so.
58 */
59 alias LV2_State_Flags = int;
60 enum : LV2_State_Flags {
61    /**
62       Plain Old Data.
63 
64       Values with this flag contain no pointers or references to other areas
65       of memory.  It is safe to copy POD values with a simple memcpy and store
66       them for the duration of the process.  A POD value is not necessarily
67       safe to transmit between processes or machines (e.g. filenames are POD),
68       see LV2_STATE_IS_PORTABLE for details.
69 
70       Implementations MUST NOT attempt to copy or serialise a non-POD value if
71       they do not understand its type (and thus know how to correctly do so).
72    */
73    LV2_STATE_IS_POD = 1,
74 
75    /**
76       Portable (architecture independent) data.
77 
78       Values with this flag are in a format that is usable on any
79       architecture.  A portable value saved on one machine can be restored on
80       another machine regardless of architecture.  The format of portable
81       values MUST NOT depend on architecture-specific properties like
82       endianness or alignment.  Portable values MUST NOT contain filenames.
83    */
84    LV2_STATE_IS_PORTABLE = 1 << 1,
85 
86    /**
87       Native data.
88 
89       This flag is used by the host to indicate that the saved data is only
90       going to be used locally in the currently running process (e.g. for
91       instance duplication or snapshots), so the plugin should use the most
92       efficient representation possible and not worry about serialisation
93       and portability.
94    */
95    LV2_STATE_IS_NATIVE = 1 << 2
96 }
97 
98 /** A status code for state functions. */
99 alias LV2_State_Status = int;
100 enum : int {
101    LV2_STATE_SUCCESS         = 0,  /**< Completed successfully. */
102    LV2_STATE_ERR_UNKNOWN     = 1,  /**< Unknown error. */
103    LV2_STATE_ERR_BAD_TYPE    = 2,  /**< Failed due to unsupported type. */
104    LV2_STATE_ERR_BAD_FLAGS   = 3,  /**< Failed due to unsupported flags. */
105    LV2_STATE_ERR_NO_FEATURE  = 4,  /**< Failed due to missing features. */
106    LV2_STATE_ERR_NO_PROPERTY = 5,  /**< Failed due to missing property. */
107    LV2_STATE_ERR_NO_SPACE    = 6   /**< Failed due to insufficient space. */
108 }
109 
110 /**
111    A host-provided function to store a property.
112    @param handle Must be the handle passed to LV2_State_Interface.save().
113    @param key The key to store `value` under (URID).
114    @param value Pointer to the value to be stored.
115    @param size The size of `value` in bytes.
116    @param type The type of `value` (URID).
117    @param flags LV2_State_Flags for `value`.
118    @return 0 on success, otherwise a non-zero error code.
119 
120    The host passes a callback of this type to LV2_State_Interface.save(). This
121    callback is called repeatedly by the plugin to store all the properties that
122    describe its current state.
123 
124    DO NOT INVENT NONSENSE URI SCHEMES FOR THE KEY.  Best is to use keys from
125    existing vocabularies.  If nothing appropriate is available, use http URIs
126    that point to somewhere you can host documents so documentation can be made
127    resolvable (e.g. a child of the plugin or project URI).  If this is not
128    possible, invent a URN scheme, e.g. urn:myproj:whatever.  The plugin MUST
129    NOT pass an invalid URI key.
130 
131    The host MAY fail to store a property for whatever reason, but SHOULD
132    store any property that is LV2_STATE_IS_POD and LV2_STATE_IS_PORTABLE.
133    Implementations SHOULD use the types from the LV2 Atom extension
134    (http://lv2plug.in/ns/ext/atom) wherever possible.  The plugin SHOULD
135    attempt to fall-back and avoid the error if possible.
136 
137    Note that `size` MUST be > 0, and `value` MUST point to a valid region of
138    memory `size` bytes long (this is required to make restore unambiguous).
139 
140    The plugin MUST NOT attempt to use this function outside of the
141    LV2_State_Interface.restore() context.
142 */
143 extern(C)
144 {
145     alias LV2_State_Store_Function = LV2_State_Status function(LV2_State_Handle handle, uint key, const(void)* value, size_t size, uint type, uint flags);
146 }
147 
148 /**
149    A host-provided function to retrieve a property.
150    @param handle Must be the handle passed to LV2_State_Interface.restore().
151    @param key The key of the property to retrieve (URID).
152    @param size (Output) If non-NULL, set to the size of the restored value.
153    @param type (Output) If non-NULL, set to the type of the restored value.
154    @param flags (Output) If non-NULL, set to the flags for the restored value.
155    @return A pointer to the restored value (object), or NULL if no value
156    has been stored under `key`.
157 
158    A callback of this type is passed by the host to
159    LV2_State_Interface.restore().  This callback is called repeatedly by the
160    plugin to retrieve any properties it requires to restore its state.
161 
162    The returned value MUST remain valid until LV2_State_Interface.restore()
163    returns.  The plugin MUST NOT attempt to use this function, or any value
164    returned from it, outside of the LV2_State_Interface.restore() context.
165 */
166 extern(C)
167 {
168     alias LV2_State_Retrieve_Function = const(void)* function(LV2_State_Handle handle, uint key, size_t* size, uint* type, uint* flags);
169 }
170 
171 /**
172    LV2 Plugin State Interface.
173 
174    When the plugin's extension_data is called with argument
175    LV2_STATE__interface, the plugin MUST return an LV2_State_Interface
176    structure, which remains valid for the lifetime of the plugin.
177 
178    The host can use the contained function pointers to save and restore the
179    state of a plugin instance at any time, provided the threading restrictions
180    of the functions are met.
181 
182    Stored data is only guaranteed to be compatible between instances of plugins
183    with the same URI (i.e. if a change to a plugin would cause a fatal error
184    when restoring state saved by a previous version of that plugin, the plugin
185    URI MUST change just as it must when ports change incompatibly).  Plugin
186    authors should consider this possibility, and always store sensible data
187    with meaningful types to avoid such problems in the future.
188 */
189 struct LV2_State_Interface 
190 {
191 nothrow @nogc:
192    /**
193       Save plugin state using a host-provided `store` callback.
194 
195       @param instance The instance handle of the plugin.
196       @param store The host-provided store callback.
197       @param handle An opaque pointer to host data which MUST be passed as the
198       handle parameter to `store` if it is called.
199       @param flags Flags describing desired properties of this save.  These
200       flags may be used to determine the most appropriate values to store.
201       @param features Extensible parameter for passing any additional
202       features to be used for this save.
203 
204       The plugin is expected to store everything necessary to completely
205       restore its state later.  Plugins SHOULD store simple POD data whenever
206       possible, and consider the possibility of state being restored much
207       later on a different machine.
208 
209       The `handle` pointer and `store` function MUST NOT be used
210       beyond the scope of save().
211 
212       This function has its own special threading class: it may not be called
213       concurrently with any "Instantiation" function, but it may be called
214       concurrently with functions in any other class, unless the definition of
215       that class prohibits it (e.g. it may not be called concurrently with a
216       "Discovery" function, but it may be called concurrently with an "Audio"
217       function.  The plugin is responsible for any locking or lock-free
218       techniques necessary to make this possible.
219 
220       Note that in the simple case where state is only modified by restore(),
221       there are no synchronization issues since save() is never called
222       concurrently with restore() (though run() may read it during a save).
223 
224       Plugins that dynamically modify state while running, however, must take
225       care to do so in such a way that a concurrent call to save() will save a
226       consistent representation of plugin state for a single instant in time.
227    */
228     extern(C)
229     {
230         alias saveFun_t = LV2_State_Status function(LV2_Handle                 instance,
231                                                   LV2_State_Store_Function   store,
232                                                   LV2_State_Handle           handle,
233                                                   uint                   flags,
234                                                   const(LV2_Feature*)*       features);
235     }
236     saveFun_t save;
237 
238    /**
239       Restore plugin state using a host-provided `retrieve` callback.
240 
241       @param instance The instance handle of the plugin.
242       @param retrieve The host-provided retrieve callback.
243       @param handle An opaque pointer to host data which MUST be passed as the
244       handle parameter to `retrieve` if it is called.
245       @param flags Currently unused.
246       @param features Extensible parameter for passing any additional
247       features to be used for this restore.
248 
249       The plugin MAY assume a restored value was set by a previous call to
250       LV2_State_Interface.save() by a plugin with the same URI.
251 
252       The plugin MUST gracefully fall back to a default value when a value can
253       not be retrieved.  This allows the host to reset the plugin state with
254       an empty map.
255 
256       The `handle` pointer and `store` function MUST NOT be used
257       beyond the scope of restore().
258 
259       This function is in the "Instantiation" threading class as defined by
260       LV2. This means it MUST NOT be called concurrently with any other
261       function on the same plugin instance.
262    */
263    extern(C)
264    {
265        alias restoreFun_t = LV2_State_Status function(LV2_Handle                  instance,
266                                                    LV2_State_Retrieve_Function retrieve,
267                                                    LV2_State_Handle            handle,
268                                                    uint                    flags,
269                                                    const(LV2_Feature*)*       features);
270    }
271    restoreFun_t restore;
272    
273 };