blob: baf01d415a87379d408fb9ec6943faf12108e4ea [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>
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 Ossmanc9dd3a42015-11-18 16:24:16 +010049#include "PlatformPixelBuffer.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000050#include "i18n.h"
51#include "parameters.h"
Pierre Ossman39ceb502011-07-12 15:54:25 +000052#include "vncviewer.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000053
DRCb65bb932011-06-24 03:17:00 +000054#ifdef WIN32
55#include "win32.h"
56#endif
57
Pierre Ossman5156d5e2011-03-09 09:42:34 +000058using namespace rdr;
59using namespace rfb;
60using namespace std;
61
Pierre Ossman5156d5e2011-03-09 09:42:34 +000062static rfb::LogWriter vlog("CConn");
63
Pierre Ossmancf836f22011-07-14 14:34:09 +000064// 8 colours (1 bit per component)
65static const PixelFormat verylowColourPF(8, 3,false, true,
66 1, 1, 1, 2, 1, 0);
67// 64 colours (2 bits per component)
68static const PixelFormat lowColourPF(8, 6, false, true,
69 3, 3, 3, 4, 2, 0);
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010070// 256 colours (2-3 bits per component)
71static const PixelFormat mediumColourPF(8, 8, false, true,
72 7, 7, 3, 5, 2, 0);
Pierre Ossmanf4f30942011-05-17 09:39:07 +000073
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000074CConn::CConn(const char* vncServerName, network::Socket* socket=NULL)
75 : serverHost(0), serverPort(0), desktop(NULL),
Pierre Ossman5d512c32011-11-04 11:42:16 +000076 pendingPFChange(false),
Pierre Ossman5156d5e2011-03-09 09:42:34 +000077 currentEncoding(encodingTight), lastServerEncoding((unsigned int)-1),
78 formatChange(false), encodingChange(false),
Pierre Ossmanaa73c892011-11-15 12:13:37 +000079 firstUpdate(true), pendingUpdate(false), continuousUpdates(false),
Pierre Ossmane28bdb22011-11-14 16:02:06 +000080 forceNonincremental(true), supportsSyncFence(false)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000081{
82 setShared(::shared);
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000083 sock = socket;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000084
85 int encNum = encodingNum(preferredEncoding);
86 if (encNum != -1)
87 currentEncoding = encNum;
88
Pierre Ossmanda389562011-06-09 08:24:37 +000089 cp.supportsLocalCursor = true;
90
Pierre Ossman5156d5e2011-03-09 09:42:34 +000091 cp.supportsDesktopResize = true;
92 cp.supportsExtendedDesktopSize = true;
93 cp.supportsDesktopRename = true;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000094
Pierre Ossmana22459d2014-03-17 14:42:10 +010095 if (customCompressLevel)
96 cp.compressLevel = compressLevel;
97 else
98 cp.compressLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000099
Pierre Ossmana22459d2014-03-17 14:42:10 +0100100 if (!noJpeg)
101 cp.qualityLevel = qualityLevel;
102 else
103 cp.qualityLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000104
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000105 if(sock == NULL) {
106 try {
107 getHostAndPort(vncServerName, &serverHost, &serverPort);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000108
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000109 sock = new network::TcpSocket(serverHost, serverPort);
110 vlog.info(_("connected to host %s port %d"), serverHost, serverPort);
111 } catch (rdr::Exception& e) {
112 vlog.error("%s", e.str());
113 fl_alert("%s", e.str());
114 exit_vncviewer();
115 return;
116 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000117 }
118
119 Fl::add_fd(sock->getFd(), FL_READ | FL_EXCEPT, socketEvent, this);
120
121 // See callback below
122 sock->inStream().setBlockCallback(this);
123
124 setServerName(serverHost);
125 setStreams(&sock->inStream(), &sock->outStream());
126
127 initialiseProtocol();
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000128
129 OptionsDialog::addCallback(handleOptions, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000130}
131
132CConn::~CConn()
133{
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000134 OptionsDialog::removeCallback(handleOptions);
Pierre Ossmand9b90032015-09-23 12:18:52 +0200135 Fl::remove_timeout(handleUpdateTimeout, this);
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000136
Pierre Ossman6a9e2e62011-05-19 14:47:43 +0000137 if (desktop)
138 delete desktop;
139
Pierre Ossman5e04c262011-11-09 11:31:12 +0000140 delete [] serverHost;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000141 if (sock)
142 Fl::remove_fd(sock->getFd());
143 delete sock;
144}
145
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000146void CConn::refreshFramebuffer()
147{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000148 forceNonincremental = true;
149
150 // Without fences, we cannot safely trigger an update request directly
151 // but must wait for the next update to arrive.
152 if (supportsSyncFence)
153 requestNewUpdate();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000154}
155
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000156const char *CConn::connectionInfo()
157{
158 static char infoText[1024] = "";
159
Pierre Ossmanb2046432014-09-22 11:17:34 +0200160 char scratch[100];
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000161 char pfStr[100];
Pierre Ossmanb2046432014-09-22 11:17:34 +0200162
163 // Crude way of avoiding constant overflow checks
164 assert((sizeof(scratch) + 1) * 10 < sizeof(infoText));
165
166 infoText[0] = '\0';
167
168 snprintf(scratch, sizeof(scratch),
169 _("Desktop name: %.80s"), cp.name());
170 strcat(infoText, scratch);
171 strcat(infoText, "\n");
172
173 snprintf(scratch, sizeof(scratch),
174 _("Host: %.80s port: %d"), serverHost, serverPort);
175 strcat(infoText, scratch);
176 strcat(infoText, "\n");
177
178 snprintf(scratch, sizeof(scratch),
179 _("Size: %d x %d"), cp.width, cp.height);
180 strcat(infoText, scratch);
181 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000182
Pierre Ossman744e55c2014-12-03 14:00:54 +0100183 // TRANSLATORS: Will be filled in with a string describing the
184 // protocol pixel format in a fairly language neutral way
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000185 cp.pf().print(pfStr, 100);
Pierre Ossmanb2046432014-09-22 11:17:34 +0200186 snprintf(scratch, sizeof(scratch),
187 _("Pixel format: %s"), pfStr);
188 strcat(infoText, scratch);
189 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000190
Pierre Ossman744e55c2014-12-03 14:00:54 +0100191 // TRANSLATORS: Similar to the earlier "Pixel format" string
Pierre Ossmanb2046432014-09-22 11:17:34 +0200192 serverPF.print(pfStr, 100);
193 snprintf(scratch, sizeof(scratch),
194 _("(server default %s)"), pfStr);
195 strcat(infoText, scratch);
196 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000197
Pierre Ossmanb2046432014-09-22 11:17:34 +0200198 snprintf(scratch, sizeof(scratch),
199 _("Requested encoding: %s"), encodingName(currentEncoding));
200 strcat(infoText, scratch);
201 strcat(infoText, "\n");
202
203 snprintf(scratch, sizeof(scratch),
204 _("Last used encoding: %s"), encodingName(lastServerEncoding));
205 strcat(infoText, scratch);
206 strcat(infoText, "\n");
207
208 snprintf(scratch, sizeof(scratch),
209 _("Line speed estimate: %d kbit/s"), sock->inStream().kbitsPerSecond());
210 strcat(infoText, scratch);
211 strcat(infoText, "\n");
212
213 snprintf(scratch, sizeof(scratch),
214 _("Protocol version: %d.%d"), cp.majorVersion, cp.minorVersion);
215 strcat(infoText, scratch);
216 strcat(infoText, "\n");
217
218 snprintf(scratch, sizeof(scratch),
219 _("Security method: %s"), secTypeName(csecurity->getType()));
220 strcat(infoText, scratch);
221 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000222
223 return infoText;
224}
225
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000226// The RFB core is not properly asynchronous, so it calls this callback
227// whenever it needs to block to wait for more data. Since FLTK is
228// monitoring the socket, we just make sure FLTK gets to run.
229
230void CConn::blockCallback()
231{
Pierre Ossman5102fa92015-11-18 16:23:21 +0100232 run_mainloop();
Pierre Ossman1db73242015-09-23 12:20:32 +0200233
234 if (should_exit())
235 throw rdr::Exception("Termination requested");
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000236}
237
DRC3e7ed812013-02-26 10:34:22 +0000238void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000239{
240 CConn *cc;
241 static bool recursing = false;
242
243 assert(data);
244 cc = (CConn*)data;
245
246 // I don't think processMsg() is recursion safe, so add this check
247 if (recursing)
248 return;
249
250 recursing = true;
251
252 try {
253 // processMsg() only processes one message, so we need to loop
254 // until the buffers are empty or things will stall.
255 do {
256 cc->processMsg();
Pierre Ossman9a73adf2016-12-29 16:52:56 +0100257
258 // Make sure that the FLTK handling and the timers gets some CPU
259 // time in case of back to back messages
260 Fl::check();
261 Timer::checkTimeouts();
262
263 // Also check if we need to stop reading and terminate
264 if (should_exit())
265 break;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000266 } while (cc->sock->inStream().checkNoWait(1));
267 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000268 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000269 exit_vncviewer();
270 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000271 vlog.error("%s", e.str());
Pierre Ossman1db73242015-09-23 12:20:32 +0200272 // Somebody might already have requested us to terminate, and
273 // might have already provided an error message.
274 if (!should_exit())
275 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000276 }
277
278 recursing = false;
279}
280
281////////////////////// CConnection callback methods //////////////////////
282
283// serverInit() is called when the serverInit message has been received. At
284// this point we create the desktop window and display it. We also tell the
285// server the pixel format and encodings to use and request the first update.
286void CConn::serverInit()
287{
288 CConnection::serverInit();
289
290 // If using AutoSelect with old servers, start in FullColor
291 // mode. See comment in autoSelectFormatAndEncoding.
292 if (cp.beforeVersion(3, 8) && autoSelect)
293 fullColour.setParam(true);
294
295 serverPF = cp.pf();
296
297 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
298 fullColourPF = desktop->getPreferredPF();
299
Pierre Ossman5d512c32011-11-04 11:42:16 +0000300 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000301 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000302
303 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000304 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000305
306 // This initial update request is a bit of a corner case, so we need
307 // to help out setting the correct format here.
308 assert(pendingPFChange);
Pierre Ossman5d512c32011-11-04 11:42:16 +0000309 cp.setPF(pendingPF);
310 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000311}
312
313// setDesktopSize() is called when the desktop size changes (including when
314// it is set initially).
315void CConn::setDesktopSize(int w, int h)
316{
317 CConnection::setDesktopSize(w,h);
318 resizeFramebuffer();
319}
320
321// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
Pierre Ossman28c1d542015-03-03 16:27:44 +0100322void CConn::setExtendedDesktopSize(unsigned reason, unsigned result,
323 int w, int h, const rfb::ScreenSet& layout)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000324{
325 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
326
327 if ((reason == reasonClient) && (result != resultSuccess)) {
328 vlog.error(_("SetDesktopSize failed: %d"), result);
329 return;
330 }
331
332 resizeFramebuffer();
333}
334
335// setName() is called when the desktop name changes
336void CConn::setName(const char* name)
337{
338 CConnection::setName(name);
339 if (desktop)
340 desktop->setName(name);
341}
342
343// framebufferUpdateStart() is called at the beginning of an update.
344// Here we try to send out a new framebuffer update request so that the
345// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000346// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000347void CConn::framebufferUpdateStart()
348{
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +0100349 ModifiablePixelBuffer* pb;
350 PlatformPixelBuffer* ppb;
351
Pierre Ossman3da238d2015-11-12 12:20:05 +0100352 CConnection::framebufferUpdateStart();
353
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000354 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000355 pendingUpdate = false;
356
357 requestNewUpdate();
Pierre Ossmand9b90032015-09-23 12:18:52 +0200358
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +0100359 // We might still be rendering the previous update
360 pb = getFramebuffer();
361 assert(pb != NULL);
362 ppb = dynamic_cast<PlatformPixelBuffer*>(pb);
363 assert(ppb != NULL);
364 if (ppb->isRendering()) {
365 // Need to stop monitoring the socket or we'll just busy loop
366 assert(sock != NULL);
367 Fl::remove_fd(sock->getFd());
368
369 while (ppb->isRendering())
370 run_mainloop();
371
372 Fl::add_fd(sock->getFd(), FL_READ | FL_EXCEPT, socketEvent, this);
373 }
374
Pierre Ossmand9b90032015-09-23 12:18:52 +0200375 // Update the screen prematurely for very slow updates
376 Fl::add_timeout(1.0, handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000377}
378
379// framebufferUpdateEnd() is called at the end of an update.
380// For each rectangle, the FdInStream will have timed the speed
381// of the connection, allowing us to select format and encoding
382// appropriately, and then request another incremental update.
383void CConn::framebufferUpdateEnd()
384{
Pierre Ossman3da238d2015-11-12 12:20:05 +0100385 CConnection::framebufferUpdateEnd();
386
Pierre Ossmand9b90032015-09-23 12:18:52 +0200387 Fl::remove_timeout(handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000388 desktop->updateWindow();
389
390 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000391 // We need fences to make extra update requests and continuous
392 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000393 if (cp.supportsFence)
394 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
395
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000396 firstUpdate = false;
397 }
398
Pierre Ossman5d512c32011-11-04 11:42:16 +0000399 // A format change has been scheduled and we are now past the update
400 // with the old format. Time to active the new one.
401 if (pendingPFChange) {
Pierre Ossman5d512c32011-11-04 11:42:16 +0000402 cp.setPF(pendingPF);
403 pendingPFChange = false;
404 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000405
406 // Compute new settings based on updated bandwidth values
407 if (autoSelect)
408 autoSelectFormatAndEncoding();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000409}
410
411// The rest of the callbacks are fairly self-explanatory...
412
413void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
414{
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200415 vlog.error(_("Invalid SetColourMapEntries from server!"));
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000416}
417
418void CConn::bell()
419{
420 fl_beep();
421}
422
423void CConn::serverCutText(const char* str, rdr::U32 len)
424{
Pierre Ossman689c4582011-05-26 15:39:41 +0000425 char *buffer;
426 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000427
Pierre Ossman50104882013-05-24 10:47:27 +0000428 if (!acceptClipboard)
429 return;
430
Pierre Ossman689c4582011-05-26 15:39:41 +0000431 size = fl_utf8froma(NULL, 0, str, len);
432 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000433 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000434
435 size++;
436
437 buffer = new char[size];
438
439 ret = fl_utf8froma(buffer, size, str, len);
440 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000441
Pierre Ossmanfb450fb2015-03-03 16:34:56 +0100442 vlog.debug("Got clipboard data (%d bytes)", (int)strlen(buffer));
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000443
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000444 // RFB doesn't have separate selection and clipboard concepts, so we
445 // dump the data into both variants.
Pierre Ossmanf862c2e2016-03-29 14:15:38 +0200446 if (setPrimary)
447 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000448 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000449
450 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000451}
452
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100453void CConn::dataRect(const Rect& r, int encoding)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000454{
455 sock->inStream().startTiming();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000456
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100457 if (encoding != encodingCopyRect)
458 lastServerEncoding = encoding;
459
Pierre Ossman9f273e92015-11-09 16:34:54 +0100460 CConnection::dataRect(r, encoding);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100461
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000462 sock->inStream().stopTiming();
463}
464
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000465void CConn::setCursor(int width, int height, const Point& hotspot,
466 void* data, void* mask)
467{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000468 desktop->setCursor(width, height, hotspot, data, mask);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000469}
470
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000471void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
472{
473 CMsgHandler::fence(flags, len, data);
474
475 if (flags & fenceFlagRequest) {
476 // We handle everything synchronously so we trivially honor these modes
477 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
478
479 writer()->writeFence(flags, len, data);
480 return;
481 }
482
483 if (len == 0) {
484 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000485 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000486 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000487
488 if (cp.supportsContinuousUpdates) {
489 vlog.info(_("Enabling continuous updates"));
490 continuousUpdates = true;
491 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
492 }
493 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000494 } else {
495 // Pixel format change
496 rdr::MemInStream memStream(data, len);
497 PixelFormat pf;
498
499 pf.read(&memStream);
500
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000501 cp.setPF(pf);
502 }
503}
504
DRC33c15e32011-11-03 18:49:21 +0000505
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000506////////////////////// Internal methods //////////////////////
507
508void CConn::resizeFramebuffer()
509{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000510 if (!desktop)
511 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000512
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000513 if (continuousUpdates)
514 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
515
Pierre Ossman6455d852011-06-01 09:33:00 +0000516 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) {
581 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
582 kbitsPerSecond,
583 newFullColour ? _("enabled") : _("disabled"));
584 fullColour.setParam(newFullColour);
585 formatChange = true;
586 }
587}
588
589// checkEncodings() sends a setEncodings message if one is needed.
590void CConn::checkEncodings()
591{
592 if (encodingChange && writer()) {
593 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
594 writer()->writeSetEncodings(currentEncoding, true);
595 encodingChange = false;
596 }
597}
598
599// requestNewUpdate() requests an update from the server, having set the
600// format and encoding appropriately.
601void CConn::requestNewUpdate()
602{
603 if (formatChange) {
604 PixelFormat pf;
605
606 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000607 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000608
609 if (fullColour) {
610 pf = fullColourPF;
611 } else {
612 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000613 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000614 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000615 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000616 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000617 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000618 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000619
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000620 if (supportsSyncFence) {
621 // We let the fence carry the pixel format and switch once we
622 // get the response back. That way we will be synchronised with
623 // when the server switches.
624 rdr::MemOutStream memStream;
625
626 pf.write(&memStream);
627
628 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
629 memStream.length(), (const char*)memStream.data());
630 } else {
631 // New requests are sent out at the start of processing the last
632 // one, so we cannot switch our internal format right now (doing so
633 // would mean misdecoding the current update).
634 pendingPFChange = true;
635 pendingPF = pf;
636 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000637
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000638 char str[256];
639 pf.print(str, 256);
640 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000641 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000642
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000643 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000644 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000645
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000646 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000647
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000648 if (forceNonincremental || !continuousUpdates) {
649 pendingUpdate = true;
650 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
651 !forceNonincremental);
652 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000653
654 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000655}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000656
657void CConn::handleOptions(void *data)
658{
659 CConn *self = (CConn*)data;
660
661 // Checking all the details of the current set of encodings is just
662 // a pain. Assume something has changed, as resending the encoding
663 // list is cheap. Avoid overriding what the auto logic has selected
664 // though.
665 if (!autoSelect) {
666 int encNum = encodingNum(preferredEncoding);
667
668 if (encNum != -1)
669 self->currentEncoding = encNum;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000670 }
671
Pierre Ossmanda389562011-06-09 08:24:37 +0000672 self->cp.supportsLocalCursor = true;
673
Pierre Ossmana22459d2014-03-17 14:42:10 +0100674 if (customCompressLevel)
675 self->cp.compressLevel = compressLevel;
676 else
677 self->cp.compressLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000678
Pierre Ossmana22459d2014-03-17 14:42:10 +0100679 if (!noJpeg && !autoSelect)
680 self->cp.qualityLevel = qualityLevel;
681 else
682 self->cp.qualityLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000683
684 self->encodingChange = true;
685
686 // Format changes refreshes the entire screen though and are therefore
687 // very costly. It's probably worth the effort to see if it is necessary
688 // here.
689 PixelFormat pf;
690
691 if (fullColour) {
692 pf = self->fullColourPF;
693 } else {
694 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000695 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000696 else if (lowColourLevel == 1)
697 pf = lowColourPF;
698 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000699 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000700 }
701
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000702 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000703 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000704
705 // Without fences, we cannot safely trigger an update request directly
706 // but must wait for the next update to arrive.
707 if (self->supportsSyncFence)
708 self->requestNewUpdate();
709 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000710}
Pierre Ossmand9b90032015-09-23 12:18:52 +0200711
712void CConn::handleUpdateTimeout(void *data)
713{
714 CConn *self = (CConn *)data;
715
716 assert(self);
717
718 self->desktop->updateWindow();
719
720 Fl::repeat_timeout(1.0, handleUpdateTimeout, data);
721}