1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- // mythread.cpp
- #include "mythread.h"
- #include "QProcess"
- #include "QtGlobal"
- #ifdef Q_OS_LINUX
- // linux
- #define CMD_LEFT "xdotool key Left"
- #define CMD_RIGHT "xdotool key Right"
- #define CMD_ESC "xdotool key Escape"
- #define CMD_F5 "xdotool key F5"
- #endif
- #ifdef Q_OS_WIN32
- // win
- #define CMD_LEFT "KeyUtil Left"
- #define CMD_RIGHT "KeyUtil Right"
- #define CMD_ESC "KeyUtil Esc"
- #define CMD_F5 "KeyUtil F5"
- #endif
- MyThread::MyThread(long ID, QObject *parent) :
- QThread(parent)
- {
- this->socketDescriptor = ID;
- }
- void MyThread::run()
- {
- // thread starts here
- qDebug() << socketDescriptor << " Starting thread";
- socket = new QTcpSocket();
- if(!socket->setSocketDescriptor(this->socketDescriptor))
- {
- emit error(socket->error());
- return;
- }
- connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()),Qt::DirectConnection);
- connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()),Qt::DirectConnection);
- qDebug() << socketDescriptor << " Client connected";
- // make this thread a loop
- exec();
- }
- void MyThread::readyRead()
- {
- QByteArray Data = socket->readAll();
- QString d =QString::fromUtf8(Data);
- qDebug() << socketDescriptor << " Data in: " << d;
- QProcess p(0);
- if(!d.trimmed().compare("Left")){
- //receive left
- qDebug()<<socketDescriptor<<"deal left";
- p.start(CMD_LEFT);
- p.waitForFinished();
- }else if(!d.trimmed().compare("Right")){
- //receive Right
- qDebug()<<socketDescriptor<<"deal Right";
- p.start(CMD_RIGHT);
- p.waitForFinished();
- }else if(!d.trimmed().compare("Escape")){
- //receive Escape
- qDebug()<<socketDescriptor<<"deal Escape";
- p.start(CMD_ESC);
- p.waitForFinished();
- }else if(!d.trimmed().compare("F5")){
- //receive F5
- qDebug()<<socketDescriptor<<"deal F5";
- p.start(CMD_F5);
- p.waitForFinished();
- }
- socket->write(Data);
- }
- void MyThread::disconnected()
- {
- qDebug() << socketDescriptor << " Disconnected";
- socket->deleteLater();
- exit(0);
- }
|