blob: f8b45af2003aa6337343fe588acb67a7f05a6dce [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 Ossman5c23b9e2015-03-03 16:26:03 +0100137 for (size_t i = 0; i < sizeof(decoders)/sizeof(decoders[0]); i++)
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100138 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
Pierre Ossmanb2046432014-09-22 11:17:34 +0200163 char scratch[100];
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000164 char pfStr[100];
Pierre Ossmanb2046432014-09-22 11:17:34 +0200165
166 // Crude way of avoiding constant overflow checks
167 assert((sizeof(scratch) + 1) * 10 < sizeof(infoText));
168
169 infoText[0] = '\0';
170
171 snprintf(scratch, sizeof(scratch),
172 _("Desktop name: %.80s"), cp.name());
173 strcat(infoText, scratch);
174 strcat(infoText, "\n");
175
176 snprintf(scratch, sizeof(scratch),
177 _("Host: %.80s port: %d"), serverHost, serverPort);
178 strcat(infoText, scratch);
179 strcat(infoText, "\n");
180
181 snprintf(scratch, sizeof(scratch),
182 _("Size: %d x %d"), cp.width, cp.height);
183 strcat(infoText, scratch);
184 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000185
Pierre Ossman744e55c2014-12-03 14:00:54 +0100186 // TRANSLATORS: Will be filled in with a string describing the
187 // protocol pixel format in a fairly language neutral way
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000188 cp.pf().print(pfStr, 100);
Pierre Ossmanb2046432014-09-22 11:17:34 +0200189 snprintf(scratch, sizeof(scratch),
190 _("Pixel format: %s"), pfStr);
191 strcat(infoText, scratch);
192 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000193
Pierre Ossman744e55c2014-12-03 14:00:54 +0100194 // TRANSLATORS: Similar to the earlier "Pixel format" string
Pierre Ossmanb2046432014-09-22 11:17:34 +0200195 serverPF.print(pfStr, 100);
196 snprintf(scratch, sizeof(scratch),
197 _("(server default %s)"), pfStr);
198 strcat(infoText, scratch);
199 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000200
Pierre Ossmanb2046432014-09-22 11:17:34 +0200201 snprintf(scratch, sizeof(scratch),
202 _("Requested encoding: %s"), encodingName(currentEncoding));
203 strcat(infoText, scratch);
204 strcat(infoText, "\n");
205
206 snprintf(scratch, sizeof(scratch),
207 _("Last used encoding: %s"), encodingName(lastServerEncoding));
208 strcat(infoText, scratch);
209 strcat(infoText, "\n");
210
211 snprintf(scratch, sizeof(scratch),
212 _("Line speed estimate: %d kbit/s"), sock->inStream().kbitsPerSecond());
213 strcat(infoText, scratch);
214 strcat(infoText, "\n");
215
216 snprintf(scratch, sizeof(scratch),
217 _("Protocol version: %d.%d"), cp.majorVersion, cp.minorVersion);
218 strcat(infoText, scratch);
219 strcat(infoText, "\n");
220
221 snprintf(scratch, sizeof(scratch),
222 _("Security method: %s"), secTypeName(csecurity->getType()));
223 strcat(infoText, scratch);
224 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000225
226 return infoText;
227}
228
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000229// The RFB core is not properly asynchronous, so it calls this callback
230// whenever it needs to block to wait for more data. Since FLTK is
231// monitoring the socket, we just make sure FLTK gets to run.
232
233void CConn::blockCallback()
234{
235 int next_timer;
236
237 next_timer = Timer::checkTimeouts();
238 if (next_timer == 0)
239 next_timer = INT_MAX;
240
241 Fl::wait((double)next_timer / 1000.0);
242}
243
DRC3e7ed812013-02-26 10:34:22 +0000244void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000245{
246 CConn *cc;
247 static bool recursing = false;
248
249 assert(data);
250 cc = (CConn*)data;
251
252 // I don't think processMsg() is recursion safe, so add this check
253 if (recursing)
254 return;
255
256 recursing = true;
257
258 try {
259 // processMsg() only processes one message, so we need to loop
260 // until the buffers are empty or things will stall.
261 do {
262 cc->processMsg();
263 } while (cc->sock->inStream().checkNoWait(1));
264 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000265 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000266 exit_vncviewer();
267 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000268 vlog.error("%s", e.str());
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000269 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000270 }
271
272 recursing = false;
273}
274
275////////////////////// CConnection callback methods //////////////////////
276
277// serverInit() is called when the serverInit message has been received. At
278// this point we create the desktop window and display it. We also tell the
279// server the pixel format and encodings to use and request the first update.
280void CConn::serverInit()
281{
282 CConnection::serverInit();
283
284 // If using AutoSelect with old servers, start in FullColor
285 // mode. See comment in autoSelectFormatAndEncoding.
286 if (cp.beforeVersion(3, 8) && autoSelect)
287 fullColour.setParam(true);
288
289 serverPF = cp.pf();
290
291 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
292 fullColourPF = desktop->getPreferredPF();
293
Pierre Ossman5d512c32011-11-04 11:42:16 +0000294 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000295 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000296
297 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000298 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000299
300 // This initial update request is a bit of a corner case, so we need
301 // to help out setting the correct format here.
302 assert(pendingPFChange);
Pierre Ossman5d512c32011-11-04 11:42:16 +0000303 cp.setPF(pendingPF);
304 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000305}
306
307// setDesktopSize() is called when the desktop size changes (including when
308// it is set initially).
309void CConn::setDesktopSize(int w, int h)
310{
311 CConnection::setDesktopSize(w,h);
312 resizeFramebuffer();
313}
314
315// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
Pierre Ossman28c1d542015-03-03 16:27:44 +0100316void CConn::setExtendedDesktopSize(unsigned reason, unsigned result,
317 int w, int h, const rfb::ScreenSet& layout)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000318{
319 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
320
321 if ((reason == reasonClient) && (result != resultSuccess)) {
322 vlog.error(_("SetDesktopSize failed: %d"), result);
323 return;
324 }
325
326 resizeFramebuffer();
327}
328
329// setName() is called when the desktop name changes
330void CConn::setName(const char* name)
331{
332 CConnection::setName(name);
333 if (desktop)
334 desktop->setName(name);
335}
336
337// framebufferUpdateStart() is called at the beginning of an update.
338// Here we try to send out a new framebuffer update request so that the
339// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000340// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000341void CConn::framebufferUpdateStart()
342{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000343 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000344 pendingUpdate = false;
345
346 requestNewUpdate();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000347}
348
349// framebufferUpdateEnd() is called at the end of an update.
350// For each rectangle, the FdInStream will have timed the speed
351// of the connection, allowing us to select format and encoding
352// appropriately, and then request another incremental update.
353void CConn::framebufferUpdateEnd()
354{
355 desktop->updateWindow();
356
357 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000358 // We need fences to make extra update requests and continuous
359 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000360 if (cp.supportsFence)
361 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
362
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000363 firstUpdate = false;
364 }
365
Pierre Ossman5d512c32011-11-04 11:42:16 +0000366 // A format change has been scheduled and we are now past the update
367 // with the old format. Time to active the new one.
368 if (pendingPFChange) {
Pierre Ossman5d512c32011-11-04 11:42:16 +0000369 cp.setPF(pendingPF);
370 pendingPFChange = false;
371 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000372
373 // Compute new settings based on updated bandwidth values
374 if (autoSelect)
375 autoSelectFormatAndEncoding();
376
377 // Make sure that the FLTK handling and the timers gets some CPU time
378 // in case of back to back framebuffer updates.
379 Fl::check();
380 Timer::checkTimeouts();
381}
382
383// The rest of the callbacks are fairly self-explanatory...
384
385void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
386{
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200387 vlog.error(_("Invalid SetColourMapEntries from server!"));
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000388}
389
390void CConn::bell()
391{
392 fl_beep();
393}
394
395void CConn::serverCutText(const char* str, rdr::U32 len)
396{
Pierre Ossman689c4582011-05-26 15:39:41 +0000397 char *buffer;
398 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000399
Pierre Ossman50104882013-05-24 10:47:27 +0000400 if (!acceptClipboard)
401 return;
402
Pierre Ossman689c4582011-05-26 15:39:41 +0000403 size = fl_utf8froma(NULL, 0, str, len);
404 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000405 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000406
407 size++;
408
409 buffer = new char[size];
410
411 ret = fl_utf8froma(buffer, size, str, len);
412 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000413
Pierre Ossmanfb450fb2015-03-03 16:34:56 +0100414 vlog.debug("Got clipboard data (%d bytes)", (int)strlen(buffer));
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000415
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000416 // RFB doesn't have separate selection and clipboard concepts, so we
417 // dump the data into both variants.
418 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000419 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000420
421 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000422}
423
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100424void CConn::dataRect(const Rect& r, int encoding)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000425{
426 sock->inStream().startTiming();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000427
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100428 if (encoding != encodingCopyRect)
429 lastServerEncoding = encoding;
430
431 if (!Decoder::supported(encoding)) {
Pierre Ossman744e55c2014-12-03 14:00:54 +0100432 // TRANSLATORS: Refers to a VNC protocol encoding type
Pierre Ossmanc76bd2b2014-12-03 14:01:31 +0100433 vlog.error(_("Unknown encoding %d"), encoding);
434 throw Exception(_("Unknown encoding"));
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100435 }
436
437 if (!decoders[encoding]) {
Pierre Ossman668468b2014-01-31 12:37:32 +0100438 decoders[encoding] = Decoder::createDecoder(encoding, this);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100439 if (!decoders[encoding]) {
Pierre Ossmanc76bd2b2014-12-03 14:01:31 +0100440 vlog.error(_("Unknown encoding %d"), encoding);
441 throw Exception(_("Unknown encoding"));
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100442 }
443 }
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200444 decoders[encoding]->readRect(r, desktop->getFramebuffer());
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100445
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000446 sock->inStream().stopTiming();
447}
448
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000449void CConn::setCursor(int width, int height, const Point& hotspot,
450 void* data, void* mask)
451{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000452 desktop->setCursor(width, height, hotspot, data, mask);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000453}
454
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000455void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
456{
457 CMsgHandler::fence(flags, len, data);
458
459 if (flags & fenceFlagRequest) {
460 // We handle everything synchronously so we trivially honor these modes
461 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
462
463 writer()->writeFence(flags, len, data);
464 return;
465 }
466
467 if (len == 0) {
468 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000469 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000470 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000471
472 if (cp.supportsContinuousUpdates) {
473 vlog.info(_("Enabling continuous updates"));
474 continuousUpdates = true;
475 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
476 }
477 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000478 } else {
479 // Pixel format change
480 rdr::MemInStream memStream(data, len);
481 PixelFormat pf;
482
483 pf.read(&memStream);
484
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000485 cp.setPF(pf);
486 }
487}
488
DRC33c15e32011-11-03 18:49:21 +0000489
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000490////////////////////// Internal methods //////////////////////
491
492void CConn::resizeFramebuffer()
493{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000494 if (!desktop)
495 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000496
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000497 if (continuousUpdates)
498 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
499
Pierre Ossman6455d852011-06-01 09:33:00 +0000500 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000501}
502
503// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
504// to the connection speed:
505//
506// First we wait for at least one second of bandwidth measurement.
507//
508// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
509// which should be perceptually lossless.
510//
511// If the bandwidth is below that, we choose a more lossy JPEG quality.
512//
513// If the bandwidth drops below 256 Kbps, we switch to palette mode.
514//
515// Note: The system here is fairly arbitrary and should be replaced
516// with something more intelligent at the server end.
517//
518void CConn::autoSelectFormatAndEncoding()
519{
520 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
521 unsigned int timeWaited = sock->inStream().timeWaited();
522 bool newFullColour = fullColour;
523 int newQualityLevel = qualityLevel;
524
525 // Always use Tight
526 if (currentEncoding != encodingTight) {
527 currentEncoding = encodingTight;
528 encodingChange = true;
529 }
530
531 // Check that we have a decent bandwidth measurement
532 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
533 return;
534
535 // Select appropriate quality level
536 if (!noJpeg) {
537 if (kbitsPerSecond > 16000)
538 newQualityLevel = 8;
539 else
540 newQualityLevel = 6;
541
542 if (newQualityLevel != qualityLevel) {
543 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
544 kbitsPerSecond, newQualityLevel);
545 cp.qualityLevel = newQualityLevel;
546 qualityLevel.setParam(newQualityLevel);
547 encodingChange = true;
548 }
549 }
550
551 if (cp.beforeVersion(3, 8)) {
552 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
553 // cursors "asynchronously". If this happens in the middle of a
554 // pixel format change, the server will encode the cursor with
555 // the old format, but the client will try to decode it
556 // according to the new format. This will lead to a
557 // crash. Therefore, we do not allow automatic format change for
558 // old servers.
559 return;
560 }
561
562 // Select best color level
563 newFullColour = (kbitsPerSecond > 256);
564 if (newFullColour != fullColour) {
565 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
566 kbitsPerSecond,
567 newFullColour ? _("enabled") : _("disabled"));
568 fullColour.setParam(newFullColour);
569 formatChange = true;
570 }
571}
572
573// checkEncodings() sends a setEncodings message if one is needed.
574void CConn::checkEncodings()
575{
576 if (encodingChange && writer()) {
577 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
578 writer()->writeSetEncodings(currentEncoding, true);
579 encodingChange = false;
580 }
581}
582
583// requestNewUpdate() requests an update from the server, having set the
584// format and encoding appropriately.
585void CConn::requestNewUpdate()
586{
587 if (formatChange) {
588 PixelFormat pf;
589
590 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000591 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000592
593 if (fullColour) {
594 pf = fullColourPF;
595 } else {
596 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000597 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000598 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000599 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000600 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000601 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000602 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000603
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000604 if (supportsSyncFence) {
605 // We let the fence carry the pixel format and switch once we
606 // get the response back. That way we will be synchronised with
607 // when the server switches.
608 rdr::MemOutStream memStream;
609
610 pf.write(&memStream);
611
612 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
613 memStream.length(), (const char*)memStream.data());
614 } else {
615 // New requests are sent out at the start of processing the last
616 // one, so we cannot switch our internal format right now (doing so
617 // would mean misdecoding the current update).
618 pendingPFChange = true;
619 pendingPF = pf;
620 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000621
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000622 char str[256];
623 pf.print(str, 256);
624 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000625 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000626
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000627 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000628 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000629
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000630 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000631
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000632 if (forceNonincremental || !continuousUpdates) {
633 pendingUpdate = true;
634 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
635 !forceNonincremental);
636 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000637
638 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000639}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000640
641void CConn::handleOptions(void *data)
642{
643 CConn *self = (CConn*)data;
644
645 // Checking all the details of the current set of encodings is just
646 // a pain. Assume something has changed, as resending the encoding
647 // list is cheap. Avoid overriding what the auto logic has selected
648 // though.
649 if (!autoSelect) {
650 int encNum = encodingNum(preferredEncoding);
651
652 if (encNum != -1)
653 self->currentEncoding = encNum;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000654 }
655
Pierre Ossmanda389562011-06-09 08:24:37 +0000656 self->cp.supportsLocalCursor = true;
657
Pierre Ossmana22459d2014-03-17 14:42:10 +0100658 if (customCompressLevel)
659 self->cp.compressLevel = compressLevel;
660 else
661 self->cp.compressLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000662
Pierre Ossmana22459d2014-03-17 14:42:10 +0100663 if (!noJpeg && !autoSelect)
664 self->cp.qualityLevel = qualityLevel;
665 else
666 self->cp.qualityLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000667
668 self->encodingChange = true;
669
670 // Format changes refreshes the entire screen though and are therefore
671 // very costly. It's probably worth the effort to see if it is necessary
672 // here.
673 PixelFormat pf;
674
675 if (fullColour) {
676 pf = self->fullColourPF;
677 } else {
678 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000679 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000680 else if (lowColourLevel == 1)
681 pf = lowColourPF;
682 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000683 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000684 }
685
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000686 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000687 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000688
689 // Without fences, we cannot safely trigger an update request directly
690 // but must wait for the next update to arrive.
691 if (self->supportsSyncFence)
692 self->requestNewUpdate();
693 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000694}