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.

106 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_ : 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. MSG& msg = *(MSG*) lParam;
  51. if (nCode == HC_ACTION && wParam == PM_REMOVE
  52. && HWNDComponentPeer::offerKeyMessageToJUCEWindow (msg))
  53. {
  54. zerostruct (msg);
  55. msg.message = WM_USER;
  56. return 0;
  57. }
  58. return CallNextHookEx (getSingleton()->keyboardHook, nCode, wParam, lParam);
  59. }
  60. HHOOK mouseWheelHook = SetWindowsHookEx (WH_MOUSE,
  61. mouseWheelHookCallback,
  62. (HINSTANCE) juce::Process::getCurrentModuleInstanceHandle(),
  63. GetCurrentThreadId());
  64. HHOOK keyboardHook = SetWindowsHookEx (WH_GETMESSAGE,
  65. keyboardHookCallback,
  66. (HINSTANCE) juce::Process::getCurrentModuleInstanceHandle(),
  67. GetCurrentThreadId());
  68. };
  69. auto WindowsHooks::getSingleton() -> std::shared_ptr<Hooks>
  70. {
  71. auto& weak = Hooks::weak;
  72. if (auto locked = weak.lock())
  73. return locked;
  74. auto strong = std::make_shared<Hooks>();
  75. weak = strong;
  76. return strong;
  77. }
  78. } // namespace juce::detail
  79. #endif