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.

361 lines
13KB

  1. /*
  2. * Juce Plugin Window Helper
  3. * Copyright (C) 2013-2021 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 JUCE_PLUGIN_WINDOW_HPP_INCLUDED
  18. #define JUCE_PLUGIN_WINDOW_HPP_INCLUDED
  19. #include "CarlaJuceUtils.hpp"
  20. #include "CarlaVstUtils.hpp"
  21. #include "AppConfig.h"
  22. #include "juce_gui_basics/juce_gui_basics.h"
  23. #if defined(CARLA_OS_LINUX) && defined(HAVE_X11)
  24. # include <X11/Xlib.h>
  25. #elif defined(CARLA_OS_MAC)
  26. # define Component CocoaComponent
  27. # define MemoryBlock CocoaMemoryBlock
  28. # define Point CocoaPoint
  29. # import <Cocoa/Cocoa.h>
  30. # undef Component
  31. # undef MemoryBlock
  32. # undef Point
  33. #endif
  34. // -----------------------------------------------------------------------
  35. namespace juce {
  36. class JucePluginWindow : public DialogWindow
  37. {
  38. public:
  39. JucePluginWindow(const uintptr_t parentId, const bool isStandalone, AEffect* const vstEffect)
  40. : DialogWindow("JucePluginWindow", Colour(50, 50, 200), true, false),
  41. fIsStandalone(isStandalone),
  42. fClosed(false),
  43. fShown(false),
  44. fTransientId(parentId),
  45. fLastKeyIndex(0),
  46. fLastKeyValue(0),
  47. fLastModifiers(),
  48. fVstEffect(vstEffect)
  49. {
  50. setVisible(false);
  51. setOpaque(true);
  52. setResizable(false, false);
  53. setUsingNativeTitleBar(true);
  54. }
  55. void show(Component* const comp)
  56. {
  57. fClosed = false;
  58. fShown = true;
  59. centreWithSize(comp->getWidth(), comp->getHeight());
  60. setContentNonOwned(comp, true);
  61. if (! isOnDesktop())
  62. addToDesktop();
  63. #ifndef CARLA_OS_LINUX
  64. setAlwaysOnTop(true);
  65. #endif
  66. setTransient();
  67. setVisible(true);
  68. toFront(true);
  69. #ifndef CARLA_OS_LINUX
  70. postCommandMessage(0);
  71. #endif
  72. }
  73. bool keyPressed (const KeyPress& key) override
  74. {
  75. if (DialogWindow::keyPressed (key))
  76. return true;
  77. if (fVstEffect != nullptr)
  78. {
  79. const int juceKeyCode = key.getKeyCode();
  80. int vstKeyIndex = 0;
  81. int vstKeyValue = 0;
  82. do {
  83. /**/ if (juceKeyCode == KeyPress::spaceKey) vstKeyValue = 7;
  84. else if (juceKeyCode == KeyPress::escapeKey) vstKeyValue = 6;
  85. else if (juceKeyCode == KeyPress::returnKey) vstKeyValue = 19; // NOTE: 4 is '\r' while 19 is '\n'
  86. else if (juceKeyCode == KeyPress::tabKey) vstKeyValue = 2;
  87. else if (juceKeyCode == KeyPress::deleteKey) vstKeyValue = 22;
  88. else if (juceKeyCode == KeyPress::backspaceKey) vstKeyValue = 1;
  89. else if (juceKeyCode == KeyPress::insertKey) vstKeyValue = 21;
  90. else if (juceKeyCode == KeyPress::upKey) vstKeyValue = 12;
  91. else if (juceKeyCode == KeyPress::downKey) vstKeyValue = 14;
  92. else if (juceKeyCode == KeyPress::leftKey) vstKeyValue = 11;
  93. else if (juceKeyCode == KeyPress::rightKey) vstKeyValue = 13;
  94. else if (juceKeyCode == KeyPress::pageUpKey) vstKeyValue = 15;
  95. else if (juceKeyCode == KeyPress::pageDownKey) vstKeyValue = 16;
  96. else if (juceKeyCode == KeyPress::homeKey) vstKeyValue = 10;
  97. else if (juceKeyCode == KeyPress::endKey) vstKeyValue = 9;
  98. else if (juceKeyCode == KeyPress::F1Key) vstKeyValue = 40;
  99. else if (juceKeyCode == KeyPress::F2Key) vstKeyValue = 41;
  100. else if (juceKeyCode == KeyPress::F3Key) vstKeyValue = 42;
  101. else if (juceKeyCode == KeyPress::F4Key) vstKeyValue = 43;
  102. else if (juceKeyCode == KeyPress::F5Key) vstKeyValue = 44;
  103. else if (juceKeyCode == KeyPress::F6Key) vstKeyValue = 45;
  104. else if (juceKeyCode == KeyPress::F7Key) vstKeyValue = 46;
  105. else if (juceKeyCode == KeyPress::F8Key) vstKeyValue = 47;
  106. else if (juceKeyCode == KeyPress::F9Key) vstKeyValue = 48;
  107. else if (juceKeyCode == KeyPress::F10Key) vstKeyValue = 49;
  108. else if (juceKeyCode == KeyPress::F11Key) vstKeyValue = 50;
  109. else if (juceKeyCode == KeyPress::F12Key) vstKeyValue = 51;
  110. else if (juceKeyCode == KeyPress::F13Key) break;
  111. else if (juceKeyCode == KeyPress::F14Key) break;
  112. else if (juceKeyCode == KeyPress::F15Key) break;
  113. else if (juceKeyCode == KeyPress::F16Key) break;
  114. else if (juceKeyCode == KeyPress::F17Key) break;
  115. else if (juceKeyCode == KeyPress::F18Key) break;
  116. else if (juceKeyCode == KeyPress::F19Key) break;
  117. else if (juceKeyCode == KeyPress::F20Key) break;
  118. else if (juceKeyCode == KeyPress::F21Key) break;
  119. else if (juceKeyCode == KeyPress::F22Key) break;
  120. else if (juceKeyCode == KeyPress::F23Key) break;
  121. else if (juceKeyCode == KeyPress::F24Key) break;
  122. else if (juceKeyCode == KeyPress::F25Key) break;
  123. else if (juceKeyCode == KeyPress::F26Key) break;
  124. else if (juceKeyCode == KeyPress::F27Key) break;
  125. else if (juceKeyCode == KeyPress::F28Key) break;
  126. else if (juceKeyCode == KeyPress::F29Key) break;
  127. else if (juceKeyCode == KeyPress::F30Key) break;
  128. else if (juceKeyCode == KeyPress::F31Key) break;
  129. else if (juceKeyCode == KeyPress::F32Key) break;
  130. else if (juceKeyCode == KeyPress::F33Key) break;
  131. else if (juceKeyCode == KeyPress::F34Key) break;
  132. else if (juceKeyCode == KeyPress::F35Key) break;
  133. else if (juceKeyCode == KeyPress::numberPad0) vstKeyValue = 24;
  134. else if (juceKeyCode == KeyPress::numberPad1) vstKeyValue = 25;
  135. else if (juceKeyCode == KeyPress::numberPad2) vstKeyValue = 26;
  136. else if (juceKeyCode == KeyPress::numberPad3) vstKeyValue = 27;
  137. else if (juceKeyCode == KeyPress::numberPad4) vstKeyValue = 28;
  138. else if (juceKeyCode == KeyPress::numberPad5) vstKeyValue = 29;
  139. else if (juceKeyCode == KeyPress::numberPad6) vstKeyValue = 30;
  140. else if (juceKeyCode == KeyPress::numberPad7) vstKeyValue = 31;
  141. else if (juceKeyCode == KeyPress::numberPad8) vstKeyValue = 32;
  142. else if (juceKeyCode == KeyPress::numberPad9) vstKeyValue = 33;
  143. else if (juceKeyCode == KeyPress::numberPadAdd) vstKeyValue = 35;
  144. else if (juceKeyCode == KeyPress::numberPadSubtract) vstKeyValue = 37;
  145. else if (juceKeyCode == KeyPress::numberPadMultiply) vstKeyValue = 34;
  146. else if (juceKeyCode == KeyPress::numberPadDivide) vstKeyValue = 39;
  147. else if (juceKeyCode == KeyPress::numberPadSeparator) vstKeyValue = 36;
  148. else if (juceKeyCode == KeyPress::numberPadDecimalPoint) vstKeyValue = 38;
  149. else if (juceKeyCode == KeyPress::numberPadEquals) vstKeyValue = 57;
  150. else if (juceKeyCode == KeyPress::numberPadDelete) break;
  151. else if (juceKeyCode == KeyPress::playKey) break;
  152. else if (juceKeyCode == KeyPress::stopKey) break;
  153. else if (juceKeyCode == KeyPress::fastForwardKey) break;
  154. else if (juceKeyCode == KeyPress::rewindKey) break;
  155. else vstKeyIndex = juceKeyCode;
  156. } while (false);
  157. fLastKeyIndex = vstKeyIndex;
  158. fLastKeyValue = vstKeyValue;
  159. return fVstEffect->dispatcher (fVstEffect, effEditKeyDown,
  160. vstKeyIndex, vstKeyValue,
  161. nullptr, 0.0f) != 0;
  162. }
  163. if (Component* const comp = getContentComponent())
  164. return comp->keyPressed (key);
  165. return false;
  166. }
  167. bool keyStateChanged (bool isKeyDown) override
  168. {
  169. if (DialogWindow::keyStateChanged (isKeyDown))
  170. return true;
  171. if (fVstEffect != nullptr && (fLastKeyIndex != 0 || fLastKeyValue != 0) && ! isKeyDown)
  172. {
  173. const int vstKeyIndex = fLastKeyIndex;
  174. const int vstKeyValue = fLastKeyValue;
  175. fLastKeyIndex = fLastKeyValue = 0;
  176. return fVstEffect->dispatcher (fVstEffect, effEditKeyUp,
  177. vstKeyIndex, vstKeyValue,
  178. nullptr, 0.0f) != 0;
  179. }
  180. if (Component* const comp = getContentComponent())
  181. return comp->keyStateChanged (isKeyDown);
  182. return false;
  183. }
  184. void modifierKeysChanged (const ModifierKeys& modifiers) override
  185. {
  186. DialogWindow::modifierKeysChanged (modifiers);
  187. if (fVstEffect != nullptr)
  188. {
  189. const int oldRawFlags = fLastModifiers.getRawFlags();
  190. const int newRawFlags = modifiers.getRawFlags();
  191. if ((oldRawFlags & ModifierKeys::shiftModifier) != (newRawFlags & ModifierKeys::shiftModifier))
  192. {
  193. fVstEffect->dispatcher (fVstEffect, (newRawFlags & ModifierKeys::shiftModifier) ? effEditKeyDown : effEditKeyUp, 0,
  194. 54, nullptr, 0.0f);
  195. }
  196. if ((oldRawFlags & ModifierKeys::ctrlModifier) != (newRawFlags & ModifierKeys::ctrlModifier))
  197. {
  198. fVstEffect->dispatcher (fVstEffect, (newRawFlags & ModifierKeys::ctrlModifier) ? effEditKeyDown : effEditKeyUp, 0,
  199. 55, nullptr, 0.0f);
  200. }
  201. if ((oldRawFlags & ModifierKeys::altModifier) != (newRawFlags & ModifierKeys::altModifier))
  202. {
  203. fVstEffect->dispatcher (fVstEffect, (newRawFlags & ModifierKeys::altModifier) ? effEditKeyDown : effEditKeyUp, 0,
  204. 56, nullptr, 0.0f);
  205. }
  206. if ((oldRawFlags & ModifierKeys::popupMenuClickModifier) != (newRawFlags & ModifierKeys::popupMenuClickModifier))
  207. {
  208. fVstEffect->dispatcher (fVstEffect, (newRawFlags & ModifierKeys::popupMenuClickModifier) ? effEditKeyDown : effEditKeyUp, 0,
  209. 58, nullptr, 0.0f);
  210. }
  211. }
  212. fLastModifiers = modifiers;
  213. }
  214. void hide()
  215. {
  216. setVisible(false);
  217. if (isOnDesktop())
  218. removeFromDesktop();
  219. clearContentComponent();
  220. }
  221. bool wasClosedByUser() const noexcept
  222. {
  223. return fClosed;
  224. }
  225. protected:
  226. void closeButtonPressed() override
  227. {
  228. fClosed = true;
  229. }
  230. bool escapeKeyPressed() override
  231. {
  232. fClosed = true;
  233. return true;
  234. }
  235. int getDesktopWindowStyleFlags() const override
  236. {
  237. int wflags = 0;
  238. wflags |= ComponentPeer::windowHasCloseButton;
  239. wflags |= ComponentPeer::windowHasDropShadow;
  240. wflags |= ComponentPeer::windowHasTitleBar;
  241. if (fIsStandalone)
  242. wflags |= ComponentPeer::windowAppearsOnTaskbar;
  243. return wflags;
  244. }
  245. #ifndef CARLA_OS_LINUX
  246. void handleCommandMessage(const int comamndId) override
  247. {
  248. CARLA_SAFE_ASSERT_RETURN(comamndId == 0,);
  249. if (fShown)
  250. {
  251. fShown = false;
  252. setAlwaysOnTop(false);
  253. }
  254. }
  255. #endif
  256. private:
  257. const bool fIsStandalone;
  258. volatile bool fClosed;
  259. bool fShown;
  260. const uintptr_t fTransientId;
  261. int fLastKeyIndex, fLastKeyValue;
  262. ModifierKeys fLastModifiers;
  263. AEffect* const fVstEffect;
  264. void setTransient()
  265. {
  266. if (fTransientId == 0)
  267. return;
  268. #if defined(CARLA_OS_LINUX) && defined(HAVE_X11)
  269. Display* const display = XWindowSystem::getInstance()->getDisplay();
  270. CARLA_SAFE_ASSERT_RETURN(display != nullptr,);
  271. ::Window window = (::Window)getWindowHandle();
  272. CARLA_SAFE_ASSERT_RETURN(window != 0,);
  273. XSetTransientForHint(display, window, static_cast<::Window>(fTransientId));
  274. #endif
  275. #ifdef CARLA_OS_MAC
  276. NSView* const view = (NSView*)getWindowHandle();
  277. CARLA_SAFE_ASSERT_RETURN(view != nullptr,);
  278. NSWindow* const window = [view window];
  279. CARLA_SAFE_ASSERT_RETURN(window != nullptr,);
  280. NSWindow* const parentWindow = [NSApp windowWithWindowNumber:fTransientId];
  281. CARLA_SAFE_ASSERT_RETURN(parentWindow != nullptr,);
  282. [parentWindow addChildWindow:window
  283. ordered:NSWindowAbove];
  284. #endif
  285. #ifdef CARLA_OS_WIN
  286. const HWND window = (HWND)getWindowHandle();
  287. CARLA_SAFE_ASSERT_RETURN(window != nullptr,);
  288. SetWindowLongPtr(window, GWLP_HWNDPARENT, static_cast<LONG_PTR>(fTransientId));
  289. #endif
  290. }
  291. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(JucePluginWindow)
  292. };
  293. } // namespace juce
  294. using juce::JucePluginWindow;
  295. // -----------------------------------------------------------------------
  296. #endif // JUCE_PLUGIN_WINDOW_HPP_INCLUDED