remove high-level k/m hooks and temporarily remove immune keys

implementation in synwinhk
This commit is contained in:
walker0643 2018-02-23 15:06:27 -05:00
parent 4571f4cc6e
commit a71779647d
2 changed files with 52 additions and 192 deletions

View File

@ -16,6 +16,54 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
/* REMOVED ImmuneKeys for migration of synwinhk out of DLL
// because all of our global data is shared between processes,
// allocating shared data on-the-fly is tricky. therefore let's
// store all of our immune keys in a pre-allocated array. the
// downside here is that we have to pick a maximum number of
// immune keys to store. who would ever want barrier to ignore
// more than 32 keys on their keyboard??
struct ImmuneKeys
{
static const std::size_t MaxKeys = 32;
DWORD list[MaxKeys];
std::size_t count;
};
static ImmuneKeys g_immuneKeys{ {0}, 0 };
inline static
bool is_immune_key(DWORD target)
{
for (std::size_t idx = 0; idx < g_immuneKeys.count; ++idx) {
if (g_immuneKeys.list[idx] == target)
return true;
}
return false;
}
// allow all immune keys to pass without filtering
if (is_immune_key(static_cast<DWORD>(wParam)))
return false;
// do not call this while the hooks are active!
void
setImmuneKeys(const DWORD *list, std::size_t size)
{
if (size > ImmuneKeys::MaxKeys)
size = ImmuneKeys::MaxKeys;
g_immuneKeys.count = size;
if (size > 0) {
for (std::size_t idx = 0; idx < size; ++idx)
g_immuneKeys.list[idx] = list[idx];
}
}
*/
#include "synwinhk/synwinhk.h" #include "synwinhk/synwinhk.h"
#include "barrier/protocol_types.h" #include "barrier/protocol_types.h"
@ -23,18 +71,13 @@
#include <zmouse.h> #include <zmouse.h>
#include <tchar.h> #include <tchar.h>
#if _MSC_VER >= 1400
// VS2005 hack - we don't use assert here because we don't want to link with the CRT.
#undef assert #undef assert
#if _DEBUG #if _DEBUG
#define assert(_X_) if (!(_X_)) __debugbreak() #define assert(_X_) if (!(_X_)) __debugbreak()
#else #else
#define assert(_X_) __noop() #define assert(_X_) __noop()
#endif #endif
// VS2005 is a bit more smart than VC6 and optimize simple copy loop to
// intrinsic memcpy.
#pragma function(memcpy) #pragma function(memcpy)
#endif
// //
// debugging compile flag. when not zero the server doesn't grab // debugging compile flag. when not zero the server doesn't grab
@ -44,12 +87,6 @@
// //
#define NO_GRAB_KEYBOARD 0 #define NO_GRAB_KEYBOARD 0
//
// debugging compile flag. when not zero the server will not
// install low level hooks.
//
#define NO_LOWLEVEL_HOOKS 0
// //
// extra mouse wheel stuff // extra mouse wheel stuff
// //
@ -67,37 +104,6 @@ typedef struct tagMOUSEHOOKSTRUCTWin2000 {
DWORD mouseData; DWORD mouseData;
} MOUSEHOOKSTRUCTWin2000; } MOUSEHOOKSTRUCTWin2000;
#if !defined(SM_MOUSEWHEELPRESENT)
#define SM_MOUSEWHEELPRESENT 75
#endif
// X button stuff
#if !defined(WM_XBUTTONDOWN)
#define WM_XBUTTONDOWN 0x020B
#define WM_XBUTTONUP 0x020C
#define WM_XBUTTONDBLCLK 0x020D
#define WM_NCXBUTTONDOWN 0x00AB
#define WM_NCXBUTTONUP 0x00AC
#define WM_NCXBUTTONDBLCLK 0x00AD
#define MOUSEEVENTF_XDOWN 0x0080
#define MOUSEEVENTF_XUP 0x0100
#define XBUTTON1 0x0001
#define XBUTTON2 0x0002
#endif
// because all of our global data is shared between processes,
// allocating shared data on-the-fly is tricky. therefore let's
// store all of our immune keys in a pre-allocated array. the
// downside here is that we have to pick a maximum number of
// immune keys to store. who would ever want barrier to ignore
// more than 32 keys on their keyboard??
struct ImmuneKeys
{
static const std::size_t MaxKeys = 32;
DWORD list[MaxKeys];
std::size_t count;
};
// //
// globals // globals
// //
@ -113,8 +119,6 @@ static DWORD g_processID = 0;
static EWheelSupport g_wheelSupport = kWheelNone; static EWheelSupport g_wheelSupport = kWheelNone;
static UINT g_wmMouseWheel = 0; static UINT g_wmMouseWheel = 0;
static DWORD g_threadID = 0; static DWORD g_threadID = 0;
static HHOOK g_keyboard = NULL;
static HHOOK g_mouse = NULL;
static HHOOK g_getMessage = NULL; static HHOOK g_getMessage = NULL;
static HHOOK g_keyboardLL = NULL; static HHOOK g_keyboardLL = NULL;
static HHOOK g_mouseLL = NULL; static HHOOK g_mouseLL = NULL;
@ -133,7 +137,6 @@ static BYTE g_deadKeyState[256] = { 0 };
static BYTE g_keyState[256] = { 0 }; static BYTE g_keyState[256] = { 0 };
static DWORD g_hookThread = 0; static DWORD g_hookThread = 0;
static bool g_fakeInput = false; static bool g_fakeInput = false;
static ImmuneKeys g_immuneKeys{ {0}, 0 };
#if defined(_MSC_VER) #if defined(_MSC_VER)
#pragma data_seg() #pragma data_seg()
@ -199,7 +202,7 @@ keyboardGetState(BYTE keys[256], DWORD vkCode, bool kf_up)
static static
bool bool
doKeyboardHookHandler(WPARAM wParam, LPARAM lParam) keyboardHookHandler(WPARAM wParam, LPARAM lParam)
{ {
DWORD vkCode = static_cast<DWORD>(wParam); DWORD vkCode = static_cast<DWORD>(wParam);
bool kf_up = (lParam & (KF_UP << 16)) != 0; bool kf_up = (lParam & (KF_UP << 16)) != 0;
@ -457,31 +460,11 @@ doKeyboardHookHandler(WPARAM wParam, LPARAM lParam)
return false; return false;
} }
inline static
bool is_immune_key(DWORD target)
{
for (std::size_t idx = 0; idx < g_immuneKeys.count; ++idx) {
if (g_immuneKeys.list[idx] == target)
return true;
}
return false;
}
static
bool
keyboardHookHandler(WPARAM wParam, LPARAM lParam)
{
// allow all immune keys to pass without filtering
if (is_immune_key(static_cast<DWORD>(wParam)))
return false;
return doKeyboardHookHandler(wParam, lParam);
}
#endif #endif
static static
bool bool
doMouseHookHandler(WPARAM wParam, SInt32 x, SInt32 y, SInt32 data) mouseHookHandler(WPARAM wParam, SInt32 x, SInt32 y, SInt32 data)
{ {
switch (wParam) { switch (wParam) {
case WM_LBUTTONDOWN: case WM_LBUTTONDOWN:
@ -581,74 +564,6 @@ doMouseHookHandler(WPARAM wParam, SInt32 x, SInt32 y, SInt32 data)
return false; return false;
} }
static
bool
mouseHookHandler(WPARAM wParam, SInt32 x, SInt32 y, SInt32 data)
{
return doMouseHookHandler(wParam, x, y, data);
}
#if !NO_GRAB_KEYBOARD
static
LRESULT CALLBACK
keyboardHook(int code, WPARAM wParam, LPARAM lParam)
{
if (code >= 0) {
// handle the message
if (keyboardHookHandler(wParam, lParam)) {
return 1;
}
}
return CallNextHookEx(g_keyboard, code, wParam, lParam);
}
#endif
static
LRESULT CALLBACK
mouseHook(int code, WPARAM wParam, LPARAM lParam)
{
if (code >= 0) {
// decode message
const MOUSEHOOKSTRUCT* info = (const MOUSEHOOKSTRUCT*)lParam;
SInt32 x = (SInt32)info->pt.x;
SInt32 y = (SInt32)info->pt.y;
SInt32 w = 0;
if (wParam == WM_MOUSEWHEEL) {
// win2k and other systems supporting WM_MOUSEWHEEL in
// the mouse hook are gratuitously different (and poorly
// documented). if a low-level mouse hook is in place
// it should capture these events so we'll never see
// them.
switch (g_wheelSupport) {
case kWheelModern:
w = static_cast<SInt16>(LOWORD(info->dwExtraInfo));
break;
case kWheelWin2000: {
const MOUSEHOOKSTRUCTWin2000* info2k =
(const MOUSEHOOKSTRUCTWin2000*)lParam;
w = static_cast<SInt16>(HIWORD(info2k->mouseData));
break;
}
default:
break;
}
}
// handle the message. note that we don't handle X buttons
// here. that's okay because they're only supported on
// win2k and winxp and up and on those platforms we'll get
// get the mouse events through the low level hook.
if (mouseHookHandler(wParam, x, y, w)) {
return 1;
}
}
return CallNextHookEx(g_mouse, code, wParam, lParam);
}
static static
LRESULT CALLBACK LRESULT CALLBACK
getMessageHook(int code, WPARAM wParam, LPARAM lParam) getMessageHook(int code, WPARAM wParam, LPARAM lParam)
@ -681,8 +596,6 @@ getMessageHook(int code, WPARAM wParam, LPARAM lParam)
return CallNextHookEx(g_getMessage, code, wParam, lParam); return CallNextHookEx(g_getMessage, code, wParam, lParam);
} }
#if (_WIN32_WINNT >= 0x0400) && defined(_MSC_VER) && !NO_LOWLEVEL_HOOKS
// //
// low-level keyboard hook -- this allows us to capture and handle // low-level keyboard hook -- this allows us to capture and handle
// alt+tab, alt+esc, ctrl+esc, and windows key hot keys. on the down // alt+tab, alt+esc, ctrl+esc, and windows key hot keys. on the down
@ -748,8 +661,6 @@ mouseLLHook(int code, WPARAM wParam, LPARAM lParam)
return CallNextHookEx(g_mouseLL, code, wParam, lParam); return CallNextHookEx(g_mouseLL, code, wParam, lParam);
} }
#endif
static static
EWheelSupport EWheelSupport
getWheelSupport() getWheelSupport()
@ -922,8 +833,6 @@ init(DWORD threadID)
g_processID = GetCurrentProcessId(); g_processID = GetCurrentProcessId();
g_wheelSupport = kWheelNone; g_wheelSupport = kWheelNone;
g_threadID = 0; g_threadID = 0;
g_keyboard = NULL;
g_mouse = NULL;
g_getMessage = NULL; g_getMessage = NULL;
g_keyboardLL = NULL; g_keyboardLL = NULL;
g_mouseLL = NULL; g_mouseLL = NULL;
@ -962,8 +871,6 @@ EHookResult
install() install()
{ {
assert(g_hinstance != NULL); assert(g_hinstance != NULL);
assert(g_keyboard == NULL);
assert(g_mouse == NULL);
assert(g_getMessage == NULL || g_screenSaver); assert(g_getMessage == NULL || g_screenSaver);
// must be initialized // must be initialized
@ -990,7 +897,6 @@ install()
} }
// install low-level hooks. we require that they both get installed. // install low-level hooks. we require that they both get installed.
#if (_WIN32_WINNT >= 0x0400) && defined(_MSC_VER) && !NO_LOWLEVEL_HOOKS
g_mouseLL = SetWindowsHookEx(WH_MOUSE_LL, g_mouseLL = SetWindowsHookEx(WH_MOUSE_LL,
&mouseLLHook, &mouseLLHook,
g_hinstance, g_hinstance,
@ -1011,30 +917,13 @@ install()
} }
} }
#endif #endif
#endif
// install regular hooks
if (g_mouseLL == NULL) {
g_mouse = SetWindowsHookEx(WH_MOUSE,
&mouseHook,
g_hinstance,
0);
}
#if !NO_GRAB_KEYBOARD
if (g_keyboardLL == NULL) {
g_keyboard = SetWindowsHookEx(WH_KEYBOARD,
&keyboardHook,
g_hinstance,
0);
}
#endif
// check that we got all the hooks we wanted // check that we got all the hooks we wanted
if ((g_getMessage == NULL && g_wheelSupport == kWheelOld) || if ((g_getMessage == NULL && g_wheelSupport == kWheelOld) ||
#if !NO_GRAB_KEYBOARD #if !NO_GRAB_KEYBOARD
(g_keyboardLL == NULL && g_keyboard == NULL) || (g_keyboardLL == NULL) ||
#endif #endif
(g_mouseLL == NULL && g_mouse == NULL)) { (g_mouseLL == NULL)) {
uninstall(); uninstall();
return kHOOK_FAILED; return kHOOK_FAILED;
} }
@ -1065,14 +954,6 @@ uninstall(void)
UnhookWindowsHookEx(g_mouseLL); UnhookWindowsHookEx(g_mouseLL);
g_mouseLL = NULL; g_mouseLL = NULL;
} }
if (g_keyboard != NULL) {
UnhookWindowsHookEx(g_keyboard);
g_keyboard = NULL;
}
if (g_mouse != NULL) {
UnhookWindowsHookEx(g_mouse);
g_mouse = NULL;
}
if (g_getMessage != NULL && !g_screenSaver) { if (g_getMessage != NULL && !g_screenSaver) {
UnhookWindowsHookEx(g_getMessage); UnhookWindowsHookEx(g_getMessage);
g_getMessage = NULL; g_getMessage = NULL;
@ -1149,17 +1030,4 @@ setMode(EHookMode mode)
g_mode = mode; g_mode = mode;
} }
// do not call this while the hooks are active!
void
setImmuneKeys(const DWORD *list, std::size_t size)
{
if (size > ImmuneKeys::MaxKeys)
size = ImmuneKeys::MaxKeys;
g_immuneKeys.count = size;
if (size > 0) {
for (std::size_t idx = 0; idx < size; ++idx)
g_immuneKeys.list[idx] = list[idx];
}
}
} }

View File

@ -18,14 +18,6 @@
#pragma once #pragma once
// hack: vs2005 doesn't declare _WIN32_WINNT, so we need to hard code it.
// however, some say that this should be hard coded since it defines the
// target system, but since this is suposed to compile on pre-XP, maybe
// we should just leave it like this.
#if _MSC_VER == 1400
#define _WIN32_WINNT 0x0400
#endif
#include "base/EventTypes.h" #include "base/EventTypes.h"
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN