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.

state.h 16KB

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