DISTRHO Plugin Framework
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.

442 lines
13KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 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/data-access.h"
  20. #include "lv2/instance-access.h"
  21. #include "lv2/options.h"
  22. #include "lv2/ui.h"
  23. #include "lv2/urid.h"
  24. #include "lv2/lv2_programs.h"
  25. #include <string>
  26. START_NAMESPACE_DISTRHO
  27. // -----------------------------------------------------------------------
  28. class UiLv2
  29. {
  30. public:
  31. UiLv2(const intptr_t winId,
  32. const LV2_Options_Option* options, const LV2_URID_Map* const uridMap, const LV2UI_Resize* const uiResz, const LV2UI_Touch* uiTouch,
  33. const LV2UI_Controller controller, const LV2UI_Write_Function writeFunc, void* const dspPtr)
  34. : fUI(this, winId, editParameterCallback, setParameterCallback, setStateCallback, sendNoteCallback, uiResizeCallback, dspPtr),
  35. fUridMap(uridMap),
  36. fUiResize(uiResz),
  37. fUiTouch(uiTouch),
  38. fController(controller),
  39. fWriteFunction(writeFunc),
  40. fEventTransferURID(uridMap->map(uridMap->handle, LV2_ATOM__eventTransfer)),
  41. fKeyValueURID(uridMap->map(uridMap->handle, "urn:distrho:keyValueState")),
  42. fWinIdWasNull(winId == 0)
  43. {
  44. if (winId == 0)
  45. fUI.setTitle(fUI.getName());
  46. else if (fUiResize != nullptr)
  47. fUiResize->ui_resize(fUiResize->handle, fUI.getWidth(), fUI.getHeight());
  48. #if DISTRHO_PLUGIN_WANT_STATE
  49. // tell the DSP we're ready to receive msgs
  50. setState("__dpf_ui_data__", "");
  51. #endif
  52. if (winId != 0)
  53. return;
  54. const LV2_URID uridFrontendWinId(uridMap->map(uridMap->handle, "http://kxstudio.sf.net/ns/carla/frontendWinId"));
  55. for (int i=0; options[i].key != 0; ++i)
  56. {
  57. if (options[i].key == uridFrontendWinId)
  58. {
  59. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Long))
  60. {
  61. if (const int64_t frontendWinId = *(const int64_t*)options[i].value)
  62. fUI.setTransientWinId(static_cast<intptr_t>(frontendWinId));
  63. }
  64. else
  65. d_stderr("Host provides frontendWinId but has wrong value type");
  66. break;
  67. }
  68. }
  69. }
  70. // -------------------------------------------------------------------
  71. void lv2ui_port_event(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  72. {
  73. if (format == 0)
  74. {
  75. const uint32_t parameterOffset(fUI.getParameterOffset());
  76. if (rindex < parameterOffset)
  77. return;
  78. if (bufferSize != sizeof(float))
  79. return;
  80. const float value(*(const float*)buffer);
  81. fUI.parameterChanged(rindex-parameterOffset, value);
  82. }
  83. #if DISTRHO_PLUGIN_WANT_STATE
  84. else if (format == fEventTransferURID)
  85. {
  86. const LV2_Atom* const atom((const LV2_Atom*)buffer);
  87. // TODO - check atom type
  88. const char* const stateKey((const char*)LV2_ATOM_BODY_CONST(atom));
  89. const char* const stateValue(stateKey+std::strlen(stateKey)+1);
  90. d_stdout("Got MSG in UI from DSP ==> %s | %s", stateKey, stateValue);
  91. fUI.stateChanged(stateKey, stateValue);
  92. }
  93. #endif
  94. }
  95. // -------------------------------------------------------------------
  96. int lv2ui_idle()
  97. {
  98. if (fWinIdWasNull)
  99. return (fUI.idle() && fUI.isVisible()) ? 0 : 1;
  100. return fUI.idle() ? 0 : 1;
  101. }
  102. int lv2ui_show()
  103. {
  104. return fUI.setVisible(true) ? 0 : 1;
  105. }
  106. int lv2ui_hide()
  107. {
  108. return fUI.setVisible(false) ? 0 : 1;
  109. }
  110. // -------------------------------------------------------------------
  111. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  112. void lv2ui_select_program(const uint32_t bank, const uint32_t program)
  113. {
  114. const uint32_t realProgram(bank * 128 + program);
  115. fUI.programChanged(realProgram);
  116. }
  117. #endif
  118. // -------------------------------------------------------------------
  119. protected:
  120. void editParameterValue(const uint32_t rindex, const bool started)
  121. {
  122. if (fUiTouch != nullptr && fUiTouch->touch != nullptr)
  123. fUiTouch->touch(fUiTouch->handle, rindex, started);
  124. }
  125. void setParameterValue(const uint32_t rindex, const float value)
  126. {
  127. if (fWriteFunction != nullptr)
  128. fWriteFunction(fController, rindex, sizeof(float), 0, &value);
  129. }
  130. void setState(const char* const key, const char* const value)
  131. {
  132. if (fWriteFunction == nullptr)
  133. return;
  134. const uint32_t eventInPortIndex(DISTRHO_PLUGIN_NUM_INPUTS + DISTRHO_PLUGIN_NUM_OUTPUTS);
  135. // join key and value
  136. std::string tmpStr;
  137. tmpStr += std::string(key);
  138. tmpStr += std::string("\0", 1);
  139. tmpStr += std::string(value);
  140. // get msg size
  141. const size_t msgSize(tmpStr.size()+1);
  142. // reserve atom space
  143. const size_t atomSize(lv2_atom_pad_size(sizeof(LV2_Atom) + msgSize));
  144. char atomBuf[atomSize];
  145. std::memset(atomBuf, 0, atomSize);
  146. // set atom info
  147. LV2_Atom* const atom((LV2_Atom*)atomBuf);
  148. atom->size = msgSize;
  149. atom->type = fKeyValueURID;
  150. // set atom data
  151. std::memcpy(atomBuf + sizeof(LV2_Atom), tmpStr.data(), msgSize-1);
  152. // send to DSP side
  153. fWriteFunction(fController, eventInPortIndex, atomSize, fEventTransferURID, atom);
  154. }
  155. void sendNote(const uint8_t /*channel*/, const uint8_t /*note*/, const uint8_t /*velocity*/)
  156. {
  157. }
  158. void uiResize(const uint width, const uint height)
  159. {
  160. fUI.setSize(width, height);
  161. if (fUiResize != nullptr && ! fWinIdWasNull)
  162. fUiResize->ui_resize(fUiResize->handle, width, height);
  163. }
  164. private:
  165. UIExporter fUI;
  166. // LV2 features
  167. const LV2_URID_Map* const fUridMap;
  168. const LV2UI_Resize* const fUiResize;
  169. const LV2UI_Touch* const fUiTouch;
  170. // LV2 UI stuff
  171. const LV2UI_Controller fController;
  172. const LV2UI_Write_Function fWriteFunction;
  173. // Need to save this
  174. const LV2_URID fEventTransferURID;
  175. const LV2_URID fKeyValueURID;
  176. // using ui:showInterface if true
  177. bool fWinIdWasNull;
  178. // -------------------------------------------------------------------
  179. // Callbacks
  180. #define uiPtr ((UiLv2*)ptr)
  181. static void editParameterCallback(void* ptr, uint32_t rindex, bool started)
  182. {
  183. uiPtr->editParameterValue(rindex, started);
  184. }
  185. static void setParameterCallback(void* ptr, uint32_t rindex, float value)
  186. {
  187. uiPtr->setParameterValue(rindex, value);
  188. }
  189. static void setStateCallback(void* ptr, const char* key, const char* value)
  190. {
  191. uiPtr->setState(key, value);
  192. }
  193. static void sendNoteCallback(void* ptr, uint8_t channel, uint8_t note, uint8_t velocity)
  194. {
  195. uiPtr->sendNote(channel, note, velocity);
  196. }
  197. static void uiResizeCallback(void* ptr, uint width, uint height)
  198. {
  199. uiPtr->uiResize(width, height);
  200. }
  201. #undef uiPtr
  202. };
  203. // -----------------------------------------------------------------------
  204. 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)
  205. {
  206. if (uri == nullptr || std::strcmp(uri, DISTRHO_PLUGIN_URI) != 0)
  207. {
  208. d_stderr("Invalid plugin URI");
  209. return nullptr;
  210. }
  211. const LV2_Options_Option* options = nullptr;
  212. const LV2_URID_Map* uridMap = nullptr;
  213. const LV2UI_Resize* uiResize = nullptr;
  214. const LV2UI_Touch* uiTouch = nullptr;
  215. void* parentId = nullptr;
  216. void* instance = nullptr;
  217. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  218. # define DISTRHO_DIRECT_ACCESS_URI "urn:distrho:direct-access"
  219. struct LV2_DirectAccess_Interface {
  220. void* (*get_instance_pointer)(LV2_Handle handle);
  221. };
  222. const LV2_Extension_Data_Feature* extData = nullptr;
  223. #endif
  224. for (int i=0; features[i] != nullptr; ++i)
  225. {
  226. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  227. options = (const LV2_Options_Option*)features[i]->data;
  228. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  229. uridMap = (const LV2_URID_Map*)features[i]->data;
  230. else if (std::strcmp(features[i]->URI, LV2_UI__resize) == 0)
  231. uiResize = (const LV2UI_Resize*)features[i]->data;
  232. else if (std::strcmp(features[i]->URI, LV2_UI__parent) == 0)
  233. parentId = features[i]->data;
  234. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  235. else if (std::strcmp(features[i]->URI, LV2_DATA_ACCESS_URI) == 0)
  236. extData = (const LV2_Extension_Data_Feature*)features[i]->data;
  237. else if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  238. instance = features[i]->data;
  239. #endif
  240. }
  241. if (options == nullptr)
  242. {
  243. d_stderr("Options feature missing, cannot continue!");
  244. return nullptr;
  245. }
  246. if (uridMap == nullptr)
  247. {
  248. d_stderr("URID Map feature missing, cannot continue!");
  249. return nullptr;
  250. }
  251. if (parentId == nullptr)
  252. {
  253. d_stdout("Parent Window Id missing, host should be using ui:showInterface...");
  254. }
  255. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  256. if (extData == nullptr || instance == nullptr)
  257. {
  258. d_stderr("Data or instance access missing, cannot continue!");
  259. return nullptr;
  260. }
  261. if (const LV2_DirectAccess_Interface* const directAccess = (const LV2_DirectAccess_Interface*)extData->data_access(DISTRHO_DIRECT_ACCESS_URI))
  262. {
  263. instance = directAccess->get_instance_pointer(instance);
  264. }
  265. else
  266. {
  267. d_stderr("Failed to get direct access, cannot continue!");
  268. return nullptr;
  269. }
  270. #endif
  271. *widget = parentId;
  272. const intptr_t winId((intptr_t)parentId);
  273. const LV2_URID uridSampleRate(uridMap->map(uridMap->handle, LV2_CORE__sampleRate));
  274. for (int i=0; options[i].key != 0; ++i)
  275. {
  276. if (options[i].key == uridSampleRate)
  277. {
  278. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Double))
  279. d_lastUiSampleRate = *(const double*)options[i].value;
  280. else
  281. d_stderr("Host provides sampleRate but has wrong value type");
  282. break;
  283. }
  284. }
  285. if (d_lastUiSampleRate == 0.0)
  286. d_lastUiSampleRate = 44100.0;
  287. return new UiLv2(winId, options, uridMap, uiResize, uiTouch, controller, writeFunction, instance);
  288. }
  289. #define uiPtr ((UiLv2*)ui)
  290. static void lv2ui_cleanup(LV2UI_Handle ui)
  291. {
  292. delete uiPtr;
  293. }
  294. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  295. {
  296. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  297. }
  298. // -----------------------------------------------------------------------
  299. static int lv2ui_idle(LV2UI_Handle ui)
  300. {
  301. return uiPtr->lv2ui_idle();
  302. }
  303. static int lv2ui_show(LV2UI_Handle ui)
  304. {
  305. return uiPtr->lv2ui_show();
  306. }
  307. static int lv2ui_hide(LV2UI_Handle ui)
  308. {
  309. return uiPtr->lv2ui_hide();
  310. }
  311. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  312. static void lv2ui_select_program(LV2UI_Handle ui, uint32_t bank, uint32_t program)
  313. {
  314. uiPtr->lv2ui_select_program(bank, program);
  315. }
  316. #endif
  317. // -----------------------------------------------------------------------
  318. static const void* lv2ui_extension_data(const char* uri)
  319. {
  320. static const LV2UI_Idle_Interface uiIdle = { lv2ui_idle };
  321. static const LV2UI_Show_Interface uiShow = { lv2ui_show, lv2ui_hide };
  322. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  323. return &uiIdle;
  324. if (std::strcmp(uri, LV2_UI__showInterface) == 0)
  325. return &uiShow;
  326. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  327. static const LV2_Programs_UI_Interface uiPrograms = { lv2ui_select_program };
  328. if (std::strcmp(uri, LV2_PROGRAMS__UIInterface) == 0)
  329. return &uiPrograms;
  330. #endif
  331. return nullptr;
  332. }
  333. #undef instancePtr
  334. // -----------------------------------------------------------------------
  335. static const LV2UI_Descriptor sLv2UiDescriptor = {
  336. DISTRHO_UI_URI,
  337. lv2ui_instantiate,
  338. lv2ui_cleanup,
  339. lv2ui_port_event,
  340. lv2ui_extension_data
  341. };
  342. // -----------------------------------------------------------------------
  343. END_NAMESPACE_DISTRHO
  344. DISTRHO_PLUGIN_EXPORT
  345. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  346. {
  347. USE_NAMESPACE_DISTRHO
  348. return (index == 0) ? &sLv2UiDescriptor : nullptr;
  349. }
  350. // -----------------------------------------------------------------------