00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef CKEYMAP_H
00020 #define CKEYMAP_H
00021
00022 #include "KeyTypes.h"
00023 #include "CString.h"
00024 #include "CStringUtil.h"
00025 #include "stdmap.h"
00026 #include "stdset.h"
00027 #include "stdvector.h"
00028
00030
00033 class CKeyMap {
00034 public:
00035 CKeyMap();
00036 ~CKeyMap();
00037
00039
00047 struct KeyItem {
00048 public:
00049 KeyID m_id;
00050 SInt32 m_group;
00051 KeyButton m_button;
00052 KeyModifierMask m_required;
00053 KeyModifierMask m_sensitive;
00054 KeyModifierMask m_generates;
00055 bool m_dead;
00056 bool m_lock;
00057 UInt32 m_client;
00058
00059 public:
00060 bool operator==(const KeyItem&) const;
00061 };
00062
00064
00073 typedef std::vector<KeyItem> KeyItemList;
00074
00076 class Keystroke {
00077 public:
00078 enum EType {
00079 kButton,
00080 kGroup
00081 };
00082
00083 Keystroke(KeyButton, bool press, bool repeat, UInt32 clientData);
00084 Keystroke(SInt32 group, bool absolute, bool restore);
00085
00086 public:
00087 struct CButton {
00088 public:
00089 KeyButton m_button;
00090 bool m_press;
00091 bool m_repeat;
00092 UInt32 m_client;
00093 };
00094 struct CGroup {
00095 public:
00096 SInt32 m_group;
00097 bool m_absolute;
00098 bool m_restore;
00099 };
00100 union CData {
00101 public:
00102 CButton m_button;
00103 CGroup m_group;
00104 };
00105
00106 EType m_type;
00107 CData m_data;
00108 };
00109
00111 typedef std::vector<Keystroke> Keystrokes;
00112
00114 typedef std::multimap<KeyModifierMask, KeyItem> ModifierToKeys;
00115
00117 typedef std::map<KeyButton, const KeyItem*> ButtonToKeyMap;
00118
00120 typedef void (*ForeachKeyCallback)(KeyID, SInt32 group,
00121 KeyItem&, void* userData);
00122
00124
00125
00127 virtual void swap(CKeyMap&);
00128
00130
00134 void addKeyEntry(const KeyItem& item);
00135
00137
00146 void addKeyAliasEntry(KeyID targetID, SInt32 group,
00147 KeyModifierMask targetRequired,
00148 KeyModifierMask targetSensitive,
00149 KeyID sourceID,
00150 KeyModifierMask sourceRequired,
00151 KeyModifierMask sourceSensitive);
00152
00154
00163 bool addKeyCombinationEntry(KeyID id, SInt32 group,
00164 const KeyID* keys, UInt32 numKeys);
00165
00167
00171 void allowGroupSwitchDuringCompose();
00172
00174
00179 void addHalfDuplexButton(KeyButton button);
00180
00182
00186 void clearHalfDuplexModifiers();
00187
00189
00193 virtual void addHalfDuplexModifier(KeyID key);
00194
00196
00199 virtual void finish();
00200
00202
00205 virtual void foreachKey(ForeachKeyCallback cb, void* userData);
00206
00208
00209
00210
00212
00219 virtual const KeyItem* mapKey(Keystrokes& keys, KeyID id, SInt32 group,
00220 ModifierToKeys& activeModifiers,
00221 KeyModifierMask& currentState,
00222 KeyModifierMask desiredMask,
00223 bool isAutoRepeat) const;
00224
00226
00229 SInt32 getNumGroups() const;
00230
00232
00235 SInt32 getEffectiveGroup(SInt32 group, SInt32 offset) const;
00236
00238
00246 const KeyItemList* findCompatibleKey(KeyID id, SInt32 group,
00247 KeyModifierMask required,
00248 KeyModifierMask sensitive) const;
00249
00251
00255 virtual bool isHalfDuplex(KeyID key, KeyButton button) const;
00256
00258
00266 bool isCommand(KeyModifierMask mask) const;
00267
00268
00273 KeyModifierMask getCommandModifiers() const;
00274
00276
00279 static void collectButtons(const ModifierToKeys& modifiers,
00280 ButtonToKeyMap& keys);
00281
00283
00287 static void initModifierKey(KeyItem& item);
00288
00290
00293 static bool isDeadKey(KeyID key);
00294
00296
00300 static KeyID getDeadKey(KeyID key);
00301
00303
00307 static CString formatKey(KeyID key, KeyModifierMask);
00308
00310
00314 static bool parseKey(const CString&, KeyID&);
00315
00317
00323 static bool parseModifiers(CString&, KeyModifierMask&);
00324
00326
00327 private:
00329 enum EKeystroke {
00330 kKeystrokePress,
00331 kKeystrokeRelease,
00332 kKeystrokeRepeat,
00333 kKeystrokeClick,
00334 kKeystrokeModify,
00335 kKeystrokeUnmodify
00336 };
00337
00338
00339 typedef std::vector<KeyItemList> KeyEntryList;
00340
00341
00342 SInt32 findNumGroups() const;
00343
00344
00345 void setModifierKeys();
00346
00347
00348
00349
00350
00351
00352
00353 const KeyItem* mapCommandKey(Keystrokes& keys,
00354 KeyID id, SInt32 group,
00355 ModifierToKeys& activeModifiers,
00356 KeyModifierMask& currentState,
00357 KeyModifierMask desiredMask,
00358 bool isAutoRepeat) const;
00359
00360
00361
00362
00363 const KeyItem* mapCharacterKey(Keystrokes& keys,
00364 KeyID id, SInt32 group,
00365 ModifierToKeys& activeModifiers,
00366 KeyModifierMask& currentState,
00367 KeyModifierMask desiredMask,
00368 bool isAutoRepeat) const;
00369
00370
00371 const KeyItem* mapModifierKey(Keystrokes& keys,
00372 KeyID id, SInt32 group,
00373 ModifierToKeys& activeModifiers,
00374 KeyModifierMask& currentState,
00375 KeyModifierMask desiredMask,
00376 bool isAutoRepeat) const;
00377
00378
00379
00380
00381 SInt32 findBestKey(const KeyEntryList& entryList,
00382 KeyModifierMask currentState,
00383 KeyModifierMask desiredState) const;
00384
00385
00386
00387
00388 const KeyItem* keyForModifier(KeyButton button, SInt32 group,
00389 SInt32 modifierBit) const;
00390
00391
00392
00393
00394
00395 bool keysForKeyItem(const KeyItem& keyItem,
00396 SInt32& group,
00397 ModifierToKeys& activeModifiers,
00398 KeyModifierMask& currentState,
00399 KeyModifierMask desiredState,
00400 KeyModifierMask overrideModifiers,
00401 bool isAutoRepeat,
00402 Keystrokes& keystrokes) const;
00403
00404
00405
00406
00407
00408 bool keysToRestoreModifiers(const KeyItem& keyItem,
00409 SInt32 group,
00410 ModifierToKeys& activeModifiers,
00411 KeyModifierMask& currentState,
00412 const ModifierToKeys& desiredModifiers,
00413 Keystrokes& keystrokes) const;
00414
00415
00416
00417
00418
00419
00420 bool keysForModifierState(KeyButton button, SInt32 group,
00421 ModifierToKeys& activeModifiers,
00422 KeyModifierMask& currentState,
00423 KeyModifierMask requiredState,
00424 KeyModifierMask sensitiveMask,
00425 KeyModifierMask notRequiredMask,
00426 Keystrokes& keystrokes) const;
00427
00428
00429
00430 void addKeystrokes(EKeystroke type,
00431 const KeyItem& keyItem,
00432 ModifierToKeys& activeModifiers,
00433 KeyModifierMask& currentState,
00434 Keystrokes& keystrokes) const;
00435
00436
00437 static SInt32 getNumModifiers(KeyModifierMask state);
00438
00439
00440 static void initKeyNameMaps();
00441
00442
00443 CKeyMap(const CKeyMap&);
00444 CKeyMap& operator=(const CKeyMap&);
00445
00446 private:
00447
00448 typedef std::vector<KeyEntryList> KeyGroupTable;
00449
00450
00451 typedef std::map<KeyID, KeyGroupTable> KeyIDMap;
00452
00453
00454 typedef std::vector<const KeyItem*> ModifierKeyItemList;
00455
00456
00457 typedef std::vector<ModifierKeyItemList> ModifierToKeyTable;
00458
00459
00460 typedef std::set<KeyID> KeySet;
00461
00462
00463 typedef std::set<KeyButton> KeyButtonSet;
00464
00465
00466 typedef std::map<CString, KeyID,
00467 CStringUtil::CaselessCmp> CNameToKeyMap;
00468 typedef std::map<CString, KeyModifierMask,
00469 CStringUtil::CaselessCmp> CNameToModifierMap;
00470 typedef std::map<KeyID, CString> CKeyToNameMap;
00471 typedef std::map<KeyModifierMask, CString> CModifierToNameMap;
00472
00473
00474 KeyIDMap m_keyIDMap;
00475 SInt32 m_numGroups;
00476 ModifierToKeyTable m_modifierKeys;
00477
00478
00479 bool m_composeAcrossGroups;
00480
00481
00482 KeyButtonSet m_halfDuplex;
00483 KeySet m_halfDuplexMods;
00484
00485
00486 KeyItem m_modifierKeyItem;
00487
00488
00489 static CNameToKeyMap* s_nameToKeyMap;
00490 static CNameToModifierMap* s_nameToModifierMap;
00491 static CKeyToNameMap* s_keyToNameMap;
00492 static CModifierToNameMap* s_modifierToNameMap;
00493 };
00494
00495 #endif