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.

115 lines
3.6KB

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