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 import core.stdc.stddef;
32 import core.stdc.stdint;
33 
34 import dplug.lv2.lv2;
35 
36 enum LV2_STATE_URI    = "http://lv2plug.in/ns/ext/state";  ///< http://lv2plug.in/ns/ext/state
37 enum LV2_STATE_PREFIX = LV2_STATE_URI ~ "#";                 ///< http://lv2plug.in/ns/ext/state#
38 
39 enum LV2_STATE__State             = LV2_STATE_PREFIX ~ "State";              ///< http://lv2plug.in/ns/ext/state#State
40 enum LV2_STATE__interface         = LV2_STATE_PREFIX ~ "interface";          ///< http://lv2plug.in/ns/ext/state#interface
41 enum LV2_STATE__loadDefaultState  = LV2_STATE_PREFIX ~ "loadDefaultState";   ///< http://lv2plug.in/ns/ext/state#loadDefaultState
42 enum LV2_STATE__makePath          = LV2_STATE_PREFIX ~ "makePath";           ///< http://lv2plug.in/ns/ext/state#makePath
43 enum LV2_STATE__mapPath           = LV2_STATE_PREFIX ~ "mapPath";            ///< http://lv2plug.in/ns/ext/state#mapPath
44 enum LV2_STATE__state             = LV2_STATE_PREFIX ~ "state";              ///< http://lv2plug.in/ns/ext/state#state
45 enum LV2_STATE__threadSafeRestore = LV2_STATE_PREFIX ~ "threadSafeRestore";  ///< http://lv2plug.in/ns/ext/state#threadSafeRestore
46 
47 extern(C) {
48 
49 alias LV2_State_Handle           = void*; ///< 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 : int {
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 trasmit 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 alias LV2_State_Store_Function = LV2_State_Status function(
144 	LV2_State_Handle handle,
145 	uint32_t         key,
146 	const void*      value,
147 	size_t           size,
148 	uint32_t         type,
149 	uint32_t         flags);
150 
151 /**
152    A host-provided function to retrieve a property.
153    @param handle Must be the handle passed to LV2_State_Interface.restore().
154    @param key The key of the property to retrieve (URID).
155    @param size (Output) If non-NULL, set to the size of the restored value.
156    @param type (Output) If non-NULL, set to the type of the restored value.
157    @param flags (Output) If non-NULL, set to the flags for the restored value.
158    @return A pointer to the restored value (object), or NULL if no value
159    has been stored under `key`.
160 
161    A callback of this type is passed by the host to
162    LV2_State_Interface.restore().  This callback is called repeatedly by the
163    plugin to retrieve any properties it requires to restore its state.
164 
165    The returned value MUST remain valid until LV2_State_Interface.restore()
166    returns.  The plugin MUST NOT attempt to use this function, or any value
167    returned from it, outside of the LV2_State_Interface.restore() context.
168 */
169 alias LV2_State_Retrieve_Function = const void* function(
170 	LV2_State_Handle handle,
171 	uint32_t         key,
172 	size_t*          size,
173 	uint32_t*        type,
174 	uint32_t*        flags);
175 
176 /**
177    LV2 Plugin State Interface.
178 
179    When the plugin's extension_data is called with argument
180    LV2_STATE__interface, the plugin MUST return an LV2_State_Interface
181    structure, which remains valid for the lifetime of the plugin.
182 
183    The host can use the contained function pointers to save and restore the
184    state of a plugin instance at any time, provided the threading restrictions
185    of the functions are met.
186 
187    Stored data is only guaranteed to be compatible between instances of plugins
188    with the same URI (i.e. if a change to a plugin would cause a fatal error
189    when restoring state saved by a previous version of that plugin, the plugin
190    URI MUST change just as it must when ports change incompatibly).  Plugin
191    authors should consider this possibility, and always store sensible data
192    with meaningful types to avoid such problems in the future.
193 */
194 struct _LV2_State_Interface {
195 	/**
196 	   Save plugin state using a host-provided `store` callback.
197 
198 	   @param instance The instance handle of the plugin.
199 	   @param store The host-provided store callback.
200 	   @param handle An opaque pointer to host data which MUST be passed as the
201 	   handle parameter to `store` if it is called.
202 	   @param flags Flags describing desired properties of this save.  These
203 	   flags may be used to determine the most appropriate values to store.
204 	   @param features Extensible parameter for passing any additional
205 	   features to be used for this save.
206 
207 	   The plugin is expected to store everything necessary to completely
208 	   restore its state later.  Plugins SHOULD store simple POD data whenever
209 	   possible, and consider the possibility of state being restored much
210 	   later on a different machine.
211 
212 	   The `handle` pointer and `store` function MUST NOT be used
213 	   beyond the scope of save().
214 
215 	   This function has its own special threading class: it may not be called
216 	   concurrently with any "Instantiation" function, but it may be called
217 	   concurrently with functions in any other class, unless the definition of
218 	   that class prohibits it (e.g. it may not be called concurrently with a
219 	   "Discovery" function, but it may be called concurrently with an "Audio"
220 	   function.  The plugin is responsible for any locking or lock-free
221 	   techniques necessary to make this possible.
222 
223 	   Note that in the simple case where state is only modified by restore(),
224 	   there are no synchronization issues since save() is never called
225 	   concurrently with restore() (though run() may read it during a save).
226 
227 	   Plugins that dynamically modify state while running, however, must take
228 	   care to do so in such a way that a concurrent call to save() will save a
229 	   consistent representation of plugin state for a single instant in time.
230 	*/
231 	LV2_State_Status function(LV2_Handle                 instance,
232 	                         LV2_State_Store_Function   store,
233 	                         LV2_State_Handle           handle,
234 	                         uint32_t                   flags,
235 	                         const(LV2_Feature*)*       features) save;
236 
237 	/**
238 	   Restore plugin state using a host-provided `retrieve` callback.
239 
240 	   @param instance The instance handle of the plugin.
241 	   @param retrieve The host-provided retrieve callback.
242 	   @param handle An opaque pointer to host data which MUST be passed as the
243 	   handle parameter to `retrieve` if it is called.
244 	   @param flags Currently unused.
245 	   @param features Extensible parameter for passing any additional
246 	   features to be used for this restore.
247 
248 	   The plugin MAY assume a restored value was set by a previous call to
249 	   LV2_State_Interface.save() by a plugin with the same URI.
250 
251 	   The plugin MUST gracefully fall back to a default value when a value can
252 	   not be retrieved.  This allows the host to reset the plugin state with
253 	   an empty map.
254 
255 	   The `handle` pointer and `store` function MUST NOT be used
256 	   beyond the scope of restore().
257 
258 	   This function is in the "Instantiation" threading class as defined by
259 	   LV2. This means it MUST NOT be called concurrently with any other
260 	   function on the same plugin instance.
261 	*/
262 	LV2_State_Status function(LV2_Handle                  instance,
263 	                            LV2_State_Retrieve_Function retrieve,
264 	                            LV2_State_Handle            handle,
265 	                            uint32_t                    flags,
266 	                            const(LV2_Feature*)*       features) restore;
267 };
268 alias LV2_State_Interface = _LV2_State_Interface;
269 
270 /**
271    Feature data for state:mapPath (@ref LV2_STATE__mapPath).
272 */
273 struct LV2_State_Map_Path {
274 	/**
275 	   Opaque host data.
276 	*/
277 	LV2_State_Map_Path_Handle handle;
278 
279 	/**
280 	   Map an absolute path to an abstract path for use in plugin state.
281 	   @param handle MUST be the `handle` member of this struct.
282 	   @param absolute_path The absolute path of a file.
283 	   @return An abstract path suitable for use in plugin state.
284 
285 	   The plugin MUST use this function to map any paths that will be stored
286 	   in plugin state.  The returned value is an abstract path which MAY not
287 	   be an actual file system path; absolute_path() MUST be used to map
288 	   it to an actual path in order to use the file.
289 
290 	   Plugins MUST NOT make any assumptions about abstract paths except that
291 	   they can be mapped back to the absolute path of the "same" file (though
292 	   not necessarily the same original path) using absolute_path().
293 
294 	   This function may only be called within the context of
295 	   LV2_State_Interface methods.  The caller is responsible for freeing the
296 	   returned value with free().
297 	*/
298 	char* function(LV2_State_Map_Path_Handle handle,
299 	                       const char*               absolute_path) abstract_path;
300 
301 	/**
302 	   Map an abstract path from plugin state to an absolute path.
303 	   @param handle MUST be the `handle` member of this struct.
304 	   @param abstract_path An abstract path (e.g. a path from plugin state).
305 	   @return An absolute file system path.
306 
307 	   The plugin MUST use this function in order to actually open or otherwise
308 	   use any paths loaded from plugin state.
309 
310 	   This function may only be called within the context of
311 	   LV2_State_Interface methods.  The caller is responsible for freeing the
312 	   returned value with free().
313 	*/
314 	char* function(LV2_State_Map_Path_Handle handle,
315 	                       const char*               abstract_path) absolute_path;
316 };
317 
318 /**
319    Feature data for state:makePath (@ref LV2_STATE__makePath).
320 */
321 struct LV2_State_Make_Path {
322 	/**
323 	   Opaque host data.
324 	*/
325 	LV2_State_Make_Path_Handle handle;
326 
327 	/**
328 	   Return a path the plugin may use to create a new file.
329 	   @param handle MUST be the `handle` member of this struct.
330 	   @param path The path of the new file within a namespace unique to this
331 	   plugin instance.
332 	   @return The absolute path to use for the new file.
333 
334 	   This function can be used by plugins to create files and directories,
335 	   either at state saving time (if this feature is passed to
336 	   LV2_State_Interface.save()) or any time (if this feature is passed to
337 	   LV2_Descriptor.instantiate()).
338 
339 	   The host MUST do whatever is necessary for the plugin to be able to
340 	   create a file at the returned path (e.g. using fopen), including
341 	   creating any leading directories.
342 
343 	   If this function is passed to LV2_Descriptor.instantiate(), it may be
344 	   called from any non-realtime context.  If it is passed to
345 	   LV2_State_Interface.save(), it may only be called within the dynamic
346 	   scope of that function call.
347 
348 	   The caller is responsible for freeing the returned value with free().
349 	*/
350 	char* function(LV2_State_Make_Path_Handle handle,
351 	              const char*                path) path;
352 };
353 
354 } /* extern "C" */