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.

base.h 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * travesty, pure C VST3-compatible interface
  3. * Copyright (C) 2021-2022 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #pragma once
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. #include <string.h>
  20. /**
  21. * deal with C vs C++ differences
  22. */
  23. #if !defined(__cplusplus) && !defined(constexpr)
  24. # define constexpr
  25. #endif
  26. /**
  27. * various types
  28. */
  29. typedef int32_t v3_result;
  30. typedef int16_t v3_str_128[128];
  31. typedef uint8_t v3_bool;
  32. typedef uint32_t v3_param_id;
  33. /**
  34. * low-level ABI nonsense
  35. */
  36. typedef uint8_t v3_tuid[16];
  37. static inline
  38. bool v3_tuid_match(const v3_tuid a, const v3_tuid b)
  39. {
  40. return memcmp(a, b, sizeof(v3_tuid)) == 0;
  41. }
  42. #if defined(_WIN32)
  43. # define V3_COM_COMPAT 1
  44. # define V3_API __stdcall
  45. #else
  46. # define V3_COM_COMPAT 0
  47. # define V3_API
  48. #endif
  49. #if V3_COM_COMPAT
  50. enum {
  51. V3_NO_INTERFACE = 0x80004002L,
  52. V3_OK = 0,
  53. V3_TRUE = 0,
  54. V3_FALSE = 1,
  55. V3_INVALID_ARG = 0x80070057L,
  56. V3_NOT_IMPLEMENTED = 0x80004001L,
  57. V3_INTERNAL_ERR = 0x80004005L,
  58. V3_NOT_INITIALIZED = 0x8000FFFFL,
  59. V3_NOMEM = 0x8007000EL
  60. };
  61. # define V3_ID(a, b, c, d) { \
  62. ((a) & 0x000000FF), \
  63. ((a) & 0x0000FF00) >> 8, \
  64. ((a) & 0x00FF0000) >> 16, \
  65. ((a) & 0xFF000000) >> 24, \
  66. \
  67. ((b) & 0x00FF0000) >> 16, \
  68. ((b) & 0xFF000000) >> 24, \
  69. ((b) & 0x000000FF), \
  70. ((b) & 0x0000FF00) >> 8, \
  71. \
  72. ((c) & 0xFF000000) >> 24, \
  73. ((c) & 0x00FF0000) >> 16, \
  74. ((c) & 0x0000FF00) >> 8, \
  75. ((c) & 0x000000FF), \
  76. \
  77. ((d) & 0xFF000000) >> 24, \
  78. ((d) & 0x00FF0000) >> 16, \
  79. ((d) & 0x0000FF00) >> 8, \
  80. ((d) & 0x000000FF), \
  81. }
  82. #else // V3_COM_COMPAT
  83. enum {
  84. V3_NO_INTERFACE = -1,
  85. V3_OK,
  86. V3_TRUE = V3_OK,
  87. V3_FALSE,
  88. V3_INVALID_ARG,
  89. V3_NOT_IMPLEMENTED,
  90. V3_INTERNAL_ERR,
  91. V3_NOT_INITIALIZED,
  92. V3_NOMEM
  93. };
  94. # define V3_ID(a, b, c, d) { \
  95. ((a) & 0xFF000000) >> 24, \
  96. ((a) & 0x00FF0000) >> 16, \
  97. ((a) & 0x0000FF00) >> 8, \
  98. ((a) & 0x000000FF), \
  99. \
  100. ((b) & 0xFF000000) >> 24, \
  101. ((b) & 0x00FF0000) >> 16, \
  102. ((b) & 0x0000FF00) >> 8, \
  103. ((b) & 0x000000FF), \
  104. \
  105. ((c) & 0xFF000000) >> 24, \
  106. ((c) & 0x00FF0000) >> 16, \
  107. ((c) & 0x0000FF00) >> 8, \
  108. ((c) & 0x000000FF), \
  109. \
  110. ((d) & 0xFF000000) >> 24, \
  111. ((d) & 0x00FF0000) >> 16, \
  112. ((d) & 0x0000FF00) >> 8, \
  113. ((d) & 0x000000FF), \
  114. }
  115. #endif // V3_COM_COMPAT
  116. #define V3_ID_COPY(iid) \
  117. { iid[0], iid[1], iid[ 2], iid[ 3], iid[ 4], iid[ 5], iid[ 6], iid[ 7], \
  118. iid[8], iid[9], iid[10], iid[11], iid[12], iid[13], iid[14], iid[15] }
  119. /**
  120. * funknown
  121. */
  122. struct v3_funknown {
  123. v3_result (V3_API* query_interface)(void* self, const v3_tuid iid, void** obj);
  124. uint32_t (V3_API* ref)(void* self);
  125. uint32_t (V3_API* unref)(void* self);
  126. };
  127. static constexpr const v3_tuid v3_funknown_iid =
  128. V3_ID(0x00000000, 0x00000000, 0xC0000000, 0x00000046);
  129. /**
  130. * plugin base
  131. */
  132. struct v3_plugin_base {
  133. #ifndef __cplusplus
  134. struct v3_funknown;
  135. #endif
  136. v3_result (V3_API* initialize)(void* self, struct v3_funknown** context);
  137. v3_result (V3_API* terminate)(void* self);
  138. };
  139. static constexpr const v3_tuid v3_plugin_base_iid =
  140. V3_ID(0x22888DDB, 0x156E45AE, 0x8358B348, 0x08190625);
  141. #ifdef __cplusplus
  142. /**
  143. * cast object into its proper C++ type.
  144. * this is needed because `struct v3_funknown;` on a C++ class does not inherit `v3_funknown`'s fields.
  145. *
  146. * we can use this as a little helper for keeping both C and C++ compatiblity.
  147. * specialized templated calls are defined where required
  148. * (that is, object inherits from something other than `v3_funknown`)
  149. *
  150. * example usage: `v3_cpp_obj(obj)->method(obj, args...);`
  151. */
  152. template<class T> static inline
  153. constexpr T* v3_cpp_obj(T** obj)
  154. {
  155. /**
  156. * this ugly piece of code is required due to C++ assuming `reinterpret_cast` by default,
  157. * but we need everything to be `static_cast` for it to be `constexpr` compatible.
  158. */
  159. return static_cast<T*>(static_cast<void*>(static_cast<uint8_t*>(static_cast<void*>(*obj)) + sizeof(void*)*3));
  160. }
  161. /**
  162. * helper C++ functions to manually call v3_funknown methods on an object.
  163. */
  164. template<class T, class M> static inline
  165. v3_result v3_cpp_obj_query_interface(T** obj, const v3_tuid iid, M*** obj2)
  166. {
  167. return static_cast<v3_funknown*>(static_cast<void*>(*obj))->query_interface(obj, iid, (void**)obj2);
  168. }
  169. template<class T> static inline
  170. uint32_t v3_cpp_obj_ref(T** obj)
  171. {
  172. return static_cast<v3_funknown*>(static_cast<void*>(*obj))->ref(obj);
  173. }
  174. template<class T> static inline
  175. uint32_t v3_cpp_obj_unref(T** obj)
  176. {
  177. return static_cast<v3_funknown*>(static_cast<void*>(*obj))->unref(obj);
  178. }
  179. template<class T> static inline
  180. v3_result v3_cpp_obj_initialize(T** obj, v3_funknown** context)
  181. {
  182. return static_cast<v3_plugin_base*>(
  183. static_cast<void*>(static_cast<uint8_t*>(static_cast<void*>(*obj)) + sizeof(void*)*3))->initialize(obj, context);
  184. }
  185. template<class T> static inline
  186. v3_result v3_cpp_obj_terminate(T** obj)
  187. {
  188. return static_cast<v3_plugin_base*>(
  189. static_cast<void*>(static_cast<uint8_t*>(static_cast<void*>(*obj)) + sizeof(void*)*3))->terminate(obj);
  190. }
  191. #endif