-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.cpp
97 lines (83 loc) · 2.3 KB
/
server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "server.h"
Server::Server(QWidget *parent) : QDialog(parent)
{
trayMenu = new QMenu;
trayMenu->addAction(QIcon(":/off"), "Exit", qApp, SLOT(quit()));
tray = new QSystemTrayIcon(QIcon(":/icon"), this);
tray->setContextMenu(trayMenu);
tray->show();
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(accept()));
if(server->listen(QHostAddress::AnyIPv4, 7777))
{
QMessageBox::information(this, "Success", "Listening on port 7777");
}
else
{
QMessageBox::critical(this, "Error", "Unable to listen on port 7777");
exit(EXIT_FAILURE);
}
}
Server::~Server()
{
}
void Server::accept()
{
client = server->nextPendingConnection();
client->setSocketOption(QAbstractSocket::LowDelayOption, 1);
connect(client, SIGNAL(readyRead()), this, SLOT(read()));
}
void Server::read()
{
#ifdef Q_OS_LINUX
QString request = QString(client->readAll()).remove('|');
exec(request);
#endif
#ifdef Q_OS_WIN32
/** \bug winsock causes abnormal buffer read, so using a delimiter
* to separate the requests and execute them one by one
*/
QStringList requests = QString(client->readAll()).split('|');
requests.pop_back(); // remove the last '|' character
foreach (QString cmd, requests)
exec(cmd);
#endif
}
void Server::exec(QString request)
{
QPoint pos = QCursor::pos();
if(request == "connect")
{
tray->setIcon(QIcon(":/on"));
}
else if(request == "disconnect")
{
tray->setIcon(QIcon(":/off"));
}
else if(request == "click")
{
#ifdef Q_OS_WIN32
mouse_event(MOUSEEVENTF_LEFTDOWN, pos.x(), pos.y(), 0,0);
mouse_event(MOUSEEVENTF_LEFTUP, pos.x(), pos.y(), 0,0);
#endif
#ifdef Q_OS_LINUX
/** \todo add x11 based left click event **/
#endif
}
else if(request == "menu")
{
#ifdef Q_OS_WIN32
mouse_event(MOUSEEVENTF_RIGHTDOWN, pos.x(), pos.y(), 0,0);
mouse_event(MOUSEEVENTF_RIGHTUP, pos.x(), pos.y(), 0,0);
#endif
#ifdef Q_OS_LINUX
/** \todo add x11 based right click event **/
#endif
}
else
{
float x = request.split(",")[0].toFloat();
float y = request.split(",")[1].toFloat();
QCursor::setPos(pos.x() + x, pos.y() + y);
}
}