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
kbmanager.cpp
Go to the documentation of this file.
1 #include "kbmanager.h"
2 
3 #ifndef Q_OS_MACX
4 QString devpath = "/dev/input/ckb%1";
5 #else
6 QString devpath = "/var/run/ckb%1";
7 #endif
8 
11 
12 void KbManager::init(QString guiVersion){
13  _guiVersion = guiVersion;
14  if(_kbManager)
15  return;
16  _kbManager = new KbManager();
17 }
18 
20  if(!_kbManager)
21  return;
22  delete _kbManager;
23  _kbManager = 0;
24 }
25 
27  // Set up the timers
28  _eventTimer = new QTimer(this);
29  _eventTimer->setTimerType(Qt::PreciseTimer);
30  _scanTimer = new QTimer(this);
31  _scanTimer->start(100);
32  connect(_scanTimer, SIGNAL(timeout()), this, SLOT(scanKeyboards()));
33 }
34 
35 void KbManager::fps(int framerate){
36  QTimer* timer = eventTimer();
37  if(!timer)
38  return;
39  if(timer->isActive())
40  timer->setInterval(1000 / framerate);
41  else
42  timer->start(1000 / framerate);
43 }
44 
45 float KbManager::parseVersionString(QString version){
46  // Remove extraneous info (anything after a +, anything non-numeric)
47  QStringList dots = version.replace(QRegExp("\\+.+"), "").replace(QRegExp("[^\\d\\.]"), "").split(".");
48  float base = 1.f;
49  float res = 0.f;
50  // A number like "1.2.3" will result in 1.0203
51  // NB: will fail if a point version goes over 99 or if using more than two dots. floats can only reliably encode 7 decimal digits.
52  foreach(const QString& dot, dots){
53  res += dot.toFloat() * base;
54  base /= 100.f;
55  }
56  return res;
57 }
58 
60  QString rootdev = devpath.arg(0);
61  QFile connected(rootdev + "/connected");
62  if(!connected.open(QIODevice::ReadOnly)){
63  // No root controller - remove all keyboards
64  foreach(Kb* kb, _devices){
65  emit kbDisconnected(kb);
66  kb->save();
67  delete kb;
68  }
69  _devices.clear();
72  emit versionUpdated();
73  }
74  return;
75  }
76  // Check daemon version
77  QFile version(rootdev + "/version");
78  QString vString;
79  if(version.open(QIODevice::ReadOnly)){
80  vString = QString::fromUtf8(version.readLine()).trimmed();
81  version.close();
82  } else
83  vString = DAEMON_UNAVAILABLE_STR;
84  if(_daemonVersion != vString){
85  _daemonVersion = vString;
86  emit versionUpdated();
87  }
88 
89  // Scan connected devices
90  QList<QStringList> lines;
91  while(1){
92  QString line = connected.readLine().trimmed();
93  if(line.isEmpty())
94  break;
95  QStringList components = line.split(" ");
96  if(components.length() < 2) // "<path> <serial> <name>" (we're only interested in the first two)
97  continue;
98  lines.append(components);
99  }
100  connected.close();
101 
102  // Remove any active devices not in the list
103  QMutableSetIterator<Kb*> i(_devices);
104  while(i.hasNext()){
105  Kb* kb = i.next();
106  bool matched = false;
107  foreach(const QStringList& line, lines){
108  if(kb->matches(line[0], line[1])){
109  matched = true;
110  break;
111  }
112  }
113  if(matched)
114  continue;
115  // Device not found, remove
116  i.remove();
117  emit kbDisconnected(kb);
118  kb->save();
119  delete kb;
120  }
121 
122  // Add any new devices found in the list
123  foreach(const QStringList& line, lines){
124  bool matched = false;
125  foreach(Kb* kb, _devices){
126  if(kb->matches(line[0], line[1])){
127  matched = true;
128  break;
129  }
130  }
131  if(matched)
132  continue;
133  // Device not found, create new
134  Kb* kb = new Kb(this, line[0]);
135  if(!kb->isOpen()){
136  delete kb;
137  continue;
138  }
139  _devices.insert(kb);
140  // Load preferences and send signal
141  emit kbConnected(kb);
142  kb->load();
143  connect(_eventTimer, SIGNAL(timeout()), kb, SLOT(frameUpdate()));
144  connect(_scanTimer, SIGNAL(timeout()), kb, SLOT(autoSave()));
145  }
146 }
147 
static void stop()
Definition: kbmanager.cpp:19
bool matches(const QString &path, const QString &serial)
Definition: kb.h:157
void load()
Definition: kb.cpp:241
static QTimer * eventTimer()
Definition: kbmanager.h:38
static QString _daemonVersion
Definition: kbmanager.h:59
static float parseVersionString(QString version)
Definition: kbmanager.cpp:45
void versionUpdated()
QTimer * _eventTimer
Definition: kbmanager.h:64
QString devpath
Definition: kbmanager.cpp:4
QTimer * _scanTimer
Definition: kbmanager.h:64
void kbDisconnected(Kb *device)
QSet< Kb * > _devices
Definition: kbmanager.h:63
void kbConnected(Kb *device)
Definition: kb.h:11
static void init(QString guiVersion)
Definition: kbmanager.cpp:12
#define DAEMON_UNAVAILABLE_STR
Definition: kbmanager.h:12
KbManager(QObject *parent=0)
Definition: kbmanager.cpp:26
bool isOpen() const
Definition: kb.h:147
void scanKeyboards()
Definition: kbmanager.cpp:59
static QString _guiVersion
Definition: kbmanager.h:59
void save()
Definition: kb.cpp:273
static KbManager * _kbManager
Definition: kbmanager.h:58
static void fps(int framerate)
Definition: kbmanager.cpp:35