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.

385 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. // (This file gets included by juce_win32_NativeCode.cpp, rather than being
  19. // compiled on its own).
  20. #if JUCE_INCLUDED_FILE
  21. //==============================================================================
  22. class HiddenMessageWindow
  23. {
  24. public:
  25. HiddenMessageWindow (const TCHAR* const messageWindowName, WNDPROC wndProc)
  26. {
  27. String className ("JUCE_");
  28. className << String::toHexString (Time::getHighResolutionTicks());
  29. HMODULE moduleHandle = (HMODULE) Process::getCurrentModuleInstanceHandle();
  30. WNDCLASSEX wc = { 0 };
  31. wc.cbSize = sizeof (wc);
  32. wc.lpfnWndProc = wndProc;
  33. wc.cbWndExtra = 4;
  34. wc.hInstance = moduleHandle;
  35. wc.lpszClassName = className.toWideCharPointer();
  36. atom = RegisterClassEx (&wc);
  37. jassert (atom != 0);
  38. hwnd = CreateWindow (getClassNameFromAtom(), messageWindowName,
  39. 0, 0, 0, 0, 0, 0, 0, moduleHandle, 0);
  40. jassert (hwnd != 0);
  41. }
  42. ~HiddenMessageWindow()
  43. {
  44. DestroyWindow (hwnd);
  45. UnregisterClass (getClassNameFromAtom(), 0);
  46. }
  47. inline HWND getHWND() const noexcept { return hwnd; }
  48. private:
  49. ATOM atom;
  50. HWND hwnd;
  51. LPCTSTR getClassNameFromAtom() noexcept { return (LPCTSTR) MAKELONG (atom, 0); }
  52. };
  53. //==============================================================================
  54. class JuceWindowIdentifier
  55. {
  56. public:
  57. static bool isJUCEWindow (HWND hwnd) noexcept
  58. {
  59. return GetWindowLong (hwnd, GWLP_USERDATA) == improbableWindowNumber;
  60. }
  61. static void setAsJUCEWindow (HWND hwnd, bool isJuceWindow) noexcept
  62. {
  63. SetWindowLongPtr (hwnd, GWLP_USERDATA, isJuceWindow ? improbableWindowNumber : 0);
  64. }
  65. private:
  66. enum { improbableWindowNumber = 0xf965aa01 };
  67. };
  68. //==============================================================================
  69. namespace WindowsMessageHelpers
  70. {
  71. const unsigned int specialId = WM_APP + 0x4400;
  72. const unsigned int broadcastId = WM_APP + 0x4403;
  73. const unsigned int specialCallbackId = WM_APP + 0x4402;
  74. const TCHAR messageWindowName[] = _T("JUCEWindow");
  75. ScopedPointer<HiddenMessageWindow> messageWindow;
  76. //==============================================================================
  77. LRESULT CALLBACK messageWndProc (HWND h, const UINT message, const WPARAM wParam, const LPARAM lParam) noexcept
  78. {
  79. JUCE_TRY
  80. {
  81. if (h == juce_messageWindowHandle)
  82. {
  83. if (message == specialCallbackId)
  84. {
  85. MessageCallbackFunction* const func = (MessageCallbackFunction*) wParam;
  86. return (LRESULT) (*func) ((void*) lParam);
  87. }
  88. else if (message == specialId)
  89. {
  90. // these are trapped early in the dispatch call, but must also be checked
  91. // here in case there are windows modal dialog boxes doing their own
  92. // dispatch loop and not calling our version
  93. Message* const message = reinterpret_cast <Message*> (lParam);
  94. MessageManager::getInstance()->deliverMessage (message);
  95. message->decReferenceCount();
  96. return 0;
  97. }
  98. else if (message == broadcastId)
  99. {
  100. const ScopedPointer <String> messageString ((String*) lParam);
  101. MessageManager::getInstance()->deliverBroadcastMessage (*messageString);
  102. return 0;
  103. }
  104. else if (message == WM_COPYDATA && ((const COPYDATASTRUCT*) lParam)->dwData == broadcastId)
  105. {
  106. const COPYDATASTRUCT* data = (COPYDATASTRUCT*) lParam;
  107. const String messageString (CharPointer_UTF32 ((const CharPointer_UTF32::CharType*) data->lpData),
  108. data->cbData / sizeof (CharPointer_UTF32::CharType));
  109. PostMessage (juce_messageWindowHandle, broadcastId, 0, (LPARAM) new String (messageString));
  110. return 0;
  111. }
  112. }
  113. }
  114. JUCE_CATCH_EXCEPTION
  115. return DefWindowProc (h, message, wParam, lParam);
  116. }
  117. bool isHWNDBlockedByModalComponents (HWND h) noexcept
  118. {
  119. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  120. {
  121. Component* const c = Desktop::getInstance().getComponent (i);
  122. if (c != nullptr
  123. && (! c->isCurrentlyBlockedByAnotherModalComponent())
  124. && IsChild ((HWND) c->getWindowHandle(), h))
  125. return false;
  126. }
  127. return true;
  128. }
  129. bool isEventBlockedByModalComps (MSG& m)
  130. {
  131. if (Component::getNumCurrentlyModalComponents() == 0 || JuceWindowIdentifier::isJUCEWindow (m.hwnd))
  132. return false;
  133. switch (m.message)
  134. {
  135. case WM_MOUSEMOVE:
  136. case WM_NCMOUSEMOVE:
  137. case 0x020A: /* WM_MOUSEWHEEL */
  138. case 0x020E: /* WM_MOUSEHWHEEL */
  139. case WM_KEYUP:
  140. case WM_SYSKEYUP:
  141. case WM_CHAR:
  142. case WM_APPCOMMAND:
  143. case WM_LBUTTONUP:
  144. case WM_MBUTTONUP:
  145. case WM_RBUTTONUP:
  146. case WM_MOUSEACTIVATE:
  147. case WM_NCMOUSEHOVER:
  148. case WM_MOUSEHOVER:
  149. return isHWNDBlockedByModalComponents (m.hwnd);
  150. case WM_NCLBUTTONDOWN:
  151. case WM_NCLBUTTONDBLCLK:
  152. case WM_NCRBUTTONDOWN:
  153. case WM_NCRBUTTONDBLCLK:
  154. case WM_NCMBUTTONDOWN:
  155. case WM_NCMBUTTONDBLCLK:
  156. case WM_LBUTTONDOWN:
  157. case WM_LBUTTONDBLCLK:
  158. case WM_MBUTTONDOWN:
  159. case WM_MBUTTONDBLCLK:
  160. case WM_RBUTTONDOWN:
  161. case WM_RBUTTONDBLCLK:
  162. case WM_KEYDOWN:
  163. case WM_SYSKEYDOWN:
  164. if (isHWNDBlockedByModalComponents (m.hwnd))
  165. {
  166. Component* const modal = Component::getCurrentlyModalComponent (0);
  167. if (modal != nullptr)
  168. modal->inputAttemptWhenModal();
  169. return true;
  170. }
  171. break;
  172. default:
  173. break;
  174. }
  175. return false;
  176. }
  177. BOOL CALLBACK broadcastEnumWindowProc (HWND hwnd, LPARAM lParam)
  178. {
  179. if (hwnd != juce_messageWindowHandle)
  180. reinterpret_cast <Array<HWND>*> (lParam)->add (hwnd);
  181. return TRUE;
  182. }
  183. }
  184. //==============================================================================
  185. bool MessageManager::dispatchNextMessageOnSystemQueue (const bool returnIfNoPendingMessages)
  186. {
  187. using namespace WindowsMessageHelpers;
  188. MSG m;
  189. if (returnIfNoPendingMessages && ! PeekMessage (&m, (HWND) 0, 0, 0, 0))
  190. return false;
  191. if (GetMessage (&m, (HWND) 0, 0, 0) >= 0)
  192. {
  193. if (m.message == specialId && m.hwnd == juce_messageWindowHandle)
  194. {
  195. Message* const message = reinterpret_cast <Message*> (m.lParam);
  196. MessageManager::getInstance()->deliverMessage (message);
  197. message->decReferenceCount();
  198. }
  199. else if (m.message == WM_QUIT)
  200. {
  201. if (JUCEApplication::getInstance() != nullptr)
  202. JUCEApplication::getInstance()->systemRequestedQuit();
  203. }
  204. else if (! WindowsMessageHelpers::isEventBlockedByModalComps (m))
  205. {
  206. if ((m.message == WM_LBUTTONDOWN || m.message == WM_RBUTTONDOWN)
  207. && ! JuceWindowIdentifier::isJUCEWindow (m.hwnd))
  208. {
  209. // if it's someone else's window being clicked on, and the focus is
  210. // currently on a juce window, pass the kb focus over..
  211. HWND currentFocus = GetFocus();
  212. if (currentFocus == 0 || JuceWindowIdentifier::isJUCEWindow (currentFocus))
  213. SetFocus (m.hwnd);
  214. }
  215. TranslateMessage (&m);
  216. DispatchMessage (&m);
  217. }
  218. }
  219. return true;
  220. }
  221. bool MessageManager::postMessageToSystemQueue (Message* message)
  222. {
  223. message->incReferenceCount();
  224. return PostMessage (juce_messageWindowHandle, WindowsMessageHelpers::specialId, 0, (LPARAM) message) != 0;
  225. }
  226. void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* callback,
  227. void* userData)
  228. {
  229. if (MessageManager::getInstance()->isThisTheMessageThread())
  230. {
  231. return (*callback) (userData);
  232. }
  233. else
  234. {
  235. // If a thread has a MessageManagerLock and then tries to call this method, it'll
  236. // deadlock because the message manager is blocked from running, and can't
  237. // call your function..
  238. jassert (! MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  239. return (void*) SendMessage (juce_messageWindowHandle,
  240. WindowsMessageHelpers::specialCallbackId,
  241. (WPARAM) callback,
  242. (LPARAM) userData);
  243. }
  244. }
  245. void MessageManager::broadcastMessage (const String& value)
  246. {
  247. Array<HWND> windows;
  248. EnumWindows (&WindowsMessageHelpers::broadcastEnumWindowProc, (LPARAM) &windows);
  249. const String localCopy (value);
  250. COPYDATASTRUCT data;
  251. data.dwData = WindowsMessageHelpers::broadcastId;
  252. data.cbData = (localCopy.length() + 1) * sizeof (CharPointer_UTF32::CharType);
  253. data.lpData = (void*) localCopy.toUTF32().getAddress();
  254. for (int i = windows.size(); --i >= 0;)
  255. {
  256. HWND hwnd = windows.getUnchecked(i);
  257. TCHAR windowName [64]; // no need to read longer strings than this
  258. GetWindowText (hwnd, windowName, 64);
  259. windowName [63] = 0;
  260. if (String (windowName) == WindowsMessageHelpers::messageWindowName)
  261. {
  262. DWORD_PTR result;
  263. SendMessageTimeout (hwnd, WM_COPYDATA,
  264. (WPARAM) juce_messageWindowHandle,
  265. (LPARAM) &data,
  266. SMTO_BLOCK | SMTO_ABORTIFHUNG, 8000, &result);
  267. }
  268. }
  269. }
  270. //==============================================================================
  271. void MessageManager::doPlatformSpecificInitialisation()
  272. {
  273. OleInitialize (0);
  274. using namespace WindowsMessageHelpers;
  275. messageWindow = new HiddenMessageWindow (messageWindowName, (WNDPROC) messageWndProc);
  276. juce_messageWindowHandle = messageWindow->getHWND();
  277. }
  278. void MessageManager::doPlatformSpecificShutdown()
  279. {
  280. WindowsMessageHelpers::messageWindow = nullptr;
  281. OleUninitialize();
  282. }
  283. //==============================================================================
  284. class DeviceChangeDetector : private Timer // (Used by various audio classes)
  285. {
  286. public:
  287. DeviceChangeDetector (const wchar_t* const name)
  288. : messageWindow (name, (WNDPROC) deviceChangeEventCallback)
  289. {
  290. SetWindowLongPtr (messageWindow.getHWND(), GWLP_USERDATA, (LONG_PTR) this);
  291. }
  292. virtual ~DeviceChangeDetector() {}
  293. protected:
  294. virtual void systemDeviceChanged() = 0;
  295. private:
  296. HiddenMessageWindow messageWindow;
  297. static LRESULT CALLBACK deviceChangeEventCallback (HWND h, const UINT message,
  298. const WPARAM wParam, const LPARAM lParam)
  299. {
  300. if (message == WM_DEVICECHANGE
  301. && (wParam == 0x8000 /*DBT_DEVICEARRIVAL*/
  302. || wParam == 0x8004 /*DBT_DEVICEREMOVECOMPLETE*/
  303. || wParam == 0x0007 /*DBT_DEVNODES_CHANGED*/))
  304. {
  305. // We'll pause before sending a message, because on device removal, the OS hasn't always updated
  306. // its device lists correctly at this point. This also helps avoid repeated callbacks.
  307. ((DeviceChangeDetector*) GetWindowLongPtr (h, GWLP_USERDATA))->startTimer (500);
  308. }
  309. return DefWindowProc (h, message, wParam, lParam);
  310. }
  311. void timerCallback()
  312. {
  313. systemDeviceChanged();
  314. }
  315. };
  316. #endif