Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
krogank9 committed Aug 12, 2017
1 parent 0a55558 commit 39ed495
Show file tree
Hide file tree
Showing 9 changed files with 411 additions and 45 deletions.
271 changes: 271 additions & 0 deletions WifiMouseServer.pro.user

Large diffs are not rendered by default.

32 changes: 27 additions & 5 deletions fakeinput-linux.cpp
@@ -1,3 +1,5 @@
#include <QString>
#include <QDebug>
extern "C" {
#include <xdo.h>
}
Expand All @@ -16,6 +18,7 @@ void initFakeInput() {
}

void freeFakeInput() {
stopZoom();
xdo_free(xdoInstance);
}

Expand All @@ -24,17 +27,19 @@ void typeChar(wchar_t c) {
keyTap("Return");
}
else if(c >= 0x00A0) { // 0x00A0 is the end of ASCII characters
char unicodeCharNumber[10];
sprintf(unicodeCharNumber, "U%04X", c);
QString unicodeHex;
unicodeHex.setNum(c, 16);
unicodeHex = "U"+unicodeHex.toUpper();

// Note: this starts glitching out if you set the pause too short.
// Maybe because for unicode characters, the keys must be remapped.
// 95ms seems to be sufficient.
xdo_send_keysequence_window(xdoInstance, CURRENTWINDOW, unicodeCharNumber, 95000);
xdo_send_keysequence_window(xdoInstance, CURRENTWINDOW, unicodeHex.toLocal8Bit().data(), 95000);
}
else {
else {
char str[] = {c, '\0'};
xdo_enter_text_window(xdoInstance, CURRENTWINDOW, str, 12000);
}
}
}


Expand Down Expand Up @@ -95,4 +100,21 @@ void mouseScroll(int amount) {
}
}

bool zooming = false;

void stopZoom() {
if(!zooming)
return;
keyUp("Alt");
zooming = false;
}

void zoom(int amount) {
if(!zooming) {
zooming = true;
keyDown("Alt");
}
mouseScroll(amount);
}

}
1 change: 0 additions & 1 deletion fakeinput-mac.cpp
Expand Up @@ -62,7 +62,6 @@ void typeUniChar(wchar_t c) {
CGKeyCode lastKeyDown = 0;
qint64 lastKeyTime = 0;
bool lastKeyStillDown = false;

void keyRepeatCallback() {
static int i = 1;
qInfo() << "Test " << i++ << "\n";
Expand Down
2 changes: 2 additions & 0 deletions fakeinput.h
Expand Up @@ -10,5 +10,7 @@ void mouseMove(int addX, int addY);
void mouseDown(int button);
void mouseUp(int button);
void mouseScroll(int amount);
void zoom(int amount);
void stopZoom();

}
Binary file modified images/icon64.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
136 changes: 102 additions & 34 deletions networkthread.cpp
Expand Up @@ -4,46 +4,102 @@
#include <QDebug>
#include <QDateTime>
#include <QAbstractSocket>
#include <QHostInfo>

NetworkThread::NetworkThread()
{
FakeInput::initFakeInput();
QString serverVersion = "1";
QTcpSocket *socket;

// todo: change protocol, first byte = # of bytes sent

QString bytesToString(QByteArray bytes) {
int len = 0;
while(len < bytes.length() && bytes.at(len) != 0)
len++;
bytes.resize(len);

return QString(bytes);
}

NetworkThread::~NetworkThread()
{
FakeInput::freeFakeInput();
bool writeData(QByteArray data) {
data.resize(1024);
socket->write(data);
return socket->waitForBytesWritten(60);
}

bool writeString(QString str) {
return writeData(str.toUtf8());
}

qint64 readData(QByteArray *data) {
int bytesLeft = data->length();
int totalBytesRead = 0;
while(bytesLeft > 0) {
qint64 bytesRead = socket->read(data->data() + totalBytesRead, bytesLeft);

if(bytesRead <= 0)
break;
else {
totalBytesRead += bytesRead;
bytesLeft -= bytesRead;
}

if(bytesLeft > 0 && socket->waitForReadyRead(50) == false)
break;
}
return totalBytesRead;
}

QString readString() {
QByteArray bytes(1024, 0);
if(readData(&bytes) == 1024)
return bytesToString(bytes);
else
return QString("");
}

void NetworkThread::run()
{
FakeInput::initFakeInput();

QTcpServer tcpServer;

while(!tcpServer.isListening() && !tcpServer.listen(QHostAddress::Any, 9798)) {
qInfo() << "Unable to start server on port 9798. Retrying...\n";
this->sleep(1);
}

qInfo() << "Server started, listening for connection...";

while(true) {
updateClientIp("Not connected");

if(!tcpServer.isListening() && !tcpServer.listen(QHostAddress::Any, 9798)) {
qInfo() << "Unable to start server on port 9798. Retrying...\n";
this->sleep(1);
tcpServer.waitForNewConnection(1000);
socket = tcpServer.nextPendingConnection();
tcpServer.setMaxPendingConnections(1);

if(socket == 0)
continue;
}
qInfo() << "Server started, listening for connection...";
tcpServer.waitForNewConnection(-1);
QTcpSocket *socket = tcpServer.nextPendingConnection();
else
socket->setSocketOption(QAbstractSocket::LowDelayOption, 1);

if( socket != 0 && verifyClient() ) {

if( socket != 0 && verifyClient(socket) ) {
QString clientIp = socket->peerAddress().toString();
int index = clientIp.lastIndexOf(':'); index = index==-1?0:index;
clientIp = clientIp.right(clientIp.length() - index - 1);
updateClientIp(clientIp);

qInfo() << "Client verified\n";
startInputLoop(socket);
startInputLoop();
}
else
else if(socket != 0)
qInfo() << "Could not verify client";

delete socket;
socket = 0;
}

FakeInput::freeFakeInput();
}

QString NetworkThread::getPassword()
Expand All @@ -53,25 +109,27 @@ QString NetworkThread::getPassword()
return password;
}

bool NetworkThread::verifyClient(QTcpSocket *socket)
bool NetworkThread::verifyClient()
{
if( !socket->waitForReadyRead(2000) )
if( !socket->waitForReadyRead(2000) ) {
qInfo() << "Read timed out\n";
return false;
}

QString desiredResponse("cow.emoji.WifiMouseClient "+getPassword()+"\n");
QString clientResponse( socket->readLine() );
QString desiredResponse("cow.emoji.WifiMouseClient "+getPassword());
QString clientResponse = readString();
qInfo() << clientResponse;

if(clientResponse == desiredResponse) {
socket->write("cow.emoji.WifiMouseServer Accepted\n");
socket->waitForBytesWritten(60);
QString str = "cow.emoji.WifiMouseServer Accepted "+serverVersion+" "+QHostInfo::localHostName();
writeString(str);
return true;
}
else {
socket->write("cow.emoji.WifiMouseServer Pending\n");
socket->waitForBytesWritten(60);
QString str = "cow.emoji.WifiMouseServer Pending "+serverVersion+" "+QHostInfo::localHostName();
writeString(str);
return false;
}

return true;
}

void wcharToChar(wchar_t *wca, char *ca)
Expand Down Expand Up @@ -112,26 +170,29 @@ void specialKeyEvent(QString message)
}
}

void NetworkThread::startInputLoop(QTcpSocket *socket)
void NetworkThread::startInputLoop()
{
int pingCount = 0;
QString startPassword = getPassword();
while(true) {
if(!socket->waitForReadyRead(2000)) {
if(!socket->waitForReadyRead(1000)) {
qInfo() << "Read timed out...\n";
break;
}

// Read out all lines
for(QString message = socket->readLine(); message.length() > 0; message = socket->readLine()) {
message = message.left(message.length() - 1); // remove \n at end of each line
// Read out all messages sent
for(QString message = readString(); message.length() > 0; message = readString()) {
bool zoomEvent = false;

if(message == "PING") {
if(getPassword() != startPassword)
return;
socket->write("PING");
socket->waitForBytesWritten();
qInfo() << "Pinging... " << ++pingCount << "\n";
writeString("PING");
continue;
}
else if(message.startsWith("MouseMove ")) {

if(message.startsWith("MouseMove ")) {
message.remove("MouseMove ");
QStringList coords = message.split(",");
int x = ((QString)coords.at(0)).toInt();
Expand Down Expand Up @@ -162,7 +223,14 @@ void NetworkThread::startInputLoop(QTcpSocket *socket)
} else if(message.startsWith("SpecialKey ")) {
message.remove("SpecialKey ");
specialKeyEvent(message);
} else if(message.startsWith("Zoom ")) {
zoomEvent = true;
message.remove("Zoom ");
FakeInput::zoom(message.toInt());
}

if(!zoomEvent)
FakeInput::stopZoom();
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions networkthread.h
Expand Up @@ -10,15 +10,12 @@
class NetworkThread : public QThread
{
public:
NetworkThread();
~NetworkThread();

void run();
MainWindow *mainWindow;
private:
QString getPassword();
bool verifyClient(QTcpSocket *socket);
void startInputLoop(QTcpSocket *socket);
bool verifyClient();
void startInputLoop();
void updateClientIp(QString ip);
};

Expand Down
6 changes: 6 additions & 0 deletions setpassworddialog.cpp
Expand Up @@ -33,3 +33,9 @@ void SetPasswordDialog::closeEvent(QCloseEvent *event)
ui->setPasswordEdit->setText("");
this->hide();
}

void SetPasswordDialog::show()
{
QDialog::show();
ui->setPasswordEdit->setFocus(Qt::OtherFocusReason);
}
1 change: 1 addition & 0 deletions setpassworddialog.h
Expand Up @@ -16,6 +16,7 @@ class SetPasswordDialog : public QDialog
~SetPasswordDialog();

void closeEvent(QCloseEvent *event);
void show();

private:
Ui::SetPasswordDialog *ui;
Expand Down

0 comments on commit 39ed495

Please sign in to comment.