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.

ui_launcher.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Carla Native Plugin UI launcher
  3. * Copyright (C) 2018-2022 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. #include "dgl/OpenGL.hpp"
  18. #include "dgl/src/pugl.hpp"
  19. #include "dgl/src/WindowPrivateData.hpp"
  20. #include "CarlaNative.h"
  21. #include "ui_launcher_res.hpp"
  22. #include "CarlaDefines.h"
  23. // --------------------------------------------------------------------------------------------------------------------
  24. START_NAMESPACE_DISTRHO
  25. class PluginApplication : public DGL_NAMESPACE::Application
  26. {
  27. public:
  28. explicit PluginApplication()
  29. : Application(false)
  30. {
  31. setClassName("CarlaPluginWrapper");
  32. }
  33. };
  34. class PluginWindow : public DGL_NAMESPACE::Window
  35. {
  36. public:
  37. explicit PluginWindow(PluginApplication& app, const uintptr_t winId)
  38. : Window(app, winId, ui_launcher_res::carla_uiWidth, ui_launcher_res::carla_uiHeight, 0.0, false, false, false)
  39. {
  40. // this is called just before creating UI, ensuring proper context to it
  41. if (pData->view != nullptr && pData->initPost())
  42. puglBackendEnter(pData->view);
  43. }
  44. ~PluginWindow()
  45. {
  46. if (pData->view != nullptr)
  47. puglBackendLeave(pData->view);
  48. }
  49. // called right before deleting UI, ensuring correct context
  50. void enterContextForDeletion()
  51. {
  52. if (pData->view != nullptr)
  53. puglBackendEnter(pData->view);
  54. }
  55. // called after creating UI, restoring proper context
  56. void leaveContextAfterCreation()
  57. {
  58. if (pData->view != nullptr)
  59. puglBackendLeave(pData->view);
  60. }
  61. };
  62. END_NAMESPACE_DISTRHO
  63. // --------------------------------------------------------------------------------------------------------------------
  64. START_NAMESPACE_DGL
  65. class CarlaButtonWidget : public TopLevelWidget,
  66. private OpenGLImageButton::Callback
  67. {
  68. public:
  69. explicit CarlaButtonWidget(PluginWindow& parent, const NativePluginDescriptor* const d, const NativePluginHandle h)
  70. : TopLevelWidget(parent),
  71. startButtonImage(ui_launcher_res::carla_uiData,
  72. ui_launcher_res::carla_uiWidth,
  73. ui_launcher_res::carla_uiHeight,
  74. kImageFormatBGR),
  75. startButton(this, startButtonImage),
  76. descriptor(d),
  77. handle(h),
  78. pluginWindow(parent)
  79. {
  80. startButton.setCallback(this);
  81. pluginWindow.leaveContextAfterCreation();
  82. }
  83. ~CarlaButtonWidget() override
  84. {
  85. pluginWindow.enterContextForDeletion();
  86. }
  87. protected:
  88. void onDisplay() override
  89. {
  90. }
  91. void imageButtonClicked(OpenGLImageButton* imageButton, int) override
  92. {
  93. if (imageButton != &startButton)
  94. return;
  95. if (descriptor->ui_show != nullptr)
  96. descriptor->ui_show(handle, true);
  97. }
  98. private:
  99. OpenGLImage startButtonImage;
  100. OpenGLImageButton startButton;
  101. const NativePluginDescriptor* const descriptor;
  102. const NativePluginHandle handle;
  103. PluginWindow& pluginWindow;
  104. CARLA_DECLARE_NON_COPYABLE(CarlaButtonWidget);
  105. };
  106. END_NAMESPACE_DGL
  107. // --------------------------------------------------------------------------------------------------------------------
  108. USE_NAMESPACE_DGL
  109. struct CarlaUILauncher {
  110. PluginApplication app;
  111. PluginWindow window;
  112. CarlaButtonWidget widget;
  113. CarlaUILauncher(const uintptr_t winId, const NativePluginDescriptor* const d, const NativePluginHandle h)
  114. : app(),
  115. window(app, winId),
  116. widget(window, d, h) {}
  117. };
  118. CarlaUILauncher* createUILauncher(const uintptr_t winId,
  119. const NativePluginDescriptor* const d,
  120. const NativePluginHandle h)
  121. {
  122. return new CarlaUILauncher(winId, d, h);
  123. }
  124. void idleUILauncher(CarlaUILauncher* const ui)
  125. {
  126. ui->app.idle();
  127. }
  128. void destoryUILauncher(CarlaUILauncher* const ui)
  129. {
  130. delete ui;
  131. }
  132. // --------------------------------------------------------------------------------------------------------------------