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.

110 lines
3.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #if JUCE_WINDOWS
  19. namespace juce::detail
  20. {
  21. class WindowsHooks::Hooks
  22. {
  23. public:
  24. Hooks() = default;
  25. ~Hooks()
  26. {
  27. if (mouseWheelHook != nullptr)
  28. UnhookWindowsHookEx (mouseWheelHook);
  29. if (keyboardHook != nullptr)
  30. UnhookWindowsHookEx (keyboardHook);
  31. }
  32. static inline std::weak_ptr<Hooks> weak;
  33. private:
  34. static LRESULT CALLBACK mouseWheelHookCallback (int nCode, WPARAM wParam, LPARAM lParam)
  35. {
  36. if (nCode >= 0 && wParam == WM_MOUSEWHEEL)
  37. {
  38. // using a local copy of this struct to support old mingw libraries
  39. struct MOUSEHOOKSTRUCTEX_ final : public MOUSEHOOKSTRUCT { DWORD mouseData; };
  40. auto& hs = *(MOUSEHOOKSTRUCTEX_*) lParam;
  41. if (auto* comp = Desktop::getInstance().findComponentAt ({ hs.pt.x, hs.pt.y }))
  42. {
  43. if (auto* target = static_cast<HWND> (comp->getWindowHandle()))
  44. {
  45. const ScopedThreadDPIAwarenessSetter scope { target };
  46. return PostMessage (target, WM_MOUSEWHEEL,
  47. hs.mouseData & 0xffff0000, MAKELPARAM (hs.pt.x, hs.pt.y));
  48. }
  49. }
  50. }
  51. return CallNextHookEx (getSingleton()->mouseWheelHook, nCode, wParam, lParam);
  52. }
  53. static LRESULT CALLBACK keyboardHookCallback (int nCode, WPARAM wParam, LPARAM lParam)
  54. {
  55. auto& msg = *reinterpret_cast<MSG*> (lParam);
  56. if (nCode == HC_ACTION && wParam == PM_REMOVE && HWNDComponentPeer::offerKeyMessageToJUCEWindow (msg))
  57. {
  58. msg = {};
  59. msg.message = WM_USER;
  60. return 0;
  61. }
  62. return CallNextHookEx (getSingleton()->keyboardHook, nCode, wParam, lParam);
  63. }
  64. HHOOK mouseWheelHook = SetWindowsHookEx (WH_MOUSE,
  65. mouseWheelHookCallback,
  66. (HINSTANCE) juce::Process::getCurrentModuleInstanceHandle(),
  67. GetCurrentThreadId());
  68. HHOOK keyboardHook = SetWindowsHookEx (WH_GETMESSAGE,
  69. keyboardHookCallback,
  70. (HINSTANCE) juce::Process::getCurrentModuleInstanceHandle(),
  71. GetCurrentThreadId());
  72. };
  73. auto WindowsHooks::getSingleton() -> std::shared_ptr<Hooks>
  74. {
  75. auto& weak = Hooks::weak;
  76. if (auto locked = weak.lock())
  77. return locked;
  78. auto strong = std::make_shared<Hooks>();
  79. weak = strong;
  80. return strong;
  81. }
  82. } // namespace juce::detail
  83. #endif