• Main Page
  • Classes
  • Files
  • File List

COSXKeyState.h

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2012 Bolton Software Ltd.
00004  * Copyright (C) 2004 Chris Schoeneman
00005  * 
00006  * This package is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * found in the file COPYING that should have accompanied this file.
00009  * 
00010  * This package is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #ifndef COSXKEYSTATE_H
00020 #define COSXKEYSTATE_H
00021 
00022 #include <Carbon/Carbon.h>
00023 #include "CKeyState.h"
00024 #include "stdmap.h"
00025 #include "stdset.h"
00026 #include "stdvector.h"
00027 
00028 #if defined(MAC_OS_X_VERSION_10_5)
00029     typedef TISInputSourceRef KeyLayout;
00030 #else
00031     typedef KeyboardLayoutRef KeyLayout;
00032 #endif
00033 
00035 
00038 class COSXKeyState : public CKeyState {
00039 public:
00040     typedef std::vector<KeyID> CKeyIDs;
00041 
00042     COSXKeyState();
00043     COSXKeyState(IEventQueue& eventQueue, CKeyMap& keyMap);
00044     virtual ~COSXKeyState();
00045 
00047 
00048 
00050 
00054     void                handleModifierKeys(void* target,
00055                             KeyModifierMask oldMask, KeyModifierMask newMask);
00056 
00058 
00059 
00060 
00062 
00066     KeyModifierMask     mapModifiersFromOSX(UInt32 mask) const;
00067 
00069 
00072     KeyModifierMask     mapModifiersToCarbon(UInt32 mask) const;
00073     
00075 
00082     KeyButton           mapKeyFromEvent(CKeyIDs& ids,
00083                             KeyModifierMask* maskOut, CGEventRef event) const;
00084 
00086 
00090     bool                mapSynergyHotKeyToMac(KeyID key, KeyModifierMask mask,
00091                             UInt32& macVirtualKey,
00092                             UInt32& macModifierMask) const;
00093 
00095 
00096     // IKeyState overrides
00097     virtual bool        fakeCtrlAltDel();
00098     virtual KeyModifierMask
00099                         pollActiveModifiers() const;
00100     virtual SInt32      pollActiveGroup() const;
00101     virtual void        pollPressedKeys(KeyButtonSet& pressedKeys) const;
00102 
00103 protected:
00104     // CKeyState overrides
00105     virtual void        getKeyMap(CKeyMap& keyMap);
00106     virtual void        fakeKey(const Keystroke& keystroke);
00107 
00108 private:
00109     class CKeyResource;
00110     typedef std::vector<KeyLayout> GroupList;
00111 
00112     // Add hard coded special keys to a CKeyMap.
00113     void                getKeyMapForSpecialKeys(
00114                             CKeyMap& keyMap, SInt32 group) const;
00115 
00116     // Convert keyboard resource to a key map
00117     bool                getKeyMap(CKeyMap& keyMap,
00118                             SInt32 group, const CKeyResource& r) const;
00119 
00120     // Get the available keyboard groups
00121     bool                getGroups(GroupList&) const;
00122 
00123     // Change active keyboard group to group
00124     void                setGroup(SInt32 group);
00125 
00126     // Check if the keyboard layout has changed and update keyboard state
00127     // if so.
00128     void                checkKeyboardLayout();
00129 
00130     // Send an event for the given modifier key
00131     void                handleModifierKey(void* target,
00132                             UInt32 virtualKey, KeyID id,
00133                             bool down, KeyModifierMask newMask);
00134 
00135     // Checks if any in \p ids is a glyph key and if \p isCommand is false.
00136     // If so it adds the AltGr modifier to \p mask.  This allows OS X
00137     // servers to use the option key both as AltGr and as a modifier.  If
00138     // option is acting as AltGr (i.e. it generates a glyph and there are
00139     // no command modifiers active) then we don't send the super modifier
00140     // to clients because they'd try to match it as a command modifier.
00141     void                adjustAltGrModifier(const CKeyIDs& ids,
00142                             KeyModifierMask* mask, bool isCommand) const;
00143 
00144     // Maps an OS X virtual key id to a KeyButton.  This simply remaps
00145     // the ids so we don't use KeyButton 0.
00146     static KeyButton    mapVirtualKeyToKeyButton(UInt32 keyCode);
00147 
00148     // Maps a KeyButton to an OS X key code.  This is the inverse of
00149     // mapVirtualKeyToKeyButton.
00150     static UInt32       mapKeyButtonToVirtualKey(KeyButton keyButton);
00151 
00152     void                init();
00153 
00154 private:
00155     class CKeyResource : public IInterface {
00156     public:
00157         virtual bool    isValid() const = 0;
00158         virtual UInt32  getNumModifierCombinations() const = 0;
00159         virtual UInt32  getNumTables() const = 0;
00160         virtual UInt32  getNumButtons() const = 0;
00161         virtual UInt32  getTableForModifier(UInt32 mask) const = 0;
00162         virtual KeyID   getKey(UInt32 table, UInt32 button) const = 0;
00163 
00164         // Convert a character in the current script to the equivalent KeyID
00165         static KeyID    getKeyID(UInt8);
00166 
00167         // Convert a unicode character to the equivalent KeyID.
00168         static KeyID    unicharToKeyID(UniChar);
00169     };
00170 
00171 
00172     class CUCHRKeyResource : public CKeyResource {
00173     public:
00174         CUCHRKeyResource(const void*, UInt32 keyboardType);
00175 
00176         // CKeyResource overrides
00177         virtual bool    isValid() const;
00178         virtual UInt32  getNumModifierCombinations() const;
00179         virtual UInt32  getNumTables() const;
00180         virtual UInt32  getNumButtons() const;
00181         virtual UInt32  getTableForModifier(UInt32 mask) const;
00182         virtual KeyID   getKey(UInt32 table, UInt32 button) const;
00183 
00184     private:
00185         typedef std::vector<KeyID> KeySequence;
00186 
00187         bool            getDeadKey(KeySequence& keys, UInt16 index) const;
00188         bool            getKeyRecord(KeySequence& keys,
00189                             UInt16 index, UInt16& state) const;
00190         bool            addSequence(KeySequence& keys, UCKeyCharSeq c) const;
00191 
00192     private:
00193         const UCKeyboardLayout*         m_resource;
00194         const UCKeyModifiersToTableNum* m_m;
00195         const UCKeyToCharTableIndex*    m_cti;
00196         const UCKeySequenceDataIndex*   m_sdi;
00197         const UCKeyStateRecordsIndex*   m_sri;
00198         const UCKeyStateTerminators*    m_st;
00199         UInt16                          m_spaceOutput;
00200     };
00201 
00202     // OS X uses a physical key if 0 for the 'A' key.  synergy reserves
00203     // KeyButton 0 so we offset all OS X physical key ids by this much
00204     // when used as a KeyButton and by minus this much to map a KeyButton
00205     // to a physical button.
00206     enum {
00207         KeyButtonOffset = 1
00208     };
00209 
00210     typedef std::map<KeyLayout, SInt32> GroupMap;
00211     typedef std::map<UInt32, KeyID> CVirtualKeyMap;
00212 
00213     CVirtualKeyMap      m_virtualKeyMap;
00214     mutable UInt32      m_deadKeyState;
00215     GroupList           m_groups;
00216     GroupMap            m_groupMap;
00217     
00218     // Hold the current state of modifier keys
00219     bool shiftPressed;
00220     bool controlPressed;
00221     bool altPressed;
00222     bool superPressed;
00223     bool capsPressed;
00224 };
00225 
00226 #endif

Generated on Wed May 22 2013 00:00:05 for Synergy by  doxygen 1.7.1