blob: e87ebcdef9849f3cf982d0b13674cfb40881c24a [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();
Pierre Ossman4af93a92018-10-25 10:32:38 +0200116 vlog.info(_("Connected to socket %s"), serverHost);
Pierre Ossman5d055462018-05-03 14:04:38 +0200117 } 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);
Pierre Ossman4af93a92018-10-25 10:32:38 +0200123 vlog.info(_("Connected to host %s port %d"), serverHost, serverPort);
Pierre Ossman5d055462018-05-03 14:04:38 +0200124 }
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),
Pierre Ossman9312b0e2018-06-20 12:25:14 +0200194 _("Size: %d x %d"), cp.width(), cp.height());
Pierre Ossmanb2046432014-09-22 11:17:34 +0200195 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
Pierre Ossman9312b0e2018-06-20 12:25:14 +0200327 desktop = new DesktopWindow(cp.width(), cp.height(),
328 cp.name(), serverPF, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000329 fullColourPF = desktop->getPreferredPF();
330
Pierre Ossman5d512c32011-11-04 11:42:16 +0000331 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000332 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000333
334 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000335 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000336
337 // This initial update request is a bit of a corner case, so we need
338 // to help out setting the correct format here.
339 assert(pendingPFChange);
Pierre Ossman5d512c32011-11-04 11:42:16 +0000340 cp.setPF(pendingPF);
341 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000342}
343
344// setDesktopSize() is called when the desktop size changes (including when
345// it is set initially).
346void CConn::setDesktopSize(int w, int h)
347{
348 CConnection::setDesktopSize(w,h);
349 resizeFramebuffer();
350}
351
352// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
Pierre Ossman28c1d542015-03-03 16:27:44 +0100353void CConn::setExtendedDesktopSize(unsigned reason, unsigned result,
354 int w, int h, const rfb::ScreenSet& layout)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000355{
356 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
357
358 if ((reason == reasonClient) && (result != resultSuccess)) {
359 vlog.error(_("SetDesktopSize failed: %d"), result);
360 return;
361 }
362
363 resizeFramebuffer();
364}
365
366// setName() is called when the desktop name changes
367void CConn::setName(const char* name)
368{
369 CConnection::setName(name);
370 if (desktop)
371 desktop->setName(name);
372}
373
374// framebufferUpdateStart() is called at the beginning of an update.
375// Here we try to send out a new framebuffer update request so that the
376// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000377// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000378void CConn::framebufferUpdateStart()
379{
Pierre Ossman3da238d2015-11-12 12:20:05 +0100380 CConnection::framebufferUpdateStart();
381
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000382 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000383 pendingUpdate = false;
384
385 requestNewUpdate();
Pierre Ossmand9b90032015-09-23 12:18:52 +0200386
387 // Update the screen prematurely for very slow updates
388 Fl::add_timeout(1.0, handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000389}
390
391// framebufferUpdateEnd() is called at the end of an update.
392// For each rectangle, the FdInStream will have timed the speed
393// of the connection, allowing us to select format and encoding
394// appropriately, and then request another incremental update.
395void CConn::framebufferUpdateEnd()
396{
Pierre Ossman3da238d2015-11-12 12:20:05 +0100397 CConnection::framebufferUpdateEnd();
398
Pierre Ossmanfecf0a42018-03-26 12:22:47 +0200399 updateCount++;
Pierre Ossman921f6c82017-02-24 12:33:09 +0100400
Pierre Ossmand9b90032015-09-23 12:18:52 +0200401 Fl::remove_timeout(handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000402 desktop->updateWindow();
403
404 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000405 // We need fences to make extra update requests and continuous
406 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000407 if (cp.supportsFence)
408 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
409
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000410 firstUpdate = false;
411 }
412
Pierre Ossman5d512c32011-11-04 11:42:16 +0000413 // A format change has been scheduled and we are now past the update
414 // with the old format. Time to active the new one.
415 if (pendingPFChange) {
Pierre Ossman5d512c32011-11-04 11:42:16 +0000416 cp.setPF(pendingPF);
417 pendingPFChange = false;
418 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000419
420 // Compute new settings based on updated bandwidth values
421 if (autoSelect)
422 autoSelectFormatAndEncoding();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000423}
424
425// The rest of the callbacks are fairly self-explanatory...
426
427void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
428{
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200429 vlog.error(_("Invalid SetColourMapEntries from server!"));
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000430}
431
432void CConn::bell()
433{
434 fl_beep();
435}
436
437void CConn::serverCutText(const char* str, rdr::U32 len)
438{
Pierre Ossman4c204232018-03-26 13:32:49 +0200439 desktop->serverCutText(str, len);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000440}
441
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100442void CConn::dataRect(const Rect& r, int encoding)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000443{
444 sock->inStream().startTiming();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000445
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100446 if (encoding != encodingCopyRect)
447 lastServerEncoding = encoding;
448
Pierre Ossman9f273e92015-11-09 16:34:54 +0100449 CConnection::dataRect(r, encoding);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100450
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000451 sock->inStream().stopTiming();
Pierre Ossman921f6c82017-02-24 12:33:09 +0100452
453 pixelCount += r.area();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000454}
455
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000456void CConn::setCursor(int width, int height, const Point& hotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100457 const rdr::U8* data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000458{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100459 desktop->setCursor(width, height, hotspot, data);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000460}
461
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000462void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
463{
464 CMsgHandler::fence(flags, len, data);
465
466 if (flags & fenceFlagRequest) {
467 // We handle everything synchronously so we trivially honor these modes
468 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
469
470 writer()->writeFence(flags, len, data);
471 return;
472 }
473
474 if (len == 0) {
475 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000476 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000477 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000478
479 if (cp.supportsContinuousUpdates) {
480 vlog.info(_("Enabling continuous updates"));
481 continuousUpdates = true;
Pierre Ossman9312b0e2018-06-20 12:25:14 +0200482 writer()->writeEnableContinuousUpdates(true, 0, 0,
483 cp.width(), cp.height());
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000484 }
485 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000486 } else {
487 // Pixel format change
488 rdr::MemInStream memStream(data, len);
489 PixelFormat pf;
490
491 pf.read(&memStream);
492
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000493 cp.setPF(pf);
494 }
495}
496
Pierre Ossman2fa63f82016-12-05 15:26:21 +0100497void CConn::setLEDState(unsigned int state)
498{
499 CConnection::setLEDState(state);
500
501 desktop->setLEDState(state);
502}
503
DRC33c15e32011-11-03 18:49:21 +0000504
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000505////////////////////// Internal methods //////////////////////
506
507void CConn::resizeFramebuffer()
508{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000509 if (!desktop)
510 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000511
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000512 if (continuousUpdates)
Pierre Ossman9312b0e2018-06-20 12:25:14 +0200513 writer()->writeEnableContinuousUpdates(true, 0, 0,
514 cp.width(), cp.height());
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000515
Pierre Ossman9312b0e2018-06-20 12:25:14 +0200516 desktop->resizeFramebuffer(cp.width(), cp.height());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000517}
518
519// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
520// to the connection speed:
521//
522// First we wait for at least one second of bandwidth measurement.
523//
524// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
525// which should be perceptually lossless.
526//
527// If the bandwidth is below that, we choose a more lossy JPEG quality.
528//
529// If the bandwidth drops below 256 Kbps, we switch to palette mode.
530//
531// Note: The system here is fairly arbitrary and should be replaced
532// with something more intelligent at the server end.
533//
534void CConn::autoSelectFormatAndEncoding()
535{
536 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
537 unsigned int timeWaited = sock->inStream().timeWaited();
538 bool newFullColour = fullColour;
539 int newQualityLevel = qualityLevel;
540
541 // Always use Tight
542 if (currentEncoding != encodingTight) {
543 currentEncoding = encodingTight;
544 encodingChange = true;
545 }
546
547 // Check that we have a decent bandwidth measurement
548 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
549 return;
550
551 // Select appropriate quality level
552 if (!noJpeg) {
553 if (kbitsPerSecond > 16000)
554 newQualityLevel = 8;
555 else
556 newQualityLevel = 6;
557
558 if (newQualityLevel != qualityLevel) {
559 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
560 kbitsPerSecond, newQualityLevel);
561 cp.qualityLevel = newQualityLevel;
562 qualityLevel.setParam(newQualityLevel);
563 encodingChange = true;
564 }
565 }
566
567 if (cp.beforeVersion(3, 8)) {
568 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
569 // cursors "asynchronously". If this happens in the middle of a
570 // pixel format change, the server will encode the cursor with
571 // the old format, but the client will try to decode it
572 // according to the new format. This will lead to a
573 // crash. Therefore, we do not allow automatic format change for
574 // old servers.
575 return;
576 }
577
578 // Select best color level
579 newFullColour = (kbitsPerSecond > 256);
580 if (newFullColour != fullColour) {
Pierre Ossman4af93a92018-10-25 10:32:38 +0200581 if (newFullColour)
582 vlog.info(_("Throughput %d kbit/s - full color is now enabled"),
583 kbitsPerSecond);
584 else
585 vlog.info(_("Throughput %d kbit/s - full color is now disabled"),
586 kbitsPerSecond);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000587 fullColour.setParam(newFullColour);
588 formatChange = true;
589 }
590}
591
592// checkEncodings() sends a setEncodings message if one is needed.
593void CConn::checkEncodings()
594{
595 if (encodingChange && writer()) {
596 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
597 writer()->writeSetEncodings(currentEncoding, true);
598 encodingChange = false;
599 }
600}
601
602// requestNewUpdate() requests an update from the server, having set the
603// format and encoding appropriately.
604void CConn::requestNewUpdate()
605{
606 if (formatChange) {
607 PixelFormat pf;
608
609 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000610 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000611
612 if (fullColour) {
613 pf = fullColourPF;
614 } else {
615 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000616 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000617 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000618 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000619 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000620 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000621 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000622
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000623 if (supportsSyncFence) {
624 // We let the fence carry the pixel format and switch once we
625 // get the response back. That way we will be synchronised with
626 // when the server switches.
627 rdr::MemOutStream memStream;
628
629 pf.write(&memStream);
630
631 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
632 memStream.length(), (const char*)memStream.data());
633 } else {
634 // New requests are sent out at the start of processing the last
635 // one, so we cannot switch our internal format right now (doing so
636 // would mean misdecoding the current update).
637 pendingPFChange = true;
638 pendingPF = pf;
639 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000640
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000641 char str[256];
642 pf.print(str, 256);
643 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000644 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000645
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000646 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000647 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000648
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000649 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000650
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000651 if (forceNonincremental || !continuousUpdates) {
652 pendingUpdate = true;
Pierre Ossman9312b0e2018-06-20 12:25:14 +0200653 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width(), cp.height()),
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000654 !forceNonincremental);
655 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000656
657 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000658}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000659
660void CConn::handleOptions(void *data)
661{
662 CConn *self = (CConn*)data;
663
664 // Checking all the details of the current set of encodings is just
665 // a pain. Assume something has changed, as resending the encoding
666 // list is cheap. Avoid overriding what the auto logic has selected
667 // though.
668 if (!autoSelect) {
669 int encNum = encodingNum(preferredEncoding);
670
671 if (encNum != -1)
672 self->currentEncoding = encNum;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000673 }
674
Pierre Ossmanda389562011-06-09 08:24:37 +0000675 self->cp.supportsLocalCursor = true;
676
Pierre Ossmana22459d2014-03-17 14:42:10 +0100677 if (customCompressLevel)
678 self->cp.compressLevel = compressLevel;
679 else
680 self->cp.compressLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000681
Pierre Ossmana22459d2014-03-17 14:42:10 +0100682 if (!noJpeg && !autoSelect)
683 self->cp.qualityLevel = qualityLevel;
684 else
685 self->cp.qualityLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000686
687 self->encodingChange = true;
688
689 // Format changes refreshes the entire screen though and are therefore
690 // very costly. It's probably worth the effort to see if it is necessary
691 // here.
692 PixelFormat pf;
693
694 if (fullColour) {
695 pf = self->fullColourPF;
696 } else {
697 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000698 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000699 else if (lowColourLevel == 1)
700 pf = lowColourPF;
701 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000702 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000703 }
704
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000705 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000706 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000707
708 // Without fences, we cannot safely trigger an update request directly
709 // but must wait for the next update to arrive.
710 if (self->supportsSyncFence)
711 self->requestNewUpdate();
712 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000713}
Pierre Ossmand9b90032015-09-23 12:18:52 +0200714
715void CConn::handleUpdateTimeout(void *data)
716{
717 CConn *self = (CConn *)data;
718
719 assert(self);
720
721 self->desktop->updateWindow();
722
723 Fl::repeat_timeout(1.0, handleUpdateTimeout, data);
724}