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.

144 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_WIN32_HIDDENMESSAGEWINDOW_H_INCLUDED
  24. #define JUCE_WIN32_HIDDENMESSAGEWINDOW_H_INCLUDED
  25. //==============================================================================
  26. class HiddenMessageWindow
  27. {
  28. public:
  29. HiddenMessageWindow (const TCHAR* const messageWindowName, WNDPROC wndProc)
  30. {
  31. String className ("JUCE_");
  32. className << String::toHexString (Time::getHighResolutionTicks());
  33. HMODULE moduleHandle = (HMODULE) Process::getCurrentModuleInstanceHandle();
  34. WNDCLASSEX wc = { 0 };
  35. wc.cbSize = sizeof (wc);
  36. wc.lpfnWndProc = wndProc;
  37. wc.cbWndExtra = 4;
  38. wc.hInstance = moduleHandle;
  39. wc.lpszClassName = className.toWideCharPointer();
  40. atom = RegisterClassEx (&wc);
  41. jassert (atom != 0);
  42. hwnd = CreateWindow (getClassNameFromAtom(), messageWindowName,
  43. 0, 0, 0, 0, 0, 0, 0, moduleHandle, 0);
  44. jassert (hwnd != 0);
  45. }
  46. ~HiddenMessageWindow()
  47. {
  48. DestroyWindow (hwnd);
  49. UnregisterClass (getClassNameFromAtom(), 0);
  50. }
  51. inline HWND getHWND() const noexcept { return hwnd; }
  52. private:
  53. ATOM atom;
  54. HWND hwnd;
  55. LPCTSTR getClassNameFromAtom() noexcept { return (LPCTSTR) (pointer_sized_uint) atom; }
  56. };
  57. //==============================================================================
  58. class JuceWindowIdentifier
  59. {
  60. public:
  61. static bool isJUCEWindow (HWND hwnd) noexcept
  62. {
  63. return GetWindowLongPtr (hwnd, GWLP_USERDATA) == getImprobableWindowNumber();
  64. }
  65. static void setAsJUCEWindow (HWND hwnd, bool isJuceWindow) noexcept
  66. {
  67. SetWindowLongPtr (hwnd, GWLP_USERDATA, isJuceWindow ? getImprobableWindowNumber() : 0);
  68. }
  69. private:
  70. static LONG_PTR getImprobableWindowNumber() noexcept
  71. {
  72. static LONG_PTR number = (LONG_PTR) Random::getSystemRandom().nextInt64();
  73. return number;
  74. }
  75. };
  76. //==============================================================================
  77. class DeviceChangeDetector : private Timer
  78. {
  79. public:
  80. DeviceChangeDetector (const wchar_t* const name)
  81. : messageWindow (name, (WNDPROC) deviceChangeEventCallback)
  82. {
  83. SetWindowLongPtr (messageWindow.getHWND(), GWLP_USERDATA, (LONG_PTR) this);
  84. }
  85. virtual ~DeviceChangeDetector() {}
  86. virtual void systemDeviceChanged() = 0;
  87. void triggerAsyncDeviceChangeCallback()
  88. {
  89. // We'll pause before sending a message, because on device removal, the OS hasn't always updated
  90. // its device lists correctly at this point. This also helps avoid repeated callbacks.
  91. startTimer (500);
  92. }
  93. private:
  94. HiddenMessageWindow messageWindow;
  95. static LRESULT CALLBACK deviceChangeEventCallback (HWND h, const UINT message,
  96. const WPARAM wParam, const LPARAM lParam)
  97. {
  98. if (message == WM_DEVICECHANGE
  99. && (wParam == 0x8000 /*DBT_DEVICEARRIVAL*/
  100. || wParam == 0x8004 /*DBT_DEVICEREMOVECOMPLETE*/
  101. || wParam == 0x0007 /*DBT_DEVNODES_CHANGED*/))
  102. {
  103. ((DeviceChangeDetector*) GetWindowLongPtr (h, GWLP_USERDATA))
  104. ->triggerAsyncDeviceChangeCallback();
  105. }
  106. return DefWindowProc (h, message, wParam, lParam);
  107. }
  108. void timerCallback() override
  109. {
  110. stopTimer();
  111. systemDeviceChanged();
  112. }
  113. };
  114. #endif // JUCE_WIN32_HIDDENMESSAGEWINDOW_H_INCLUDED