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.

1131 lines
38KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-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. #include "DistrhoUIInternal.hpp"
  17. #include "travesty/edit_controller.h"
  18. #include "travesty/host.h"
  19. #include "travesty/view.h"
  20. #ifdef DISTRHO_PROPER_CPP11_SUPPORT
  21. # include <atomic>
  22. #else
  23. // quick and dirty std::atomic replacement for the things we need
  24. namespace std {
  25. struct atomic_int {
  26. volatile int value;
  27. explicit atomic_int(volatile int v) noexcept : value(v) {}
  28. int operator++() volatile noexcept { return __atomic_add_fetch(&value, 1, __ATOMIC_RELAXED); }
  29. int operator--() volatile noexcept { return __atomic_sub_fetch(&value, 1, __ATOMIC_RELAXED); }
  30. operator int() volatile noexcept { return __atomic_load_n(&value, __ATOMIC_RELAXED); }
  31. };
  32. };
  33. #endif
  34. /* TODO items:
  35. * - mousewheel event
  36. * - key down/up events
  37. * - size constraints
  38. * - host-side resize
  39. * - oddities with init and size callback (triggered too early?)
  40. */
  41. #if !(defined(DISTRHO_OS_MAC) || defined(DISTRHO_OS_WINDOWS))
  42. # define DPF_VST3_USING_HOST_RUN_LOOP
  43. #endif
  44. #ifndef DPF_VST3_TIMER_INTERVAL
  45. # define DPF_VST3_TIMER_INTERVAL 16 /* ~60 fps */
  46. #endif
  47. START_NAMESPACE_DISTRHO
  48. // --------------------------------------------------------------------------------------------------------------------
  49. #if ! DISTRHO_PLUGIN_WANT_MIDI_INPUT
  50. static constexpr const sendNoteFunc sendNoteCallback = nullptr;
  51. #endif
  52. #if ! DISTRHO_PLUGIN_WANT_STATE
  53. static constexpr const setStateFunc setStateCallback = nullptr;
  54. #endif
  55. // --------------------------------------------------------------------------------------------------------------------
  56. // Utility functions (defined on plugin side)
  57. const char* tuid2str(const v3_tuid iid);
  58. void strncpy_utf16(int16_t* dst, const char* src, size_t length);
  59. struct ScopedUTF16String {
  60. int16_t* str;
  61. ScopedUTF16String(const char* const s) noexcept;
  62. ~ScopedUTF16String() noexcept;
  63. operator int16_t*() const noexcept;
  64. };
  65. // --------------------------------------------------------------------------------------------------------------------
  66. /**
  67. * VST3 UI class.
  68. *
  69. * All the dynamic things from VST3 get implemented here, free of complex low-level VST3 pointer things.
  70. * The UI is created during the "attach" view event, and destroyed during "removed".
  71. *
  72. * Note that DPF VST3 implementation works over the connection point interface,
  73. * rather than using edit controller directly.
  74. * This allows the UI to be running remotely from the DSP.
  75. *
  76. * The low-level VST3 stuff comes after.
  77. */
  78. class UIVst3
  79. #if defined(DISTRHO_OS_MAC) || defined(DISTRHO_OS_WINDOWS)
  80. : public IdleCallback
  81. #endif
  82. {
  83. public:
  84. UIVst3(v3_plugin_view** const view,
  85. v3_host_application** const host,
  86. const intptr_t winId,
  87. const float scaleFactor,
  88. const double sampleRate,
  89. void* const instancePointer)
  90. : fUI(this, winId, sampleRate,
  91. editParameterCallback,
  92. setParameterCallback,
  93. setStateCallback,
  94. sendNoteCallback,
  95. setSizeCallback,
  96. nullptr, // TODO file request
  97. nullptr, // bundlePath
  98. instancePointer,
  99. scaleFactor),
  100. fView(view),
  101. fHostContext(host),
  102. fConnection(nullptr),
  103. fFrame(nullptr),
  104. fReadyForPluginData(false),
  105. fScaleFactor(scaleFactor)
  106. {
  107. #if defined(DISTRHO_OS_MAC) || defined(DISTRHO_OS_WINDOWS)
  108. fUI.addIdleCallbackForVST3(this, DPF_VST3_TIMER_INTERVAL);
  109. #endif
  110. }
  111. ~UIVst3()
  112. {
  113. #if defined(DISTRHO_OS_MAC) || defined(DISTRHO_OS_WINDOWS)
  114. fUI.removeIdleCallbackForVST3(this);
  115. #endif
  116. if (fConnection != nullptr)
  117. disconnect();
  118. }
  119. // ----------------------------------------------------------------------------------------------------------------
  120. // v3_plugin_view interface calls
  121. v3_result onWheel(float /*distance*/)
  122. {
  123. // TODO
  124. return V3_NOT_IMPLEMENTED;
  125. }
  126. v3_result onKeyDown(int16_t /*key_char*/, int16_t /*key_code*/, int16_t /*modifiers*/)
  127. {
  128. // TODO
  129. return V3_NOT_IMPLEMENTED;
  130. }
  131. v3_result onKeyUp(int16_t /*key_char*/, int16_t /*key_code*/, int16_t /*modifiers*/)
  132. {
  133. // TODO
  134. return V3_NOT_IMPLEMENTED;
  135. }
  136. v3_result getSize(v3_view_rect* const rect) const noexcept
  137. {
  138. std::memset(rect, 0, sizeof(v3_view_rect));
  139. rect->right = fUI.getWidth();
  140. rect->bottom = fUI.getHeight();
  141. #ifdef DISTRHO_OS_MAC
  142. const double scaleFactor = fUI.getScaleFactor();
  143. rect->right /= scaleFactor;
  144. rect->bottom /= scaleFactor;
  145. #endif
  146. return V3_OK;
  147. }
  148. v3_result onSize(v3_view_rect* const /*rect*/)
  149. {
  150. // TODO
  151. return V3_NOT_IMPLEMENTED;
  152. }
  153. v3_result onFocus(const bool state)
  154. {
  155. #if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  156. fUI.notifyFocusChanged(state);
  157. return V3_OK;
  158. #else
  159. return V3_NOT_IMPLEMENTED;
  160. // unused
  161. (void)state;
  162. #endif
  163. }
  164. v3_result setFrame(v3_plugin_frame** const frame) noexcept
  165. {
  166. fFrame = frame;
  167. return V3_OK;
  168. }
  169. v3_result checkSizeConstraint(v3_view_rect* const /*rect*/)
  170. {
  171. // TODO
  172. return V3_NOT_IMPLEMENTED;
  173. }
  174. // ----------------------------------------------------------------------------------------------------------------
  175. // v3_connection_point interface calls
  176. void connect(v3_connection_point** const point) noexcept
  177. {
  178. DISTRHO_SAFE_ASSERT_RETURN(point != nullptr,);
  179. fConnection = point;
  180. d_stdout("requesting current plugin state");
  181. v3_message** const message = createMessage("init");
  182. DISTRHO_SAFE_ASSERT_RETURN(message != nullptr,);
  183. v3_attribute_list** const attrlist = v3_cpp_obj(message)->get_attributes(message);
  184. DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,);
  185. v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 1);
  186. v3_cpp_obj(fConnection)->notify(fConnection, message);
  187. v3_cpp_obj_unref(attrlist);
  188. v3_cpp_obj_unref(message);
  189. }
  190. void disconnect() noexcept
  191. {
  192. DISTRHO_SAFE_ASSERT_RETURN(fConnection != nullptr,);
  193. d_stdout("reporting UI closed");
  194. fReadyForPluginData = false;
  195. v3_message** const message = createMessage("close");
  196. DISTRHO_SAFE_ASSERT_RETURN(message != nullptr,);
  197. v3_attribute_list** const attrlist = v3_cpp_obj(message)->get_attributes(message);
  198. DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,);
  199. v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 1);
  200. v3_cpp_obj(fConnection)->notify(fConnection, message);
  201. v3_cpp_obj_unref(attrlist);
  202. v3_cpp_obj_unref(message);
  203. fConnection = nullptr;
  204. }
  205. v3_result notify(v3_message** const message)
  206. {
  207. const char* const msgid = v3_cpp_obj(message)->get_message_id(message);
  208. DISTRHO_SAFE_ASSERT_RETURN(msgid != nullptr, V3_INVALID_ARG);
  209. v3_attribute_list** const attrs = v3_cpp_obj(message)->get_attributes(message);
  210. DISTRHO_SAFE_ASSERT_RETURN(attrs != nullptr, V3_INVALID_ARG);
  211. if (std::strcmp(msgid, "ready") == 0)
  212. {
  213. DISTRHO_SAFE_ASSERT_RETURN(! fReadyForPluginData, V3_INTERNAL_ERR);
  214. fReadyForPluginData = true;
  215. return V3_OK;
  216. }
  217. if (std::strcmp(msgid, "parameter-set") == 0)
  218. {
  219. int64_t rindex;
  220. double value;
  221. v3_result res;
  222. res = v3_cpp_obj(attrs)->get_int(attrs, "rindex", &rindex);
  223. DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res);
  224. res = v3_cpp_obj(attrs)->get_float(attrs, "value", &value);
  225. DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res);
  226. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  227. if (rindex == 0)
  228. {
  229. DISTRHO_SAFE_ASSERT_RETURN(value >= 0.0, V3_INTERNAL_ERR);
  230. fUI.programLoaded(static_cast<uint32_t>(value + 0.5));
  231. }
  232. else
  233. #endif
  234. {
  235. rindex -= fUI.getParameterOffset();
  236. DISTRHO_SAFE_ASSERT_RETURN(rindex >= 0, V3_INTERNAL_ERR);
  237. fUI.parameterChanged(static_cast<uint32_t>(rindex), value);
  238. }
  239. return V3_OK;
  240. }
  241. #if DISTRHO_PLUGIN_WANT_STATE
  242. if (std::strcmp(msgid, "state-set") == 0)
  243. {
  244. int16_t* key16;
  245. int16_t* value16;
  246. uint32_t keySize, valueSize;
  247. v3_result res;
  248. res = v3_cpp_obj(attrs)->get_binary(attrs, "key", (const void**)&key16, &keySize);
  249. DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res);
  250. res = v3_cpp_obj(attrs)->get_binary(attrs, "value", (const void**)&value16, &valueSize);
  251. DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res);
  252. // do cheap inline conversion
  253. char* const key = (char*)key16;
  254. char* const value = (char*)value16;
  255. for (uint32_t i=0; i<keySize/sizeof(int16_t); ++i)
  256. key[i] = key16[i];
  257. for (uint32_t i=0; i<valueSize/sizeof(int16_t); ++i)
  258. value[i] = value16[i];
  259. fUI.stateChanged(key, value);
  260. return V3_OK;
  261. }
  262. #endif
  263. if (std::strcmp(msgid, "sample-rate") == 0)
  264. {
  265. double sampleRate;
  266. v3_result res;
  267. res = v3_cpp_obj(attrs)->get_float(attrs, "value", &sampleRate);
  268. DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res);
  269. DISTRHO_SAFE_ASSERT_RETURN(sampleRate > 0, V3_INVALID_ARG);
  270. fUI.setSampleRate(sampleRate, true);
  271. return V3_OK;
  272. }
  273. d_stdout("UIVst3 received unknown msg '%s'", msgid);
  274. return V3_NOT_IMPLEMENTED;
  275. }
  276. // ----------------------------------------------------------------------------------------------------------------
  277. // v3_plugin_view_content_scale_steinberg interface calls
  278. v3_result setContentScaleFactor(const float factor)
  279. {
  280. if (d_isEqual(fScaleFactor, factor))
  281. return V3_OK;
  282. fScaleFactor = factor;
  283. fUI.notifyScaleFactorChanged(factor);
  284. return V3_OK;
  285. }
  286. // ----------------------------------------------------------------------------------------------------------------
  287. // special idle callback on macOS and Windows
  288. #if defined(DISTRHO_OS_MAC) || defined(DISTRHO_OS_WINDOWS)
  289. void idleCallback() override
  290. {
  291. if (fReadyForPluginData)
  292. {
  293. fReadyForPluginData = false;
  294. requestMorePluginData();
  295. }
  296. fUI.idleForVST3();
  297. }
  298. #else
  299. // ----------------------------------------------------------------------------------------------------------------
  300. // v3_timer_handler interface calls
  301. void onTimer()
  302. {
  303. if (fReadyForPluginData)
  304. {
  305. fReadyForPluginData = false;
  306. requestMorePluginData();
  307. }
  308. fUI.plugin_idle();
  309. }
  310. #endif
  311. // ----------------------------------------------------------------------------------------------------------------
  312. private:
  313. // Plugin UI
  314. UIExporter fUI;
  315. // VST3 stuff
  316. v3_plugin_view** const fView;
  317. v3_host_application** const fHostContext;
  318. v3_connection_point** fConnection;
  319. v3_plugin_frame** fFrame;
  320. // Temporary data
  321. bool fReadyForPluginData;
  322. float fScaleFactor;
  323. // ----------------------------------------------------------------------------------------------------------------
  324. // helper functions called during message passing
  325. v3_message** createMessage(const char* const id) const
  326. {
  327. DISTRHO_SAFE_ASSERT_RETURN(fHostContext != nullptr, nullptr);
  328. v3_tuid iid;
  329. memcpy(iid, v3_message_iid, sizeof(v3_tuid));
  330. v3_message** msg = nullptr;
  331. const v3_result res = v3_cpp_obj(fHostContext)->create_instance(fHostContext, iid, iid, (void**)&msg);
  332. DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_TRUE, res, nullptr);
  333. DISTRHO_SAFE_ASSERT_RETURN(msg != nullptr, nullptr);
  334. v3_cpp_obj(msg)->set_message_id(msg, id);
  335. return msg;
  336. }
  337. void requestMorePluginData() const
  338. {
  339. DISTRHO_SAFE_ASSERT_RETURN(fConnection != nullptr,);
  340. v3_message** const message = createMessage("idle");
  341. DISTRHO_SAFE_ASSERT_RETURN(message != nullptr,);
  342. v3_attribute_list** const attrlist = v3_cpp_obj(message)->get_attributes(message);
  343. DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,);
  344. v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 1);
  345. v3_cpp_obj(fConnection)->notify(fConnection, message);
  346. v3_cpp_obj_unref(attrlist);
  347. v3_cpp_obj_unref(message);
  348. }
  349. // ----------------------------------------------------------------------------------------------------------------
  350. // DPF callbacks
  351. void editParameter(const uint32_t rindex, const bool started) const
  352. {
  353. DISTRHO_SAFE_ASSERT_RETURN(fConnection != nullptr,);
  354. v3_message** const message = createMessage("parameter-edit");
  355. DISTRHO_SAFE_ASSERT_RETURN(message != nullptr,);
  356. v3_attribute_list** const attrlist = v3_cpp_obj(message)->get_attributes(message);
  357. DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,);
  358. v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 1);
  359. v3_cpp_obj(attrlist)->set_int(attrlist, "rindex", rindex);
  360. v3_cpp_obj(attrlist)->set_int(attrlist, "started", started ? 1 : 0);
  361. v3_cpp_obj(fConnection)->notify(fConnection, message);
  362. v3_cpp_obj_unref(attrlist);
  363. v3_cpp_obj_unref(message);
  364. }
  365. static void editParameterCallback(void* ptr, uint32_t rindex, bool started)
  366. {
  367. ((UIVst3*)ptr)->editParameter(rindex, started);
  368. }
  369. void setParameterValue(const uint32_t rindex, const float realValue)
  370. {
  371. DISTRHO_SAFE_ASSERT_RETURN(fConnection != nullptr,);
  372. v3_message** const message = createMessage("parameter-set");
  373. DISTRHO_SAFE_ASSERT_RETURN(message != nullptr,);
  374. v3_attribute_list** const attrlist = v3_cpp_obj(message)->get_attributes(message);
  375. DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,);
  376. v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 1);
  377. v3_cpp_obj(attrlist)->set_int(attrlist, "rindex", rindex);
  378. v3_cpp_obj(attrlist)->set_float(attrlist, "value", realValue);
  379. v3_cpp_obj(fConnection)->notify(fConnection, message);
  380. v3_cpp_obj_unref(attrlist);
  381. v3_cpp_obj_unref(message);
  382. }
  383. static void setParameterCallback(void* ptr, uint32_t rindex, float value)
  384. {
  385. ((UIVst3*)ptr)->setParameterValue(rindex, value);
  386. }
  387. void setSize(uint width, uint height)
  388. {
  389. DISTRHO_SAFE_ASSERT_RETURN(fView != nullptr,);
  390. DISTRHO_SAFE_ASSERT_RETURN(fFrame != nullptr,);
  391. d_stdout("from UI setSize %u %u | %p %p", width, height, fView, fFrame);
  392. #ifdef DISTRHO_OS_MAC
  393. const double scaleFactor = fUI.getScaleFactor();
  394. width /= scaleFactor;
  395. height /= scaleFactor;
  396. #endif
  397. v3_view_rect rect;
  398. std::memset(&rect, 0, sizeof(rect));
  399. rect.right = width;
  400. rect.bottom = height;
  401. v3_cpp_obj(fFrame)->resize_view(fFrame, fView, &rect);
  402. }
  403. static void setSizeCallback(void* ptr, uint width, uint height)
  404. {
  405. ((UIVst3*)ptr)->setSize(width, height);
  406. }
  407. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  408. void sendNote(const uint8_t channel, const uint8_t note, const uint8_t velocity)
  409. {
  410. DISTRHO_SAFE_ASSERT_RETURN(fConnection != nullptr,);
  411. v3_message** const message = createMessage("midi");
  412. DISTRHO_SAFE_ASSERT_RETURN(message != nullptr,);
  413. v3_attribute_list** const attrlist = v3_cpp_obj(message)->get_attributes(message);
  414. DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,);
  415. uint8_t midiData[3];
  416. midiData[0] = (velocity != 0 ? 0x90 : 0x80) | channel;
  417. midiData[1] = note;
  418. midiData[2] = velocity;
  419. v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 1);
  420. v3_cpp_obj(attrlist)->set_binary(attrlist, "data", midiData, sizeof(midiData));
  421. v3_cpp_obj(fConnection)->notify(fConnection, message);
  422. v3_cpp_obj_unref(attrlist);
  423. v3_cpp_obj_unref(message);
  424. }
  425. static void sendNoteCallback(void* ptr, uint8_t channel, uint8_t note, uint8_t velocity)
  426. {
  427. ((UIVst3*)ptr)->sendNote(channel, note, velocity);
  428. }
  429. #endif
  430. #if DISTRHO_PLUGIN_WANT_STATE
  431. void setState(const char* const key, const char* const value)
  432. {
  433. DISTRHO_SAFE_ASSERT_RETURN(fConnection != nullptr,);
  434. v3_message** const message = createMessage("state-set");
  435. DISTRHO_SAFE_ASSERT_RETURN(message != nullptr,);
  436. v3_attribute_list** const attrlist = v3_cpp_obj(message)->get_attributes(message);
  437. DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,);
  438. v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 1);
  439. v3_cpp_obj(attrlist)->set_string(attrlist, "key", ScopedUTF16String(key));
  440. v3_cpp_obj(attrlist)->set_string(attrlist, "value", ScopedUTF16String(value));
  441. v3_cpp_obj(fConnection)->notify(fConnection, message);
  442. v3_cpp_obj_unref(attrlist);
  443. v3_cpp_obj_unref(message);
  444. }
  445. static void setStateCallback(void* ptr, const char* key, const char* value)
  446. {
  447. ((UIVst3*)ptr)->setState(key, value);
  448. }
  449. #endif
  450. };
  451. // --------------------------------------------------------------------------------------------------------------------
  452. /**
  453. * VST3 low-level pointer thingies follow, proceed with care.
  454. */
  455. // --------------------------------------------------------------------------------------------------------------------
  456. // v3_funknown for static and single instances of classes
  457. template<const v3_tuid& v3_interface>
  458. static V3_API v3_result dpf_static__query_interface(void* self, const v3_tuid iid, void** iface)
  459. {
  460. if (v3_tuid_match(iid, v3_funknown_iid))
  461. {
  462. *iface = self;
  463. return V3_OK;
  464. }
  465. if (v3_tuid_match(iid, v3_interface))
  466. {
  467. *iface = self;
  468. return V3_OK;
  469. }
  470. *iface = NULL;
  471. return V3_NO_INTERFACE;
  472. }
  473. static V3_API uint32_t dpf_static__ref(void*) { return 1; }
  474. static V3_API uint32_t dpf_static__unref(void*) { return 0; }
  475. // --------------------------------------------------------------------------------------------------------------------
  476. // v3_funknown with refcounter
  477. template<class T>
  478. static V3_API uint32_t dpf_refcounter__ref(void* self)
  479. {
  480. return ++(*(T**)self)->refcounter;
  481. }
  482. template<class T>
  483. static V3_API uint32_t dpf_refcounter__unref(void* self)
  484. {
  485. return --(*(T**)self)->refcounter;
  486. }
  487. // --------------------------------------------------------------------------------------------------------------------
  488. // dpf_ui_connection_point
  489. struct dpf_ui_connection_point : v3_connection_point_cpp {
  490. std::atomic_int refcounter;
  491. ScopedPointer<UIVst3>& uivst3;
  492. v3_connection_point** other;
  493. dpf_ui_connection_point(ScopedPointer<UIVst3>& v)
  494. : refcounter(1),
  495. uivst3(v),
  496. other(nullptr)
  497. {
  498. static constexpr const v3_tuid interface = V3_ID_COPY(v3_connection_point_iid);
  499. // v3_funknown, single instance
  500. query_interface = dpf_static__query_interface<interface>;
  501. ref = dpf_refcounter__ref<dpf_ui_connection_point>;
  502. unref = dpf_refcounter__unref<dpf_ui_connection_point>;
  503. // v3_connection_point
  504. point.connect = connect;
  505. point.disconnect = disconnect;
  506. point.notify = notify;
  507. }
  508. // ----------------------------------------------------------------------------------------------------------------
  509. // v3_connection_point
  510. static V3_API v3_result connect(void* self, v3_connection_point** other)
  511. {
  512. d_stdout("dpf_ui_connection_point::connect => %p %p", self, other);
  513. dpf_ui_connection_point* const point = *(dpf_ui_connection_point**)self;
  514. DISTRHO_SAFE_ASSERT_RETURN(point != nullptr, V3_NOT_INITIALIZED);
  515. DISTRHO_SAFE_ASSERT_RETURN(point->other == nullptr, V3_INVALID_ARG);
  516. point->other = other;
  517. if (UIVst3* const uivst3 = point->uivst3)
  518. uivst3->connect(other);
  519. return V3_OK;
  520. };
  521. static V3_API v3_result disconnect(void* self, v3_connection_point** other)
  522. {
  523. d_stdout("dpf_ui_connection_point::disconnect => %p %p", self, other);
  524. dpf_ui_connection_point* const point = *(dpf_ui_connection_point**)self;
  525. DISTRHO_SAFE_ASSERT_RETURN(point != nullptr, V3_NOT_INITIALIZED);
  526. DISTRHO_SAFE_ASSERT_RETURN(point->other != nullptr, V3_INVALID_ARG);
  527. point->other = nullptr;
  528. if (UIVst3* const uivst3 = point->uivst3)
  529. uivst3->disconnect();
  530. return V3_OK;
  531. };
  532. static V3_API v3_result notify(void* self, v3_message** message)
  533. {
  534. dpf_ui_connection_point* const point = *(dpf_ui_connection_point**)self;
  535. DISTRHO_SAFE_ASSERT_RETURN(point != nullptr, V3_NOT_INITIALIZED);
  536. UIVst3* const uivst3 = point->uivst3;
  537. DISTRHO_SAFE_ASSERT_RETURN(uivst3 != nullptr, V3_NOT_INITIALIZED);
  538. return uivst3->notify(message);
  539. }
  540. };
  541. // --------------------------------------------------------------------------------------------------------------------
  542. // dpf_plugin_view_content_scale
  543. struct dpf_plugin_view_content_scale : v3_plugin_view_content_scale_cpp {
  544. std::atomic_int refcounter;
  545. ScopedPointer<UIVst3>& uivst3;
  546. // cached values
  547. float scaleFactor;
  548. dpf_plugin_view_content_scale(ScopedPointer<UIVst3>& v)
  549. : refcounter(1),
  550. uivst3(v),
  551. scaleFactor(0.0f)
  552. {
  553. static constexpr const v3_tuid interface = V3_ID_COPY(v3_plugin_view_content_scale_iid);
  554. // v3_funknown, single instance
  555. query_interface = dpf_static__query_interface<interface>;
  556. ref = dpf_refcounter__ref<dpf_plugin_view_content_scale>;
  557. unref = dpf_refcounter__unref<dpf_plugin_view_content_scale>;
  558. // v3_plugin_view_content_scale
  559. scale.set_content_scale_factor = set_content_scale_factor;
  560. }
  561. // ----------------------------------------------------------------------------------------------------------------
  562. // v3_plugin_view_content_scale
  563. static V3_API v3_result set_content_scale_factor(void* self, float factor)
  564. {
  565. d_stdout("dpf_plugin_view::set_content_scale_factor => %p %f", self, factor);
  566. dpf_plugin_view_content_scale* const scale = *(dpf_plugin_view_content_scale**)self;
  567. DISTRHO_SAFE_ASSERT_RETURN(scale != nullptr, V3_NOT_INITIALIZED);
  568. scale->scaleFactor = factor;
  569. if (UIVst3* const uivst3 = scale->uivst3)
  570. return uivst3->setContentScaleFactor(factor);
  571. return V3_NOT_INITIALIZED;
  572. }
  573. };
  574. #ifdef DPF_VST3_USING_HOST_RUN_LOOP
  575. // --------------------------------------------------------------------------------------------------------------------
  576. // dpf_timer_handler
  577. struct dpf_timer_handler : v3_timer_handler_cpp {
  578. ScopedPointer<UIVst3>& uivst3;
  579. dpf_timer_handler(ScopedPointer<UIVst3>& v)
  580. : uivst3(v)
  581. {
  582. static constexpr const v3_tuid interface = V3_ID_COPY(v3_timer_handler_iid);
  583. // v3_funknown, single instance
  584. query_interface = dpf_static__query_interface<interface>;
  585. ref = dpf_static__ref;
  586. unref = dpf_static__unref;
  587. // v3_timer_handler
  588. handler.on_timer = on_timer;
  589. }
  590. // ----------------------------------------------------------------------------------------------------------------
  591. // v3_timer_handler
  592. static V3_API void on_timer(void* self)
  593. {
  594. dpf_timer_handler* const handler = *(dpf_timer_handler**)self;
  595. DISTRHO_SAFE_ASSERT_RETURN(handler != nullptr,);
  596. handler->uivst3->onTimer();
  597. }
  598. };
  599. #endif
  600. // --------------------------------------------------------------------------------------------------------------------
  601. // dpf_plugin_view
  602. static const char* const kSupportedPlatforms[] = {
  603. #ifdef _WIN32
  604. V3_VIEW_PLATFORM_TYPE_HWND,
  605. #elif defined(__APPLE__)
  606. V3_VIEW_PLATFORM_TYPE_NSVIEW,
  607. #else
  608. V3_VIEW_PLATFORM_TYPE_X11,
  609. #endif
  610. };
  611. struct dpf_plugin_view : v3_plugin_view_cpp {
  612. std::atomic_int refcounter;
  613. dpf_plugin_view** const self;
  614. ScopedPointer<dpf_ui_connection_point> connection;
  615. ScopedPointer<dpf_plugin_view_content_scale> scale;
  616. #ifdef DPF_VST3_USING_HOST_RUN_LOOP
  617. ScopedPointer<dpf_timer_handler> timer;
  618. #endif
  619. ScopedPointer<UIVst3> uivst3;
  620. // cached values
  621. v3_host_application** const host;
  622. void* const instancePointer;
  623. double sampleRate;
  624. v3_plugin_frame** frame;
  625. dpf_plugin_view(dpf_plugin_view** const s, v3_host_application** const h, void* const instance, const double sr)
  626. : refcounter(1),
  627. self(s),
  628. host(h),
  629. instancePointer(instance),
  630. sampleRate(sr),
  631. frame(nullptr)
  632. {
  633. // v3_funknown, everything custom
  634. query_interface = query_interface_view;
  635. ref = ref_view;
  636. unref = unref_view;
  637. // v3_plugin_view
  638. view.is_platform_type_supported = is_platform_type_supported;
  639. view.attached = attached;
  640. view.removed = removed;
  641. view.on_wheel = on_wheel;
  642. view.on_key_down = on_key_down;
  643. view.on_key_up = on_key_up;
  644. view.get_size = get_size;
  645. view.on_size = on_size;
  646. view.on_focus = on_focus;
  647. view.set_frame = set_frame;
  648. view.can_resize = can_resize;
  649. view.check_size_constraint = check_size_constraint;
  650. }
  651. // ----------------------------------------------------------------------------------------------------------------
  652. // v3_funknown
  653. static V3_API v3_result query_interface_view(void* self, const v3_tuid iid, void** iface)
  654. {
  655. d_stdout("dpf_plugin_view::query_interface => %p %s %p", self, tuid2str(iid), iface);
  656. *iface = NULL;
  657. DISTRHO_SAFE_ASSERT_RETURN(self != nullptr, V3_NO_INTERFACE);
  658. if (v3_tuid_match(iid, v3_funknown_iid))
  659. {
  660. *iface = self;
  661. return V3_OK;
  662. }
  663. if (v3_tuid_match(iid, v3_plugin_view_iid))
  664. {
  665. *iface = self;
  666. return V3_OK;
  667. }
  668. dpf_plugin_view* const view = *(dpf_plugin_view**)self;
  669. DISTRHO_SAFE_ASSERT_RETURN(view != nullptr, V3_NO_INTERFACE);
  670. if (v3_tuid_match(v3_connection_point_iid, iid))
  671. {
  672. if (view->connection == nullptr)
  673. view->connection = new dpf_ui_connection_point(view->uivst3);
  674. *iface = &view->connection;
  675. return V3_OK;
  676. }
  677. if (v3_tuid_match(v3_plugin_view_content_scale_iid, iid))
  678. {
  679. if (view->scale == nullptr)
  680. view->scale = new dpf_plugin_view_content_scale(view->uivst3);
  681. *iface = &view->scale;
  682. return V3_OK;
  683. }
  684. return V3_NO_INTERFACE;
  685. }
  686. static V3_API uint32_t ref_view(void* self)
  687. {
  688. return ++(*(dpf_plugin_view**)self)->refcounter;
  689. }
  690. static V3_API uint32_t unref_view(void* self)
  691. {
  692. dpf_plugin_view** const viewptr = (dpf_plugin_view**)self;
  693. dpf_plugin_view* const view = *viewptr;
  694. if (const int refcount = --view->refcounter)
  695. {
  696. d_stdout("dpf_plugin_view::unref => %p | refcount %i", self, refcount);
  697. return refcount;
  698. }
  699. d_stdout("dpf_plugin_view::unref => %p | refcount is zero, deleting everything now!", self);
  700. DISTRHO_SAFE_ASSERT_RETURN(viewptr == view->self, V3_INTERNAL_ERR);
  701. if (view->connection != nullptr && view->connection->other)
  702. v3_cpp_obj(view->connection->other)->disconnect(view->connection->other,
  703. (v3_connection_point**)&view->connection);
  704. if (dpf_ui_connection_point* const conn = view->connection)
  705. {
  706. if (const int refcount = conn->refcounter)
  707. {
  708. d_stderr("DPF warning: asked to delete view while connection point still active (refcount %d)", refcount);
  709. return V3_INVALID_ARG;
  710. }
  711. }
  712. if (dpf_plugin_view_content_scale* const scale = view->scale)
  713. {
  714. if (const int refcount = scale->refcounter)
  715. {
  716. d_stderr("DPF warning: asked to delete view while content scale still active (refcount %d)", refcount);
  717. return V3_INVALID_ARG;
  718. }
  719. }
  720. delete view;
  721. delete viewptr;
  722. return 0;
  723. }
  724. // ----------------------------------------------------------------------------------------------------------------
  725. // v3_plugin_view
  726. static V3_API v3_result is_platform_type_supported(void* self, const char* platform_type)
  727. {
  728. d_stdout("dpf_plugin_view::is_platform_type_supported => %p %s", self, platform_type);
  729. dpf_plugin_view* const view = *(dpf_plugin_view**)self;
  730. DISTRHO_SAFE_ASSERT_RETURN(view != nullptr, V3_NOT_INITIALIZED);
  731. for (size_t i=0; i<ARRAY_SIZE(kSupportedPlatforms); ++i)
  732. {
  733. if (std::strcmp(kSupportedPlatforms[i], platform_type) == 0)
  734. return V3_OK;
  735. }
  736. return V3_NOT_IMPLEMENTED;
  737. }
  738. static V3_API v3_result attached(void* self, void* parent, const char* platform_type)
  739. {
  740. d_stdout("dpf_plugin_view::attached => %p %p %s", self, parent, platform_type);
  741. dpf_plugin_view* const view = *(dpf_plugin_view**)self;
  742. DISTRHO_SAFE_ASSERT_RETURN(view != nullptr, V3_NOT_INITIALIZED);
  743. DISTRHO_SAFE_ASSERT_RETURN(view->uivst3 == nullptr, V3_INVALID_ARG);
  744. for (size_t i=0; i<ARRAY_SIZE(kSupportedPlatforms); ++i)
  745. {
  746. if (std::strcmp(kSupportedPlatforms[i], platform_type) == 0)
  747. {
  748. #ifdef DPF_VST3_USING_HOST_RUN_LOOP
  749. // find host run loop to plug ourselves into (required on some systems)
  750. DISTRHO_SAFE_ASSERT_RETURN(view->frame != nullptr, V3_INVALID_ARG);
  751. v3_run_loop** runloop = nullptr;
  752. v3_cpp_obj_query_interface(view->frame, v3_run_loop_iid, &runloop);
  753. DISTRHO_SAFE_ASSERT_RETURN(runloop != nullptr, V3_INVALID_ARG);
  754. #endif
  755. const float scaleFactor = view->scale != nullptr ? view->scale->scaleFactor : 0.0f;
  756. view->uivst3 = new UIVst3((v3_plugin_view**)view->self,
  757. view->host,
  758. (uintptr_t)parent,
  759. scaleFactor,
  760. view->sampleRate,
  761. view->instancePointer);
  762. if (dpf_ui_connection_point* const point = view->connection)
  763. if (point->other != nullptr)
  764. view->uivst3->connect(point->other);
  765. view->uivst3->setFrame(view->frame);
  766. #ifdef DPF_VST3_USING_HOST_RUN_LOOP
  767. // register a timer host run loop stuff
  768. view->timer = new dpf_timer_handler(view->uivst3);
  769. v3_cpp_obj(runloop)->register_timer(runloop,
  770. (v3_timer_handler**)&view->timer,
  771. DPF_VST3_TIMER_INTERVAL);
  772. #endif
  773. return V3_OK;
  774. }
  775. }
  776. return V3_NOT_IMPLEMENTED;
  777. }
  778. static V3_API v3_result removed(void* self)
  779. {
  780. d_stdout("dpf_plugin_view::removed => %p", self);
  781. dpf_plugin_view* const view = *(dpf_plugin_view**)self;
  782. DISTRHO_SAFE_ASSERT_RETURN(view != nullptr, V3_NOT_INITIALIZED);
  783. DISTRHO_SAFE_ASSERT_RETURN(view->uivst3 != nullptr, V3_INVALID_ARG);
  784. #ifdef DPF_VST3_USING_HOST_RUN_LOOP
  785. // unregister our timer as needed
  786. if (view->timer != nullptr)
  787. {
  788. v3_run_loop** runloop = nullptr;
  789. v3_cpp_obj_query_interface(view->host, v3_run_loop_iid, &runloop);
  790. if (runloop != nullptr)
  791. v3_cpp_obj(runloop)->unregister_timer(runloop, (v3_timer_handler**)&view->timer);
  792. view->timer = nullptr;
  793. }
  794. #endif
  795. view->uivst3 = nullptr;
  796. return V3_OK;
  797. }
  798. static V3_API v3_result on_wheel(void* self, float distance)
  799. {
  800. d_stdout("dpf_plugin_view::on_wheel => %p %f", self, distance);
  801. dpf_plugin_view* const view = *(dpf_plugin_view**)self;
  802. DISTRHO_SAFE_ASSERT_RETURN(view != nullptr, V3_NOT_INITIALIZED);
  803. UIVst3* const uivst3 = view->uivst3;
  804. DISTRHO_SAFE_ASSERT_RETURN(uivst3 != nullptr, V3_NOT_INITIALIZED);
  805. return uivst3->onWheel(distance);
  806. }
  807. static V3_API v3_result on_key_down(void* self, int16_t key_char, int16_t key_code, int16_t modifiers)
  808. {
  809. d_stdout("dpf_plugin_view::on_key_down => %p %i %i %i", self, key_char, key_code, modifiers);
  810. dpf_plugin_view* const view = *(dpf_plugin_view**)self;
  811. DISTRHO_SAFE_ASSERT_RETURN(view != nullptr, V3_NOT_INITIALIZED);
  812. UIVst3* const uivst3 = view->uivst3;
  813. DISTRHO_SAFE_ASSERT_RETURN(uivst3 != nullptr, V3_NOT_INITIALIZED);
  814. return uivst3->onKeyDown(key_char, key_code, modifiers);
  815. }
  816. static V3_API v3_result on_key_up(void* self, int16_t key_char, int16_t key_code, int16_t modifiers)
  817. {
  818. d_stdout("dpf_plugin_view::on_key_up => %p %i %i %i", self, key_char, key_code, modifiers);
  819. dpf_plugin_view* const view = *(dpf_plugin_view**)self;
  820. DISTRHO_SAFE_ASSERT_RETURN(view != nullptr, V3_NOT_INITIALIZED);
  821. UIVst3* const uivst3 = view->uivst3;
  822. DISTRHO_SAFE_ASSERT_RETURN(uivst3 != nullptr, V3_NOT_INITIALIZED);
  823. return uivst3->onKeyUp(key_char, key_code, modifiers);
  824. }
  825. static V3_API v3_result get_size(void* self, v3_view_rect* rect)
  826. {
  827. d_stdout("dpf_plugin_view::get_size => %p", self);
  828. dpf_plugin_view* const view = *(dpf_plugin_view**)self;
  829. DISTRHO_SAFE_ASSERT_RETURN(view != nullptr, V3_NOT_INITIALIZED);
  830. if (UIVst3* const uivst3 = view->uivst3)
  831. return uivst3->getSize(rect);
  832. // special case: allow UI to not be attached yet, as a way to get size before window creation
  833. const float scaleFactor = view->scale != nullptr ? view->scale->scaleFactor : 0.0f;
  834. UIExporter tmpUI(nullptr, 0, view->sampleRate,
  835. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  836. view->instancePointer, scaleFactor);
  837. rect->right = tmpUI.getWidth();
  838. rect->bottom = tmpUI.getHeight();
  839. return V3_OK;
  840. }
  841. static V3_API v3_result on_size(void* self, v3_view_rect* rect)
  842. {
  843. d_stdout("dpf_plugin_view::on_size => %p %p", self, rect);
  844. dpf_plugin_view* const view = *(dpf_plugin_view**)self;
  845. DISTRHO_SAFE_ASSERT_RETURN(view != nullptr, V3_NOT_INITIALIZED);
  846. UIVst3* const uivst3 = view->uivst3;
  847. DISTRHO_SAFE_ASSERT_RETURN(uivst3 != nullptr, V3_NOT_INITIALIZED);
  848. return uivst3->onSize(rect);
  849. }
  850. static V3_API v3_result on_focus(void* self, v3_bool state)
  851. {
  852. d_stdout("dpf_plugin_view::on_focus => %p %u", self, state);
  853. dpf_plugin_view* const view = *(dpf_plugin_view**)self;
  854. DISTRHO_SAFE_ASSERT_RETURN(view != nullptr, V3_NOT_INITIALIZED);
  855. UIVst3* const uivst3 = view->uivst3;
  856. DISTRHO_SAFE_ASSERT_RETURN(uivst3 != nullptr, V3_NOT_INITIALIZED);
  857. return uivst3->onFocus(state);
  858. }
  859. static V3_API v3_result set_frame(void* self, v3_plugin_frame** frame)
  860. {
  861. d_stdout("dpf_plugin_view::set_frame => %p %p", self, frame);
  862. dpf_plugin_view* const view = *(dpf_plugin_view**)self;
  863. DISTRHO_SAFE_ASSERT_RETURN(view != nullptr, V3_NOT_INITIALIZED);
  864. view->frame = frame;
  865. if (UIVst3* const uivst3 = view->uivst3)
  866. return uivst3->setFrame(frame);
  867. return V3_NOT_INITIALIZED;
  868. }
  869. static V3_API v3_result can_resize(void* self)
  870. {
  871. d_stdout("dpf_plugin_view::can_resize => %p", self);
  872. // #if DISTRHO_UI_USER_RESIZABLE
  873. // return V3_OK;
  874. // #else
  875. return V3_NOT_IMPLEMENTED;
  876. // #endif
  877. }
  878. static V3_API v3_result check_size_constraint(void* self, v3_view_rect* rect)
  879. {
  880. d_stdout("dpf_plugin_view::check_size_constraint => %p %p", self, rect);
  881. dpf_plugin_view* const view = *(dpf_plugin_view**)self;
  882. DISTRHO_SAFE_ASSERT_RETURN(view != nullptr, V3_NOT_INITIALIZED);
  883. UIVst3* const uivst3 = view->uivst3;
  884. DISTRHO_SAFE_ASSERT_RETURN(uivst3 != nullptr, V3_NOT_INITIALIZED);
  885. return uivst3->checkSizeConstraint(rect);
  886. }
  887. };
  888. // --------------------------------------------------------------------------------------------------------------------
  889. // dpf_plugin_view_create (called from plugin side)
  890. v3_plugin_view** dpf_plugin_view_create(v3_host_application** host, void* instancePointer, double sampleRate);
  891. v3_plugin_view** dpf_plugin_view_create(v3_host_application** const host,
  892. void* const instancePointer,
  893. const double sampleRate)
  894. {
  895. dpf_plugin_view** const viewptr = new dpf_plugin_view*;
  896. *viewptr = new dpf_plugin_view(viewptr, host, instancePointer, sampleRate);
  897. return (v3_plugin_view**)static_cast<void*>(viewptr);
  898. }
  899. // --------------------------------------------------------------------------------------------------------------------
  900. END_NAMESPACE_DISTRHO