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.

233 lines
8.0KB

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