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.

109 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. // This function is in juce_win32_Windowing.cpp
  22. bool offerKeyMessageToJUCEWindow (MSG&);
  23. class WindowsHooks::Hooks
  24. {
  25. public:
  26. Hooks() = default;
  27. ~Hooks()
  28. {
  29. if (mouseWheelHook != nullptr)
  30. UnhookWindowsHookEx (mouseWheelHook);
  31. if (keyboardHook != nullptr)
  32. UnhookWindowsHookEx (keyboardHook);
  33. }
  34. static inline std::weak_ptr<Hooks> weak;
  35. private:
  36. static LRESULT CALLBACK mouseWheelHookCallback (int nCode, WPARAM wParam, LPARAM lParam)
  37. {
  38. if (nCode >= 0 && wParam == WM_MOUSEWHEEL)
  39. {
  40. // using a local copy of this struct to support old mingw libraries
  41. struct MOUSEHOOKSTRUCTEX_ : public MOUSEHOOKSTRUCT { DWORD mouseData; };
  42. auto& hs = *(MOUSEHOOKSTRUCTEX_*) lParam;
  43. if (auto* comp = Desktop::getInstance().findComponentAt ({ hs.pt.x, hs.pt.y }))
  44. if (comp->getWindowHandle() != nullptr)
  45. return PostMessage ((HWND) comp->getWindowHandle(), WM_MOUSEWHEEL,
  46. hs.mouseData & 0xffff0000, (hs.pt.x & 0xffff) | (hs.pt.y << 16));
  47. }
  48. return CallNextHookEx (getSingleton()->mouseWheelHook, nCode, wParam, lParam);
  49. }
  50. static LRESULT CALLBACK keyboardHookCallback (int nCode, WPARAM wParam, LPARAM lParam)
  51. {
  52. MSG& msg = *(MSG*) lParam;
  53. if (nCode == HC_ACTION && wParam == PM_REMOVE
  54. && offerKeyMessageToJUCEWindow (msg))
  55. {
  56. zerostruct (msg);
  57. msg.message = WM_USER;
  58. return 0;
  59. }
  60. return CallNextHookEx (getSingleton()->keyboardHook, nCode, wParam, lParam);
  61. }
  62. HHOOK mouseWheelHook = SetWindowsHookEx (WH_MOUSE,
  63. mouseWheelHookCallback,
  64. (HINSTANCE) juce::Process::getCurrentModuleInstanceHandle(),
  65. GetCurrentThreadId());
  66. HHOOK keyboardHook = SetWindowsHookEx (WH_GETMESSAGE,
  67. keyboardHookCallback,
  68. (HINSTANCE) juce::Process::getCurrentModuleInstanceHandle(),
  69. GetCurrentThreadId());
  70. };
  71. auto WindowsHooks::getSingleton() -> std::shared_ptr<Hooks>
  72. {
  73. auto& weak = Hooks::weak;
  74. if (auto locked = weak.lock())
  75. return locked;
  76. auto strong = std::make_shared<Hooks>();
  77. weak = strong;
  78. return strong;
  79. }
  80. } // namespace juce::detail
  81. #endif