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.

329 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. #if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client && JucePlugin_Build_Unity
  21. bool juce_isRunningInUnity();
  22. #endif
  23. #if JUCE_MODULE_AVAILABLE_juce_gui_extra
  24. LRESULT juce_offerEventToActiveXControl (::MSG&);
  25. #endif
  26. using CheckEventBlockedByModalComps = bool (*)(const MSG&);
  27. CheckEventBlockedByModalComps isEventBlockedByModalComps = nullptr;
  28. using SettingChangeCallbackFunc = void (*)(void);
  29. SettingChangeCallbackFunc settingChangeCallback = nullptr;
  30. //==============================================================================
  31. class InternalMessageQueue
  32. {
  33. public:
  34. InternalMessageQueue()
  35. {
  36. messageWindow = std::make_unique<HiddenMessageWindow> (messageWindowName, (WNDPROC) messageWndProc);
  37. juce_messageWindowHandle = messageWindow->getHWND();
  38. }
  39. ~InternalMessageQueue()
  40. {
  41. juce_messageWindowHandle = nullptr;
  42. clearSingletonInstance();
  43. }
  44. JUCE_DECLARE_SINGLETON (InternalMessageQueue, false)
  45. //==============================================================================
  46. void broadcastMessage (const String& message)
  47. {
  48. auto localCopy = message;
  49. Array<HWND> windows;
  50. EnumWindows (&broadcastEnumWindowProc, (LPARAM) &windows);
  51. for (int i = windows.size(); --i >= 0;)
  52. {
  53. COPYDATASTRUCT data;
  54. data.dwData = broadcastMessageMagicNumber;
  55. data.cbData = (DWORD) (((size_t) localCopy.length() + 1) * sizeof (CharPointer_UTF32::CharType));
  56. data.lpData = (void*) localCopy.toUTF32().getAddress();
  57. DWORD_PTR result;
  58. SendMessageTimeout (windows.getUnchecked (i), WM_COPYDATA,
  59. (WPARAM) juce_messageWindowHandle,
  60. (LPARAM) &data,
  61. SMTO_BLOCK | SMTO_ABORTIFHUNG, 8000, &result);
  62. }
  63. }
  64. void postMessage (MessageManager::MessageBase* message)
  65. {
  66. bool shouldTriggerMessageQueueDispatch = false;
  67. {
  68. const ScopedLock sl (lock);
  69. shouldTriggerMessageQueueDispatch = messageQueue.isEmpty();
  70. messageQueue.add (message);
  71. }
  72. if (! shouldTriggerMessageQueueDispatch)
  73. return;
  74. #if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client && JucePlugin_Build_Unity
  75. if (juce_isRunningInUnity())
  76. {
  77. SendNotifyMessage (juce_messageWindowHandle, customMessageID, 0, 0);
  78. return;
  79. }
  80. #endif
  81. PostMessage (juce_messageWindowHandle, customMessageID, 0, 0);
  82. }
  83. bool dispatchNextMessage (bool returnIfNoPendingMessages)
  84. {
  85. MSG m;
  86. if (returnIfNoPendingMessages && ! PeekMessage (&m, nullptr, 0, 0, PM_NOREMOVE))
  87. return false;
  88. if (GetMessage (&m, nullptr, 0, 0) >= 0)
  89. {
  90. #if JUCE_MODULE_AVAILABLE_juce_gui_extra
  91. if (juce_offerEventToActiveXControl (m) != S_FALSE)
  92. return true;
  93. #endif
  94. if (m.message == customMessageID && m.hwnd == juce_messageWindowHandle)
  95. {
  96. dispatchMessages();
  97. }
  98. else if (m.message == WM_QUIT)
  99. {
  100. if (auto* app = JUCEApplicationBase::getInstance())
  101. app->systemRequestedQuit();
  102. }
  103. else if (isEventBlockedByModalComps == nullptr || ! isEventBlockedByModalComps (m))
  104. {
  105. if ((m.message == WM_LBUTTONDOWN || m.message == WM_RBUTTONDOWN)
  106. && ! JuceWindowIdentifier::isJUCEWindow (m.hwnd))
  107. {
  108. // if it's someone else's window being clicked on, and the focus is
  109. // currently on a juce window, pass the kb focus over..
  110. auto currentFocus = GetFocus();
  111. if (currentFocus == nullptr || JuceWindowIdentifier::isJUCEWindow (currentFocus))
  112. SetFocus (m.hwnd);
  113. }
  114. TranslateMessage (&m);
  115. DispatchMessage (&m);
  116. }
  117. }
  118. return true;
  119. }
  120. private:
  121. //==============================================================================
  122. static LRESULT CALLBACK messageWndProc (HWND h, UINT message, WPARAM wParam, LPARAM lParam) noexcept
  123. {
  124. if (h == juce_messageWindowHandle)
  125. {
  126. if (message == customMessageID)
  127. {
  128. if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
  129. queue->dispatchMessages();
  130. return 0;
  131. }
  132. if (message == WM_COPYDATA)
  133. {
  134. handleBroadcastMessage (reinterpret_cast<const COPYDATASTRUCT*> (lParam));
  135. return 0;
  136. }
  137. if (message == WM_SETTINGCHANGE)
  138. if (settingChangeCallback != nullptr)
  139. settingChangeCallback();
  140. }
  141. return DefWindowProc (h, message, wParam, lParam);
  142. }
  143. static BOOL CALLBACK broadcastEnumWindowProc (HWND hwnd, LPARAM lParam)
  144. {
  145. if (hwnd != juce_messageWindowHandle)
  146. {
  147. TCHAR windowName[64] = { 0 }; // no need to read longer strings than this
  148. GetWindowText (hwnd, windowName, 63);
  149. if (String (windowName) == messageWindowName)
  150. reinterpret_cast<Array<HWND>*> (lParam)->add (hwnd);
  151. }
  152. return TRUE;
  153. }
  154. static void dispatchMessage (MessageManager::MessageBase* message)
  155. {
  156. JUCE_TRY
  157. {
  158. message->messageCallback();
  159. }
  160. JUCE_CATCH_EXCEPTION
  161. message->decReferenceCount();
  162. }
  163. static void handleBroadcastMessage (const COPYDATASTRUCT* data)
  164. {
  165. if (data != nullptr && data->dwData == broadcastMessageMagicNumber)
  166. {
  167. struct BroadcastMessage : public CallbackMessage
  168. {
  169. BroadcastMessage (CharPointer_UTF32 text, size_t length) : message (text, length) {}
  170. void messageCallback() override { MessageManager::getInstance()->deliverBroadcastMessage (message); }
  171. String message;
  172. };
  173. (new BroadcastMessage (CharPointer_UTF32 ((const CharPointer_UTF32::CharType*) data->lpData),
  174. data->cbData / sizeof (CharPointer_UTF32::CharType)))
  175. ->post();
  176. }
  177. }
  178. void dispatchMessages()
  179. {
  180. ReferenceCountedArray<MessageManager::MessageBase> messagesToDispatch;
  181. {
  182. const ScopedLock sl (lock);
  183. if (messageQueue.isEmpty())
  184. return;
  185. messagesToDispatch.swapWith (messageQueue);
  186. }
  187. for (int i = 0; i < messagesToDispatch.size(); ++i)
  188. {
  189. auto message = messagesToDispatch.getUnchecked (i);
  190. message->incReferenceCount();
  191. dispatchMessage (message.get());
  192. }
  193. }
  194. //==============================================================================
  195. static constexpr unsigned int customMessageID = WM_USER + 123;
  196. static constexpr unsigned int broadcastMessageMagicNumber = 0xc403;
  197. static const TCHAR messageWindowName[];
  198. std::unique_ptr<HiddenMessageWindow> messageWindow;
  199. CriticalSection lock;
  200. ReferenceCountedArray<MessageManager::MessageBase> messageQueue;
  201. //==============================================================================
  202. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InternalMessageQueue)
  203. };
  204. JUCE_IMPLEMENT_SINGLETON (InternalMessageQueue)
  205. const TCHAR InternalMessageQueue::messageWindowName[] = _T("JUCEWindow");
  206. //==============================================================================
  207. bool dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages)
  208. {
  209. if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
  210. return queue->dispatchNextMessage (returnIfNoPendingMessages);
  211. return false;
  212. }
  213. bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
  214. {
  215. if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
  216. {
  217. queue->postMessage (message);
  218. return true;
  219. }
  220. return false;
  221. }
  222. void MessageManager::broadcastMessage (const String& value)
  223. {
  224. if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
  225. queue->broadcastMessage (value);
  226. }
  227. //==============================================================================
  228. void MessageManager::doPlatformSpecificInitialisation()
  229. {
  230. ignoreUnused (OleInitialize (nullptr));
  231. InternalMessageQueue::getInstance();
  232. }
  233. void MessageManager::doPlatformSpecificShutdown()
  234. {
  235. InternalMessageQueue::deleteInstance();
  236. OleUninitialize();
  237. }
  238. //==============================================================================
  239. struct MountedVolumeListChangeDetector::Pimpl : private DeviceChangeDetector
  240. {
  241. Pimpl (MountedVolumeListChangeDetector& d) : DeviceChangeDetector (L"MountedVolumeList"), owner (d)
  242. {
  243. File::findFileSystemRoots (lastVolumeList);
  244. }
  245. void systemDeviceChanged() override
  246. {
  247. Array<File> newList;
  248. File::findFileSystemRoots (newList);
  249. if (lastVolumeList != newList)
  250. {
  251. lastVolumeList = newList;
  252. owner.mountedVolumeListChanged();
  253. }
  254. }
  255. MountedVolumeListChangeDetector& owner;
  256. Array<File> lastVolumeList;
  257. };
  258. MountedVolumeListChangeDetector::MountedVolumeListChangeDetector() { pimpl.reset (new Pimpl (*this)); }
  259. MountedVolumeListChangeDetector::~MountedVolumeListChangeDetector() {}
  260. } // namespace juce