blob: 2acb373e5f42fac9133a43ac1825e8ffb90c9061 [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);
Pierre Ossmand9b90032015-09-23 12:18:52 +0200136 Fl::remove_timeout(handleUpdateTimeout, this);
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000137
Pierre Ossman5c23b9e2015-03-03 16:26:03 +0100138 for (size_t i = 0; i < sizeof(decoders)/sizeof(decoders[0]); i++)
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100139 delete decoders[i];
140
Pierre Ossman6a9e2e62011-05-19 14:47:43 +0000141 if (desktop)
142 delete desktop;
143
Pierre Ossman5e04c262011-11-09 11:31:12 +0000144 delete [] serverHost;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000145 if (sock)
146 Fl::remove_fd(sock->getFd());
147 delete sock;
148}
149
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000150void CConn::refreshFramebuffer()
151{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000152 forceNonincremental = true;
153
154 // Without fences, we cannot safely trigger an update request directly
155 // but must wait for the next update to arrive.
156 if (supportsSyncFence)
157 requestNewUpdate();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000158}
159
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000160const char *CConn::connectionInfo()
161{
162 static char infoText[1024] = "";
163
Pierre Ossmanb2046432014-09-22 11:17:34 +0200164 char scratch[100];
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000165 char pfStr[100];
Pierre Ossmanb2046432014-09-22 11:17:34 +0200166
167 // Crude way of avoiding constant overflow checks
168 assert((sizeof(scratch) + 1) * 10 < sizeof(infoText));
169
170 infoText[0] = '\0';
171
172 snprintf(scratch, sizeof(scratch),
173 _("Desktop name: %.80s"), cp.name());
174 strcat(infoText, scratch);
175 strcat(infoText, "\n");
176
177 snprintf(scratch, sizeof(scratch),
178 _("Host: %.80s port: %d"), serverHost, serverPort);
179 strcat(infoText, scratch);
180 strcat(infoText, "\n");
181
182 snprintf(scratch, sizeof(scratch),
183 _("Size: %d x %d"), cp.width, cp.height);
184 strcat(infoText, scratch);
185 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000186
Pierre Ossman744e55c2014-12-03 14:00:54 +0100187 // TRANSLATORS: Will be filled in with a string describing the
188 // protocol pixel format in a fairly language neutral way
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000189 cp.pf().print(pfStr, 100);
Pierre Ossmanb2046432014-09-22 11:17:34 +0200190 snprintf(scratch, sizeof(scratch),
191 _("Pixel format: %s"), pfStr);
192 strcat(infoText, scratch);
193 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000194
Pierre Ossman744e55c2014-12-03 14:00:54 +0100195 // TRANSLATORS: Similar to the earlier "Pixel format" string
Pierre Ossmanb2046432014-09-22 11:17:34 +0200196 serverPF.print(pfStr, 100);
197 snprintf(scratch, sizeof(scratch),
198 _("(server default %s)"), pfStr);
199 strcat(infoText, scratch);
200 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000201
Pierre Ossmanb2046432014-09-22 11:17:34 +0200202 snprintf(scratch, sizeof(scratch),
203 _("Requested encoding: %s"), encodingName(currentEncoding));
204 strcat(infoText, scratch);
205 strcat(infoText, "\n");
206
207 snprintf(scratch, sizeof(scratch),
208 _("Last used encoding: %s"), encodingName(lastServerEncoding));
209 strcat(infoText, scratch);
210 strcat(infoText, "\n");
211
212 snprintf(scratch, sizeof(scratch),
213 _("Line speed estimate: %d kbit/s"), sock->inStream().kbitsPerSecond());
214 strcat(infoText, scratch);
215 strcat(infoText, "\n");
216
217 snprintf(scratch, sizeof(scratch),
218 _("Protocol version: %d.%d"), cp.majorVersion, cp.minorVersion);
219 strcat(infoText, scratch);
220 strcat(infoText, "\n");
221
222 snprintf(scratch, sizeof(scratch),
223 _("Security method: %s"), secTypeName(csecurity->getType()));
224 strcat(infoText, scratch);
225 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000226
227 return infoText;
228}
229
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000230// The RFB core is not properly asynchronous, so it calls this callback
231// whenever it needs to block to wait for more data. Since FLTK is
232// monitoring the socket, we just make sure FLTK gets to run.
233
234void CConn::blockCallback()
235{
236 int next_timer;
237
238 next_timer = Timer::checkTimeouts();
239 if (next_timer == 0)
240 next_timer = INT_MAX;
241
242 Fl::wait((double)next_timer / 1000.0);
243}
244
DRC3e7ed812013-02-26 10:34:22 +0000245void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000246{
247 CConn *cc;
248 static bool recursing = false;
249
250 assert(data);
251 cc = (CConn*)data;
252
253 // I don't think processMsg() is recursion safe, so add this check
254 if (recursing)
255 return;
256
257 recursing = true;
258
259 try {
260 // processMsg() only processes one message, so we need to loop
261 // until the buffers are empty or things will stall.
262 do {
263 cc->processMsg();
264 } while (cc->sock->inStream().checkNoWait(1));
265 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000266 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000267 exit_vncviewer();
268 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000269 vlog.error("%s", e.str());
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000270 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000271 }
272
273 recursing = false;
274}
275
276////////////////////// CConnection callback methods //////////////////////
277
278// serverInit() is called when the serverInit message has been received. At
279// this point we create the desktop window and display it. We also tell the
280// server the pixel format and encodings to use and request the first update.
281void CConn::serverInit()
282{
283 CConnection::serverInit();
284
285 // If using AutoSelect with old servers, start in FullColor
286 // mode. See comment in autoSelectFormatAndEncoding.
287 if (cp.beforeVersion(3, 8) && autoSelect)
288 fullColour.setParam(true);
289
290 serverPF = cp.pf();
291
292 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
293 fullColourPF = desktop->getPreferredPF();
294
Pierre Ossman5d512c32011-11-04 11:42:16 +0000295 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000296 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000297
298 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000299 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000300
301 // This initial update request is a bit of a corner case, so we need
302 // to help out setting the correct format here.
303 assert(pendingPFChange);
Pierre Ossman5d512c32011-11-04 11:42:16 +0000304 cp.setPF(pendingPF);
305 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000306}
307
308// setDesktopSize() is called when the desktop size changes (including when
309// it is set initially).
310void CConn::setDesktopSize(int w, int h)
311{
312 CConnection::setDesktopSize(w,h);
313 resizeFramebuffer();
314}
315
316// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
Pierre Ossman28c1d542015-03-03 16:27:44 +0100317void CConn::setExtendedDesktopSize(unsigned reason, unsigned result,
318 int w, int h, const rfb::ScreenSet& layout)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000319{
320 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
321
322 if ((reason == reasonClient) && (result != resultSuccess)) {
323 vlog.error(_("SetDesktopSize failed: %d"), result);
324 return;
325 }
326
327 resizeFramebuffer();
328}
329
330// setName() is called when the desktop name changes
331void CConn::setName(const char* name)
332{
333 CConnection::setName(name);
334 if (desktop)
335 desktop->setName(name);
336}
337
338// framebufferUpdateStart() is called at the beginning of an update.
339// Here we try to send out a new framebuffer update request so that the
340// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000341// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000342void CConn::framebufferUpdateStart()
343{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000344 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000345 pendingUpdate = false;
346
347 requestNewUpdate();
Pierre Ossmand9b90032015-09-23 12:18:52 +0200348
349 // Update the screen prematurely for very slow updates
350 Fl::add_timeout(1.0, handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000351}
352
353// framebufferUpdateEnd() is called at the end of an update.
354// For each rectangle, the FdInStream will have timed the speed
355// of the connection, allowing us to select format and encoding
356// appropriately, and then request another incremental update.
357void CConn::framebufferUpdateEnd()
358{
Pierre Ossmand9b90032015-09-23 12:18:52 +0200359 Fl::remove_timeout(handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000360 desktop->updateWindow();
361
362 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000363 // We need fences to make extra update requests and continuous
364 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000365 if (cp.supportsFence)
366 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
367
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000368 firstUpdate = false;
369 }
370
Pierre Ossman5d512c32011-11-04 11:42:16 +0000371 // A format change has been scheduled and we are now past the update
372 // with the old format. Time to active the new one.
373 if (pendingPFChange) {
Pierre Ossman5d512c32011-11-04 11:42:16 +0000374 cp.setPF(pendingPF);
375 pendingPFChange = false;
376 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000377
378 // Compute new settings based on updated bandwidth values
379 if (autoSelect)
380 autoSelectFormatAndEncoding();
381
382 // Make sure that the FLTK handling and the timers gets some CPU time
383 // in case of back to back framebuffer updates.
384 Fl::check();
385 Timer::checkTimeouts();
386}
387
388// The rest of the callbacks are fairly self-explanatory...
389
390void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
391{
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200392 vlog.error(_("Invalid SetColourMapEntries from server!"));
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000393}
394
395void CConn::bell()
396{
397 fl_beep();
398}
399
400void CConn::serverCutText(const char* str, rdr::U32 len)
401{
Pierre Ossman689c4582011-05-26 15:39:41 +0000402 char *buffer;
403 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000404
Pierre Ossman50104882013-05-24 10:47:27 +0000405 if (!acceptClipboard)
406 return;
407
Pierre Ossman689c4582011-05-26 15:39:41 +0000408 size = fl_utf8froma(NULL, 0, str, len);
409 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000410 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000411
412 size++;
413
414 buffer = new char[size];
415
416 ret = fl_utf8froma(buffer, size, str, len);
417 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000418
Pierre Ossmanfb450fb2015-03-03 16:34:56 +0100419 vlog.debug("Got clipboard data (%d bytes)", (int)strlen(buffer));
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000420
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000421 // RFB doesn't have separate selection and clipboard concepts, so we
422 // dump the data into both variants.
423 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000424 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000425
426 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000427}
428
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100429void CConn::dataRect(const Rect& r, int encoding)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000430{
431 sock->inStream().startTiming();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000432
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100433 if (encoding != encodingCopyRect)
434 lastServerEncoding = encoding;
435
436 if (!Decoder::supported(encoding)) {
Pierre Ossman744e55c2014-12-03 14:00:54 +0100437 // TRANSLATORS: Refers to a VNC protocol encoding type
Pierre Ossmanc76bd2b2014-12-03 14:01:31 +0100438 vlog.error(_("Unknown encoding %d"), encoding);
439 throw Exception(_("Unknown encoding"));
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100440 }
441
442 if (!decoders[encoding]) {
Pierre Ossman668468b2014-01-31 12:37:32 +0100443 decoders[encoding] = Decoder::createDecoder(encoding, this);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100444 if (!decoders[encoding]) {
Pierre Ossmanc76bd2b2014-12-03 14:01:31 +0100445 vlog.error(_("Unknown encoding %d"), encoding);
446 throw Exception(_("Unknown encoding"));
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100447 }
448 }
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200449 decoders[encoding]->readRect(r, desktop->getFramebuffer());
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100450
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000451 sock->inStream().stopTiming();
452}
453
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000454void CConn::setCursor(int width, int height, const Point& hotspot,
455 void* data, void* mask)
456{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000457 desktop->setCursor(width, height, hotspot, data, mask);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000458}
459
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000460void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
461{
462 CMsgHandler::fence(flags, len, data);
463
464 if (flags & fenceFlagRequest) {
465 // We handle everything synchronously so we trivially honor these modes
466 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
467
468 writer()->writeFence(flags, len, data);
469 return;
470 }
471
472 if (len == 0) {
473 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000474 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000475 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000476
477 if (cp.supportsContinuousUpdates) {
478 vlog.info(_("Enabling continuous updates"));
479 continuousUpdates = true;
480 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
481 }
482 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000483 } else {
484 // Pixel format change
485 rdr::MemInStream memStream(data, len);
486 PixelFormat pf;
487
488 pf.read(&memStream);
489
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000490 cp.setPF(pf);
491 }
492}
493
DRC33c15e32011-11-03 18:49:21 +0000494
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000495////////////////////// Internal methods //////////////////////
496
497void CConn::resizeFramebuffer()
498{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000499 if (!desktop)
500 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000501
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000502 if (continuousUpdates)
503 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
504
Pierre Ossman6455d852011-06-01 09:33:00 +0000505 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000506}
507
508// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
509// to the connection speed:
510//
511// First we wait for at least one second of bandwidth measurement.
512//
513// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
514// which should be perceptually lossless.
515//
516// If the bandwidth is below that, we choose a more lossy JPEG quality.
517//
518// If the bandwidth drops below 256 Kbps, we switch to palette mode.
519//
520// Note: The system here is fairly arbitrary and should be replaced
521// with something more intelligent at the server end.
522//
523void CConn::autoSelectFormatAndEncoding()
524{
525 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
526 unsigned int timeWaited = sock->inStream().timeWaited();
527 bool newFullColour = fullColour;
528 int newQualityLevel = qualityLevel;
529
530 // Always use Tight
531 if (currentEncoding != encodingTight) {
532 currentEncoding = encodingTight;
533 encodingChange = true;
534 }
535
536 // Check that we have a decent bandwidth measurement
537 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
538 return;
539
540 // Select appropriate quality level
541 if (!noJpeg) {
542 if (kbitsPerSecond > 16000)
543 newQualityLevel = 8;
544 else
545 newQualityLevel = 6;
546
547 if (newQualityLevel != qualityLevel) {
548 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
549 kbitsPerSecond, newQualityLevel);
550 cp.qualityLevel = newQualityLevel;
551 qualityLevel.setParam(newQualityLevel);
552 encodingChange = true;
553 }
554 }
555
556 if (cp.beforeVersion(3, 8)) {
557 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
558 // cursors "asynchronously". If this happens in the middle of a
559 // pixel format change, the server will encode the cursor with
560 // the old format, but the client will try to decode it
561 // according to the new format. This will lead to a
562 // crash. Therefore, we do not allow automatic format change for
563 // old servers.
564 return;
565 }
566
567 // Select best color level
568 newFullColour = (kbitsPerSecond > 256);
569 if (newFullColour != fullColour) {
570 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
571 kbitsPerSecond,
572 newFullColour ? _("enabled") : _("disabled"));
573 fullColour.setParam(newFullColour);
574 formatChange = true;
575 }
576}
577
578// checkEncodings() sends a setEncodings message if one is needed.
579void CConn::checkEncodings()
580{
581 if (encodingChange && writer()) {
582 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
583 writer()->writeSetEncodings(currentEncoding, true);
584 encodingChange = false;
585 }
586}
587
588// requestNewUpdate() requests an update from the server, having set the
589// format and encoding appropriately.
590void CConn::requestNewUpdate()
591{
592 if (formatChange) {
593 PixelFormat pf;
594
595 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000596 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000597
598 if (fullColour) {
599 pf = fullColourPF;
600 } else {
601 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000602 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000603 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000604 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000605 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000606 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000607 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000608
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000609 if (supportsSyncFence) {
610 // We let the fence carry the pixel format and switch once we
611 // get the response back. That way we will be synchronised with
612 // when the server switches.
613 rdr::MemOutStream memStream;
614
615 pf.write(&memStream);
616
617 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
618 memStream.length(), (const char*)memStream.data());
619 } else {
620 // New requests are sent out at the start of processing the last
621 // one, so we cannot switch our internal format right now (doing so
622 // would mean misdecoding the current update).
623 pendingPFChange = true;
624 pendingPF = pf;
625 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000626
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000627 char str[256];
628 pf.print(str, 256);
629 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000630 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000631
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000632 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000633 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000634
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000635 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000636
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000637 if (forceNonincremental || !continuousUpdates) {
638 pendingUpdate = true;
639 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
640 !forceNonincremental);
641 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000642
643 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000644}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000645
646void CConn::handleOptions(void *data)
647{
648 CConn *self = (CConn*)data;
649
650 // Checking all the details of the current set of encodings is just
651 // a pain. Assume something has changed, as resending the encoding
652 // list is cheap. Avoid overriding what the auto logic has selected
653 // though.
654 if (!autoSelect) {
655 int encNum = encodingNum(preferredEncoding);
656
657 if (encNum != -1)
658 self->currentEncoding = encNum;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000659 }
660
Pierre Ossmanda389562011-06-09 08:24:37 +0000661 self->cp.supportsLocalCursor = true;
662
Pierre Ossmana22459d2014-03-17 14:42:10 +0100663 if (customCompressLevel)
664 self->cp.compressLevel = compressLevel;
665 else
666 self->cp.compressLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000667
Pierre Ossmana22459d2014-03-17 14:42:10 +0100668 if (!noJpeg && !autoSelect)
669 self->cp.qualityLevel = qualityLevel;
670 else
671 self->cp.qualityLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000672
673 self->encodingChange = true;
674
675 // Format changes refreshes the entire screen though and are therefore
676 // very costly. It's probably worth the effort to see if it is necessary
677 // here.
678 PixelFormat pf;
679
680 if (fullColour) {
681 pf = self->fullColourPF;
682 } else {
683 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000684 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000685 else if (lowColourLevel == 1)
686 pf = lowColourPF;
687 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000688 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000689 }
690
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000691 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000692 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000693
694 // Without fences, we cannot safely trigger an update request directly
695 // but must wait for the next update to arrive.
696 if (self->supportsSyncFence)
697 self->requestNewUpdate();
698 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000699}
Pierre Ossmand9b90032015-09-23 12:18:52 +0200700
701void CConn::handleUpdateTimeout(void *data)
702{
703 CConn *self = (CConn *)data;
704
705 assert(self);
706
707 self->desktop->updateWindow();
708
709 Fl::repeat_timeout(1.0, handleUpdateTimeout, data);
710}