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.

337 lines
11KB

  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. #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 = 0;
  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 = (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. bool postMessage (MessageManager::MessageBase* message)
  65. {
  66. message->incReferenceCount();
  67. #if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client && JucePlugin_Build_Unity
  68. if (juce_isRunningInUnity())
  69. return SendNotifyMessage (juce_messageWindowHandle, customMessageID, 0, (LPARAM) message) != 0;
  70. #endif
  71. if (PostMessage (juce_messageWindowHandle, customMessageID, 0, (LPARAM) message) != 0)
  72. return true;
  73. if (GetLastError() == ERROR_NOT_ENOUGH_QUOTA)
  74. {
  75. const ScopedLock sl (lock);
  76. overflowQueue.add (message);
  77. message->decReferenceCount();
  78. return true;
  79. }
  80. return false;
  81. }
  82. bool dispatchNextMessage (bool returnIfNoPendingMessages)
  83. {
  84. MSG m;
  85. if (returnIfNoPendingMessages && ! PeekMessage (&m, (HWND) 0, 0, 0, PM_NOREMOVE) && overflowQueue.size() == 0)
  86. return false;
  87. if (GetMessage (&m, (HWND) 0, 0, 0) >= 0)
  88. {
  89. #if JUCE_MODULE_AVAILABLE_juce_gui_extra
  90. if (juce_offerEventToActiveXControl (m) != S_FALSE)
  91. return true;
  92. #endif
  93. if (m.message == customMessageID && m.hwnd == juce_messageWindowHandle)
  94. {
  95. dispatchMessageFromLParam (m.lParam);
  96. }
  97. else if (m.message == WM_QUIT)
  98. {
  99. if (auto* app = JUCEApplicationBase::getInstance())
  100. app->systemRequestedQuit();
  101. }
  102. else if (isEventBlockedByModalComps == nullptr || ! isEventBlockedByModalComps (m))
  103. {
  104. if ((m.message == WM_LBUTTONDOWN || m.message == WM_RBUTTONDOWN)
  105. && ! JuceWindowIdentifier::isJUCEWindow (m.hwnd))
  106. {
  107. // if it's someone else's window being clicked on, and the focus is
  108. // currently on a juce window, pass the kb focus over..
  109. auto currentFocus = GetFocus();
  110. if (currentFocus == 0 || JuceWindowIdentifier::isJUCEWindow (currentFocus))
  111. SetFocus (m.hwnd);
  112. }
  113. TranslateMessage (&m);
  114. DispatchMessage (&m);
  115. }
  116. }
  117. dispatchOverflowMessages();
  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. // (These are trapped early in our dispatch loop, but must also be checked
  129. // here in case some 3rd-party code is running the dispatch loop).
  130. dispatchMessageFromLParam (lParam);
  131. if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
  132. queue->dispatchOverflowMessages();
  133. return 0;
  134. }
  135. if (message == WM_COPYDATA)
  136. {
  137. handleBroadcastMessage (reinterpret_cast<const COPYDATASTRUCT*> (lParam));
  138. return 0;
  139. }
  140. if (message == WM_SETTINGCHANGE)
  141. if (settingChangeCallback != nullptr)
  142. settingChangeCallback();
  143. }
  144. return DefWindowProc (h, message, wParam, lParam);
  145. }
  146. static BOOL CALLBACK broadcastEnumWindowProc (HWND hwnd, LPARAM lParam)
  147. {
  148. if (hwnd != juce_messageWindowHandle)
  149. {
  150. TCHAR windowName[64] = { 0 }; // no need to read longer strings than this
  151. GetWindowText (hwnd, windowName, 63);
  152. if (String (windowName) == messageWindowName)
  153. reinterpret_cast<Array<HWND>*> (lParam)->add (hwnd);
  154. }
  155. return TRUE;
  156. }
  157. static void dispatchMessageFromLParam (LPARAM lParam)
  158. {
  159. if (auto message = reinterpret_cast<MessageManager::MessageBase*> (lParam))
  160. {
  161. JUCE_TRY
  162. {
  163. message->messageCallback();
  164. }
  165. JUCE_CATCH_EXCEPTION
  166. message->decReferenceCount();
  167. }
  168. }
  169. static void handleBroadcastMessage (const COPYDATASTRUCT* data)
  170. {
  171. if (data != nullptr && data->dwData == broadcastMessageMagicNumber)
  172. {
  173. struct BroadcastMessage : public CallbackMessage
  174. {
  175. BroadcastMessage (CharPointer_UTF32 text, size_t length) : message (text, length) {}
  176. void messageCallback() override { MessageManager::getInstance()->deliverBroadcastMessage (message); }
  177. String message;
  178. };
  179. (new BroadcastMessage (CharPointer_UTF32 ((const CharPointer_UTF32::CharType*) data->lpData),
  180. data->cbData / sizeof (CharPointer_UTF32::CharType)))
  181. ->post();
  182. }
  183. }
  184. void dispatchOverflowMessages()
  185. {
  186. ReferenceCountedArray<MessageManager::MessageBase> messagesToDispatch;
  187. {
  188. const ScopedLock sl (lock);
  189. if (overflowQueue.isEmpty())
  190. return;
  191. messagesToDispatch.swapWith (overflowQueue);
  192. }
  193. for (int i = 0; i < messagesToDispatch.size(); ++i)
  194. {
  195. auto message = messagesToDispatch.getUnchecked (i);
  196. message->incReferenceCount();
  197. dispatchMessageFromLParam ((LPARAM) message.get());
  198. }
  199. }
  200. //==============================================================================
  201. static constexpr unsigned int customMessageID = WM_USER + 123;
  202. static constexpr unsigned int broadcastMessageMagicNumber = 0xc403;
  203. static const TCHAR messageWindowName[];
  204. std::unique_ptr<HiddenMessageWindow> messageWindow;
  205. CriticalSection lock;
  206. ReferenceCountedArray<MessageManager::MessageBase> overflowQueue;
  207. //==============================================================================
  208. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InternalMessageQueue)
  209. };
  210. JUCE_IMPLEMENT_SINGLETON (InternalMessageQueue)
  211. const TCHAR InternalMessageQueue::messageWindowName[] = _T("JUCEWindow");
  212. //==============================================================================
  213. bool MessageManager::dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages)
  214. {
  215. if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
  216. return queue->dispatchNextMessage (returnIfNoPendingMessages);
  217. return false;
  218. }
  219. bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
  220. {
  221. if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
  222. {
  223. queue->postMessage (message);
  224. return true;
  225. }
  226. return false;
  227. }
  228. void MessageManager::broadcastMessage (const String& value)
  229. {
  230. if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
  231. queue->broadcastMessage (value);
  232. }
  233. //==============================================================================
  234. void MessageManager::doPlatformSpecificInitialisation()
  235. {
  236. OleInitialize (0);
  237. InternalMessageQueue::getInstance();
  238. }
  239. void MessageManager::doPlatformSpecificShutdown()
  240. {
  241. InternalMessageQueue::deleteInstance();
  242. OleUninitialize();
  243. }
  244. //==============================================================================
  245. struct MountedVolumeListChangeDetector::Pimpl : private DeviceChangeDetector
  246. {
  247. Pimpl (MountedVolumeListChangeDetector& d) : DeviceChangeDetector (L"MountedVolumeList"), owner (d)
  248. {
  249. File::findFileSystemRoots (lastVolumeList);
  250. }
  251. void systemDeviceChanged() override
  252. {
  253. Array<File> newList;
  254. File::findFileSystemRoots (newList);
  255. if (lastVolumeList != newList)
  256. {
  257. lastVolumeList = newList;
  258. owner.mountedVolumeListChanged();
  259. }
  260. }
  261. MountedVolumeListChangeDetector& owner;
  262. Array<File> lastVolumeList;
  263. };
  264. MountedVolumeListChangeDetector::MountedVolumeListChangeDetector() { pimpl.reset (new Pimpl (*this)); }
  265. MountedVolumeListChangeDetector::~MountedVolumeListChangeDetector() {}
  266. } // namespace juce