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.

133 lines
4.3KB

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