ckb-next  beta-v0.2.8 at branch testing
ckb-next driver for corsair devices
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
keymap.h
Go to the documentation of this file.
1 #ifndef KEYMAP_H
2 #define KEYMAP_H
3 
4 #include <QColor>
5 #include <QHash>
6 
7 // Key information
8 struct Key {
9  // Name stored in settings (this is here due to a bad design decision - it will be removed later)
10  const char* _storageName;
11  inline const char* storageName() const { return _storageName ? _storageName : name; }
12  // Key name
13  const char* _friendlyName;
14  const char* name;
15  // LED position, measured roughly in 16th inches. Most keys are 3/4" apart (12x12).
16  short x, y;
17  short width, height;
18  // Whether or not the key has an LED, scancode, or both
19  bool hasLed;
20  bool hasScan;
21 
22  // Friendly name with optional OS-based labels
23  inline QString friendlyName(bool os = true) const {
24  if(os){
25 #ifdef Q_OS_MACX
26  if(!strcmp(name, "lwin")) return "Left Cmd";
27  if(!strcmp(name, "rwin")) return "Right Cmd";
28  if(!strcmp(name, "lalt")) return "Left Option";
29  if(!strcmp(name, "ralt")) return "Right Option";
30  if(!strcmp(name, "prtscn")) return "F13";
31  if(!strcmp(name, "scroll")) return "F14";
32  if(!strcmp(name, "pause")) return "F15";
33  if(!strcmp(name, "ins")) return "Help";
34  if(!strcmp(name, "numlock")) return "Clear";
35 #elif defined(Q_OS_LINUX)
36  if(!strcmp(name, "lwin")) return "Left Super";
37  if(!strcmp(name, "rwin")) return "Right Super";
38 #endif
39  }
40  return _friendlyName ? _friendlyName : QString(name).toUpper();
41  }
42 
43  // Convert to bool -> key exists?
44  inline operator bool () const { return name != 0; }
45  inline bool operator !() const { return !(bool)*this; }
46 };
47 
48 // Key layout/device info class
49 class KeyMap {
50 public:
51  enum Model {
52  NO_MODEL = -1,
53  // Keyboard models
54  K65,
55  K70,
56  K95,
58  // Mouse models
59  M65,
63  };
64  // Key layouts (ordered alphabetically by name)
65  enum Layout {
66  NO_LAYOUT = -1,
67  DK, // Danish
68  EU, // English (EU)
70  GB, // English (UK)
72  US, // English (US)
74  FR, // French
75  DE, // German
76  IT, // Italian
77  NO, // Norwegian
78  PL, // Polish (identical to US)
79  MX, // Spanish (Mexico/Latin America)
80  ES, // Spanish (Spain)
81  SE, // Swedish
83  };
84  // Human-readable names of each layout
85  static QStringList layoutNames();
86  // ISO (105-key) or ANSI (104-key)?
87  inline static bool isISO(Layout layout) { return layout != US && layout != US_DVORAK && layout != PL; }
88  inline bool isISO() const { return isISO(keyLayout); }
89  // Auto-detects layout from system locale
90  static Layout locale();
91 
92  // Keyboard or mouse?
93  inline static bool isKeyboard(Model model) { return !isMouse(model) && model != NO_MODEL; }
94  inline bool isKeyboard() const { return isKeyboard(keyModel); }
95  inline static bool isMouse(Model model) { return model == M65 || model == SABRE || model == SCIMITAR; }
96  inline bool isMouse() const { return isMouse(keyModel); }
97 
98  // Creates a blank key map
99  KeyMap();
100  // Creates a standard key map
101  KeyMap(Model _keyModel, Layout _keyLayout);
102  static KeyMap fromName(const QString& name);
103  // Gets a layout by name or name by layout
104  static Layout getLayout(const QString& name);
105  static QString getLayout(Layout layout);
106  inline QString strLayout() const { return getLayout(keyLayout); }
107  // Gets a model by name or name by model
108  static Model getModel(const QString& name);
109  static QString getModel(Model model);
110  inline QString strModel() const { return getModel(keyModel); }
111 
112  // Keyboard model and layout
113  inline Model model() const { return keyModel; }
114  inline Layout layout() const { return keyLayout; }
115  inline QString name() const { return (strModel() + " " + strLayout()).toUpper(); }
116 
117  // Number of keys in the keymap
118  inline uint count() const { return _keys.count(); }
119  // Keyboard total width
120  inline uint width() const { return keyWidth; }
121  // Keyboard total height
122  inline uint height() const { return keyHeight; }
123 
124  // Keys by name
125  inline Key key(const QString& name) const { Key empty = {0,0,0,0,0,0,0,0,0}; return _keys.value(name, empty); }
126  inline Key operator[](const QString& name) const { return key(name); }
127  inline bool contains(const QString& name) const { return _keys.contains(name); }
128  // List all key names/values
129  inline const QHash<QString, Key>& map() const { return _keys; }
130  inline operator const QHash<QString, Key>& () const { return _keys; }
131  QStringList keys() const { return _keys.keys(); }
132  QList<Key> positions() const { return _keys.values(); }
133  // Key name to/from storage name. Returns the given name if not found.
134  inline QString toStorage(const QString& name) { const char* storage = key(name).storageName(); if(!storage) return name; return storage; }
135  inline QString fromStorage(const QString& storage) { QHashIterator<QString, Key> i(*this); while(i.hasNext()) { i.next(); const char* s = i.value().storageName(); if(s == storage) return i.value().name; } return storage; }
136 
137  // Keys by position (top to bottom, left to right)
138  QStringList byPosition() const;
139 
140  // Friendly key name on any device
141  static QString friendlyName(const QString& key, Layout layout = US);
142 
143 private:
144  static int modelWidth(Model model);
145  static int modelHeight(Model model);
146 
147  QHash<QString, Key> _keys;
151 };
152 
153 #endif // KEYMAP_H
Layout
Definition: keymap.h:65
bool isKeyboard() const
Definition: keymap.h:94
KeyMap()
Definition: keymap.cpp:583
uint width() const
Definition: keymap.h:120
short keyHeight
Definition: keymap.h:148
QStringList byPosition() const
Definition: keymap.cpp:588
static QString friendlyName(const QString &key, Layout layout=US)
Definition: keymap.cpp:611
static bool isMouse(Model model)
Definition: keymap.h:95
short keyWidth
Definition: keymap.h:148
QString friendlyName(bool os=true) const
Definition: keymap.h:23
bool isMouse() const
Definition: keymap.h:96
Layout keyLayout
Definition: keymap.h:150
static QStringList layoutNames()
Definition: keymap.cpp:479
short width
Definition: keymap.h:17
QHash< QString, Key > _keys
Definition: keymap.h:147
bool hasLed
Definition: keymap.h:19
static Model getModel(const QString &name)
Definition: keymap.cpp:495
Key key(const QString &name) const
Definition: keymap.h:125
short height
Definition: keymap.h:17
Model keyModel
Definition: keymap.h:149
bool contains(const QString &name) const
Definition: keymap.h:127
QString fromStorage(const QString &storage)
Definition: keymap.h:135
Definition: keymap.h:49
Key operator[](const QString &name) const
Definition: keymap.h:126
const char * _storageName
Definition: keymap.h:10
static bool isKeyboard(Model model)
Definition: keymap.h:93
QString strLayout() const
Definition: keymap.h:106
static Layout getLayout(const QString &name)
Definition: keymap.cpp:407
const QHash< QString, Key > & map() const
Definition: keymap.h:129
static bool isISO(Layout layout)
Definition: keymap.h:87
short x
Definition: keymap.h:16
Layout layout() const
Definition: keymap.h:114
bool isISO() const
Definition: keymap.h:88
bool hasScan
Definition: keymap.h:20
const char * name
Definition: keymap.h:14
const char * storageName() const
Definition: keymap.h:11
QString strModel() const
Definition: keymap.h:110
Model
Definition: keymap.h:51
bool operator!() const
Definition: keymap.h:45
static int modelWidth(Model model)
Definition: keymap.cpp:542
Definition: keymap.h:8
QStringList keys() const
Definition: keymap.h:131
Definition: keymap.h:49
QString name() const
Definition: keymap.h:115
QString toStorage(const QString &name)
Definition: keymap.h:134
static Layout locale()
Definition: keymap.cpp:375
Model model() const
Definition: keymap.h:113
static KeyMap fromName(const QString &name)
Definition: keymap.cpp:535
static int modelHeight(Model model)
Definition: keymap.cpp:561
const char * _friendlyName
Definition: keymap.h:13
QList< Key > positions() const
Definition: keymap.h:132
uint height() const
Definition: keymap.h:122
uint count() const
Definition: keymap.h:118
short y
Definition: keymap.h:16