From 06d5080b42002773e15f825d5394fc9d02bea4ed Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Fri, 23 Feb 2018 21:31:05 -0500 Subject: [PATCH] reimplement immune keys --- src/lib/platform/MSWindowsDesks.cpp | 19 --------------- src/lib/platform/MSWindowsHook.cpp | 38 ++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/lib/platform/MSWindowsDesks.cpp b/src/lib/platform/MSWindowsDesks.cpp index 8a4f3b7c..b43a218c 100644 --- a/src/lib/platform/MSWindowsDesks.cpp +++ b/src/lib/platform/MSWindowsDesks.cpp @@ -20,7 +20,6 @@ #include "platform/MSWindowsDesks.h" #include "platform/MSWindowsScreen.h" -#include "platform/ImmuneKeysReader.h" #include "barrier/IScreenSaver.h" #include "barrier/XScreen.h" #include "mt/Lock.h" @@ -89,17 +88,6 @@ // enable; #define BARRIER_MSG_FAKE_INPUT BARRIER_HOOK_LAST_MSG + 12 -static const std::string ImmuneKeysPath = ArchFileWindows().getProfileDirectory() + "\\ImmuneKeys.txt"; - -static std::vector immune_keys_list() -{ - std::vector keys; - std::string badLine; - if (!ImmuneKeysReader::get_list(ImmuneKeysPath.c_str(), keys, badLine)) - LOG((CLOG_ERR "Reading immune keys stopped at: %s", badLine.c_str())); - return keys; -} - // // MSWindowsDesks // @@ -126,8 +114,6 @@ MSWindowsDesks::MSWindowsDesks( m_events(events), m_stopOnDeskSwitch(stopOnDeskSwitch) { - LOG((CLOG_DEBUG "Immune Keys Path: %s", ImmuneKeysPath.c_str())); - m_cursor = createBlankCursor(); m_deskClass = createDeskWindowClass(m_isPrimary); m_keyLayout = GetKeyboardLayout(GetCurrentThreadId()); @@ -657,11 +643,6 @@ MSWindowsDesks::deskThread(void* vdesk) MSWindowsHook::uninstallScreenSaver(); MSWindowsHook::installScreenSaver(); } - // populate immune keys list in the DLL's shared memory - // before the hooks are activated - auto list = immune_keys_list(); - LOG((CLOG_DEBUG "Found %u immune keys", list.size())); - //m_setImmuneKeys(list.data(), list.size()); if (!MSWindowsHook::install()) { // we won't work on this desk LOG((CLOG_DEBUG "Cannot hook on this desk")); diff --git a/src/lib/platform/MSWindowsHook.cpp b/src/lib/platform/MSWindowsHook.cpp index a27ef981..929888e4 100644 --- a/src/lib/platform/MSWindowsHook.cpp +++ b/src/lib/platform/MSWindowsHook.cpp @@ -19,6 +19,7 @@ #include "platform/MSWindowsHook.h" #include "platform/MSWindowsHookResource.h" +#include "platform/ImmuneKeysReader.h" #include "barrier/protocol_types.h" #include "barrier/XScreen.h" #include "base/Log.h" @@ -49,6 +50,28 @@ static LPARAM g_deadLParam = 0; static BYTE g_deadKeyState[256] = { 0 }; static BYTE g_keyState[256] = { 0 }; static bool g_fakeServerInput = false; +static std::vector g_immuneKeys; + +static const std::string ImmuneKeysPath = ArchFileWindows().getProfileDirectory() + "\\ImmuneKeys.txt"; + +static std::vector immune_keys_list() +{ + std::vector keys; + std::string badLine; + if (!ImmuneKeysReader::get_list(ImmuneKeysPath.c_str(), keys, badLine)) + LOG((CLOG_ERR "Reading immune keys stopped at: %s", badLine.c_str())); + return keys; +} + +inline static +bool is_immune_key(DWORD target) +{ + for (auto key : g_immuneKeys) { + if (key == target) + return true; + } + return false; +} void MSWindowsHook::setSides(UInt32 sides) @@ -378,9 +401,11 @@ static LRESULT CALLBACK keyboardLLHook(int code, WPARAM wParam, LPARAM lParam) { - if (code >= 0) { - // decode the message - KBDLLHOOKSTRUCT* info = reinterpret_cast(lParam); + // decode the message + KBDLLHOOKSTRUCT* info = reinterpret_cast(lParam); + + // do not filter non-action events nor immune keys + if (code == HC_ACTION && !is_immune_key(info->vkCode)) { WPARAM wParam = info->vkCode; LPARAM lParam = 1; // repeat code lParam |= (info->scanCode << 16); // scan code @@ -510,7 +535,8 @@ static LRESULT CALLBACK mouseLLHook(int code, WPARAM wParam, LPARAM lParam) { - if (code >= 0) { + // do not filter non-action events + if (code == HC_ACTION) { // decode the message MSLLHOOKSTRUCT* info = reinterpret_cast(lParam); SInt32 x = static_cast(info->pt.x); @@ -536,6 +562,10 @@ MSWindowsHook::install() // reset fake input flag g_fakeServerInput = false; + // setup immune keys + g_immuneKeys = immune_keys_list(); + LOG((CLOG_DEBUG "Found %u immune keys in %s", g_immuneKeys.size(), ImmuneKeysPath.c_str())); + #if NO_GRAB_KEYBOARD // we only need the mouse hook if (!g_hkMouse.set(WH_MOUSE_LL, &mouseLLHook, NULL, 0))