-
Notifications
You must be signed in to change notification settings - Fork 10
/
UserInputClient.cpp
266 lines (247 loc) · 6.97 KB
/
UserInputClient.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (C) 2009,2010,2011,2012 GlavSoft LLC.
// All rights reserved.
//
//-------------------------------------------------------------------------
// This file is part of the TightVNC software. Please visit our Web site:
//
// http://www.tightvnc.com/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//-------------------------------------------------------------------------
//
#include "UserInputClient.h"
#include "thread/AutoLock.h"
#include "ReconnectException.h"
#include "util/BrokenHandleException.h"
UserInputClient::UserInputClient(BlockingGate *forwGate,
DesktopSrvDispatcher *dispatcher,
ClipboardListener *clipboardListener)
: DesktopServerProto(forwGate),
m_clipboardListener(clipboardListener),
m_sendMouseFlags(0)
{
dispatcher->registerNewHandle(CLIPBOARD_CHANGED, this);
}
UserInputClient::~UserInputClient()
{
}
void UserInputClient::onRequest(UINT8 reqCode, BlockingGate *backGate)
{
switch (reqCode) {
case CLIPBOARD_CHANGED:
{
StringStorage newClipboard;
readNewClipboard(&newClipboard, backGate);
m_clipboardListener->onClipboardUpdate(&newClipboard);
}
break;
default:
StringStorage errMess;
errMess.format(_T("Unknown %d protocol code received from a pipe ")
_T("UserInputServer"), (int)reqCode);
throw Exception(errMess.getString());
break;
}
}
void UserInputClient::sendInit(BlockingGate *gate)
{
AutoLock al(gate);
gate->writeUInt8(USER_INPUT_INIT);
gate->writeUInt8(m_sendMouseFlags);
}
void UserInputClient::setMouseEvent(const Point newPos, UINT8 keyFlag)
{
AutoLock al(m_forwGate);
try {
// Send mouse data
m_forwGate->writeUInt8(POINTER_POS_CHANGED);
sendNewPointerPos(newPos, keyFlag, m_forwGate);
m_sendMouseFlags = keyFlag;
} catch (ReconnectException &) {
}
}
void UserInputClient::setNewClipboard(const StringStorage *newClipboard)
{
AutoLock al(m_forwGate);
try {
// Send clipboard data
m_forwGate->writeUInt8(CLIPBOARD_CHANGED);
sendNewClipboard(newClipboard, m_forwGate);
} catch (ReconnectException &) {
}
}
void UserInputClient::setKeyboardEvent(UINT32 keySym, bool down)
{
AutoLock al(m_forwGate);
try {
// Send keyboard data
m_forwGate->writeUInt8(KEYBOARD_EVENT);
sendKeyEvent(keySym, down, m_forwGate);
} catch (ReconnectException &) {
}
}
void UserInputClient::getCurrentUserInfo(StringStorage *desktopName,
StringStorage *userName)
{
AutoLock al(m_forwGate);
try {
// Send request
m_forwGate->writeUInt8(USER_INFO_REQ);
readUserInfo(desktopName, userName, m_forwGate);
} catch (ReconnectException &) {
}
}
void UserInputClient::getPrimaryDisplayCoords(Rect *rect)
{
AutoLock al(m_forwGate);
bool success = false;
do {
try {
// Send request
m_forwGate->writeUInt8(DESKTOP_COORDS_REQ);
*rect = readRect(m_forwGate);
success = true;
} catch (ReconnectException &) {
}
} while (!success);
}
void UserInputClient::getDisplayNumberCoords(Rect *rect,
unsigned char dispNumber)
{
AutoLock al(m_forwGate);
bool success = false;
do {
try {
// Send request
m_forwGate->writeUInt8(DISPLAY_NUMBER_COORDS_REQ);
m_forwGate->writeUInt8(dispNumber);
*rect = readRect(m_forwGate);
success = true;
} catch (ReconnectException &) {
}
} while (!success);
}
std::vector<Rect> UserInputClient::getDisplaysCoords()
{
std::vector<Rect> res;
AutoLock al(m_forwGate);
bool success = false;
unsigned char number;
do {
try {
res.resize(0);
// Send request
m_forwGate->writeUInt8(DISPLAYS_COORDS_REQ);
number = m_forwGate->readUInt8();
for (size_t i = 0; i < number; i++) {
Rect rect = readRect(m_forwGate);
res.push_back(rect);
}
success = true;
}
catch (ReconnectException &) {
}
} while (!success);
return res;
}
void UserInputClient::getNormalizedRect(Rect *rect)
{
AutoLock al(m_forwGate);
bool success = false;
do {
try {
// Send request
m_forwGate->writeUInt8(NORMALIZE_RECT_REQ);
sendRect(rect, m_forwGate);
*rect = readRect(m_forwGate);
success = true;
} catch (ReconnectException &) {
}
} while (!success);
}
void UserInputClient::getWindowCoords(HWND hwnd, Rect *rect)
{
AutoLock al(m_forwGate);
bool success = false;
do {
try {
// Send request
m_forwGate->writeUInt8(WINDOW_COORDS_REQ);
m_forwGate->writeUInt64((UINT64)hwnd);
bool isBrokenWindow = m_forwGate->readUInt8() != 0;
if (!isBrokenWindow) {
*rect = readRect(m_forwGate);
} else {
// Receive error discription (do not generate it here).
// This made to avoid code duplication.
StringStorage errMess;
m_forwGate->readUTF8(&errMess);
throw BrokenHandleException(errMess.getString());
}
success = true;
} catch (ReconnectException &) {
}
} while (!success);
}
HWND UserInputClient::getWindowHandleByName(const StringStorage *windowName)
{
AutoLock al(m_forwGate);
bool success = false;
HWND hwnd = 0;
do {
try {
// Send request
m_forwGate->writeUInt8(WINDOW_HANDLE_REQ);
m_forwGate->writeUTF8(windowName->getString());
hwnd = (HWND)m_forwGate->readUInt64();
success = true;
} catch (ReconnectException &) {
}
} while (!success);
return hwnd;
}
void UserInputClient::getApplicationRegion(unsigned int procId, Region *region)
{
AutoLock al(m_forwGate);
bool success = false;
do {
try {
// Send request
m_forwGate->writeUInt8(APPLICATION_REGION_REQ);
m_forwGate->writeUInt32(procId);
readRegion(region, m_forwGate);
success = true;
} catch (ReconnectException &) {
}
} while (!success);
}
bool UserInputClient::isApplicationInFocus(unsigned int procId)
{
bool result = false;
AutoLock al(m_forwGate);
bool success = false;
do {
try {
// Send request
m_forwGate->writeUInt8(APPLICATION_CHECK_FOCUS);
m_forwGate->writeUInt32(procId);
// readRegion(region, m_forwGate);
result = (m_forwGate->readUInt8() != 0);
success = true;
} catch (ReconnectException &) {
}
} while (!success);
return result;
}