From 38a6bd9d589445dbfbcd798b6f965c476eb37d2b Mon Sep 17 00:00:00 2001 From: Rajveer Aujla Date: Thu, 19 Jan 2023 13:37:15 +0000 Subject: [PATCH] [FEATURE] Implement `disableGlobalHotkeyRegister` option for keystroke conditions to allow apps on the primary client to respond to the keystroke without the OS blocking them --- src/lib/barrier/IPlatformScreen.h | 4 ++-- src/lib/barrier/IPrimaryScreen.h | 4 ++-- src/lib/barrier/PlatformScreen.h | 4 ++-- src/lib/barrier/Screen.cpp | 8 ++++---- src/lib/barrier/Screen.h | 4 ++-- src/lib/platform/MSWindowsScreen.cpp | 15 ++++++--------- src/lib/platform/MSWindowsScreen.h | 4 ++-- src/lib/server/Config.cpp | 25 ++++++++++++++++++++++--- src/lib/server/Config.h | 1 + src/lib/server/InputFilter.cpp | 16 +++++++++------- src/lib/server/InputFilter.h | 5 +++-- src/lib/server/PrimaryClient.cpp | 8 ++++---- src/lib/server/PrimaryClient.h | 4 ++-- src/lib/server/Server.cpp | 2 +- 14 files changed, 62 insertions(+), 42 deletions(-) diff --git a/src/lib/barrier/IPlatformScreen.h b/src/lib/barrier/IPlatformScreen.h index 995ff733..65e1522a 100644 --- a/src/lib/barrier/IPlatformScreen.h +++ b/src/lib/barrier/IPlatformScreen.h @@ -155,8 +155,8 @@ public: // IPrimaryScreen overrides virtual void reconfigure(UInt32 activeSides) = 0; virtual void warpCursor(SInt32 x, SInt32 y) = 0; - virtual UInt32 registerHotKey(KeyID key, KeyModifierMask mask) = 0; - virtual void unregisterHotKey(UInt32 id) = 0; + virtual UInt32 registerHotKey(KeyID key, KeyModifierMask mask, bool registerGlobalHotkey) = 0; + virtual void unregisterHotKey(UInt32 id, bool unregisterGlobalHotkey) = 0; virtual void fakeInputBegin() = 0; virtual void fakeInputEnd() = 0; virtual SInt32 getJumpZoneSize() const = 0; diff --git a/src/lib/barrier/IPrimaryScreen.h b/src/lib/barrier/IPrimaryScreen.h index 0cf3688a..b0dda2eb 100644 --- a/src/lib/barrier/IPrimaryScreen.h +++ b/src/lib/barrier/IPrimaryScreen.h @@ -111,13 +111,13 @@ public: the modifiers in any order or to require the user to press the given key last. */ - virtual UInt32 registerHotKey(KeyID key, KeyModifierMask mask) = 0; + virtual UInt32 registerHotKey(KeyID key, KeyModifierMask mask, bool registerGlobalHotkey) = 0; //! Unregister a system hotkey /*! Unregisters a previously registered hot key. */ - virtual void unregisterHotKey(UInt32 id) = 0; + virtual void unregisterHotKey(UInt32 id, bool unregisterGlobalHotkey) = 0; //! Prepare to synthesize input on primary screen /*! diff --git a/src/lib/barrier/PlatformScreen.h b/src/lib/barrier/PlatformScreen.h index 19a3da10..a6fec7fb 100644 --- a/src/lib/barrier/PlatformScreen.h +++ b/src/lib/barrier/PlatformScreen.h @@ -44,8 +44,8 @@ public: virtual void reconfigure(UInt32 activeSides) = 0; virtual void warpCursor(SInt32 x, SInt32 y) = 0; virtual UInt32 registerHotKey(KeyID key, - KeyModifierMask mask) = 0; - virtual void unregisterHotKey(UInt32 id) = 0; + KeyModifierMask mask, bool registerGlobalHotkey) = 0; + virtual void unregisterHotKey(UInt32 id, bool unregisterGlobalHotkey) = 0; virtual void fakeInputBegin() = 0; virtual void fakeInputEnd() = 0; virtual SInt32 getJumpZoneSize() const = 0; diff --git a/src/lib/barrier/Screen.cpp b/src/lib/barrier/Screen.cpp index 2a2c8776..2011e9c5 100644 --- a/src/lib/barrier/Screen.cpp +++ b/src/lib/barrier/Screen.cpp @@ -330,15 +330,15 @@ Screen::setSequenceNumber(UInt32 seqNum) } UInt32 -Screen::registerHotKey(KeyID key, KeyModifierMask mask) +Screen::registerHotKey(KeyID key, KeyModifierMask mask, bool registerGlobalHotkey) { - return m_screen->registerHotKey(key, mask); + return m_screen->registerHotKey(key, mask, registerGlobalHotkey); } void -Screen::unregisterHotKey(UInt32 id) +Screen::unregisterHotKey(UInt32 id, bool unregisterGlobalHotkey) { - m_screen->unregisterHotKey(id); + m_screen->unregisterHotKey(id, unregisterGlobalHotkey); } void diff --git a/src/lib/barrier/Screen.h b/src/lib/barrier/Screen.h index 1c8e7dec..9b94a9e6 100644 --- a/src/lib/barrier/Screen.h +++ b/src/lib/barrier/Screen.h @@ -198,13 +198,13 @@ public: Registers a system-wide hotkey for key \p key with modifiers \p mask. Returns an id used to unregister the hotkey. */ - UInt32 registerHotKey(KeyID key, KeyModifierMask mask); + UInt32 registerHotKey(KeyID key, KeyModifierMask mask, bool registerGlobalHotkey); //! Unregister a system hotkey /*! Unregisters a previously registered hot key. */ - void unregisterHotKey(UInt32 id); + void unregisterHotKey(UInt32 id, bool unregisterGlobalHotkey); //! Prepare to synthesize input on primary screen /*! diff --git a/src/lib/platform/MSWindowsScreen.cpp b/src/lib/platform/MSWindowsScreen.cpp index c995a40d..d51f2b5f 100644 --- a/src/lib/platform/MSWindowsScreen.cpp +++ b/src/lib/platform/MSWindowsScreen.cpp @@ -548,7 +548,7 @@ void MSWindowsScreen::saveMousePosition(SInt32 x, SInt32 y) { } UInt32 -MSWindowsScreen::registerHotKey(KeyID key, KeyModifierMask mask) +MSWindowsScreen::registerHotKey(KeyID key, KeyModifierMask mask, bool registerGlobalHotkey) { // only allow certain modifiers if ((mask & ~(KeyModifierShift | KeyModifierControl | @@ -599,12 +599,12 @@ MSWindowsScreen::registerHotKey(KeyID key, KeyModifierMask mask) } // if this hot key has modifiers only then we'll handle it specially - bool err; + bool err = false; if (key == kKeyNone) { // check if already registered err = (m_hotKeyToIDMap.count(HotKeyItem(vk, modifiers)) > 0); } - else { + else if (registerGlobalHotkey) { // register with OS err = (RegisterHotKey(NULL, id, modifiers, vk) == 0); } @@ -625,7 +625,7 @@ MSWindowsScreen::registerHotKey(KeyID key, KeyModifierMask mask) } void -MSWindowsScreen::unregisterHotKey(UInt32 id) +MSWindowsScreen::unregisterHotKey(UInt32 id, bool unregisterGlobalHotkey) { // look up hotkey HotKeyMap::iterator i = m_hotKeys.find(id); @@ -634,13 +634,10 @@ MSWindowsScreen::unregisterHotKey(UInt32 id) } // unregister with OS - bool err; - if (i->second.getVirtualKey() != 0) { + bool err = false; + if (unregisterGlobalHotkey && i->second.getVirtualKey() != 0) { err = !UnregisterHotKey(NULL, id); } - else { - err = false; - } if (err) { LOG((CLOG_WARN "failed to unregister hotkey id=%d", id)); } diff --git a/src/lib/platform/MSWindowsScreen.h b/src/lib/platform/MSWindowsScreen.h index eaa7b88a..c7d3944e 100644 --- a/src/lib/platform/MSWindowsScreen.h +++ b/src/lib/platform/MSWindowsScreen.h @@ -80,8 +80,8 @@ public: virtual void reconfigure(UInt32 activeSides); virtual void warpCursor(SInt32 x, SInt32 y); virtual UInt32 registerHotKey(KeyID key, - KeyModifierMask mask); - virtual void unregisterHotKey(UInt32 id); + KeyModifierMask mask, bool registerGlobalHotkey); + virtual void unregisterHotKey(UInt32 id, bool unregisterGlobalHotkey); virtual void fakeInputBegin(); virtual void fakeInputEnd(); virtual SInt32 getJumpZoneSize() const; diff --git a/src/lib/server/Config.cpp b/src/lib/server/Config.cpp index 86568b80..22a5f187 100644 --- a/src/lib/server/Config.cpp +++ b/src/lib/server/Config.cpp @@ -1029,13 +1029,19 @@ InputFilter::Condition* Config::parseCondition(ConfigReadContext& s, const std:: const std::vector& args) { if (name == "keystroke") { - if (args.size() != 1) { - throw XConfigRead(s, "syntax for condition: keystroke(modifiers+key)"); + if (args.size() < 1 || args.size() > 2) { + throw XConfigRead(s, "syntax for condition: keystroke(modifiers+key[,options])"); } IPlatformScreen::KeyInfo* keyInfo = s.parseKeystroke(args[0]); + bool disableGlobalHotkeyRegister = false; - return new InputFilter::KeystrokeCondition(m_events, keyInfo); + if (args.size() > 1) + { + parseKeystrokeConditionOptions(s, args[1], disableGlobalHotkeyRegister); + } + + return new InputFilter::KeystrokeCondition(m_events, keyInfo, disableGlobalHotkeyRegister); } if (name == "mousebutton") { @@ -1293,6 +1299,19 @@ void Config::parseScreens(ConfigReadContext& c, const std::string& s, } } +void Config::parseKeystrokeConditionOptions(ConfigReadContext& c, const std::string& s, + bool& disableGlobalHotkeyRegister) const +{ + if (s == "disableGlobalHotkeyRegister") + { + disableGlobalHotkeyRegister = true; + } + else + { + disableGlobalHotkeyRegister = false; + } +} + void Config::parseKeystrokeActionOptions(ConfigReadContext& c, const std::string& s, bool& activeScreenOnly) const { diff --git a/src/lib/server/Config.h b/src/lib/server/Config.h index 49fd548e..4fb0453a 100644 --- a/src/lib/server/Config.h +++ b/src/lib/server/Config.h @@ -453,6 +453,7 @@ private: void parseScreens(ConfigReadContext&, const std::string&, std::set& screens) const; + void parseKeystrokeConditionOptions(ConfigReadContext& c, const std::string& s, bool& activeScreenOnly) const; void parseKeystrokeActionOptions(ConfigReadContext& c, const std::string& s, bool& activeScreenOnly) const; static const char* getOptionName(OptionID); diff --git a/src/lib/server/InputFilter.cpp b/src/lib/server/InputFilter.cpp index a0dce17c..72123c4e 100644 --- a/src/lib/server/InputFilter.cpp +++ b/src/lib/server/InputFilter.cpp @@ -53,21 +53,23 @@ InputFilter::Condition::disablePrimary(PrimaryClient*) } InputFilter::KeystrokeCondition::KeystrokeCondition( - IEventQueue* events, IPlatformScreen::KeyInfo* info) : + IEventQueue* events, IPlatformScreen::KeyInfo* info, bool disableGlobalHotkeyRegister) : m_id(0), m_key(info->m_key), m_mask(info->m_mask), - m_events(events) + m_events(events), + m_disableGlobalHotkeyRegister(disableGlobalHotkeyRegister) { free(info); } InputFilter::KeystrokeCondition::KeystrokeCondition( - IEventQueue* events, KeyID key, KeyModifierMask mask) : + IEventQueue* events, KeyID key, KeyModifierMask mask, bool disableGlobalHotkeyRegister) : m_id(0), m_key(key), m_mask(mask), - m_events(events) + m_events(events), + m_disableGlobalHotkeyRegister(disableGlobalHotkeyRegister) { // do nothing } @@ -92,7 +94,7 @@ InputFilter::KeystrokeCondition::getMask() const InputFilter::Condition* InputFilter::KeystrokeCondition::clone() const { - return new KeystrokeCondition(m_events, m_key, m_mask); + return new KeystrokeCondition(m_events, m_key, m_mask, m_disableGlobalHotkeyRegister); } std::string InputFilter::KeystrokeCondition::format() const @@ -131,13 +133,13 @@ InputFilter::KeystrokeCondition::match(const Event& event) void InputFilter::KeystrokeCondition::enablePrimary(PrimaryClient* primary) { - m_id = primary->registerHotKey(m_key, m_mask); + m_id = primary->registerHotKey(m_key, m_mask, !m_disableGlobalHotkeyRegister); } void InputFilter::KeystrokeCondition::disablePrimary(PrimaryClient* primary) { - primary->unregisterHotKey(m_id); + primary->unregisterHotKey(m_id, !m_disableGlobalHotkeyRegister); m_id = 0; } diff --git a/src/lib/server/InputFilter.h b/src/lib/server/InputFilter.h index 5e6ef9cf..38c10abf 100644 --- a/src/lib/server/InputFilter.h +++ b/src/lib/server/InputFilter.h @@ -57,8 +57,8 @@ public: // KeystrokeCondition class KeystrokeCondition : public Condition { public: - KeystrokeCondition(IEventQueue* events, IPlatformScreen::KeyInfo*); - KeystrokeCondition(IEventQueue* events, KeyID key, KeyModifierMask mask); + KeystrokeCondition(IEventQueue* events, IPlatformScreen::KeyInfo*, bool disableGlobalHotkeyRegister); + KeystrokeCondition(IEventQueue* events, KeyID key, KeyModifierMask mask, bool disableGlobalHotkeyRegister); virtual ~KeystrokeCondition(); KeyID getKey() const; @@ -76,6 +76,7 @@ public: KeyID m_key; KeyModifierMask m_mask; IEventQueue* m_events; + bool m_disableGlobalHotkeyRegister; }; // MouseButtonCondition diff --git a/src/lib/server/PrimaryClient.cpp b/src/lib/server/PrimaryClient.cpp index 7e052749..46e78612 100644 --- a/src/lib/server/PrimaryClient.cpp +++ b/src/lib/server/PrimaryClient.cpp @@ -49,15 +49,15 @@ PrimaryClient::reconfigure(UInt32 activeSides) } UInt32 -PrimaryClient::registerHotKey(KeyID key, KeyModifierMask mask) +PrimaryClient::registerHotKey(KeyID key, KeyModifierMask mask, bool registerGlobalHotkey) { - return m_screen->registerHotKey(key, mask); + return m_screen->registerHotKey(key, mask, registerGlobalHotkey); } void -PrimaryClient::unregisterHotKey(UInt32 id) +PrimaryClient::unregisterHotKey(UInt32 id, bool unregisterGlobalHotkey) { - m_screen->unregisterHotKey(id); + m_screen->unregisterHotKey(id, unregisterGlobalHotkey); } void diff --git a/src/lib/server/PrimaryClient.h b/src/lib/server/PrimaryClient.h index 13be8389..a38ffb18 100644 --- a/src/lib/server/PrimaryClient.h +++ b/src/lib/server/PrimaryClient.h @@ -55,13 +55,13 @@ public: Registers a system-wide hotkey for key \p key with modifiers \p mask. Returns an id used to unregister the hotkey. */ - virtual UInt32 registerHotKey(KeyID key, KeyModifierMask mask); + virtual UInt32 registerHotKey(KeyID key, KeyModifierMask mask, bool registerGlobalHotkey); //! Unregister a system hotkey /*! Unregisters a previously registered hot key. */ - virtual void unregisterHotKey(UInt32 id); + virtual void unregisterHotKey(UInt32 id, bool unregisterGlobalHotkey); //! Prepare to synthesize input on primary screen /*! diff --git a/src/lib/server/Server.cpp b/src/lib/server/Server.cpp index 5dde031f..835e2536 100644 --- a/src/lib/server/Server.cpp +++ b/src/lib/server/Server.cpp @@ -293,7 +293,7 @@ Server::setConfig(const Config& config) if (!m_config->hasLockToScreenAction()) { IPlatformScreen::KeyInfo* key = IPlatformScreen::KeyInfo::alloc(kKeyScrollLock, 0, 0, 0); - InputFilter::Rule rule(new InputFilter::KeystrokeCondition(m_events, key)); + InputFilter::Rule rule(new InputFilter::KeystrokeCondition(m_events, key, true)); rule.adoptAction(new InputFilter::LockCursorToScreenAction(m_events), true); m_inputFilter->addFilterRule(rule); }