DPF

DISTRHO Plugin Framework
Log | Files | Refs | Submodules | README | LICENSE

state.h (16452B)


      1 /*
      2   Copyright 2010-2016 David Robillard <http://drobilla.net>
      3   Copyright 2010 Leonard Ritter <paniq@paniq.org>
      4 
      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.
      8 
      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.
     16 */
     17 
     18 /**
     19    @defgroup state State
     20    @ingroup lv2
     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 
     28 #ifndef LV2_STATE_H
     29 #define LV2_STATE_H
     30 
     31 #include "lv2.h"
     32 
     33 #include <stdbool.h>
     34 #include <stddef.h>
     35 #include <stdint.h>
     36 
     37 #define LV2_STATE_URI    "http://lv2plug.in/ns/ext/state"  ///< http://lv2plug.in/ns/ext/state
     38 #define LV2_STATE_PREFIX LV2_STATE_URI "#"                 ///< http://lv2plug.in/ns/ext/state#
     39 
     40 #define LV2_STATE__State             LV2_STATE_PREFIX "State"              ///< http://lv2plug.in/ns/ext/state#State
     41 #define LV2_STATE__interface         LV2_STATE_PREFIX "interface"          ///< http://lv2plug.in/ns/ext/state#interface
     42 #define LV2_STATE__loadDefaultState  LV2_STATE_PREFIX "loadDefaultState"   ///< http://lv2plug.in/ns/ext/state#loadDefaultState
     43 #define LV2_STATE__freePath          LV2_STATE_PREFIX "freePath"           ///< http://lv2plug.in/ns/ext/state#freePath
     44 #define LV2_STATE__makePath          LV2_STATE_PREFIX "makePath"           ///< http://lv2plug.in/ns/ext/state#makePath
     45 #define LV2_STATE__mapPath           LV2_STATE_PREFIX "mapPath"            ///< http://lv2plug.in/ns/ext/state#mapPath
     46 #define LV2_STATE__state             LV2_STATE_PREFIX "state"              ///< http://lv2plug.in/ns/ext/state#state
     47 #define LV2_STATE__threadSafeRestore LV2_STATE_PREFIX "threadSafeRestore"  ///< http://lv2plug.in/ns/ext/state#threadSafeRestore
     48 #define LV2_STATE__StateChanged      LV2_STATE_PREFIX "StateChanged"       ///< http://lv2plug.in/ns/ext/state#StateChanged
     49 
     50 #ifdef __cplusplus
     51 extern "C" {
     52 #endif
     53 
     54 typedef void* LV2_State_Handle;            ///< Opaque handle for state save/restore
     55 typedef void* LV2_State_Free_Path_Handle;  ///< Opaque handle for state:freePath feature
     56 typedef void* LV2_State_Map_Path_Handle;   ///< Opaque handle for state:mapPath feature
     57 typedef void* LV2_State_Make_Path_Handle;  ///< Opaque handle for state:makePath feature
     58 
     59 /**
     60    Flags describing value characteristics.
     61 
     62    These flags are used along with the value's type URI to determine how to
     63    (de-)serialise the value data, or whether it is even possible to do so.
     64 */
     65 typedef enum {
     66 	/**
     67 	   Plain Old Data.
     68 
     69 	   Values with this flag contain no pointers or references to other areas
     70 	   of memory.  It is safe to copy POD values with a simple memcpy and store
     71 	   them for the duration of the process.  A POD value is not necessarily
     72 	   safe to trasmit between processes or machines (for example, filenames
     73 	   are POD), see LV2_STATE_IS_PORTABLE for details.
     74 
     75 	   Implementations MUST NOT attempt to copy or serialise a non-POD value if
     76 	   they do not understand its type (and thus know how to correctly do so).
     77 	*/
     78 	LV2_STATE_IS_POD = 1,
     79 
     80 	/**
     81 	   Portable (architecture independent) data.
     82 
     83 	   Values with this flag are in a format that is usable on any
     84 	   architecture.  A portable value saved on one machine can be restored on
     85 	   another machine regardless of architecture.  The format of portable
     86 	   values MUST NOT depend on architecture-specific properties like
     87 	   endianness or alignment.  Portable values MUST NOT contain filenames.
     88 	*/
     89 	LV2_STATE_IS_PORTABLE = 1 << 1,
     90 
     91 	/**
     92 	   Native data.
     93 
     94 	   This flag is used by the host to indicate that the saved data is only
     95 	   going to be used locally in the currently running process (for things
     96 	   like instance duplication or snapshots), so the plugin should use the
     97 	   most efficient representation possible and not worry about serialisation
     98 	   and portability.
     99 	*/
    100 	LV2_STATE_IS_NATIVE = 1 << 2
    101 } LV2_State_Flags;
    102 
    103 /** A status code for state functions. */
    104 typedef enum {
    105 	LV2_STATE_SUCCESS         = 0,  /**< Completed successfully. */
    106 	LV2_STATE_ERR_UNKNOWN     = 1,  /**< Unknown error. */
    107 	LV2_STATE_ERR_BAD_TYPE    = 2,  /**< Failed due to unsupported type. */
    108 	LV2_STATE_ERR_BAD_FLAGS   = 3,  /**< Failed due to unsupported flags. */
    109 	LV2_STATE_ERR_NO_FEATURE  = 4,  /**< Failed due to missing features. */
    110 	LV2_STATE_ERR_NO_PROPERTY = 5,  /**< Failed due to missing property. */
    111 	LV2_STATE_ERR_NO_SPACE    = 6   /**< Failed due to insufficient space. */
    112 } LV2_State_Status;
    113 
    114 /**
    115    A host-provided function to store a property.
    116    @param handle Must be the handle passed to LV2_State_Interface.save().
    117    @param key The key to store `value` under (URID).
    118    @param value Pointer to the value to be stored.
    119    @param size The size of `value` in bytes.
    120    @param type The type of `value` (URID).
    121    @param flags LV2_State_Flags for `value`.
    122    @return 0 on success, otherwise a non-zero error code.
    123 
    124    The host passes a callback of this type to LV2_State_Interface.save(). This
    125    callback is called repeatedly by the plugin to store all the properties that
    126    describe its current state.
    127 
    128    DO NOT INVENT NONSENSE URI SCHEMES FOR THE KEY.  Best is to use keys from
    129    existing vocabularies.  If nothing appropriate is available, use http URIs
    130    that point to somewhere you can host documents so documentation can be made
    131    resolvable (typically a child of the plugin or project URI).  If this is not
    132    possible, invent a URN scheme, e.g. urn:myproj:whatever.  The plugin MUST
    133    NOT pass an invalid URI key.
    134 
    135    The host MAY fail to store a property for whatever reason, but SHOULD
    136    store any property that is LV2_STATE_IS_POD and LV2_STATE_IS_PORTABLE.
    137    Implementations SHOULD use the types from the LV2 Atom extension
    138    (http://lv2plug.in/ns/ext/atom) wherever possible.  The plugin SHOULD
    139    attempt to fall-back and avoid the error if possible.
    140 
    141    Note that `size` MUST be > 0, and `value` MUST point to a valid region of
    142    memory `size` bytes long (this is required to make restore unambiguous).
    143 
    144    The plugin MUST NOT attempt to use this function outside of the
    145    LV2_State_Interface.restore() context.
    146 */
    147 typedef LV2_State_Status (*LV2_State_Store_Function)(
    148 	LV2_State_Handle handle,
    149 	uint32_t         key,
    150 	const void*      value,
    151 	size_t           size,
    152 	uint32_t         type,
    153 	uint32_t         flags);
    154 
    155 /**
    156    A host-provided function to retrieve a property.
    157    @param handle Must be the handle passed to LV2_State_Interface.restore().
    158    @param key The key of the property to retrieve (URID).
    159    @param size (Output) If non-NULL, set to the size of the restored value.
    160    @param type (Output) If non-NULL, set to the type of the restored value.
    161    @param flags (Output) If non-NULL, set to the flags for the restored value.
    162    @return A pointer to the restored value (object), or NULL if no value
    163    has been stored under `key`.
    164 
    165    A callback of this type is passed by the host to
    166    LV2_State_Interface.restore().  This callback is called repeatedly by the
    167    plugin to retrieve any properties it requires to restore its state.
    168 
    169    The returned value MUST remain valid until LV2_State_Interface.restore()
    170    returns.  The plugin MUST NOT attempt to use this function, or any value
    171    returned from it, outside of the LV2_State_Interface.restore() context.
    172 */
    173 typedef const void* (*LV2_State_Retrieve_Function)(
    174 	LV2_State_Handle handle,
    175 	uint32_t         key,
    176 	size_t*          size,
    177 	uint32_t*        type,
    178 	uint32_t*        flags);
    179 
    180 /**
    181    LV2 Plugin State Interface.
    182 
    183    When the plugin's extension_data is called with argument
    184    LV2_STATE__interface, the plugin MUST return an LV2_State_Interface
    185    structure, which remains valid for the lifetime of the plugin.
    186 
    187    The host can use the contained function pointers to save and restore the
    188    state of a plugin instance at any time, provided the threading restrictions
    189    of the functions are met.
    190 
    191    Stored data is only guaranteed to be compatible between instances of plugins
    192    with the same URI (i.e. if a change to a plugin would cause a fatal error
    193    when restoring state saved by a previous version of that plugin, the plugin
    194    URI MUST change just as it must when ports change incompatibly).  Plugin
    195    authors should consider this possibility, and always store sensible data
    196    with meaningful types to avoid such problems in the future.
    197 */
    198 typedef struct {
    199 	/**
    200 	   Save plugin state using a host-provided `store` callback.
    201 
    202 	   @param instance The instance handle of the plugin.
    203 	   @param store The host-provided store callback.
    204 	   @param handle An opaque pointer to host data which MUST be passed as the
    205 	   handle parameter to `store` if it is called.
    206 	   @param flags Flags describing desired properties of this save.  These
    207 	   flags may be used to determine the most appropriate values to store.
    208 	   @param features Extensible parameter for passing any additional
    209 	   features to be used for this save.
    210 
    211 	   The plugin is expected to store everything necessary to completely
    212 	   restore its state later.  Plugins SHOULD store simple POD data whenever
    213 	   possible, and consider the possibility of state being restored much
    214 	   later on a different machine.
    215 
    216 	   The `handle` pointer and `store` function MUST NOT be used
    217 	   beyond the scope of save().
    218 
    219 	   This function has its own special threading class: it may not be called
    220 	   concurrently with any "Instantiation" function, but it may be called
    221 	   concurrently with functions in any other class, unless the definition of
    222 	   that class prohibits it (for example, it may not be called concurrently
    223 	   with a "Discovery" function, but it may be called concurrently with an
    224 	   "Audio" function.  The plugin is responsible for any locking or
    225 	   lock-free techniques necessary to make this possible.
    226 
    227 	   Note that in the simple case where state is only modified by restore(),
    228 	   there are no synchronization issues since save() is never called
    229 	   concurrently with restore() (though run() may read it during a save).
    230 
    231 	   Plugins that dynamically modify state while running, however, must take
    232 	   care to do so in such a way that a concurrent call to save() will save a
    233 	   consistent representation of plugin state for a single instant in time.
    234 	*/
    235 	LV2_State_Status (*save)(LV2_Handle                 instance,
    236 	                         LV2_State_Store_Function   store,
    237 	                         LV2_State_Handle           handle,
    238 	                         uint32_t                   flags,
    239 	                         const LV2_Feature *const * features);
    240 
    241 	/**
    242 	   Restore plugin state using a host-provided `retrieve` callback.
    243 
    244 	   @param instance The instance handle of the plugin.
    245 	   @param retrieve The host-provided retrieve callback.
    246 	   @param handle An opaque pointer to host data which MUST be passed as the
    247 	   handle parameter to `retrieve` if it is called.
    248 	   @param flags Currently unused.
    249 	   @param features Extensible parameter for passing any additional
    250 	   features to be used for this restore.
    251 
    252 	   The plugin MAY assume a restored value was set by a previous call to
    253 	   LV2_State_Interface.save() by a plugin with the same URI.
    254 
    255 	   The plugin MUST gracefully fall back to a default value when a value can
    256 	   not be retrieved.  This allows the host to reset the plugin state with
    257 	   an empty map.
    258 
    259 	   The `handle` pointer and `store` function MUST NOT be used
    260 	   beyond the scope of restore().
    261 
    262 	   This function is in the "Instantiation" threading class as defined by
    263 	   LV2. This means it MUST NOT be called concurrently with any other
    264 	   function on the same plugin instance.
    265 	*/
    266 	LV2_State_Status (*restore)(LV2_Handle                  instance,
    267 	                            LV2_State_Retrieve_Function retrieve,
    268 	                            LV2_State_Handle            handle,
    269 	                            uint32_t                    flags,
    270 	                            const LV2_Feature *const *  features);
    271 } LV2_State_Interface;
    272 
    273 /**
    274    Feature data for state:mapPath (@ref LV2_STATE__mapPath).
    275 */
    276 typedef struct {
    277 	/**
    278 	   Opaque host data.
    279 	*/
    280 	LV2_State_Map_Path_Handle handle;
    281 
    282 	/**
    283 	   Map an absolute path to an abstract path for use in plugin state.
    284 	   @param handle MUST be the `handle` member of this struct.
    285 	   @param absolute_path The absolute path of a file.
    286 	   @return An abstract path suitable for use in plugin state.
    287 
    288 	   The plugin MUST use this function to map any paths that will be stored
    289 	   in plugin state.  The returned value is an abstract path which MAY not
    290 	   be an actual file system path; absolute_path() MUST be used to map
    291 	   it to an actual path in order to use the file.
    292 
    293 	   Plugins MUST NOT make any assumptions about abstract paths except that
    294 	   they can be mapped back to the absolute path of the "same" file (though
    295 	   not necessarily the same original path) using absolute_path().
    296 
    297 	   This function may only be called within the context of
    298 	   LV2_State_Interface methods.  The caller must free the returned value
    299 	   with LV2_State_Free_Path.free_path().
    300 	*/
    301 	char* (*abstract_path)(LV2_State_Map_Path_Handle handle,
    302 	                       const char*               absolute_path);
    303 
    304 	/**
    305 	   Map an abstract path from plugin state to an absolute path.
    306 	   @param handle MUST be the `handle` member of this struct.
    307 	   @param abstract_path An abstract path (typically from plugin state).
    308 	   @return An absolute file system path.
    309 
    310 	   The plugin MUST use this function in order to actually open or otherwise
    311 	   use any paths loaded from plugin state.
    312 
    313 	   This function may only be called within the context of
    314 	   LV2_State_Interface methods.  The caller must free the returned value
    315 	   with LV2_State_Free_Path.free_path().
    316 	*/
    317 	char* (*absolute_path)(LV2_State_Map_Path_Handle handle,
    318 	                       const char*               abstract_path);
    319 } LV2_State_Map_Path;
    320 
    321 /**
    322    Feature data for state:makePath (@ref LV2_STATE__makePath).
    323 */
    324 typedef struct {
    325 	/**
    326 	   Opaque host data.
    327 	*/
    328 	LV2_State_Make_Path_Handle handle;
    329 
    330 	/**
    331 	   Return a path the plugin may use to create a new file.
    332 	   @param handle MUST be the `handle` member of this struct.
    333 	   @param path The path of the new file within a namespace unique to this
    334 	   plugin instance.
    335 	   @return The absolute path to use for the new file.
    336 
    337 	   This function can be used by plugins to create files and directories,
    338 	   either at state saving time (if this feature is passed to
    339 	   LV2_State_Interface.save()) or any time (if this feature is passed to
    340 	   LV2_Descriptor.instantiate()).
    341 
    342 	   The host MUST do whatever is necessary for the plugin to be able to
    343 	   create a file at the returned path (for example, using fopen()),
    344 	   including creating any leading directories.
    345 
    346 	   If this function is passed to LV2_Descriptor.instantiate(), it may be
    347 	   called from any non-realtime context.  If it is passed to
    348 	   LV2_State_Interface.save(), it may only be called within the dynamic
    349 	   scope of that function call.
    350 
    351 	   The caller must free the returned value with
    352 	   LV2_State_Free_Path.free_path().
    353 	*/
    354 	char* (*path)(LV2_State_Make_Path_Handle handle,
    355 	              const char*                path);
    356 } LV2_State_Make_Path;
    357 
    358 /**
    359    Feature data for state:freePath (@ref LV2_STATE__freePath).
    360 */
    361 typedef struct {
    362 	/**
    363 	   Opaque host data.
    364 	*/
    365 	LV2_State_Free_Path_Handle handle;
    366 
    367 	/**
    368 	   Free a path returned by a state feature.
    369 
    370 	   @param handle MUST be the `handle` member of this struct.
    371 	   @param path The path previously returned by a state feature.
    372 
    373 	   This function can be used by plugins to free paths allocated by the host
    374 	   and returned by state features (LV2_State_Map_Path.abstract_path(),
    375 	   LV2_State_Map_Path.absolute_path(), and LV2_State_Make_Path.path()).
    376 	*/
    377 	void (*free_path)(LV2_State_Free_Path_Handle handle,
    378 	                  char*                      path);
    379 } LV2_State_Free_Path;
    380 
    381 #ifdef __cplusplus
    382 } /* extern "C" */
    383 #endif
    384 
    385 #endif /* LV2_STATE_H */
    386 
    387 /**
    388    @}
    389 */