blob: 686ce30db181866c34cf4ee2e345f81df7d50b82 [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);
Pierre Ossman1db73242015-09-23 12:20:32 +0200243
244 if (should_exit())
245 throw rdr::Exception("Termination requested");
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000246}
247
DRC3e7ed812013-02-26 10:34:22 +0000248void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000249{
250 CConn *cc;
251 static bool recursing = false;
252
253 assert(data);
254 cc = (CConn*)data;
255
256 // I don't think processMsg() is recursion safe, so add this check
257 if (recursing)
258 return;
259
260 recursing = true;
261
262 try {
263 // processMsg() only processes one message, so we need to loop
264 // until the buffers are empty or things will stall.
265 do {
266 cc->processMsg();
267 } while (cc->sock->inStream().checkNoWait(1));
268 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000269 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000270 exit_vncviewer();
271 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000272 vlog.error("%s", e.str());
Pierre Ossman1db73242015-09-23 12:20:32 +0200273 // Somebody might already have requested us to terminate, and
274 // might have already provided an error message.
275 if (!should_exit())
276 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000277 }
278
279 recursing = false;
280}
281
282////////////////////// CConnection callback methods //////////////////////
283
284// serverInit() is called when the serverInit message has been received. At
285// this point we create the desktop window and display it. We also tell the
286// server the pixel format and encodings to use and request the first update.
287void CConn::serverInit()
288{
289 CConnection::serverInit();
290
291 // If using AutoSelect with old servers, start in FullColor
292 // mode. See comment in autoSelectFormatAndEncoding.
293 if (cp.beforeVersion(3, 8) && autoSelect)
294 fullColour.setParam(true);
295
296 serverPF = cp.pf();
297
298 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
299 fullColourPF = desktop->getPreferredPF();
300
Pierre Ossman5d512c32011-11-04 11:42:16 +0000301 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000302 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000303
304 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000305 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000306
307 // This initial update request is a bit of a corner case, so we need
308 // to help out setting the correct format here.
309 assert(pendingPFChange);
Pierre Ossman5d512c32011-11-04 11:42:16 +0000310 cp.setPF(pendingPF);
311 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000312}
313
314// setDesktopSize() is called when the desktop size changes (including when
315// it is set initially).
316void CConn::setDesktopSize(int w, int h)
317{
318 CConnection::setDesktopSize(w,h);
319 resizeFramebuffer();
320}
321
322// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
Pierre Ossman28c1d542015-03-03 16:27:44 +0100323void CConn::setExtendedDesktopSize(unsigned reason, unsigned result,
324 int w, int h, const rfb::ScreenSet& layout)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000325{
326 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
327
328 if ((reason == reasonClient) && (result != resultSuccess)) {
329 vlog.error(_("SetDesktopSize failed: %d"), result);
330 return;
331 }
332
333 resizeFramebuffer();
334}
335
336// setName() is called when the desktop name changes
337void CConn::setName(const char* name)
338{
339 CConnection::setName(name);
340 if (desktop)
341 desktop->setName(name);
342}
343
344// framebufferUpdateStart() is called at the beginning of an update.
345// Here we try to send out a new framebuffer update request so that the
346// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000347// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000348void CConn::framebufferUpdateStart()
349{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000350 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000351 pendingUpdate = false;
352
353 requestNewUpdate();
Pierre Ossmand9b90032015-09-23 12:18:52 +0200354
355 // Update the screen prematurely for very slow updates
356 Fl::add_timeout(1.0, handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000357}
358
359// framebufferUpdateEnd() is called at the end of an update.
360// For each rectangle, the FdInStream will have timed the speed
361// of the connection, allowing us to select format and encoding
362// appropriately, and then request another incremental update.
363void CConn::framebufferUpdateEnd()
364{
Pierre Ossmand9b90032015-09-23 12:18:52 +0200365 Fl::remove_timeout(handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000366 desktop->updateWindow();
367
368 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000369 // We need fences to make extra update requests and continuous
370 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000371 if (cp.supportsFence)
372 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
373
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000374 firstUpdate = false;
375 }
376
Pierre Ossman5d512c32011-11-04 11:42:16 +0000377 // A format change has been scheduled and we are now past the update
378 // with the old format. Time to active the new one.
379 if (pendingPFChange) {
Pierre Ossman5d512c32011-11-04 11:42:16 +0000380 cp.setPF(pendingPF);
381 pendingPFChange = false;
382 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000383
384 // Compute new settings based on updated bandwidth values
385 if (autoSelect)
386 autoSelectFormatAndEncoding();
387
388 // Make sure that the FLTK handling and the timers gets some CPU time
389 // in case of back to back framebuffer updates.
390 Fl::check();
391 Timer::checkTimeouts();
392}
393
394// The rest of the callbacks are fairly self-explanatory...
395
396void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
397{
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200398 vlog.error(_("Invalid SetColourMapEntries from server!"));
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000399}
400
401void CConn::bell()
402{
403 fl_beep();
404}
405
406void CConn::serverCutText(const char* str, rdr::U32 len)
407{
Pierre Ossman689c4582011-05-26 15:39:41 +0000408 char *buffer;
409 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000410
Pierre Ossman50104882013-05-24 10:47:27 +0000411 if (!acceptClipboard)
412 return;
413
Pierre Ossman689c4582011-05-26 15:39:41 +0000414 size = fl_utf8froma(NULL, 0, str, len);
415 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000416 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000417
418 size++;
419
420 buffer = new char[size];
421
422 ret = fl_utf8froma(buffer, size, str, len);
423 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000424
Pierre Ossmanfb450fb2015-03-03 16:34:56 +0100425 vlog.debug("Got clipboard data (%d bytes)", (int)strlen(buffer));
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000426
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000427 // RFB doesn't have separate selection and clipboard concepts, so we
428 // dump the data into both variants.
429 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000430 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000431
432 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000433}
434
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100435void CConn::dataRect(const Rect& r, int encoding)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000436{
437 sock->inStream().startTiming();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000438
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100439 if (encoding != encodingCopyRect)
440 lastServerEncoding = encoding;
441
442 if (!Decoder::supported(encoding)) {
Pierre Ossman744e55c2014-12-03 14:00:54 +0100443 // TRANSLATORS: Refers to a VNC protocol encoding type
Pierre Ossmanc76bd2b2014-12-03 14:01:31 +0100444 vlog.error(_("Unknown encoding %d"), encoding);
445 throw Exception(_("Unknown encoding"));
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100446 }
447
448 if (!decoders[encoding]) {
Pierre Ossman668468b2014-01-31 12:37:32 +0100449 decoders[encoding] = Decoder::createDecoder(encoding, this);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100450 if (!decoders[encoding]) {
Pierre Ossmanc76bd2b2014-12-03 14:01:31 +0100451 vlog.error(_("Unknown encoding %d"), encoding);
452 throw Exception(_("Unknown encoding"));
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100453 }
454 }
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200455 decoders[encoding]->readRect(r, desktop->getFramebuffer());
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100456
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000457 sock->inStream().stopTiming();
458}
459
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000460void CConn::setCursor(int width, int height, const Point& hotspot,
461 void* data, void* mask)
462{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000463 desktop->setCursor(width, height, hotspot, data, mask);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000464}
465
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000466void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
467{
468 CMsgHandler::fence(flags, len, data);
469
470 if (flags & fenceFlagRequest) {
471 // We handle everything synchronously so we trivially honor these modes
472 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
473
474 writer()->writeFence(flags, len, data);
475 return;
476 }
477
478 if (len == 0) {
479 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000480 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000481 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000482
483 if (cp.supportsContinuousUpdates) {
484 vlog.info(_("Enabling continuous updates"));
485 continuousUpdates = true;
486 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
487 }
488 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000489 } else {
490 // Pixel format change
491 rdr::MemInStream memStream(data, len);
492 PixelFormat pf;
493
494 pf.read(&memStream);
495
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000496 cp.setPF(pf);
497 }
498}
499
DRC33c15e32011-11-03 18:49:21 +0000500
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000501////////////////////// Internal methods //////////////////////
502
503void CConn::resizeFramebuffer()
504{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000505 if (!desktop)
506 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000507
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000508 if (continuousUpdates)
509 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
510
Pierre Ossman6455d852011-06-01 09:33:00 +0000511 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000512}
513
514// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
515// to the connection speed:
516//
517// First we wait for at least one second of bandwidth measurement.
518//
519// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
520// which should be perceptually lossless.
521//
522// If the bandwidth is below that, we choose a more lossy JPEG quality.
523//
524// If the bandwidth drops below 256 Kbps, we switch to palette mode.
525//
526// Note: The system here is fairly arbitrary and should be replaced
527// with something more intelligent at the server end.
528//
529void CConn::autoSelectFormatAndEncoding()
530{
531 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
532 unsigned int timeWaited = sock->inStream().timeWaited();
533 bool newFullColour = fullColour;
534 int newQualityLevel = qualityLevel;
535
536 // Always use Tight
537 if (currentEncoding != encodingTight) {
538 currentEncoding = encodingTight;
539 encodingChange = true;
540 }
541
542 // Check that we have a decent bandwidth measurement
543 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
544 return;
545
546 // Select appropriate quality level
547 if (!noJpeg) {
548 if (kbitsPerSecond > 16000)
549 newQualityLevel = 8;
550 else
551 newQualityLevel = 6;
552
553 if (newQualityLevel != qualityLevel) {
554 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
555 kbitsPerSecond, newQualityLevel);
556 cp.qualityLevel = newQualityLevel;
557 qualityLevel.setParam(newQualityLevel);
558 encodingChange = true;
559 }
560 }
561
562 if (cp.beforeVersion(3, 8)) {
563 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
564 // cursors "asynchronously". If this happens in the middle of a
565 // pixel format change, the server will encode the cursor with
566 // the old format, but the client will try to decode it
567 // according to the new format. This will lead to a
568 // crash. Therefore, we do not allow automatic format change for
569 // old servers.
570 return;
571 }
572
573 // Select best color level
574 newFullColour = (kbitsPerSecond > 256);
575 if (newFullColour != fullColour) {
576 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
577 kbitsPerSecond,
578 newFullColour ? _("enabled") : _("disabled"));
579 fullColour.setParam(newFullColour);
580 formatChange = true;
581 }
582}
583
584// checkEncodings() sends a setEncodings message if one is needed.
585void CConn::checkEncodings()
586{
587 if (encodingChange && writer()) {
588 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
589 writer()->writeSetEncodings(currentEncoding, true);
590 encodingChange = false;
591 }
592}
593
594// requestNewUpdate() requests an update from the server, having set the
595// format and encoding appropriately.
596void CConn::requestNewUpdate()
597{
598 if (formatChange) {
599 PixelFormat pf;
600
601 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000602 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000603
604 if (fullColour) {
605 pf = fullColourPF;
606 } else {
607 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000608 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000609 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000610 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000611 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000612 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000613 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000614
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000615 if (supportsSyncFence) {
616 // We let the fence carry the pixel format and switch once we
617 // get the response back. That way we will be synchronised with
618 // when the server switches.
619 rdr::MemOutStream memStream;
620
621 pf.write(&memStream);
622
623 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
624 memStream.length(), (const char*)memStream.data());
625 } else {
626 // New requests are sent out at the start of processing the last
627 // one, so we cannot switch our internal format right now (doing so
628 // would mean misdecoding the current update).
629 pendingPFChange = true;
630 pendingPF = pf;
631 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000632
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000633 char str[256];
634 pf.print(str, 256);
635 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000636 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000637
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000638 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000639 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000640
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000641 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000642
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000643 if (forceNonincremental || !continuousUpdates) {
644 pendingUpdate = true;
645 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
646 !forceNonincremental);
647 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000648
649 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000650}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000651
652void CConn::handleOptions(void *data)
653{
654 CConn *self = (CConn*)data;
655
656 // Checking all the details of the current set of encodings is just
657 // a pain. Assume something has changed, as resending the encoding
658 // list is cheap. Avoid overriding what the auto logic has selected
659 // though.
660 if (!autoSelect) {
661 int encNum = encodingNum(preferredEncoding);
662
663 if (encNum != -1)
664 self->currentEncoding = encNum;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000665 }
666
Pierre Ossmanda389562011-06-09 08:24:37 +0000667 self->cp.supportsLocalCursor = true;
668
Pierre Ossmana22459d2014-03-17 14:42:10 +0100669 if (customCompressLevel)
670 self->cp.compressLevel = compressLevel;
671 else
672 self->cp.compressLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000673
Pierre Ossmana22459d2014-03-17 14:42:10 +0100674 if (!noJpeg && !autoSelect)
675 self->cp.qualityLevel = qualityLevel;
676 else
677 self->cp.qualityLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000678
679 self->encodingChange = true;
680
681 // Format changes refreshes the entire screen though and are therefore
682 // very costly. It's probably worth the effort to see if it is necessary
683 // here.
684 PixelFormat pf;
685
686 if (fullColour) {
687 pf = self->fullColourPF;
688 } else {
689 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000690 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000691 else if (lowColourLevel == 1)
692 pf = lowColourPF;
693 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000694 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000695 }
696
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000697 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000698 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000699
700 // Without fences, we cannot safely trigger an update request directly
701 // but must wait for the next update to arrive.
702 if (self->supportsSyncFence)
703 self->requestNewUpdate();
704 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000705}
Pierre Ossmand9b90032015-09-23 12:18:52 +0200706
707void CConn::handleUpdateTimeout(void *data)
708{
709 CConn *self = (CConn *)data;
710
711 assert(self);
712
713 self->desktop->updateWindow();
714
715 Fl::repeat_timeout(1.0, handleUpdateTimeout, data);
716}