blob: 35d6e231d3d56b3ad7824124886648d9db27886b [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 Ossmanfecf0a42018-03-26 12:22:47 +020076 updateCount(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 Ossman2fa63f82016-12-05 15:26:21 +010095 cp.supportsLEDState = true;
96
Pierre Ossmana22459d2014-03-17 14:42:10 +010097 if (customCompressLevel)
98 cp.compressLevel = compressLevel;
99 else
100 cp.compressLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000101
Pierre Ossmana22459d2014-03-17 14:42:10 +0100102 if (!noJpeg)
103 cp.qualityLevel = qualityLevel;
104 else
105 cp.qualityLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000106
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000107 if(sock == NULL) {
108 try {
109 getHostAndPort(vncServerName, &serverHost, &serverPort);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000110
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000111 sock = new network::TcpSocket(serverHost, serverPort);
112 vlog.info(_("connected to host %s port %d"), serverHost, serverPort);
113 } catch (rdr::Exception& e) {
114 vlog.error("%s", e.str());
Dr. David Alan Gilbertf4d1d892017-07-11 12:11:50 +0100115 if (alertOnFatalError)
116 fl_alert("%s", e.str());
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000117 exit_vncviewer();
118 return;
119 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000120 }
121
122 Fl::add_fd(sock->getFd(), FL_READ | FL_EXCEPT, socketEvent, this);
123
124 // See callback below
125 sock->inStream().setBlockCallback(this);
126
127 setServerName(serverHost);
128 setStreams(&sock->inStream(), &sock->outStream());
129
130 initialiseProtocol();
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000131
132 OptionsDialog::addCallback(handleOptions, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000133}
134
135CConn::~CConn()
136{
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000137 OptionsDialog::removeCallback(handleOptions);
Pierre Ossmand9b90032015-09-23 12:18:52 +0200138 Fl::remove_timeout(handleUpdateTimeout, this);
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000139
Pierre Ossman6a9e2e62011-05-19 14:47:43 +0000140 if (desktop)
141 delete desktop;
142
Pierre Ossman5e04c262011-11-09 11:31:12 +0000143 delete [] serverHost;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000144 if (sock)
145 Fl::remove_fd(sock->getFd());
146 delete sock;
147}
148
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000149void CConn::refreshFramebuffer()
150{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000151 forceNonincremental = true;
152
153 // Without fences, we cannot safely trigger an update request directly
154 // but must wait for the next update to arrive.
155 if (supportsSyncFence)
156 requestNewUpdate();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000157}
158
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000159const char *CConn::connectionInfo()
160{
161 static char infoText[1024] = "";
162
Pierre Ossmanb2046432014-09-22 11:17:34 +0200163 char scratch[100];
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000164 char pfStr[100];
Pierre Ossmanb2046432014-09-22 11:17:34 +0200165
166 // Crude way of avoiding constant overflow checks
167 assert((sizeof(scratch) + 1) * 10 < sizeof(infoText));
168
169 infoText[0] = '\0';
170
171 snprintf(scratch, sizeof(scratch),
172 _("Desktop name: %.80s"), cp.name());
173 strcat(infoText, scratch);
174 strcat(infoText, "\n");
175
176 snprintf(scratch, sizeof(scratch),
177 _("Host: %.80s port: %d"), serverHost, serverPort);
178 strcat(infoText, scratch);
179 strcat(infoText, "\n");
180
181 snprintf(scratch, sizeof(scratch),
182 _("Size: %d x %d"), cp.width, cp.height);
183 strcat(infoText, scratch);
184 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000185
Pierre Ossman744e55c2014-12-03 14:00:54 +0100186 // TRANSLATORS: Will be filled in with a string describing the
187 // protocol pixel format in a fairly language neutral way
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000188 cp.pf().print(pfStr, 100);
Pierre Ossmanb2046432014-09-22 11:17:34 +0200189 snprintf(scratch, sizeof(scratch),
190 _("Pixel format: %s"), pfStr);
191 strcat(infoText, scratch);
192 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000193
Pierre Ossman744e55c2014-12-03 14:00:54 +0100194 // TRANSLATORS: Similar to the earlier "Pixel format" string
Pierre Ossmanb2046432014-09-22 11:17:34 +0200195 serverPF.print(pfStr, 100);
196 snprintf(scratch, sizeof(scratch),
197 _("(server default %s)"), pfStr);
198 strcat(infoText, scratch);
199 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000200
Pierre Ossmanb2046432014-09-22 11:17:34 +0200201 snprintf(scratch, sizeof(scratch),
202 _("Requested encoding: %s"), encodingName(currentEncoding));
203 strcat(infoText, scratch);
204 strcat(infoText, "\n");
205
206 snprintf(scratch, sizeof(scratch),
207 _("Last used encoding: %s"), encodingName(lastServerEncoding));
208 strcat(infoText, scratch);
209 strcat(infoText, "\n");
210
211 snprintf(scratch, sizeof(scratch),
212 _("Line speed estimate: %d kbit/s"), sock->inStream().kbitsPerSecond());
213 strcat(infoText, scratch);
214 strcat(infoText, "\n");
215
216 snprintf(scratch, sizeof(scratch),
217 _("Protocol version: %d.%d"), cp.majorVersion, cp.minorVersion);
218 strcat(infoText, scratch);
219 strcat(infoText, "\n");
220
221 snprintf(scratch, sizeof(scratch),
222 _("Security method: %s"), secTypeName(csecurity->getType()));
223 strcat(infoText, scratch);
224 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000225
226 return infoText;
227}
228
Pierre Ossmanfecf0a42018-03-26 12:22:47 +0200229unsigned CConn::getUpdateCount()
Pierre Ossman921f6c82017-02-24 12:33:09 +0100230{
Pierre Ossmanfecf0a42018-03-26 12:22:47 +0200231 return updateCount;
Pierre Ossman921f6c82017-02-24 12:33:09 +0100232}
233
234unsigned CConn::getPixelCount()
235{
236 return pixelCount;
237}
238
239unsigned CConn::getPosition()
240{
241 return sock->inStream().pos();
242}
243
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000244// The RFB core is not properly asynchronous, so it calls this callback
245// whenever it needs to block to wait for more data. Since FLTK is
246// monitoring the socket, we just make sure FLTK gets to run.
247
248void CConn::blockCallback()
249{
Pierre Ossman5102fa92015-11-18 16:23:21 +0100250 run_mainloop();
Pierre Ossman1db73242015-09-23 12:20:32 +0200251
252 if (should_exit())
253 throw rdr::Exception("Termination requested");
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000254}
255
DRC3e7ed812013-02-26 10:34:22 +0000256void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000257{
258 CConn *cc;
259 static bool recursing = false;
260
261 assert(data);
262 cc = (CConn*)data;
263
264 // I don't think processMsg() is recursion safe, so add this check
265 if (recursing)
266 return;
267
268 recursing = true;
269
270 try {
271 // processMsg() only processes one message, so we need to loop
272 // until the buffers are empty or things will stall.
273 do {
274 cc->processMsg();
Pierre Ossman9a73adf2016-12-29 16:52:56 +0100275
276 // Make sure that the FLTK handling and the timers gets some CPU
277 // time in case of back to back messages
278 Fl::check();
279 Timer::checkTimeouts();
280
281 // Also check if we need to stop reading and terminate
282 if (should_exit())
283 break;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000284 } while (cc->sock->inStream().checkNoWait(1));
285 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000286 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000287 exit_vncviewer();
288 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000289 vlog.error("%s", e.str());
Pierre Ossman1db73242015-09-23 12:20:32 +0200290 // Somebody might already have requested us to terminate, and
291 // might have already provided an error message.
292 if (!should_exit())
293 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000294 }
295
296 recursing = false;
297}
298
299////////////////////// CConnection callback methods //////////////////////
300
301// serverInit() is called when the serverInit message has been received. At
302// this point we create the desktop window and display it. We also tell the
303// server the pixel format and encodings to use and request the first update.
304void CConn::serverInit()
305{
306 CConnection::serverInit();
307
308 // If using AutoSelect with old servers, start in FullColor
309 // mode. See comment in autoSelectFormatAndEncoding.
310 if (cp.beforeVersion(3, 8) && autoSelect)
311 fullColour.setParam(true);
312
313 serverPF = cp.pf();
314
315 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
316 fullColourPF = desktop->getPreferredPF();
317
Pierre Ossman5d512c32011-11-04 11:42:16 +0000318 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000319 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000320
321 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000322 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000323
324 // This initial update request is a bit of a corner case, so we need
325 // to help out setting the correct format here.
326 assert(pendingPFChange);
Pierre Ossman5d512c32011-11-04 11:42:16 +0000327 cp.setPF(pendingPF);
328 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000329}
330
331// setDesktopSize() is called when the desktop size changes (including when
332// it is set initially).
333void CConn::setDesktopSize(int w, int h)
334{
335 CConnection::setDesktopSize(w,h);
336 resizeFramebuffer();
337}
338
339// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
Pierre Ossman28c1d542015-03-03 16:27:44 +0100340void CConn::setExtendedDesktopSize(unsigned reason, unsigned result,
341 int w, int h, const rfb::ScreenSet& layout)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000342{
343 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
344
345 if ((reason == reasonClient) && (result != resultSuccess)) {
346 vlog.error(_("SetDesktopSize failed: %d"), result);
347 return;
348 }
349
350 resizeFramebuffer();
351}
352
353// setName() is called when the desktop name changes
354void CConn::setName(const char* name)
355{
356 CConnection::setName(name);
357 if (desktop)
358 desktop->setName(name);
359}
360
361// framebufferUpdateStart() is called at the beginning of an update.
362// Here we try to send out a new framebuffer update request so that the
363// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000364// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000365void CConn::framebufferUpdateStart()
366{
Pierre Ossman3da238d2015-11-12 12:20:05 +0100367 CConnection::framebufferUpdateStart();
368
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000369 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000370 pendingUpdate = false;
371
372 requestNewUpdate();
Pierre Ossmand9b90032015-09-23 12:18:52 +0200373
374 // Update the screen prematurely for very slow updates
375 Fl::add_timeout(1.0, handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000376}
377
378// framebufferUpdateEnd() is called at the end of an update.
379// For each rectangle, the FdInStream will have timed the speed
380// of the connection, allowing us to select format and encoding
381// appropriately, and then request another incremental update.
382void CConn::framebufferUpdateEnd()
383{
Pierre Ossman3da238d2015-11-12 12:20:05 +0100384 CConnection::framebufferUpdateEnd();
385
Pierre Ossmanfecf0a42018-03-26 12:22:47 +0200386 updateCount++;
Pierre Ossman921f6c82017-02-24 12:33:09 +0100387
Pierre Ossmand9b90032015-09-23 12:18:52 +0200388 Fl::remove_timeout(handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000389 desktop->updateWindow();
390
391 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000392 // We need fences to make extra update requests and continuous
393 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000394 if (cp.supportsFence)
395 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
396
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000397 firstUpdate = false;
398 }
399
Pierre Ossman5d512c32011-11-04 11:42:16 +0000400 // A format change has been scheduled and we are now past the update
401 // with the old format. Time to active the new one.
402 if (pendingPFChange) {
Pierre Ossman5d512c32011-11-04 11:42:16 +0000403 cp.setPF(pendingPF);
404 pendingPFChange = false;
405 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000406
407 // Compute new settings based on updated bandwidth values
408 if (autoSelect)
409 autoSelectFormatAndEncoding();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000410}
411
412// The rest of the callbacks are fairly self-explanatory...
413
414void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
415{
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200416 vlog.error(_("Invalid SetColourMapEntries from server!"));
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000417}
418
419void CConn::bell()
420{
421 fl_beep();
422}
423
424void CConn::serverCutText(const char* str, rdr::U32 len)
425{
Pierre Ossman4c204232018-03-26 13:32:49 +0200426 desktop->serverCutText(str, len);
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
Pierre Ossman9f273e92015-11-09 16:34:54 +0100436 CConnection::dataRect(r, encoding);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100437
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000438 sock->inStream().stopTiming();
Pierre Ossman921f6c82017-02-24 12:33:09 +0100439
440 pixelCount += r.area();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000441}
442
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000443void CConn::setCursor(int width, int height, const Point& hotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100444 const rdr::U8* data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000445{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100446 desktop->setCursor(width, height, hotspot, data);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000447}
448
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000449void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
450{
451 CMsgHandler::fence(flags, len, data);
452
453 if (flags & fenceFlagRequest) {
454 // We handle everything synchronously so we trivially honor these modes
455 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
456
457 writer()->writeFence(flags, len, data);
458 return;
459 }
460
461 if (len == 0) {
462 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000463 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000464 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000465
466 if (cp.supportsContinuousUpdates) {
467 vlog.info(_("Enabling continuous updates"));
468 continuousUpdates = true;
469 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
470 }
471 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000472 } else {
473 // Pixel format change
474 rdr::MemInStream memStream(data, len);
475 PixelFormat pf;
476
477 pf.read(&memStream);
478
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000479 cp.setPF(pf);
480 }
481}
482
Pierre Ossman2fa63f82016-12-05 15:26:21 +0100483void CConn::setLEDState(unsigned int state)
484{
485 CConnection::setLEDState(state);
486
487 desktop->setLEDState(state);
488}
489
DRC33c15e32011-11-03 18:49:21 +0000490
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000491////////////////////// Internal methods //////////////////////
492
493void CConn::resizeFramebuffer()
494{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000495 if (!desktop)
496 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000497
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000498 if (continuousUpdates)
499 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
500
Pierre Ossman6455d852011-06-01 09:33:00 +0000501 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000502}
503
504// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
505// to the connection speed:
506//
507// First we wait for at least one second of bandwidth measurement.
508//
509// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
510// which should be perceptually lossless.
511//
512// If the bandwidth is below that, we choose a more lossy JPEG quality.
513//
514// If the bandwidth drops below 256 Kbps, we switch to palette mode.
515//
516// Note: The system here is fairly arbitrary and should be replaced
517// with something more intelligent at the server end.
518//
519void CConn::autoSelectFormatAndEncoding()
520{
521 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
522 unsigned int timeWaited = sock->inStream().timeWaited();
523 bool newFullColour = fullColour;
524 int newQualityLevel = qualityLevel;
525
526 // Always use Tight
527 if (currentEncoding != encodingTight) {
528 currentEncoding = encodingTight;
529 encodingChange = true;
530 }
531
532 // Check that we have a decent bandwidth measurement
533 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
534 return;
535
536 // Select appropriate quality level
537 if (!noJpeg) {
538 if (kbitsPerSecond > 16000)
539 newQualityLevel = 8;
540 else
541 newQualityLevel = 6;
542
543 if (newQualityLevel != qualityLevel) {
544 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
545 kbitsPerSecond, newQualityLevel);
546 cp.qualityLevel = newQualityLevel;
547 qualityLevel.setParam(newQualityLevel);
548 encodingChange = true;
549 }
550 }
551
552 if (cp.beforeVersion(3, 8)) {
553 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
554 // cursors "asynchronously". If this happens in the middle of a
555 // pixel format change, the server will encode the cursor with
556 // the old format, but the client will try to decode it
557 // according to the new format. This will lead to a
558 // crash. Therefore, we do not allow automatic format change for
559 // old servers.
560 return;
561 }
562
563 // Select best color level
564 newFullColour = (kbitsPerSecond > 256);
565 if (newFullColour != fullColour) {
566 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
567 kbitsPerSecond,
568 newFullColour ? _("enabled") : _("disabled"));
569 fullColour.setParam(newFullColour);
570 formatChange = true;
571 }
572}
573
574// checkEncodings() sends a setEncodings message if one is needed.
575void CConn::checkEncodings()
576{
577 if (encodingChange && writer()) {
578 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
579 writer()->writeSetEncodings(currentEncoding, true);
580 encodingChange = false;
581 }
582}
583
584// requestNewUpdate() requests an update from the server, having set the
585// format and encoding appropriately.
586void CConn::requestNewUpdate()
587{
588 if (formatChange) {
589 PixelFormat pf;
590
591 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000592 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000593
594 if (fullColour) {
595 pf = fullColourPF;
596 } else {
597 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000598 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000599 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000600 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000601 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000602 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000603 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000604
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000605 if (supportsSyncFence) {
606 // We let the fence carry the pixel format and switch once we
607 // get the response back. That way we will be synchronised with
608 // when the server switches.
609 rdr::MemOutStream memStream;
610
611 pf.write(&memStream);
612
613 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
614 memStream.length(), (const char*)memStream.data());
615 } else {
616 // New requests are sent out at the start of processing the last
617 // one, so we cannot switch our internal format right now (doing so
618 // would mean misdecoding the current update).
619 pendingPFChange = true;
620 pendingPF = pf;
621 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000622
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000623 char str[256];
624 pf.print(str, 256);
625 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000626 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000627
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000628 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000629 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000630
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000631 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000632
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000633 if (forceNonincremental || !continuousUpdates) {
634 pendingUpdate = true;
635 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
636 !forceNonincremental);
637 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000638
639 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000640}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000641
642void CConn::handleOptions(void *data)
643{
644 CConn *self = (CConn*)data;
645
646 // Checking all the details of the current set of encodings is just
647 // a pain. Assume something has changed, as resending the encoding
648 // list is cheap. Avoid overriding what the auto logic has selected
649 // though.
650 if (!autoSelect) {
651 int encNum = encodingNum(preferredEncoding);
652
653 if (encNum != -1)
654 self->currentEncoding = encNum;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000655 }
656
Pierre Ossmanda389562011-06-09 08:24:37 +0000657 self->cp.supportsLocalCursor = true;
658
Pierre Ossmana22459d2014-03-17 14:42:10 +0100659 if (customCompressLevel)
660 self->cp.compressLevel = compressLevel;
661 else
662 self->cp.compressLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000663
Pierre Ossmana22459d2014-03-17 14:42:10 +0100664 if (!noJpeg && !autoSelect)
665 self->cp.qualityLevel = qualityLevel;
666 else
667 self->cp.qualityLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000668
669 self->encodingChange = true;
670
671 // Format changes refreshes the entire screen though and are therefore
672 // very costly. It's probably worth the effort to see if it is necessary
673 // here.
674 PixelFormat pf;
675
676 if (fullColour) {
677 pf = self->fullColourPF;
678 } else {
679 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000680 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000681 else if (lowColourLevel == 1)
682 pf = lowColourPF;
683 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000684 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000685 }
686
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000687 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000688 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000689
690 // Without fences, we cannot safely trigger an update request directly
691 // but must wait for the next update to arrive.
692 if (self->supportsSyncFence)
693 self->requestNewUpdate();
694 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000695}
Pierre Ossmand9b90032015-09-23 12:18:52 +0200696
697void CConn::handleUpdateTimeout(void *data)
698{
699 CConn *self = (CConn *)data;
700
701 assert(self);
702
703 self->desktop->updateWindow();
704
705 Fl::repeat_timeout(1.0, handleUpdateTimeout, data);
706}