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.

108 lines
3.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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
  20. {
  21. // This function is in juce_win32_Windowing.cpp
  22. extern bool offerKeyMessageToJUCEWindow (MSG&);
  23. static HHOOK mouseWheelHook = nullptr, keyboardHook = nullptr;
  24. static int numHookUsers = 0;
  25. struct WindowsHooks
  26. {
  27. WindowsHooks()
  28. {
  29. if (numHookUsers++ == 0)
  30. {
  31. mouseWheelHook = SetWindowsHookEx (WH_MOUSE, mouseWheelHookCallback,
  32. (HINSTANCE) juce::Process::getCurrentModuleInstanceHandle(),
  33. GetCurrentThreadId());
  34. keyboardHook = SetWindowsHookEx (WH_GETMESSAGE, keyboardHookCallback,
  35. (HINSTANCE) juce::Process::getCurrentModuleInstanceHandle(),
  36. GetCurrentThreadId());
  37. }
  38. }
  39. ~WindowsHooks()
  40. {
  41. if (--numHookUsers == 0)
  42. {
  43. if (mouseWheelHook != nullptr)
  44. {
  45. UnhookWindowsHookEx (mouseWheelHook);
  46. mouseWheelHook = nullptr;
  47. }
  48. if (keyboardHook != nullptr)
  49. {
  50. UnhookWindowsHookEx (keyboardHook);
  51. keyboardHook = nullptr;
  52. }
  53. }
  54. }
  55. static LRESULT CALLBACK mouseWheelHookCallback (int nCode, WPARAM wParam, LPARAM lParam)
  56. {
  57. if (nCode >= 0 && wParam == WM_MOUSEWHEEL)
  58. {
  59. // using a local copy of this struct to support old mingw libraries
  60. struct MOUSEHOOKSTRUCTEX_ : public MOUSEHOOKSTRUCT { DWORD mouseData; };
  61. auto& hs = *(MOUSEHOOKSTRUCTEX_*) lParam;
  62. if (auto* comp = Desktop::getInstance().findComponentAt ({ hs.pt.x, hs.pt.y }))
  63. if (comp->getWindowHandle() != nullptr)
  64. return PostMessage ((HWND) comp->getWindowHandle(), WM_MOUSEWHEEL,
  65. hs.mouseData & 0xffff0000, (hs.pt.x & 0xffff) | (hs.pt.y << 16));
  66. }
  67. return CallNextHookEx (mouseWheelHook, nCode, wParam, lParam);
  68. }
  69. static LRESULT CALLBACK keyboardHookCallback (int nCode, WPARAM wParam, LPARAM lParam)
  70. {
  71. MSG& msg = *(MSG*) lParam;
  72. if (nCode == HC_ACTION && wParam == PM_REMOVE
  73. && offerKeyMessageToJUCEWindow (msg))
  74. {
  75. zerostruct (msg);
  76. msg.message = WM_USER;
  77. return 1;
  78. }
  79. return CallNextHookEx (keyboardHook, nCode, wParam, lParam);
  80. }
  81. };
  82. } // namespace juce
  83. #endif