Runner: improve debugging experience with hooks

This commit is contained in:
yuyoyuppe 2020-06-18 14:27:20 +03:00
parent 7ed03c8b90
commit 2effbd0baf
No known key found for this signature in database
GPG Key ID: B240219D92C197D0
10 changed files with 83 additions and 25 deletions

View File

@ -120,6 +120,7 @@
<ClInclude Include="d2d_svg.h" /> <ClInclude Include="d2d_svg.h" />
<ClInclude Include="d2d_text.h" /> <ClInclude Include="d2d_text.h" />
<ClInclude Include="d2d_window.h" /> <ClInclude Include="d2d_window.h" />
<ClInclude Include="debug_control.h" />
<ClInclude Include="dpi_aware.h" /> <ClInclude Include="dpi_aware.h" />
<ClInclude Include="com_object_factory.h" /> <ClInclude Include="com_object_factory.h" />
<ClInclude Include="keyboard_layout.h" /> <ClInclude Include="keyboard_layout.h" />

View File

@ -117,6 +117,9 @@
<ClInclude Include="RestartManagement.h"> <ClInclude Include="RestartManagement.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="debug_control.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="d2d_svg.cpp"> <ClCompile Include="d2d_svg.cpp">

View File

@ -0,0 +1,4 @@
#pragma once
// Prevent system-wide input lagging while paused in the debugger
//#define DISABLE_LOWLEVEL_KBHOOK_WHEN_DEBUGGED

View File

@ -3,6 +3,7 @@
#include <exception> #include <exception>
#include <msclr\marshal.h> #include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h> #include <msclr\marshal_cppstd.h>
#include <common/debug_control.h>
using namespace interop; using namespace interop;
using namespace System::Runtime::InteropServices; using namespace System::Runtime::InteropServices;
@ -45,9 +46,9 @@ void KeyboardHook::DispatchProc()
// Release lock while callback is being invoked // Release lock while callback is being invoked
Monitor::Exit(queue); Monitor::Exit(queue);
keyboardEventCallback->Invoke(nextEv); keyboardEventCallback->Invoke(nextEv);
// Re-aquire lock // Re-aquire lock
Monitor::Enter(queue); Monitor::Enter(queue);
} }
@ -60,15 +61,23 @@ void KeyboardHook::Start()
hookProc = gcnew HookProcDelegate(this, &KeyboardHook::HookProc); hookProc = gcnew HookProcDelegate(this, &KeyboardHook::HookProc);
Process ^ curProcess = Process::GetCurrentProcess(); Process ^ curProcess = Process::GetCurrentProcess();
ProcessModule ^ curModule = curProcess->MainModule; ProcessModule ^ curModule = curProcess->MainModule;
// register low level hook procedure #if defined(DISABLE_LOWLEVEL_KBHOOK_WHEN_DEBUGGED)
hookHandle = SetWindowsHookEx( const bool hookDisabled = IsDebuggerPresent();
WH_KEYBOARD_LL, #else
(HOOKPROC)(void*)Marshal::GetFunctionPointerForDelegate(hookProc), const bool hookDisabled = false;
0, #endif
0); if (!hookDisabled)
if (hookHandle == nullptr)
{ {
throw std::exception("SetWindowsHookEx failed."); // register low level hook procedure
hookHandle = SetWindowsHookEx(
WH_KEYBOARD_LL,
(HOOKPROC)(void*)Marshal::GetFunctionPointerForDelegate(hookProc),
0,
0);
if (hookHandle == nullptr)
{
throw std::exception("SetWindowsHookEx failed.");
}
} }
kbEventDispatch->Start(); kbEventDispatch->Start();

View File

@ -1,6 +1,7 @@
#include "pch.h" #include "pch.h"
#include <common/settings_objects.h> #include <common/settings_objects.h>
#include <common/common.h> #include <common/common.h>
#include <common/debug_control.h>
#include <interface/powertoy_module_interface.h> #include <interface/powertoy_module_interface.h>
#include <interface/lowlevel_keyboard_event_data.h> #include <interface/lowlevel_keyboard_event_data.h>
#include <interface/win_hook_event_data.h> #include <interface/win_hook_event_data.h>
@ -78,11 +79,18 @@ public:
InitializeWinhookEventIds(); InitializeWinhookEventIds();
Trace::FancyZones::EnableFancyZones(true); Trace::FancyZones::EnableFancyZones(true);
m_app = MakeFancyZones(reinterpret_cast<HINSTANCE>(&__ImageBase), m_settings); m_app = MakeFancyZones(reinterpret_cast<HINSTANCE>(&__ImageBase), m_settings);
#if defined(DISABLE_LOWLEVEL_KBHOOK_WHEN_DEBUGGED)
s_llKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), NULL); const bool hook_disabled = IsDebuggerPresent();
if (!s_llKeyboardHook) #else
const bool hook_disabled = false;
#endif
if (!hook_disabled)
{ {
MessageBoxW(NULL, L"Cannot install keyboard listener.", L"PowerToys - FancyZones", MB_OK | MB_ICONERROR); s_llKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), NULL);
if (!s_llKeyboardHook)
{
MessageBoxW(NULL, L"Cannot install keyboard listener.", L"PowerToys - FancyZones", MB_OK | MB_ICONERROR);
}
} }
std::array<DWORD, 6> events_to_subscribe = { std::array<DWORD, 6> events_to_subscribe = {

View File

@ -1,5 +1,6 @@
#include "pch.h" #include "pch.h"
#include "SecondaryMouseButtonsHook.h" #include "SecondaryMouseButtonsHook.h"
#include <common/debug_control.h>
#pragma region public #pragma region public
@ -9,11 +10,23 @@ std::function<void()> SecondaryMouseButtonsHook::callback = {};
SecondaryMouseButtonsHook::SecondaryMouseButtonsHook(std::function<void()> extCallback) SecondaryMouseButtonsHook::SecondaryMouseButtonsHook(std::function<void()> extCallback)
{ {
callback = std::move(extCallback); callback = std::move(extCallback);
#if defined(DISABLE_LOWLEVEL_KBHOOK_WHEN_DEBUGGED)
if (IsDebuggerPresent())
{
return;
}
#endif
hHook = SetWindowsHookEx(WH_MOUSE_LL, SecondaryMouseButtonsProc, GetModuleHandle(NULL), 0); hHook = SetWindowsHookEx(WH_MOUSE_LL, SecondaryMouseButtonsProc, GetModuleHandle(NULL), 0);
} }
void SecondaryMouseButtonsHook::enable() void SecondaryMouseButtonsHook::enable()
{ {
#if defined(DISABLE_LOWLEVEL_KBHOOK_WHEN_DEBUGGED)
if (IsDebuggerPresent())
{
return;
}
#endif
if (!hHook) if (!hHook)
{ {
hHook = SetWindowsHookEx(WH_MOUSE_LL, SecondaryMouseButtonsProc, GetModuleHandle(NULL), 0); hHook = SetWindowsHookEx(WH_MOUSE_LL, SecondaryMouseButtonsProc, GetModuleHandle(NULL), 0);

View File

@ -1,6 +1,8 @@
#include "pch.h" #include "pch.h"
#include "ShiftKeyHook.h" #include "ShiftKeyHook.h"
#include <common/debug_control.h>
#pragma region public #pragma region public
HHOOK ShiftKeyHook::hHook = {}; HHOOK ShiftKeyHook::hHook = {};
@ -9,11 +11,23 @@ std::function<void(bool)> ShiftKeyHook::callback = {};
ShiftKeyHook::ShiftKeyHook(std::function<void(bool)> extCallback) ShiftKeyHook::ShiftKeyHook(std::function<void(bool)> extCallback)
{ {
callback = std::move(extCallback); callback = std::move(extCallback);
#if defined(DISABLE_LOWLEVEL_KBHOOK_WHEN_DEBUGGED)
if (IsDebuggerPresent())
{
return;
}
#endif
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, ShiftKeyHookProc, GetModuleHandle(NULL), 0); hHook = SetWindowsHookEx(WH_KEYBOARD_LL, ShiftKeyHookProc, GetModuleHandle(NULL), 0);
} }
void ShiftKeyHook::enable() void ShiftKeyHook::enable()
{ {
#if defined(DISABLE_LOWLEVEL_KBHOOK_WHEN_DEBUGGED)
if (IsDebuggerPresent())
{
return;
}
#endif
if (!hHook) if (!hHook)
{ {
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, ShiftKeyHookProc, GetModuleHandle(NULL), 0); hHook = SetWindowsHookEx(WH_KEYBOARD_LL, ShiftKeyHookProc, GetModuleHandle(NULL), 0);

View File

@ -12,6 +12,7 @@
#include <keyboardmanager/common/RemapShortcut.h> #include <keyboardmanager/common/RemapShortcut.h>
#include <keyboardmanager/common/KeyboardManagerConstants.h> #include <keyboardmanager/common/KeyboardManagerConstants.h>
#include <common/settings_helpers.h> #include <common/settings_helpers.h>
#include <common/debug_control.h>
#include <keyboardmanager/common/trace.h> #include <keyboardmanager/common/trace.h>
#include "KeyboardEventHandlers.h" #include "KeyboardEventHandlers.h"
#include "Input.h" #include "Input.h"
@ -298,11 +299,9 @@ public:
return CallNextHookEx(hook_handle_copy, nCode, wParam, lParam); return CallNextHookEx(hook_handle_copy, nCode, wParam, lParam);
} }
// Prevent system-wide input lagging while paused in the debugger
//#define DISABLE_LOWLEVEL_KBHOOK_WHEN_DEBUGGED
void start_lowlevel_keyboard_hook() void start_lowlevel_keyboard_hook()
{ {
#if defined(_DEBUG) && defined(DISABLE_LOWLEVEL_KBHOOK_WHEN_DEBUGGED) #if defined(DISABLE_LOWLEVEL_KBHOOK_WHEN_DEBUGGED)
if (IsDebuggerPresent()) if (IsDebuggerPresent())
{ {
return; return;

View File

@ -6,6 +6,7 @@
#include <common/common.h> #include <common/common.h>
#include <common/settings_objects.h> #include <common/settings_objects.h>
#include <common/debug_control.h>
extern "C" IMAGE_DOS_HEADER __ImageBase; extern "C" IMAGE_DOS_HEADER __ImageBase;
@ -134,10 +135,18 @@ void OverlayWindow::enable()
winkey_popup->set_theme(theme.value); winkey_popup->set_theme(theme.value);
target_state = std::make_unique<TargetState>(pressTime.value); target_state = std::make_unique<TargetState>(pressTime.value);
winkey_popup->initialize(); winkey_popup->initialize();
hook_handle = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), NULL); #if defined(DISABLE_LOWLEVEL_KBHOOK_WHEN_DEBUGGED)
if (!hook_handle) const bool hook_disabled = IsDebuggerPresent();
#else
const bool hook_disabled = false;
#endif
if (!hook_disabled)
{ {
MessageBoxW(NULL, L"Cannot install keyboard listener.", L"PowerToys - Shortcut Guide", MB_OK | MB_ICONERROR); hook_handle = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), NULL);
if (!hook_handle)
{
MessageBoxW(NULL, L"Cannot install keyboard listener.", L"PowerToys - Shortcut Guide", MB_OK | MB_ICONERROR);
}
} }
} }
_enabled = true; _enabled = true;
@ -195,7 +204,7 @@ intptr_t OverlayWindow::signal_event(LowlevelKeyboardEvent* event)
event->wParam == WM_SYSKEYUP) event->wParam == WM_SYSKEYUP)
{ {
bool suppress = target_state->signal_event(event->lParam->vkCode, bool suppress = target_state->signal_event(event->lParam->vkCode,
event->wParam == WM_KEYDOWN || event->wParam == WM_SYSKEYDOWN); event->wParam == WM_KEYDOWN || event->wParam == WM_SYSKEYDOWN);
return suppress ? 1 : 0; return suppress ? 1 : 0;
} }
else else

View File

@ -1,6 +1,7 @@
#include "pch.h" #include "pch.h"
#include "lowlevel_keyboard_event.h" #include "lowlevel_keyboard_event.h"
#include "powertoys_events.h" #include "powertoys_events.h"
#include <common/debug_control.h>
namespace namespace
{ {
@ -22,12 +23,9 @@ namespace
} }
} }
// Prevent system-wide input lagging while paused in the debugger
//#define DISABLE_LOWLEVEL_KBHOOK_WHEN_DEBUGGED
void start_lowlevel_keyboard_hook() void start_lowlevel_keyboard_hook()
{ {
#if defined(_DEBUG) && defined(DISABLE_LOWLEVEL_KBHOOK_WHEN_DEBUGGED) #if defined(DISABLE_LOWLEVEL_KBHOOK_WHEN_DEBUGGED)
if (IsDebuggerPresent()) if (IsDebuggerPresent())
{ {
return; return;