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
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(1000);
32  connect(_scanTimer, SIGNAL(timeout()), this, SLOT(scanKeyboards()));
33 
34  // Scan for keyboards immediately so they show up as soon as the GUI appears.
35  QTimer::singleShot(0,this,SLOT(scanKeyboards()));
36 }
37 
38 void KbManager::fps(int framerate){
39  QTimer* timer = eventTimer();
40  if(!timer)
41  return;
42  if(timer->isActive())
43  timer->setInterval(1000 / framerate);
44  else
45  timer->start(1000 / framerate);
46 }
47 
48 float KbManager::parseVersionString(QString version){
49  // Remove extraneous info (anything after a +, anything non-numeric)
50  QStringList dots = version.replace(QRegExp("\\+.+"), "").replace(QRegExp("[^\\d\\.]"), "").split(".");
51  float base = 1.f;
52  float res = 0.f;
53  // A number like "1.2.3" will result in 1.0203
54  // 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.
55  foreach(const QString& dot, dots){
56  res += dot.toFloat() * base;
57  base /= 100.f;
58  }
59  return res;
60 }
61 
63  QString rootdev = devpath.arg(0);
64  QFile connected(rootdev + "/connected");
65  if(!connected.open(QIODevice::ReadOnly)){
66  // No root controller - remove all keyboards
67  foreach(Kb* kb, _devices){
68  emit kbDisconnected(kb);
69  kb->save();
70  delete kb;
71  }
72  _devices.clear();
75  emit versionUpdated();
76  }
77  return;
78  }
79  // Check daemon version
80  QFile version(rootdev + "/version");
81  QString vString;
82  if(version.open(QIODevice::ReadOnly)){
83  vString = QString::fromUtf8(version.readLine()).trimmed();
84  version.close();
85  } else
86  vString = DAEMON_UNAVAILABLE_STR;
87  if(_daemonVersion != vString){
88  _daemonVersion = vString;
89  emit versionUpdated();
90  }
91 
92  // Scan connected devices
93  QList<QStringList> lines;
94  while(1){
95  QString line = connected.readLine().trimmed();
96  if(line.isEmpty())
97  break;
98  QStringList components = line.split(" ");
99  if(components.length() < 2) // "<path> <serial> <name>" (we're only interested in the first two)
100  continue;
101  lines.append(components);
102  }
103  connected.close();
104 
105  // Remove any active devices not in the list
106  QMutableSetIterator<Kb*> i(_devices);
107  while(i.hasNext()){
108  Kb* kb = i.next();
109  bool matched = false;
110  foreach(const QStringList& line, lines){
111  if(kb->matches(line[0], line[1])){
112  matched = true;
113  break;
114  }
115  }
116  if(matched)
117  continue;
118  // Device not found, remove
119  i.remove();
120  emit kbDisconnected(kb);
121  kb->save();
122  delete kb;
123  }
124 
125  // Add any new devices found in the list
126  foreach(const QStringList& line, lines){
127  bool matched = false;
128  foreach(Kb* kb, _devices){
129  if(kb->matches(line[0], line[1])){
130  matched = true;
131  break;
132  }
133  }
134  if(matched)
135  continue;
136  // Device not found, create new
137  Kb* kb = new Kb(this, line[0]);
138  if(!kb->isOpen()){
139  delete kb;
140  continue;
141  }
142  _devices.insert(kb);
143  // Load preferences and send signal
144  emit kbConnected(kb);
145  kb->load();
146  connect(_eventTimer, SIGNAL(timeout()), kb, SLOT(frameUpdate()));
147  connect(_scanTimer, SIGNAL(timeout()), kb, SLOT(autoSave()));
148  }
149 }
150 
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:48
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:62
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:38