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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * travesty, pure C VST3-compatible interface
  3. * Copyright (C) 2021 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. #ifdef __cplusplus
  24. /**
  25. * cast object into its proper C++ type.
  26. * this is needed because `struct v3_funknown;` on a C++ class does not inherit `v3_funknown`'s fields.
  27. * we can use this as a little helper for keeping both C and C++ compatiblity.
  28. * specialized templated calls are defined where required
  29. * (that is, object inherits from something other than `v3_funknown`)
  30. *
  31. * example usage: `v3_cpp_obj(obj)->method(obj, args...);`
  32. */
  33. template<class T> static inline
  34. constexpr T* v3_cpp_obj(T** obj)
  35. {
  36. /**
  37. * this ugly piece of code is required due to C++ assuming `reinterpret_cast` by default,
  38. * but we need everything to be `static_cast` for it to be `constexpr` compatible.
  39. */
  40. return static_cast<T*>(static_cast<void*>(static_cast<uint8_t*>(static_cast<void*>(*obj)) + sizeof(void*)*3));
  41. }
  42. #else
  43. # ifndef constexpr
  44. # define constexpr
  45. # endif
  46. #endif
  47. /**
  48. * various types
  49. */
  50. typedef int32_t v3_result;
  51. typedef int16_t v3_str_128[128];
  52. typedef uint8_t v3_bool;
  53. typedef uint32_t v3_param_id;
  54. /**
  55. * low-level ABI nonsense
  56. */
  57. typedef uint8_t v3_tuid[16];
  58. static inline
  59. bool v3_tuid_match(const v3_tuid a, const v3_tuid b)
  60. {
  61. return memcmp(a, b, sizeof(v3_tuid)) == 0;
  62. }
  63. #if defined(_WIN32)
  64. # define V3_COM_COMPAT 1
  65. # define V3_API __stdcall
  66. #else
  67. # define V3_COM_COMPAT 0
  68. # define V3_API
  69. #endif
  70. #if V3_COM_COMPAT
  71. enum {
  72. V3_NO_INTERFACE = 0x80004002L,
  73. V3_OK = 0,
  74. V3_TRUE = 0,
  75. V3_FALSE = 1,
  76. V3_INVALID_ARG = 0x80070057L,
  77. V3_NOT_IMPLEMENTED = 0x80004001L,
  78. V3_INTERNAL_ERR = 0x80004005L,
  79. V3_NOT_INITIALIZED = 0x8000FFFFL,
  80. V3_NOMEM = 0x8007000EL
  81. };
  82. # define V3_ID(a, b, c, d) { \
  83. ((a) & 0x000000FF), \
  84. ((a) & 0x0000FF00) >> 8, \
  85. ((a) & 0x00FF0000) >> 16, \
  86. ((a) & 0xFF000000) >> 24, \
  87. \
  88. ((b) & 0x00FF0000) >> 16, \
  89. ((b) & 0xFF000000) >> 24, \
  90. ((b) & 0x000000FF), \
  91. ((b) & 0x0000FF00) >> 8, \
  92. \
  93. ((c) & 0xFF000000) >> 24, \
  94. ((c) & 0x00FF0000) >> 16, \
  95. ((c) & 0x0000FF00) >> 8, \
  96. ((c) & 0x000000FF), \
  97. \
  98. ((d) & 0xFF000000) >> 24, \
  99. ((d) & 0x00FF0000) >> 16, \
  100. ((d) & 0x0000FF00) >> 8, \
  101. ((d) & 0x000000FF), \
  102. }
  103. #else // V3_COM_COMPAT
  104. enum {
  105. V3_NO_INTERFACE = -1,
  106. V3_OK,
  107. V3_TRUE = V3_OK,
  108. V3_FALSE,
  109. V3_INVALID_ARG,
  110. V3_NOT_IMPLEMENTED,
  111. V3_INTERNAL_ERR,
  112. V3_NOT_INITIALIZED,
  113. V3_NOMEM
  114. };
  115. # define V3_ID(a, b, c, d) { \
  116. ((a) & 0xFF000000) >> 24, \
  117. ((a) & 0x00FF0000) >> 16, \
  118. ((a) & 0x0000FF00) >> 8, \
  119. ((a) & 0x000000FF), \
  120. \
  121. ((b) & 0xFF000000) >> 24, \
  122. ((b) & 0x00FF0000) >> 16, \
  123. ((b) & 0x0000FF00) >> 8, \
  124. ((b) & 0x000000FF), \
  125. \
  126. ((c) & 0xFF000000) >> 24, \
  127. ((c) & 0x00FF0000) >> 16, \
  128. ((c) & 0x0000FF00) >> 8, \
  129. ((c) & 0x000000FF), \
  130. \
  131. ((d) & 0xFF000000) >> 24, \
  132. ((d) & 0x00FF0000) >> 16, \
  133. ((d) & 0x0000FF00) >> 8, \
  134. ((d) & 0x000000FF), \
  135. }
  136. #endif // V3_COM_COMPAT
  137. #define V3_ID_COPY(iid) \
  138. { iid[0], iid[1], iid[ 2], iid[ 3], iid[ 4], iid[ 5], iid[ 6], iid[ 7], \
  139. iid[8], iid[9], iid[10], iid[11], iid[12], iid[13], iid[14], iid[15] }
  140. /**
  141. * funknown
  142. */
  143. struct v3_funknown {
  144. v3_result (V3_API* query_interface)(void* self, const v3_tuid iid, void** obj);
  145. uint32_t (V3_API* ref)(void* self);
  146. uint32_t (V3_API* unref)(void* self);
  147. };
  148. static constexpr const v3_tuid v3_funknown_iid =
  149. V3_ID(0x00000000, 0x00000000, 0xC0000000, 0x00000046);
  150. /**
  151. * plugin base
  152. */
  153. struct v3_plugin_base {
  154. struct v3_funknown;
  155. v3_result (V3_API* initialize)(void* self, struct v3_funknown** context);
  156. v3_result (V3_API* terminate)(void* self);
  157. };
  158. static constexpr const v3_tuid v3_plugin_base_iid =
  159. V3_ID(0x22888DDB, 0x156E45AE, 0x8358B348, 0x08190625);
  160. #ifdef __cplusplus
  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. #endif