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.

227 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. extern HWND juce_messageWindowHandle;
  24. typedef bool (*CheckEventBlockedByModalComps) (const MSG&);
  25. CheckEventBlockedByModalComps isEventBlockedByModalComps = nullptr;
  26. //==============================================================================
  27. namespace WindowsMessageHelpers
  28. {
  29. const unsigned int customMessageID = WM_USER + 123;
  30. const unsigned int broadcastMessageMagicNumber = 0xc403;
  31. const TCHAR messageWindowName[] = _T("JUCEWindow");
  32. ScopedPointer<HiddenMessageWindow> messageWindow;
  33. void dispatchMessageFromLParam (LPARAM lParam)
  34. {
  35. if (MessageManager::MessageBase* message = reinterpret_cast<MessageManager::MessageBase*> (lParam))
  36. {
  37. JUCE_TRY
  38. {
  39. message->messageCallback();
  40. }
  41. JUCE_CATCH_EXCEPTION
  42. message->decReferenceCount();
  43. }
  44. }
  45. BOOL CALLBACK broadcastEnumWindowProc (HWND hwnd, LPARAM lParam)
  46. {
  47. if (hwnd != juce_messageWindowHandle)
  48. {
  49. TCHAR windowName[64] = { 0 }; // no need to read longer strings than this
  50. GetWindowText (hwnd, windowName, 63);
  51. if (String (windowName) == messageWindowName)
  52. reinterpret_cast<Array<HWND>*> (lParam)->add (hwnd);
  53. }
  54. return TRUE;
  55. }
  56. void handleBroadcastMessage (const COPYDATASTRUCT* const data)
  57. {
  58. if (data != nullptr && data->dwData == broadcastMessageMagicNumber)
  59. {
  60. struct BroadcastMessage : public CallbackMessage
  61. {
  62. BroadcastMessage (CharPointer_UTF32 text, size_t length) : message (text, length) {}
  63. void messageCallback() override { MessageManager::getInstance()->deliverBroadcastMessage (message); }
  64. String message;
  65. };
  66. (new BroadcastMessage (CharPointer_UTF32 ((const CharPointer_UTF32::CharType*) data->lpData),
  67. data->cbData / sizeof (CharPointer_UTF32::CharType)))
  68. ->post();
  69. }
  70. }
  71. //==============================================================================
  72. LRESULT CALLBACK messageWndProc (HWND h, const UINT message, const WPARAM wParam, const LPARAM lParam) noexcept
  73. {
  74. if (h == juce_messageWindowHandle)
  75. {
  76. if (message == customMessageID)
  77. {
  78. // (These are trapped early in our dispatch loop, but must also be checked
  79. // here in case some 3rd-party code is running the dispatch loop).
  80. dispatchMessageFromLParam (lParam);
  81. return 0;
  82. }
  83. if (message == WM_COPYDATA)
  84. {
  85. handleBroadcastMessage (reinterpret_cast<const COPYDATASTRUCT*> (lParam));
  86. return 0;
  87. }
  88. }
  89. return DefWindowProc (h, message, wParam, lParam);
  90. }
  91. }
  92. //==============================================================================
  93. bool MessageManager::dispatchNextMessageOnSystemQueue (const bool returnIfNoPendingMessages)
  94. {
  95. using namespace WindowsMessageHelpers;
  96. MSG m;
  97. if (returnIfNoPendingMessages && ! PeekMessage (&m, (HWND) 0, 0, 0, PM_NOREMOVE))
  98. return false;
  99. if (GetMessage (&m, (HWND) 0, 0, 0) >= 0)
  100. {
  101. if (m.message == customMessageID && m.hwnd == juce_messageWindowHandle)
  102. {
  103. dispatchMessageFromLParam (m.lParam);
  104. }
  105. else if (m.message == WM_QUIT)
  106. {
  107. if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance())
  108. app->systemRequestedQuit();
  109. }
  110. else if (isEventBlockedByModalComps == nullptr || ! isEventBlockedByModalComps (m))
  111. {
  112. if ((m.message == WM_LBUTTONDOWN || m.message == WM_RBUTTONDOWN)
  113. && ! JuceWindowIdentifier::isJUCEWindow (m.hwnd))
  114. {
  115. // if it's someone else's window being clicked on, and the focus is
  116. // currently on a juce window, pass the kb focus over..
  117. HWND currentFocus = GetFocus();
  118. if (currentFocus == 0 || JuceWindowIdentifier::isJUCEWindow (currentFocus))
  119. SetFocus (m.hwnd);
  120. }
  121. TranslateMessage (&m);
  122. DispatchMessage (&m);
  123. }
  124. }
  125. return true;
  126. }
  127. bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
  128. {
  129. message->incReferenceCount();
  130. return PostMessage (juce_messageWindowHandle, WindowsMessageHelpers::customMessageID, 0, (LPARAM) message) != 0;
  131. }
  132. void MessageManager::broadcastMessage (const String& value)
  133. {
  134. const String localCopy (value);
  135. Array<HWND> windows;
  136. EnumWindows (&WindowsMessageHelpers::broadcastEnumWindowProc, (LPARAM) &windows);
  137. for (int i = windows.size(); --i >= 0;)
  138. {
  139. COPYDATASTRUCT data;
  140. data.dwData = WindowsMessageHelpers::broadcastMessageMagicNumber;
  141. data.cbData = (localCopy.length() + 1) * sizeof (CharPointer_UTF32::CharType);
  142. data.lpData = (void*) localCopy.toUTF32().getAddress();
  143. DWORD_PTR result;
  144. SendMessageTimeout (windows.getUnchecked(i), WM_COPYDATA,
  145. (WPARAM) juce_messageWindowHandle,
  146. (LPARAM) &data,
  147. SMTO_BLOCK | SMTO_ABORTIFHUNG, 8000, &result);
  148. }
  149. }
  150. //==============================================================================
  151. void MessageManager::doPlatformSpecificInitialisation()
  152. {
  153. OleInitialize (0);
  154. using namespace WindowsMessageHelpers;
  155. messageWindow = new HiddenMessageWindow (messageWindowName, (WNDPROC) messageWndProc);
  156. juce_messageWindowHandle = messageWindow->getHWND();
  157. }
  158. void MessageManager::doPlatformSpecificShutdown()
  159. {
  160. WindowsMessageHelpers::messageWindow = nullptr;
  161. OleUninitialize();
  162. }
  163. //==============================================================================
  164. struct MountedVolumeListChangeDetector::Pimpl : private DeviceChangeDetector
  165. {
  166. Pimpl (MountedVolumeListChangeDetector& d) : DeviceChangeDetector (L"MountedVolumeList"), owner (d)
  167. {
  168. File::findFileSystemRoots (lastVolumeList);
  169. }
  170. void systemDeviceChanged() override
  171. {
  172. Array<File> newList;
  173. File::findFileSystemRoots (newList);
  174. if (lastVolumeList != newList)
  175. {
  176. lastVolumeList = newList;
  177. owner.mountedVolumeListChanged();
  178. }
  179. }
  180. MountedVolumeListChangeDetector& owner;
  181. Array<File> lastVolumeList;
  182. };
  183. MountedVolumeListChangeDetector::MountedVolumeListChangeDetector() { pimpl = new Pimpl (*this); }
  184. MountedVolumeListChangeDetector::~MountedVolumeListChangeDetector() {}