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.

458 lines
14KB

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