blob: c78bb8911fde709ba17f7b8e5da79146a19bc335 [file] [log] [blame]
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
DRC33c15e32011-11-03 18:49:21 +00002 * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01003 * Copyright 2009-2014 Pierre Ossman for Cendio AB
Pierre Ossman5156d5e2011-03-09 09:42:34 +00004 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 * USA.
19 */
20
Peter Åstrandc359f362011-08-23 12:04:46 +000021#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
Pierre Ossman5156d5e2011-03-09 09:42:34 +000025#include <assert.h>
DRCb65bb932011-06-24 03:17:00 +000026#ifndef _WIN32
Pierre Ossman5156d5e2011-03-09 09:42:34 +000027#include <unistd.h>
DRCb65bb932011-06-24 03:17:00 +000028#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +000029
30#include <rfb/CMsgWriter.h>
31#include <rfb/encodings.h>
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +010032#include <rfb/Decoder.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000033#include <rfb/Hostname.h>
34#include <rfb/LogWriter.h>
35#include <rfb/util.h>
36#include <rfb/screenTypes.h>
Pierre Ossmane28bdb22011-11-14 16:02:06 +000037#include <rfb/fenceTypes.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000038#include <rfb/Timer.h>
Pierre Ossmane28bdb22011-11-14 16:02:06 +000039#include <rdr/MemInStream.h>
40#include <rdr/MemOutStream.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000041#include <network/TcpSocket.h>
42
43#include <FL/Fl.H>
44#include <FL/fl_ask.H>
45
46#include "CConn.h"
Pierre Ossmanf4f30942011-05-17 09:39:07 +000047#include "OptionsDialog.h"
Pierre Ossman947b48d2014-01-27 16:52:35 +010048#include "DesktopWindow.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000049#include "i18n.h"
50#include "parameters.h"
Pierre Ossman39ceb502011-07-12 15:54:25 +000051#include "vncviewer.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000052
DRCb65bb932011-06-24 03:17:00 +000053#ifdef WIN32
54#include "win32.h"
55#endif
56
Pierre Ossman5156d5e2011-03-09 09:42:34 +000057using namespace rdr;
58using namespace rfb;
59using namespace std;
60
Pierre Ossman5156d5e2011-03-09 09:42:34 +000061static rfb::LogWriter vlog("CConn");
62
Pierre Ossmancf836f22011-07-14 14:34:09 +000063// 8 colours (1 bit per component)
64static const PixelFormat verylowColourPF(8, 3,false, true,
65 1, 1, 1, 2, 1, 0);
66// 64 colours (2 bits per component)
67static const PixelFormat lowColourPF(8, 6, false, true,
68 3, 3, 3, 4, 2, 0);
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010069// 256 colours (2-3 bits per component)
70static const PixelFormat mediumColourPF(8, 8, false, true,
71 7, 7, 3, 5, 2, 0);
Pierre Ossmanf4f30942011-05-17 09:39:07 +000072
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000073CConn::CConn(const char* vncServerName, network::Socket* socket=NULL)
74 : serverHost(0), serverPort(0), desktop(NULL),
Pierre Ossman5d512c32011-11-04 11:42:16 +000075 pendingPFChange(false),
Pierre Ossman5156d5e2011-03-09 09:42:34 +000076 currentEncoding(encodingTight), lastServerEncoding((unsigned int)-1),
77 formatChange(false), encodingChange(false),
Pierre Ossmanaa73c892011-11-15 12:13:37 +000078 firstUpdate(true), pendingUpdate(false), continuousUpdates(false),
Pierre Ossmane28bdb22011-11-14 16:02:06 +000079 forceNonincremental(true), supportsSyncFence(false)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000080{
81 setShared(::shared);
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000082 sock = socket;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000083
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +010084 memset(decoders, 0, sizeof(decoders));
85
Pierre Ossman5156d5e2011-03-09 09:42:34 +000086 int encNum = encodingNum(preferredEncoding);
87 if (encNum != -1)
88 currentEncoding = encNum;
89
Pierre Ossmanda389562011-06-09 08:24:37 +000090 cp.supportsLocalCursor = true;
91
Pierre Ossman5156d5e2011-03-09 09:42:34 +000092 cp.supportsDesktopResize = true;
93 cp.supportsExtendedDesktopSize = true;
94 cp.supportsDesktopRename = true;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000095
Pierre Ossmana22459d2014-03-17 14:42:10 +010096 if (customCompressLevel)
97 cp.compressLevel = compressLevel;
98 else
99 cp.compressLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000100
Pierre Ossmana22459d2014-03-17 14:42:10 +0100101 if (!noJpeg)
102 cp.qualityLevel = qualityLevel;
103 else
104 cp.qualityLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000105
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000106 if(sock == NULL) {
107 try {
108 getHostAndPort(vncServerName, &serverHost, &serverPort);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000109
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000110 sock = new network::TcpSocket(serverHost, serverPort);
111 vlog.info(_("connected to host %s port %d"), serverHost, serverPort);
112 } catch (rdr::Exception& e) {
113 vlog.error("%s", e.str());
114 fl_alert("%s", e.str());
115 exit_vncviewer();
116 return;
117 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000118 }
119
120 Fl::add_fd(sock->getFd(), FL_READ | FL_EXCEPT, socketEvent, this);
121
122 // See callback below
123 sock->inStream().setBlockCallback(this);
124
125 setServerName(serverHost);
126 setStreams(&sock->inStream(), &sock->outStream());
127
128 initialiseProtocol();
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000129
130 OptionsDialog::addCallback(handleOptions, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000131}
132
133CConn::~CConn()
134{
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000135 OptionsDialog::removeCallback(handleOptions);
136
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100137 for (int i = 0; i < sizeof(decoders)/sizeof(decoders[0]); i++)
138 delete decoders[i];
139
Pierre Ossman6a9e2e62011-05-19 14:47:43 +0000140 if (desktop)
141 delete desktop;
142
Pierre Ossman5e04c262011-11-09 11:31:12 +0000143 delete [] serverHost;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000144 if (sock)
145 Fl::remove_fd(sock->getFd());
146 delete sock;
147}
148
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000149void CConn::refreshFramebuffer()
150{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000151 forceNonincremental = true;
152
153 // Without fences, we cannot safely trigger an update request directly
154 // but must wait for the next update to arrive.
155 if (supportsSyncFence)
156 requestNewUpdate();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000157}
158
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000159const char *CConn::connectionInfo()
160{
161 static char infoText[1024] = "";
162
163 char pfStr[100];
164 char spfStr[100];
165
166 cp.pf().print(pfStr, 100);
167 serverPF.print(spfStr, 100);
168
169 int secType = csecurity->getType();
170
171 snprintf(infoText, sizeof(infoText),
172 _("Desktop name: %.80s\n"
173 "Host: %.80s port: %d\n"
174 "Size: %d x %d\n"
175 "Pixel format: %s\n"
176 "(server default %s)\n"
177 "Requested encoding: %s\n"
178 "Last used encoding: %s\n"
179 "Line speed estimate: %d kbit/s\n"
180 "Protocol version: %d.%d\n"
181 "Security method: %s\n"),
182 cp.name(), serverHost, serverPort, cp.width, cp.height,
183 pfStr, spfStr, encodingName(currentEncoding),
184 encodingName(lastServerEncoding),
185 sock->inStream().kbitsPerSecond(),
186 cp.majorVersion, cp.minorVersion,
187 secTypeName(secType));
188
189 return infoText;
190}
191
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000192// The RFB core is not properly asynchronous, so it calls this callback
193// whenever it needs to block to wait for more data. Since FLTK is
194// monitoring the socket, we just make sure FLTK gets to run.
195
196void CConn::blockCallback()
197{
198 int next_timer;
199
200 next_timer = Timer::checkTimeouts();
201 if (next_timer == 0)
202 next_timer = INT_MAX;
203
204 Fl::wait((double)next_timer / 1000.0);
205}
206
DRC3e7ed812013-02-26 10:34:22 +0000207void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000208{
209 CConn *cc;
210 static bool recursing = false;
211
212 assert(data);
213 cc = (CConn*)data;
214
215 // I don't think processMsg() is recursion safe, so add this check
216 if (recursing)
217 return;
218
219 recursing = true;
220
221 try {
222 // processMsg() only processes one message, so we need to loop
223 // until the buffers are empty or things will stall.
224 do {
225 cc->processMsg();
226 } while (cc->sock->inStream().checkNoWait(1));
227 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000228 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000229 exit_vncviewer();
230 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000231 vlog.error("%s", e.str());
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000232 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000233 }
234
235 recursing = false;
236}
237
238////////////////////// CConnection callback methods //////////////////////
239
240// serverInit() is called when the serverInit message has been received. At
241// this point we create the desktop window and display it. We also tell the
242// server the pixel format and encodings to use and request the first update.
243void CConn::serverInit()
244{
245 CConnection::serverInit();
246
247 // If using AutoSelect with old servers, start in FullColor
248 // mode. See comment in autoSelectFormatAndEncoding.
249 if (cp.beforeVersion(3, 8) && autoSelect)
250 fullColour.setParam(true);
251
252 serverPF = cp.pf();
253
254 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
255 fullColourPF = desktop->getPreferredPF();
256
Pierre Ossman5d512c32011-11-04 11:42:16 +0000257 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000258 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000259
260 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000261 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000262
263 // This initial update request is a bit of a corner case, so we need
264 // to help out setting the correct format here.
265 assert(pendingPFChange);
266 desktop->setServerPF(pendingPF);
267 cp.setPF(pendingPF);
268 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000269}
270
271// setDesktopSize() is called when the desktop size changes (including when
272// it is set initially).
273void CConn::setDesktopSize(int w, int h)
274{
275 CConnection::setDesktopSize(w,h);
276 resizeFramebuffer();
277}
278
279// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
280void CConn::setExtendedDesktopSize(int reason, int result, int w, int h,
281 const rfb::ScreenSet& layout)
282{
283 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
284
285 if ((reason == reasonClient) && (result != resultSuccess)) {
286 vlog.error(_("SetDesktopSize failed: %d"), result);
287 return;
288 }
289
290 resizeFramebuffer();
291}
292
293// setName() is called when the desktop name changes
294void CConn::setName(const char* name)
295{
296 CConnection::setName(name);
297 if (desktop)
298 desktop->setName(name);
299}
300
301// framebufferUpdateStart() is called at the beginning of an update.
302// Here we try to send out a new framebuffer update request so that the
303// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000304// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000305void CConn::framebufferUpdateStart()
306{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000307 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000308 pendingUpdate = false;
309
310 requestNewUpdate();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000311}
312
313// framebufferUpdateEnd() is called at the end of an update.
314// For each rectangle, the FdInStream will have timed the speed
315// of the connection, allowing us to select format and encoding
316// appropriately, and then request another incremental update.
317void CConn::framebufferUpdateEnd()
318{
319 desktop->updateWindow();
320
321 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000322 // We need fences to make extra update requests and continuous
323 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000324 if (cp.supportsFence)
325 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
326
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000327 firstUpdate = false;
328 }
329
Pierre Ossman5d512c32011-11-04 11:42:16 +0000330 // A format change has been scheduled and we are now past the update
331 // with the old format. Time to active the new one.
332 if (pendingPFChange) {
333 desktop->setServerPF(pendingPF);
334 cp.setPF(pendingPF);
335 pendingPFChange = false;
336 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000337
338 // Compute new settings based on updated bandwidth values
339 if (autoSelect)
340 autoSelectFormatAndEncoding();
341
342 // Make sure that the FLTK handling and the timers gets some CPU time
343 // in case of back to back framebuffer updates.
344 Fl::check();
345 Timer::checkTimeouts();
346}
347
348// The rest of the callbacks are fairly self-explanatory...
349
350void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
351{
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100352 vlog.error("Invalid SetColourMapEntries from server!");
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000353}
354
355void CConn::bell()
356{
357 fl_beep();
358}
359
360void CConn::serverCutText(const char* str, rdr::U32 len)
361{
Pierre Ossman689c4582011-05-26 15:39:41 +0000362 char *buffer;
363 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000364
Pierre Ossman50104882013-05-24 10:47:27 +0000365 if (!acceptClipboard)
366 return;
367
Pierre Ossman689c4582011-05-26 15:39:41 +0000368 size = fl_utf8froma(NULL, 0, str, len);
369 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000370 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000371
372 size++;
373
374 buffer = new char[size];
375
376 ret = fl_utf8froma(buffer, size, str, len);
377 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000378
Pierre Ossman5803d3b2013-09-05 14:25:40 +0000379 vlog.debug("Got clipboard data (%d bytes)", strlen(buffer));
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000380
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000381 // RFB doesn't have separate selection and clipboard concepts, so we
382 // dump the data into both variants.
383 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000384 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000385
386 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000387}
388
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100389void CConn::dataRect(const Rect& r, int encoding)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000390{
391 sock->inStream().startTiming();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000392
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100393 if (encoding != encodingCopyRect)
394 lastServerEncoding = encoding;
395
396 if (!Decoder::supported(encoding)) {
397 fprintf(stderr, "Unknown rect encoding %d\n", encoding);
398 throw Exception("Unknown rect encoding");
399 }
400
401 if (!decoders[encoding]) {
Pierre Ossman668468b2014-01-31 12:37:32 +0100402 decoders[encoding] = Decoder::createDecoder(encoding, this);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100403 if (!decoders[encoding]) {
404 fprintf(stderr, "Unknown rect encoding %d\n", encoding);
405 throw Exception("Unknown rect encoding");
406 }
407 }
408 decoders[encoding]->readRect(r, this);
409
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000410 sock->inStream().stopTiming();
411}
412
413void CConn::fillRect(const rfb::Rect& r, rfb::Pixel p)
414{
415 desktop->fillRect(r,p);
416}
Pierre Ossman0dff4b82014-01-27 16:17:21 +0100417
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000418void CConn::imageRect(const rfb::Rect& r, void* p)
419{
420 desktop->imageRect(r,p);
421}
Pierre Ossman0dff4b82014-01-27 16:17:21 +0100422
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000423void CConn::copyRect(const rfb::Rect& r, int sx, int sy)
424{
425 desktop->copyRect(r,sx,sy);
426}
Pierre Ossman0dff4b82014-01-27 16:17:21 +0100427
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000428void CConn::setCursor(int width, int height, const Point& hotspot,
429 void* data, void* mask)
430{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000431 desktop->setCursor(width, height, hotspot, data, mask);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000432}
433
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000434void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
435{
436 CMsgHandler::fence(flags, len, data);
437
438 if (flags & fenceFlagRequest) {
439 // We handle everything synchronously so we trivially honor these modes
440 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
441
442 writer()->writeFence(flags, len, data);
443 return;
444 }
445
446 if (len == 0) {
447 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000448 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000449 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000450
451 if (cp.supportsContinuousUpdates) {
452 vlog.info(_("Enabling continuous updates"));
453 continuousUpdates = true;
454 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
455 }
456 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000457 } else {
458 // Pixel format change
459 rdr::MemInStream memStream(data, len);
460 PixelFormat pf;
461
462 pf.read(&memStream);
463
464 desktop->setServerPF(pf);
465 cp.setPF(pf);
466 }
467}
468
Pierre Ossman945cdda2014-01-28 14:13:12 +0100469rdr::U8* CConn::getRawBufferRW(const rfb::Rect& r, int* stride) {
470 return desktop->getBufferRW(r, stride);
DRC33c15e32011-11-03 18:49:21 +0000471}
Pierre Ossman945cdda2014-01-28 14:13:12 +0100472void CConn::releaseRawBuffer(const rfb::Rect& r) {
Pierre Ossmana32040d2014-02-06 16:31:10 +0100473 desktop->commitBufferRW(r);
DRC33c15e32011-11-03 18:49:21 +0000474}
475
476
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000477////////////////////// Internal methods //////////////////////
478
479void CConn::resizeFramebuffer()
480{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000481 if (!desktop)
482 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000483
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000484 if (continuousUpdates)
485 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
486
Pierre Ossman6455d852011-06-01 09:33:00 +0000487 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000488}
489
490// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
491// to the connection speed:
492//
493// First we wait for at least one second of bandwidth measurement.
494//
495// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
496// which should be perceptually lossless.
497//
498// If the bandwidth is below that, we choose a more lossy JPEG quality.
499//
500// If the bandwidth drops below 256 Kbps, we switch to palette mode.
501//
502// Note: The system here is fairly arbitrary and should be replaced
503// with something more intelligent at the server end.
504//
505void CConn::autoSelectFormatAndEncoding()
506{
507 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
508 unsigned int timeWaited = sock->inStream().timeWaited();
509 bool newFullColour = fullColour;
510 int newQualityLevel = qualityLevel;
511
512 // Always use Tight
513 if (currentEncoding != encodingTight) {
514 currentEncoding = encodingTight;
515 encodingChange = true;
516 }
517
518 // Check that we have a decent bandwidth measurement
519 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
520 return;
521
522 // Select appropriate quality level
523 if (!noJpeg) {
524 if (kbitsPerSecond > 16000)
525 newQualityLevel = 8;
526 else
527 newQualityLevel = 6;
528
529 if (newQualityLevel != qualityLevel) {
530 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
531 kbitsPerSecond, newQualityLevel);
532 cp.qualityLevel = newQualityLevel;
533 qualityLevel.setParam(newQualityLevel);
534 encodingChange = true;
535 }
536 }
537
538 if (cp.beforeVersion(3, 8)) {
539 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
540 // cursors "asynchronously". If this happens in the middle of a
541 // pixel format change, the server will encode the cursor with
542 // the old format, but the client will try to decode it
543 // according to the new format. This will lead to a
544 // crash. Therefore, we do not allow automatic format change for
545 // old servers.
546 return;
547 }
548
549 // Select best color level
550 newFullColour = (kbitsPerSecond > 256);
551 if (newFullColour != fullColour) {
552 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
553 kbitsPerSecond,
554 newFullColour ? _("enabled") : _("disabled"));
555 fullColour.setParam(newFullColour);
556 formatChange = true;
557 }
558}
559
560// checkEncodings() sends a setEncodings message if one is needed.
561void CConn::checkEncodings()
562{
563 if (encodingChange && writer()) {
564 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
565 writer()->writeSetEncodings(currentEncoding, true);
566 encodingChange = false;
567 }
568}
569
570// requestNewUpdate() requests an update from the server, having set the
571// format and encoding appropriately.
572void CConn::requestNewUpdate()
573{
574 if (formatChange) {
575 PixelFormat pf;
576
577 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000578 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000579
580 if (fullColour) {
581 pf = fullColourPF;
582 } else {
583 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000584 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000585 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000586 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000587 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000588 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000589 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000590
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000591 if (supportsSyncFence) {
592 // We let the fence carry the pixel format and switch once we
593 // get the response back. That way we will be synchronised with
594 // when the server switches.
595 rdr::MemOutStream memStream;
596
597 pf.write(&memStream);
598
599 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
600 memStream.length(), (const char*)memStream.data());
601 } else {
602 // New requests are sent out at the start of processing the last
603 // one, so we cannot switch our internal format right now (doing so
604 // would mean misdecoding the current update).
605 pendingPFChange = true;
606 pendingPF = pf;
607 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000608
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000609 char str[256];
610 pf.print(str, 256);
611 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000612 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000613
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000614 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000615 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000616
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000617 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000618
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000619 if (forceNonincremental || !continuousUpdates) {
620 pendingUpdate = true;
621 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
622 !forceNonincremental);
623 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000624
625 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000626}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000627
628void CConn::handleOptions(void *data)
629{
630 CConn *self = (CConn*)data;
631
632 // Checking all the details of the current set of encodings is just
633 // a pain. Assume something has changed, as resending the encoding
634 // list is cheap. Avoid overriding what the auto logic has selected
635 // though.
636 if (!autoSelect) {
637 int encNum = encodingNum(preferredEncoding);
638
639 if (encNum != -1)
640 self->currentEncoding = encNum;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000641 }
642
Pierre Ossmanda389562011-06-09 08:24:37 +0000643 self->cp.supportsLocalCursor = true;
644
Pierre Ossmana22459d2014-03-17 14:42:10 +0100645 if (customCompressLevel)
646 self->cp.compressLevel = compressLevel;
647 else
648 self->cp.compressLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000649
Pierre Ossmana22459d2014-03-17 14:42:10 +0100650 if (!noJpeg && !autoSelect)
651 self->cp.qualityLevel = qualityLevel;
652 else
653 self->cp.qualityLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000654
655 self->encodingChange = true;
656
657 // Format changes refreshes the entire screen though and are therefore
658 // very costly. It's probably worth the effort to see if it is necessary
659 // here.
660 PixelFormat pf;
661
662 if (fullColour) {
663 pf = self->fullColourPF;
664 } else {
665 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000666 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000667 else if (lowColourLevel == 1)
668 pf = lowColourPF;
669 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000670 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000671 }
672
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000673 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000674 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000675
676 // Without fences, we cannot safely trigger an update request directly
677 // but must wait for the next update to arrive.
678 if (self->supportsSyncFence)
679 self->requestNewUpdate();
680 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000681}