2002-08-02 19:57:46 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2001-11-19 00:33:36 +00:00
|
|
|
#include "CSynergyHook.h"
|
2002-07-15 15:03:04 +00:00
|
|
|
#include "ProtocolTypes.h"
|
2002-05-23 18:35:08 +00:00
|
|
|
#include <zmouse.h>
|
|
|
|
|
2003-02-16 19:51:46 +00:00
|
|
|
//
|
|
|
|
// debugging compile flag. when not zero the server doesn't grab
|
|
|
|
// the keyboard when the mouse leaves the server screen. this
|
|
|
|
// makes it possible to use the debugger (via the keyboard) when
|
|
|
|
// all user input would normally be caught by the hook procedures.
|
|
|
|
//
|
|
|
|
#define NO_GRAB_KEYBOARD 0
|
|
|
|
|
2004-03-08 20:53:32 +00:00
|
|
|
//
|
|
|
|
// debugging compile flag. when not zero the server will not
|
|
|
|
// install low level hooks.
|
|
|
|
//
|
|
|
|
#define NO_LOWLEVEL_HOOKS 0
|
|
|
|
|
2002-05-23 18:35:08 +00:00
|
|
|
//
|
|
|
|
// extra mouse wheel stuff
|
|
|
|
//
|
|
|
|
|
|
|
|
enum EWheelSupport {
|
|
|
|
kWheelNone,
|
|
|
|
kWheelOld,
|
|
|
|
kWheelWin2000,
|
|
|
|
kWheelModern
|
|
|
|
};
|
|
|
|
|
|
|
|
// declare extended mouse hook struct. useable on win2k
|
|
|
|
typedef struct tagMOUSEHOOKSTRUCTWin2000 {
|
|
|
|
MOUSEHOOKSTRUCT mhs;
|
|
|
|
DWORD mouseData;
|
|
|
|
} MOUSEHOOKSTRUCTWin2000;
|
|
|
|
|
|
|
|
#if !defined(SM_MOUSEWHEELPRESENT)
|
|
|
|
#define SM_MOUSEWHEELPRESENT 75
|
|
|
|
#endif
|
|
|
|
|
2003-05-04 21:40:42 +00:00
|
|
|
// 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
|
2007-06-17 11:19:18 +00:00
|
|
|
#define MOUSEEVENTF_XDOWN 0x0080
|
|
|
|
#define MOUSEEVENTF_XUP 0x0100
|
2003-05-04 21:40:42 +00:00
|
|
|
#define XBUTTON1 0x0001
|
|
|
|
#define XBUTTON2 0x0002
|
|
|
|
#endif
|
|
|
|
|
2001-11-19 00:33:36 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// globals
|
|
|
|
//
|
|
|
|
|
2004-04-11 19:15:09 +00:00
|
|
|
#if defined(_MSC_VER)
|
2001-11-19 00:33:36 +00:00
|
|
|
#pragma comment(linker, "-section:shared,rws")
|
|
|
|
#pragma data_seg("shared")
|
2004-04-11 19:15:09 +00:00
|
|
|
#endif
|
2001-11-19 00:33:36 +00:00
|
|
|
// all data in this shared section *must* be initialized
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
static HINSTANCE g_hinstance = NULL;
|
2002-07-18 17:03:10 +00:00
|
|
|
static DWORD g_processID = 0;
|
2002-06-08 21:48:00 +00:00
|
|
|
static EWheelSupport g_wheelSupport = kWheelNone;
|
|
|
|
static UINT g_wmMouseWheel = 0;
|
|
|
|
static DWORD g_threadID = 0;
|
|
|
|
static HHOOK g_keyboard = NULL;
|
|
|
|
static HHOOK g_mouse = NULL;
|
|
|
|
static HHOOK g_getMessage = NULL;
|
|
|
|
static HHOOK g_keyboardLL = NULL;
|
2003-01-07 21:11:54 +00:00
|
|
|
static HHOOK g_mouseLL = NULL;
|
2002-06-23 21:53:31 +00:00
|
|
|
static bool g_screenSaver = false;
|
2004-02-28 12:19:49 +00:00
|
|
|
static EHookMode g_mode = kHOOK_DISABLE;
|
2002-06-08 21:48:00 +00:00
|
|
|
static UInt32 g_zoneSides = 0;
|
2002-07-11 13:13:37 +00:00
|
|
|
static SInt32 g_zoneSize = 0;
|
2002-06-19 17:03:29 +00:00
|
|
|
static SInt32 g_xScreen = 0;
|
|
|
|
static SInt32 g_yScreen = 0;
|
2002-06-08 21:48:00 +00:00
|
|
|
static SInt32 g_wScreen = 0;
|
|
|
|
static SInt32 g_hScreen = 0;
|
2004-03-08 20:53:32 +00:00
|
|
|
static WPARAM g_deadVirtKey = 0;
|
|
|
|
static LPARAM g_deadLParam = 0;
|
|
|
|
static BYTE g_deadKeyState[256] = { 0 };
|
|
|
|
static DWORD g_hookThread = 0;
|
|
|
|
static DWORD g_attachedThread = 0;
|
2007-06-17 11:19:18 +00:00
|
|
|
static bool g_fakeInput = false;
|
2002-05-23 18:35:08 +00:00
|
|
|
|
2004-04-11 19:15:09 +00:00
|
|
|
#if defined(_MSC_VER)
|
2001-11-19 00:33:36 +00:00
|
|
|
#pragma data_seg()
|
2004-04-11 19:15:09 +00:00
|
|
|
#endif
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-07-31 12:40:41 +00:00
|
|
|
// keep linker quiet about floating point stuff. we don't use any
|
|
|
|
// floating point operations but our includes may define some
|
|
|
|
// (unused) floating point values.
|
|
|
|
#ifndef _DEBUG
|
2004-04-11 19:15:09 +00:00
|
|
|
extern "C" {
|
|
|
|
int _fltused=0;
|
|
|
|
}
|
2002-07-31 12:40:41 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-11-19 00:33:36 +00:00
|
|
|
//
|
|
|
|
// internal functions
|
|
|
|
//
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
2004-11-01 22:26:52 +00:00
|
|
|
detachThread()
|
|
|
|
{
|
|
|
|
if (g_attachedThread != 0 && g_hookThread != g_attachedThread) {
|
|
|
|
AttachThreadInput(g_hookThread, g_attachedThread, FALSE);
|
|
|
|
g_attachedThread = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
bool
|
2004-03-08 20:53:32 +00:00
|
|
|
attachThreadToForeground()
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
2004-03-08 20:53:32 +00:00
|
|
|
// only attach threads if using low level hooks. a low level hook
|
|
|
|
// runs in the thread that installed the hook but we have to make
|
|
|
|
// changes that require being attached to the target thread (which
|
|
|
|
// should be the foreground window). a regular hook runs in the
|
|
|
|
// thread that just removed the event from its queue so we're
|
|
|
|
// already in the right thread.
|
|
|
|
if (g_hookThread != 0) {
|
|
|
|
HWND window = GetForegroundWindow();
|
|
|
|
DWORD threadID = GetWindowThreadProcessId(window, NULL);
|
|
|
|
// skip if no change
|
|
|
|
if (g_attachedThread != threadID) {
|
|
|
|
// detach from previous thread
|
2004-11-01 22:26:52 +00:00
|
|
|
detachThread();
|
|
|
|
|
2004-03-08 20:53:32 +00:00
|
|
|
// attach to new thread
|
2004-11-01 22:26:52 +00:00
|
|
|
if (threadID != 0 && threadID != g_hookThread) {
|
|
|
|
AttachThreadInput(g_hookThread, threadID, TRUE);
|
|
|
|
g_attachedThread = threadID;
|
2004-03-08 20:53:32 +00:00
|
|
|
}
|
2004-11-01 22:26:52 +00:00
|
|
|
return true;
|
2004-03-08 20:53:32 +00:00
|
|
|
}
|
2003-05-17 20:58:27 +00:00
|
|
|
}
|
2004-11-01 22:26:52 +00:00
|
|
|
return false;
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
|
2004-02-28 12:19:49 +00:00
|
|
|
#if !NO_GRAB_KEYBOARD
|
2004-03-08 20:53:32 +00:00
|
|
|
static
|
|
|
|
WPARAM
|
2007-06-17 11:19:18 +00:00
|
|
|
makeKeyMsg(UINT virtKey, char c, bool noAltGr)
|
2004-03-08 20:53:32 +00:00
|
|
|
{
|
2007-06-17 11:19:18 +00:00
|
|
|
return MAKEWPARAM(MAKEWORD(virtKey & 0xff, (BYTE)c), noAltGr ? 1 : 0);
|
2004-03-08 20:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
keyboardGetState(BYTE keys[256])
|
|
|
|
{
|
2007-06-17 11:19:18 +00:00
|
|
|
// we have to use GetAsyncKeyState() rather than GetKeyState() because
|
|
|
|
// we don't pass through most keys so the event synchronous state
|
|
|
|
// doesn't get updated. we do that because certain modifier keys have
|
|
|
|
// side effects, like alt and the windows key.
|
|
|
|
SHORT key;
|
|
|
|
for (int i = 0; i < 256; ++i) {
|
|
|
|
key = GetAsyncKeyState(i);
|
|
|
|
keys[i] = (BYTE)((key < 0) ? 0x80u : 0);
|
2004-03-08 20:53:32 +00:00
|
|
|
}
|
2007-06-17 11:19:18 +00:00
|
|
|
key = GetKeyState(VK_CAPITAL);
|
|
|
|
keys[VK_CAPITAL] = (BYTE)(((key < 0) ? 0x80 : 0) | (key & 1));
|
2004-03-08 20:53:32 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
2003-05-03 12:54:22 +00:00
|
|
|
bool
|
2004-10-27 21:38:05 +00:00
|
|
|
doKeyboardHookHandler(WPARAM wParam, LPARAM lParam)
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
2007-06-17 11:19:18 +00:00
|
|
|
// check for special events indicating if we should start or stop
|
|
|
|
// passing events through and not report them to the server. this
|
|
|
|
// is used to allow the server to synthesize events locally but
|
|
|
|
// not pick them up as user events.
|
|
|
|
if (wParam == SYNERGY_HOOK_FAKE_INPUT_VIRTUAL_KEY &&
|
|
|
|
((lParam >> 16) & 0xffu) == SYNERGY_HOOK_FAKE_INPUT_SCANCODE) {
|
|
|
|
// update flag
|
|
|
|
g_fakeInput = ((lParam & 0x80000000u) == 0);
|
|
|
|
PostThreadMessage(g_threadID, SYNERGY_MSG_DEBUG,
|
|
|
|
0xff000000u | wParam, lParam);
|
|
|
|
|
|
|
|
// discard event
|
|
|
|
return true;
|
2004-03-08 20:53:32 +00:00
|
|
|
}
|
|
|
|
|
2007-06-17 11:19:18 +00:00
|
|
|
// if we're expecting fake input then just pass the event through
|
|
|
|
// and do not forward to the server
|
|
|
|
if (g_fakeInput) {
|
|
|
|
PostThreadMessage(g_threadID, SYNERGY_MSG_DEBUG,
|
|
|
|
0xfe000000u | wParam, lParam);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// VK_RSHIFT may be sent with an extended scan code but right shift
|
|
|
|
// is not an extended key so we reset that bit.
|
|
|
|
if (wParam == VK_RSHIFT) {
|
|
|
|
lParam &= ~0x01000000u;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tell server about event
|
|
|
|
PostThreadMessage(g_threadID, SYNERGY_MSG_DEBUG, wParam, lParam);
|
|
|
|
|
|
|
|
// ignore dead key release
|
|
|
|
if (g_deadVirtKey == wParam &&
|
|
|
|
(lParam & 0x80000000u) != 0) {
|
|
|
|
PostThreadMessage(g_threadID, SYNERGY_MSG_DEBUG,
|
|
|
|
wParam | 0x04000000, lParam);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need the keyboard state for ToAscii()
|
|
|
|
BYTE keys[256];
|
|
|
|
keyboardGetState(keys);
|
|
|
|
|
|
|
|
// ToAscii() maps ctrl+letter to the corresponding control code
|
|
|
|
// and ctrl+backspace to delete. we don't want those translations
|
|
|
|
// so clear the control modifier state. however, if we want to
|
|
|
|
// simulate AltGr (which is ctrl+alt) then we must not clear it.
|
|
|
|
UINT control = keys[VK_CONTROL] | keys[VK_LCONTROL] | keys[VK_RCONTROL];
|
|
|
|
UINT menu = keys[VK_MENU] | keys[VK_LMENU] | keys[VK_RMENU];
|
|
|
|
if ((control & 0x80) == 0 || (menu & 0x80) == 0) {
|
|
|
|
keys[VK_LCONTROL] = 0;
|
|
|
|
keys[VK_RCONTROL] = 0;
|
|
|
|
keys[VK_CONTROL] = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keys[VK_LCONTROL] = 0x80;
|
|
|
|
keys[VK_RCONTROL] = 0x80;
|
|
|
|
keys[VK_CONTROL] = 0x80;
|
|
|
|
keys[VK_LMENU] = 0x80;
|
|
|
|
keys[VK_RMENU] = 0x80;
|
|
|
|
keys[VK_MENU] = 0x80;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToAscii() needs to know if a menu is active for some reason.
|
|
|
|
// we don't know and there doesn't appear to be any way to find
|
|
|
|
// out. so we'll just assume a menu is active if the menu key
|
|
|
|
// is down.
|
|
|
|
// FIXME -- figure out some way to check if a menu is active
|
|
|
|
UINT flags = 0;
|
|
|
|
if ((menu & 0x80) != 0)
|
|
|
|
flags |= 1;
|
|
|
|
|
|
|
|
// if we're on the server screen then just pass numpad keys with alt
|
|
|
|
// key down as-is. we won't pick up the resulting character but the
|
|
|
|
// local app will. if on a client screen then grab keys as usual;
|
|
|
|
// if the client is a windows system it'll synthesize the expected
|
|
|
|
// character. if not then it'll probably just do nothing.
|
|
|
|
if (g_mode != kHOOK_RELAY_EVENTS) {
|
|
|
|
// we don't use virtual keys because we don't know what the
|
|
|
|
// state of the numlock key is. we'll hard code the scan codes
|
|
|
|
// instead. hopefully this works across all keyboards.
|
|
|
|
UINT sc = (lParam & 0x01ff0000u) >> 16;
|
|
|
|
if (menu &&
|
|
|
|
(sc >= 0x47u && sc <= 0x52u && sc != 0x4au && sc != 0x4eu)) {
|
|
|
|
return false;
|
2004-03-08 20:53:32 +00:00
|
|
|
}
|
2007-06-17 11:19:18 +00:00
|
|
|
}
|
2004-03-08 20:53:32 +00:00
|
|
|
|
2007-06-17 11:19:18 +00:00
|
|
|
// map the key event to a character. we have to put the dead
|
|
|
|
// key back first and this has the side effect of removing it.
|
|
|
|
if (g_deadVirtKey != 0) {
|
|
|
|
WORD c = 0;
|
|
|
|
ToAscii(g_deadVirtKey, (g_deadLParam & 0x10ff0000u) >> 16,
|
|
|
|
g_deadKeyState, &c, flags);
|
|
|
|
}
|
|
|
|
WORD c = 0;
|
|
|
|
UINT scanCode = ((lParam & 0x10ff0000u) >> 16);
|
|
|
|
int n = ToAscii(wParam, scanCode, keys, &c, flags);
|
|
|
|
|
|
|
|
// if mapping failed and ctrl and alt are pressed then try again
|
|
|
|
// with both not pressed. this handles the case where ctrl and
|
|
|
|
// alt are being used as individual modifiers rather than AltGr.
|
|
|
|
// we note that's the case in the message sent back to synergy
|
|
|
|
// because there's no simple way to deduce it after the fact.
|
|
|
|
// we have to put the dead key back first, if there was one.
|
|
|
|
bool noAltGr = false;
|
|
|
|
if (n == 0 && (control & 0x80) != 0 && (menu & 0x80) != 0) {
|
|
|
|
noAltGr = true;
|
|
|
|
PostThreadMessage(g_threadID, SYNERGY_MSG_DEBUG,
|
2004-03-28 14:07:58 +00:00
|
|
|
wParam | 0x05000000, lParam);
|
2007-06-17 11:19:18 +00:00
|
|
|
if (g_deadVirtKey != 0) {
|
|
|
|
ToAscii(g_deadVirtKey, (g_deadLParam & 0x10ff0000u) >> 16,
|
2004-03-08 20:53:32 +00:00
|
|
|
g_deadKeyState, &c, flags);
|
|
|
|
}
|
2007-06-17 11:19:18 +00:00
|
|
|
BYTE keys2[256];
|
|
|
|
for (size_t i = 0; i < sizeof(keys) / sizeof(keys[0]); ++i) {
|
|
|
|
keys2[i] = keys[i];
|
|
|
|
}
|
|
|
|
keys2[VK_LCONTROL] = 0;
|
|
|
|
keys2[VK_RCONTROL] = 0;
|
|
|
|
keys2[VK_CONTROL] = 0;
|
|
|
|
keys2[VK_LMENU] = 0;
|
|
|
|
keys2[VK_RMENU] = 0;
|
|
|
|
keys2[VK_MENU] = 0;
|
|
|
|
n = ToAscii(wParam, scanCode, keys2, &c, flags);
|
|
|
|
}
|
2004-03-08 20:53:32 +00:00
|
|
|
|
2007-06-17 11:19:18 +00:00
|
|
|
PostThreadMessage(g_threadID, SYNERGY_MSG_DEBUG,
|
|
|
|
wParam | ((c & 0xff) << 8) |
|
|
|
|
((n & 0xff) << 16) | 0x06000000,
|
2004-03-28 14:07:58 +00:00
|
|
|
lParam);
|
2007-06-17 11:19:18 +00:00
|
|
|
WPARAM charAndVirtKey = 0;
|
|
|
|
bool clearDeadKey = false;
|
|
|
|
switch (n) {
|
|
|
|
default:
|
|
|
|
// key is a dead key
|
|
|
|
g_deadVirtKey = wParam;
|
|
|
|
g_deadLParam = lParam;
|
|
|
|
for (size_t i = 0; i < sizeof(keys) / sizeof(keys[0]); ++i) {
|
|
|
|
g_deadKeyState[i] = keys[i];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
// key doesn't map to a character. this can happen if
|
|
|
|
// non-character keys are pressed after a dead key.
|
|
|
|
charAndVirtKey = makeKeyMsg(wParam, (char)0, noAltGr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// key maps to a character composed with dead key
|
|
|
|
charAndVirtKey = makeKeyMsg(wParam, (char)LOBYTE(c), noAltGr);
|
|
|
|
clearDeadKey = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: {
|
|
|
|
// previous dead key not composed. send a fake key press
|
|
|
|
// and release for the dead key to our window.
|
|
|
|
WPARAM deadCharAndVirtKey =
|
|
|
|
makeKeyMsg(g_deadVirtKey, (char)LOBYTE(c), noAltGr);
|
|
|
|
PostThreadMessage(g_threadID, SYNERGY_MSG_KEY,
|
2004-03-08 20:53:32 +00:00
|
|
|
deadCharAndVirtKey, g_deadLParam & 0x7fffffffu);
|
2007-06-17 11:19:18 +00:00
|
|
|
PostThreadMessage(g_threadID, SYNERGY_MSG_KEY,
|
2004-03-08 20:53:32 +00:00
|
|
|
deadCharAndVirtKey, g_deadLParam | 0x80000000u);
|
|
|
|
|
2007-06-17 11:19:18 +00:00
|
|
|
// use uncomposed character
|
|
|
|
charAndVirtKey = makeKeyMsg(wParam, (char)HIBYTE(c), noAltGr);
|
|
|
|
clearDeadKey = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-03-08 20:53:32 +00:00
|
|
|
|
2007-06-17 11:19:18 +00:00
|
|
|
// put back the dead key, if any, for the application to use
|
|
|
|
if (g_deadVirtKey != 0) {
|
|
|
|
ToAscii(g_deadVirtKey, (g_deadLParam & 0x10ff0000u) >> 16,
|
2004-03-08 20:53:32 +00:00
|
|
|
g_deadKeyState, &c, flags);
|
2007-06-17 11:19:18 +00:00
|
|
|
}
|
2004-03-08 20:53:32 +00:00
|
|
|
|
2007-06-17 11:19:18 +00:00
|
|
|
// clear out old dead key state
|
|
|
|
if (clearDeadKey) {
|
2004-03-08 20:53:32 +00:00
|
|
|
g_deadVirtKey = 0;
|
|
|
|
g_deadLParam = 0;
|
|
|
|
}
|
|
|
|
|
2003-05-03 12:54:22 +00:00
|
|
|
// forward message to our window. do this whether or not we're
|
|
|
|
// forwarding events to clients because this'll keep our thread's
|
|
|
|
// key state table up to date. that's important for querying
|
|
|
|
// the scroll lock toggle state.
|
2007-06-17 11:19:18 +00:00
|
|
|
// XXX -- with hot keys for actions we may only need to do this when
|
|
|
|
// forwarding.
|
|
|
|
if (charAndVirtKey != 0) {
|
2004-03-28 14:07:58 +00:00
|
|
|
PostThreadMessage(g_threadID, SYNERGY_MSG_DEBUG,
|
2007-06-17 11:19:18 +00:00
|
|
|
charAndVirtKey | 0x07000000, lParam);
|
|
|
|
PostThreadMessage(g_threadID, SYNERGY_MSG_KEY, charAndVirtKey, lParam);
|
2004-03-08 20:53:32 +00:00
|
|
|
}
|
2003-05-03 12:54:22 +00:00
|
|
|
|
2003-09-02 22:05:47 +00:00
|
|
|
if (g_mode == kHOOK_RELAY_EVENTS) {
|
2003-05-03 12:54:22 +00:00
|
|
|
// let certain keys pass through
|
|
|
|
switch (wParam) {
|
|
|
|
case VK_CAPITAL:
|
|
|
|
case VK_NUMLOCK:
|
|
|
|
case VK_SCROLL:
|
|
|
|
// pass event on. we want to let these through to
|
|
|
|
// the window proc because otherwise the keyboard
|
|
|
|
// lights may not stay synchronized.
|
|
|
|
break;
|
2002-05-23 18:35:08 +00:00
|
|
|
|
2004-03-08 20:53:32 +00:00
|
|
|
case VK_HANGUL:
|
2007-06-17 11:19:18 +00:00
|
|
|
// pass these modifiers if using a low level hook, discard
|
|
|
|
// them if not.
|
2004-07-29 21:59:26 +00:00
|
|
|
if (g_hookThread == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-05-03 12:54:22 +00:00
|
|
|
default:
|
2004-03-08 20:53:32 +00:00
|
|
|
// discard
|
2003-05-03 12:54:22 +00:00
|
|
|
return true;
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
|
2003-05-03 12:54:22 +00:00
|
|
|
return false;
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
2003-05-03 12:54:22 +00:00
|
|
|
bool
|
2004-10-27 21:38:05 +00:00
|
|
|
keyboardHookHandler(WPARAM wParam, LPARAM lParam)
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
2004-03-08 20:53:32 +00:00
|
|
|
attachThreadToForeground();
|
2004-11-01 22:26:52 +00:00
|
|
|
return doKeyboardHookHandler(wParam, lParam);
|
2004-10-27 21:38:05 +00:00
|
|
|
}
|
|
|
|
#endif
|
2004-03-08 20:53:32 +00:00
|
|
|
|
2004-10-27 21:38:05 +00:00
|
|
|
static
|
|
|
|
bool
|
|
|
|
doMouseHookHandler(WPARAM wParam, SInt32 x, SInt32 y, SInt32 data)
|
|
|
|
{
|
2003-05-03 12:54:22 +00:00
|
|
|
switch (wParam) {
|
|
|
|
case WM_LBUTTONDOWN:
|
|
|
|
case WM_MBUTTONDOWN:
|
|
|
|
case WM_RBUTTONDOWN:
|
2003-05-04 21:40:42 +00:00
|
|
|
case WM_XBUTTONDOWN:
|
2003-05-03 12:54:22 +00:00
|
|
|
case WM_LBUTTONDBLCLK:
|
|
|
|
case WM_MBUTTONDBLCLK:
|
|
|
|
case WM_RBUTTONDBLCLK:
|
2003-05-04 21:40:42 +00:00
|
|
|
case WM_XBUTTONDBLCLK:
|
2003-05-03 12:54:22 +00:00
|
|
|
case WM_LBUTTONUP:
|
|
|
|
case WM_MBUTTONUP:
|
|
|
|
case WM_RBUTTONUP:
|
2003-05-04 21:40:42 +00:00
|
|
|
case WM_XBUTTONUP:
|
2003-05-03 12:54:22 +00:00
|
|
|
case WM_NCLBUTTONDOWN:
|
|
|
|
case WM_NCMBUTTONDOWN:
|
|
|
|
case WM_NCRBUTTONDOWN:
|
2003-05-04 21:40:42 +00:00
|
|
|
case WM_NCXBUTTONDOWN:
|
2003-05-03 12:54:22 +00:00
|
|
|
case WM_NCLBUTTONDBLCLK:
|
|
|
|
case WM_NCMBUTTONDBLCLK:
|
|
|
|
case WM_NCRBUTTONDBLCLK:
|
2003-05-04 21:40:42 +00:00
|
|
|
case WM_NCXBUTTONDBLCLK:
|
2003-05-03 12:54:22 +00:00
|
|
|
case WM_NCLBUTTONUP:
|
|
|
|
case WM_NCMBUTTONUP:
|
|
|
|
case WM_NCRBUTTONUP:
|
2003-05-04 21:40:42 +00:00
|
|
|
case WM_NCXBUTTONUP:
|
2003-05-03 12:54:22 +00:00
|
|
|
// always relay the event. eat it if relaying.
|
2003-05-04 21:40:42 +00:00
|
|
|
PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_BUTTON, wParam, data);
|
2003-09-02 22:05:47 +00:00
|
|
|
return (g_mode == kHOOK_RELAY_EVENTS);
|
2003-05-03 12:54:22 +00:00
|
|
|
|
|
|
|
case WM_MOUSEWHEEL:
|
2003-09-02 22:05:47 +00:00
|
|
|
if (g_mode == kHOOK_RELAY_EVENTS) {
|
2003-05-03 12:54:22 +00:00
|
|
|
// relay event
|
2003-05-04 21:40:42 +00:00
|
|
|
PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_WHEEL, data, 0);
|
2003-05-03 12:54:22 +00:00
|
|
|
}
|
2003-09-02 22:05:47 +00:00
|
|
|
return (g_mode == kHOOK_RELAY_EVENTS);
|
2003-05-03 12:54:22 +00:00
|
|
|
|
|
|
|
case WM_NCMOUSEMOVE:
|
|
|
|
case WM_MOUSEMOVE:
|
2003-09-02 22:05:47 +00:00
|
|
|
if (g_mode == kHOOK_RELAY_EVENTS) {
|
2003-05-03 12:54:22 +00:00
|
|
|
// relay and eat event
|
|
|
|
PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_MOVE, x, y);
|
|
|
|
return true;
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
2004-02-28 12:19:49 +00:00
|
|
|
else if (g_mode == kHOOK_WATCH_JUMP_ZONE) {
|
2004-04-05 21:10:06 +00:00
|
|
|
// low level hooks can report bogus mouse positions that are
|
|
|
|
// outside of the screen. jeez. naturally we end up getting
|
|
|
|
// fake motion in the other direction to get the position back
|
|
|
|
// on the screen, which plays havoc with switch on double tap.
|
|
|
|
// CServer deals with that. we'll clamp positions onto the
|
|
|
|
// screen. also, if we discard events for positions outside
|
|
|
|
// of the screen then the mouse appears to get a bit jerky
|
|
|
|
// near the edge. we can either accept that or pass the bogus
|
|
|
|
// events. we'll try passing the events.
|
|
|
|
bool bogus = false;
|
|
|
|
if (x < g_xScreen) {
|
|
|
|
x = g_xScreen;
|
|
|
|
bogus = true;
|
|
|
|
}
|
|
|
|
else if (x >= g_xScreen + g_wScreen) {
|
|
|
|
x = g_xScreen + g_wScreen - 1;
|
|
|
|
bogus = true;
|
|
|
|
}
|
|
|
|
if (y < g_yScreen) {
|
|
|
|
y = g_yScreen;
|
|
|
|
bogus = true;
|
|
|
|
}
|
|
|
|
else if (y >= g_yScreen + g_hScreen) {
|
|
|
|
y = g_yScreen + g_hScreen - 1;
|
|
|
|
bogus = true;
|
|
|
|
}
|
|
|
|
|
2003-03-25 21:31:39 +00:00
|
|
|
// check for mouse inside jump zone
|
2001-11-19 00:33:36 +00:00
|
|
|
bool inside = false;
|
2002-07-15 15:03:04 +00:00
|
|
|
if (!inside && (g_zoneSides & kLeftMask) != 0) {
|
2002-06-19 17:03:29 +00:00
|
|
|
inside = (x < g_xScreen + g_zoneSize);
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
2002-07-15 15:03:04 +00:00
|
|
|
if (!inside && (g_zoneSides & kRightMask) != 0) {
|
2002-06-19 17:03:29 +00:00
|
|
|
inside = (x >= g_xScreen + g_wScreen - g_zoneSize);
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
2002-07-15 15:03:04 +00:00
|
|
|
if (!inside && (g_zoneSides & kTopMask) != 0) {
|
2002-06-19 17:03:29 +00:00
|
|
|
inside = (y < g_yScreen + g_zoneSize);
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
2002-07-15 15:03:04 +00:00
|
|
|
if (!inside && (g_zoneSides & kBottomMask) != 0) {
|
2002-06-19 17:03:29 +00:00
|
|
|
inside = (y >= g_yScreen + g_hScreen - g_zoneSize);
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
|
2003-05-03 12:54:22 +00:00
|
|
|
// relay the event
|
|
|
|
PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_MOVE, x, y);
|
|
|
|
|
2004-04-05 21:10:06 +00:00
|
|
|
// if inside and not bogus then eat the event
|
|
|
|
return inside && !bogus;
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-03 12:54:22 +00:00
|
|
|
// pass the event
|
|
|
|
return false;
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:38:05 +00:00
|
|
|
static
|
|
|
|
bool
|
|
|
|
mouseHookHandler(WPARAM wParam, SInt32 x, SInt32 y, SInt32 data)
|
|
|
|
{
|
2007-06-17 11:19:18 +00:00
|
|
|
// attachThreadToForeground();
|
2004-11-01 22:26:52 +00:00
|
|
|
return doMouseHookHandler(wParam, x, y, data);
|
2004-10-27 21:38:05 +00:00
|
|
|
}
|
|
|
|
|
2004-02-28 12:19:49 +00:00
|
|
|
#if !NO_GRAB_KEYBOARD
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
LRESULT CALLBACK
|
2003-05-03 12:54:22 +00:00
|
|
|
keyboardHook(int code, WPARAM wParam, LPARAM lParam)
|
2002-05-22 17:05:26 +00:00
|
|
|
{
|
|
|
|
if (code >= 0) {
|
2003-05-03 12:54:22 +00:00
|
|
|
// handle the message
|
|
|
|
if (keyboardHookHandler(wParam, lParam)) {
|
|
|
|
return 1;
|
2002-05-22 17:05:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-03 12:54:22 +00:00
|
|
|
return CallNextHookEx(g_keyboard, code, wParam, lParam);
|
|
|
|
}
|
2004-02-28 12:19:49 +00:00
|
|
|
#endif
|
2003-05-03 12:54:22 +00:00
|
|
|
|
|
|
|
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:
|
2004-04-05 21:10:06 +00:00
|
|
|
w = static_cast<SInt16>(LOWORD(info->dwExtraInfo));
|
2003-05-03 12:54:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case kWheelWin2000: {
|
|
|
|
const MOUSEHOOKSTRUCTWin2000* info2k =
|
|
|
|
(const MOUSEHOOKSTRUCTWin2000*)lParam;
|
2004-04-05 21:10:06 +00:00
|
|
|
w = static_cast<SInt16>(HIWORD(info2k->mouseData));
|
2003-05-03 12:54:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-04-11 19:15:09 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2003-05-03 12:54:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-04 21:40:42 +00:00
|
|
|
// 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.
|
2003-05-03 12:54:22 +00:00
|
|
|
if (mouseHookHandler(wParam, x, y, w)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CallNextHookEx(g_mouse, code, wParam, lParam);
|
2002-05-22 17:05:26 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
LRESULT CALLBACK
|
2002-06-17 13:31:21 +00:00
|
|
|
getMessageHook(int code, WPARAM wParam, LPARAM lParam)
|
2002-05-23 18:35:08 +00:00
|
|
|
{
|
|
|
|
if (code >= 0) {
|
2002-06-23 21:53:31 +00:00
|
|
|
if (g_screenSaver) {
|
|
|
|
MSG* msg = reinterpret_cast<MSG*>(lParam);
|
2003-03-16 17:40:47 +00:00
|
|
|
if (msg->message == WM_SYSCOMMAND &&
|
|
|
|
msg->wParam == SC_SCREENSAVE) {
|
2002-06-23 21:53:31 +00:00
|
|
|
// broadcast screen saver started message
|
|
|
|
PostThreadMessage(g_threadID,
|
|
|
|
SYNERGY_MSG_SCREEN_SAVER, TRUE, 0);
|
|
|
|
}
|
|
|
|
}
|
2003-09-02 22:05:47 +00:00
|
|
|
if (g_mode == kHOOK_RELAY_EVENTS) {
|
2002-05-23 18:35:08 +00:00
|
|
|
MSG* msg = reinterpret_cast<MSG*>(lParam);
|
2004-10-27 21:29:19 +00:00
|
|
|
if (g_wheelSupport == kWheelOld && msg->message == g_wmMouseWheel) {
|
2002-05-23 18:35:08 +00:00
|
|
|
// post message to our window
|
2002-06-08 21:48:00 +00:00
|
|
|
PostThreadMessage(g_threadID,
|
2004-04-05 21:10:06 +00:00
|
|
|
SYNERGY_MSG_MOUSE_WHEEL,
|
|
|
|
static_cast<SInt16>(msg->wParam & 0xffffu), 0);
|
2002-05-23 18:35:08 +00:00
|
|
|
|
|
|
|
// zero out the delta in the message so it's (hopefully)
|
|
|
|
// ignored
|
|
|
|
msg->wParam = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CallNextHookEx(g_getMessage, code, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
2004-04-11 19:15:09 +00:00
|
|
|
#if (_WIN32_WINNT >= 0x0400) && defined(_MSC_VER) && !NO_LOWLEVEL_HOOKS
|
2002-06-01 19:26:11 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// 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
|
2003-05-03 12:54:22 +00:00
|
|
|
// side, key repeats are not reported to us.
|
2002-06-01 19:26:11 +00:00
|
|
|
//
|
|
|
|
|
2004-02-28 12:19:49 +00:00
|
|
|
#if !NO_GRAB_KEYBOARD
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
LRESULT CALLBACK
|
2002-06-17 13:31:21 +00:00
|
|
|
keyboardLLHook(int code, WPARAM wParam, LPARAM lParam)
|
2002-06-01 19:26:11 +00:00
|
|
|
{
|
|
|
|
if (code >= 0) {
|
2003-05-03 12:54:22 +00:00
|
|
|
// decode the message
|
|
|
|
KBDLLHOOKSTRUCT* info = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);
|
|
|
|
WPARAM wParam = info->vkCode;
|
|
|
|
LPARAM lParam = 1; // repeat code
|
|
|
|
lParam |= (info->scanCode << 16); // scan code
|
|
|
|
if (info->flags & LLKHF_EXTENDED) {
|
|
|
|
lParam |= (1lu << 24); // extended key
|
|
|
|
}
|
|
|
|
if (info->flags & LLKHF_ALTDOWN) {
|
|
|
|
lParam |= (1lu << 29); // context code
|
|
|
|
}
|
|
|
|
if (info->flags & LLKHF_UP) {
|
|
|
|
lParam |= (1lu << 31); // transition
|
|
|
|
}
|
|
|
|
// FIXME -- bit 30 should be set if key was already down but
|
|
|
|
// we don't know that info. as a result we'll never generate
|
|
|
|
// key repeat events.
|
2002-06-01 19:26:11 +00:00
|
|
|
|
2003-05-03 12:54:22 +00:00
|
|
|
// handle the message
|
|
|
|
if (keyboardHookHandler(wParam, lParam)) {
|
|
|
|
return 1;
|
2002-06-01 19:26:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CallNextHookEx(g_keyboardLL, code, wParam, lParam);
|
|
|
|
}
|
2004-02-28 12:19:49 +00:00
|
|
|
#endif
|
2002-06-01 19:26:11 +00:00
|
|
|
|
2003-01-07 21:11:54 +00:00
|
|
|
//
|
|
|
|
// low-level mouse hook -- this allows us to capture and handle mouse
|
2003-05-03 12:54:22 +00:00
|
|
|
// events very early. the earlier the better.
|
2003-01-07 21:11:54 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
static
|
|
|
|
LRESULT CALLBACK
|
|
|
|
mouseLLHook(int code, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
if (code >= 0) {
|
2003-05-03 12:54:22 +00:00
|
|
|
// decode the message
|
|
|
|
MSLLHOOKSTRUCT* info = reinterpret_cast<MSLLHOOKSTRUCT*>(lParam);
|
2004-04-05 21:10:06 +00:00
|
|
|
SInt32 x = static_cast<SInt32>(info->pt.x);
|
|
|
|
SInt32 y = static_cast<SInt32>(info->pt.y);
|
|
|
|
SInt32 w = static_cast<SInt16>(HIWORD(info->mouseData));
|
2003-05-03 12:54:22 +00:00
|
|
|
|
|
|
|
// handle the message
|
|
|
|
if (mouseHookHandler(wParam, x, y, w)) {
|
|
|
|
return 1;
|
2003-01-07 21:11:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CallNextHookEx(g_mouseLL, code, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
2002-06-01 19:26:11 +00:00
|
|
|
#endif
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
EWheelSupport
|
|
|
|
getWheelSupport()
|
2002-05-23 18:35:08 +00:00
|
|
|
{
|
|
|
|
// get operating system
|
|
|
|
OSVERSIONINFO info;
|
|
|
|
info.dwOSVersionInfoSize = sizeof(info);
|
|
|
|
if (!GetVersionEx(&info)) {
|
|
|
|
return kWheelNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
// see if modern wheel is present
|
|
|
|
if (GetSystemMetrics(SM_MOUSEWHEELPRESENT)) {
|
|
|
|
// note if running on win2k
|
|
|
|
if (info.dwPlatformId == VER_PLATFORM_WIN32_NT &&
|
|
|
|
info.dwMajorVersion == 5 &&
|
|
|
|
info.dwMinorVersion == 0) {
|
|
|
|
return kWheelWin2000;
|
|
|
|
}
|
|
|
|
return kWheelModern;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not modern. see if we've got old-style support.
|
2004-04-11 19:15:09 +00:00
|
|
|
#if defined(MSH_WHEELSUPPORT)
|
2002-05-23 18:35:08 +00:00
|
|
|
UINT wheelSupportMsg = RegisterWindowMessage(MSH_WHEELSUPPORT);
|
|
|
|
HWND wheelSupportWindow = FindWindow(MSH_WHEELMODULE_CLASS,
|
|
|
|
MSH_WHEELMODULE_TITLE);
|
|
|
|
if (wheelSupportWindow != NULL && wheelSupportMsg != 0) {
|
|
|
|
if (SendMessage(wheelSupportWindow, wheelSupportMsg, 0, 0) != 0) {
|
|
|
|
g_wmMouseWheel = RegisterWindowMessage(MSH_MOUSEWHEEL);
|
|
|
|
if (g_wmMouseWheel != 0) {
|
|
|
|
return kWheelOld;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-04-11 19:15:09 +00:00
|
|
|
#endif
|
2002-05-23 18:35:08 +00:00
|
|
|
|
|
|
|
// assume modern. we don't do anything special in this case
|
|
|
|
// except respond to WM_MOUSEWHEEL messages. GetSystemMetrics()
|
|
|
|
// can apparently return FALSE even if a mouse wheel is present
|
2002-06-01 19:26:11 +00:00
|
|
|
// though i'm not sure exactly when it does that (WinME returns
|
|
|
|
// FALSE for my logitech USB trackball).
|
2002-05-23 18:35:08 +00:00
|
|
|
return kWheelModern;
|
|
|
|
}
|
|
|
|
|
2001-11-19 00:33:36 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// external functions
|
|
|
|
//
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
BOOL WINAPI
|
2002-06-17 13:31:21 +00:00
|
|
|
DllMain(HINSTANCE instance, DWORD reason, LPVOID)
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
|
|
|
if (reason == DLL_PROCESS_ATTACH) {
|
2002-07-18 17:03:10 +00:00
|
|
|
DisableThreadLibraryCalls(instance);
|
|
|
|
if (g_processID == 0) {
|
2001-11-19 00:33:36 +00:00
|
|
|
g_hinstance = instance;
|
2002-07-18 17:03:10 +00:00
|
|
|
g_processID = GetCurrentProcessId();
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (reason == DLL_PROCESS_DETACH) {
|
2002-07-18 17:03:10 +00:00
|
|
|
if (g_processID == GetCurrentProcessId()) {
|
2004-02-28 12:19:49 +00:00
|
|
|
uninstall();
|
|
|
|
uninstallScreenSaver();
|
2002-07-18 17:03:10 +00:00
|
|
|
g_processID = 0;
|
|
|
|
g_hinstance = NULL;
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
int
|
2002-06-23 21:53:31 +00:00
|
|
|
init(DWORD threadID)
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
2002-06-23 21:53:31 +00:00
|
|
|
assert(g_hinstance != NULL);
|
|
|
|
|
2002-07-18 17:03:10 +00:00
|
|
|
// try to open process that last called init() to see if it's
|
|
|
|
// still running or if it died without cleaning up.
|
|
|
|
if (g_processID != 0 && g_processID != GetCurrentProcessId()) {
|
|
|
|
HANDLE process = OpenProcess(STANDARD_RIGHTS_REQUIRED,
|
|
|
|
FALSE, g_processID);
|
|
|
|
if (process != NULL) {
|
|
|
|
// old process (probably) still exists so refuse to
|
|
|
|
// reinitialize this DLL (and thus steal it from the
|
|
|
|
// old process).
|
|
|
|
CloseHandle(process);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up after old process. the system should've already
|
|
|
|
// removed the hooks so we just need to reset our state.
|
|
|
|
g_hinstance = GetModuleHandle("synrgyhk");
|
|
|
|
g_processID = GetCurrentProcessId();
|
|
|
|
g_wheelSupport = kWheelNone;
|
|
|
|
g_threadID = 0;
|
|
|
|
g_keyboard = NULL;
|
|
|
|
g_mouse = NULL;
|
|
|
|
g_getMessage = NULL;
|
|
|
|
g_keyboardLL = NULL;
|
2003-01-07 21:11:54 +00:00
|
|
|
g_mouseLL = NULL;
|
2002-07-18 17:03:10 +00:00
|
|
|
g_screenSaver = false;
|
2002-06-23 21:53:31 +00:00
|
|
|
}
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
// save thread id. we'll post messages to this thread's
|
|
|
|
// message queue.
|
2007-06-17 11:19:18 +00:00
|
|
|
g_threadID = threadID;
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-07-15 15:03:04 +00:00
|
|
|
// set defaults
|
2004-03-08 20:53:32 +00:00
|
|
|
g_mode = kHOOK_DISABLE;
|
|
|
|
g_zoneSides = 0;
|
|
|
|
g_zoneSize = 0;
|
|
|
|
g_xScreen = 0;
|
|
|
|
g_yScreen = 0;
|
|
|
|
g_wScreen = 0;
|
|
|
|
g_hScreen = 0;
|
2002-07-15 15:03:04 +00:00
|
|
|
|
2002-06-23 21:53:31 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cleanup(void)
|
|
|
|
{
|
|
|
|
assert(g_hinstance != NULL);
|
|
|
|
|
2002-07-18 17:03:10 +00:00
|
|
|
if (g_processID == GetCurrentProcessId()) {
|
|
|
|
g_threadID = 0;
|
|
|
|
}
|
2002-06-23 21:53:31 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-05-03 12:54:22 +00:00
|
|
|
EHookResult
|
2002-06-23 21:53:31 +00:00
|
|
|
install()
|
|
|
|
{
|
|
|
|
assert(g_hinstance != NULL);
|
|
|
|
assert(g_keyboard == NULL);
|
|
|
|
assert(g_mouse == NULL);
|
|
|
|
assert(g_getMessage == NULL || g_screenSaver);
|
|
|
|
|
|
|
|
// must be initialized
|
|
|
|
if (g_threadID == 0) {
|
2003-05-03 12:54:22 +00:00
|
|
|
return kHOOK_FAILED;
|
2002-06-23 21:53:31 +00:00
|
|
|
}
|
|
|
|
|
2004-03-08 20:53:32 +00:00
|
|
|
// discard old dead keys
|
|
|
|
g_deadVirtKey = 0;
|
|
|
|
g_deadLParam = 0;
|
|
|
|
|
2007-06-17 11:19:18 +00:00
|
|
|
// reset fake input flag
|
|
|
|
g_fakeInput = false;
|
|
|
|
|
2002-05-23 18:35:08 +00:00
|
|
|
// check for mouse wheel support
|
2002-06-10 22:06:45 +00:00
|
|
|
g_wheelSupport = getWheelSupport();
|
2002-05-23 18:35:08 +00:00
|
|
|
|
2002-06-23 21:53:31 +00:00
|
|
|
// install GetMessage hook (unless already installed)
|
|
|
|
if (g_wheelSupport == kWheelOld && g_getMessage == NULL) {
|
2002-05-23 18:35:08 +00:00
|
|
|
g_getMessage = SetWindowsHookEx(WH_GETMESSAGE,
|
|
|
|
&getMessageHook,
|
|
|
|
g_hinstance,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
2004-03-08 20:53:32 +00:00
|
|
|
// install low-level hooks. we require that they both get installed.
|
2004-04-11 19:15:09 +00:00
|
|
|
#if (_WIN32_WINNT >= 0x0400) && defined(_MSC_VER) && !NO_LOWLEVEL_HOOKS
|
2004-03-08 20:53:32 +00:00
|
|
|
g_mouseLL = SetWindowsHookEx(WH_MOUSE_LL,
|
|
|
|
&mouseLLHook,
|
|
|
|
g_hinstance,
|
|
|
|
0);
|
2003-05-03 12:54:22 +00:00
|
|
|
#if !NO_GRAB_KEYBOARD
|
2004-02-28 12:19:49 +00:00
|
|
|
g_keyboardLL = SetWindowsHookEx(WH_KEYBOARD_LL,
|
|
|
|
&keyboardLLHook,
|
|
|
|
g_hinstance,
|
|
|
|
0);
|
2004-03-08 20:53:32 +00:00
|
|
|
if (g_mouseLL == NULL || g_keyboardLL == NULL) {
|
|
|
|
if (g_keyboardLL != NULL) {
|
|
|
|
UnhookWindowsHookEx(g_keyboardLL);
|
|
|
|
g_keyboardLL = NULL;
|
|
|
|
}
|
|
|
|
if (g_mouseLL != NULL) {
|
|
|
|
UnhookWindowsHookEx(g_mouseLL);
|
|
|
|
g_mouseLL = NULL;
|
|
|
|
}
|
2004-02-28 12:19:49 +00:00
|
|
|
}
|
|
|
|
#endif
|
2003-05-03 12:54:22 +00:00
|
|
|
#endif
|
2004-03-08 20:53:32 +00:00
|
|
|
|
|
|
|
// install regular hooks
|
2004-02-28 12:19:49 +00:00
|
|
|
if (g_mouseLL == NULL) {
|
2003-05-03 12:54:22 +00:00
|
|
|
g_mouse = SetWindowsHookEx(WH_MOUSE,
|
|
|
|
&mouseHook,
|
|
|
|
g_hinstance,
|
|
|
|
0);
|
|
|
|
}
|
2004-03-28 14:07:58 +00:00
|
|
|
#if !NO_GRAB_KEYBOARD
|
2004-03-08 20:53:32 +00:00
|
|
|
if (g_keyboardLL == NULL) {
|
|
|
|
g_keyboard = SetWindowsHookEx(WH_KEYBOARD,
|
|
|
|
&keyboardHook,
|
|
|
|
g_hinstance,
|
|
|
|
0);
|
|
|
|
}
|
2004-03-28 14:07:58 +00:00
|
|
|
#endif
|
2003-05-03 12:54:22 +00:00
|
|
|
|
2004-02-28 12:19:49 +00:00
|
|
|
// check that we got all the hooks we wanted
|
|
|
|
if ((g_getMessage == NULL && g_wheelSupport == kWheelOld) ||
|
2003-05-03 12:54:22 +00:00
|
|
|
#if !NO_GRAB_KEYBOARD
|
2004-02-28 12:19:49 +00:00
|
|
|
(g_keyboardLL == NULL && g_keyboard == NULL) ||
|
2003-05-03 12:54:22 +00:00
|
|
|
#endif
|
2004-02-28 12:19:49 +00:00
|
|
|
(g_mouseLL == NULL && g_mouse == NULL)) {
|
|
|
|
uninstall();
|
2003-05-03 12:54:22 +00:00
|
|
|
return kHOOK_FAILED;
|
|
|
|
}
|
|
|
|
|
2004-02-28 12:19:49 +00:00
|
|
|
if (g_keyboardLL != NULL || g_mouseLL != NULL) {
|
2004-03-08 20:53:32 +00:00
|
|
|
g_hookThread = GetCurrentThreadId();
|
2004-02-28 12:19:49 +00:00
|
|
|
return kHOOK_OKAY_LL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kHOOK_OKAY;
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
int
|
2002-06-17 13:31:21 +00:00
|
|
|
uninstall(void)
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
2002-06-23 21:53:31 +00:00
|
|
|
assert(g_hinstance != NULL);
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2004-03-08 20:53:32 +00:00
|
|
|
// discard old dead keys
|
|
|
|
g_deadVirtKey = 0;
|
|
|
|
g_deadLParam = 0;
|
|
|
|
|
|
|
|
// detach from thread
|
|
|
|
detachThread();
|
|
|
|
|
2001-11-19 00:33:36 +00:00
|
|
|
// uninstall hooks
|
2004-02-28 12:19:49 +00:00
|
|
|
if (g_keyboardLL != NULL) {
|
|
|
|
UnhookWindowsHookEx(g_keyboardLL);
|
|
|
|
g_keyboardLL = NULL;
|
|
|
|
}
|
|
|
|
if (g_mouseLL != NULL) {
|
|
|
|
UnhookWindowsHookEx(g_mouseLL);
|
|
|
|
g_mouseLL = NULL;
|
2002-06-01 19:26:11 +00:00
|
|
|
}
|
2002-06-23 21:53:31 +00:00
|
|
|
if (g_keyboard != NULL) {
|
|
|
|
UnhookWindowsHookEx(g_keyboard);
|
2003-05-03 12:54:22 +00:00
|
|
|
g_keyboard = NULL;
|
2002-06-23 21:53:31 +00:00
|
|
|
}
|
|
|
|
if (g_mouse != NULL) {
|
|
|
|
UnhookWindowsHookEx(g_mouse);
|
2003-05-03 12:54:22 +00:00
|
|
|
g_mouse = NULL;
|
2002-06-23 21:53:31 +00:00
|
|
|
}
|
|
|
|
if (g_getMessage != NULL && !g_screenSaver) {
|
2002-05-23 18:35:08 +00:00
|
|
|
UnhookWindowsHookEx(g_getMessage);
|
2002-06-23 21:53:31 +00:00
|
|
|
g_getMessage = NULL;
|
2002-05-23 18:35:08 +00:00
|
|
|
}
|
2002-06-26 16:31:48 +00:00
|
|
|
g_wheelSupport = kWheelNone;
|
2001-11-19 00:33:36 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-06-23 21:53:31 +00:00
|
|
|
int
|
|
|
|
installScreenSaver(void)
|
|
|
|
{
|
|
|
|
assert(g_hinstance != NULL);
|
|
|
|
|
|
|
|
// must be initialized
|
|
|
|
if (g_threadID == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate screen saver messages
|
|
|
|
g_screenSaver = true;
|
|
|
|
|
|
|
|
// install hook unless it's already installed
|
|
|
|
if (g_getMessage == NULL) {
|
|
|
|
g_getMessage = SetWindowsHookEx(WH_GETMESSAGE,
|
|
|
|
&getMessageHook,
|
|
|
|
g_hinstance,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (g_getMessage != NULL) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
uninstallScreenSaver(void)
|
|
|
|
{
|
|
|
|
assert(g_hinstance != NULL);
|
|
|
|
|
|
|
|
// uninstall hook unless the mouse wheel hook is installed
|
2003-03-16 17:40:47 +00:00
|
|
|
if (g_getMessage != NULL && g_wheelSupport != kWheelOld) {
|
2002-06-23 21:53:31 +00:00
|
|
|
UnhookWindowsHookEx(g_getMessage);
|
|
|
|
g_getMessage = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// screen saver hook is no longer installed
|
|
|
|
g_screenSaver = false;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-07-11 13:13:37 +00:00
|
|
|
setSides(UInt32 sides)
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
|
|
|
g_zoneSides = sides;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-07-11 13:13:37 +00:00
|
|
|
setZone(SInt32 x, SInt32 y, SInt32 w, SInt32 h, SInt32 jumpZoneSize)
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
2002-07-11 13:13:37 +00:00
|
|
|
g_zoneSize = jumpZoneSize;
|
|
|
|
g_xScreen = x;
|
|
|
|
g_yScreen = y;
|
|
|
|
g_wScreen = w;
|
|
|
|
g_hScreen = h;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-09-02 22:05:47 +00:00
|
|
|
setMode(EHookMode mode)
|
2002-07-11 13:13:37 +00:00
|
|
|
{
|
2003-09-02 22:05:47 +00:00
|
|
|
if (mode == g_mode) {
|
2003-05-03 12:54:22 +00:00
|
|
|
// no change
|
|
|
|
return;
|
|
|
|
}
|
2003-09-02 22:05:47 +00:00
|
|
|
g_mode = mode;
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|