2012-06-10 16:50:54 +00:00
|
|
|
/*
|
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
2016-09-07 14:24:00 +00:00
|
|
|
* Copyright (C) 2012-2016 Symless Ltd.
|
2012-09-04 02:09:56 +00:00
|
|
|
* Copyright (C) 2004 Chris Schoeneman
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#pragma once
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "synergy/IKeyState.h"
|
|
|
|
|
#include "synergy/KeyMap.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//! Core key state
|
|
|
|
|
/*!
|
|
|
|
|
This class provides key state services. Subclasses must implement a few
|
|
|
|
|
platform specific methods.
|
|
|
|
|
*/
|
2014-11-11 13:51:47 +00:00
|
|
|
class KeyState : public IKeyState {
|
2012-06-10 16:50:54 +00:00
|
|
|
public:
|
2024-07-16 13:36:44 +00:00
|
|
|
KeyState(
|
|
|
|
|
IEventQueue *events, std::vector<String> layouts, bool isLangSyncEnabled);
|
|
|
|
|
KeyState(
|
|
|
|
|
IEventQueue *events, synergy::KeyMap &keyMap, std::vector<String> layouts,
|
|
|
|
|
bool isLangSyncEnabled);
|
2024-07-02 19:07:06 +00:00
|
|
|
virtual ~KeyState();
|
|
|
|
|
|
|
|
|
|
//! @name manipulators
|
|
|
|
|
//@{
|
|
|
|
|
|
|
|
|
|
//! Handle key event
|
|
|
|
|
/*!
|
|
|
|
|
Sets the state of \p button to down or up and updates the current
|
|
|
|
|
modifier state to \p newState. This method should be called by
|
|
|
|
|
primary screens only in response to local events. For auto-repeat
|
|
|
|
|
set \p down to \c true. Overrides must forward to the superclass.
|
|
|
|
|
*/
|
|
|
|
|
virtual void onKey(KeyButton button, bool down, KeyModifierMask newState);
|
|
|
|
|
|
|
|
|
|
//! Post a key event
|
|
|
|
|
/*!
|
|
|
|
|
Posts a key event. This may adjust the event or post additional
|
|
|
|
|
events in some circumstances. If this is overridden it must forward
|
|
|
|
|
to the superclass.
|
|
|
|
|
*/
|
2024-07-16 13:36:44 +00:00
|
|
|
virtual void sendKeyEvent(
|
|
|
|
|
void *target, bool press, bool isAutoRepeat, KeyID key,
|
|
|
|
|
KeyModifierMask mask, SInt32 count, KeyButton button);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//@}
|
|
|
|
|
//! @name accessors
|
|
|
|
|
//@{
|
|
|
|
|
|
|
|
|
|
//@}
|
|
|
|
|
|
|
|
|
|
void updateKeyMap(synergy::KeyMap *existing);
|
|
|
|
|
// IKeyState overrides
|
|
|
|
|
void updateKeyMap() override { this->updateKeyMap(nullptr); }
|
|
|
|
|
void updateKeyState() override;
|
|
|
|
|
void setHalfDuplexMask(KeyModifierMask) override;
|
2024-07-16 13:36:44 +00:00
|
|
|
void fakeKeyDown(
|
|
|
|
|
KeyID id, KeyModifierMask mask, KeyButton button,
|
|
|
|
|
const String &lang) override;
|
|
|
|
|
bool fakeKeyRepeat(
|
|
|
|
|
KeyID id, KeyModifierMask mask, SInt32 count, KeyButton button,
|
|
|
|
|
const String &lang) override;
|
2024-07-02 19:07:06 +00:00
|
|
|
bool fakeKeyUp(KeyButton button) override;
|
|
|
|
|
void fakeAllKeysUp() override;
|
|
|
|
|
bool fakeMediaKey(KeyID id) override;
|
|
|
|
|
|
|
|
|
|
bool isKeyDown(KeyButton) const override;
|
|
|
|
|
KeyModifierMask getActiveModifiers() const override;
|
|
|
|
|
// Left abstract
|
|
|
|
|
virtual bool fakeCtrlAltDel() = 0;
|
|
|
|
|
virtual KeyModifierMask pollActiveModifiers() const = 0;
|
|
|
|
|
virtual SInt32 pollActiveGroup() const = 0;
|
|
|
|
|
virtual void pollPressedKeys(KeyButtonSet &pressedKeys) const = 0;
|
|
|
|
|
|
|
|
|
|
SInt32 getKeyState(KeyButton keyButton) { return m_keys[keyButton]; }
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
protected:
|
2024-07-02 19:07:06 +00:00
|
|
|
typedef synergy::KeyMap::Keystroke Keystroke;
|
|
|
|
|
|
|
|
|
|
//! @name protected manipulators
|
|
|
|
|
//@{
|
|
|
|
|
|
|
|
|
|
//! Get the keyboard map
|
|
|
|
|
/*!
|
|
|
|
|
Fills \p keyMap with the current keyboard map.
|
|
|
|
|
*/
|
|
|
|
|
virtual void getKeyMap(synergy::KeyMap &keyMap) = 0;
|
|
|
|
|
|
|
|
|
|
//! Fake a key event
|
|
|
|
|
/*!
|
|
|
|
|
Synthesize an event for \p keystroke.
|
|
|
|
|
*/
|
|
|
|
|
virtual void fakeKey(const Keystroke &keystroke) = 0;
|
|
|
|
|
|
|
|
|
|
//! Get the active modifiers
|
|
|
|
|
/*!
|
|
|
|
|
Returns the modifiers that are currently active according to our
|
|
|
|
|
shadowed state. The state may be modified.
|
|
|
|
|
*/
|
|
|
|
|
virtual KeyModifierMask &getActiveModifiersRValue();
|
|
|
|
|
|
|
|
|
|
//@}
|
|
|
|
|
//! @name protected accessors
|
|
|
|
|
//@{
|
|
|
|
|
|
|
|
|
|
//! Compute a group number
|
|
|
|
|
/*!
|
|
|
|
|
Returns the number of the group \p offset groups after group \p group.
|
|
|
|
|
*/
|
|
|
|
|
SInt32 getEffectiveGroup(SInt32 group, SInt32 offset) const;
|
|
|
|
|
|
|
|
|
|
//! Check if key is ignored
|
|
|
|
|
/*!
|
|
|
|
|
Returns \c true if and only if the key should always be ignored.
|
|
|
|
|
The default returns \c true only for the toggle keys.
|
|
|
|
|
*/
|
|
|
|
|
virtual bool isIgnoredKey(KeyID key, KeyModifierMask mask) const;
|
|
|
|
|
|
|
|
|
|
//! Get button for a KeyID
|
|
|
|
|
/*!
|
|
|
|
|
Return the button mapped to key \p id in group \p group if any,
|
|
|
|
|
otherwise returns 0.
|
|
|
|
|
*/
|
|
|
|
|
KeyButton getButton(KeyID id, SInt32 group) const;
|
|
|
|
|
|
|
|
|
|
//@}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
private:
|
2024-07-02 19:07:06 +00:00
|
|
|
typedef synergy::KeyMap::Keystrokes Keystrokes;
|
|
|
|
|
typedef synergy::KeyMap::ModifierToKeys ModifierToKeys;
|
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
public:
|
2024-07-02 19:07:06 +00:00
|
|
|
struct AddActiveModifierContext {
|
|
|
|
|
public:
|
2024-07-16 13:36:44 +00:00
|
|
|
AddActiveModifierContext(
|
|
|
|
|
SInt32 group, KeyModifierMask mask, ModifierToKeys &activeModifiers);
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
public:
|
|
|
|
|
SInt32 m_activeGroup;
|
|
|
|
|
KeyModifierMask m_mask;
|
|
|
|
|
ModifierToKeys &m_activeModifiers;
|
|
|
|
|
|
|
|
|
|
private:
|
2016-12-28 11:50:32 +00:00
|
|
|
// not implemented
|
2024-07-02 19:07:06 +00:00
|
|
|
AddActiveModifierContext(const AddActiveModifierContext &);
|
|
|
|
|
AddActiveModifierContext &operator=(const AddActiveModifierContext &);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
class ButtonToKeyLess {
|
|
|
|
|
public:
|
2024-07-16 13:36:44 +00:00
|
|
|
bool operator()(
|
|
|
|
|
const synergy::KeyMap::ButtonToKeyMap::value_type &a,
|
|
|
|
|
const synergy::KeyMap::ButtonToKeyMap::value_type b) const {
|
2024-07-02 19:07:06 +00:00
|
|
|
return (a.first < b.first);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// not implemented
|
|
|
|
|
KeyState(const KeyState &);
|
|
|
|
|
KeyState &operator=(const KeyState &);
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// called by all ctors.
|
|
|
|
|
void init();
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// adds alias key sequences. these are sequences that are equivalent
|
|
|
|
|
// to other sequences.
|
|
|
|
|
void addAliasEntries();
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// adds non-keypad key sequences for keypad KeyIDs
|
|
|
|
|
void addKeypadEntries();
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// adds key sequences for combination KeyIDs (those built using
|
|
|
|
|
// dead keys)
|
|
|
|
|
void addCombinationEntries();
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// synthesize key events. synthesize auto-repeat events count times.
|
|
|
|
|
void fakeKeys(const Keystrokes &, UInt32 count);
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// update key state to match changes to modifiers
|
2024-07-16 13:36:44 +00:00
|
|
|
void updateModifierKeyState(
|
|
|
|
|
KeyButton button, const ModifierToKeys &oldModifiers,
|
|
|
|
|
const ModifierToKeys &newModifiers);
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// active modifiers collection callback
|
2024-07-16 13:36:44 +00:00
|
|
|
static void addActiveModifierCB(
|
|
|
|
|
KeyID id, SInt32 group, synergy::KeyMap::KeyItem &keyItem,
|
|
|
|
|
void *vcontext);
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
private:
|
2024-07-02 19:07:06 +00:00
|
|
|
// must be declared before m_keyMap. used when this class owns the key map.
|
|
|
|
|
synergy::KeyMap *m_keyMapPtr;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// the keyboard map
|
|
|
|
|
synergy::KeyMap &m_keyMap;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// current modifier state
|
|
|
|
|
KeyModifierMask m_mask;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// the active modifiers and the buttons activating them
|
|
|
|
|
ModifierToKeys m_activeModifiers;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// current keyboard state (> 0 if pressed, 0 otherwise). this is
|
|
|
|
|
// initialized to the keyboard state according to the system then
|
|
|
|
|
// it tracks synthesized events.
|
|
|
|
|
SInt32 m_keys[kNumButtons];
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// synthetic keyboard state (> 0 if pressed, 0 otherwise). this
|
|
|
|
|
// tracks the synthesized keyboard state. if m_keys[n] > 0 but
|
|
|
|
|
// m_syntheticKeys[n] == 0 then the key was pressed locally and
|
|
|
|
|
// not synthesized yet.
|
|
|
|
|
SInt32 m_syntheticKeys[kNumButtons];
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// client data for each pressed key
|
|
|
|
|
UInt32 m_keyClientData[kNumButtons];
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// server keyboard state. an entry is 0 if not the key isn't pressed
|
|
|
|
|
// otherwise it's the local KeyButton synthesized for the server key.
|
|
|
|
|
KeyButton m_serverKeys[kNumButtons];
|
2013-06-29 14:17:49 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
IEventQueue *m_events;
|
2021-10-04 15:51:12 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
bool m_isLangSyncEnabled;
|
2012-06-10 16:50:54 +00:00
|
|
|
};
|