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.

105 lines
3.4KB

  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. if (comp->getWindowHandle() != nullptr)
  43. return PostMessage ((HWND) comp->getWindowHandle(), WM_MOUSEWHEEL,
  44. hs.mouseData & 0xffff0000, (hs.pt.x & 0xffff) | (hs.pt.y << 16));
  45. }
  46. return CallNextHookEx (getSingleton()->mouseWheelHook, nCode, wParam, lParam);
  47. }
  48. static LRESULT CALLBACK keyboardHookCallback (int nCode, WPARAM wParam, LPARAM lParam)
  49. {
  50. auto& msg = *reinterpret_cast<MSG*> (lParam);
  51. if (nCode == HC_ACTION && wParam == PM_REMOVE && HWNDComponentPeer::offerKeyMessageToJUCEWindow (msg))
  52. {
  53. msg = {};
  54. msg.message = WM_USER;
  55. return 0;
  56. }
  57. return CallNextHookEx (getSingleton()->keyboardHook, nCode, wParam, lParam);
  58. }
  59. HHOOK mouseWheelHook = SetWindowsHookEx (WH_MOUSE,
  60. mouseWheelHookCallback,
  61. (HINSTANCE) juce::Process::getCurrentModuleInstanceHandle(),
  62. GetCurrentThreadId());
  63. HHOOK keyboardHook = SetWindowsHookEx (WH_GETMESSAGE,
  64. keyboardHookCallback,
  65. (HINSTANCE) juce::Process::getCurrentModuleInstanceHandle(),
  66. GetCurrentThreadId());
  67. };
  68. auto WindowsHooks::getSingleton() -> std::shared_ptr<Hooks>
  69. {
  70. auto& weak = Hooks::weak;
  71. if (auto locked = weak.lock())
  72. return locked;
  73. auto strong = std::make_shared<Hooks>();
  74. weak = strong;
  75. return strong;
  76. }
  77. } // namespace juce::detail
  78. #endif