blob: 07e7841cb9cae4aae57ff308c94b2224f83e6df2 [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 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 Ossman921f6c82017-02-24 12:33:09 +0100229unsigned CConn::getFrameCount()
230{
231 return frameCount;
232}
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 Ossman921f6c82017-02-24 12:33:09 +0100386 frameCount++;
387
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 Ossman689c4582011-05-26 15:39:41 +0000426 char *buffer;
427 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000428
Pierre Ossman50104882013-05-24 10:47:27 +0000429 if (!acceptClipboard)
430 return;
431
Pierre Ossman689c4582011-05-26 15:39:41 +0000432 size = fl_utf8froma(NULL, 0, str, len);
433 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000434 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000435
436 size++;
437
438 buffer = new char[size];
439
440 ret = fl_utf8froma(buffer, size, str, len);
441 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000442
Pierre Ossmanfb450fb2015-03-03 16:34:56 +0100443 vlog.debug("Got clipboard data (%d bytes)", (int)strlen(buffer));
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000444
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000445 // RFB doesn't have separate selection and clipboard concepts, so we
446 // dump the data into both variants.
Pierre Ossmanf862c2e2016-03-29 14:15:38 +0200447 if (setPrimary)
448 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000449 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000450
451 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000452}
453
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100454void CConn::dataRect(const Rect& r, int encoding)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000455{
456 sock->inStream().startTiming();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000457
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100458 if (encoding != encodingCopyRect)
459 lastServerEncoding = encoding;
460
Pierre Ossman9f273e92015-11-09 16:34:54 +0100461 CConnection::dataRect(r, encoding);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100462
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000463 sock->inStream().stopTiming();
Pierre Ossman921f6c82017-02-24 12:33:09 +0100464
465 pixelCount += r.area();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000466}
467
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000468void CConn::setCursor(int width, int height, const Point& hotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100469 const rdr::U8* data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000470{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100471 desktop->setCursor(width, height, hotspot, data);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000472}
473
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000474void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
475{
476 CMsgHandler::fence(flags, len, data);
477
478 if (flags & fenceFlagRequest) {
479 // We handle everything synchronously so we trivially honor these modes
480 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
481
482 writer()->writeFence(flags, len, data);
483 return;
484 }
485
486 if (len == 0) {
487 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000488 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000489 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000490
491 if (cp.supportsContinuousUpdates) {
492 vlog.info(_("Enabling continuous updates"));
493 continuousUpdates = true;
494 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
495 }
496 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000497 } else {
498 // Pixel format change
499 rdr::MemInStream memStream(data, len);
500 PixelFormat pf;
501
502 pf.read(&memStream);
503
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000504 cp.setPF(pf);
505 }
506}
507
Pierre Ossman2fa63f82016-12-05 15:26:21 +0100508void CConn::setLEDState(unsigned int state)
509{
510 CConnection::setLEDState(state);
511
512 desktop->setLEDState(state);
513}
514
DRC33c15e32011-11-03 18:49:21 +0000515
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000516////////////////////// Internal methods //////////////////////
517
518void CConn::resizeFramebuffer()
519{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000520 if (!desktop)
521 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000522
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000523 if (continuousUpdates)
524 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
525
Pierre Ossman6455d852011-06-01 09:33:00 +0000526 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000527}
528
529// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
530// to the connection speed:
531//
532// First we wait for at least one second of bandwidth measurement.
533//
534// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
535// which should be perceptually lossless.
536//
537// If the bandwidth is below that, we choose a more lossy JPEG quality.
538//
539// If the bandwidth drops below 256 Kbps, we switch to palette mode.
540//
541// Note: The system here is fairly arbitrary and should be replaced
542// with something more intelligent at the server end.
543//
544void CConn::autoSelectFormatAndEncoding()
545{
546 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
547 unsigned int timeWaited = sock->inStream().timeWaited();
548 bool newFullColour = fullColour;
549 int newQualityLevel = qualityLevel;
550
551 // Always use Tight
552 if (currentEncoding != encodingTight) {
553 currentEncoding = encodingTight;
554 encodingChange = true;
555 }
556
557 // Check that we have a decent bandwidth measurement
558 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
559 return;
560
561 // Select appropriate quality level
562 if (!noJpeg) {
563 if (kbitsPerSecond > 16000)
564 newQualityLevel = 8;
565 else
566 newQualityLevel = 6;
567
568 if (newQualityLevel != qualityLevel) {
569 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
570 kbitsPerSecond, newQualityLevel);
571 cp.qualityLevel = newQualityLevel;
572 qualityLevel.setParam(newQualityLevel);
573 encodingChange = true;
574 }
575 }
576
577 if (cp.beforeVersion(3, 8)) {
578 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
579 // cursors "asynchronously". If this happens in the middle of a
580 // pixel format change, the server will encode the cursor with
581 // the old format, but the client will try to decode it
582 // according to the new format. This will lead to a
583 // crash. Therefore, we do not allow automatic format change for
584 // old servers.
585 return;
586 }
587
588 // Select best color level
589 newFullColour = (kbitsPerSecond > 256);
590 if (newFullColour != fullColour) {
591 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
592 kbitsPerSecond,
593 newFullColour ? _("enabled") : _("disabled"));
594 fullColour.setParam(newFullColour);
595 formatChange = true;
596 }
597}
598
599// checkEncodings() sends a setEncodings message if one is needed.
600void CConn::checkEncodings()
601{
602 if (encodingChange && writer()) {
603 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
604 writer()->writeSetEncodings(currentEncoding, true);
605 encodingChange = false;
606 }
607}
608
609// requestNewUpdate() requests an update from the server, having set the
610// format and encoding appropriately.
611void CConn::requestNewUpdate()
612{
613 if (formatChange) {
614 PixelFormat pf;
615
616 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000617 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000618
619 if (fullColour) {
620 pf = fullColourPF;
621 } else {
622 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000623 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000624 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000625 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000626 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000627 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000628 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000629
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000630 if (supportsSyncFence) {
631 // We let the fence carry the pixel format and switch once we
632 // get the response back. That way we will be synchronised with
633 // when the server switches.
634 rdr::MemOutStream memStream;
635
636 pf.write(&memStream);
637
638 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
639 memStream.length(), (const char*)memStream.data());
640 } else {
641 // New requests are sent out at the start of processing the last
642 // one, so we cannot switch our internal format right now (doing so
643 // would mean misdecoding the current update).
644 pendingPFChange = true;
645 pendingPF = pf;
646 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000647
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000648 char str[256];
649 pf.print(str, 256);
650 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000651 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000652
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000653 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000654 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000655
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000656 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000657
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000658 if (forceNonincremental || !continuousUpdates) {
659 pendingUpdate = true;
660 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
661 !forceNonincremental);
662 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000663
664 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000665}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000666
667void CConn::handleOptions(void *data)
668{
669 CConn *self = (CConn*)data;
670
671 // Checking all the details of the current set of encodings is just
672 // a pain. Assume something has changed, as resending the encoding
673 // list is cheap. Avoid overriding what the auto logic has selected
674 // though.
675 if (!autoSelect) {
676 int encNum = encodingNum(preferredEncoding);
677
678 if (encNum != -1)
679 self->currentEncoding = encNum;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000680 }
681
Pierre Ossmanda389562011-06-09 08:24:37 +0000682 self->cp.supportsLocalCursor = true;
683
Pierre Ossmana22459d2014-03-17 14:42:10 +0100684 if (customCompressLevel)
685 self->cp.compressLevel = compressLevel;
686 else
687 self->cp.compressLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000688
Pierre Ossmana22459d2014-03-17 14:42:10 +0100689 if (!noJpeg && !autoSelect)
690 self->cp.qualityLevel = qualityLevel;
691 else
692 self->cp.qualityLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000693
694 self->encodingChange = true;
695
696 // Format changes refreshes the entire screen though and are therefore
697 // very costly. It's probably worth the effort to see if it is necessary
698 // here.
699 PixelFormat pf;
700
701 if (fullColour) {
702 pf = self->fullColourPF;
703 } else {
704 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000705 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000706 else if (lowColourLevel == 1)
707 pf = lowColourPF;
708 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000709 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000710 }
711
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000712 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000713 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000714
715 // Without fences, we cannot safely trigger an update request directly
716 // but must wait for the next update to arrive.
717 if (self->supportsSyncFence)
718 self->requestNewUpdate();
719 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000720}
Pierre Ossmand9b90032015-09-23 12:18:52 +0200721
722void CConn::handleUpdateTimeout(void *data)
723{
724 CConn *self = (CConn *)data;
725
726 assert(self);
727
728 self->desktop->updateWindow();
729
730 Fl::repeat_timeout(1.0, handleUpdateTimeout, data);
731}