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