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.

334 lines
9.8KB

  1. /*
  2. * DISTRHO Plugin Toolkit (DPT)
  3. * Copyright (C) 2012-2013 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. #include "DistrhoUIInternal.hpp"
  17. #include "lv2/atom.h"
  18. #include "lv2/atom-util.h"
  19. #include "lv2/options.h"
  20. #include "lv2/ui.h"
  21. #include "lv2/urid.h"
  22. #include "lv2/lv2_programs.h"
  23. #include <string>
  24. START_NAMESPACE_DISTRHO
  25. // -----------------------------------------------------------------------
  26. class UiLv2
  27. {
  28. public:
  29. UiLv2(const intptr_t winId, const LV2_URID_Map* const uridMap, const LV2UI_Resize* const uiResz, const LV2UI_Touch* uiTouch, const LV2UI_Controller controller, const LV2UI_Write_Function writeFunc)
  30. : fUI(this, winId, editParameterCallback, setParameterCallback, setStateCallback, sendNoteCallback, uiResizeCallback),
  31. fUridMap(uridMap),
  32. fUiResize(uiResz),
  33. fUiTouch(uiTouch),
  34. fController(controller),
  35. fWriteFunction(writeFunc)
  36. {
  37. fUiResize->ui_resize(fUiResize->handle, fUI.getWidth(), fUI.getHeight());
  38. }
  39. // -------------------------------------------------------------------
  40. void lv2ui_port_event(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  41. {
  42. if (format == 0)
  43. {
  44. const uint32_t parameterOffset(fUI.getParameterOffset());
  45. if (rindex < parameterOffset)
  46. return;
  47. if (bufferSize != sizeof(float))
  48. return;
  49. const float value(*(const float*)buffer);
  50. fUI.parameterChanged(rindex-parameterOffset, value);
  51. }
  52. else
  53. {
  54. //fUI.stateChanged(key, value);
  55. }
  56. }
  57. // -------------------------------------------------------------------
  58. int lv2ui_idle()
  59. {
  60. fUI.idle();
  61. return 0;
  62. }
  63. // -------------------------------------------------------------------
  64. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  65. void lv2ui_select_program(const uint32_t bank, const uint32_t program)
  66. {
  67. const uint32_t realProgram(bank * 128 + program);
  68. fUI.programChanged(realProgram);
  69. }
  70. #endif
  71. // -------------------------------------------------------------------
  72. protected:
  73. void editParameterValue(const uint32_t rindex, const bool started)
  74. {
  75. if (fUiTouch != nullptr && fUiTouch->touch != nullptr)
  76. fUiTouch->touch(fUiTouch->handle, rindex, started);
  77. }
  78. void setParameterValue(const uint32_t rindex, const float value)
  79. {
  80. if (fWriteFunction != nullptr)
  81. fWriteFunction(fController, rindex, sizeof(float), 0, &value);
  82. }
  83. void setState(const char* const key, const char* const value)
  84. {
  85. if (fWriteFunction == nullptr)
  86. return;
  87. const uint32_t eventInPortIndex(DISTRHO_PLUGIN_NUM_INPUTS + DISTRHO_PLUGIN_NUM_OUTPUTS);
  88. // join key and value
  89. std::string tmpStr;
  90. tmpStr += std::string(key);
  91. tmpStr += std::string("\0", 1);
  92. tmpStr += std::string(value);
  93. // get msg size
  94. const size_t msgSize(tmpStr.size()+1);
  95. // reserve atom space
  96. const size_t atomSize(lv2_atom_pad_size(sizeof(LV2_Atom) + msgSize));
  97. char atomBuf[atomSize];
  98. std::memset(atomBuf, 0, atomSize);
  99. // set atom info
  100. LV2_Atom* const atom((LV2_Atom*)atomBuf);
  101. atom->size = msgSize;
  102. atom->type = fUridMap->map(fUridMap->handle, "urn:distrho:keyValueState");
  103. // set atom data
  104. std::memcpy(atomBuf + sizeof(LV2_Atom), tmpStr.data(), msgSize-1);
  105. // send to DSP side
  106. fWriteFunction(fController, eventInPortIndex, atomSize, fUridMap->map(fUridMap->handle, LV2_ATOM__eventTransfer), atom);
  107. }
  108. void sendNote(const uint8_t /*channel*/, const uint8_t /*note*/, const uint8_t /*velocity*/)
  109. {
  110. }
  111. void uiResize(const unsigned int width, const unsigned int height)
  112. {
  113. fUI.setSize(width, height);
  114. fUiResize->ui_resize(fUiResize->handle, width, height);
  115. }
  116. private:
  117. UIExporter fUI;
  118. // LV2 features
  119. const LV2_URID_Map* const fUridMap;
  120. const LV2UI_Resize* const fUiResize;
  121. const LV2UI_Touch* const fUiTouch;
  122. // LV2 UI stuff
  123. const LV2UI_Controller fController;
  124. const LV2UI_Write_Function fWriteFunction;
  125. // -------------------------------------------------------------------
  126. // Callbacks
  127. #define uiPtr ((UiLv2*)ptr)
  128. static void editParameterCallback(void* ptr, uint32_t rindex, bool started)
  129. {
  130. uiPtr->editParameterValue(rindex, started);
  131. }
  132. static void setParameterCallback(void* ptr, uint32_t rindex, float value)
  133. {
  134. uiPtr->setParameterValue(rindex, value);
  135. }
  136. static void setStateCallback(void* ptr, const char* key, const char* value)
  137. {
  138. uiPtr->setState(key, value);
  139. }
  140. static void sendNoteCallback(void* ptr, uint8_t channel, uint8_t note, uint8_t velocity)
  141. {
  142. uiPtr->sendNote(channel, note, velocity);
  143. }
  144. static void uiResizeCallback(void* ptr, unsigned int width, unsigned int height)
  145. {
  146. uiPtr->uiResize(width, height);
  147. }
  148. #undef uiPtr
  149. };
  150. // -----------------------------------------------------------------------
  151. static LV2UI_Handle lv2ui_instantiate(const LV2UI_Descriptor*, const char* uri, const char*, LV2UI_Write_Function writeFunction, LV2UI_Controller controller, LV2UI_Widget* widget, const LV2_Feature* const* features)
  152. {
  153. if (uri == nullptr || std::strcmp(uri, DISTRHO_PLUGIN_URI) != 0)
  154. {
  155. d_stderr("Invalid plugin URI");
  156. return nullptr;
  157. }
  158. const LV2_Options_Option* options = nullptr;
  159. const LV2_URID_Map* uridMap = nullptr;
  160. const LV2UI_Resize* uiResize = nullptr;
  161. const LV2UI_Touch* uiTouch = nullptr;
  162. void* parentId = nullptr;
  163. for (int i=0; features[i] != nullptr; ++i)
  164. {
  165. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  166. options = (const LV2_Options_Option*)features[i]->data;
  167. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  168. uridMap = (const LV2_URID_Map*)features[i]->data;
  169. else if (std::strcmp(features[i]->URI, LV2_UI__resize) == 0)
  170. uiResize = (const LV2UI_Resize*)features[i]->data;
  171. else if (std::strcmp(features[i]->URI, LV2_UI__parent) == 0)
  172. parentId = features[i]->data;
  173. }
  174. if (options == nullptr)
  175. {
  176. d_stderr("Options feature missing, cannot continue!");
  177. return nullptr;
  178. }
  179. if (uridMap == nullptr)
  180. {
  181. d_stderr("URID Map feature missing, cannot continue!");
  182. return nullptr;
  183. }
  184. if (uiResize == nullptr)
  185. {
  186. d_stderr("UI Resize feature missing, cannot continue!");
  187. return nullptr;
  188. }
  189. if (parentId == nullptr)
  190. {
  191. d_stderr("Parent Window Id missing, cannot continue!");
  192. return nullptr;
  193. }
  194. *widget = parentId;
  195. const intptr_t winId(*((intptr_t*)&parentId));
  196. for (int i=0; options[i].key != 0; ++i)
  197. {
  198. if (options[i].key == uridMap->map(uridMap->handle, LV2_CORE__sampleRate))
  199. {
  200. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Double))
  201. d_lastUiSampleRate = *(const double*)options[i].value;
  202. else
  203. d_stderr("Host provides sampleRate but has wrong value type");
  204. break;
  205. }
  206. }
  207. if (d_lastUiSampleRate == 0.0)
  208. d_lastUiSampleRate = 44100.0;
  209. return new UiLv2(winId, uridMap, uiResize, uiTouch, controller, writeFunction);
  210. }
  211. #define uiPtr ((UiLv2*)ui)
  212. static void lv2ui_cleanup(LV2UI_Handle ui)
  213. {
  214. delete uiPtr;
  215. }
  216. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  217. {
  218. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  219. }
  220. // -----------------------------------------------------------------------
  221. static int lv2ui_idle(LV2UI_Handle ui)
  222. {
  223. return uiPtr->lv2ui_idle();
  224. }
  225. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  226. static void lv2ui_select_program(LV2UI_Handle ui, uint32_t bank, uint32_t program)
  227. {
  228. uiPtr->lv2ui_select_program(bank, program);
  229. }
  230. #endif
  231. // -----------------------------------------------------------------------
  232. static const void* lv2ui_extension_data(const char* uri)
  233. {
  234. static const LV2UI_Idle_Interface uiIdle = { lv2ui_idle };
  235. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  236. return &uiIdle;
  237. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  238. static const LV2_Programs_UI_Interface uiPrograms = { lv2ui_select_program };
  239. if (std::strcmp(uri, LV2_PROGRAMS__UIInterface) == 0)
  240. return &uiPrograms;
  241. #endif
  242. return nullptr;
  243. }
  244. #undef instancePtr
  245. // -----------------------------------------------------------------------
  246. static const LV2UI_Descriptor sLv2UiDescriptor = {
  247. DISTRHO_UI_URI,
  248. lv2ui_instantiate,
  249. lv2ui_cleanup,
  250. lv2ui_port_event,
  251. lv2ui_extension_data
  252. };
  253. // -----------------------------------------------------------------------
  254. END_NAMESPACE_DISTRHO
  255. DISTRHO_PLUGIN_EXPORT
  256. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  257. {
  258. USE_NAMESPACE_DISTRHO
  259. return (index == 0) ? &sLv2UiDescriptor : nullptr;
  260. }
  261. // -----------------------------------------------------------------------