-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathglobalgestureforwarder.cpp
More file actions
154 lines (137 loc) · 4.86 KB
/
globalgestureforwarder.cpp
File metadata and controls
154 lines (137 loc) · 4.86 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "globalgestureforwarder.h"
#include "globalgesturetypes.h"
#include <QDebug>
#include <QTimer>
#include <QSizeF>
GlobalGestureForwarder* GlobalGestureForwarder::_self = new GlobalGestureForwarder();
const QDataStream::Version QDATA_STREAM_VERSION = QDataStream::Qt_5_14;
GlobalGestureForwarder::GlobalGestureForwarder(QObject *parent)
: QObject(parent),
socket(new QLocalSocket(this)) ,
connectTimer(new QTimer(this))
{
outStream = new QDataStream(&block, QIODevice::WriteOnly);
outStream->setVersion(QDATA_STREAM_VERSION);
connect(socket, QOverload<QLocalSocket::LocalSocketError>::of(&QLocalSocket::error),
this, [this](QLocalSocket::LocalSocketError socketError) {
switch (socketError) {
case QLocalSocket::ServerNotFoundError:
qCritical()<<tr("The host was not found. Please make sure "
"that the server is running and that the "
"server name is correct.");
break;
case QLocalSocket::ConnectionRefusedError:
qCritical()<<tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the server name "
"is correct.");
break;
case QLocalSocket::PeerClosedError:
break;
default:
qCritical()<<"The following error occurred:"<<socket->errorString();
}
QTimer::singleShot(50, this, [this] {
connectToServer();
});
});
connect(socket, &QLocalSocket::connected, this, [this]() {
qDebug()<<"connection success";
bConnected = true;
});
connect(socket, &QLocalSocket::disconnected, this, [this] {
qDebug()<<"connection is broken.";
QTimer::singleShot(50, this, [this] {
bConnected = false;
connectToServer();
});
});
}
void GlobalGestureForwarder::init(int pid)
{
curServerPid = pid;
connectToServer();
}
void GlobalGestureForwarder::pinchGestureBegin(int fingerCount, quint32 time)
{
if (bConnected) {
*outStream << (quint32)(sizeof(GESTURE_PINCH_BEGIN) + sizeof(fingerCount) + sizeof(time));
*outStream << GESTURE_PINCH_BEGIN << fingerCount << time;
sendData();
}
}
void GlobalGestureForwarder::pinchGestureUpdate(qreal scale, qreal angleDelta, const QSizeF &delta, quint32 time)
{
if (bConnected) {
*outStream << (quint32)(sizeof(qreal)*4 + sizeof(time) + sizeof(GESTURE_PINCH_UPDATE));
*outStream << GESTURE_PINCH_UPDATE << scale << angleDelta << delta.width() << delta.height() << time;
sendData();
}
}
void GlobalGestureForwarder::pinchGestureEnd(quint32 time)
{
if (bConnected) {
*outStream << (quint32)(sizeof(GESTURE_PINCH_END) + sizeof(time));
*outStream << GESTURE_PINCH_END << time;
sendData();
}
}
void GlobalGestureForwarder::pinchGestureCancelled(quint32 time)
{
if (bConnected) {
*outStream << (quint32)(sizeof(GESTURE_PINCH_CANCELL) + sizeof(time));
*outStream << GESTURE_PINCH_CANCELL << time;
sendData();
}
}
void GlobalGestureForwarder::swipeGestureBegin(int fingerCount, quint32 time)
{
if (bConnected) {
*outStream << (quint32)(sizeof(GESTURE_SWIPE_BEGIN) + sizeof(fingerCount) + sizeof(time));
*outStream << GESTURE_SWIPE_BEGIN << fingerCount << time;
sendData();
lastSwipDelta = QSizeF(0,0);
}
}
void GlobalGestureForwarder::swipeGestureUpdate(const QSizeF &delta, quint32 time)
{
if (bConnected && delta != lastSwipDelta) {
*outStream << (quint32)(sizeof(qreal)*2 + sizeof(time) + sizeof(GESTURE_SWIPE_UPDATE));
*outStream << GESTURE_SWIPE_UPDATE << delta.width() << delta.height() << time;
sendData();
lastSwipDelta = QSizeF(delta);
}
}
void GlobalGestureForwarder::swipeGestureEnd(quint32 time)
{
if (bConnected) {
*outStream << (quint32)(sizeof(GESTURE_SWIPE_END) + sizeof(time));
*outStream << GESTURE_SWIPE_END << time;
sendData();
lastSwipDelta = QSizeF(0,0);
}
}
void GlobalGestureForwarder::swipeGestureCancelled(quint32 time)
{
if (bConnected) {
*outStream << (quint32)(sizeof(GESTURE_SWIPE_CANCELL) + sizeof(time));
*outStream << GESTURE_SWIPE_CANCELL << time;
sendData();
lastSwipDelta = QSizeF(0,0);
}
}
void GlobalGestureForwarder::connectToServer()
{
socket->abort();
QString serverName = QString(GESTURE_SERVER_NAME).arg(curServerPid);
const char* name = serverName.toStdString().c_str();
socket->connectToServer(serverName);
}
void GlobalGestureForwarder::sendData()
{
if (bConnected) {
socket->write(block);
block.clear();
(*outStream).device()->seek(0);
}
}