CKeyState.h

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2004 Chris Schoeneman
00004  * 
00005  * This package is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * found in the file COPYING that should have accompanied this file.
00008  * 
00009  * This package is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #ifndef CKEYSTATE_H
00016 #define CKEYSTATE_H
00017 
00018 #include "IKeyState.h"
00019 #include "CKeyMap.h"
00020 
00022 
00026 class CKeyState : public IKeyState {
00027 public:
00028     CKeyState();
00029     virtual ~CKeyState();
00030 
00032 
00033 
00035 
00041     virtual void        onKey(KeyButton button, bool down,
00042                             KeyModifierMask newState);
00043 
00045 
00050     virtual void        sendKeyEvent(void* target,
00051                             bool press, bool isAutoRepeat,
00052                             KeyID key, KeyModifierMask mask,
00053                             SInt32 count, KeyButton button);
00054 
00056 
00057 
00058 
00060 
00061     // IKeyState overrides
00062     virtual void        updateKeyMap();
00063     virtual void        updateKeyState();
00064     virtual void        setHalfDuplexMask(KeyModifierMask);
00065     virtual void        fakeKeyDown(KeyID id, KeyModifierMask mask,
00066                             KeyButton button);
00067     virtual void        fakeKeyRepeat(KeyID id, KeyModifierMask mask,
00068                             SInt32 count, KeyButton button);
00069     virtual void        fakeKeyUp(KeyButton button);
00070     virtual void        fakeAllKeysUp();
00071     virtual bool        fakeCtrlAltDel() = 0;
00072     virtual bool        isKeyDown(KeyButton) const;
00073     virtual KeyModifierMask
00074                         getActiveModifiers() const;
00075     virtual KeyModifierMask
00076                         pollActiveModifiers() const = 0;
00077     virtual SInt32      pollActiveGroup() const = 0;
00078     virtual void        pollPressedKeys(KeyButtonSet& pressedKeys) const = 0;
00079 
00080 protected:
00081     typedef CKeyMap::Keystroke Keystroke;
00082 
00084 
00085 
00087 
00090     virtual void        getKeyMap(CKeyMap& keyMap) = 0;
00091 
00093 
00096     virtual void        fakeKey(const Keystroke& keystroke) = 0;
00097 
00099 
00103     virtual KeyModifierMask&
00104                         getActiveModifiersRValue();
00105 
00107 
00108 
00109 
00111 
00114     SInt32              getEffectiveGroup(SInt32 group, SInt32 offset) const;
00115 
00117 
00121     virtual bool        isIgnoredKey(KeyID key, KeyModifierMask mask) const;
00122 
00124 
00128     KeyButton           getButton(KeyID id, SInt32 group) const;
00129 
00131 
00132 private:
00133     typedef CKeyMap::Keystrokes Keystrokes;
00134     typedef CKeyMap::ModifierToKeys ModifierToKeys;
00135     struct CAddActiveModifierContext {
00136     public:
00137         CAddActiveModifierContext(SInt32 group, KeyModifierMask mask,
00138                             ModifierToKeys& activeModifiers);
00139 
00140     public:
00141         SInt32          m_activeGroup;
00142         KeyModifierMask m_mask;
00143         ModifierToKeys& m_activeModifiers;
00144 
00145     private:
00146         // not implemented
00147         CAddActiveModifierContext(const CAddActiveModifierContext&);
00148         CAddActiveModifierContext& operator=(const CAddActiveModifierContext&);
00149     };
00150     
00151     class ButtonToKeyLess {
00152     public:
00153         bool operator()(const CKeyMap::ButtonToKeyMap::value_type& a,
00154                         const CKeyMap::ButtonToKeyMap::value_type b) const
00155         {
00156             return (a.first < b.first);
00157         }
00158     };
00159 
00160     // not implemented
00161     CKeyState(const CKeyState&);
00162     CKeyState& operator=(const CKeyState&);
00163 
00164     // adds alias key sequences.  these are sequences that are equivalent
00165     // to other sequences.
00166     void                addAliasEntries();
00167 
00168     // adds non-keypad key sequences for keypad KeyIDs
00169     void                addKeypadEntries();
00170 
00171     // adds key sequences for combination KeyIDs (those built using
00172     // dead keys)
00173     void                addCombinationEntries();
00174 
00175     // synthesize key events.  synthesize auto-repeat events count times.
00176     void                fakeKeys(const Keystrokes&, UInt32 count);
00177 
00178     // update key state to match changes to modifiers
00179     void                updateModifierKeyState(KeyButton button,
00180                             const ModifierToKeys& oldModifiers,
00181                             const ModifierToKeys& newModifiers);
00182 
00183     // active modifiers collection callback
00184     static void         addActiveModifierCB(KeyID id, SInt32 group,
00185                             CKeyMap::KeyItem& keyItem, void* vcontext);
00186 
00187 private:
00188     // the keyboard map
00189     CKeyMap             m_keyMap;
00190 
00191     // current modifier state
00192     KeyModifierMask     m_mask;
00193 
00194     // the active modifiers and the buttons activating them
00195     ModifierToKeys      m_activeModifiers;
00196 
00197     // current keyboard state (> 0 if pressed, 0 otherwise).  this is
00198     // initialized to the keyboard state according to the system then
00199     // it tracks synthesized events.
00200     SInt32              m_keys[kNumButtons];
00201 
00202     // synthetic keyboard state (> 0 if pressed, 0 otherwise).  this
00203     // tracks the synthesized keyboard state.  if m_keys[n] > 0 but
00204     // m_syntheticKeys[n] == 0 then the key was pressed locally and
00205     // not synthesized yet.
00206     SInt32              m_syntheticKeys[kNumButtons];
00207 
00208     // client data for each pressed key
00209     UInt32              m_keyClientData[kNumButtons];
00210 
00211     // server keyboard state.  an entry is 0 if not the key isn't pressed
00212     // otherwise it's the local KeyButton synthesized for the server key.
00213     KeyButton           m_serverKeys[kNumButtons];
00214 };
00215 
00216 #endif

Generated on Fri Nov 6 00:18:45 2009 for synergy-plus by  doxygen 1.4.7