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.

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