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.

carla-lv2-ui.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef HAVE_PYQT
  18. # error This file should not be built
  19. #endif
  20. #include "CarlaLv2Utils.hpp"
  21. #include "CarlaPipeUtils.hpp"
  22. // --------------------------------------------------------------------------------------------------------------------
  23. class NativePluginUI : public LV2_External_UI_Widget_Compat
  24. {
  25. public:
  26. NativePluginUI(LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  27. LV2UI_Widget* widget, const LV2_Feature* const* features)
  28. : fUridMap(nullptr),
  29. fUridUnmap(nullptr),
  30. fUridTranser(0),
  31. fUridTranser2(0),
  32. fUI()
  33. {
  34. run = extui_run;
  35. show = extui_show;
  36. hide = extui_hide;
  37. fUI.writeFunction = writeFunction;
  38. fUI.controller = controller;
  39. const LV2_URID_Map* uridMap = nullptr;
  40. const LV2_URID_Unmap* uridUnmap = nullptr;
  41. for (int i=0; features[i] != nullptr; ++i)
  42. {
  43. /**/ if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  44. uridMap = (const LV2_URID_Map*)features[i]->data;
  45. else if (std::strcmp(features[i]->URI, LV2_URID__unmap) == 0)
  46. uridUnmap = (const LV2_URID_Unmap*)features[i]->data;
  47. }
  48. if (uridMap == nullptr)
  49. {
  50. carla_stderr("Host doesn't provide urid-map feature");
  51. return;
  52. }
  53. fUridMap = uridMap;
  54. fUridUnmap = uridUnmap;
  55. fUridTranser = uridMap->map(uridMap->handle, LV2_ATOM__eventTransfer);
  56. fUridTranser2 = uridMap->map(uridMap->handle, "urn:carla:transmitEv");
  57. // ---------------------------------------------------------------
  58. // see if the host supports external-ui
  59. for (int i=0; features[i] != nullptr; ++i)
  60. {
  61. if (std::strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host) == 0 ||
  62. std::strcmp(features[i]->URI, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  63. {
  64. fUI.host = (const LV2_External_UI_Host*)features[i]->data;
  65. break;
  66. }
  67. }
  68. if (fUI.host != nullptr)
  69. {
  70. fUI.name = carla_strdup(fUI.host->plugin_human_id);
  71. *widget = (LV2_External_UI_Widget_Compat*)this;
  72. return;
  73. }
  74. // ---------------------------------------------------------------
  75. // no external-ui support, use showInterface
  76. for (int i=0; features[i] != nullptr; ++i)
  77. {
  78. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) != 0)
  79. continue;
  80. const LV2_Options_Option* const options((const LV2_Options_Option*)features[i]->data);
  81. CARLA_SAFE_ASSERT_BREAK(options != nullptr);
  82. for (int j=0; options[j].key != 0; ++j)
  83. {
  84. if (options[j].key != uridMap->map(uridMap->handle, LV2_UI__windowTitle))
  85. continue;
  86. const char* const title((const char*)options[j].value);
  87. CARLA_SAFE_ASSERT_BREAK(title != nullptr && title[0] != '\0');
  88. fUI.name = carla_strdup(title);
  89. break;
  90. }
  91. break;
  92. }
  93. if (fUI.name == nullptr)
  94. fUI.name = carla_strdup("Carla");
  95. *widget = nullptr;
  96. }
  97. ~NativePluginUI()
  98. {
  99. if (fUI.isVisible)
  100. writeAtomMessage("quit");
  101. fUI.host = nullptr;
  102. fUI.writeFunction = nullptr;
  103. fUI.controller = nullptr;
  104. }
  105. // ----------------------------------------------------------------------------------------------------------------
  106. void lv2ui_port_event(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  107. {
  108. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  109. if (format == 0)
  110. {
  111. char msg[128];
  112. const float* const valuePtr = (const float*)buffer;
  113. {
  114. const ScopedLocale csl;
  115. std::snprintf(msg, 127, "control %u %f", portIndex, *valuePtr);
  116. }
  117. msg[127] = '\0';
  118. writeAtomMessage(msg);
  119. return;
  120. }
  121. if (format == fUridTranser)
  122. {
  123. CARLA_SAFE_ASSERT_RETURN(bufferSize > sizeof(LV2_Atom),);
  124. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  125. if (atom->type == fUridTranser2)
  126. {
  127. const char* const msg = (const char*)(atom + 1);
  128. if (std::strcmp(msg, "quit") == 0)
  129. {
  130. handleUiClosed();
  131. }
  132. return;
  133. }
  134. }
  135. if (fUridUnmap != nullptr)
  136. {
  137. carla_stdout("lv2ui_port_event %u %u %u:%s %p",
  138. portIndex, bufferSize, format, fUridUnmap->unmap(fUridUnmap->handle, format), buffer);
  139. }
  140. }
  141. // ----------------------------------------------------------------------------------------------------------------
  142. void lv2ui_select_program(uint32_t bank, uint32_t program) const
  143. {
  144. char msg[128];
  145. std::snprintf(msg, 127, "program %u %u", bank, program);
  146. msg[127] = '\0';
  147. writeAtomMessage(msg);
  148. }
  149. // ----------------------------------------------------------------------------------------------------------------
  150. int lv2ui_idle() const
  151. {
  152. if (! fUI.isVisible)
  153. return 1;
  154. handleUiRun();
  155. return 0;
  156. }
  157. int lv2ui_show()
  158. {
  159. handleUiShow();
  160. return 0;
  161. }
  162. int lv2ui_hide()
  163. {
  164. handleUiHide();
  165. return 0;
  166. }
  167. // ----------------------------------------------------------------------------------------------------------------
  168. protected:
  169. void handleUiShow()
  170. {
  171. writeAtomMessage("show");
  172. fUI.isVisible = true;
  173. }
  174. void handleUiHide()
  175. {
  176. if (fUI.isVisible)
  177. {
  178. fUI.isVisible = false;
  179. writeAtomMessage("hide");
  180. }
  181. }
  182. void handleUiRun() const
  183. {
  184. if (fUI.isVisible)
  185. writeAtomMessage("idle");
  186. }
  187. void handleUiClosed()
  188. {
  189. fUI.isVisible = false;
  190. if (fUI.host != nullptr && fUI.host->ui_closed != nullptr && fUI.controller != nullptr)
  191. fUI.host->ui_closed(fUI.controller);
  192. fUI.host = nullptr;
  193. fUI.writeFunction = nullptr;
  194. fUI.controller = nullptr;
  195. }
  196. bool writeAtomMessage(const char* const msg) const
  197. {
  198. CARLA_SAFE_ASSERT_RETURN(fUI.writeFunction != nullptr, false);
  199. CARLA_SAFE_ASSERT_RETURN(fUridTranser2 != 0, false);
  200. carla_debug("writeAtomMessage(%s)", msg);
  201. const size_t msgSize = std::strlen(msg)+1;
  202. const size_t atomSize = sizeof(LV2_Atom) + msgSize;
  203. if (atomSize <= 128)
  204. {
  205. char atomBuf[atomSize];
  206. carla_zeroChars(atomBuf, atomSize);
  207. LV2_Atom* const atom = (LV2_Atom*)atomBuf;
  208. atom->size = msgSize;
  209. atom->type = fUridTranser2;
  210. std::memcpy(atomBuf+sizeof(LV2_Atom), msg, msgSize);
  211. fUI.writeFunction(fUI.controller, 0, atomSize, fUridTranser, atomBuf);
  212. }
  213. else
  214. {
  215. char* const atomBuf = new char[atomSize];
  216. carla_zeroChars(atomBuf, atomSize);
  217. LV2_Atom* const atom = (LV2_Atom*)atomBuf;
  218. atom->size = msgSize;
  219. atom->type = fUridTranser2;
  220. std::memcpy(atomBuf+sizeof(LV2_Atom), msg, msgSize);
  221. fUI.writeFunction(fUI.controller, 0, atomSize, fUridTranser, atomBuf);
  222. delete[] atomBuf;
  223. }
  224. return true;
  225. }
  226. // ----------------------------------------------------------------------------------------------------------------
  227. private:
  228. const LV2_URID_Map* fUridMap;
  229. const LV2_URID_Unmap* fUridUnmap;
  230. LV2_URID fUridTranser, fUridTranser2;
  231. struct UI {
  232. const LV2_External_UI_Host* host;
  233. LV2UI_Write_Function writeFunction;
  234. LV2UI_Controller controller;
  235. const char* name;
  236. bool isVisible;
  237. UI()
  238. : host(nullptr),
  239. writeFunction(nullptr),
  240. controller(nullptr),
  241. name(nullptr),
  242. isVisible(false) {}
  243. ~UI()
  244. {
  245. if (name != nullptr)
  246. {
  247. delete[] name;
  248. name = nullptr;
  249. }
  250. }
  251. } fUI;
  252. // ----------------------------------------------------------------------------------------------------------------
  253. #define handlePtr ((NativePluginUI*)handle)
  254. static void extui_run(LV2_External_UI_Widget_Compat* handle)
  255. {
  256. handlePtr->handleUiRun();
  257. }
  258. static void extui_show(LV2_External_UI_Widget_Compat* handle)
  259. {
  260. carla_debug("extui_show(%p)", handle);
  261. handlePtr->handleUiShow();
  262. }
  263. static void extui_hide(LV2_External_UI_Widget_Compat* handle)
  264. {
  265. carla_debug("extui_hide(%p)", handle);
  266. handlePtr->handleUiHide();
  267. }
  268. #undef handlePtr
  269. // -------------------------------------------------------------------
  270. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePluginUI)
  271. };
  272. // -----------------------------------------------------------------------
  273. // LV2 UI descriptor functions
  274. static LV2UI_Handle lv2ui_instantiate(const LV2UI_Descriptor*, const char*, const char*,
  275. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  276. LV2UI_Widget* widget, const LV2_Feature* const* features)
  277. {
  278. carla_debug("lv2ui_instantiate(..., %p, %p, %p)", writeFunction, controller, widget, features);
  279. NativePluginUI* const ui = new NativePluginUI(writeFunction, controller, widget, features);
  280. // TODO: check ok
  281. return (LV2UI_Handle)ui;
  282. }
  283. #define uiPtr ((NativePluginUI*)ui)
  284. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  285. {
  286. carla_debug("lv2ui_port_event(%p, %i, %i, %i, %p)", ui, portIndex, bufferSize, format, buffer);
  287. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  288. }
  289. static void lv2ui_cleanup(LV2UI_Handle ui)
  290. {
  291. carla_debug("lv2ui_cleanup(%p)", ui);
  292. delete uiPtr;
  293. }
  294. static void lv2ui_select_program(LV2UI_Handle ui, uint32_t bank, uint32_t program)
  295. {
  296. carla_debug("lv2ui_select_program(%p, %i, %i)", ui, bank, program);
  297. uiPtr->lv2ui_select_program(bank, program);
  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. carla_debug("lv2ui_show(%p)", ui);
  306. return uiPtr->lv2ui_show();
  307. }
  308. static int lv2ui_hide(LV2UI_Handle ui)
  309. {
  310. carla_debug("lv2ui_hide(%p)", ui);
  311. return uiPtr->lv2ui_hide();
  312. }
  313. static const void* lv2ui_extension_data(const char* uri)
  314. {
  315. carla_stdout("lv2ui_extension_data(\"%s\")", uri);
  316. static const LV2UI_Idle_Interface uiidle = { lv2ui_idle };
  317. static const LV2UI_Show_Interface uishow = { lv2ui_show, lv2ui_hide };
  318. static const LV2_Programs_UI_Interface uiprograms = { lv2ui_select_program };
  319. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  320. return &uiidle;
  321. if (std::strcmp(uri, LV2_UI__showInterface) == 0)
  322. return &uishow;
  323. if (std::strcmp(uri, LV2_PROGRAMS__UIInterface) == 0)
  324. return &uiprograms;
  325. return nullptr;
  326. }
  327. #undef uiPtr
  328. // -----------------------------------------------------------------------
  329. // Startup code
  330. CARLA_EXPORT
  331. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  332. {
  333. carla_debug("lv2ui_descriptor(%i)", index);
  334. static const LV2UI_Descriptor lv2UiExtDesc = {
  335. /* URI */ "http://kxstudio.sf.net/carla/ui-bridge-ext",
  336. /* instantiate */ lv2ui_instantiate,
  337. /* cleanup */ lv2ui_cleanup,
  338. /* port_event */ lv2ui_port_event,
  339. /* extension_data */ lv2ui_extension_data
  340. };
  341. return (index == 0) ? &lv2UiExtDesc : nullptr;
  342. }
  343. // -----------------------------------------------------------------------
  344. #include "CarlaPipeUtils.cpp"
  345. // -----------------------------------------------------------------------