2 Copyright 2010-2011 David Robillard <http://drobilla.net>
3 Copyright 2010 Leonard Ritter <paniq@paniq.org>
5 Permission to use, copy, modify, and/or distribute this software for any
6 purpose with or without fee is hereby granted, provided that the above
7 copyright notice and this permission notice appear in all copies.
9 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 C API for the LV2 State extension <http://lv2plug.in/ns/ext/state>.
30 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
36 #define LV2_STATE_URI "http://lv2plug.in/ns/ext/state"
38 #define LV2_STATE_INTERFACE_URI LV2_STATE_URI "#Interface"
39 #define LV2_STATE_PATH_URI LV2_STATE_URI "#Path"
40 #define LV2_STATE_MAP_PATH_URI LV2_STATE_URI "#mapPath"
41 #define LV2_STATE_MAKE_PATH_URI LV2_STATE_URI "#makePath"
43 typedef void* LV2_State_Handle;
44 typedef void* LV2_State_Map_Path_Handle;
45 typedef void* LV2_State_Make_Path_Handle;
48 Flags describing value characteristics.
50 These flags are used along with the value's type URI to determine how to
51 (de-)serialise the value data, or whether it is even possible to do so.
58 Values with this flag contain no references to non-persistent or
59 non-global resources (e.g. pointers, handles, local paths, etc.). It is
60 safe to copy POD values with a simple memcpy and store them for use at
61 any time in the future on a machine with a compatible architecture
62 (e.g. the same endianness and alignment).
64 Implementations MUST NOT attempt to copy or serialise a non-POD value if
65 they do not understand its type (and thus know how to correctly do so).
70 Portable (architecture independent) data.
72 Values with this flag are in a format that is usable on any
73 architecture, i.e. if the value is saved on one machine it can safely be
74 restored on another machine regardless of endianness, alignment, etc.
76 LV2_STATE_IS_PORTABLE = 1 << 1,
81 This flag is used by the host to indicate that the saved data is only
82 going to be used locally in the currently running process (e.g. for
83 instance duplication or snapshots), so the plugin should use the most
84 efficient representation possible and not worry about serialisation
87 LV2_STATE_IS_NATIVE = 1 << 2
92 A host-provided function to store a property.
93 @param handle Must be the handle passed to LV2_State_Interface.save().
94 @param key The key (predicate) to store @c value under (URI mapped integer).
95 @param value Pointer to the value (object) to be stored.
96 @param size The size of the data at @c value in bytes.
97 @param type The type of @c value (URI).
98 @param flags LV2_State_Flags for @c value.
99 @return 0 on success, otherwise a non-zero error code.
101 The host passes a callback of this type to LV2_State_Interface.save(). This callback
102 is called repeatedly by the plugin within LV2_State_Interface.save() to store all
103 the statements that describe its current state.
105 The host MAY fail to store a property if the type is not understood and is
106 not LV2_STATE_IS_POD and/or LV2_STATE_IS_PORTABLE. Implementations are
107 encouraged to use POD and portable values (e.g. string literals) wherever
108 possible, and use common types (e.g. types from
109 http://lv2plug.in/ns/ext/atom) regardless, since hosts are likely to already
110 contain the necessary implementation.
112 Note that @c size MUST be > 0, and @c value MUST point to a valid region of
113 memory @c size bytes long (this is required to make restore unambiguous).
115 The plugin MUST NOT attempt to use this function outside of the
116 LV2_State_Interface.restore() context.
118 typedef int (*LV2_State_Store_Function)(LV2_State_Handle handle,
126 A host-provided function to retrieve a property.
127 @param handle Must be the handle passed to
128 LV2_State_Interface.restore().
129 @param key The key (predicate) of the property to retrieve (URI).
130 @param size (Output) If non-NULL, set to the size of the restored value.
131 @param type (Output) If non-NULL, set to the type of the restored value.
132 @param flags (Output) If non-NULL, set to the LV2_State_Flags for
134 @return A pointer to the restored value (object), or NULL if no value
135 has been stored under @c key.
137 A callback of this type is passed by the host to
138 LV2_State_Interface.restore(). This callback is called repeatedly by the
139 plugin within LV2_State_Interface.restore() to retrieve any properties it
140 requires to restore its state.
142 The returned value MUST remain valid until LV2_State_Interface.restore()
145 The plugin MUST NOT attempt to use this function, or any value returned from
146 it, outside of the LV2_State_Interface.restore() context. Returned values
147 MAY be copied for later use if necessary, assuming the plugin knows how to
148 do so correctly (e.g. the value is POD, or the plugin understands the type).
150 typedef const void* (*LV2_State_Retrieve_Function)(LV2_State_Handle handle,
157 State Extension Data.
159 When the plugin's extension_data is called with argument LV2_STATE_URI,
160 the plugin MUST return an LV2_State structure, which remains valid for the
161 lifetime of the plugin.
163 The host can use the contained function pointers to save and restore the
164 state of a plugin instance at any time (provided the threading restrictions
165 for the given function are met).
167 The typical use case is to save the plugin's state when a project is saved,
168 and to restore the state when a project has been loaded. Other uses are
169 possible (e.g. cloning plugin instances or taking a snapshot of plugin
172 Stored data is only guaranteed to be compatible between instances of plugins
173 with the same URI (i.e. if a change to a plugin would cause a fatal error
174 when restoring state saved by a previous version of that plugin, the plugin
175 URI MUST change just as it must when ports change incompatibly). Plugin
176 authors should consider this possibility, and always store sensible data
177 with meaningful types to avoid such compatibility issues in the future.
179 typedef struct _LV2_State_Interface {
182 Save plugin state using a host-provided @c store callback.
184 @param instance The instance handle of the plugin.
185 @param store The host-provided store callback.
186 @param handle An opaque pointer to host data, e.g. the map or
187 file where the values are to be stored. If @c store is called, this MUST
188 be passed as its handle parameter.
189 @param flags Flags describing desires properties of this save. The
190 plugin SHOULD use these values to determine the most appropriate and/or
191 efficient serialisation, but is not required to do so.
192 @param features Extensible parameter for passing any additional
193 features to be used for this save.
195 The plugin is expected to store everything necessary to completely
196 restore its state later (possibly much later, in a different process, on
197 a completely different machine, etc.)
199 The @c handle pointer and @c store function MUST NOT be used
200 beyond the scope of save().
202 This function has its own special threading class: it may not be called
203 concurrently with any "Instantiation" function, but it may be called
204 concurrently with functions in any other class, unless the definition of
205 that class prohibits it (e.g. it may not be called concurrently with a
206 "Discovery" function, but it may be called concurrently with an "Audio"
207 function. The plugin is responsible for any locking or lock-free
208 techniques necessary to make this possible.
210 Note that in the simple case where state is only modified by restore(),
211 there are no synchronization issues since save() is never called
212 concurrently with restore() (though run() may read it during a save).
214 Plugins that dynamically modify state while running, however, must take
215 care to do so in such a way that a concurrent call to save() will save a
216 consistent representation of plugin state for a single instant in time.
218 void (*save)(LV2_Handle instance,
219 LV2_State_Store_Function store,
220 LV2_State_Handle handle,
222 const LV2_Feature *const * features);
225 Restore plugin state using a host-provided @c retrieve callback.
227 @param instance The instance handle of the plugin.
228 @param retrieve The host-provided retrieve callback.
229 @param handle An opaque pointer to host data, e.g. the map or
230 file from which the values are to be restored. If @c retrieve is
231 called, this MUST be passed as its handle parameter.
232 @param flags Currently unused.
233 @param features Extensible parameter for passing any additional
234 features to be used for this restore.
236 The plugin MAY assume a restored value was set by a previous call to
237 LV2_State_Interface.save() by a plugin with the same URI.
239 The plugin MUST gracefully fall back to a default value when a value can
240 not be retrieved. This allows the host to reset the plugin state with an
243 The @c handle pointer and @c store function MUST NOT be used
244 beyond the scope of restore().
246 This function is in the "Instantiation" threading class as defined by
247 LV2. This means it MUST NOT be called concurrently with any other
248 function on the same plugin instance.
250 void (*restore)(LV2_Handle instance,
251 LV2_State_Retrieve_Function retrieve,
252 LV2_State_Handle handle,
254 const LV2_Feature *const * features);
256 } LV2_State_Interface;
259 Feature data for state:mapPath (LV2_STATE_MAP_PATH_URI).
266 LV2_State_Map_Path_Handle handle;
269 Map an absolute path to an abstract path for use in plugin state.
270 @param handle MUST be the @a handle member of this struct.
271 @param absolute_path The absolute path of a file.
272 @return An abstract path suitable for use in plugin state.
274 The plugin MUST use this function to map any paths that will be stored
275 in files in plugin state. The returned value is an abstract path which
276 MAY not be an actual file system path; @ref absolute_path MUST be used
277 to map it to an actual path in order to use the file.
279 Hosts MAY map paths in any way (e.g. by creating symbolic links within
280 the plugin's state directory or storing a list of referenced files
281 elsewhere). Plugins MUST NOT make any assumptions about abstract paths
282 except that they can be mapped back to an absolute path using @ref
285 This function may only be called within the context of
286 LV2_State_Interface.save() or LV2_State_Interface.restore(). The caller
287 is responsible for freeing the returned value.
289 char* (*abstract_path)(LV2_State_Map_Path_Handle handle,
290 const char* absolute_path);
293 Map an abstract path from plugin state to an absolute path.
294 @param handle MUST be the @a handle member of this struct.
295 @param abstract_path An abstract path (e.g. a path from plugin state).
296 @return An absolute file system path.
298 Since abstract paths are not necessarily actual file paths (or at least
299 not necessarily absolute paths), this function MUST be used in order to
300 actually open or otherwise use the file referred to by an abstract path.
302 This function may only be called within the context of
303 LV2_State_Interface.save() or LV2_State_Interface.restore(). The caller
304 is responsible for freeing the returned value.
306 char* (*absolute_path)(LV2_State_Map_Path_Handle handle,
307 const char* abstract_path);
309 } LV2_State_Map_Path;
312 Feature data for state:makePath (@ref LV2_STATE_MAKE_PATH_URI).
319 LV2_State_Make_Path_Handle handle;
322 Return a path the plugin may use to create a new file.
323 @param handle MUST be the @a handle member of this struct.
324 @param path The path of the new file relative to a namespace unique
325 to this plugin instance.
326 @return The absolute path to use for the new file.
328 This function can be used by plugins to create files and directories,
329 either at state saving time (if this feature is passed to
330 LV2_State_Interface.save()) or any time (if this feature is passed to
331 LV2_Descriptor.instantiate()).
333 The host must do whatever is necessary for the plugin to be able to
334 create a file at the returned path (e.g. using fopen), including
335 creating any leading directories.
337 If this function is passed to LV2_Descriptor.instantiate(), it may be
338 called from any non-realtime context. If it is passed to
339 LV2_State_Interface.save(), it may only be called within the dynamic
340 scope of that function call.
342 The caller is responsible for freeing the returned value with free().
344 char* (*path)(LV2_State_Make_Path_Handle handle,
347 } LV2_State_Make_Path;
353 #endif /* LV2_STATE_H */