diff --git a/CMakeLists.txt b/CMakeLists.txt
index e162d631..a2297311 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -293,12 +293,7 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
${OPENSSL_ROOT}/lib/libcrypto.lib
)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
- #Try use 1.1 for the latest features. otherwise use the default
- IF(EXISTS /usr/local/opt/openssl@1.1)
- set (OPENSSL_ROOT /usr/local/opt/openssl@1.1)
- else()
- set (OPENSSL_ROOT /usr/local/opt/openssl)
- endif()
+ set (OPENSSL_ROOT /usr/local/opt/openssl)
include_directories (BEFORE SYSTEM ${OPENSSL_ROOT}/include)
set (OPENSSL_LIBS
${OPENSSL_ROOT}/lib/libssl.a
@@ -325,7 +320,7 @@ macro (configure_files srcDir destDir)
set (sourceFilePath ${srcDir}/${sourceFile})
if (IS_DIRECTORY ${sourceFilePath})
message (STATUS "Copying directory ${sourceFile}")
- make_directory (${destDir/${sourceFile})
+ make_directory (${destDir}/${sourceFile})
else()
message (STATUS "Copying file ${sourceFile}")
configure_file (${sourceFilePath} ${destDir}/${sourceFile} COPYONLY)
@@ -349,10 +344,10 @@ macro(generate_versionfile)
"export SYNERGY_VERSION_STAGE=\"${SYNERGY_VERSION_STAGE}\"\n")
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
FILE(WRITE ${CMAKE_BINARY_DIR}/version.bat
- "SET SYNERGY_VERSION_MAJOR=\"${SYNERGY_VERSION_MAJOR}\"\n"
- "SET SYNERGY_VERSION_MINOR=\"${SYNERGY_VERSION_MINOR}\"\n"
- "SET SYNERGY_VERSION_PATCH=\"${SYNERGY_VERSION_PATCH}\"\n"
- "SET SYNERGY_VERSION_STAGE=\"${SYNERGY_VERSION_STAGE}\"\n")
+ "SET SYNERGY_VERSION_MAJOR=${SYNERGY_VERSION_MAJOR}\n"
+ "SET SYNERGY_VERSION_MINOR=${SYNERGY_VERSION_MINOR}\n"
+ "SET SYNERGY_VERSION_PATCH=${SYNERGY_VERSION_PATCH}\n"
+ "SET SYNERGY_VERSION_STAGE=${SYNERGY_VERSION_STAGE}\n")
endif()
endmacro(generate_versionfile)
@@ -377,8 +372,9 @@ endif()
#
if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
message (STATUS "Configuring the v1 installer")
- configure_files (${CMAKE_CURRENT_SOURCE_DIR}/dist/wix ${CMAKE_BINARY_DIR}/installer)
generate_versionfile()
+ set(QT_PATH $ENV{CMAKE_PREFIX_PATH})
+ configure_files (${CMAKE_CURRENT_SOURCE_DIR}/dist/wix ${CMAKE_BINARY_DIR}/installer)
endif()
#
diff --git a/ChangeLog b/ChangeLog
index 046140c4..43822a39 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,24 @@
+v1.11.0-stable
+==============
+
+Bug fixes:
+- #6575 Service start error on Windows
+- #6570 Hotkey capture of ctrl and cmd swapped on macOS
+- #6569 Unable to run on macOS 10.15 Catalina
+- #6566 Debug message output mistakenly as info message
+- #6561 Debian buster no longer supports CA key length of 1024
+- #6556 Function missing error from OpenSSL/TLS
+- #5959 User interface failed to load local fingerprint
+- #5294 Cursor lockout at Windows server login screen
+
+Enhancements:
+- #6588 Version number header with update check
+- #4957 Server fails to start up due to synwinhk.dll is in use
+
+Features:
+- #6518 Key combination that will force a server restart
+
+
v1.10.3-stable
==============
diff --git a/cmake/Version.cmake b/cmake/Version.cmake
index 26c47a30..d313ad26 100644
--- a/cmake/Version.cmake
+++ b/cmake/Version.cmake
@@ -5,8 +5,8 @@ cmake_minimum_required (VERSION 3.4)
#
set (SYNERGY_VERSION_MAJOR 1)
-set (SYNERGY_VERSION_MINOR 10)
-set (SYNERGY_VERSION_PATCH 3)
+set (SYNERGY_VERSION_MINOR 11)
+set (SYNERGY_VERSION_PATCH 0)
set (SYNERGY_VERSION_STAGE "stable")
#
diff --git a/dist/wix/Include.wxi.in b/dist/wix/Include.wxi.in
index c0391c7c..e126a26c 100644
--- a/dist/wix/Include.wxi.in
+++ b/dist/wix/Include.wxi.in
@@ -2,7 +2,6 @@
-
@@ -11,15 +10,14 @@
-
-
+
-
+
diff --git a/dist/wix/Product.wxs b/dist/wix/Product.wxs
index dce91922..29606c26 100644
--- a/dist/wix/Product.wxs
+++ b/dist/wix/Product.wxs
@@ -13,8 +13,10 @@
+
+
@@ -36,6 +38,11 @@
= 602)]]>
+
+
+
+
+
@@ -71,6 +78,7 @@
+
@@ -89,7 +97,6 @@
-
@@ -124,12 +131,17 @@
+
+
+
+
+
-
+
-
+
diff --git a/dist/wix/Synergy.sln b/dist/wix/Synergy.sln
index 81ed8811..5451972b 100644
--- a/dist/wix/Synergy.sln
+++ b/dist/wix/Synergy.sln
@@ -1,6 +1,7 @@
-Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio 14
-VisualStudioVersion = 14.0.23107.0
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 16
+VisualStudioVersion = 16.0.29411.108
MinimumVisualStudioVersion = 10.0.40219.1
Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "Synergy", "Synergy.wixproj", "{D4BA9F39-6A35-4C8F-9CB2-67FCBE5CAB17}"
EndProject
@@ -24,4 +25,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {2E0AA1C9-0F14-4FE4-8F18-430484EFBACE}
+ EndGlobalSection
EndGlobal
diff --git a/dist/wix/Synergy.wixproj b/dist/wix/Synergy.wixproj
index 003665ee..b340455f 100644
--- a/dist/wix/Synergy.wixproj
+++ b/dist/wix/Synergy.wixproj
@@ -1,31 +1,33 @@
-
- 3.10
- {d4ba9f39-6a35-4c8f-9cb2-67fcbe5cab17}
- 2.0
- Synergy
- Package
- $(MSBuildExtensionsPath32)\Microsoft\WiX\v3.x\Wix.targets
- $(MSBuildExtensionsPath)\Microsoft\WiX\v3.x\Wix.targets
- bin\$(Configuration)\
- wix\obj\$(Configuration)\
-
-
-
- $(WixExtDir)\WixFirewallExtension.dll
- WixFirewallExtension
-
-
- $(WixExtDir)\WixUtilExtension.dll
- WixUtilExtension
-
-
- $(WixExtDir)\WixUIExtension.dll
- WixUIExtension
-
-
-
-
-
+
+ 3.11
+ {d4ba9f39-6a35-4c8f-9cb2-67fcbe5cab17}
+ 2.0
+ Synergy
+ Package
+ bin\$(Configuration)\
+ wix\obj\$(Configuration)\
+
+
+
+
+
+
+
+
+ C:\Program Files (x86)\WiX Toolset v3.11\bin\WixUtilExtension.dll
+ WixUtilExtension
+
+
+ C:\Program Files (x86)\WiX Toolset v3.11\bin\WixUIExtension.dll
+ WixUIExtension
+
+
+ C:\Program Files (x86)\WiX Toolset v3.11\bin\WixFirewallExtension.dll
+ WixFirewallExtension
+
+
+
+
\ No newline at end of file
diff --git a/src/gui/src/AboutDialogBase.ui b/src/gui/src/AboutDialogBase.ui
index b20e3a50..b2cbe5e1 100644
--- a/src/gui/src/AboutDialogBase.ui
+++ b/src/gui/src/AboutDialogBase.ui
@@ -52,7 +52,7 @@
<p>
Keyboard and mouse sharing application. Cross platform and open source.<br /><br />
-Copyright © 2012-2016 Symless Ltd.<br />
+Copyright © 2012-2019 Symless Ltd.<br />
Copyright © 2002-2012 Chris Schoeneman, Nick Bolton, Volker Lanz.<br /><br />
Synergy is released under the GNU General Public License (GPLv2).<br /><br />
Synergy is based on CosmoSynergy by Richard Lee and Adam Feder.<br />
diff --git a/src/gui/src/Action.cpp b/src/gui/src/Action.cpp
index 5d3792ef..0fdbed18 100644
--- a/src/gui/src/Action.cpp
+++ b/src/gui/src/Action.cpp
@@ -24,7 +24,7 @@
const char* Action::m_ActionTypeNames[] =
{
"keyDown", "keyUp", "keystroke",
- "switchToScreen", "switchInDirection", "lockCursorToScreen",
+ "switchToScreen", "switchInDirection", "lockCursorToScreen", "restartServer",
"mouseDown", "mouseUp", "mousebutton"
};
@@ -87,6 +87,9 @@ QString Action::text() const
text += m_LockCursorModeNames[m_LockCursorMode];
break;
+ case restartAllConnections:
+ text += "restart";
+ break;
default:
Q_ASSERT(0);
break;
@@ -116,6 +119,7 @@ void Action::loadSettings(QSettings& settings)
setLockCursorMode(settings.value("lockCursorToScreen", lockCursorToggle).toInt());
setActiveOnRelease(settings.value("activeOnRelease", false).toBool());
setHaveScreens(settings.value("hasScreens", false).toBool());
+ setRestartServer(settings.value("restartServer", false).toBool());
}
void Action::saveSettings(QSettings& settings) const
@@ -136,6 +140,7 @@ void Action::saveSettings(QSettings& settings) const
settings.setValue("lockCursorToScreen", lockCursorMode());
settings.setValue("activeOnRelease", activeOnRelease());
settings.setValue("hasScreens", haveScreens());
+ settings.setValue("restartServer", restartServer());
}
QTextStream& operator<<(QTextStream& outStream, const Action& action)
diff --git a/src/gui/src/Action.h b/src/gui/src/Action.h
index dea98df6..6917233c 100644
--- a/src/gui/src/Action.h
+++ b/src/gui/src/Action.h
@@ -36,9 +36,29 @@ class Action
friend QTextStream& operator<<(QTextStream& outStream, const Action& action);
public:
- enum ActionType { keyDown, keyUp, keystroke, switchToScreen, switchInDirection, lockCursorToScreen, mouseDown, mouseUp, mousebutton };
- enum SwitchDirection { switchLeft, switchRight, switchUp, switchDown };
- enum LockCursorMode { lockCursorToggle, lockCursonOn, lockCursorOff };
+ enum ActionType {
+ keyDown,
+ keyUp,
+ keystroke,
+ switchToScreen,
+ switchInDirection,
+ lockCursorToScreen,
+ restartAllConnections,
+ mouseDown,
+ mouseUp,
+ mousebutton,
+ };
+ enum SwitchDirection {
+ switchLeft,
+ switchRight,
+ switchUp,
+ switchDown
+ };
+ enum LockCursorMode {
+ lockCursorToggle,
+ lockCursonOn,
+ lockCursorOff
+ };
public:
Action();
@@ -55,6 +75,7 @@ class Action
int lockCursorMode() const { return m_LockCursorMode; }
bool activeOnRelease() const { return m_ActiveOnRelease; }
bool haveScreens() const { return m_HasScreens; }
+ bool restartServer() const { return m_restartServer; }
protected:
KeySequence& keySequence() { return m_KeySequence; }
@@ -66,6 +87,7 @@ class Action
void setLockCursorMode(int m) { m_LockCursorMode = m; }
void setActiveOnRelease(bool b) { m_ActiveOnRelease = b; }
void setHaveScreens(bool b) { m_HasScreens = b; }
+ void setRestartServer( bool b) { m_restartServer = b; }
private:
KeySequence m_KeySequence;
@@ -76,6 +98,7 @@ class Action
int m_LockCursorMode;
bool m_ActiveOnRelease;
bool m_HasScreens;
+ bool m_restartServer;
static const char* m_ActionTypeNames[];
static const char* m_SwitchDirectionNames[];
diff --git a/src/gui/src/ActionDialog.cpp b/src/gui/src/ActionDialog.cpp
index 7518015e..d8961a2c 100644
--- a/src/gui/src/ActionDialog.cpp
+++ b/src/gui/src/ActionDialog.cpp
@@ -39,7 +39,7 @@ ActionDialog::ActionDialog(QWidget* parent, ServerConfig& config, Hotkey& hotkey
// work around Qt Designer's lack of a QButtonGroup; we need it to get
// at the button id of the checked radio button
- QRadioButton* const typeButtons[] = { m_pRadioPress, m_pRadioRelease, m_pRadioPressAndRelease, m_pRadioSwitchToScreen, m_pRadioSwitchInDirection, m_pRadioLockCursorToScreen };
+ QRadioButton* const typeButtons[] = { m_pRadioPress, m_pRadioRelease, m_pRadioPressAndRelease, m_pRadioSwitchToScreen, m_pRadioSwitchInDirection, m_pRadioLockCursorToScreen , m_pRadioRestartAllConnections};
for (unsigned int i = 0; i < sizeof(typeButtons) / sizeof(typeButtons[0]); i++)
m_pButtonGroupType->addButton(typeButtons[i], i);
@@ -91,6 +91,7 @@ void ActionDialog::accept()
m_Action.setSwitchDirection(m_pComboSwitchInDirection->currentIndex());
m_Action.setLockCursorMode(m_pComboLockCursorToScreen->currentIndex());
m_Action.setActiveOnRelease(m_pRadioHotkeyReleased->isChecked());
+ m_Action.setRestartServer(m_pRadioRestartAllConnections->isChecked());
QDialog::accept();
}
diff --git a/src/gui/src/ActionDialogBase.ui b/src/gui/src/ActionDialogBase.ui
index f6dff784..c4af18cd 100644
--- a/src/gui/src/ActionDialogBase.ui
+++ b/src/gui/src/ActionDialogBase.ui
@@ -239,6 +239,17 @@
+ -
+
+
-
+
+
+ Restart server
+
+
+
+
+
@@ -369,6 +380,22 @@
+
+ m_pRadioRestartAllConnections
+ toggled(bool)
+ m_pKeySequenceWidgetHotkey
+ setDisabled(bool)
+
+
+ 101
+ 353
+
+
+ 68
+ 126
+
+
+
m_pRadioPress
toggled(bool)
@@ -561,6 +588,22 @@
+
+ m_pRadioRestartAllConnections
+ toggled(bool)
+ m_pGroupBoxScreens
+ setDisabled(bool)
+
+
+ 48
+ 339
+
+
+ 79
+ 234
+
+
+
m_pRadioSwitchToScreen
toggled(bool)
diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp
index 499ca99b..33c89ae4 100644
--- a/src/gui/src/MainWindow.cpp
+++ b/src/gui/src/MainWindow.cpp
@@ -140,6 +140,8 @@ MainWindow::MainWindow (QSettings& settings, AppConfig& appConfig,
// hide padlock icon
secureSocket(false);
+ sslToggled(appConfig.getCryptoEnabled());
+
connect (this, SIGNAL(windowShown()),
this, SLOT(on_windowShown()), Qt::QueuedConnection);
#ifndef SYNERGY_ENTERPRISE
@@ -1104,12 +1106,6 @@ void MainWindow::setEdition(Edition edition)
#ifndef SYNERGY_ENTERPRISE
setWindowTitle(m_LicenseManager->getEditionName (edition));
#endif
- if (m_AppConfig->getCryptoEnabled()) {
- m_pSslCertificate = new SslCertificate(this);
- m_pSslCertificate->generateCertificate();
- }
- updateLocalFingerprint();
- saveSettings();
}
#ifndef SYNERGY_ENTERPRISE
diff --git a/src/gui/src/SslCertificate.cpp b/src/gui/src/SslCertificate.cpp
index 1ea4b259..ffa77b7e 100644
--- a/src/gui/src/SslCertificate.cpp
+++ b/src/gui/src/SslCertificate.cpp
@@ -25,8 +25,8 @@
-static const char kCertificateKeyLength[] = "rsa:1024"; //RSA Bit length (e.g. 1024/2048/4096)
-static const char kCertificateHashAlgorithm[] = "-sha1"; //fingerprint hashing algorithm
+static const char kCertificateKeyLength[] = "rsa:2048"; //RSA Bit length (e.g. 1024/2048/4096)
+static const char kCertificateHashAlgorithm[] = "-sha256"; //fingerprint hashing algorithm
static const char kCertificateLifetime[] = "365";
static const char kCertificateSubjectInfo[] = "/CN=Synergy";
static const char kCertificateFilename[] = "Synergy.pem";
@@ -34,8 +34,8 @@ static const char kSslDir[] = "SSL";
static const char kUnixOpenSslCommand[] = "openssl";
#if defined(Q_OS_WIN)
-static const char kWinOpenSslBinary[] = "OpenSSL\\openssl.exe";
-static const char kConfigFile[] = "OpenSSL\\synergy.conf";
+static const char kWinOpenSslBinary[] = "openssl.exe";
+static const char kConfigFile[] = "synergy.conf";
#endif
SslCertificate::SslCertificate(QObject *parent) :
diff --git a/src/gui/src/VersionChecker.cpp b/src/gui/src/VersionChecker.cpp
index f46968d1..27956b74 100644
--- a/src/gui/src/VersionChecker.cpp
+++ b/src/gui/src/VersionChecker.cpp
@@ -27,7 +27,7 @@
#define VERSION_REGEX "(\\d+\\.\\d+\\.\\d+-[a-z1-9]*)"
#define VERSION_REGEX_SECTIONED "(\\d+)\\.(\\d+)\\.(\\d+)-([a-z1-9]*)"
#define VERSION_SEGMENT_COUNT 4
-#define VERSION_URL "http://version.symless.com/synergy"
+#define VERSION_URL "https://version.symless.com/synergy"
VersionChecker::VersionChecker()
@@ -45,7 +45,10 @@ VersionChecker::~VersionChecker()
void VersionChecker::checkLatest()
{
- m_manager->get(QNetworkRequest(QUrl(VERSION_URL)));
+ auto request = QNetworkRequest(QUrl(VERSION_URL));
+ request.setHeader(QNetworkRequest::UserAgentHeader, QString("Synergy (") + getVersion() + ") " + QSysInfo::prettyProductName());
+ request.setRawHeader("X-Synergy-Version", getVersion().toStdString().c_str() );
+ m_manager->get(request);
}
void VersionChecker::replyFinished(QNetworkReply* reply)
diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt
index 7b78bc96..6a117e2a 100644
--- a/src/lib/CMakeLists.txt
+++ b/src/lib/CMakeLists.txt
@@ -26,7 +26,3 @@ add_subdirectory(platform)
add_subdirectory(server)
add_subdirectory(synergy)
add_subdirectory(shared)
-
-if (WIN32)
- add_subdirectory(synwinhk)
-endif()
diff --git a/src/lib/net/SecureSocket.cpp b/src/lib/net/SecureSocket.cpp
index 938a369b..d82547c2 100644
--- a/src/lib/net/SecureSocket.cpp
+++ b/src/lib/net/SecureSocket.cpp
@@ -27,6 +27,8 @@
#include
#include
#include
+#include
+#include
#include
#include
#include
@@ -37,15 +39,6 @@
#define MAX_ERROR_SIZE 65535
-//Add the new function names in case older ones are deprecated
-#if OPENSSL_VERSION_NUMBER > 0x10100000L
-#define SSL_SERVER_METHOD TLS_server_method
-#define SSL_CLIENT_METHOD TLS_client_method
-#else
-#define SSL_SERVER_METHOD SSLv23_server_method
-#define SSL_CLIENT_METHOD SSLv23_server_method
-#endif
-
static const float s_retryDelay = 0.01f;
enum {
@@ -392,16 +385,19 @@ SecureSocket::initContext(bool server)
}
if (server) {
- method = SSL_SERVER_METHOD();
+ method = SSLv23_server_method();
}
else {
- method = SSL_CLIENT_METHOD();
+ method = SSLv23_client_method();
}
// create new context from method
SSL_METHOD* m = const_cast(method);
m_ssl->m_context = SSL_CTX_new(m);
+ //Prevent the usage of of all version prior to TLSv1.2 as they are known to be vulnerable
+ SSL_CTX_set_options(m_ssl->m_context, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1);
+
if (m_ssl->m_context == NULL) {
showError();
}
@@ -856,9 +852,28 @@ SecureSocket::showSecureConnectInfo()
char msg[kMsgSize];
SSL_CIPHER_description(cipher, msg, kMsgSize);
LOG((CLOG_DEBUG "openssl cipher: %s", msg));
+
+ //For some reason SSL_get_version is return mismatching information to SSL_CIPHER_description
+ // so grab the version out the description instead, This seems like a hacky way of doing it.
+ // But when the cipher says "TLSv1.2" but the get_version returns "TLSv1/SSLv3" we it doesn't look right
+ // For some reason macOS hates regex's so stringstream is used
- LOG((CLOG_INFO "network encryption protocol: %s", SSL_CIPHER_get_version(cipher)));
+ std::istringstream iss(msg);
+ //Take the stream input and splits it into a vetor directly
+ const std::vector parts{std::istream_iterator{iss},
+ std::istream_iterator{}};
+ if (parts.size() > 2)
+ {
+ //log the section containing the protocol version
+ LOG((CLOG_INFO "network encryption protocol: %s", parts[1].c_str()));
+ }
+ else
+ {
+ //log the error in spliting then display the whole description rather then nothing
+ LOG((CLOG_ERR "could not split cipher for protocol"));
+ LOG((CLOG_INFO "network encryption protocol: %s", msg));
+ }
}
else {
LOG((CLOG_ERR "could not get secure socket cipher"));
diff --git a/src/lib/platform/MSWindowsDesks.cpp b/src/lib/platform/MSWindowsDesks.cpp
index c377ab76..41802891 100644
--- a/src/lib/platform/MSWindowsDesks.cpp
+++ b/src/lib/platform/MSWindowsDesks.cpp
@@ -18,7 +18,7 @@
#include "platform/MSWindowsDesks.h"
-#include "synwinhk/synwinhk.h"
+#include "platform/synwinhk.h"
#include "platform/MSWindowsScreen.h"
#include "synergy/IScreenSaver.h"
#include "synergy/XScreen.h"
@@ -93,7 +93,7 @@
//
MSWindowsDesks::MSWindowsDesks(
- bool isPrimary, bool noHooks, HINSTANCE hookLibrary,
+ bool isPrimary, bool noHooks,
const IScreenSaver* screensaver, IEventQueue* events,
IJob* updateKeys, bool stopOnDeskSwitch) :
m_isPrimary(isPrimary),
@@ -114,8 +114,6 @@ MSWindowsDesks::MSWindowsDesks(
m_events(events),
m_stopOnDeskSwitch(stopOnDeskSwitch)
{
- if (hookLibrary != NULL)
- queryHookLibrary(hookLibrary);
m_cursor = createBlankCursor();
m_deskClass = createDeskWindowClass(m_isPrimary);
@@ -348,35 +346,6 @@ MSWindowsDesks::sendMessage(UINT msg, WPARAM wParam, LPARAM lParam) const
}
}
-void
-MSWindowsDesks::queryHookLibrary(HINSTANCE hookLibrary)
-{
- // look up functions
- if (m_isPrimary && !m_noHooks) {
- m_install = (InstallFunc)GetProcAddress(hookLibrary, "install");
- m_uninstall = (UninstallFunc)GetProcAddress(hookLibrary, "uninstall");
- m_installScreensaver =
- (InstallScreenSaverFunc)GetProcAddress(
- hookLibrary, "installScreenSaver");
- m_uninstallScreensaver =
- (UninstallScreenSaverFunc)GetProcAddress(
- hookLibrary, "uninstallScreenSaver");
- if (m_install == NULL ||
- m_uninstall == NULL ||
- m_installScreensaver == NULL ||
- m_uninstallScreensaver == NULL) {
- LOG((CLOG_ERR "Invalid hook library"));
- throw XScreenOpenFailure();
- }
- }
- else {
- m_install = NULL;
- m_uninstall = NULL;
- m_installScreensaver = NULL;
- m_uninstallScreensaver = NULL;
- }
-}
-
HCURSOR
MSWindowsDesks::createBlankCursor() const
{
@@ -690,13 +659,13 @@ MSWindowsDesks::deskThread(void* vdesk)
continue;
case SYNERGY_MSG_SWITCH:
- if (m_isPrimary && !m_noHooks) {
- m_uninstall();
+ if (!m_noHooks) {
+ MSWindowsHook::uninstall();
if (m_screensaverNotify) {
- m_uninstallScreensaver();
- m_installScreensaver();
+ MSWindowsHook::uninstallScreenSaver();
+ MSWindowsHook::installScreenSaver();
}
- switch (m_install()) {
+ switch (MSWindowsHook::install()) {
case kHOOK_FAILED:
// we won't work on this desk
desk->m_lowLevel = false;
@@ -772,10 +741,10 @@ MSWindowsDesks::deskThread(void* vdesk)
case SYNERGY_MSG_SCREENSAVER:
if (!m_noHooks) {
if (msg.wParam != 0) {
- m_installScreensaver();
+ MSWindowsHook::installScreenSaver();
}
else {
- m_uninstallScreensaver();
+ MSWindowsHook::uninstallScreenSaver();
}
}
break;
diff --git a/src/lib/platform/MSWindowsDesks.h b/src/lib/platform/MSWindowsDesks.h
index d12900a1..84ded7c3 100644
--- a/src/lib/platform/MSWindowsDesks.h
+++ b/src/lib/platform/MSWindowsDesks.h
@@ -18,7 +18,7 @@
#pragma once
-#include "synwinhk/synwinhk.h"
+#include "platform/synwinhk.h"
#include "synergy/key_types.h"
#include "synergy/mouse_types.h"
#include "synergy/option_types.h"
@@ -65,7 +65,7 @@ public:
\p hookLibrary must be a handle to the hook library.
*/
MSWindowsDesks(
- bool isPrimary, bool noHooks, HINSTANCE hookLibrary,
+ bool isPrimary, bool noHooks,
const IScreenSaver* screensaver, IEventQueue* events,
IJob* updateKeys, bool stopOnDeskSwitch);
~MSWindowsDesks();
@@ -206,7 +206,6 @@ private:
typedef std::map Desks;
// initialization and shutdown operations
- void queryHookLibrary(HINSTANCE hookLibrary);
HCURSOR createBlankCursor() const;
void destroyCursor(HCURSOR cursor) const;
ATOM createDeskWindowClass(bool isPrimary) const;
@@ -283,14 +282,6 @@ private:
CondVar m_deskReady;
Desks m_desks;
- // hook library stuff
- InstallFunc m_install;
- UninstallFunc m_uninstall;
- InstallScreenSaverFunc
- m_installScreensaver;
- UninstallScreenSaverFunc
- m_uninstallScreensaver;
-
// keyboard stuff
IJob* m_updateKeys;
HKL m_keyLayout;
diff --git a/src/lib/platform/MSWindowsHook.cpp b/src/lib/platform/MSWindowsHook.cpp
index b81d9373..03b2ae67 100644
--- a/src/lib/platform/MSWindowsHook.cpp
+++ b/src/lib/platform/MSWindowsHook.cpp
@@ -17,19 +17,35 @@
*/
#include "platform/MSWindowsHook.h"
-
+#include "synergy/protocol_types.h"
#include "synergy/XScreen.h"
#include "base/Log.h"
static const char* g_name = "synwinhk";
-MSWindowsHook::MSWindowsHook() :
- m_initFunc(NULL),
- m_cleanupFunc(NULL),
- m_setSidesFunc(NULL),
- m_setZoneFunc(NULL),
- m_setModeFunc(NULL),
- m_instance(NULL)
+static DWORD g_processID = 0;
+static DWORD g_threadID = 0;
+static HHOOK g_getMessage = NULL;
+static HHOOK g_keyboardLL = NULL;
+static HHOOK g_mouseLL = NULL;
+static bool g_screenSaver = false;
+static EHookMode g_mode = kHOOK_DISABLE;
+static UInt32 g_zoneSides = 0;
+static SInt32 g_zoneSize = 0;
+static SInt32 g_xScreen = 0;
+static SInt32 g_yScreen = 0;
+static SInt32 g_wScreen = 0;
+static SInt32 g_hScreen = 0;
+static WPARAM g_deadVirtKey = 0;
+static WPARAM g_deadRelease = 0;
+static LPARAM g_deadLParam = 0;
+static BYTE g_deadKeyState[256] = { 0 };
+static BYTE g_keyState[256] = { 0 };
+static DWORD g_hookThread = 0;
+static bool g_fakeServerInput = false;
+static BOOL g_isPrimary = TRUE;
+
+MSWindowsHook::MSWindowsHook()
{
}
@@ -37,35 +53,18 @@ MSWindowsHook::~MSWindowsHook()
{
cleanup();
- if (m_instance != NULL) {
- FreeLibrary(m_instance);
+ if (g_processID == GetCurrentProcessId()) {
+ uninstall();
+ uninstallScreenSaver();
+ g_processID = 0;
}
}
void
MSWindowsHook::loadLibrary()
{
- // load library
- m_instance = LoadLibrary(g_name);
- if (m_instance == NULL) {
- LOG((CLOG_ERR "failed to load hook library, %s.dll is missing or invalid", g_name));
- throw XScreenOpenFailure();
- }
-
- // look up functions
- m_setSidesFunc = (SetSidesFunc)GetProcAddress(m_instance, "setSides");
- m_setZoneFunc = (SetZoneFunc)GetProcAddress(m_instance, "setZone");
- m_setModeFunc = (SetModeFunc)GetProcAddress(m_instance, "setMode");
- m_initFunc = (InitFunc)GetProcAddress(m_instance, "init");
- m_cleanupFunc = (CleanupFunc)GetProcAddress(m_instance, "cleanup");
-
- if (m_setSidesFunc == NULL ||
- m_setZoneFunc == NULL ||
- m_setModeFunc == NULL ||
- m_initFunc == NULL ||
- m_cleanupFunc == NULL) {
- LOG((CLOG_ERR "failed to load hook function, %s.dll could be out of date", g_name));
- throw XScreenOpenFailure();
+ if (g_processID == 0) {
+ g_processID = GetCurrentProcessId();
}
// initialize library
@@ -76,53 +75,703 @@ MSWindowsHook::loadLibrary()
}
}
-HINSTANCE
-MSWindowsHook::getInstance() const
-{
- return m_instance;
-}
-
int
MSWindowsHook::init(DWORD threadID)
{
- if (m_initFunc == NULL) {
- return NULL;
+ // 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).
+ int result = CloseHandle(process);
+ if (result == false) {
+ return 0;
+ }
+ }
+
+ // clean up after old process. the system should've already
+ // removed the hooks so we just need to reset our state.
+ g_processID = GetCurrentProcessId();
+ g_threadID = 0;
+ g_getMessage = NULL;
+ g_keyboardLL = NULL;
+ g_mouseLL = NULL;
+ g_screenSaver = false;
}
- return m_initFunc(threadID);
+
+ // save thread id. we'll post messages to this thread's
+ // message queue.
+ g_threadID = threadID;
+
+ // set defaults
+ g_mode = kHOOK_DISABLE;
+ g_zoneSides = 0;
+ g_zoneSize = 0;
+ g_xScreen = 0;
+ g_yScreen = 0;
+ g_wScreen = 0;
+ g_hScreen = 0;
+
+ return 1;
}
int
MSWindowsHook::cleanup()
{
- if (m_cleanupFunc == NULL) {
- return NULL;
+ if (g_processID == GetCurrentProcessId()) {
+ g_threadID = 0;
}
- return m_cleanupFunc();
+
+ return 1;
}
void
MSWindowsHook::setSides(UInt32 sides)
{
- if (m_setSidesFunc == NULL) {
- return;
- }
- m_setSidesFunc(sides);
+ g_zoneSides = sides;
}
void
MSWindowsHook::setZone(SInt32 x, SInt32 y, SInt32 w, SInt32 h, SInt32 jumpZoneSize)
{
- if (m_setZoneFunc == NULL) {
- return;
- }
- m_setZoneFunc(x, y, w, h, jumpZoneSize);
+ g_zoneSize = jumpZoneSize;
+ g_xScreen = x;
+ g_yScreen = y;
+ g_wScreen = w;
+ g_hScreen = h;
}
void
MSWindowsHook::setMode(EHookMode mode)
{
- if (m_setModeFunc == NULL) {
+ if (mode == g_mode) {
+ // no change
return;
}
- m_setModeFunc(mode);
+ g_mode = mode;
+}
+
+static
+void
+keyboardGetState(BYTE keys[256], DWORD vkCode, bool kf_up)
+{
+ // 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.
+ if (vkCode < 0 || vkCode >= 256) {
+ return;
+ }
+
+ // Keep track of key state on our own in case GetAsyncKeyState() fails
+ g_keyState[vkCode] = kf_up ? 0 : 0x80;
+ g_keyState[VK_SHIFT] = g_keyState[VK_LSHIFT] | g_keyState[VK_RSHIFT];
+
+ SHORT key;
+ // Test whether GetAsyncKeyState() is being honest with us
+ key = GetAsyncKeyState(vkCode);
+
+ if (key & 0x80) {
+ // The only time we know for sure that GetAsyncKeyState() is working
+ // is when it tells us that the current key is down.
+ // In this case, update g_keyState to reflect what GetAsyncKeyState()
+ // is telling us, just in case we have gotten out of sync
+
+ for (int i = 0; i < 256; ++i) {
+ key = GetAsyncKeyState(i);
+ g_keyState[i] = (BYTE)((key < 0) ? 0x80u : 0);
+ }
+ }
+
+ // copy g_keyState to keys
+ for (int i = 0; i < 256; ++i) {
+ keys[i] = g_keyState[i];
+ }
+
+ key = GetKeyState(VK_CAPITAL);
+ keys[VK_CAPITAL] = (BYTE)(((key < 0) ? 0x80 : 0) | (key & 1));
+}
+
+static
+WPARAM
+makeKeyMsg(UINT virtKey, char c, bool noAltGr)
+{
+ return MAKEWPARAM(MAKEWORD(virtKey & 0xff, (BYTE)c), noAltGr ? 1 : 0);
+}
+
+static
+bool
+keyboardHookHandler(WPARAM wParam, LPARAM lParam)
+{
+ DWORD vkCode = static_cast(wParam);
+ bool kf_up = (lParam & (KF_UP << 16)) != 0;
+
+ // 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_fakeServerInput = ((lParam & 0x80000000u) == 0);
+ PostThreadMessage(g_threadID, SYNERGY_MSG_DEBUG,
+ 0xff000000u | wParam, lParam);
+
+ // discard event
+ return true;
+ }
+
+ // if we're expecting fake input then just pass the event through
+ // and do not forward to the server
+ if (g_fakeServerInput) {
+ 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 || g_deadRelease == wParam) &&
+ (lParam & 0x80000000u) != 0) {
+ g_deadRelease = 0;
+ PostThreadMessage(g_threadID, SYNERGY_MSG_DEBUG,
+ wParam | 0x04000000, lParam);
+ return false;
+ }
+
+ // we need the keyboard state for ToAscii()
+ BYTE keys[256];
+ keyboardGetState(keys, vkCode, kf_up);
+
+ // 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;
+ }
+ }
+
+ WORD c = 0;
+
+ // 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) {
+ if (ToAscii((UINT)g_deadVirtKey, (g_deadLParam & 0x10ff0000u) >> 16,
+ g_deadKeyState, &c, flags) == 2)
+ {
+ // If ToAscii returned 2, it means that we accidentally removed
+ // a double dead key instead of restoring it. Thus, we call
+ // ToAscii again with the same parameters to restore the
+ // internal dead key state.
+ ToAscii((UINT)g_deadVirtKey, (g_deadLParam & 0x10ff0000u) >> 16,
+ g_deadKeyState, &c, flags);
+
+ // We need to keep track of this because g_deadVirtKey will be
+ // cleared later on; this would cause the dead key release to
+ // incorrectly restore the dead key state.
+ g_deadRelease = g_deadVirtKey;
+ }
+ }
+
+ UINT scanCode = ((lParam & 0x10ff0000u) >> 16);
+ int n = ToAscii((UINT)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,
+ wParam | 0x05000000, lParam);
+ if (g_deadVirtKey != 0) {
+ if (ToAscii((UINT)g_deadVirtKey, (g_deadLParam & 0x10ff0000u) >> 16,
+ g_deadKeyState, &c, flags) == 2)
+ {
+ ToAscii((UINT)g_deadVirtKey, (g_deadLParam & 0x10ff0000u) >> 16,
+ g_deadKeyState, &c, flags);
+ g_deadRelease = g_deadVirtKey;
+ }
+ }
+ 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((UINT)wParam, scanCode, keys2, &c, flags);
+ }
+
+ PostThreadMessage(g_threadID, SYNERGY_MSG_DEBUG,
+ wParam | ((c & 0xff) << 8) |
+ ((n & 0xff) << 16) | 0x06000000,
+ lParam);
+ WPARAM charAndVirtKey = 0;
+ bool clearDeadKey = false;
+ switch (n) {
+ default:
+ // key is a dead key
+
+ if (lParam & 0x80000000u)
+ // This handles the obscure situation where a key has been
+ // pressed which is both a dead key and a normal character
+ // depending on which modifiers have been pressed. We
+ // break here to prevent it from being considered a dead
+ // key.
+ break;
+
+ 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((UINT)wParam, (char)0, noAltGr);
+ break;
+
+ case 1:
+ // key maps to a character composed with dead key
+ charAndVirtKey = makeKeyMsg((UINT)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((UINT)g_deadVirtKey, (char)LOBYTE(c), noAltGr);
+ PostThreadMessage(g_threadID, SYNERGY_MSG_KEY,
+ deadCharAndVirtKey, g_deadLParam & 0x7fffffffu);
+ PostThreadMessage(g_threadID, SYNERGY_MSG_KEY,
+ deadCharAndVirtKey, g_deadLParam | 0x80000000u);
+
+ // use uncomposed character
+ charAndVirtKey = makeKeyMsg((UINT)wParam, (char)HIBYTE(c), noAltGr);
+ clearDeadKey = true;
+ break;
+ }
+ }
+
+ // put back the dead key, if any, for the application to use
+ if (g_deadVirtKey != 0) {
+ ToAscii((UINT)g_deadVirtKey, (g_deadLParam & 0x10ff0000u) >> 16,
+ g_deadKeyState, &c, flags);
+ }
+
+ // clear out old dead key state
+ if (clearDeadKey) {
+ g_deadVirtKey = 0;
+ g_deadLParam = 0;
+ }
+
+ // 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.
+ // XXX -- with hot keys for actions we may only need to do this when
+ // forwarding.
+ if (charAndVirtKey != 0) {
+ PostThreadMessage(g_threadID, SYNERGY_MSG_DEBUG,
+ charAndVirtKey | 0x07000000, lParam);
+ PostThreadMessage(g_threadID, SYNERGY_MSG_KEY, charAndVirtKey, lParam);
+ }
+
+ if (g_mode == kHOOK_RELAY_EVENTS) {
+ // 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;
+
+ case VK_HANGUL:
+ // pass these modifiers if using a low level hook, discard
+ // them if not.
+ if (g_hookThread == 0) {
+ return true;
+ }
+ break;
+
+ default:
+ // discard
+ return true;
+ }
+ }
+
+ return false;
+}
+
+
+#if !NO_GRAB_KEYBOARD
+static
+LRESULT CALLBACK
+keyboardLLHook(int code, WPARAM wParam, LPARAM lParam)
+{
+ if (code >= 0) {
+ // decode the message
+ KBDLLHOOKSTRUCT* info = reinterpret_cast(lParam);
+
+ bool const injected = info->flags & LLKHF_INJECTED;
+ if (!g_isPrimary && injected) {
+ return CallNextHookEx (g_keyboardLL, code, wParam, 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.
+
+ // handle the message
+ if (keyboardHookHandler(wParam, lParam)) {
+ return 1;
+ }
+ }
+
+ return CallNextHookEx(g_keyboardLL, code, wParam, lParam);
+}
+#endif
+
+//
+// low-level mouse hook -- this allows us to capture and handle mouse
+// events very early. the earlier the better.
+//
+
+static
+bool
+mouseHookHandler(WPARAM wParam, SInt32 x, SInt32 y, SInt32 data)
+{
+ switch (wParam) {
+ case WM_LBUTTONDOWN:
+ case WM_MBUTTONDOWN:
+ case WM_RBUTTONDOWN:
+ case WM_XBUTTONDOWN:
+ case WM_LBUTTONDBLCLK:
+ case WM_MBUTTONDBLCLK:
+ case WM_RBUTTONDBLCLK:
+ case WM_XBUTTONDBLCLK:
+ case WM_LBUTTONUP:
+ case WM_MBUTTONUP:
+ case WM_RBUTTONUP:
+ case WM_XBUTTONUP:
+ case WM_NCLBUTTONDOWN:
+ case WM_NCMBUTTONDOWN:
+ case WM_NCRBUTTONDOWN:
+ case WM_NCXBUTTONDOWN:
+ case WM_NCLBUTTONDBLCLK:
+ case WM_NCMBUTTONDBLCLK:
+ case WM_NCRBUTTONDBLCLK:
+ case WM_NCXBUTTONDBLCLK:
+ case WM_NCLBUTTONUP:
+ case WM_NCMBUTTONUP:
+ case WM_NCRBUTTONUP:
+ case WM_NCXBUTTONUP:
+ // always relay the event. eat it if relaying.
+ PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_BUTTON, wParam, data);
+ return (g_mode == kHOOK_RELAY_EVENTS);
+
+ case WM_MOUSEWHEEL:
+ if (g_mode == kHOOK_RELAY_EVENTS) {
+ // relay event
+ PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_WHEEL, data, 0);
+ }
+ return (g_mode == kHOOK_RELAY_EVENTS);
+
+ case WM_NCMOUSEMOVE:
+ case WM_MOUSEMOVE:
+ if (g_mode == kHOOK_RELAY_EVENTS) {
+ // relay and eat event
+ PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_MOVE, x, y);
+ return true;
+ }
+ else if (g_mode == kHOOK_WATCH_JUMP_ZONE) {
+ // 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.
+ // Server 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;
+ }
+
+ // check for mouse inside jump zone
+ bool inside = false;
+ if (!inside && (g_zoneSides & kLeftMask) != 0) {
+ inside = (x < g_xScreen + g_zoneSize);
+ }
+ if (!inside && (g_zoneSides & kRightMask) != 0) {
+ inside = (x >= g_xScreen + g_wScreen - g_zoneSize);
+ }
+ if (!inside && (g_zoneSides & kTopMask) != 0) {
+ inside = (y < g_yScreen + g_zoneSize);
+ }
+ if (!inside && (g_zoneSides & kBottomMask) != 0) {
+ inside = (y >= g_yScreen + g_hScreen - g_zoneSize);
+ }
+
+ // relay the event
+ PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_MOVE, x, y);
+
+ // if inside and not bogus then eat the event
+ return inside && !bogus;
+ }
+ }
+
+ // pass the event
+ return false;
+}
+
+
+static
+LRESULT CALLBACK
+mouseLLHook(int code, WPARAM wParam, LPARAM lParam)
+{
+ if (code >= 0) {
+ // decode the message
+ MSLLHOOKSTRUCT* info = reinterpret_cast(lParam);
+
+ bool const injected = info->flags & LLMHF_INJECTED;
+ if (!g_isPrimary && injected) {
+ return CallNextHookEx(g_mouseLL, code, wParam, lParam);
+ }
+
+ SInt32 x = static_cast(info->pt.x);
+ SInt32 y = static_cast(info->pt.y);
+ SInt32 w = static_cast(HIWORD(info->mouseData));
+
+ // handle the message
+ if (mouseHookHandler(wParam, x, y, w)) {
+ return 1;
+ }
+ }
+
+ return CallNextHookEx(g_mouseLL, code, wParam, lParam);
+}
+
+EHookResult
+MSWindowsHook::install()
+{
+ assert(g_getMessage == NULL || g_screenSaver);
+
+ // must be initialized
+ if (g_threadID == 0) {
+ return kHOOK_FAILED;
+ }
+
+ // discard old dead keys
+ g_deadVirtKey = 0;
+ g_deadLParam = 0;
+
+ // reset fake input flag
+ g_fakeServerInput = false;
+
+ // install low-level hooks. we require that they both get installed.
+ g_mouseLL = SetWindowsHookEx(WH_MOUSE_LL,
+ &mouseLLHook,
+ NULL,
+ 0);
+#if !NO_GRAB_KEYBOARD
+ g_keyboardLL = SetWindowsHookEx(WH_KEYBOARD_LL,
+ &keyboardLLHook,
+ NULL,
+ 0);
+ 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;
+ }
+ }
+#endif
+
+ // check that we got all the hooks we wanted
+ if ((g_mouseLL == NULL) ||
+#if !NO_GRAB_KEYBOARD
+ (g_keyboardLL == NULL)
+#endif
+ ) {
+ uninstall();
+ return kHOOK_FAILED;
+ }
+
+ if (g_keyboardLL != NULL || g_mouseLL != NULL) {
+ g_hookThread = GetCurrentThreadId();
+ return kHOOK_OKAY_LL;
+ }
+
+ return kHOOK_OKAY;
+}
+
+int
+MSWindowsHook::uninstall()
+{
+ // discard old dead keys
+ g_deadVirtKey = 0;
+ g_deadLParam = 0;
+
+ // uninstall hooks
+ if (g_keyboardLL != NULL) {
+ UnhookWindowsHookEx(g_keyboardLL);
+ g_keyboardLL = NULL;
+ }
+ if (g_mouseLL != NULL) {
+ UnhookWindowsHookEx(g_mouseLL);
+ g_mouseLL = NULL;
+ }
+ if (g_getMessage != NULL && !g_screenSaver) {
+ UnhookWindowsHookEx(g_getMessage);
+ g_getMessage = NULL;
+ }
+
+ return 1;
+}
+
+static
+LRESULT CALLBACK
+getMessageHook(int code, WPARAM wParam, LPARAM lParam)
+{
+ if (code >= 0) {
+ if (g_screenSaver) {
+ MSG* msg = reinterpret_cast(lParam);
+ if (msg->message == WM_SYSCOMMAND &&
+ msg->wParam == SC_SCREENSAVE) {
+ // broadcast screen saver started message
+ PostThreadMessage(g_threadID,
+ SYNERGY_MSG_SCREEN_SAVER, TRUE, 0);
+ }
+ }
+ }
+
+ return CallNextHookEx(g_getMessage, code, wParam, lParam);
+}
+
+int
+MSWindowsHook::installScreenSaver()
+{
+ // 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,
+ NULL,
+ 0);
+ }
+
+ return (g_getMessage != NULL) ? 1 : 0;
+}
+
+int
+MSWindowsHook::uninstallScreenSaver()
+{
+ // uninstall hook unless the mouse wheel hook is installed
+ if (g_getMessage != NULL) {
+ UnhookWindowsHookEx(g_getMessage);
+ g_getMessage = NULL;
+ }
+
+ // screen saver hook is no longer installed
+ g_screenSaver = false;
+
+ return 1;
}
diff --git a/src/lib/platform/MSWindowsHook.h b/src/lib/platform/MSWindowsHook.h
index 988528f8..cb61c6b1 100644
--- a/src/lib/platform/MSWindowsHook.h
+++ b/src/lib/platform/MSWindowsHook.h
@@ -18,7 +18,7 @@
#pragma once
-#include "synwinhk/synwinhk.h"
+#include "platform/synwinhk.h"
#define WIN32_LEAN_AND_MEAN
#include
@@ -30,19 +30,23 @@ public:
MSWindowsHook();
virtual ~MSWindowsHook();
- void loadLibrary();
- HINSTANCE getInstance() const;
- int init(DWORD threadID);
- int cleanup();
- void setSides(UInt32 sides);
- void setZone(SInt32 x, SInt32 y, SInt32 w, SInt32 h, SInt32 jumpZoneSize);
- void setMode(EHookMode mode);
+ void loadLibrary();
-private:
- InitFunc m_initFunc;
- CleanupFunc m_cleanupFunc;
- SetSidesFunc m_setSidesFunc;
- SetZoneFunc m_setZoneFunc;
- SetModeFunc m_setModeFunc;
- HINSTANCE m_instance;
+ int init(DWORD threadID);
+
+ int cleanup();
+
+ void setSides(UInt32 sides);
+
+ void setZone(SInt32 x, SInt32 y, SInt32 w, SInt32 h, SInt32 jumpZoneSize);
+
+ void setMode(EHookMode mode);
+
+ static EHookResult install();
+
+ static int uninstall();
+
+ static int installScreenSaver();
+
+ static int uninstallScreenSaver();
};
diff --git a/src/lib/platform/MSWindowsScreen.cpp b/src/lib/platform/MSWindowsScreen.cpp
index 2d7ac131..dfcc3707 100644
--- a/src/lib/platform/MSWindowsScreen.cpp
+++ b/src/lib/platform/MSWindowsScreen.cpp
@@ -139,7 +139,6 @@ MSWindowsScreen::MSWindowsScreen(
m_desks = new MSWindowsDesks(
m_isPrimary,
m_noHooks,
- m_hook.getInstance(),
m_screensaver,
m_events,
new TMethodJob(
@@ -329,6 +328,13 @@ MSWindowsScreen::enter()
bool
MSWindowsScreen::leave()
{
+ POINT pos;
+ if (!getThisCursorPos(&pos))
+ {
+ LOG((CLOG_DEBUG "Unable to leave screen as Windows security has disabled critical functions required to let synergy work"));
+ //unable to get position this means synergy will break if the cursor leaves the screen
+ return false;
+ }
// get keyboard layout of foreground window. we'll use this
// keyboard layout for translating keys sent to clients.
HWND window = GetForegroundWindow();
@@ -539,6 +545,60 @@ MSWindowsScreen::getCursorPos(SInt32& x, SInt32& y) const
m_desks->getCursorPos(x, y);
}
+/*
+ * getThisCursorPos and setThisCursorPos will attempt to negotiate with the system
+ * to try get the and set the mouse position, however on the logon screen due to
+ * hooks this process has it may unable to work around the problem. Although these
+ * functions did not fix the issue at hand (#5294) its worth keeping them here anyway.
+ */
+bool MSWindowsScreen::getThisCursorPos(LPPOINT pos)
+{
+ auto result = GetCursorPos(pos);
+ auto error = GetLastError();
+ LOG((CLOG_DEBUG3 "%s Attempt: 1 , status %d, code: %d Pos {%d, %d}", __func__, result, error, pos->x, pos->y));
+ if (!result)
+ {
+ result = GetCursorPos(pos);
+ error = GetLastError();
+ LOG((CLOG_DEBUG3 "%s Attempt: 2, status %d, code: %d Pos {%d, %d}", __func__, result, error, pos->x, pos->y));
+ updateDesktopThread();
+ }
+ return result;
+}
+
+bool MSWindowsScreen::setThisCursorPos(int x, int y)
+{
+ auto result = SetCursorPos(x, y);
+ auto error = GetLastError();
+ LOG((CLOG_DEBUG3 "%s Attempt: 1, status %d, code: %d", __func__, result, error));
+ if (!result)
+ {
+ result = SetCursorPos(x, y);
+ error = GetLastError();
+ LOG((CLOG_DEBUG3 "%s Attempt: 2, status %d, code: %d", __func__, result, error));
+ updateDesktopThread();
+ }
+
+ return result;
+}
+
+void MSWindowsScreen::updateDesktopThread()
+{
+
+ LOG((CLOG_DEBUG3 "Failed to set cursor Attempting to switch desktop"));
+ SetLastError(0);
+ HDESK cur_hdesk = OpenInputDesktop(0, true, GENERIC_ALL);
+
+ auto error = GetLastError();
+ LOG((CLOG_DEBUG3 "\tGetting desktop Handle: %p Status code: %d", cur_hdesk, error));
+
+ error = GetLastError();
+ LOG((CLOG_DEBUG3 "\tSetting desktop return: %d Status code: %d", SetThreadDesktop(cur_hdesk), GetLastError()));
+
+ CloseDesktop(cur_hdesk);
+
+}
+
void
MSWindowsScreen::reconfigure(UInt32 activeSides)
{
@@ -1524,12 +1584,12 @@ MSWindowsScreen::warpCursorNoFlush(SInt32 x, SInt32 y)
// warp mouse. hopefully this inserts a mouse motion event
// between the previous message and the following message.
- SetCursorPos(x, y);
+ setThisCursorPos(x, y);
// check to see if the mouse pos was set correctly
POINT cursorPos;
- GetCursorPos(&cursorPos);
-
+ getThisCursorPos(&cursorPos);
+
// there is a bug or round error in SetCursorPos and GetCursorPos on
// a high DPI setting. The check here is for Vista/7 login screen.
// since this feature is mainly for client, so only check on client.
diff --git a/src/lib/platform/MSWindowsScreen.h b/src/lib/platform/MSWindowsScreen.h
index 4b1f2c18..e7fab317 100644
--- a/src/lib/platform/MSWindowsScreen.h
+++ b/src/lib/platform/MSWindowsScreen.h
@@ -21,7 +21,7 @@
#include "platform/MSWindowsHook.h"
#include "synergy/PlatformScreen.h"
#include "synergy/DragInformation.h"
-#include "synwinhk/synwinhk.h"
+#include "platform/synwinhk.h"
#include "mt/CondVar.h"
#include "mt/Mutex.h"
#include "base/String.h"
@@ -75,6 +75,25 @@ public:
SInt32& width, SInt32& height) const;
virtual void getCursorPos(SInt32& x, SInt32& y) const;
+ /**
+ * \brief Get the position of the cursor on the current machine
+ * \param pos the object that the function will use to store the position of the cursor
+ * \return true if the function was successful
+ */
+ virtual bool getThisCursorPos(LPPOINT pos);
+ /**
+ * \brief Sets the cursor position on the current machine
+ * \param x The x coordinate of the cursor
+ * \param y The Y coordinate of the cursor
+ * \return True is successful
+ */
+ virtual bool setThisCursorPos(int x, int y);
+
+ /**
+ * \brief This function will attempt to switch to the current desktop the mouse is located on
+ */
+ virtual void updateDesktopThread();
+
// IPrimaryScreen overrides
virtual void reconfigure(UInt32 activeSides);
virtual void warpCursor(SInt32 x, SInt32 y);
@@ -155,6 +174,10 @@ private: // HACK
// the message should not be dispatched.
bool onPreDispatchPrimary(HWND, UINT, WPARAM, LPARAM);
+ // handle secondary message before it gets dispatched. returns true iff
+ // the message should not be dispatched.
+ bool onPreDispatchSecondary(HWND, UINT, WPARAM, LPARAM);
+
// handle message. returns true iff handled and optionally sets
// \c *result (which defaults to 0).
bool onEvent(HWND, UINT, WPARAM, LPARAM, LRESULT* result);
diff --git a/src/lib/synwinhk/synwinhk.h b/src/lib/platform/synwinhk.h
similarity index 70%
rename from src/lib/synwinhk/synwinhk.h
rename to src/lib/platform/synwinhk.h
index fe279248..05656176 100644
--- a/src/lib/synwinhk/synwinhk.h
+++ b/src/lib/platform/synwinhk.h
@@ -67,25 +67,4 @@ enum EHookMode {
kHOOK_RELAY_EVENTS
};
-typedef int (*InitFunc)(DWORD targetQueueThreadID);
-typedef int (*CleanupFunc)(void);
-typedef EHookResult (*InstallFunc)(void);
-typedef int (*UninstallFunc)(void);
-typedef int (*InstallScreenSaverFunc)(void);
-typedef int (*UninstallScreenSaverFunc)(void);
-typedef void (*SetSidesFunc)(UInt32);
-typedef void (*SetZoneFunc)(SInt32, SInt32, SInt32, SInt32, SInt32);
-typedef void (*SetModeFunc)(int);
-
-CSYNERGYHOOK_API int init(DWORD);
-CSYNERGYHOOK_API int cleanup(void);
-CSYNERGYHOOK_API EHookResult install(void);
-CSYNERGYHOOK_API int uninstall(void);
-CSYNERGYHOOK_API int installScreenSaver(void);
-CSYNERGYHOOK_API int uninstallScreenSaver(void);
-CSYNERGYHOOK_API void setSides(UInt32 sides);
-CSYNERGYHOOK_API void setZone(SInt32 x, SInt32 y, SInt32 w, SInt32 h,
- SInt32 jumpZoneSize);
-CSYNERGYHOOK_API void setMode(EHookMode mode);
-
}
diff --git a/src/lib/server/Config.cpp b/src/lib/server/Config.cpp
index 5e43d8a5..35e97b05 100644
--- a/src/lib/server/Config.cpp
+++ b/src/lib/server/Config.cpp
@@ -1241,6 +1241,26 @@ Config::parseAction(ConfigReadContext& s,
action = new InputFilter::LockCursorToScreenAction(m_events, mode);
}
+ else if (name == "restartServer") {
+ if (args.size() > 1) {
+ throw XConfigRead(s, "syntax for action: restartServer([{{restart}}])");
+ }
+
+ InputFilter::RestartServer::Mode mode =
+ InputFilter::RestartServer::restart;
+
+ if (args.size() == 1) {
+ if (args[0] == "restart") {
+ mode = InputFilter::RestartServer::restart;
+ }
+ else {
+ throw XConfigRead(s, "syntax for action: restartServer([{restart}])");
+ }
+ }
+
+ action = new InputFilter::RestartServer(m_events, mode);
+ }
+
else if (name == "keyboardBroadcast") {
if (args.size() > 2) {
throw XConfigRead(s, "syntax for action: keyboardBroadcast([{off|on|toggle}[,screens]])");
diff --git a/src/lib/server/InputFilter.cpp b/src/lib/server/InputFilter.cpp
index 363ded15..7118c33c 100644
--- a/src/lib/server/InputFilter.cpp
+++ b/src/lib/server/InputFilter.cpp
@@ -323,6 +323,40 @@ InputFilter::LockCursorToScreenAction::perform(const Event& event)
Event::kDeliverImmediately));
}
+
+InputFilter::RestartServer::RestartServer(
+ IEventQueue* events, Mode mode) :
+ m_mode(mode),
+ m_events(events)
+{
+ // do nothing
+}
+
+InputFilter::RestartServer::Mode
+InputFilter::RestartServer::getMode() const
+{
+ return m_mode;
+}
+
+InputFilter::Action *InputFilter::RestartServer::clone() const {
+ return new RestartServer(*this);
+}
+
+String
+InputFilter::RestartServer::format() const
+{
+ static const char* s_mode[] = { "restart" };
+
+ return synergy::string::sprintf("restartServer(%s)", s_mode[m_mode]);
+}
+
+void
+InputFilter::RestartServer::perform(const Event& event)
+{
+ //HACK Super hack we should gracefully exit
+ exit(0);
+}
+
InputFilter::SwitchToScreenAction::SwitchToScreenAction(
IEventQueue* events, const String& screen) :
m_screen(screen),
@@ -1088,3 +1122,4 @@ InputFilter::handleEvent(const Event& event, void*)
// not handled so pass through
m_events->addEvent(myEvent);
}
+
diff --git a/src/lib/server/InputFilter.h b/src/lib/server/InputFilter.h
index 99564176..0102315a 100644
--- a/src/lib/server/InputFilter.h
+++ b/src/lib/server/InputFilter.h
@@ -149,6 +149,24 @@ public:
Mode m_mode;
IEventQueue* m_events;
};
+
+ class RestartServer : public Action {
+ public:
+ enum Mode { restart };
+
+ RestartServer(IEventQueue* events, Mode = restart);
+
+ Mode getMode() const;
+
+ // Action overrides
+ virtual Action* clone() const;
+ virtual String format() const;
+ virtual void perform(const Event&);
+
+ private:
+ Mode m_mode;
+ IEventQueue* m_events;
+ };
// SwitchToScreenAction
class SwitchToScreenAction : public Action {
diff --git a/src/lib/synergy/KeyMap.cpp b/src/lib/synergy/KeyMap.cpp
index e2d3edb2..174ecad0 100644
--- a/src/lib/synergy/KeyMap.cpp
+++ b/src/lib/synergy/KeyMap.cpp
@@ -545,7 +545,7 @@ KeyMap::mapCommandKey(Keystrokes& keys, KeyID id, SInt32 group,
KeyModifierMask requiredIgnoreShiftMask = item.m_required & ~KeyModifierShift;
if ((item.m_required & desiredShiftMask) == (item.m_sensitive & desiredShiftMask) &&
((requiredIgnoreShiftMask & desiredMask) == requiredIgnoreShiftMask)) {
- LOG((CLOG_INFO "found key in group %d", effectiveGroup));
+ LOG((CLOG_DEBUG1 "found key in group %d", effectiveGroup));
keyItem = &item;
break;
}
diff --git a/src/lib/synwinhk/CMakeLists.txt b/src/lib/synwinhk/CMakeLists.txt
deleted file mode 100644
index c9f68baa..00000000
--- a/src/lib/synwinhk/CMakeLists.txt
+++ /dev/null
@@ -1,27 +0,0 @@
-# synergy -- mouse and keyboard sharing utility
-# Copyright (C) 2013-2016 Symless Ltd.
-#
-# 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 LICENSE 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.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see .
-
-file(GLOB headers "*.h")
-file(GLOB sources "*.cpp")
-
-if (SYNERGY_ADD_HEADERS)
- list(APPEND sources ${headers})
-endif()
-
-add_library(synwinhk SHARED ${sources})
-
-if (NOT MSVC_VERSION VERSION_LESS 1900)
- target_link_libraries(synwinhk libucrt)
-endif()
diff --git a/src/lib/synwinhk/synwinhk.cpp b/src/lib/synwinhk/synwinhk.cpp
deleted file mode 100644
index 30ba1289..00000000
--- a/src/lib/synwinhk/synwinhk.cpp
+++ /dev/null
@@ -1,1126 +0,0 @@
-/*
- * synergy -- mouse and keyboard sharing utility
- * Copyright (C) 2012-2016 Symless Ltd.
- * 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 LICENSE 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.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
- */
-
-#include "synwinhk/synwinhk.h"
-
-#include "synergy/protocol_types.h"
-
-#include
-#include
-
-#if _MSC_VER >= 1400
-// VS2005 hack - we don't use assert here because we don't want to link with the CRT.
-#undef assert
-#if _DEBUG
-#define assert(_X_) if (!(_X_)) __debugbreak()
-#else
-#define assert(_X_) __noop()
-#endif
-// VS2005 is a bit more smart than VC6 and optimize simple copy loop to
-// intrinsic memcpy.
-#pragma function(memcpy)
-#endif
-
-//
-// 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
-
-//
-// debugging compile flag. when not zero the server will not
-// install low level hooks.
-//
-#define NO_LOWLEVEL_HOOKS 0
-
-//
-// 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
-
-// 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
-
-
-//
-// globals
-//
-
-#if defined(_MSC_VER)
-#pragma comment(linker, "-section:shared,rws")
-#pragma data_seg("shared")
-#endif
-// all data in this shared section *must* be initialized
-
-static HINSTANCE g_hinstance = NULL;
-static DWORD g_processID = 0;
-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;
-static HHOOK g_mouseLL = NULL;
-static bool g_screenSaver = false;
-static EHookMode g_mode = kHOOK_DISABLE;
-static UInt32 g_zoneSides = 0;
-static SInt32 g_zoneSize = 0;
-static SInt32 g_xScreen = 0;
-static SInt32 g_yScreen = 0;
-static SInt32 g_wScreen = 0;
-static SInt32 g_hScreen = 0;
-static WPARAM g_deadVirtKey = 0;
-static WPARAM g_deadRelease = 0;
-static LPARAM g_deadLParam = 0;
-static BYTE g_deadKeyState[256] = { 0 };
-static BYTE g_keyState[256] = { 0 };
-static DWORD g_hookThread = 0;
-static bool g_fakeInput = false;
-
-#if defined(_MSC_VER)
-#pragma data_seg()
-#endif
-
-// 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
-extern "C" {
-int _fltused=0;
-}
-#endif
-
-#if !NO_GRAB_KEYBOARD
-static
-WPARAM
-makeKeyMsg(UINT virtKey, char c, bool noAltGr)
-{
- return MAKEWPARAM(MAKEWORD(virtKey & 0xff, (BYTE)c), noAltGr ? 1 : 0);
-}
-
-static
-void
-keyboardGetState(BYTE keys[256], DWORD vkCode, bool kf_up)
-{
- // 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.
- if (vkCode < 0 || vkCode >= 256) {
- return;
- }
-
- // Keep track of key state on our own in case GetAsyncKeyState() fails
- g_keyState[vkCode] = kf_up ? 0 : 0x80;
- g_keyState[VK_SHIFT] = g_keyState[VK_LSHIFT] | g_keyState[VK_RSHIFT];
-
- SHORT key;
- // Test whether GetAsyncKeyState() is being honest with us
- key = GetAsyncKeyState(vkCode);
-
- if (key & 0x80) {
- // The only time we know for sure that GetAsyncKeyState() is working
- // is when it tells us that the current key is down.
- // In this case, update g_keyState to reflect what GetAsyncKeyState()
- // is telling us, just in case we have gotten out of sync
-
- for (int i = 0; i < 256; ++i) {
- key = GetAsyncKeyState(i);
- g_keyState[i] = (BYTE)((key < 0) ? 0x80u : 0);
- }
- }
-
- // copy g_keyState to keys
- for (int i = 0; i < 256; ++i) {
- keys[i] = g_keyState[i];
- }
-
- key = GetKeyState(VK_CAPITAL);
- keys[VK_CAPITAL] = (BYTE)(((key < 0) ? 0x80 : 0) | (key & 1));
-}
-
-static
-bool
-doKeyboardHookHandler(WPARAM wParam, LPARAM lParam)
-{
- DWORD vkCode = static_cast(wParam);
- bool kf_up = (lParam & (KF_UP << 16)) != 0;
-
- // 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;
- }
-
- // 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 || g_deadRelease == wParam) &&
- (lParam & 0x80000000u) != 0) {
- g_deadRelease = 0;
- PostThreadMessage(g_threadID, SYNERGY_MSG_DEBUG,
- wParam | 0x04000000, lParam);
- return false;
- }
-
- // we need the keyboard state for ToAscii()
- BYTE keys[256];
- keyboardGetState(keys, vkCode, kf_up);
-
- // 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;
- }
- }
-
- WORD c = 0;
-
- // 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) {
- if (ToAscii((UINT)g_deadVirtKey, (g_deadLParam & 0x10ff0000u) >> 16,
- g_deadKeyState, &c, flags) == 2)
- {
- // If ToAscii returned 2, it means that we accidentally removed
- // a double dead key instead of restoring it. Thus, we call
- // ToAscii again with the same parameters to restore the
- // internal dead key state.
- ToAscii((UINT)g_deadVirtKey, (g_deadLParam & 0x10ff0000u) >> 16,
- g_deadKeyState, &c, flags);
-
- // We need to keep track of this because g_deadVirtKey will be
- // cleared later on; this would cause the dead key release to
- // incorrectly restore the dead key state.
- g_deadRelease = g_deadVirtKey;
- }
- }
-
- UINT scanCode = ((lParam & 0x10ff0000u) >> 16);
- int n = ToAscii((UINT)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,
- wParam | 0x05000000, lParam);
- if (g_deadVirtKey != 0) {
- if (ToAscii((UINT)g_deadVirtKey, (g_deadLParam & 0x10ff0000u) >> 16,
- g_deadKeyState, &c, flags) == 2)
- {
- ToAscii((UINT)g_deadVirtKey, (g_deadLParam & 0x10ff0000u) >> 16,
- g_deadKeyState, &c, flags);
- g_deadRelease = g_deadVirtKey;
- }
- }
- 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((UINT)wParam, scanCode, keys2, &c, flags);
- }
-
- PostThreadMessage(g_threadID, SYNERGY_MSG_DEBUG,
- wParam | ((c & 0xff) << 8) |
- ((n & 0xff) << 16) | 0x06000000,
- lParam);
- WPARAM charAndVirtKey = 0;
- bool clearDeadKey = false;
- switch (n) {
- default:
- // key is a dead key
-
- if (lParam & 0x80000000u)
- // This handles the obscure situation where a key has been
- // pressed which is both a dead key and a normal character
- // depending on which modifiers have been pressed. We
- // break here to prevent it from being considered a dead
- // key.
- break;
-
- 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((UINT)wParam, (char)0, noAltGr);
- break;
-
- case 1:
- // key maps to a character composed with dead key
- charAndVirtKey = makeKeyMsg((UINT)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((UINT)g_deadVirtKey, (char)LOBYTE(c), noAltGr);
- PostThreadMessage(g_threadID, SYNERGY_MSG_KEY,
- deadCharAndVirtKey, g_deadLParam & 0x7fffffffu);
- PostThreadMessage(g_threadID, SYNERGY_MSG_KEY,
- deadCharAndVirtKey, g_deadLParam | 0x80000000u);
-
- // use uncomposed character
- charAndVirtKey = makeKeyMsg((UINT)wParam, (char)HIBYTE(c), noAltGr);
- clearDeadKey = true;
- break;
- }
- }
-
- // put back the dead key, if any, for the application to use
- if (g_deadVirtKey != 0) {
- ToAscii((UINT)g_deadVirtKey, (g_deadLParam & 0x10ff0000u) >> 16,
- g_deadKeyState, &c, flags);
- }
-
- // clear out old dead key state
- if (clearDeadKey) {
- g_deadVirtKey = 0;
- g_deadLParam = 0;
- }
-
- // 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.
- // XXX -- with hot keys for actions we may only need to do this when
- // forwarding.
- if (charAndVirtKey != 0) {
- PostThreadMessage(g_threadID, SYNERGY_MSG_DEBUG,
- charAndVirtKey | 0x07000000, lParam);
- PostThreadMessage(g_threadID, SYNERGY_MSG_KEY, charAndVirtKey, lParam);
- }
-
- if (g_mode == kHOOK_RELAY_EVENTS) {
- // 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;
-
- case VK_HANGUL:
- // pass these modifiers if using a low level hook, discard
- // them if not.
- if (g_hookThread == 0) {
- return true;
- }
- break;
-
- default:
- // discard
- return true;
- }
- }
-
- return false;
-}
-
-static
-bool
-keyboardHookHandler(WPARAM wParam, LPARAM lParam)
-{
- return doKeyboardHookHandler(wParam, lParam);
-}
-#endif
-
-static
-bool
-doMouseHookHandler(WPARAM wParam, SInt32 x, SInt32 y, SInt32 data)
-{
- switch (wParam) {
- case WM_LBUTTONDOWN:
- case WM_MBUTTONDOWN:
- case WM_RBUTTONDOWN:
- case WM_XBUTTONDOWN:
- case WM_LBUTTONDBLCLK:
- case WM_MBUTTONDBLCLK:
- case WM_RBUTTONDBLCLK:
- case WM_XBUTTONDBLCLK:
- case WM_LBUTTONUP:
- case WM_MBUTTONUP:
- case WM_RBUTTONUP:
- case WM_XBUTTONUP:
- case WM_NCLBUTTONDOWN:
- case WM_NCMBUTTONDOWN:
- case WM_NCRBUTTONDOWN:
- case WM_NCXBUTTONDOWN:
- case WM_NCLBUTTONDBLCLK:
- case WM_NCMBUTTONDBLCLK:
- case WM_NCRBUTTONDBLCLK:
- case WM_NCXBUTTONDBLCLK:
- case WM_NCLBUTTONUP:
- case WM_NCMBUTTONUP:
- case WM_NCRBUTTONUP:
- case WM_NCXBUTTONUP:
- // always relay the event. eat it if relaying.
- PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_BUTTON, wParam, data);
- return (g_mode == kHOOK_RELAY_EVENTS);
-
- case WM_MOUSEWHEEL:
- if (g_mode == kHOOK_RELAY_EVENTS) {
- // relay event
- PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_WHEEL, data, 0);
- }
- return (g_mode == kHOOK_RELAY_EVENTS);
-
- case WM_NCMOUSEMOVE:
- case WM_MOUSEMOVE:
- if (g_mode == kHOOK_RELAY_EVENTS) {
- // relay and eat event
- PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_MOVE, x, y);
- return true;
- }
- else if (g_mode == kHOOK_WATCH_JUMP_ZONE) {
- // 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.
- // Server 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;
- }
-
- // check for mouse inside jump zone
- bool inside = false;
- if (!inside && (g_zoneSides & kLeftMask) != 0) {
- inside = (x < g_xScreen + g_zoneSize);
- }
- if (!inside && (g_zoneSides & kRightMask) != 0) {
- inside = (x >= g_xScreen + g_wScreen - g_zoneSize);
- }
- if (!inside && (g_zoneSides & kTopMask) != 0) {
- inside = (y < g_yScreen + g_zoneSize);
- }
- if (!inside && (g_zoneSides & kBottomMask) != 0) {
- inside = (y >= g_yScreen + g_hScreen - g_zoneSize);
- }
-
- // relay the event
- PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_MOVE, x, y);
-
- // if inside and not bogus then eat the event
- return inside && !bogus;
- }
- }
-
- // pass the event
- 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(LOWORD(info->dwExtraInfo));
- break;
-
- case kWheelWin2000: {
- const MOUSEHOOKSTRUCTWin2000* info2k =
- (const MOUSEHOOKSTRUCTWin2000*)lParam;
- w = static_cast(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
-LRESULT CALLBACK
-getMessageHook(int code, WPARAM wParam, LPARAM lParam)
-{
- if (code >= 0) {
- if (g_screenSaver) {
- MSG* msg = reinterpret_cast(lParam);
- if (msg->message == WM_SYSCOMMAND &&
- msg->wParam == SC_SCREENSAVE) {
- // broadcast screen saver started message
- PostThreadMessage(g_threadID,
- SYNERGY_MSG_SCREEN_SAVER, TRUE, 0);
- }
- }
- if (g_mode == kHOOK_RELAY_EVENTS) {
- MSG* msg = reinterpret_cast(lParam);
- if (g_wheelSupport == kWheelOld && msg->message == g_wmMouseWheel) {
- // post message to our window
- PostThreadMessage(g_threadID,
- SYNERGY_MSG_MOUSE_WHEEL,
- static_cast(msg->wParam & 0xffffu), 0);
-
- // zero out the delta in the message so it's (hopefully)
- // ignored
- msg->wParam = 0;
- }
- }
- }
-
- 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
-// alt+tab, alt+esc, ctrl+esc, and windows key hot keys. on the down
-// side, key repeats are not reported to us.
-//
-
-#if !NO_GRAB_KEYBOARD
-static
-LRESULT CALLBACK
-keyboardLLHook(int code, WPARAM wParam, LPARAM lParam)
-{
- if (code >= 0) {
- // decode the message
- KBDLLHOOKSTRUCT* info = reinterpret_cast(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.
-
- // handle the message
- if (keyboardHookHandler(wParam, lParam)) {
- return 1;
- }
- }
-
- return CallNextHookEx(g_keyboardLL, code, wParam, lParam);
-}
-#endif
-
-//
-// low-level mouse hook -- this allows us to capture and handle mouse
-// events very early. the earlier the better.
-//
-
-static
-LRESULT CALLBACK
-mouseLLHook(int code, WPARAM wParam, LPARAM lParam)
-{
- if (code >= 0) {
- // decode the message
- MSLLHOOKSTRUCT* info = reinterpret_cast(lParam);
- SInt32 x = static_cast(info->pt.x);
- SInt32 y = static_cast(info->pt.y);
- SInt32 w = static_cast(HIWORD(info->mouseData));
-
- // handle the message
- if (mouseHookHandler(wParam, x, y, w)) {
- return 1;
- }
- }
-
- return CallNextHookEx(g_mouseLL, code, wParam, lParam);
-}
-
-#endif
-
-static
-EWheelSupport
-getWheelSupport()
-{
- // see if modern wheel is present
- if (GetSystemMetrics(SM_MOUSEWHEELPRESENT)) {
- OSVERSIONINFOEX osvi;
- osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
- osvi.dwPlatformId = VER_PLATFORM_WIN32_NT;
- osvi.dwMajorVersion = 5;
- osvi.dwMinorVersion = 0;
- ULONGLONG condMask = 0;
- VER_SET_CONDITION (condMask, VER_MAJORVERSION, VER_EQUAL);
- VER_SET_CONDITION (condMask, VER_MINORVERSION, VER_EQUAL);
- VER_SET_CONDITION (condMask, VER_PLATFORMID, VER_EQUAL);
- if (VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION |
- VER_PLATFORMID, condMask)) {
- return kWheelWin2000;
- }
- return kWheelModern;
- }
-
- // not modern. see if we've got old-style support.
-#if defined(MSH_WHEELSUPPORT)
- 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;
- }
- }
- }
-#endif
-
- // 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
- // though i'm not sure exactly when it does that (WinME returns
- // FALSE for my logitech USB trackball).
- return kWheelModern;
-}
-
-
-//
-// external functions
-//
-
-BOOL WINAPI
-DllMain(HINSTANCE instance, DWORD reason, LPVOID)
-{
- if (reason == DLL_PROCESS_ATTACH) {
- DisableThreadLibraryCalls(instance);
- if (g_processID == 0) {
- g_hinstance = instance;
- g_processID = GetCurrentProcessId();
- }
- }
- else if (reason == DLL_PROCESS_DETACH) {
- if (g_processID == GetCurrentProcessId()) {
- uninstall();
- uninstallScreenSaver();
- g_processID = 0;
- g_hinstance = NULL;
- }
- }
- return TRUE;
-}
-
-extern "C" {
-
-// VS2005 hack to not link with the CRT
-#if _MSC_VER >= 1400
-BOOL WINAPI _DllMainCRTStartup(
- HINSTANCE instance, DWORD reason, LPVOID lpreserved)
-{
- return DllMain(instance, reason, lpreserved);
-}
-
-// VS2005 is a bit more bright than VC6 and optimize simple copy loop to
-// intrinsic memcpy.
-void * __cdecl memcpy(void * _Dst, const void * _Src, size_t _MaxCount)
-{
- void * _DstBackup = _Dst;
- switch (_MaxCount & 3) {
- case 3:
- ((char*)_Dst)[0] = ((char*)_Src)[0];
- ++(char*&)_Dst;
- ++(char*&)_Src;
- --_MaxCount;
- case 2:
- ((char*)_Dst)[0] = ((char*)_Src)[0];
- ++(char*&)_Dst;
- ++(char*&)_Src;
- --_MaxCount;
- case 1:
- ((char*)_Dst)[0] = ((char*)_Src)[0];
- ++(char*&)_Dst;
- ++(char*&)_Src;
- --_MaxCount;
- break;
- case 0:
- break;
-
- default:
- __assume(0);
- break;
- }
-
- // I think it's faster on intel to deference than modify the pointer.
- const size_t max = _MaxCount / sizeof(UINT_PTR);
- for (size_t i = 0; i < max; ++i) {
- ((UINT_PTR*)_Dst)[i] = ((UINT_PTR*)_Src)[i];
- }
-
- (UINT_PTR*&)_Dst += max;
- (UINT_PTR*&)_Src += max;
-
- switch (_MaxCount & 3) {
- case 3:
- ((char*)_Dst)[0] = ((char*)_Src)[0];
- ++(char*&)_Dst;
- ++(char*&)_Src;
- case 2:
- ((char*)_Dst)[0] = ((char*)_Src)[0];
- ++(char*&)_Dst;
- ++(char*&)_Src;
- case 1:
- ((char*)_Dst)[0] = ((char*)_Src)[0];
- ++(char*&)_Dst;
- ++(char*&)_Src;
- break;
- case 0:
- break;
-
- default:
- __assume(0);
- break;
- }
-
- return _DstBackup;
-}
-#endif
-
-int
-init(DWORD threadID)
-{
- assert(g_hinstance != NULL);
-
- // 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).
- int result = CloseHandle(process);
- if (result == false) {
- 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(_T("synwinhk"));
- g_processID = GetCurrentProcessId();
- g_wheelSupport = kWheelNone;
- g_threadID = 0;
- g_keyboard = NULL;
- g_mouse = NULL;
- g_getMessage = NULL;
- g_keyboardLL = NULL;
- g_mouseLL = NULL;
- g_screenSaver = false;
- }
-
- // save thread id. we'll post messages to this thread's
- // message queue.
- g_threadID = threadID;
-
- // set defaults
- g_mode = kHOOK_DISABLE;
- g_zoneSides = 0;
- g_zoneSize = 0;
- g_xScreen = 0;
- g_yScreen = 0;
- g_wScreen = 0;
- g_hScreen = 0;
-
- return 1;
-}
-
-int
-cleanup(void)
-{
- assert(g_hinstance != NULL);
-
- if (g_processID == GetCurrentProcessId()) {
- g_threadID = 0;
- }
-
- return 1;
-}
-
-EHookResult
-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) {
- return kHOOK_FAILED;
- }
-
- // discard old dead keys
- g_deadVirtKey = 0;
- g_deadLParam = 0;
-
- // reset fake input flag
- g_fakeInput = false;
-
- // check for mouse wheel support
- g_wheelSupport = getWheelSupport();
-
- // install GetMessage hook (unless already installed)
- if (g_wheelSupport == kWheelOld && g_getMessage == NULL) {
- g_getMessage = SetWindowsHookEx(WH_GETMESSAGE,
- &getMessageHook,
- g_hinstance,
- 0);
- }
-
- // 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,
- &mouseLLHook,
- g_hinstance,
- 0);
-#if !NO_GRAB_KEYBOARD
- g_keyboardLL = SetWindowsHookEx(WH_KEYBOARD_LL,
- &keyboardLLHook,
- g_hinstance,
- 0);
- 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;
- }
- }
-#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
- if ((g_getMessage == NULL && g_wheelSupport == kWheelOld) ||
-#if !NO_GRAB_KEYBOARD
- (g_keyboardLL == NULL && g_keyboard == NULL) ||
-#endif
- (g_mouseLL == NULL && g_mouse == NULL)) {
- uninstall();
- return kHOOK_FAILED;
- }
-
- if (g_keyboardLL != NULL || g_mouseLL != NULL) {
- g_hookThread = GetCurrentThreadId();
- return kHOOK_OKAY_LL;
- }
-
- return kHOOK_OKAY;
-}
-
-int
-uninstall(void)
-{
- assert(g_hinstance != NULL);
-
- // discard old dead keys
- g_deadVirtKey = 0;
- g_deadLParam = 0;
-
- // uninstall hooks
- if (g_keyboardLL != NULL) {
- UnhookWindowsHookEx(g_keyboardLL);
- g_keyboardLL = NULL;
- }
- if (g_mouseLL != NULL) {
- UnhookWindowsHookEx(g_mouseLL);
- 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) {
- UnhookWindowsHookEx(g_getMessage);
- g_getMessage = NULL;
- }
- g_wheelSupport = kWheelNone;
-
- return 1;
-}
-
-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
- if (g_getMessage != NULL && g_wheelSupport != kWheelOld) {
- UnhookWindowsHookEx(g_getMessage);
- g_getMessage = NULL;
- }
-
- // screen saver hook is no longer installed
- g_screenSaver = false;
-
- return 1;
-}
-
-void
-setSides(UInt32 sides)
-{
- g_zoneSides = sides;
-}
-
-void
-setZone(SInt32 x, SInt32 y, SInt32 w, SInt32 h, SInt32 jumpZoneSize)
-{
- g_zoneSize = jumpZoneSize;
- g_xScreen = x;
- g_yScreen = y;
- g_wScreen = w;
- g_hScreen = h;
-}
-
-void
-setMode(EHookMode mode)
-{
- if (mode == g_mode) {
- // no change
- return;
- }
- g_mode = mode;
-}
-
-}
diff --git a/src/test/integtests/platform/MSWindowsKeyStateTests.cpp b/src/test/integtests/platform/MSWindowsKeyStateTests.cpp
index 0affab6e..21cf7e6f 100644
--- a/src/test/integtests/platform/MSWindowsKeyStateTests.cpp
+++ b/src/test/integtests/platform/MSWindowsKeyStateTests.cpp
@@ -52,7 +52,7 @@ protected:
MSWindowsDesks* newDesks(IEventQueue* eventQueue)
{
return new MSWindowsDesks(
- true, false, m_hook.getInstance(), m_screensaver, eventQueue,
+ true, false, m_screensaver, eventQueue,
new TMethodJob(
this, &MSWindowsKeyStateTests::updateKeysCB), false);
}