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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/***************************************************************************
threads.cpp - threads
-------------------
begin : Thu May 09 17:01:44 CET 2002
copyright : (C) 2002 by Tim Jansen
email : tim@tjansen.de
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "kvncview.h"
#include <kdebug.h>
#include <kapplication.h>
#include "vncviewer.h"
#include "threads.h"
#include <tqcstring.h>
// Maximum idle time for writer thread in ms. When it timeouts, it will request
// another incremental update. Must be smaller than the timeout of the server
// (krfb's is 20s).
static const int MAXIMUM_WAIT_PERIOD = 8000;
// time to postpone incremental updates that have not been requested explicitly
static const int POSTPONED_INCRRTQ_WAIT_PERIOD = 110;
static const int MOUSEPRESS_TQUEUE_SIZE = 5;
static const int MOUSEMOVE_TQUEUE_SIZE = 3;
static const int KEY_TQUEUE_SIZE = 8192;
ControllerThread::ControllerThread(KVncView *v, WriterThread &wt, volatile bool &quitFlag) :
m_view(v),
m_status(REMOTE_VIEW_CONNECTING),
m_wthread(wt),
m_quitFlag(quitFlag),
m_desktopInitialized(false)
{
}
void ControllerThread::changeStatus(RemoteViewStatus s) {
m_status = s;
TQApplication::postEvent(m_view, new StatusChangeEvent(s));
}
void ControllerThread::sendFatalError(ErrorCode s) {
m_quitFlag = true;
TQApplication::postEvent(m_view, new FatalErrorEvent(s));
m_wthread.kick();
}
/*
* Calls this from the X11 thread
*/
void ControllerThread::desktopInit() {
SetVisualAndCmap();
ToplevelInit();
DesktopInit(m_view->winId());
m_desktopInitialized = true;
m_waiter.wakeAll();
}
void ControllerThread::kick() {
m_waiter.wakeAll();
}
void ControllerThread::run() {
int fd;
fd = ConnectToRFBServer(m_view->host().latin1(), m_view->port());
if (fd < 0) {
if (fd == -(int)INIT_NO_SERVER)
sendFatalError(ERROR_NO_SERVER);
else if (fd == -(int)INIT_NAME_RESOLUTION_FAILURE)
sendFatalError(ERROR_NAME);
else
sendFatalError(ERROR_CONNECTION);
return;
}
if (m_quitFlag) {
changeStatus(REMOTE_VIEW_DISCONNECTED);
return;
}
changeStatus(REMOTE_VIEW_AUTHENTICATING);
enum InitStatus s = InitialiseRFBConnection();
if (s != INIT_OK) {
if (s == INIT_CONNECTION_FAILED)
sendFatalError(ERROR_IO);
else if (s == INIT_SERVER_BLOCKED)
sendFatalError(ERROR_SERVER_BLOCKED);
else if (s == INIT_PROTOCOL_FAILURE)
sendFatalError(ERROR_PROTOCOL);
else if (s == INIT_AUTHENTICATION_FAILED)
sendFatalError(ERROR_AUTHENTICATION);
else if (s == INIT_ABORTED)
changeStatus(REMOTE_VIEW_DISCONNECTED);
else
sendFatalError(ERROR_INTERNAL);
return;
}
TQApplication::postEvent(m_view,
new ScreenResizeEvent(si.framebufferWidth,
si.framebufferHeight));
m_wthread.queueUpdateRequest(TQRegion(TQRect(0,0,si.framebufferWidth,
si.framebufferHeight)));
TQApplication::postEvent(m_view, new DesktopInitEvent());
while ((!m_quitFlag) && (!m_desktopInitialized))
m_waiter.wait(1000);
if (m_quitFlag) {
changeStatus(REMOTE_VIEW_DISCONNECTED);
return;
}
changeStatus(REMOTE_VIEW_PREPARING);
if (!SetFormatAndEncodings()) {
sendFatalError(ERROR_INTERNAL);
return;
}
changeStatus(REMOTE_VIEW_CONNECTED);
m_wthread.start();
while (!m_quitFlag) {
if ((!HandleRFBServerMessage()) && (!m_quitFlag)) {
sendFatalError(ERROR_IO);
return;
}
}
m_quitFlag = true;
changeStatus(REMOTE_VIEW_DISCONNECTED);
m_wthread.kick();
}
enum RemoteViewStatus ControllerThread::status() {
return m_status;
}
static WriterThread *writerThread;
void queueIncrementalUpdateRequest() {
writerThread->queueIncrementalUpdateRequest();
}
void announceIncrementalUpdateRequest() {
writerThread->announceIncrementalUpdateRequest();
}
WriterThread::WriterThread(KVncView *v, volatile bool &quitFlag) :
m_quitFlag(quitFlag),
m_view(v),
m_lastIncrUpdatePostponed(false),
m_incrementalUpdateRQ(false),
m_incrementalUpdateAnnounced(false),
m_mouseEventNum(0),
m_keyEventNum(0),
m_clientCut(TQString())
{
writerThread = this;
m_lastIncrUpdate.start();
}
bool WriterThread::sendIncrementalUpdateRequest() {
m_lastIncrUpdate.restart();
return SendIncrementalFramebufferUpdateRequest();
}
bool WriterThread::sendUpdateRequest(const TQRegion ®ion) {
TQMemArray<TQRect> r = region.rects();
for (unsigned int i = 0; i < r.size(); i++)
if (!SendFramebufferUpdateRequest(r[i].x(),
r[i].y(),
r[i].width(),
r[i].height(), False))
return false;
return true;
}
bool WriterThread::sendInputEvents(const TQValueList<InputEvent> &events) {
TQValueList<InputEvent>::const_iterator it = events.begin();
while (it != events.end()) {
if ((*it).type == KeyEventType) {
if (!SendKeyEvent((*it).e.k.k, (*it).e.k.down ? True : False))
return false;
}
else
if (!SendPointerEvent((*it).e.m.x, (*it).e.m.y, (*it).e.m.buttons))
return false;
it++;
}
return true;
}
void WriterThread::queueIncrementalUpdateRequest() {
m_lock.lock();
m_incrementalUpdateRQ = true;
m_waiter.wakeAll();
m_lock.unlock();
}
void WriterThread::announceIncrementalUpdateRequest() {
m_lock.lock();
m_incrementalUpdateAnnounced = true;
m_lock.unlock();
}
void WriterThread::queueUpdateRequest(const TQRegion &r) {
m_lock.lock();
m_updateRegionRQ += r;
m_waiter.wakeAll();
m_lock.unlock();
}
void WriterThread::queueMouseEvent(int x, int y, int buttonMask) {
InputEvent e;
e.type = MouseEventType;
e.e.m.x = x;
e.e.m.y = y;
e.e.m.buttons = buttonMask;
m_lock.lock();
if (m_mouseEventNum > 0) {
if ((e.e.m.x == m_lastMouseEvent.x) &&
(e.e.m.y == m_lastMouseEvent.y) &&
(e.e.m.buttons == m_lastMouseEvent.buttons)) {
m_lock.unlock();
return;
}
if (m_mouseEventNum >= MOUSEPRESS_TQUEUE_SIZE) {
m_lock.unlock();
return;
}
if ((m_lastMouseEvent.buttons == buttonMask) &&
(m_mouseEventNum >= MOUSEMOVE_TQUEUE_SIZE)) {
m_lock.unlock();
return;
}
}
m_mouseEventNum++;
m_lastMouseEvent = e.e.m;
m_inputEvents.push_back(e);
m_waiter.wakeAll();
m_lock.unlock();
}
void WriterThread::queueKeyEvent(unsigned int k, bool down) {
InputEvent e;
e.type = KeyEventType;
e.e.k.k = k;
e.e.k.down = down;
m_lock.lock();
if (m_keyEventNum >= KEY_TQUEUE_SIZE) {
m_lock.unlock();
return;
}
m_keyEventNum++;
m_inputEvents.push_back(e);
m_waiter.wakeAll();
m_lock.unlock();
}
void WriterThread::queueClientCut(const TQString &text) {
m_lock.lock();
m_clientCut = text;
m_waiter.wakeAll();
m_lock.unlock();
}
void WriterThread::kick() {
m_waiter.wakeAll();
}
void WriterThread::run() {
bool incrementalUpdateRQ = false;
bool incrementalUpdateAnnounced = false;
TQRegion updateRegionRQ;
TQValueList<InputEvent> inputEvents;
TQString clientCut;
while (!m_quitFlag) {
m_lock.lock();
incrementalUpdateRQ = m_incrementalUpdateRQ;
incrementalUpdateAnnounced = m_incrementalUpdateAnnounced;
updateRegionRQ = m_updateRegionRQ;
inputEvents = m_inputEvents;
clientCut = m_clientCut;
if ((!incrementalUpdateRQ) &&
(updateRegionRQ.isNull()) &&
(inputEvents.size() == 0) &&
(clientCut.isNull())) {
if (!m_waiter.wait(&m_lock,
m_lastIncrUpdatePostponed ?
POSTPONED_INCRRTQ_WAIT_PERIOD : MAXIMUM_WAIT_PERIOD))
m_incrementalUpdateRQ = true;
m_lock.unlock();
}
else {
m_incrementalUpdateRQ = false;
m_incrementalUpdateAnnounced = false;
m_updateRegionRQ = TQRegion();
m_inputEvents.clear();
m_keyEventNum = 0;
m_mouseEventNum = 0;
m_clientCut = TQString();
m_lock.unlock();
// always send incremental update, unless
// a) a framebuffer update is done ATM and will do the request later, or
// b) the last unrequested update has been done less than 0.1s ago
//
// if the update has not been done because of b, postpone it.
if (incrementalUpdateRQ || !incrementalUpdateAnnounced) {
bool sendUpdate;
if (incrementalUpdateRQ) {
sendUpdate = true;
m_lastIncrUpdatePostponed = false;
}
else {
if (m_lastIncrUpdate.elapsed() < 100) {
sendUpdate = false;
m_lastIncrUpdatePostponed = true;
}
else {
sendUpdate = true;
m_lastIncrUpdatePostponed = false;
}
}
if (sendUpdate)
if (!sendIncrementalUpdateRequest()) {
sendFatalError(ERROR_IO);
break;
}
}
else
m_lastIncrUpdatePostponed = false;
if (!updateRegionRQ.isNull())
if (!sendUpdateRequest(updateRegionRQ)) {
sendFatalError(ERROR_IO);
break;
}
if (inputEvents.size() != 0)
if (!sendInputEvents(inputEvents)) {
sendFatalError(ERROR_IO);
break;
}
if (!clientCut.isNull()) {
TQCString cutTextUtf8(clientCut.utf8());
if (!SendClientCutText(cutTextUtf8.data(),
(int)cutTextUtf8.length())) {
sendFatalError(ERROR_IO);
break;
}
}
}
}
m_quitFlag = true;
}
void WriterThread::sendFatalError(ErrorCode s) {
m_quitFlag = true;
TQApplication::postEvent(m_view, new FatalErrorEvent(s));
}
|