blob: 166597e5e278beb064c3d6a93402f4756a7f05d6 [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>
Pierre Ossman0068a4f2015-11-09 15:48:19 +010031#include <rfb/CSecurity.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000032#include <rfb/Hostname.h>
33#include <rfb/LogWriter.h>
Pierre Ossman0068a4f2015-11-09 15:48:19 +010034#include <rfb/Security.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000035#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>
Pierre Ossman5d055462018-05-03 14:04:38 +020042#ifndef WIN32
43#include <network/UnixSocket.h>
44#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +000045
46#include <FL/Fl.H>
47#include <FL/fl_ask.H>
48
49#include "CConn.h"
Pierre Ossmanf4f30942011-05-17 09:39:07 +000050#include "OptionsDialog.h"
Pierre Ossman947b48d2014-01-27 16:52:35 +010051#include "DesktopWindow.h"
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +010052#include "PlatformPixelBuffer.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000053#include "i18n.h"
54#include "parameters.h"
Pierre Ossman39ceb502011-07-12 15:54:25 +000055#include "vncviewer.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000056
DRCb65bb932011-06-24 03:17:00 +000057#ifdef WIN32
58#include "win32.h"
59#endif
60
Pierre Ossman5156d5e2011-03-09 09:42:34 +000061using namespace rdr;
62using namespace rfb;
63using namespace std;
64
Pierre Ossman5156d5e2011-03-09 09:42:34 +000065static rfb::LogWriter vlog("CConn");
66
Pierre Ossmancf836f22011-07-14 14:34:09 +000067// 8 colours (1 bit per component)
68static const PixelFormat verylowColourPF(8, 3,false, true,
69 1, 1, 1, 2, 1, 0);
70// 64 colours (2 bits per component)
71static const PixelFormat lowColourPF(8, 6, false, true,
72 3, 3, 3, 4, 2, 0);
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010073// 256 colours (2-3 bits per component)
74static const PixelFormat mediumColourPF(8, 8, false, true,
75 7, 7, 3, 5, 2, 0);
Pierre Ossmanf4f30942011-05-17 09:39:07 +000076
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000077CConn::CConn(const char* vncServerName, network::Socket* socket=NULL)
78 : serverHost(0), serverPort(0), desktop(NULL),
Pierre Ossmanfecf0a42018-03-26 12:22:47 +020079 updateCount(0), pixelCount(0), pendingPFChange(false),
Pierre Ossman5156d5e2011-03-09 09:42:34 +000080 currentEncoding(encodingTight), lastServerEncoding((unsigned int)-1),
81 formatChange(false), encodingChange(false),
Pierre Ossmanaa73c892011-11-15 12:13:37 +000082 firstUpdate(true), pendingUpdate(false), continuousUpdates(false),
Pierre Ossmane28bdb22011-11-14 16:02:06 +000083 forceNonincremental(true), supportsSyncFence(false)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000084{
85 setShared(::shared);
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000086 sock = socket;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000087
88 int encNum = encodingNum(preferredEncoding);
89 if (encNum != -1)
90 currentEncoding = encNum;
91
Pierre Ossmanda389562011-06-09 08:24:37 +000092 cp.supportsLocalCursor = true;
93
Pierre Ossman5156d5e2011-03-09 09:42:34 +000094 cp.supportsDesktopResize = true;
95 cp.supportsExtendedDesktopSize = true;
96 cp.supportsDesktopRename = true;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000097
Pierre Ossman2fa63f82016-12-05 15:26:21 +010098 cp.supportsLEDState = true;
99
Pierre Ossmana22459d2014-03-17 14:42:10 +0100100 if (customCompressLevel)
101 cp.compressLevel = compressLevel;
102 else
103 cp.compressLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000104
Pierre Ossmana22459d2014-03-17 14:42:10 +0100105 if (!noJpeg)
106 cp.qualityLevel = qualityLevel;
107 else
108 cp.qualityLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000109
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000110 if(sock == NULL) {
111 try {
Pierre Ossman5d055462018-05-03 14:04:38 +0200112#ifndef WIN32
113 if (strchr(vncServerName, '/') != NULL) {
114 sock = new network::UnixSocket(vncServerName);
115 serverHost = sock->getPeerAddress();
116 vlog.info(_("connected to socket %s"), serverHost);
117 } else
118#endif
119 {
120 getHostAndPort(vncServerName, &serverHost, &serverPort);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000121
Pierre Ossman5d055462018-05-03 14:04:38 +0200122 sock = new network::TcpSocket(serverHost, serverPort);
123 vlog.info(_("connected to host %s port %d"), serverHost, serverPort);
124 }
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000125 } catch (rdr::Exception& e) {
126 vlog.error("%s", e.str());
Dr. David Alan Gilbertf4d1d892017-07-11 12:11:50 +0100127 if (alertOnFatalError)
128 fl_alert("%s", e.str());
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000129 exit_vncviewer();
130 return;
131 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000132 }
133
134 Fl::add_fd(sock->getFd(), FL_READ | FL_EXCEPT, socketEvent, this);
135
136 // See callback below
137 sock->inStream().setBlockCallback(this);
138
139 setServerName(serverHost);
140 setStreams(&sock->inStream(), &sock->outStream());
141
142 initialiseProtocol();
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000143
144 OptionsDialog::addCallback(handleOptions, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000145}
146
147CConn::~CConn()
148{
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000149 OptionsDialog::removeCallback(handleOptions);
Pierre Ossmand9b90032015-09-23 12:18:52 +0200150 Fl::remove_timeout(handleUpdateTimeout, this);
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000151
Pierre Ossman6a9e2e62011-05-19 14:47:43 +0000152 if (desktop)
153 delete desktop;
154
Pierre Ossman5e04c262011-11-09 11:31:12 +0000155 delete [] serverHost;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000156 if (sock)
157 Fl::remove_fd(sock->getFd());
158 delete sock;
159}
160
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000161void CConn::refreshFramebuffer()
162{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000163 forceNonincremental = true;
164
165 // Without fences, we cannot safely trigger an update request directly
166 // but must wait for the next update to arrive.
167 if (supportsSyncFence)
168 requestNewUpdate();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000169}
170
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000171const char *CConn::connectionInfo()
172{
173 static char infoText[1024] = "";
174
Pierre Ossmanb2046432014-09-22 11:17:34 +0200175 char scratch[100];
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000176 char pfStr[100];
Pierre Ossmanb2046432014-09-22 11:17:34 +0200177
178 // Crude way of avoiding constant overflow checks
179 assert((sizeof(scratch) + 1) * 10 < sizeof(infoText));
180
181 infoText[0] = '\0';
182
183 snprintf(scratch, sizeof(scratch),
184 _("Desktop name: %.80s"), cp.name());
185 strcat(infoText, scratch);
186 strcat(infoText, "\n");
187
188 snprintf(scratch, sizeof(scratch),
189 _("Host: %.80s port: %d"), serverHost, serverPort);
190 strcat(infoText, scratch);
191 strcat(infoText, "\n");
192
193 snprintf(scratch, sizeof(scratch),
194 _("Size: %d x %d"), cp.width, cp.height);
195 strcat(infoText, scratch);
196 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000197
Pierre Ossman744e55c2014-12-03 14:00:54 +0100198 // TRANSLATORS: Will be filled in with a string describing the
199 // protocol pixel format in a fairly language neutral way
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000200 cp.pf().print(pfStr, 100);
Pierre Ossmanb2046432014-09-22 11:17:34 +0200201 snprintf(scratch, sizeof(scratch),
202 _("Pixel format: %s"), pfStr);
203 strcat(infoText, scratch);
204 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000205
Pierre Ossman744e55c2014-12-03 14:00:54 +0100206 // TRANSLATORS: Similar to the earlier "Pixel format" string
Pierre Ossmanb2046432014-09-22 11:17:34 +0200207 serverPF.print(pfStr, 100);
208 snprintf(scratch, sizeof(scratch),
209 _("(server default %s)"), pfStr);
210 strcat(infoText, scratch);
211 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000212
Pierre Ossmanb2046432014-09-22 11:17:34 +0200213 snprintf(scratch, sizeof(scratch),
214 _("Requested encoding: %s"), encodingName(currentEncoding));
215 strcat(infoText, scratch);
216 strcat(infoText, "\n");
217
218 snprintf(scratch, sizeof(scratch),
219 _("Last used encoding: %s"), encodingName(lastServerEncoding));
220 strcat(infoText, scratch);
221 strcat(infoText, "\n");
222
223 snprintf(scratch, sizeof(scratch),
224 _("Line speed estimate: %d kbit/s"), sock->inStream().kbitsPerSecond());
225 strcat(infoText, scratch);
226 strcat(infoText, "\n");
227
228 snprintf(scratch, sizeof(scratch),
229 _("Protocol version: %d.%d"), cp.majorVersion, cp.minorVersion);
230 strcat(infoText, scratch);
231 strcat(infoText, "\n");
232
233 snprintf(scratch, sizeof(scratch),
234 _("Security method: %s"), secTypeName(csecurity->getType()));
235 strcat(infoText, scratch);
236 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000237
238 return infoText;
239}
240
Pierre Ossmanfecf0a42018-03-26 12:22:47 +0200241unsigned CConn::getUpdateCount()
Pierre Ossman921f6c82017-02-24 12:33:09 +0100242{
Pierre Ossmanfecf0a42018-03-26 12:22:47 +0200243 return updateCount;
Pierre Ossman921f6c82017-02-24 12:33:09 +0100244}
245
246unsigned CConn::getPixelCount()
247{
248 return pixelCount;
249}
250
251unsigned CConn::getPosition()
252{
253 return sock->inStream().pos();
254}
255
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000256// The RFB core is not properly asynchronous, so it calls this callback
257// whenever it needs to block to wait for more data. Since FLTK is
258// monitoring the socket, we just make sure FLTK gets to run.
259
260void CConn::blockCallback()
261{
Pierre Ossman5102fa92015-11-18 16:23:21 +0100262 run_mainloop();
Pierre Ossman1db73242015-09-23 12:20:32 +0200263
264 if (should_exit())
265 throw rdr::Exception("Termination requested");
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000266}
267
DRC3e7ed812013-02-26 10:34:22 +0000268void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000269{
270 CConn *cc;
271 static bool recursing = false;
272
273 assert(data);
274 cc = (CConn*)data;
275
276 // I don't think processMsg() is recursion safe, so add this check
277 if (recursing)
278 return;
279
280 recursing = true;
281
282 try {
283 // processMsg() only processes one message, so we need to loop
284 // until the buffers are empty or things will stall.
285 do {
286 cc->processMsg();
Pierre Ossman9a73adf2016-12-29 16:52:56 +0100287
288 // Make sure that the FLTK handling and the timers gets some CPU
289 // time in case of back to back messages
290 Fl::check();
291 Timer::checkTimeouts();
292
293 // Also check if we need to stop reading and terminate
294 if (should_exit())
295 break;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000296 } while (cc->sock->inStream().checkNoWait(1));
297 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000298 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000299 exit_vncviewer();
300 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000301 vlog.error("%s", e.str());
Pierre Ossman1db73242015-09-23 12:20:32 +0200302 // Somebody might already have requested us to terminate, and
303 // might have already provided an error message.
304 if (!should_exit())
305 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000306 }
307
308 recursing = false;
309}
310
311////////////////////// CConnection callback methods //////////////////////
312
313// serverInit() is called when the serverInit message has been received. At
314// this point we create the desktop window and display it. We also tell the
315// server the pixel format and encodings to use and request the first update.
316void CConn::serverInit()
317{
318 CConnection::serverInit();
319
320 // If using AutoSelect with old servers, start in FullColor
321 // mode. See comment in autoSelectFormatAndEncoding.
322 if (cp.beforeVersion(3, 8) && autoSelect)
323 fullColour.setParam(true);
324
325 serverPF = cp.pf();
326
327 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
328 fullColourPF = desktop->getPreferredPF();
329
Pierre Ossman5d512c32011-11-04 11:42:16 +0000330 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000331 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000332
333 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000334 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000335
336 // This initial update request is a bit of a corner case, so we need
337 // to help out setting the correct format here.
338 assert(pendingPFChange);
Pierre Ossman5d512c32011-11-04 11:42:16 +0000339 cp.setPF(pendingPF);
340 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000341}
342
343// setDesktopSize() is called when the desktop size changes (including when
344// it is set initially).
345void CConn::setDesktopSize(int w, int h)
346{
347 CConnection::setDesktopSize(w,h);
348 resizeFramebuffer();
349}
350
351// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
Pierre Ossman28c1d542015-03-03 16:27:44 +0100352void CConn::setExtendedDesktopSize(unsigned reason, unsigned result,
353 int w, int h, const rfb::ScreenSet& layout)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000354{
355 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
356
357 if ((reason == reasonClient) && (result != resultSuccess)) {
358 vlog.error(_("SetDesktopSize failed: %d"), result);
359 return;
360 }
361
362 resizeFramebuffer();
363}
364
365// setName() is called when the desktop name changes
366void CConn::setName(const char* name)
367{
368 CConnection::setName(name);
369 if (desktop)
370 desktop->setName(name);
371}
372
373// framebufferUpdateStart() is called at the beginning of an update.
374// Here we try to send out a new framebuffer update request so that the
375// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000376// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000377void CConn::framebufferUpdateStart()
378{
Pierre Ossman3da238d2015-11-12 12:20:05 +0100379 CConnection::framebufferUpdateStart();
380
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000381 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000382 pendingUpdate = false;
383
384 requestNewUpdate();
Pierre Ossmand9b90032015-09-23 12:18:52 +0200385
386 // Update the screen prematurely for very slow updates
387 Fl::add_timeout(1.0, handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000388}
389
390// framebufferUpdateEnd() is called at the end of an update.
391// For each rectangle, the FdInStream will have timed the speed
392// of the connection, allowing us to select format and encoding
393// appropriately, and then request another incremental update.
394void CConn::framebufferUpdateEnd()
395{
Pierre Ossman3da238d2015-11-12 12:20:05 +0100396 CConnection::framebufferUpdateEnd();
397
Pierre Ossmanfecf0a42018-03-26 12:22:47 +0200398 updateCount++;
Pierre Ossman921f6c82017-02-24 12:33:09 +0100399
Pierre Ossmand9b90032015-09-23 12:18:52 +0200400 Fl::remove_timeout(handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000401 desktop->updateWindow();
402
403 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000404 // We need fences to make extra update requests and continuous
405 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000406 if (cp.supportsFence)
407 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
408
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000409 firstUpdate = false;
410 }
411
Pierre Ossman5d512c32011-11-04 11:42:16 +0000412 // A format change has been scheduled and we are now past the update
413 // with the old format. Time to active the new one.
414 if (pendingPFChange) {
Pierre Ossman5d512c32011-11-04 11:42:16 +0000415 cp.setPF(pendingPF);
416 pendingPFChange = false;
417 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000418
419 // Compute new settings based on updated bandwidth values
420 if (autoSelect)
421 autoSelectFormatAndEncoding();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000422}
423
424// The rest of the callbacks are fairly self-explanatory...
425
426void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
427{
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200428 vlog.error(_("Invalid SetColourMapEntries from server!"));
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000429}
430
431void CConn::bell()
432{
433 fl_beep();
434}
435
436void CConn::serverCutText(const char* str, rdr::U32 len)
437{
Pierre Ossman4c204232018-03-26 13:32:49 +0200438 desktop->serverCutText(str, len);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000439}
440
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100441void CConn::dataRect(const Rect& r, int encoding)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000442{
443 sock->inStream().startTiming();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000444
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100445 if (encoding != encodingCopyRect)
446 lastServerEncoding = encoding;
447
Pierre Ossman9f273e92015-11-09 16:34:54 +0100448 CConnection::dataRect(r, encoding);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100449
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000450 sock->inStream().stopTiming();
Pierre Ossman921f6c82017-02-24 12:33:09 +0100451
452 pixelCount += r.area();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000453}
454
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000455void CConn::setCursor(int width, int height, const Point& hotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100456 const rdr::U8* data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000457{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100458 desktop->setCursor(width, height, hotspot, data);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000459}
460
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000461void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
462{
463 CMsgHandler::fence(flags, len, data);
464
465 if (flags & fenceFlagRequest) {
466 // We handle everything synchronously so we trivially honor these modes
467 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
468
469 writer()->writeFence(flags, len, data);
470 return;
471 }
472
473 if (len == 0) {
474 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000475 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000476 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000477
478 if (cp.supportsContinuousUpdates) {
479 vlog.info(_("Enabling continuous updates"));
480 continuousUpdates = true;
481 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
482 }
483 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000484 } else {
485 // Pixel format change
486 rdr::MemInStream memStream(data, len);
487 PixelFormat pf;
488
489 pf.read(&memStream);
490
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000491 cp.setPF(pf);
492 }
493}
494
Pierre Ossman2fa63f82016-12-05 15:26:21 +0100495void CConn::setLEDState(unsigned int state)
496{
497 CConnection::setLEDState(state);
498
499 desktop->setLEDState(state);
500}
501
DRC33c15e32011-11-03 18:49:21 +0000502
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000503////////////////////// Internal methods //////////////////////
504
505void CConn::resizeFramebuffer()
506{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000507 if (!desktop)
508 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000509
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000510 if (continuousUpdates)
511 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
512
Pierre Ossman6455d852011-06-01 09:33:00 +0000513 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000514}
515
516// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
517// to the connection speed:
518//
519// First we wait for at least one second of bandwidth measurement.
520//
521// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
522// which should be perceptually lossless.
523//
524// If the bandwidth is below that, we choose a more lossy JPEG quality.
525//
526// If the bandwidth drops below 256 Kbps, we switch to palette mode.
527//
528// Note: The system here is fairly arbitrary and should be replaced
529// with something more intelligent at the server end.
530//
531void CConn::autoSelectFormatAndEncoding()
532{
533 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
534 unsigned int timeWaited = sock->inStream().timeWaited();
535 bool newFullColour = fullColour;
536 int newQualityLevel = qualityLevel;
537
538 // Always use Tight
539 if (currentEncoding != encodingTight) {
540 currentEncoding = encodingTight;
541 encodingChange = true;
542 }
543
544 // Check that we have a decent bandwidth measurement
545 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
546 return;
547
548 // Select appropriate quality level
549 if (!noJpeg) {
550 if (kbitsPerSecond > 16000)
551 newQualityLevel = 8;
552 else
553 newQualityLevel = 6;
554
555 if (newQualityLevel != qualityLevel) {
556 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
557 kbitsPerSecond, newQualityLevel);
558 cp.qualityLevel = newQualityLevel;
559 qualityLevel.setParam(newQualityLevel);
560 encodingChange = true;
561 }
562 }
563
564 if (cp.beforeVersion(3, 8)) {
565 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
566 // cursors "asynchronously". If this happens in the middle of a
567 // pixel format change, the server will encode the cursor with
568 // the old format, but the client will try to decode it
569 // according to the new format. This will lead to a
570 // crash. Therefore, we do not allow automatic format change for
571 // old servers.
572 return;
573 }
574
575 // Select best color level
576 newFullColour = (kbitsPerSecond > 256);
577 if (newFullColour != fullColour) {
578 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
579 kbitsPerSecond,
580 newFullColour ? _("enabled") : _("disabled"));
581 fullColour.setParam(newFullColour);
582 formatChange = true;
583 }
584}
585
586// checkEncodings() sends a setEncodings message if one is needed.
587void CConn::checkEncodings()
588{
589 if (encodingChange && writer()) {
590 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
591 writer()->writeSetEncodings(currentEncoding, true);
592 encodingChange = false;
593 }
594}
595
596// requestNewUpdate() requests an update from the server, having set the
597// format and encoding appropriately.
598void CConn::requestNewUpdate()
599{
600 if (formatChange) {
601 PixelFormat pf;
602
603 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000604 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000605
606 if (fullColour) {
607 pf = fullColourPF;
608 } else {
609 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000610 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000611 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000612 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000613 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000614 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000615 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000616
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000617 if (supportsSyncFence) {
618 // We let the fence carry the pixel format and switch once we
619 // get the response back. That way we will be synchronised with
620 // when the server switches.
621 rdr::MemOutStream memStream;
622
623 pf.write(&memStream);
624
625 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
626 memStream.length(), (const char*)memStream.data());
627 } else {
628 // New requests are sent out at the start of processing the last
629 // one, so we cannot switch our internal format right now (doing so
630 // would mean misdecoding the current update).
631 pendingPFChange = true;
632 pendingPF = pf;
633 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000634
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000635 char str[256];
636 pf.print(str, 256);
637 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000638 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000639
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000640 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000641 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000642
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000643 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000644
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000645 if (forceNonincremental || !continuousUpdates) {
646 pendingUpdate = true;
647 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
648 !forceNonincremental);
649 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000650
651 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000652}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000653
654void CConn::handleOptions(void *data)
655{
656 CConn *self = (CConn*)data;
657
658 // Checking all the details of the current set of encodings is just
659 // a pain. Assume something has changed, as resending the encoding
660 // list is cheap. Avoid overriding what the auto logic has selected
661 // though.
662 if (!autoSelect) {
663 int encNum = encodingNum(preferredEncoding);
664
665 if (encNum != -1)
666 self->currentEncoding = encNum;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000667 }
668
Pierre Ossmanda389562011-06-09 08:24:37 +0000669 self->cp.supportsLocalCursor = true;
670
Pierre Ossmana22459d2014-03-17 14:42:10 +0100671 if (customCompressLevel)
672 self->cp.compressLevel = compressLevel;
673 else
674 self->cp.compressLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000675
Pierre Ossmana22459d2014-03-17 14:42:10 +0100676 if (!noJpeg && !autoSelect)
677 self->cp.qualityLevel = qualityLevel;
678 else
679 self->cp.qualityLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000680
681 self->encodingChange = true;
682
683 // Format changes refreshes the entire screen though and are therefore
684 // very costly. It's probably worth the effort to see if it is necessary
685 // here.
686 PixelFormat pf;
687
688 if (fullColour) {
689 pf = self->fullColourPF;
690 } else {
691 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000692 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000693 else if (lowColourLevel == 1)
694 pf = lowColourPF;
695 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000696 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000697 }
698
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000699 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000700 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000701
702 // Without fences, we cannot safely trigger an update request directly
703 // but must wait for the next update to arrive.
704 if (self->supportsSyncFence)
705 self->requestNewUpdate();
706 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000707}
Pierre Ossmand9b90032015-09-23 12:18:52 +0200708
709void CConn::handleUpdateTimeout(void *data)
710{
711 CConn *self = (CConn *)data;
712
713 assert(self);
714
715 self->desktop->updateWindow();
716
717 Fl::repeat_timeout(1.0, handleUpdateTimeout, data);
718}