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
MacroReaderThread Class Reference

The MacroReaderThread class is responsible for reading Macro Key Values. It is created as a separate thread (worker thread) for reading macro commands from an fresh opened notify channel. Standard notify channel for macro definitions is number 2. More...

#include <src/ckb/macroreader.h>

+ Inheritance diagram for MacroReaderThread:
+ Collaboration diagram for MacroReaderThread:

Public Member Functions

 MacroReaderThread (int macNum, QString macPath, QPlainTextEdit *macBox, QPlainTextEdit *macText)
 MacroReaderThread saves the four params to local vars with similar varNames. More...
 
void run () Q_DECL_OVERRIDE
 run is the notification reader main loop. More...
 

Private Slots

void readMacro (QString line)
 readMacro is called for each line received by the worker thread. The method ist called via signal (metaObject) from the worker thread, which reads the keyboard input. Just display the key code in the macroBox Widget without he trailing newline and reposition the cursor in the macro pane. More...
 

Private Attributes

int macroNumber
 macroNumber Filenames of nofity channels have the structure <input-device-path>/ckb1/notify<number> First part is hold in macroPath, the number is hold in macroNumber. macroNumber may range from 0 to 9. More...
 
QString macroPath
 macroPath holds the path for the notify channel More...
 
QPlainTextEdit * macroBox
 macroBox will receive the Macro Key Values sent from the keyboard while defining a new macro. More...
 
QPlainTextEdit * macroText
 macroText is the other textpane used in the UI while typing new macros. That variable is used for setting the focus to that textpane and to set the cursor at EOT. More...
 

Detailed Description

While the worker Thread gets input from the keyboard, the lines are sent via signalling (metaobject) to run a member function in the context of the Qt UI manager.

When the notify channel is closed (that's normally done by pressing "Stop"-Button in the UI), the worker thread closes the channelFile and leaves.

See Also
MacroReaderThread(), ~MacroReaderThread(), readMaco(), run()

Definition at line 22 of file macroreader.h.

Constructor & Destructor Documentation

MacroReaderThread::MacroReaderThread ( int  macNum,
QString  macPath,
QPlainTextEdit *  macBox,
QPlainTextEdit *  macText 
)
inline
Parameters
macNum
macPath
macBox
macText

Definition at line 55 of file macroreader.h.

References macroBox, macroNumber, macroPath, and macroText.

55  {
56  macroNumber = macNum;
57  macroPath = macPath;
58  macroBox = macBox;
59  macroText = macText;
60  }
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
QPlainTextEdit * macroBox
macroBox will receive the Macro Key Values sent from the keyboard while defining a new macro...
Definition: macroreader.h:40
int macroNumber
macroNumber Filenames of nofity channels have the structure <input-device-path>/ckb1/notify<number> F...
Definition: macroreader.h:31

Member Function Documentation

void MacroReaderThread::readMacro ( QString  line)
privateslot

This is used, because the worker thread shouldn't get access to the UI elements (and normally has none, because the pointers macroBox and macroText remain on the stack). That mechanism guarantees, that the UI does not freeze if it happens something magic to the reading function.

Parameters
lineholds the line just got from keyboard

We want to see the keys as they appear in the macroText Widget.

Because it is possible to change the Focus via keyboard, we must set the focus on each call.

Definition at line 27 of file macroreader.cpp.

References macroBox, and macroText.

27  {
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 }
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
QPlainTextEdit * macroBox
macroBox will receive the Macro Key Values sent from the keyboard while defining a new macro...
Definition: macroreader.h:40
void MacroReaderThread::run ( )

MacroReaderThread::run is the standard main function for a thread. Tries to open a file <macroPath><macroNumber> several times (in this case, it should be possible the first time (the code was recycled from kb.cpp).

While the file is open, read lines an signal them via metaObject() to the main thread. When the file is closed by the sender, close it as reader and terminate.

Definition at line 47 of file macroreader.cpp.

References macroBox, macroNumber, macroPath, and macroText.

47  {
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 }
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
QPlainTextEdit * macroBox
macroBox will receive the Macro Key Values sent from the keyboard while defining a new macro...
Definition: macroreader.h:40
int macroNumber
macroNumber Filenames of nofity channels have the structure <input-device-path>/ckb1/notify<number> F...
Definition: macroreader.h:31

Field Documentation

QPlainTextEdit* MacroReaderThread::macroBox
private

Definition at line 40 of file macroreader.h.

Referenced by MacroReaderThread(), readMacro(), and run().

int MacroReaderThread::macroNumber
private

Definition at line 31 of file macroreader.h.

Referenced by MacroReaderThread(), and run().

QString MacroReaderThread::macroPath
private
See Also
macroNumber

Definition at line 36 of file macroreader.h.

Referenced by MacroReaderThread(), and run().

QPlainTextEdit* MacroReaderThread::macroText
private

Definition at line 45 of file macroreader.h.

Referenced by MacroReaderThread(), readMacro(), and run().


The documentation for this class was generated from the following files: