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.

juce_win32_HiddenMessageWindow.h 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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. //==============================================================================
  20. class HiddenMessageWindow
  21. {
  22. public:
  23. HiddenMessageWindow (const TCHAR* const messageWindowName, WNDPROC wndProc)
  24. {
  25. String className ("JUCE_");
  26. className << String::toHexString (Time::getHighResolutionTicks());
  27. HMODULE moduleHandle = (HMODULE) Process::getCurrentModuleInstanceHandle();
  28. WNDCLASSEX wc = {};
  29. wc.cbSize = sizeof (wc);
  30. wc.lpfnWndProc = wndProc;
  31. wc.cbWndExtra = 4;
  32. wc.hInstance = moduleHandle;
  33. wc.lpszClassName = className.toWideCharPointer();
  34. atom = RegisterClassEx (&wc);
  35. jassert (atom != 0);
  36. hwnd = CreateWindow (getClassNameFromAtom(), messageWindowName,
  37. 0, 0, 0, 0, 0,
  38. nullptr, nullptr, moduleHandle, nullptr);
  39. jassert (hwnd != nullptr);
  40. }
  41. ~HiddenMessageWindow()
  42. {
  43. DestroyWindow (hwnd);
  44. UnregisterClass (getClassNameFromAtom(), nullptr);
  45. }
  46. inline HWND getHWND() const noexcept { return hwnd; }
  47. private:
  48. ATOM atom;
  49. HWND hwnd;
  50. LPCTSTR getClassNameFromAtom() noexcept { return (LPCTSTR) (pointer_sized_uint) atom; }
  51. };
  52. //==============================================================================
  53. class JuceWindowIdentifier
  54. {
  55. public:
  56. static bool isJUCEWindow (HWND hwnd) noexcept
  57. {
  58. return GetWindowLongPtr (hwnd, GWLP_USERDATA) == getImprobableWindowNumber();
  59. }
  60. static void setAsJUCEWindow (HWND hwnd, bool isJuceWindow) noexcept
  61. {
  62. SetWindowLongPtr (hwnd, GWLP_USERDATA, isJuceWindow ? getImprobableWindowNumber() : 0);
  63. }
  64. private:
  65. static LONG_PTR getImprobableWindowNumber() noexcept
  66. {
  67. static auto number = (LONG_PTR) Random().nextInt64();
  68. return number;
  69. }
  70. };
  71. //==============================================================================
  72. class DeviceChangeDetector : private Timer
  73. {
  74. public:
  75. DeviceChangeDetector (const wchar_t* const name)
  76. : messageWindow (name, (WNDPROC) deviceChangeEventCallback)
  77. {
  78. SetWindowLongPtr (messageWindow.getHWND(), GWLP_USERDATA, (LONG_PTR) this);
  79. }
  80. virtual ~DeviceChangeDetector() {}
  81. virtual void systemDeviceChanged() = 0;
  82. void triggerAsyncDeviceChangeCallback()
  83. {
  84. // We'll pause before sending a message, because on device removal, the OS hasn't always updated
  85. // its device lists correctly at this point. This also helps avoid repeated callbacks.
  86. startTimer (500);
  87. }
  88. private:
  89. HiddenMessageWindow messageWindow;
  90. static LRESULT CALLBACK deviceChangeEventCallback (HWND h, const UINT message,
  91. const WPARAM wParam, const LPARAM lParam)
  92. {
  93. if (message == WM_DEVICECHANGE
  94. && (wParam == 0x8000 /*DBT_DEVICEARRIVAL*/
  95. || wParam == 0x8004 /*DBT_DEVICEREMOVECOMPLETE*/
  96. || wParam == 0x0007 /*DBT_DEVNODES_CHANGED*/))
  97. {
  98. ((DeviceChangeDetector*) GetWindowLongPtr (h, GWLP_USERDATA))
  99. ->triggerAsyncDeviceChangeCallback();
  100. }
  101. return DefWindowProc (h, message, wParam, lParam);
  102. }
  103. void timerCallback() override
  104. {
  105. stopTimer();
  106. systemDeviceChanged();
  107. }
  108. };
  109. } // namespace juce