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.

111 lines
3.7KB

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