blob: 9c1279628850fb920118164ba40ffd9c0afec36a [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/encodings.h>
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +010033#include <rfb/Decoder.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000034#include <rfb/Hostname.h>
35#include <rfb/LogWriter.h>
Pierre Ossman0068a4f2015-11-09 15:48:19 +010036#include <rfb/Security.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000037#include <rfb/util.h>
38#include <rfb/screenTypes.h>
Pierre Ossmane28bdb22011-11-14 16:02:06 +000039#include <rfb/fenceTypes.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000040#include <rfb/Timer.h>
Pierre Ossmane28bdb22011-11-14 16:02:06 +000041#include <rdr/MemInStream.h>
42#include <rdr/MemOutStream.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000043#include <network/TcpSocket.h>
44
45#include <FL/Fl.H>
46#include <FL/fl_ask.H>
47
48#include "CConn.h"
Pierre Ossmanf4f30942011-05-17 09:39:07 +000049#include "OptionsDialog.h"
Pierre Ossman947b48d2014-01-27 16:52:35 +010050#include "DesktopWindow.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000051#include "i18n.h"
52#include "parameters.h"
Pierre Ossman39ceb502011-07-12 15:54:25 +000053#include "vncviewer.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000054
DRCb65bb932011-06-24 03:17:00 +000055#ifdef WIN32
56#include "win32.h"
57#endif
58
Pierre Ossman5156d5e2011-03-09 09:42:34 +000059using namespace rdr;
60using namespace rfb;
61using namespace std;
62
Pierre Ossman5156d5e2011-03-09 09:42:34 +000063static rfb::LogWriter vlog("CConn");
64
Pierre Ossmancf836f22011-07-14 14:34:09 +000065// 8 colours (1 bit per component)
66static const PixelFormat verylowColourPF(8, 3,false, true,
67 1, 1, 1, 2, 1, 0);
68// 64 colours (2 bits per component)
69static const PixelFormat lowColourPF(8, 6, false, true,
70 3, 3, 3, 4, 2, 0);
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010071// 256 colours (2-3 bits per component)
72static const PixelFormat mediumColourPF(8, 8, false, true,
73 7, 7, 3, 5, 2, 0);
Pierre Ossmanf4f30942011-05-17 09:39:07 +000074
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000075CConn::CConn(const char* vncServerName, network::Socket* socket=NULL)
76 : serverHost(0), serverPort(0), desktop(NULL),
Pierre Ossman5d512c32011-11-04 11:42:16 +000077 pendingPFChange(false),
Pierre Ossman5156d5e2011-03-09 09:42:34 +000078 currentEncoding(encodingTight), lastServerEncoding((unsigned int)-1),
79 formatChange(false), encodingChange(false),
Pierre Ossmanaa73c892011-11-15 12:13:37 +000080 firstUpdate(true), pendingUpdate(false), continuousUpdates(false),
Pierre Ossmane28bdb22011-11-14 16:02:06 +000081 forceNonincremental(true), supportsSyncFence(false)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000082{
83 setShared(::shared);
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000084 sock = socket;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000085
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +010086 memset(decoders, 0, sizeof(decoders));
87
Pierre Ossman5156d5e2011-03-09 09:42:34 +000088 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 Ossmana22459d2014-03-17 14:42:10 +010098 if (customCompressLevel)
99 cp.compressLevel = compressLevel;
100 else
101 cp.compressLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000102
Pierre Ossmana22459d2014-03-17 14:42:10 +0100103 if (!noJpeg)
104 cp.qualityLevel = qualityLevel;
105 else
106 cp.qualityLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000107
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000108 if(sock == NULL) {
109 try {
110 getHostAndPort(vncServerName, &serverHost, &serverPort);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000111
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000112 sock = new network::TcpSocket(serverHost, serverPort);
113 vlog.info(_("connected to host %s port %d"), serverHost, serverPort);
114 } catch (rdr::Exception& e) {
115 vlog.error("%s", e.str());
116 fl_alert("%s", e.str());
117 exit_vncviewer();
118 return;
119 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000120 }
121
122 Fl::add_fd(sock->getFd(), FL_READ | FL_EXCEPT, socketEvent, this);
123
124 // See callback below
125 sock->inStream().setBlockCallback(this);
126
127 setServerName(serverHost);
128 setStreams(&sock->inStream(), &sock->outStream());
129
130 initialiseProtocol();
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000131
132 OptionsDialog::addCallback(handleOptions, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000133}
134
135CConn::~CConn()
136{
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000137 OptionsDialog::removeCallback(handleOptions);
Pierre Ossmand9b90032015-09-23 12:18:52 +0200138 Fl::remove_timeout(handleUpdateTimeout, this);
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000139
Pierre Ossman5c23b9e2015-03-03 16:26:03 +0100140 for (size_t i = 0; i < sizeof(decoders)/sizeof(decoders[0]); i++)
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100141 delete decoders[i];
142
Pierre Ossman6a9e2e62011-05-19 14:47:43 +0000143 if (desktop)
144 delete desktop;
145
Pierre Ossman5e04c262011-11-09 11:31:12 +0000146 delete [] serverHost;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000147 if (sock)
148 Fl::remove_fd(sock->getFd());
149 delete sock;
150}
151
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000152void CConn::refreshFramebuffer()
153{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000154 forceNonincremental = true;
155
156 // Without fences, we cannot safely trigger an update request directly
157 // but must wait for the next update to arrive.
158 if (supportsSyncFence)
159 requestNewUpdate();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000160}
161
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000162const char *CConn::connectionInfo()
163{
164 static char infoText[1024] = "";
165
Pierre Ossmanb2046432014-09-22 11:17:34 +0200166 char scratch[100];
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000167 char pfStr[100];
Pierre Ossmanb2046432014-09-22 11:17:34 +0200168
169 // Crude way of avoiding constant overflow checks
170 assert((sizeof(scratch) + 1) * 10 < sizeof(infoText));
171
172 infoText[0] = '\0';
173
174 snprintf(scratch, sizeof(scratch),
175 _("Desktop name: %.80s"), cp.name());
176 strcat(infoText, scratch);
177 strcat(infoText, "\n");
178
179 snprintf(scratch, sizeof(scratch),
180 _("Host: %.80s port: %d"), serverHost, serverPort);
181 strcat(infoText, scratch);
182 strcat(infoText, "\n");
183
184 snprintf(scratch, sizeof(scratch),
185 _("Size: %d x %d"), cp.width, cp.height);
186 strcat(infoText, scratch);
187 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000188
Pierre Ossman744e55c2014-12-03 14:00:54 +0100189 // TRANSLATORS: Will be filled in with a string describing the
190 // protocol pixel format in a fairly language neutral way
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000191 cp.pf().print(pfStr, 100);
Pierre Ossmanb2046432014-09-22 11:17:34 +0200192 snprintf(scratch, sizeof(scratch),
193 _("Pixel format: %s"), pfStr);
194 strcat(infoText, scratch);
195 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000196
Pierre Ossman744e55c2014-12-03 14:00:54 +0100197 // TRANSLATORS: Similar to the earlier "Pixel format" string
Pierre Ossmanb2046432014-09-22 11:17:34 +0200198 serverPF.print(pfStr, 100);
199 snprintf(scratch, sizeof(scratch),
200 _("(server default %s)"), pfStr);
201 strcat(infoText, scratch);
202 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000203
Pierre Ossmanb2046432014-09-22 11:17:34 +0200204 snprintf(scratch, sizeof(scratch),
205 _("Requested encoding: %s"), encodingName(currentEncoding));
206 strcat(infoText, scratch);
207 strcat(infoText, "\n");
208
209 snprintf(scratch, sizeof(scratch),
210 _("Last used encoding: %s"), encodingName(lastServerEncoding));
211 strcat(infoText, scratch);
212 strcat(infoText, "\n");
213
214 snprintf(scratch, sizeof(scratch),
215 _("Line speed estimate: %d kbit/s"), sock->inStream().kbitsPerSecond());
216 strcat(infoText, scratch);
217 strcat(infoText, "\n");
218
219 snprintf(scratch, sizeof(scratch),
220 _("Protocol version: %d.%d"), cp.majorVersion, cp.minorVersion);
221 strcat(infoText, scratch);
222 strcat(infoText, "\n");
223
224 snprintf(scratch, sizeof(scratch),
225 _("Security method: %s"), secTypeName(csecurity->getType()));
226 strcat(infoText, scratch);
227 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000228
229 return infoText;
230}
231
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000232// The RFB core is not properly asynchronous, so it calls this callback
233// whenever it needs to block to wait for more data. Since FLTK is
234// monitoring the socket, we just make sure FLTK gets to run.
235
236void CConn::blockCallback()
237{
238 int next_timer;
239
240 next_timer = Timer::checkTimeouts();
241 if (next_timer == 0)
242 next_timer = INT_MAX;
243
244 Fl::wait((double)next_timer / 1000.0);
Pierre Ossman1db73242015-09-23 12:20:32 +0200245
246 if (should_exit())
247 throw rdr::Exception("Termination requested");
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000248}
249
DRC3e7ed812013-02-26 10:34:22 +0000250void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000251{
252 CConn *cc;
253 static bool recursing = false;
254
255 assert(data);
256 cc = (CConn*)data;
257
258 // I don't think processMsg() is recursion safe, so add this check
259 if (recursing)
260 return;
261
262 recursing = true;
263
264 try {
265 // processMsg() only processes one message, so we need to loop
266 // until the buffers are empty or things will stall.
267 do {
268 cc->processMsg();
269 } while (cc->sock->inStream().checkNoWait(1));
270 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000271 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000272 exit_vncviewer();
273 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000274 vlog.error("%s", e.str());
Pierre Ossman1db73242015-09-23 12:20:32 +0200275 // Somebody might already have requested us to terminate, and
276 // might have already provided an error message.
277 if (!should_exit())
278 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000279 }
280
281 recursing = false;
282}
283
284////////////////////// CConnection callback methods //////////////////////
285
286// serverInit() is called when the serverInit message has been received. At
287// this point we create the desktop window and display it. We also tell the
288// server the pixel format and encodings to use and request the first update.
289void CConn::serverInit()
290{
291 CConnection::serverInit();
292
293 // If using AutoSelect with old servers, start in FullColor
294 // mode. See comment in autoSelectFormatAndEncoding.
295 if (cp.beforeVersion(3, 8) && autoSelect)
296 fullColour.setParam(true);
297
298 serverPF = cp.pf();
299
300 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
301 fullColourPF = desktop->getPreferredPF();
302
Pierre Ossman5d512c32011-11-04 11:42:16 +0000303 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000304 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000305
306 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000307 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000308
309 // This initial update request is a bit of a corner case, so we need
310 // to help out setting the correct format here.
311 assert(pendingPFChange);
Pierre Ossman5d512c32011-11-04 11:42:16 +0000312 cp.setPF(pendingPF);
313 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000314}
315
316// setDesktopSize() is called when the desktop size changes (including when
317// it is set initially).
318void CConn::setDesktopSize(int w, int h)
319{
320 CConnection::setDesktopSize(w,h);
321 resizeFramebuffer();
322}
323
324// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
Pierre Ossman28c1d542015-03-03 16:27:44 +0100325void CConn::setExtendedDesktopSize(unsigned reason, unsigned result,
326 int w, int h, const rfb::ScreenSet& layout)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000327{
328 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
329
330 if ((reason == reasonClient) && (result != resultSuccess)) {
331 vlog.error(_("SetDesktopSize failed: %d"), result);
332 return;
333 }
334
335 resizeFramebuffer();
336}
337
338// setName() is called when the desktop name changes
339void CConn::setName(const char* name)
340{
341 CConnection::setName(name);
342 if (desktop)
343 desktop->setName(name);
344}
345
346// framebufferUpdateStart() is called at the beginning of an update.
347// Here we try to send out a new framebuffer update request so that the
348// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000349// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000350void CConn::framebufferUpdateStart()
351{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000352 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000353 pendingUpdate = false;
354
355 requestNewUpdate();
Pierre Ossmand9b90032015-09-23 12:18:52 +0200356
357 // Update the screen prematurely for very slow updates
358 Fl::add_timeout(1.0, handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000359}
360
361// framebufferUpdateEnd() is called at the end of an update.
362// For each rectangle, the FdInStream will have timed the speed
363// of the connection, allowing us to select format and encoding
364// appropriately, and then request another incremental update.
365void CConn::framebufferUpdateEnd()
366{
Pierre Ossmand9b90032015-09-23 12:18:52 +0200367 Fl::remove_timeout(handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000368 desktop->updateWindow();
369
370 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000371 // We need fences to make extra update requests and continuous
372 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000373 if (cp.supportsFence)
374 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
375
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000376 firstUpdate = false;
377 }
378
Pierre Ossman5d512c32011-11-04 11:42:16 +0000379 // A format change has been scheduled and we are now past the update
380 // with the old format. Time to active the new one.
381 if (pendingPFChange) {
Pierre Ossman5d512c32011-11-04 11:42:16 +0000382 cp.setPF(pendingPF);
383 pendingPFChange = false;
384 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000385
386 // Compute new settings based on updated bandwidth values
387 if (autoSelect)
388 autoSelectFormatAndEncoding();
389
390 // Make sure that the FLTK handling and the timers gets some CPU time
391 // in case of back to back framebuffer updates.
392 Fl::check();
393 Timer::checkTimeouts();
394}
395
396// The rest of the callbacks are fairly self-explanatory...
397
398void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
399{
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200400 vlog.error(_("Invalid SetColourMapEntries from server!"));
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000401}
402
403void CConn::bell()
404{
405 fl_beep();
406}
407
408void CConn::serverCutText(const char* str, rdr::U32 len)
409{
Pierre Ossman689c4582011-05-26 15:39:41 +0000410 char *buffer;
411 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000412
Pierre Ossman50104882013-05-24 10:47:27 +0000413 if (!acceptClipboard)
414 return;
415
Pierre Ossman689c4582011-05-26 15:39:41 +0000416 size = fl_utf8froma(NULL, 0, str, len);
417 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000418 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000419
420 size++;
421
422 buffer = new char[size];
423
424 ret = fl_utf8froma(buffer, size, str, len);
425 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000426
Pierre Ossmanfb450fb2015-03-03 16:34:56 +0100427 vlog.debug("Got clipboard data (%d bytes)", (int)strlen(buffer));
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000428
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000429 // RFB doesn't have separate selection and clipboard concepts, so we
430 // dump the data into both variants.
431 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000432 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000433
434 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000435}
436
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100437void CConn::dataRect(const Rect& r, int encoding)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000438{
439 sock->inStream().startTiming();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000440
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100441 if (encoding != encodingCopyRect)
442 lastServerEncoding = encoding;
443
444 if (!Decoder::supported(encoding)) {
Pierre Ossman744e55c2014-12-03 14:00:54 +0100445 // TRANSLATORS: Refers to a VNC protocol encoding type
Pierre Ossmanc76bd2b2014-12-03 14:01:31 +0100446 vlog.error(_("Unknown encoding %d"), encoding);
447 throw Exception(_("Unknown encoding"));
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100448 }
449
450 if (!decoders[encoding]) {
Pierre Ossman668468b2014-01-31 12:37:32 +0100451 decoders[encoding] = Decoder::createDecoder(encoding, this);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100452 if (!decoders[encoding]) {
Pierre Ossmanc76bd2b2014-12-03 14:01:31 +0100453 vlog.error(_("Unknown encoding %d"), encoding);
454 throw Exception(_("Unknown encoding"));
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100455 }
456 }
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200457 decoders[encoding]->readRect(r, desktop->getFramebuffer());
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100458
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000459 sock->inStream().stopTiming();
460}
461
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000462void CConn::setCursor(int width, int height, const Point& hotspot,
463 void* data, void* mask)
464{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000465 desktop->setCursor(width, height, hotspot, data, mask);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000466}
467
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000468void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
469{
470 CMsgHandler::fence(flags, len, data);
471
472 if (flags & fenceFlagRequest) {
473 // We handle everything synchronously so we trivially honor these modes
474 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
475
476 writer()->writeFence(flags, len, data);
477 return;
478 }
479
480 if (len == 0) {
481 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000482 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000483 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000484
485 if (cp.supportsContinuousUpdates) {
486 vlog.info(_("Enabling continuous updates"));
487 continuousUpdates = true;
488 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
489 }
490 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000491 } else {
492 // Pixel format change
493 rdr::MemInStream memStream(data, len);
494 PixelFormat pf;
495
496 pf.read(&memStream);
497
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000498 cp.setPF(pf);
499 }
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}