Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/newsfragments/keystrokes-on-active-screen-only.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Added the ability to perform keystroke actions on a specific screen only, by adding the flag `activeScreenOnly` to the action (after the screens parameter) e.g.

`keystroke(Control) = keystroke(Super,Mac-Mini,activeScreenOnly)`

This works for both server and clients, with the exception being for keystroke actions intended for the server where the keystroke matches the condition (for instance, adding a custom action on a client, but preserving the original keystroke on the server) e.g.

`keystroke(Control) = keystroke(Super,Mac-Mini,activeScreenOnly), keystroke(Alt,Server,activeScreenOnly)` // This works
`keystroke(Control) = keystroke(Super,Mac-Mini,activeScreenOnly), keystroke(Control,Server,activeScreenOnly)` // This does not work

Since Barrier registers a hotkey for the keystroke condition on the server, this prevents the ability to specify the original keystroke as an action for the server. For this, a new option for the condition allows disabling hotkey registration. This allows the original keystroke to be performed on the primary when it is in focus i.e.

`keystroke(Control,disableGlobalHotkeyRegister) = keystroke(Super,Rajveer-Mac-Mini,activeScreenOnly)` // This allows the original keystroke to perform on the server
5 changes: 4 additions & 1 deletion src/lib/barrier/IKeyState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ IKeyState::KeyInfo::alloc(KeyID id,
info->m_mask = mask;
info->m_button = button;
info->m_count = count;
info->m_activeScreenOnly = false;
info->m_screens = NULL;
info->m_screensBuffer[0] = '\0';
return info;
Expand All @@ -51,7 +52,7 @@ IKeyState::KeyInfo::alloc(KeyID id,
IKeyState::KeyInfo*
IKeyState::KeyInfo::alloc(KeyID id,
KeyModifierMask mask, KeyButton button, SInt32 count,
const std::set<String>& destinations)
const std::set<String>& destinations, bool activeScreenOnly)
{
String screens = join(destinations);

Expand All @@ -61,6 +62,7 @@ IKeyState::KeyInfo::alloc(KeyID id,
info->m_mask = mask;
info->m_button = button;
info->m_count = count;
info->m_activeScreenOnly = activeScreenOnly;
info->m_screens = info->m_screensBuffer;
strcpy(info->m_screensBuffer, screens.c_str());
return info;
Expand All @@ -75,6 +77,7 @@ IKeyState::KeyInfo::alloc(const KeyInfo& x)
info->m_mask = x.m_mask;
info->m_button = x.m_button;
info->m_count = x.m_count;
info->m_activeScreenOnly = x.m_activeScreenOnly;
info->m_screens = x.m_screens ? info->m_screensBuffer : NULL;
strcpy(info->m_screensBuffer, x.m_screensBuffer);
return info;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/barrier/IKeyState.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class IKeyState : public IInterface {
public:
static KeyInfo* alloc(KeyID, KeyModifierMask, KeyButton, SInt32 count);
static KeyInfo* alloc(KeyID, KeyModifierMask, KeyButton, SInt32 count,
const std::set<String>& destinations);
const std::set<String>& destinations, bool activeScreenOnly);
static KeyInfo* alloc(const KeyInfo&);

static bool isDefault(const char* screens);
Expand All @@ -58,6 +58,7 @@ class IKeyState : public IInterface {
KeyModifierMask m_mask;
KeyButton m_button;
SInt32 m_count;
bool m_activeScreenOnly;
char* m_screens;
char m_screensBuffer[1];
};
Expand Down
4 changes: 2 additions & 2 deletions src/lib/barrier/IPlatformScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ class IPlatformScreen : public IScreen,
// 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;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/barrier/IPrimaryScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ class IPrimaryScreen : public IInterface {
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
/*!
Expand Down
4 changes: 2 additions & 2 deletions src/lib/barrier/PlatformScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class PlatformScreen : public IPlatformScreen {
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;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/barrier/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/lib/barrier/Screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@ class Screen : public IScreen {
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
/*!
Expand Down
15 changes: 6 additions & 9 deletions src/lib/platform/MSWindowsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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));
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/platform/MSWindowsScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class MSWindowsScreen : public PlatformScreen {
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;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/platform/OSXScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class OSXScreen : public PlatformScreen {
// IPrimaryScreen overrides
virtual void reconfigure(UInt32 activeSides);
virtual void warpCursor(SInt32 x, SInt32 y);
virtual UInt32 registerHotKey(KeyID key, KeyModifierMask mask);
virtual void unregisterHotKey(UInt32 id);
virtual UInt32 registerHotKey(KeyID key, KeyModifierMask mask, bool registerGlobalHotkey);
virtual void unregisterHotKey(UInt32 id, bool unregisterGlobalHotkey);
virtual void fakeInputBegin();
virtual void fakeInputEnd();
virtual SInt32 getJumpZoneSize() const;
Expand Down
24 changes: 14 additions & 10 deletions src/lib/platform/OSXScreen.mm
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@
}

UInt32
OSXScreen::registerHotKey(KeyID key, KeyModifierMask mask)
OSXScreen::registerHotKey(KeyID key, KeyModifierMask mask, bool registerGlobalHotkey)
{
// get mac virtual key and modifier mask matching barrier key and mask
UInt32 macKey, macMask;
Expand All @@ -336,7 +336,7 @@

// if this hot key has modifiers only then we'll handle it specially
EventHotKeyRef ref = NULL;
bool okay;
bool okay = true;
if (key == kKeyNone) {
if (m_modifierHotKeys.count(mask) > 0) {
// already registered
Expand All @@ -348,11 +348,13 @@
}
}
else {
EventHotKeyID hkid = { 'SNRG', (UInt32)id };
OSStatus status = RegisterEventHotKey(macKey, macMask, hkid,
GetApplicationEventTarget(), 0,
&ref);
okay = (status == noErr);
if (registerGlobalHotkey) {
EventHotKeyID hkid = { 'SNRG', (UInt32)id };
OSStatus status = RegisterEventHotKey(macKey, macMask, hkid,
GetApplicationEventTarget(), 0,
&ref);
okay = (status == noErr);
}
m_hotKeyToIDMap[HotKeyItem(macKey, macMask)] = id;
}

Expand All @@ -370,7 +372,7 @@
}

void
OSXScreen::unregisterHotKey(UInt32 id)
OSXScreen::unregisterHotKey(UInt32 id, bool unregisterGlobalHotkey)
{
// look up hotkey
HotKeyMap::iterator i = m_hotKeys.find(id);
Expand All @@ -379,9 +381,11 @@
}

// unregister with OS
bool okay;
bool okay = true;
if (i->second.getRef() != NULL) {
okay = (UnregisterEventHotKey(i->second.getRef()) == noErr);
if (unregisterGlobalHotkey) {
okay = (UnregisterEventHotKey(i->second.getRef()) == noErr);
}
}
else {
okay = false;
Expand Down
20 changes: 13 additions & 7 deletions src/lib/platform/XWindowsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ XWindowsScreen::warpCursor(SInt32 x, SInt32 y)
}

UInt32
XWindowsScreen::registerHotKey(KeyID key, KeyModifierMask mask)
XWindowsScreen::registerHotKey(KeyID key, KeyModifierMask mask, bool registerGlobalHotkey)
{
// only allow certain modifiers
if ((mask & ~(KeyModifierShift | KeyModifierControl |
Expand Down Expand Up @@ -642,8 +642,10 @@ XWindowsScreen::registerHotKey(KeyID key, KeyModifierMask mask)
for (int k = 0; k < modKeymap->max_keypermod && !err; ++k) {
KeyCode code = modifiermap[k];
if (modifiermap[k] != 0) {
m_impl->XGrabKey(m_display, code, modifiers2, m_root,
False, GrabModeAsync, GrabModeAsync);
if (registerGlobalHotkey) {
m_impl->XGrabKey(m_display, code, modifiers2, m_root,
False, GrabModeAsync, GrabModeAsync);
}
if (!err) {
hotKeys.push_back(std::make_pair(code, modifiers2));
m_hotKeyToIDMap[HotKeyItem(code, modifiers2)] = id;
Expand Down Expand Up @@ -689,8 +691,10 @@ XWindowsScreen::registerHotKey(KeyID key, KeyModifierMask mask)
}

// add grab
m_impl->XGrabKey(m_display, *j, tmpModifiers, m_root,
False, GrabModeAsync, GrabModeAsync);
if (registerGlobalHotkey) {
m_impl->XGrabKey(m_display, *j, tmpModifiers, m_root,
False, GrabModeAsync, GrabModeAsync);
}
if (!err) {
hotKeys.push_back(std::make_pair(*j, tmpModifiers));
m_hotKeyToIDMap[HotKeyItem(*j, tmpModifiers)] = id;
Expand Down Expand Up @@ -719,7 +723,7 @@ XWindowsScreen::registerHotKey(KeyID key, KeyModifierMask mask)
}

void
XWindowsScreen::unregisterHotKey(UInt32 id)
XWindowsScreen::unregisterHotKey(UInt32 id, bool unregisterGlobalHotkey)
{
// look up hotkey
HotKeyMap::iterator i = m_hotKeys.find(id);
Expand All @@ -734,7 +738,9 @@ XWindowsScreen::unregisterHotKey(UInt32 id)
HotKeyList& hotKeys = i->second;
for (HotKeyList::iterator j = hotKeys.begin();
j != hotKeys.end(); ++j) {
m_impl->XUngrabKey(m_display, j->first, j->second, m_root);
if (unregisterGlobalHotkey) {
m_impl->XUngrabKey(m_display, j->first, j->second, m_root);
}
m_hotKeyToIDMap.erase(HotKeyItem(j->first, j->second));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/platform/XWindowsScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class XWindowsScreen : public PlatformScreen {
// IPrimaryScreen overrides
virtual void reconfigure(UInt32 activeSides);
virtual void warpCursor(SInt32 x, SInt32 y);
virtual UInt32 registerHotKey(KeyID key, KeyModifierMask mask);
virtual void unregisterHotKey(UInt32 id);
virtual UInt32 registerHotKey(KeyID key, KeyModifierMask mask, bool registerGlobalHotkey);
virtual void unregisterHotKey(UInt32 id, bool unregisterGlobalHotkey);
virtual void fakeInputBegin();
virtual void fakeInputEnd();
virtual SInt32 getJumpZoneSize() const;
Expand Down
Loading