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.

251 lines
8.6KB

  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. typedef void (*SettingChangeCallbackFunc) (void);
  23. SettingChangeCallbackFunc settingChangeCallback = nullptr;
  24. #if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client && JucePlugin_Build_Unity
  25. bool juce_isRunningInUnity();
  26. #endif
  27. //==============================================================================
  28. namespace WindowsMessageHelpers
  29. {
  30. const unsigned int customMessageID = WM_USER + 123;
  31. const unsigned int broadcastMessageMagicNumber = 0xc403;
  32. const TCHAR messageWindowName[] = _T("JUCEWindow");
  33. std::unique_ptr<HiddenMessageWindow> messageWindow;
  34. void dispatchMessageFromLParam (LPARAM lParam)
  35. {
  36. if (MessageManager::MessageBase* message = reinterpret_cast<MessageManager::MessageBase*> (lParam))
  37. {
  38. JUCE_TRY
  39. {
  40. message->messageCallback();
  41. }
  42. JUCE_CATCH_EXCEPTION
  43. message->decReferenceCount();
  44. }
  45. }
  46. BOOL CALLBACK broadcastEnumWindowProc (HWND hwnd, LPARAM lParam)
  47. {
  48. if (hwnd != juce_messageWindowHandle)
  49. {
  50. TCHAR windowName[64] = { 0 }; // no need to read longer strings than this
  51. GetWindowText (hwnd, windowName, 63);
  52. if (String (windowName) == messageWindowName)
  53. reinterpret_cast<Array<HWND>*> (lParam)->add (hwnd);
  54. }
  55. return TRUE;
  56. }
  57. void handleBroadcastMessage (const COPYDATASTRUCT* const data)
  58. {
  59. if (data != nullptr && data->dwData == broadcastMessageMagicNumber)
  60. {
  61. struct BroadcastMessage : public CallbackMessage
  62. {
  63. BroadcastMessage (CharPointer_UTF32 text, size_t length) : message (text, length) {}
  64. void messageCallback() override { MessageManager::getInstance()->deliverBroadcastMessage (message); }
  65. String message;
  66. };
  67. (new BroadcastMessage (CharPointer_UTF32 ((const CharPointer_UTF32::CharType*) data->lpData),
  68. data->cbData / sizeof (CharPointer_UTF32::CharType)))
  69. ->post();
  70. }
  71. }
  72. //==============================================================================
  73. LRESULT CALLBACK messageWndProc (HWND h, const UINT message, const WPARAM wParam, const LPARAM lParam) noexcept
  74. {
  75. if (h == juce_messageWindowHandle)
  76. {
  77. if (message == customMessageID)
  78. {
  79. // (These are trapped early in our dispatch loop, but must also be checked
  80. // here in case some 3rd-party code is running the dispatch loop).
  81. dispatchMessageFromLParam (lParam);
  82. return 0;
  83. }
  84. if (message == WM_COPYDATA)
  85. {
  86. handleBroadcastMessage (reinterpret_cast<const COPYDATASTRUCT*> (lParam));
  87. return 0;
  88. }
  89. else if (message == WM_SETTINGCHANGE)
  90. {
  91. if (settingChangeCallback != nullptr)
  92. settingChangeCallback();
  93. }
  94. }
  95. return DefWindowProc (h, message, wParam, lParam);
  96. }
  97. }
  98. #if JUCE_MODULE_AVAILABLE_juce_gui_extra
  99. LRESULT juce_offerEventToActiveXControl (::MSG&);
  100. #endif
  101. //==============================================================================
  102. bool MessageManager::dispatchNextMessageOnSystemQueue (const bool returnIfNoPendingMessages)
  103. {
  104. using namespace WindowsMessageHelpers;
  105. MSG m;
  106. if (returnIfNoPendingMessages && ! PeekMessage (&m, (HWND) 0, 0, 0, PM_NOREMOVE))
  107. return false;
  108. if (GetMessage (&m, (HWND) 0, 0, 0) >= 0)
  109. {
  110. #if JUCE_MODULE_AVAILABLE_juce_gui_extra
  111. if (juce_offerEventToActiveXControl (m) != S_FALSE)
  112. return true;
  113. #endif
  114. if (m.message == customMessageID && m.hwnd == juce_messageWindowHandle)
  115. {
  116. dispatchMessageFromLParam (m.lParam);
  117. }
  118. else if (m.message == WM_QUIT)
  119. {
  120. if (auto* app = JUCEApplicationBase::getInstance())
  121. app->systemRequestedQuit();
  122. }
  123. else if (isEventBlockedByModalComps == nullptr || ! isEventBlockedByModalComps (m))
  124. {
  125. if ((m.message == WM_LBUTTONDOWN || m.message == WM_RBUTTONDOWN)
  126. && ! JuceWindowIdentifier::isJUCEWindow (m.hwnd))
  127. {
  128. // if it's someone else's window being clicked on, and the focus is
  129. // currently on a juce window, pass the kb focus over..
  130. HWND currentFocus = GetFocus();
  131. if (currentFocus == 0 || JuceWindowIdentifier::isJUCEWindow (currentFocus))
  132. SetFocus (m.hwnd);
  133. }
  134. TranslateMessage (&m);
  135. DispatchMessage (&m);
  136. }
  137. }
  138. return true;
  139. }
  140. bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
  141. {
  142. message->incReferenceCount();
  143. #if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client && JucePlugin_Build_Unity
  144. if (juce_isRunningInUnity())
  145. return SendNotifyMessage (juce_messageWindowHandle, WindowsMessageHelpers::customMessageID, 0, (LPARAM) message) != 0;
  146. else
  147. #endif
  148. return PostMessage (juce_messageWindowHandle, WindowsMessageHelpers::customMessageID, 0, (LPARAM) message) != 0;
  149. }
  150. void MessageManager::broadcastMessage (const String& value)
  151. {
  152. const String localCopy (value);
  153. Array<HWND> windows;
  154. EnumWindows (&WindowsMessageHelpers::broadcastEnumWindowProc, (LPARAM) &windows);
  155. for (int i = windows.size(); --i >= 0;)
  156. {
  157. COPYDATASTRUCT data;
  158. data.dwData = WindowsMessageHelpers::broadcastMessageMagicNumber;
  159. data.cbData = (localCopy.length() + 1) * sizeof (CharPointer_UTF32::CharType);
  160. data.lpData = (void*) localCopy.toUTF32().getAddress();
  161. DWORD_PTR result;
  162. SendMessageTimeout (windows.getUnchecked (i), WM_COPYDATA,
  163. (WPARAM) juce_messageWindowHandle,
  164. (LPARAM) &data,
  165. SMTO_BLOCK | SMTO_ABORTIFHUNG, 8000, &result);
  166. }
  167. }
  168. //==============================================================================
  169. void MessageManager::doPlatformSpecificInitialisation()
  170. {
  171. OleInitialize (0);
  172. using namespace WindowsMessageHelpers;
  173. messageWindow.reset (new HiddenMessageWindow (messageWindowName, (WNDPROC) messageWndProc));
  174. juce_messageWindowHandle = messageWindow->getHWND();
  175. }
  176. void MessageManager::doPlatformSpecificShutdown()
  177. {
  178. WindowsMessageHelpers::messageWindow = nullptr;
  179. OleUninitialize();
  180. }
  181. //==============================================================================
  182. struct MountedVolumeListChangeDetector::Pimpl : private DeviceChangeDetector
  183. {
  184. Pimpl (MountedVolumeListChangeDetector& d) : DeviceChangeDetector (L"MountedVolumeList"), owner (d)
  185. {
  186. File::findFileSystemRoots (lastVolumeList);
  187. }
  188. void systemDeviceChanged() override
  189. {
  190. Array<File> newList;
  191. File::findFileSystemRoots (newList);
  192. if (lastVolumeList != newList)
  193. {
  194. lastVolumeList = newList;
  195. owner.mountedVolumeListChanged();
  196. }
  197. }
  198. MountedVolumeListChangeDetector& owner;
  199. Array<File> lastVolumeList;
  200. };
  201. MountedVolumeListChangeDetector::MountedVolumeListChangeDetector() { pimpl.reset (new Pimpl (*this)); }
  202. MountedVolumeListChangeDetector::~MountedVolumeListChangeDetector() {}
  203. } // namespace juce