The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

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