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
macroreader.cpp
Go to the documentation of this file.
1 #include <qdebug.h>
2 #include "macroreader.h"
3 #include <sys/time.h>
4 
8 
10  qDebug() << "Calling MacroReader without params is not allowed.";
11 }
12 
13 MacroReader::MacroReader(int macroNumber, QString macroPath, QPlainTextEdit* macBox, QPlainTextEdit* macText) {
14  startWorkInAThread(macroNumber, macroPath, macBox, macText);
15 }
17 
18 void MacroReader::startWorkInAThread(int macroNumber, QString macroPath, QPlainTextEdit* macBox, QPlainTextEdit* macText) {
19  macroReaderThread = new MacroReaderThread(macroNumber, macroPath, macBox, macText);
20  connect(macroReaderThread, &MacroReaderThread::finished, macroReaderThread, &QObject::deleteLater);
21  macroReaderThread->start();
22 }
23 
27 void MacroReaderThread::readMacro(QString line) {
32  macroText->setFocus();
33  QTextCursor c = macroText->textCursor();
34  c.setPosition(macroText->toPlainText().length());
35  macroText->setTextCursor(c);
36  macroBox->appendPlainText(line.left(line.size()-1));
37 }
38 
48  qDebug() << "MacroReader::run() started with" << macroNumber << "and" << macroPath << "and" << macroBox << "and" << macroText;
49 
50  QFile macroFile(macroPath);
51  // Wait a small amount of time for the node to open (100ms) (code reused from kb.cpp)
52  QThread::usleep(100000);
53  if(!macroFile.open(QIODevice::ReadOnly)){
54  // If it's still not open, try again before giving up (1s at a time, 10s total)
55  QThread::usleep(900000);
56  for(int i = 1; i < 10; i++){
57  if(macroFile.open(QIODevice::ReadOnly))
58  break;
59  QThread::sleep(1);
60  }
61  if(!macroFile.isOpen()) {
62  qDebug() << QString("unable to open macroFile (%1)").arg(macroPath);
63  return;
64  }
65  }
66  // Read data from notification node macroPath
67  // Count time between lines read from the interface
68  QByteArray line;
69  timeval t;
70  gettimeofday(&t, NULL);
71  double tstart = t.tv_sec+(t.tv_usec/1000000.0);
72  bool firstline = true;
73 
74  while(macroFile.isOpen() && (line = macroFile.readLine()).length() > 0){
75  QString text = QString::fromUtf8(line);
76  gettimeofday(&t, NULL);
77  double tnow = t.tv_sec+(t.tv_usec/1000000.0);
78 
79  // in the first line, there is only a delay "before start". Don't use it.
80  if (!firstline) {
81  text.prepend ("\n");
82  text.prepend (QString::number ((tnow - tstart) * 1000000.0, 'f', 0));
83  text.prepend ("=");
84  } else firstline = false;
85  tstart = tnow;
86 
87  metaObject()->invokeMethod(this, "readMacro", Qt::QueuedConnection, Q_ARG(QString, text));
88  }
89  qDebug() << "MacroReader::run() ends.";
90  macroFile.close();
91  QThread::exit ();
92 }
MacroReader()
MacroReader Calling MacroReader without params is not allowed.
Definition: macroreader.cpp:9
QPlainTextEdit * macroText
macroText is the other textpane used in the UI while typing new macros. That variable is used for set...
Definition: macroreader.h:45
QString macroPath
macroPath holds the path for the notify channel
Definition: macroreader.h:36
void run() Q_DECL_OVERRIDE
run is the notification reader main loop.
Definition: macroreader.cpp:47
The MacroReaderThread class is responsible for reading Macro Key Values. It is created as a separate ...
Definition: macroreader.h:22
QPlainTextEdit * macroBox
macroBox will receive the Macro Key Values sent from the keyboard while defining a new macro...
Definition: macroreader.h:40
void startWorkInAThread(int macroNumber, QString macroPath, QPlainTextEdit *macBox, QPlainTextEdit *macText)
startWorkInAThread This member function creates the new thread object and starts it with the four par...
Definition: macroreader.cpp:18
int macroNumber
macroNumber Filenames of nofity channels have the structure <input-device-path>/ckb1/notify<number> F...
Definition: macroreader.h:31
MacroReaderThread * macroReaderThread
Definition: macroreader.h:123
void readMacro(QString line)
readMacro is called for each line received by the worker thread. The method ist called via signal (me...
Definition: macroreader.cpp:27