Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

361 lines
14KB

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