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
kb.h
Go to the documentation of this file.
1 #ifndef KB_H
2 #define KB_H
3 
4 #include <QObject>
5 #include <QFile>
6 #include <QThread>
7 #include "kbprofile.h"
8 
9 // Class for managing devices
10 
11 class Kb : public QThread
12 {
13  Q_OBJECT
14 public:
15  // USB model and serial number
16  QString usbModel, usbSerial;
17  // Device information
19  bool monochrome;
20 
21  // Keyboard model
22  inline KeyMap::Model model() const { return _model; }
23  bool isKeyboard() const { return KeyMap::isKeyboard(_model); }
24  bool isMouse() const { return KeyMap::isMouse(_model); }
25 
26  // Frame rate (all devices). Also updates the event timer in KbManager.
27  static inline int frameRate() { return _frameRate; }
28  static void frameRate(int newFrameRate);
29  // Layout (all devices)
30  static inline KeyMap::Layout layout() { return _layout; }
31  static void layout(KeyMap::Layout newLayout);
32  // Whether dithering is used (all devices)
33  static inline bool dither() { return _dither; }
34  static void dither(bool newDither);
35  // Macro Delay setting
36  static inline bool macroDelay() { return _delay; }
37  static void macroDelay(bool flag);
38  // OSX: mouse acceleration toggle (all devices)
39  static inline bool mouseAccel() { return _mouseAccel; }
40  static void mouseAccel(bool newAccel);
41  // OSX: scroll speed (-1 = use acceleration)
42  static inline int scrollSpeed() { return _scrollSpeed; }
43  static void scrollSpeed(int newSpeed);
44 
45  // Profile saved to hardware
46  inline KbProfile* hwProfile() { return _hwProfile; }
47  void hwProfile(KbProfile* newHwProfile);
48  // Required hardware modes
50  const static int HWMODE_MAX = 3;
51 
52  // Perform a firmware update
53  void fwUpdate(const QString& path);
54 
55  // Currently-selected profile
57  // Profile list
58  inline const QList<KbProfile*>& profiles() const { return _profiles; }
59  void profiles(const QList<KbProfile*>& newProfiles) { _needsSave = true; _profiles = newProfiles; }
60  void appendProfile(KbProfile* newProfile) { _needsSave = true; _profiles.append(newProfile); }
61  inline int indexOf(KbProfile* profile) { return _profiles.indexOf(profile); }
62  inline KbProfile* find(const QUuid& id) { foreach(KbProfile* profile, _profiles) { if(profile->id().guid == id) return profile; } return 0; }
63 
64  // Currently-selected mode
65  inline KbMode* currentMode() { return _currentMode; }
66  inline KbLight* currentLight() { return !_currentMode ? 0 : _currentMode->light(); }
67  inline KbBind* currentBind() { return !_currentMode ? 0 : _currentMode->bind(); }
68  inline KbPerf* currentPerf() { return !_currentMode ? 0 : _currentMode->perf(); }
69 
70  // Update selection
71  void setCurrentProfile(KbProfile* profile, bool spontaneous = true);
72  void setCurrentMode(KbProfile* profile, KbMode* mode, bool spontaneous = true);
73  inline void setCurrentMode(KbProfile* profile, int index, bool spontaneous = true) { setCurrentMode(profile, profile->modes()[index], spontaneous); }
74  inline void setCurrentMode(KbMode* mode, bool spontaneous = true) { setCurrentMode(_currentProfile, mode, spontaneous); }
75 
76  // Create a new profile/mode. The newly-created object will NOT be inserted into the current profile/mode list.
77  inline KbProfile* newProfile() { return new KbProfile(this, getKeyMap()); }
78  inline KbProfile* newProfile(KbProfile* other) { return new KbProfile(this, getKeyMap(), *other); }
79  inline KbMode* newMode() { return new KbMode(this, getKeyMap()); }
80  inline KbMode* newMode(KbMode* other) { return new KbMode(this, getKeyMap(), *other); }
81 
82  // Load/save stored settings
83  void load();
84  void save();
85  bool needsSave() const;
86 
87  void hwSave();
88 
97  inline int getMacroNumber () { return macroNumber; }
98 
106  inline QString getMacroPath () { return macroPath; }
107 
108  ~Kb();
109 
110 signals:
111  // Layout/model updated
112  void infoUpdated();
113 
114  // Profile/mode updates
115  void profileAdded();
116  void profileRenamed();
117  void modeRenamed();
118 
119  void profileChanged();
120  void modeChanged(bool spontaneous);
121 
122  // FW update status
123  void fwUpdateProgress(int current, int total);
124  void fwUpdateFinished(bool succeeded);
125 
126 public slots:
127  // Send lighting and settings to the driver
128  void frameUpdate();
129 
130  // Auto-save every 15s (if settings have changed, and no other writes are in progress)
131  void autoSave();
132 
133 private slots:
134  // Processes lines read from the notification node
135  void readNotify(QString line);
136 
137  void deleteHw();
138  void deletePrevious();
139 
140 private:
141  // Following methods should only be used by KbManager
142  friend class KbManager;
143 
144  // Creates a keyboard object with the given device path
145  Kb(QObject *parent, const QString& path);
146 
147  inline bool isOpen() const { return cmd.isOpen(); }
148 
156  // Is this the keyboard at the given serial/path?
157  inline bool matches(const QString& path, const QString& serial) { return path.trimmed() == devpath.trimmed() && usbSerial == serial.trimmed().toUpper(); }
158 
159 private:
160  // Following properties shouldn't be used by any other classes
162  void updateLayout();
163 
165  static bool _dither, _mouseAccel;
166 
168  QList<KbProfile*> _profiles;
170 
172 
173  // Indicator light state
175 
176  // CkbSettings path
177  QString prefsPath;
178  quint64 lastAutoSave;
179 
180  // Current firmware update file
181  QString fwUpdPath;
182 
184  // Previously-selected profile and mode
187  // Used to write the profile info when switching
188  void writeProfileHeader();
189 
190  // cmd and notify file handles
191  QFile cmd;
192 
195  // Macro Numer to notify macro definition events
197  // flag if macro delay hast to be switched on
198  static bool _delay;
199 
200  // Needs to be saved?
202 
203  // Whether or not the hardware profile is being loaded
204  // (0 = profile, 1...3 = modes)
206 
207  // Key map for this keyboard
208  KeyMap getKeyMap();
209 
210  // Notification reader, launches as a separate thread and reads from file.
211  // (QFile doesn't have readyRead() so there's no other way to do this asynchronously)
212  void run();
213 };
214 
215 #endif // KB_H
Layout
Definition: keymap.h:70
static KeyMap::Layout layout()
Definition: kb.h:30
void modeRenamed()
Definition: moc_kb.cpp:251
bool isKeyboard() const
Definition: keymap.h:110
static const int HW_I_COUNT
Definition: kbperf.h:107
static bool mouseAccel()
Definition: kb.h:39
KbProfile * newProfile(KbProfile *other)
Definition: kb.h:78
const ModeList & modes() const
Definition: kbprofile.h:42
QUuid guid
Definition: kbmode.h:12
rgb * current
Definition: main.c:46
void setCurrentMode(KbProfile *profile, KbMode *mode, bool spontaneous=true)
Definition: kb.cpp:769
QString prefsPath
Definition: kb.h:177
void infoUpdated()
Definition: moc_kb.cpp:233
int hwModeCount
Definition: kb.h:49
bool matches(const QString &path, const QString &serial)
Definition: kb.h:157
KbBind * bind()
Definition: kbmode.h:61
QString getMacroPath()
getMacroPath returns the macroPath (e.g. /dev/input/ckb1/notify), which we have saved in the construc...
Definition: kb.h:106
KbMode * currentMode()
Definition: kb.h:65
void profileChanged()
Definition: moc_kb.cpp:257
bool isKeyboard() const
Definition: kb.h:23
void load()
Definition: kb.cpp:241
static int frameRate()
Definition: kb.h:27
QList< KbProfile * > _profiles
Definition: kb.h:168
KbMode * newMode(KbMode *other)
Definition: kb.h:80
void updateLayout()
Definition: kb.cpp:186
QString macroPath
Definition: kb.h:155
cmd
Definition: command.h:7
static bool macroDelay()
Definition: kb.h:36
int getMacroNumber()
For usage with macro definions, these two params must only be readable. So there are no setters...
Definition: kb.h:97
void writeProfileHeader()
Definition: kb.cpp:354
bool isMouse() const
Definition: keymap.h:112
KbPerf * currentPerf()
Definition: kb.h:68
QString pollrate
Definition: kb.h:18
KbProfile * currentProfile()
Definition: kb.h:56
KbLight * light()
Definition: kbmode.h:60
bool iState[KbPerf::HW_I_COUNT]
Definition: kb.h:174
KbMode * _currentMode
Definition: kb.h:169
int indexOf(KbProfile *profile)
Definition: kb.h:61
static bool _mouseAccel
Definition: kb.h:165
static int scrollSpeed()
Definition: kb.h:42
QString firmware
Definition: kb.h:18
QString cmdpath
Definition: kb.h:155
static int _frameRate
Definition: kb.h:164
bool _needsSave
Definition: kb.h:201
~Kb()
Definition: kb.cpp:141
bool isMouse() const
Definition: kb.h:24
void setCurrentProfile(KbProfile *profile, bool spontaneous=true)
Definition: kb.cpp:760
KbProfile * prevProfile
Definition: kb.h:185
void frameUpdate()
Definition: kb.cpp:375
UsbId & id()
Definition: kbprofile.h:30
void fwUpdate(const QString &path)
Definition: kb.cpp:365
KbMode * newMode()
Definition: kb.h:79
bool monochrome
Definition: kb.h:19
Definition: kbmode.h:36
static const int HWMODE_MAX
Definition: kb.h:50
KeyMap::Model _model
Definition: kb.h:171
Definition: kbperf.h:15
QFile cmd
Definition: kb.h:191
static bool dither()
Definition: kb.h:33
Definition: kb.h:11
int macroNumber
Definition: kb.h:196
void setCurrentMode(KbMode *mode, bool spontaneous=true)
Definition: kb.h:74
KbBind * currentBind()
Definition: kb.h:67
void hwSave()
Definition: kb.cpp:297
KeyMap::Model model() const
Definition: kb.h:22
Definition: kbbind.h:20
void profileRenamed()
Definition: moc_kb.cpp:245
quint64 lastAutoSave
Definition: kb.h:178
QString usbModel
Definition: kb.h:16
void profiles(const QList< KbProfile * > &newProfiles)
Definition: kb.h:59
void deleteHw()
Definition: kb.cpp:441
QString devpath
pathVars devpath is the device root path (e.g. /dev/device/ckb1), cmdpath leads to the daemon input p...
Definition: kb.h:155
Model
Definition: keymap.h:51
KbProfile * hwProfile()
Definition: kb.h:46
const QList< KbProfile * > & profiles() const
Definition: kb.h:58
static bool _dither
Definition: kb.h:165
Definition: keymap.h:49
void appendProfile(KbProfile *newProfile)
Definition: kb.h:60
void run()
Definition: kb.cpp:446
QString features
Definition: kb.h:18
QString usbSerial
Definition: kb.h:16
static bool _delay
Definition: kb.h:198
static int _scrollSpeed
Definition: kb.h:164
bool needsSave() const
Definition: kb.cpp:344
void profileAdded()
Definition: moc_kb.cpp:239
bool isOpen() const
Definition: kb.h:147
void modeChanged(bool spontaneous)
Definition: moc_kb.cpp:263
bool hwLoading[HWMODE_MAX+1]
Definition: kb.h:205
void fwUpdateProgress(int current, int total)
Definition: moc_kb.cpp:270
static KeyMap::Layout _layout
Definition: kb.h:161
void fwUpdateFinished(bool succeeded)
Definition: moc_kb.cpp:277
QString fwUpdPath
Definition: kb.h:181
void save()
Definition: kb.cpp:273
int notifyNumber
notifyNumber is the trailing number in the device path.
Definition: kb.h:194
KbMode * prevMode
Definition: kb.h:186
QString notifyPath
Definition: kb.h:155
Kb(QObject *parent, const QString &path)
Definition: kb.cpp:19
KbProfile * _hwProfile
Definition: kb.h:183
KbProfile * find(const QUuid &id)
Definition: kb.h:62
KbPerf * perf()
Definition: kbmode.h:62
KeyMap getKeyMap()
Definition: kb.cpp:756
void autoSave()
Definition: kb.cpp:289
void setCurrentMode(KbProfile *profile, int index, bool spontaneous=true)
Definition: kb.h:73
KbLight * currentLight()
Definition: kb.h:66
void deletePrevious()
Definition: kb.cpp:426
KbProfile * _currentProfile
Definition: kb.h:167
void readNotify(QString line)
Definition: kb.cpp:471
KbProfile * newProfile()
Definition: kb.h:77