1 | /*
|
---|
2 | * synergy -- mouse and keyboard sharing utility
|
---|
3 | * Copyright (C) 2004 Chris Schoeneman
|
---|
4 | *
|
---|
5 | * This package is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU General Public License
|
---|
7 | * found in the file COPYING that should have accompanied this file.
|
---|
8 | *
|
---|
9 | * This package is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 | */
|
---|
14 |
|
---|
15 | #ifndef COSXKEYSTATE_H
|
---|
16 | #define COSXKEYSTATE_H
|
---|
17 |
|
---|
18 | #include "CKeyState.h"
|
---|
19 | #include "stdmap.h"
|
---|
20 | #include "stdset.h"
|
---|
21 | #include "stdvector.h"
|
---|
22 | #include <Carbon/Carbon.h>
|
---|
23 |
|
---|
24 | //! OS X key state
|
---|
25 | /*!
|
---|
26 | A key state for OS X.
|
---|
27 | */
|
---|
28 | class COSXKeyState : public CKeyState {
|
---|
29 | public:
|
---|
30 | typedef std::vector<KeyID> CKeyIDs;
|
---|
31 |
|
---|
32 | COSXKeyState();
|
---|
33 | virtual ~COSXKeyState();
|
---|
34 |
|
---|
35 | //! @name modifiers
|
---|
36 | //@{
|
---|
37 |
|
---|
38 | //! Handle modifier key change
|
---|
39 | /*!
|
---|
40 | Determines which modifier keys have changed and updates the modifier
|
---|
41 | state and sends key events as appropriate.
|
---|
42 | */
|
---|
43 | void handleModifierKeys(void* target,
|
---|
44 | KeyModifierMask oldMask, KeyModifierMask newMask);
|
---|
45 |
|
---|
46 | //@}
|
---|
47 | //! @name accessors
|
---|
48 | //@{
|
---|
49 |
|
---|
50 | //! Convert OS X modifier mask to synergy mask
|
---|
51 | /*!
|
---|
52 | Returns the synergy modifier mask corresponding to the OS X modifier
|
---|
53 | mask in \p mask.
|
---|
54 | */
|
---|
55 | KeyModifierMask mapModifiersFromOSX(UInt32 mask) const;
|
---|
56 |
|
---|
57 | //! Map key event to keys
|
---|
58 | /*!
|
---|
59 | Converts a key event into a sequence of KeyIDs and the shadow modifier
|
---|
60 | state to a modifier mask. The KeyIDs list, in order, the characters
|
---|
61 | generated by the key press/release. It returns the id of the button
|
---|
62 | that was pressed or released, or 0 if the button doesn't map to a known
|
---|
63 | KeyID.
|
---|
64 | */
|
---|
65 | KeyButton mapKeyFromEvent(CKeyIDs& ids,
|
---|
66 | KeyModifierMask* maskOut, EventRef event) const;
|
---|
67 |
|
---|
68 | //! Map key and mask to native values
|
---|
69 | /*!
|
---|
70 | Calculates mac virtual key and mask for a key \p key and modifiers
|
---|
71 | \p mask. Returns \c true if the key can be mapped, \c false otherwise.
|
---|
72 | */
|
---|
73 | bool mapSynergyHotKeyToMac(KeyID key, KeyModifierMask mask,
|
---|
74 | UInt32& macVirtualKey,
|
---|
75 | UInt32& macModifierMask) const;
|
---|
76 |
|
---|
77 | //@}
|
---|
78 |
|
---|
79 | // IKeyState overrides
|
---|
80 | virtual bool fakeCtrlAltDel();
|
---|
81 | virtual KeyModifierMask
|
---|
82 | pollActiveModifiers() const;
|
---|
83 | virtual SInt32 pollActiveGroup() const;
|
---|
84 | virtual void pollPressedKeys(KeyButtonSet& pressedKeys) const;
|
---|
85 |
|
---|
86 | protected:
|
---|
87 | // CKeyState overrides
|
---|
88 | virtual void getKeyMap(CKeyMap& keyMap);
|
---|
89 | virtual void fakeKey(const Keystroke& keystroke);
|
---|
90 |
|
---|
91 | private:
|
---|
92 | class CKeyResource;
|
---|
93 | typedef std::vector<KeyboardLayoutRef> GroupList;
|
---|
94 |
|
---|
95 | // Add hard coded special keys to a CKeyMap.
|
---|
96 | void getKeyMapForSpecialKeys(
|
---|
97 | CKeyMap& keyMap, SInt32 group) const;
|
---|
98 |
|
---|
99 | // Convert keyboard resource to a key map
|
---|
100 | bool getKeyMap(CKeyMap& keyMap,
|
---|
101 | SInt32 group, const CKeyResource& r) const;
|
---|
102 |
|
---|
103 | // Get the available keyboard groups
|
---|
104 | bool getGroups(GroupList&) const;
|
---|
105 |
|
---|
106 | // Change active keyboard group to group
|
---|
107 | void setGroup(SInt32 group);
|
---|
108 |
|
---|
109 | // Check if the keyboard layout has changed and update keyboard state
|
---|
110 | // if so.
|
---|
111 | void checkKeyboardLayout();
|
---|
112 |
|
---|
113 | // Send an event for the given modifier key
|
---|
114 | void handleModifierKey(void* target,
|
---|
115 | UInt32 virtualKey, KeyID id,
|
---|
116 | bool down, KeyModifierMask newMask);
|
---|
117 |
|
---|
118 | // Checks if any in \p ids is a glyph key and if \p isCommand is false.
|
---|
119 | // If so it adds the AltGr modifier to \p mask. This allows OS X
|
---|
120 | // servers to use the option key both as AltGr and as a modifier. If
|
---|
121 | // option is acting as AltGr (i.e. it generates a glyph and there are
|
---|
122 | // no command modifiers active) then we don't send the super modifier
|
---|
123 | // to clients because they'd try to match it as a command modifier.
|
---|
124 | void adjustAltGrModifier(const CKeyIDs& ids,
|
---|
125 | KeyModifierMask* mask, bool isCommand) const;
|
---|
126 |
|
---|
127 | // Maps an OS X virtual key id to a KeyButton. This simply remaps
|
---|
128 | // the ids so we don't use KeyButton 0.
|
---|
129 | static KeyButton mapVirtualKeyToKeyButton(UInt32 keyCode);
|
---|
130 |
|
---|
131 | // Maps a KeyButton to an OS X key code. This is the inverse of
|
---|
132 | // mapVirtualKeyToKeyButton.
|
---|
133 | static UInt32 mapKeyButtonToVirtualKey(KeyButton keyButton);
|
---|
134 |
|
---|
135 | private:
|
---|
136 | class CKeyResource : public IInterface {
|
---|
137 | public:
|
---|
138 | virtual bool isValid() const = 0;
|
---|
139 | virtual UInt32 getNumModifierCombinations() const = 0;
|
---|
140 | virtual UInt32 getNumTables() const = 0;
|
---|
141 | virtual UInt32 getNumButtons() const = 0;
|
---|
142 | virtual UInt32 getTableForModifier(UInt32 mask) const = 0;
|
---|
143 | virtual KeyID getKey(UInt32 table, UInt32 button) const = 0;
|
---|
144 |
|
---|
145 | // Convert a character in the current script to the equivalent KeyID
|
---|
146 | static KeyID getKeyID(UInt8);
|
---|
147 |
|
---|
148 | // Convert a unicode character to the equivalent KeyID.
|
---|
149 | static KeyID unicharToKeyID(UniChar);
|
---|
150 | };
|
---|
151 |
|
---|
152 | class CKCHRKeyResource : public CKeyResource {
|
---|
153 | public:
|
---|
154 | CKCHRKeyResource(const void*);
|
---|
155 |
|
---|
156 | // CKeyResource overrides
|
---|
157 | virtual bool isValid() const;
|
---|
158 | virtual UInt32 getNumModifierCombinations() const;
|
---|
159 | virtual UInt32 getNumTables() const;
|
---|
160 | virtual UInt32 getNumButtons() const;
|
---|
161 | virtual UInt32 getTableForModifier(UInt32 mask) const;
|
---|
162 | virtual KeyID getKey(UInt32 table, UInt32 button) const;
|
---|
163 |
|
---|
164 | private:
|
---|
165 | struct KCHRResource {
|
---|
166 | public:
|
---|
167 | SInt16 m_version;
|
---|
168 | UInt8 m_tableSelectionIndex[256];
|
---|
169 | SInt16 m_numTables;
|
---|
170 | UInt8 m_characterTables[1][128];
|
---|
171 | };
|
---|
172 | struct CKCHRDeadKeyRecord {
|
---|
173 | public:
|
---|
174 | UInt8 m_tableIndex;
|
---|
175 | UInt8 m_virtualKey;
|
---|
176 | SInt16 m_numCompletions;
|
---|
177 | UInt8 m_completion[1][2];
|
---|
178 | };
|
---|
179 | struct CKCHRDeadKeys {
|
---|
180 | public:
|
---|
181 | SInt16 m_numRecords;
|
---|
182 | CKCHRDeadKeyRecord m_records[1];
|
---|
183 | };
|
---|
184 |
|
---|
185 | const KCHRResource* m_resource;
|
---|
186 | };
|
---|
187 |
|
---|
188 | class CUCHRKeyResource : public CKeyResource {
|
---|
189 | public:
|
---|
190 | CUCHRKeyResource(const void*, UInt32 keyboardType);
|
---|
191 |
|
---|
192 | // CKeyResource overrides
|
---|
193 | virtual bool isValid() const;
|
---|
194 | virtual UInt32 getNumModifierCombinations() const;
|
---|
195 | virtual UInt32 getNumTables() const;
|
---|
196 | virtual UInt32 getNumButtons() const;
|
---|
197 | virtual UInt32 getTableForModifier(UInt32 mask) const;
|
---|
198 | virtual KeyID getKey(UInt32 table, UInt32 button) const;
|
---|
199 |
|
---|
200 | private:
|
---|
201 | typedef std::vector<KeyID> KeySequence;
|
---|
202 |
|
---|
203 | bool getDeadKey(KeySequence& keys, UInt16 index) const;
|
---|
204 | bool getKeyRecord(KeySequence& keys,
|
---|
205 | UInt16 index, UInt16& state) const;
|
---|
206 | bool addSequence(KeySequence& keys, UCKeyCharSeq c) const;
|
---|
207 |
|
---|
208 | private:
|
---|
209 | const UCKeyboardLayout* m_resource;
|
---|
210 | const UCKeyModifiersToTableNum* m_m;
|
---|
211 | const UCKeyToCharTableIndex* m_cti;
|
---|
212 | const UCKeySequenceDataIndex* m_sdi;
|
---|
213 | const UCKeyStateRecordsIndex* m_sri;
|
---|
214 | const UCKeyStateTerminators* m_st;
|
---|
215 | UInt16 m_spaceOutput;
|
---|
216 | };
|
---|
217 |
|
---|
218 | // OS X uses a physical key if 0 for the 'A' key. synergy reserves
|
---|
219 | // KeyButton 0 so we offset all OS X physical key ids by this much
|
---|
220 | // when used as a KeyButton and by minus this much to map a KeyButton
|
---|
221 | // to a physical button.
|
---|
222 | enum {
|
---|
223 | KeyButtonOffset = 1
|
---|
224 | };
|
---|
225 |
|
---|
226 | typedef std::map<KeyboardLayoutRef, SInt32> GroupMap;
|
---|
227 | typedef std::map<UInt32, KeyID> CVirtualKeyMap;
|
---|
228 |
|
---|
229 | CVirtualKeyMap m_virtualKeyMap;
|
---|
230 | mutable UInt32 m_deadKeyState;
|
---|
231 | GroupList m_groups;
|
---|
232 | GroupMap m_groupMap;
|
---|
233 | };
|
---|
234 |
|
---|
235 | #endif
|
---|