summaryrefslogtreecommitdiffstats
path: root/krdc/vnc/threads.cpp
blob: 860511e9d9c443481dee0f65dbc8f74e8cdaa712 (plain)
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
/***************************************************************************
                            threads.cpp  -  threads
                             -------------------
    begin                : Thu May 09 17:01:44 CET 2002
    copyright            : (C) 2015 Timothy Pearson <kb9vqf@pearsoncomputing.net>
                           (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 <tdeapplication.h>

#include "vncviewer.h"
#include "threads.h"

#include <tqcstring.h>
#include <tqpainter.h>

#include <math.h>

extern void pnmscale_fractional(const TQImage& src, TQImage& dst, int x, int y, int w, int h);

int getPassword(char * &passwd);

extern rfbBool newclient(rfbClient *cl)
{
	int width = cl->width, height = cl->height, depth = cl->format.bitsPerPixel;
	int size = width * height * (depth / 8);
	uint8_t *buf = new uint8_t[size];
	memset(buf, '\0', size);
	cl->frameBuffer = buf;
	cl->format.bitsPerPixel = 32;
	cl->format.redShift = 16;
	cl->format.greenShift = 8;
	cl->format.blueShift = 0;
	cl->format.redMax = 0xff;
	cl->format.greenMax = 0xff;
	cl->format.blueMax = 0xff;
	
	SetFormatAndEncodings(cl);
	
	return true;
}

extern void updatefb(rfbClient* cl, int x, int y, int w, int h)
{
// 	kdDebug(5011) << "updated client: x: " << x << ", y: " << y << ", w: " << w << ", h: " << h << endl;
	
	int width = cl->width, height = cl->height;
	
	TQImage img(cl->frameBuffer, width, height, 32, NULL, 256, TQImage::IgnoreEndian);
	
	if (img.isNull())
		kdDebug(5011) << "image not loaded" << endl;
	
	
	ControllerThreadObject *t = (ControllerThreadObject*)rfbClientGetClientData(cl, 0);
	
	t->setImage(img);
	t->queueDrawRegion(x, y, w, h);
}

extern char *passwd(rfbClient *cl)
{
	Q_UNUSED(cl)

	kdDebug(5011) << "password request" << endl;

	char *passwd;
	if (getPassword(passwd)) {
		return passwd;
	}
	else {
		return NULL;
	}
}

extern void authresults(rfbClient *cl, uint32_t authResult)
{
	kdDebug(5011) << "authentication result: " << authResult << endl;
	
	ControllerThreadObject *t = (ControllerThreadObject*)rfbClientGetClientData(cl, 0);

	t->authenticationResults(authResult);
}

extern void networkstat(rfbClient *cl, uint32_t statuscode)
{
	kdDebug(5011) << "network status: " << statuscode << endl;
	
	ControllerThreadObject *t = (ControllerThreadObject*)rfbClientGetClientData(cl, 0);

	t->networkStatus(statuscode);
}

extern void output(const char *format, ...)
{
	va_list args;
	va_start(args, format);
	
	TQString message;
	message.vsprintf(format, args);
	
	va_end(args);
	
	kdDebug(5011) << message.local8Bit();
	
	if (message.contains("Could not open")) {
		kdDebug(5011) << "Server not found!" << endl;
	}
	
	if (message.contains("VNC authentication succeeded")) {
		kdDebug(5011) << "Password OK" << endl;
	}
}

ControllerThreadObject::ControllerThreadObject(KVncView *v, volatile bool &quitFlag) :
	cl(0L),
	m_view(v),
	m_status(REMOTE_VIEW_CONNECTING),
	m_quitFlag(quitFlag),
	m_scaling(false),
	m_scalingWidth(-1),
	m_scalingHeight(-1),
	m_resizeEntireFrame(false)
{
	TQMutexLocker locker(&mutex);

	cl = rfbGetClient(8, 3, 4);
}

ControllerThreadObject::~ControllerThreadObject() {
	TQMutexLocker locker(&mutex);

	rfbClientCleanup(cl);
}

void ControllerThreadObject::changeStatus(RemoteViewStatus s) {
	m_status = s;
	TQApplication::postEvent(m_view, new StatusChangeEvent(s));
}

void ControllerThreadObject::sendFatalError(ErrorCode s) {
	m_quitFlag = true;
	TQApplication::postEvent(m_view, new FatalErrorEvent(s));
}

void ControllerThreadObject::queueDrawRegion(int x, int y, int w, int h) {
	if (m_scaling) {
		// Rescale desktop

		int new_x;
		int new_y;
		int new_w;
		int new_h;

		mutex.lock();

		if (m_resizeEntireFrame) {
			m_scaledImage.create(m_scalingWidth, m_scalingHeight, 32);

			new_x = 0;
			new_y = 0;
			new_w = m_scalingWidth;
			new_h = m_scalingHeight;

			TQImage scaledBlock = m_image.smoothScale(new_w, new_h);
			bitBlt(&m_scaledImage, new_x, new_y, &scaledBlock, 0, 0, new_w, new_h);

			m_resizeEntireFrame = false;
		}
		else {
			// Extend redraw boundaries to avoid pixelation artifacts due to rounding errors
			x = x - round((2.0 * m_image.width()) / m_scalingWidth);
			y = y - round((2.0 * m_image.height()) / m_scalingHeight);
			w = w + round((4.0 * m_image.width()) / m_scalingWidth);
			h = h + round((4.0 * m_image.height()) / m_scalingHeight);

			if (x < 0) {
				x = 0;
			}
			if (y < 0) {
				y = 0;
			}
			if (w > m_image.width()) {
				w = m_image.width();
			}
			if (h > m_image.height()) {
				h = m_image.height();
			}

			new_x = round((x * m_scalingWidth) / m_image.width());
			new_y = round((y * m_scalingHeight) / m_image.height());
			new_w = round((w * m_scalingWidth) / m_image.width());
			new_h = round((h * m_scalingHeight) / m_image.height());

			TQImage scaledBlock(m_scalingWidth, m_scalingHeight, 32);
			pnmscale_fractional(m_image, scaledBlock, new_x, new_y, new_w, new_h);
			bitBlt(&m_scaledImage, new_x, new_y, &scaledBlock, new_x, new_y, new_w, new_h);
		}

		mutex.unlock();

		DrawScreenRegion(new_x, new_y, new_w, new_h);
	}
	else {
		DrawScreenRegion(x, y, w, h);
	}
}

void ControllerThreadObject::setImage(const TQImage &img) {
	TQMutexLocker locker(&mutex);

	m_image = img;
}

const TQImage ControllerThreadObject::image(int x, int y, int w, int h) {
	TQMutexLocker locker(&mutex);

	if (m_scaling) {
		return m_scaledImage.copy(x, y, w, h);
	}
	else {
		return m_image.copy(x, y, w, h);
	}
}

void ControllerThreadObject::setScaling(int w, int h) {
	bool scale;

	if (w <= 0) {
		scale = false;
	}
	else {
		scale = true;
	}

	if ((m_scalingWidth != w) || (m_scalingHeight = h) || (m_scaling != scale)) {
		m_resizeEntireFrame = true;
	}

	m_scaling = scale;
	m_scalingWidth = w;
	m_scalingHeight = h;
}

void ControllerThreadObject::run() {
	mutex.lock();

	rfbClientLog = output;
	rfbClientErr = output;
	cl->MallocFrameBuffer = newclient;
	cl->canHandleNewFBSize = true;
	cl->GetPassword = passwd;
	cl->AuthenticationResults = authresults;
	cl->NetworkStatus = networkstat;
	cl->GotFrameBufferUpdate = updatefb;
	rfbClientSetClientData(cl, 0, this);

	// make a copy of the host string...
	char *host = (char*) malloc(m_view->host().length());
	strcpy(host, m_view->host().ascii());

	cl->serverHost = host;

	int port = m_view->port();
	if(port >= 0 && port < 100) // the user most likely used the short form (e.g. :1)
		port += 5900;
	cl->serverPort = port;

	mutex.unlock();

	if(!rfbInitClient(cl, 0, 0)) {
		sendFatalError(ERROR_INTERNAL);
		// Terminate thread
		TQThread::exit();
		return;
	}

	TQApplication::postEvent(m_view,
				new ScreenResizeEvent(cl->width,
						      cl->height));

	changeStatus(REMOTE_VIEW_CONNECTED);
	
	while (!m_quitFlag) {	
		int i = WaitForMessage(cl, 500);
		if (i < 0) {
			m_quitFlag = true;
			changeStatus(REMOTE_VIEW_DISCONNECTED);

			// Terminate thread
			TQThread::exit();
			return;
		}
		if (i) {
			if(!HandleRFBServerMessage(cl)) {
				m_quitFlag = true;
				changeStatus(REMOTE_VIEW_DISCONNECTED);

				// Terminate thread
				TQThread::exit();
				return;
			}
		}
	}

	m_quitFlag = true;
	changeStatus(REMOTE_VIEW_DISCONNECTED);

	// Terminate thread
	TQThread::exit();
}

void ControllerThreadObject::authenticationResults(int resultCode) {
	if (resultCode == rfbVncAuthOK) {
		changeStatus(REMOTE_VIEW_PREPARING);
	}
	else {
		sendFatalError(ERROR_AUTHENTICATION);

		// Terminate thread
		TQThread::exit();
	}
}

void ControllerThreadObject::networkStatus(int statusCode) {
	if (statusCode == rfbNetworkConnectionSuccess) {
		// Stage 1 OK...
		changeStatus(REMOTE_VIEW_AUTHENTICATING);
	}
	else if (statusCode == rfbNetworkRFBConnectionSuccess) {
		// Stage 2 OK!
	}
	else {
		if (statusCode == rfbNetworkConnectionClosed) {
			sendFatalError(ERROR_CONNECTION);
		}
		else if (statusCode == rfbNetworkConnectionFailed) {
			sendFatalError(ERROR_CONNECTION);
		}
		else if (statusCode == rfbNetworkNameResolutionFailed) {
			sendFatalError(ERROR_NAME);
		}
		else if (statusCode == rfbNetworkRFBServerNotValid) {
			sendFatalError(ERROR_IO);
		}
		else if (statusCode == rfbNetworkRFBProtocolFailure) {
			sendFatalError(ERROR_PROTOCOL);
		}
	
		// Terminate thread
		TQThread::exit();
	}
}

enum RemoteViewStatus ControllerThreadObject::status() {
	return m_status;
}

void ControllerThreadObject::queueMouseEvent(int x, int y, int buttonMask) {
	SendPointerEvent(cl, x, y, buttonMask);
}

void ControllerThreadObject::queueKeyEvent(unsigned int k, bool down) {
	SendKeyEvent(cl, k, down);
}

void ControllerThreadObject::queueClientCut(const TQString &text) {
	SendClientCutText(cl, (char*)text.ascii(), text.length());
}

#include "threads.moc"