blob: 2e97ec281987e06c1605b06d740eb65d94604bc0 [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 Ossman921f6c82017-02-24 12:33:09 +010076 frameCount(0), pixelCount(0), 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());
Dr. David Alan Gilbertf4d1d892017-07-11 12:11:50 +0100113 if (alertOnFatalError)
114 fl_alert("%s", e.str());
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000115 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 Ossman6a9e2e62011-05-19 14:47:43 +0000138 if (desktop)
139 delete desktop;
140
Pierre Ossman5e04c262011-11-09 11:31:12 +0000141 delete [] serverHost;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000142 if (sock)
143 Fl::remove_fd(sock->getFd());
144 delete sock;
145}
146
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000147void CConn::refreshFramebuffer()
148{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000149 forceNonincremental = true;
150
151 // Without fences, we cannot safely trigger an update request directly
152 // but must wait for the next update to arrive.
153 if (supportsSyncFence)
154 requestNewUpdate();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000155}
156
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000157const char *CConn::connectionInfo()
158{
159 static char infoText[1024] = "";
160
Pierre Ossmanb2046432014-09-22 11:17:34 +0200161 char scratch[100];
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000162 char pfStr[100];
Pierre Ossmanb2046432014-09-22 11:17:34 +0200163
164 // Crude way of avoiding constant overflow checks
165 assert((sizeof(scratch) + 1) * 10 < sizeof(infoText));
166
167 infoText[0] = '\0';
168
169 snprintf(scratch, sizeof(scratch),
170 _("Desktop name: %.80s"), cp.name());
171 strcat(infoText, scratch);
172 strcat(infoText, "\n");
173
174 snprintf(scratch, sizeof(scratch),
175 _("Host: %.80s port: %d"), serverHost, serverPort);
176 strcat(infoText, scratch);
177 strcat(infoText, "\n");
178
179 snprintf(scratch, sizeof(scratch),
180 _("Size: %d x %d"), cp.width, cp.height);
181 strcat(infoText, scratch);
182 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000183
Pierre Ossman744e55c2014-12-03 14:00:54 +0100184 // TRANSLATORS: Will be filled in with a string describing the
185 // protocol pixel format in a fairly language neutral way
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000186 cp.pf().print(pfStr, 100);
Pierre Ossmanb2046432014-09-22 11:17:34 +0200187 snprintf(scratch, sizeof(scratch),
188 _("Pixel format: %s"), pfStr);
189 strcat(infoText, scratch);
190 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000191
Pierre Ossman744e55c2014-12-03 14:00:54 +0100192 // TRANSLATORS: Similar to the earlier "Pixel format" string
Pierre Ossmanb2046432014-09-22 11:17:34 +0200193 serverPF.print(pfStr, 100);
194 snprintf(scratch, sizeof(scratch),
195 _("(server default %s)"), pfStr);
196 strcat(infoText, scratch);
197 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000198
Pierre Ossmanb2046432014-09-22 11:17:34 +0200199 snprintf(scratch, sizeof(scratch),
200 _("Requested encoding: %s"), encodingName(currentEncoding));
201 strcat(infoText, scratch);
202 strcat(infoText, "\n");
203
204 snprintf(scratch, sizeof(scratch),
205 _("Last used encoding: %s"), encodingName(lastServerEncoding));
206 strcat(infoText, scratch);
207 strcat(infoText, "\n");
208
209 snprintf(scratch, sizeof(scratch),
210 _("Line speed estimate: %d kbit/s"), sock->inStream().kbitsPerSecond());
211 strcat(infoText, scratch);
212 strcat(infoText, "\n");
213
214 snprintf(scratch, sizeof(scratch),
215 _("Protocol version: %d.%d"), cp.majorVersion, cp.minorVersion);
216 strcat(infoText, scratch);
217 strcat(infoText, "\n");
218
219 snprintf(scratch, sizeof(scratch),
220 _("Security method: %s"), secTypeName(csecurity->getType()));
221 strcat(infoText, scratch);
222 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000223
224 return infoText;
225}
226
Pierre Ossman921f6c82017-02-24 12:33:09 +0100227unsigned CConn::getFrameCount()
228{
229 return frameCount;
230}
231
232unsigned CConn::getPixelCount()
233{
234 return pixelCount;
235}
236
237unsigned CConn::getPosition()
238{
239 return sock->inStream().pos();
240}
241
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000242// The RFB core is not properly asynchronous, so it calls this callback
243// whenever it needs to block to wait for more data. Since FLTK is
244// monitoring the socket, we just make sure FLTK gets to run.
245
246void CConn::blockCallback()
247{
Pierre Ossman5102fa92015-11-18 16:23:21 +0100248 run_mainloop();
Pierre Ossman1db73242015-09-23 12:20:32 +0200249
250 if (should_exit())
251 throw rdr::Exception("Termination requested");
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000252}
253
DRC3e7ed812013-02-26 10:34:22 +0000254void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000255{
256 CConn *cc;
257 static bool recursing = false;
258
259 assert(data);
260 cc = (CConn*)data;
261
262 // I don't think processMsg() is recursion safe, so add this check
263 if (recursing)
264 return;
265
266 recursing = true;
267
268 try {
269 // processMsg() only processes one message, so we need to loop
270 // until the buffers are empty or things will stall.
271 do {
272 cc->processMsg();
Pierre Ossman9a73adf2016-12-29 16:52:56 +0100273
274 // Make sure that the FLTK handling and the timers gets some CPU
275 // time in case of back to back messages
276 Fl::check();
277 Timer::checkTimeouts();
278
279 // Also check if we need to stop reading and terminate
280 if (should_exit())
281 break;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000282 } while (cc->sock->inStream().checkNoWait(1));
283 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000284 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000285 exit_vncviewer();
286 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000287 vlog.error("%s", e.str());
Pierre Ossman1db73242015-09-23 12:20:32 +0200288 // Somebody might already have requested us to terminate, and
289 // might have already provided an error message.
290 if (!should_exit())
291 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000292 }
293
294 recursing = false;
295}
296
297////////////////////// CConnection callback methods //////////////////////
298
299// serverInit() is called when the serverInit message has been received. At
300// this point we create the desktop window and display it. We also tell the
301// server the pixel format and encodings to use and request the first update.
302void CConn::serverInit()
303{
304 CConnection::serverInit();
305
306 // If using AutoSelect with old servers, start in FullColor
307 // mode. See comment in autoSelectFormatAndEncoding.
308 if (cp.beforeVersion(3, 8) && autoSelect)
309 fullColour.setParam(true);
310
311 serverPF = cp.pf();
312
313 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
314 fullColourPF = desktop->getPreferredPF();
315
Pierre Ossman5d512c32011-11-04 11:42:16 +0000316 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000317 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000318
319 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000320 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000321
322 // This initial update request is a bit of a corner case, so we need
323 // to help out setting the correct format here.
324 assert(pendingPFChange);
Pierre Ossman5d512c32011-11-04 11:42:16 +0000325 cp.setPF(pendingPF);
326 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000327}
328
329// setDesktopSize() is called when the desktop size changes (including when
330// it is set initially).
331void CConn::setDesktopSize(int w, int h)
332{
333 CConnection::setDesktopSize(w,h);
334 resizeFramebuffer();
335}
336
337// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
Pierre Ossman28c1d542015-03-03 16:27:44 +0100338void CConn::setExtendedDesktopSize(unsigned reason, unsigned result,
339 int w, int h, const rfb::ScreenSet& layout)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000340{
341 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
342
343 if ((reason == reasonClient) && (result != resultSuccess)) {
344 vlog.error(_("SetDesktopSize failed: %d"), result);
345 return;
346 }
347
348 resizeFramebuffer();
349}
350
351// setName() is called when the desktop name changes
352void CConn::setName(const char* name)
353{
354 CConnection::setName(name);
355 if (desktop)
356 desktop->setName(name);
357}
358
359// framebufferUpdateStart() is called at the beginning of an update.
360// Here we try to send out a new framebuffer update request so that the
361// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000362// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000363void CConn::framebufferUpdateStart()
364{
Pierre Ossman3da238d2015-11-12 12:20:05 +0100365 CConnection::framebufferUpdateStart();
366
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000367 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000368 pendingUpdate = false;
369
370 requestNewUpdate();
Pierre Ossmand9b90032015-09-23 12:18:52 +0200371
372 // Update the screen prematurely for very slow updates
373 Fl::add_timeout(1.0, handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000374}
375
376// framebufferUpdateEnd() is called at the end of an update.
377// For each rectangle, the FdInStream will have timed the speed
378// of the connection, allowing us to select format and encoding
379// appropriately, and then request another incremental update.
380void CConn::framebufferUpdateEnd()
381{
Pierre Ossman3da238d2015-11-12 12:20:05 +0100382 CConnection::framebufferUpdateEnd();
383
Pierre Ossman921f6c82017-02-24 12:33:09 +0100384 frameCount++;
385
Pierre Ossmand9b90032015-09-23 12:18:52 +0200386 Fl::remove_timeout(handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000387 desktop->updateWindow();
388
389 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000390 // We need fences to make extra update requests and continuous
391 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000392 if (cp.supportsFence)
393 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
394
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000395 firstUpdate = false;
396 }
397
Pierre Ossman5d512c32011-11-04 11:42:16 +0000398 // A format change has been scheduled and we are now past the update
399 // with the old format. Time to active the new one.
400 if (pendingPFChange) {
Pierre Ossman5d512c32011-11-04 11:42:16 +0000401 cp.setPF(pendingPF);
402 pendingPFChange = false;
403 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000404
405 // Compute new settings based on updated bandwidth values
406 if (autoSelect)
407 autoSelectFormatAndEncoding();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000408}
409
410// The rest of the callbacks are fairly self-explanatory...
411
412void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
413{
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200414 vlog.error(_("Invalid SetColourMapEntries from server!"));
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000415}
416
417void CConn::bell()
418{
419 fl_beep();
420}
421
422void CConn::serverCutText(const char* str, rdr::U32 len)
423{
Pierre Ossman689c4582011-05-26 15:39:41 +0000424 char *buffer;
425 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000426
Pierre Ossman50104882013-05-24 10:47:27 +0000427 if (!acceptClipboard)
428 return;
429
Pierre Ossman689c4582011-05-26 15:39:41 +0000430 size = fl_utf8froma(NULL, 0, str, len);
431 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000432 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000433
434 size++;
435
436 buffer = new char[size];
437
438 ret = fl_utf8froma(buffer, size, str, len);
439 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000440
Pierre Ossmanfb450fb2015-03-03 16:34:56 +0100441 vlog.debug("Got clipboard data (%d bytes)", (int)strlen(buffer));
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000442
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000443 // RFB doesn't have separate selection and clipboard concepts, so we
444 // dump the data into both variants.
Pierre Ossmanf862c2e2016-03-29 14:15:38 +0200445 if (setPrimary)
446 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000447 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000448
449 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000450}
451
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100452void CConn::dataRect(const Rect& r, int encoding)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000453{
454 sock->inStream().startTiming();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000455
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100456 if (encoding != encodingCopyRect)
457 lastServerEncoding = encoding;
458
Pierre Ossman9f273e92015-11-09 16:34:54 +0100459 CConnection::dataRect(r, encoding);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100460
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000461 sock->inStream().stopTiming();
Pierre Ossman921f6c82017-02-24 12:33:09 +0100462
463 pixelCount += r.area();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000464}
465
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000466void CConn::setCursor(int width, int height, const Point& hotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100467 const rdr::U8* data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000468{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100469 desktop->setCursor(width, height, hotspot, data);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000470}
471
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000472void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
473{
474 CMsgHandler::fence(flags, len, data);
475
476 if (flags & fenceFlagRequest) {
477 // We handle everything synchronously so we trivially honor these modes
478 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
479
480 writer()->writeFence(flags, len, data);
481 return;
482 }
483
484 if (len == 0) {
485 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000486 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000487 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000488
489 if (cp.supportsContinuousUpdates) {
490 vlog.info(_("Enabling continuous updates"));
491 continuousUpdates = true;
492 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
493 }
494 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000495 } else {
496 // Pixel format change
497 rdr::MemInStream memStream(data, len);
498 PixelFormat pf;
499
500 pf.read(&memStream);
501
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000502 cp.setPF(pf);
503 }
504}
505
DRC33c15e32011-11-03 18:49:21 +0000506
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000507////////////////////// Internal methods //////////////////////
508
509void CConn::resizeFramebuffer()
510{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000511 if (!desktop)
512 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000513
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000514 if (continuousUpdates)
515 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
516
Pierre Ossman6455d852011-06-01 09:33:00 +0000517 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000518}
519
520// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
521// to the connection speed:
522//
523// First we wait for at least one second of bandwidth measurement.
524//
525// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
526// which should be perceptually lossless.
527//
528// If the bandwidth is below that, we choose a more lossy JPEG quality.
529//
530// If the bandwidth drops below 256 Kbps, we switch to palette mode.
531//
532// Note: The system here is fairly arbitrary and should be replaced
533// with something more intelligent at the server end.
534//
535void CConn::autoSelectFormatAndEncoding()
536{
537 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
538 unsigned int timeWaited = sock->inStream().timeWaited();
539 bool newFullColour = fullColour;
540 int newQualityLevel = qualityLevel;
541
542 // Always use Tight
543 if (currentEncoding != encodingTight) {
544 currentEncoding = encodingTight;
545 encodingChange = true;
546 }
547
548 // Check that we have a decent bandwidth measurement
549 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
550 return;
551
552 // Select appropriate quality level
553 if (!noJpeg) {
554 if (kbitsPerSecond > 16000)
555 newQualityLevel = 8;
556 else
557 newQualityLevel = 6;
558
559 if (newQualityLevel != qualityLevel) {
560 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
561 kbitsPerSecond, newQualityLevel);
562 cp.qualityLevel = newQualityLevel;
563 qualityLevel.setParam(newQualityLevel);
564 encodingChange = true;
565 }
566 }
567
568 if (cp.beforeVersion(3, 8)) {
569 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
570 // cursors "asynchronously". If this happens in the middle of a
571 // pixel format change, the server will encode the cursor with
572 // the old format, but the client will try to decode it
573 // according to the new format. This will lead to a
574 // crash. Therefore, we do not allow automatic format change for
575 // old servers.
576 return;
577 }
578
579 // Select best color level
580 newFullColour = (kbitsPerSecond > 256);
581 if (newFullColour != fullColour) {
582 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
583 kbitsPerSecond,
584 newFullColour ? _("enabled") : _("disabled"));
585 fullColour.setParam(newFullColour);
586 formatChange = true;
587 }
588}
589
590// checkEncodings() sends a setEncodings message if one is needed.
591void CConn::checkEncodings()
592{
593 if (encodingChange && writer()) {
594 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
595 writer()->writeSetEncodings(currentEncoding, true);
596 encodingChange = false;
597 }
598}
599
600// requestNewUpdate() requests an update from the server, having set the
601// format and encoding appropriately.
602void CConn::requestNewUpdate()
603{
604 if (formatChange) {
605 PixelFormat pf;
606
607 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000608 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000609
610 if (fullColour) {
611 pf = fullColourPF;
612 } else {
613 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000614 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000615 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000616 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000617 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000618 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000619 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000620
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000621 if (supportsSyncFence) {
622 // We let the fence carry the pixel format and switch once we
623 // get the response back. That way we will be synchronised with
624 // when the server switches.
625 rdr::MemOutStream memStream;
626
627 pf.write(&memStream);
628
629 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
630 memStream.length(), (const char*)memStream.data());
631 } else {
632 // New requests are sent out at the start of processing the last
633 // one, so we cannot switch our internal format right now (doing so
634 // would mean misdecoding the current update).
635 pendingPFChange = true;
636 pendingPF = pf;
637 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000638
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000639 char str[256];
640 pf.print(str, 256);
641 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000642 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000643
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000644 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000645 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000646
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000647 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000648
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000649 if (forceNonincremental || !continuousUpdates) {
650 pendingUpdate = true;
651 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
652 !forceNonincremental);
653 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000654
655 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000656}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000657
658void CConn::handleOptions(void *data)
659{
660 CConn *self = (CConn*)data;
661
662 // Checking all the details of the current set of encodings is just
663 // a pain. Assume something has changed, as resending the encoding
664 // list is cheap. Avoid overriding what the auto logic has selected
665 // though.
666 if (!autoSelect) {
667 int encNum = encodingNum(preferredEncoding);
668
669 if (encNum != -1)
670 self->currentEncoding = encNum;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000671 }
672
Pierre Ossmanda389562011-06-09 08:24:37 +0000673 self->cp.supportsLocalCursor = true;
674
Pierre Ossmana22459d2014-03-17 14:42:10 +0100675 if (customCompressLevel)
676 self->cp.compressLevel = compressLevel;
677 else
678 self->cp.compressLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000679
Pierre Ossmana22459d2014-03-17 14:42:10 +0100680 if (!noJpeg && !autoSelect)
681 self->cp.qualityLevel = qualityLevel;
682 else
683 self->cp.qualityLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000684
685 self->encodingChange = true;
686
687 // Format changes refreshes the entire screen though and are therefore
688 // very costly. It's probably worth the effort to see if it is necessary
689 // here.
690 PixelFormat pf;
691
692 if (fullColour) {
693 pf = self->fullColourPF;
694 } else {
695 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000696 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000697 else if (lowColourLevel == 1)
698 pf = lowColourPF;
699 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000700 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000701 }
702
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000703 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000704 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000705
706 // Without fences, we cannot safely trigger an update request directly
707 // but must wait for the next update to arrive.
708 if (self->supportsSyncFence)
709 self->requestNewUpdate();
710 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000711}
Pierre Ossmand9b90032015-09-23 12:18:52 +0200712
713void CConn::handleUpdateTimeout(void *data)
714{
715 CConn *self = (CConn *)data;
716
717 assert(self);
718
719 self->desktop->updateWindow();
720
721 Fl::repeat_timeout(1.0, handleUpdateTimeout, data);
722}