ckb-next  v0.2.8 at branch master
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  K63,
55  K65,
56  K68,
57  K70,
58  K95,
61  // Mouse models
62  M65,
68  };
69  // Key layouts (ordered alphabetically by name)
70  enum Layout {
71  NO_LAYOUT = -1,
72  DK, // Danish
73  EU, // English (EU)
75  GB, // English (UK)
77  US, // English (US)
79  FR, // French
80  DE, // German
81  IT, // Italian
82  JP, // Japanese
83  NO, // Norwegian
84  PL, // Polish (identical to US)
85  PT_BR, // Portuguese (Brazil)
86  MX, // Spanish (Mexico/Latin America)
87  ES, // Spanish (Spain)
88  SE, // Swedish
90  };
91  // Human-readable names of each layout
92  static QStringList layoutNames();
93  // ISO (105-key) or ANSI (104-key)?
94  inline static bool isISO(Layout layout) { return layout != US && layout != US_DVORAK && layout != PL; }
95  inline bool isISO() const { return isISO(keyLayout); }
96 
97  // JP (106-key)?
98  inline static bool isJP(Layout layout) { return layout == JP; }
99  inline bool isJP() const { return isJP(keyLayout); }
100 
101  // PT_BR?
102  inline static bool isPTBR(Layout layout) { return layout == PT_BR; }
103  inline bool isPTBR() const { return isPTBR(keyLayout); }
104 
105  // Auto-detects layout from system locale
106  static Layout locale();
107 
108  // Keyboard or mouse?
109  inline static bool isKeyboard(Model model) { return !isMouse(model) && model != NO_MODEL; }
110  inline bool isKeyboard() const { return isKeyboard(keyModel); }
111  inline static bool isMouse(Model model) { return model == M65 || model == SABRE || model == SCIMITAR || model == HARPOON || model == GLAIVE; }
112  inline bool isMouse() const { return isMouse(keyModel); }
113 
114  // Creates a blank key map
115  KeyMap();
116  // Creates a standard key map
117  KeyMap(Model _keyModel, Layout _keyLayout);
118  static KeyMap fromName(const QString& name);
119  // Gets a layout by name or name by layout
120  static Layout getLayout(const QString& name);
121  static QString getLayout(Layout layout);
122  inline QString strLayout() const { return getLayout(keyLayout); }
123  // Gets a model by name or name by model
124  static Model getModel(const QString& name);
125  static QString getModel(Model model);
126  inline QString strModel() const { return getModel(keyModel); }
127 
128  // Keyboard model and layout
129  inline Model model() const { return keyModel; }
130  inline Layout layout() const { return keyLayout; }
131  inline QString name() const { return (strModel() + " " + strLayout()).toUpper(); }
132 
133  // Number of keys in the keymap
134  inline uint count() const { return _keys.count(); }
135  // Keyboard total width
136  inline uint width() const { return keyWidth; }
137  // Keyboard total height
138  inline uint height() const { return keyHeight; }
139 
140  // Keys by name
141  inline Key key(const QString& name) const { Key empty = {0,0,0,0,0,0,0,0,0}; return _keys.value(name, empty); }
142  inline Key operator[](const QString& name) const { return key(name); }
143  inline bool contains(const QString& name) const { return _keys.contains(name); }
144  // List all key names/values
145  inline const QHash<QString, Key>& map() const { return _keys; }
146  inline operator const QHash<QString, Key>& () const { return _keys; }
147  QStringList keys() const { return _keys.keys(); }
148  QList<Key> positions() const { return _keys.values(); }
149  // Key name to/from storage name. Returns the given name if not found.
150  inline QString toStorage(const QString& name) { const char* storage = key(name).storageName(); if(!storage) return name; return storage; }
151  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; }
152 
153  // Keys by position (top to bottom, left to right)
154  QStringList byPosition() const;
155 
156  // Friendly key name on any device
157  static QString friendlyName(const QString& key, Layout layout = US);
158 
159 private:
160  static int modelWidth(Model model);
161  static int modelHeight(Model model);
162 
163  QHash<QString, Key> _keys;
167 };
168 
169 #endif // KEYMAP_H
Layout
Definition: keymap.h:70
bool isKeyboard() const
Definition: keymap.h:110
static bool isJP(Layout layout)
Definition: keymap.h:98
KeyMap()
Definition: keymap.cpp:897
uint width() const
Definition: keymap.h:136
short keyHeight
Definition: keymap.h:164
QStringList byPosition() const
Definition: keymap.cpp:902
static QString friendlyName(const QString &key, Layout layout=US)
Definition: keymap.cpp:925
static bool isMouse(Model model)
Definition: keymap.h:111
short keyWidth
Definition: keymap.h:164
QString friendlyName(bool os=true) const
Definition: keymap.h:23
bool isMouse() const
Definition: keymap.h:112
Layout keyLayout
Definition: keymap.h:166
static QStringList layoutNames()
Definition: keymap.cpp:757
short width
Definition: keymap.h:17
QHash< QString, Key > _keys
Definition: keymap.h:163
bool hasLed
Definition: keymap.h:19
static Model getModel(const QString &name)
Definition: keymap.cpp:775
Key key(const QString &name) const
Definition: keymap.h:141
short height
Definition: keymap.h:17
Model keyModel
Definition: keymap.h:165
bool contains(const QString &name) const
Definition: keymap.h:143
QString fromStorage(const QString &storage)
Definition: keymap.h:151
Definition: keymap.h:49
Key operator[](const QString &name) const
Definition: keymap.h:142
const char * _storageName
Definition: keymap.h:10
static bool isKeyboard(Model model)
Definition: keymap.h:109
QString strLayout() const
Definition: keymap.h:122
static Layout getLayout(const QString &name)
Definition: keymap.cpp:677
bool isJP() const
Definition: keymap.h:99
const QHash< QString, Key > & map() const
Definition: keymap.h:145
static bool isISO(Layout layout)
Definition: keymap.h:94
short x
Definition: keymap.h:16
Layout layout() const
Definition: keymap.h:130
bool isISO() const
Definition: keymap.h:95
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:126
Model
Definition: keymap.h:51
bool operator!() const
Definition: keymap.h:45
static bool isPTBR(Layout layout)
Definition: keymap.h:102
static int modelWidth(Model model)
Definition: keymap.cpp:842
Definition: keymap.h:8
QStringList keys() const
Definition: keymap.h:147
Definition: keymap.h:49
QString name() const
Definition: keymap.h:131
QString toStorage(const QString &name)
Definition: keymap.h:150
static Layout locale()
Definition: keymap.cpp:641
Model model() const
Definition: keymap.h:129
static KeyMap fromName(const QString &name)
Definition: keymap.cpp:835
bool isPTBR() const
Definition: keymap.h:103
static int modelHeight(Model model)
Definition: keymap.cpp:869
const char * _friendlyName
Definition: keymap.h:13
QList< Key > positions() const
Definition: keymap.h:148
uint height() const
Definition: keymap.h:138
uint count() const
Definition: keymap.h:134
short y
Definition: keymap.h:16