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
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "OSXAutoTypes.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "common/stdmap.h"
|
|
|
|
|
#include "common/stdset.h"
|
|
|
|
|
#include "common/stdvector.h"
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "synergy/KeyState.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
|
|
2016-05-05 14:24:16 +00:00
|
|
|
class IOSXKeyResource;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//! OS X key state
|
|
|
|
|
/*!
|
|
|
|
|
A key state for OS X.
|
|
|
|
|
*/
|
2014-11-11 13:51:47 +00:00
|
|
|
class OSXKeyState : public KeyState {
|
2012-06-10 16:50:54 +00:00
|
|
|
public:
|
2024-07-02 19:07:06 +00:00
|
|
|
typedef std::vector<KeyID> KeyIDs;
|
|
|
|
|
|
2024-07-16 13:36:44 +00:00
|
|
|
OSXKeyState(
|
|
|
|
|
IEventQueue *events, std::vector<String> layouts, bool isLangSyncEnabled);
|
|
|
|
|
OSXKeyState(
|
|
|
|
|
IEventQueue *events, synergy::KeyMap &keyMap, std::vector<String> layouts,
|
|
|
|
|
bool isLangSyncEnabled);
|
2024-07-02 19:07:06 +00:00
|
|
|
virtual ~OSXKeyState();
|
|
|
|
|
|
|
|
|
|
//! @name modifiers
|
|
|
|
|
//@{
|
|
|
|
|
|
|
|
|
|
//! Handle modifier key change
|
|
|
|
|
/*!
|
|
|
|
|
Determines which modifier keys have changed and updates the modifier
|
|
|
|
|
state and sends key events as appropriate.
|
|
|
|
|
*/
|
2024-07-16 13:36:44 +00:00
|
|
|
void handleModifierKeys(
|
|
|
|
|
void *target, KeyModifierMask oldMask, KeyModifierMask newMask);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//@}
|
|
|
|
|
//! @name accessors
|
|
|
|
|
//@{
|
|
|
|
|
|
|
|
|
|
//! Convert OS X modifier mask to synergy mask
|
|
|
|
|
/*!
|
|
|
|
|
Returns the synergy modifier mask corresponding to the OS X modifier
|
|
|
|
|
mask in \p mask.
|
|
|
|
|
*/
|
|
|
|
|
KeyModifierMask mapModifiersFromOSX(UInt32 mask) const;
|
|
|
|
|
|
|
|
|
|
//! Convert CG flags-style modifier mask to old-style Carbon
|
|
|
|
|
/*!
|
|
|
|
|
Still required in a few places for translation calls.
|
|
|
|
|
*/
|
|
|
|
|
KeyModifierMask mapModifiersToCarbon(UInt32 mask) const;
|
|
|
|
|
|
|
|
|
|
//! Map key event to keys
|
|
|
|
|
/*!
|
|
|
|
|
Converts a key event into a sequence of KeyIDs and the shadow modifier
|
|
|
|
|
state to a modifier mask. The KeyIDs list, in order, the characters
|
|
|
|
|
generated by the key press/release. It returns the id of the button
|
|
|
|
|
that was pressed or released, or 0 if the button doesn't map to a known
|
|
|
|
|
KeyID.
|
|
|
|
|
*/
|
2024-07-16 13:36:44 +00:00
|
|
|
KeyButton mapKeyFromEvent(
|
|
|
|
|
KeyIDs &ids, KeyModifierMask *maskOut, CGEventRef event) const;
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Map key and mask to native values
|
|
|
|
|
/*!
|
|
|
|
|
Calculates mac virtual key and mask for a key \p key and modifiers
|
|
|
|
|
\p mask. Returns \c true if the key can be mapped, \c false otherwise.
|
|
|
|
|
*/
|
2024-07-16 13:36:44 +00:00
|
|
|
bool mapSynergyHotKeyToMac(
|
|
|
|
|
KeyID key, KeyModifierMask mask, UInt32 &macVirtualKey,
|
|
|
|
|
UInt32 &macModifierMask) const;
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//@}
|
|
|
|
|
|
|
|
|
|
// IKeyState overrides
|
|
|
|
|
virtual bool fakeCtrlAltDel();
|
|
|
|
|
virtual bool fakeMediaKey(KeyID id);
|
|
|
|
|
virtual KeyModifierMask pollActiveModifiers() const;
|
|
|
|
|
virtual SInt32 pollActiveGroup() const;
|
|
|
|
|
virtual void pollPressedKeys(KeyButtonSet &pressedKeys) const;
|
|
|
|
|
|
|
|
|
|
CGEventFlags getModifierStateAsOSXFlags() const;
|
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
protected:
|
2024-07-02 19:07:06 +00:00
|
|
|
// KeyState overrides
|
|
|
|
|
virtual void getKeyMap(synergy::KeyMap &keyMap);
|
|
|
|
|
virtual void fakeKey(const Keystroke &keystroke);
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
private:
|
2024-07-02 19:07:06 +00:00
|
|
|
class KeyResource;
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// Add hard coded special keys to a synergy::KeyMap.
|
|
|
|
|
void getKeyMapForSpecialKeys(synergy::KeyMap &keyMap, SInt32 group) const;
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// Convert keyboard resource to a key map
|
2024-07-16 13:36:44 +00:00
|
|
|
bool getKeyMap(
|
|
|
|
|
synergy::KeyMap &keyMap, SInt32 group, const IOSXKeyResource &r) const;
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// Get the available keyboard groups
|
|
|
|
|
bool getGroups(AutoCFArray &) const;
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// Change active keyboard group to group
|
|
|
|
|
void setGroup(SInt32 group);
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// Send an event for the given modifier key
|
2024-07-16 13:36:44 +00:00
|
|
|
void handleModifierKey(
|
|
|
|
|
void *target, UInt32 virtualKey, KeyID id, bool down,
|
|
|
|
|
KeyModifierMask newMask);
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// Checks if any in \p ids is a glyph key and if \p isCommand is false.
|
|
|
|
|
// If so it adds the AltGr modifier to \p mask. This allows OS X
|
|
|
|
|
// servers to use the option key both as AltGr and as a modifier. If
|
|
|
|
|
// option is acting as AltGr (i.e. it generates a glyph and there are
|
|
|
|
|
// no command modifiers active) then we don't send the super modifier
|
|
|
|
|
// to clients because they'd try to match it as a command modifier.
|
2024-07-16 13:36:44 +00:00
|
|
|
void adjustAltGrModifier(
|
|
|
|
|
const KeyIDs &ids, KeyModifierMask *mask, bool isCommand) const;
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// Maps an OS X virtual key id to a KeyButton. This simply remaps
|
|
|
|
|
// the ids so we don't use KeyButton 0.
|
|
|
|
|
static KeyButton mapVirtualKeyToKeyButton(UInt32 keyCode);
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// Maps a KeyButton to an OS X key code. This is the inverse of
|
|
|
|
|
// mapVirtualKeyToKeyButton.
|
|
|
|
|
static UInt32 mapKeyButtonToVirtualKey(KeyButton keyButton);
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void init();
|
2021-08-17 13:40:36 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// Post a key event to HID manager. It posts an event to HID client, a
|
|
|
|
|
// much lower level than window manager which's the target from carbon
|
|
|
|
|
// CGEventPost
|
|
|
|
|
kern_return_t postHIDVirtualKey(UInt8 virtualKeyCode, bool postDown);
|
2022-01-13 16:58:29 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// Get keyboard event flags accorfing to keyboard modifiers
|
|
|
|
|
CGEventFlags getKeyboardEventFlags() const;
|
|
|
|
|
CGEventFlags getDeviceDependedFlags() const;
|
2021-08-17 13:40:36 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void setKeyboardModifiers(CGKeyCode virtualKey, bool keyDown);
|
2021-08-17 13:40:36 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void postKeyboardKey(CGKeyCode virtualKey, bool keyDown);
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
private:
|
2024-07-02 19:07:06 +00:00
|
|
|
// OS X uses a physical key if 0 for the 'A' key. synergy reserves
|
|
|
|
|
// KeyButton 0 so we offset all OS X physical key ids by this much
|
|
|
|
|
// when used as a KeyButton and by minus this much to map a KeyButton
|
|
|
|
|
// to a physical button.
|
|
|
|
|
enum { KeyButtonOffset = 1 };
|
|
|
|
|
|
|
|
|
|
typedef std::map<CFDataRef, SInt32> GroupMap;
|
|
|
|
|
typedef std::map<UInt32, KeyID> VirtualKeyMap;
|
|
|
|
|
|
|
|
|
|
VirtualKeyMap m_virtualKeyMap;
|
|
|
|
|
mutable UInt32 m_deadKeyState;
|
|
|
|
|
AutoCFArray m_groups{nullptr, CFRelease};
|
|
|
|
|
GroupMap m_groupMap;
|
|
|
|
|
bool m_shiftPressed;
|
|
|
|
|
bool m_controlPressed;
|
|
|
|
|
bool m_altPressed;
|
|
|
|
|
bool m_superPressed;
|
|
|
|
|
bool m_capsPressed;
|
2012-06-10 16:50:54 +00:00
|
|
|
};
|