blob: 78eadb545e8f60232e5de9050def0f37b493b3ae [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 Ossman5156d5e2011-03-09 09:42:34 +000049#include "i18n.h"
50#include "parameters.h"
Pierre Ossman39ceb502011-07-12 15:54:25 +000051#include "vncviewer.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000052
DRCb65bb932011-06-24 03:17:00 +000053#ifdef WIN32
54#include "win32.h"
55#endif
56
Pierre Ossman5156d5e2011-03-09 09:42:34 +000057using namespace rdr;
58using namespace rfb;
59using namespace std;
60
Pierre Ossman5156d5e2011-03-09 09:42:34 +000061static rfb::LogWriter vlog("CConn");
62
Pierre Ossmancf836f22011-07-14 14:34:09 +000063// 8 colours (1 bit per component)
64static const PixelFormat verylowColourPF(8, 3,false, true,
65 1, 1, 1, 2, 1, 0);
66// 64 colours (2 bits per component)
67static const PixelFormat lowColourPF(8, 6, false, true,
68 3, 3, 3, 4, 2, 0);
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010069// 256 colours (2-3 bits per component)
70static const PixelFormat mediumColourPF(8, 8, false, true,
71 7, 7, 3, 5, 2, 0);
Pierre Ossmanf4f30942011-05-17 09:39:07 +000072
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000073CConn::CConn(const char* vncServerName, network::Socket* socket=NULL)
74 : serverHost(0), serverPort(0), desktop(NULL),
Pierre Ossman5d512c32011-11-04 11:42:16 +000075 pendingPFChange(false),
Pierre Ossman5156d5e2011-03-09 09:42:34 +000076 currentEncoding(encodingTight), lastServerEncoding((unsigned int)-1),
77 formatChange(false), encodingChange(false),
Pierre Ossmanaa73c892011-11-15 12:13:37 +000078 firstUpdate(true), pendingUpdate(false), continuousUpdates(false),
Pierre Ossmane28bdb22011-11-14 16:02:06 +000079 forceNonincremental(true), supportsSyncFence(false)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000080{
81 setShared(::shared);
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000082 sock = socket;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000083
84 int encNum = encodingNum(preferredEncoding);
85 if (encNum != -1)
86 currentEncoding = encNum;
87
Pierre Ossmanda389562011-06-09 08:24:37 +000088 cp.supportsLocalCursor = true;
89
Pierre Ossman5156d5e2011-03-09 09:42:34 +000090 cp.supportsDesktopResize = true;
91 cp.supportsExtendedDesktopSize = true;
92 cp.supportsDesktopRename = true;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000093
Pierre Ossmana22459d2014-03-17 14:42:10 +010094 if (customCompressLevel)
95 cp.compressLevel = compressLevel;
96 else
97 cp.compressLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000098
Pierre Ossmana22459d2014-03-17 14:42:10 +010099 if (!noJpeg)
100 cp.qualityLevel = qualityLevel;
101 else
102 cp.qualityLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000103
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000104 if(sock == NULL) {
105 try {
106 getHostAndPort(vncServerName, &serverHost, &serverPort);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000107
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000108 sock = new network::TcpSocket(serverHost, serverPort);
109 vlog.info(_("connected to host %s port %d"), serverHost, serverPort);
110 } catch (rdr::Exception& e) {
111 vlog.error("%s", e.str());
112 fl_alert("%s", e.str());
113 exit_vncviewer();
114 return;
115 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000116 }
117
118 Fl::add_fd(sock->getFd(), FL_READ | FL_EXCEPT, socketEvent, this);
119
120 // See callback below
121 sock->inStream().setBlockCallback(this);
122
123 setServerName(serverHost);
124 setStreams(&sock->inStream(), &sock->outStream());
125
126 initialiseProtocol();
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000127
128 OptionsDialog::addCallback(handleOptions, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000129}
130
131CConn::~CConn()
132{
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000133 OptionsDialog::removeCallback(handleOptions);
Pierre Ossmand9b90032015-09-23 12:18:52 +0200134 Fl::remove_timeout(handleUpdateTimeout, this);
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000135
Pierre Ossman6a9e2e62011-05-19 14:47:43 +0000136 if (desktop)
137 delete desktop;
138
Pierre Ossman5e04c262011-11-09 11:31:12 +0000139 delete [] serverHost;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000140 if (sock)
141 Fl::remove_fd(sock->getFd());
142 delete sock;
143}
144
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000145void CConn::refreshFramebuffer()
146{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000147 forceNonincremental = true;
148
149 // Without fences, we cannot safely trigger an update request directly
150 // but must wait for the next update to arrive.
151 if (supportsSyncFence)
152 requestNewUpdate();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000153}
154
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000155const char *CConn::connectionInfo()
156{
157 static char infoText[1024] = "";
158
Pierre Ossmanb2046432014-09-22 11:17:34 +0200159 char scratch[100];
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000160 char pfStr[100];
Pierre Ossmanb2046432014-09-22 11:17:34 +0200161
162 // Crude way of avoiding constant overflow checks
163 assert((sizeof(scratch) + 1) * 10 < sizeof(infoText));
164
165 infoText[0] = '\0';
166
167 snprintf(scratch, sizeof(scratch),
168 _("Desktop name: %.80s"), cp.name());
169 strcat(infoText, scratch);
170 strcat(infoText, "\n");
171
172 snprintf(scratch, sizeof(scratch),
173 _("Host: %.80s port: %d"), serverHost, serverPort);
174 strcat(infoText, scratch);
175 strcat(infoText, "\n");
176
177 snprintf(scratch, sizeof(scratch),
178 _("Size: %d x %d"), cp.width, cp.height);
179 strcat(infoText, scratch);
180 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000181
Pierre Ossman744e55c2014-12-03 14:00:54 +0100182 // TRANSLATORS: Will be filled in with a string describing the
183 // protocol pixel format in a fairly language neutral way
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000184 cp.pf().print(pfStr, 100);
Pierre Ossmanb2046432014-09-22 11:17:34 +0200185 snprintf(scratch, sizeof(scratch),
186 _("Pixel format: %s"), pfStr);
187 strcat(infoText, scratch);
188 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000189
Pierre Ossman744e55c2014-12-03 14:00:54 +0100190 // TRANSLATORS: Similar to the earlier "Pixel format" string
Pierre Ossmanb2046432014-09-22 11:17:34 +0200191 serverPF.print(pfStr, 100);
192 snprintf(scratch, sizeof(scratch),
193 _("(server default %s)"), pfStr);
194 strcat(infoText, scratch);
195 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000196
Pierre Ossmanb2046432014-09-22 11:17:34 +0200197 snprintf(scratch, sizeof(scratch),
198 _("Requested encoding: %s"), encodingName(currentEncoding));
199 strcat(infoText, scratch);
200 strcat(infoText, "\n");
201
202 snprintf(scratch, sizeof(scratch),
203 _("Last used encoding: %s"), encodingName(lastServerEncoding));
204 strcat(infoText, scratch);
205 strcat(infoText, "\n");
206
207 snprintf(scratch, sizeof(scratch),
208 _("Line speed estimate: %d kbit/s"), sock->inStream().kbitsPerSecond());
209 strcat(infoText, scratch);
210 strcat(infoText, "\n");
211
212 snprintf(scratch, sizeof(scratch),
213 _("Protocol version: %d.%d"), cp.majorVersion, cp.minorVersion);
214 strcat(infoText, scratch);
215 strcat(infoText, "\n");
216
217 snprintf(scratch, sizeof(scratch),
218 _("Security method: %s"), secTypeName(csecurity->getType()));
219 strcat(infoText, scratch);
220 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000221
222 return infoText;
223}
224
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000225// The RFB core is not properly asynchronous, so it calls this callback
226// whenever it needs to block to wait for more data. Since FLTK is
227// monitoring the socket, we just make sure FLTK gets to run.
228
229void CConn::blockCallback()
230{
231 int next_timer;
232
233 next_timer = Timer::checkTimeouts();
234 if (next_timer == 0)
235 next_timer = INT_MAX;
236
237 Fl::wait((double)next_timer / 1000.0);
Pierre Ossman1db73242015-09-23 12:20:32 +0200238
239 if (should_exit())
240 throw rdr::Exception("Termination requested");
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000241}
242
DRC3e7ed812013-02-26 10:34:22 +0000243void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000244{
245 CConn *cc;
246 static bool recursing = false;
247
248 assert(data);
249 cc = (CConn*)data;
250
251 // I don't think processMsg() is recursion safe, so add this check
252 if (recursing)
253 return;
254
255 recursing = true;
256
257 try {
258 // processMsg() only processes one message, so we need to loop
259 // until the buffers are empty or things will stall.
260 do {
261 cc->processMsg();
262 } while (cc->sock->inStream().checkNoWait(1));
263 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000264 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000265 exit_vncviewer();
266 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000267 vlog.error("%s", e.str());
Pierre Ossman1db73242015-09-23 12:20:32 +0200268 // Somebody might already have requested us to terminate, and
269 // might have already provided an error message.
270 if (!should_exit())
271 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000272 }
273
274 recursing = false;
275}
276
277////////////////////// CConnection callback methods //////////////////////
278
279// serverInit() is called when the serverInit message has been received. At
280// this point we create the desktop window and display it. We also tell the
281// server the pixel format and encodings to use and request the first update.
282void CConn::serverInit()
283{
284 CConnection::serverInit();
285
286 // If using AutoSelect with old servers, start in FullColor
287 // mode. See comment in autoSelectFormatAndEncoding.
288 if (cp.beforeVersion(3, 8) && autoSelect)
289 fullColour.setParam(true);
290
291 serverPF = cp.pf();
292
293 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
294 fullColourPF = desktop->getPreferredPF();
295
Pierre Ossman5d512c32011-11-04 11:42:16 +0000296 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000297 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000298
299 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000300 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000301
302 // This initial update request is a bit of a corner case, so we need
303 // to help out setting the correct format here.
304 assert(pendingPFChange);
Pierre Ossman5d512c32011-11-04 11:42:16 +0000305 cp.setPF(pendingPF);
306 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000307}
308
309// setDesktopSize() is called when the desktop size changes (including when
310// it is set initially).
311void CConn::setDesktopSize(int w, int h)
312{
313 CConnection::setDesktopSize(w,h);
314 resizeFramebuffer();
315}
316
317// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
Pierre Ossman28c1d542015-03-03 16:27:44 +0100318void CConn::setExtendedDesktopSize(unsigned reason, unsigned result,
319 int w, int h, const rfb::ScreenSet& layout)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000320{
321 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
322
323 if ((reason == reasonClient) && (result != resultSuccess)) {
324 vlog.error(_("SetDesktopSize failed: %d"), result);
325 return;
326 }
327
328 resizeFramebuffer();
329}
330
331// setName() is called when the desktop name changes
332void CConn::setName(const char* name)
333{
334 CConnection::setName(name);
335 if (desktop)
336 desktop->setName(name);
337}
338
339// framebufferUpdateStart() is called at the beginning of an update.
340// Here we try to send out a new framebuffer update request so that the
341// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000342// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000343void CConn::framebufferUpdateStart()
344{
Pierre Ossman3da238d2015-11-12 12:20:05 +0100345 CConnection::framebufferUpdateStart();
346
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000347 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000348 pendingUpdate = false;
349
350 requestNewUpdate();
Pierre Ossmand9b90032015-09-23 12:18:52 +0200351
352 // Update the screen prematurely for very slow updates
353 Fl::add_timeout(1.0, handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000354}
355
356// framebufferUpdateEnd() is called at the end of an update.
357// For each rectangle, the FdInStream will have timed the speed
358// of the connection, allowing us to select format and encoding
359// appropriately, and then request another incremental update.
360void CConn::framebufferUpdateEnd()
361{
Pierre Ossman3da238d2015-11-12 12:20:05 +0100362 CConnection::framebufferUpdateEnd();
363
Pierre Ossmand9b90032015-09-23 12:18:52 +0200364 Fl::remove_timeout(handleUpdateTimeout, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000365 desktop->updateWindow();
366
367 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000368 // We need fences to make extra update requests and continuous
369 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000370 if (cp.supportsFence)
371 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
372
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000373 firstUpdate = false;
374 }
375
Pierre Ossman5d512c32011-11-04 11:42:16 +0000376 // A format change has been scheduled and we are now past the update
377 // with the old format. Time to active the new one.
378 if (pendingPFChange) {
Pierre Ossman5d512c32011-11-04 11:42:16 +0000379 cp.setPF(pendingPF);
380 pendingPFChange = false;
381 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000382
383 // Compute new settings based on updated bandwidth values
384 if (autoSelect)
385 autoSelectFormatAndEncoding();
386
387 // Make sure that the FLTK handling and the timers gets some CPU time
388 // in case of back to back framebuffer updates.
389 Fl::check();
390 Timer::checkTimeouts();
391}
392
393// The rest of the callbacks are fairly self-explanatory...
394
395void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
396{
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200397 vlog.error(_("Invalid SetColourMapEntries from server!"));
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000398}
399
400void CConn::bell()
401{
402 fl_beep();
403}
404
405void CConn::serverCutText(const char* str, rdr::U32 len)
406{
Pierre Ossman689c4582011-05-26 15:39:41 +0000407 char *buffer;
408 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000409
Pierre Ossman50104882013-05-24 10:47:27 +0000410 if (!acceptClipboard)
411 return;
412
Pierre Ossman689c4582011-05-26 15:39:41 +0000413 size = fl_utf8froma(NULL, 0, str, len);
414 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000415 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000416
417 size++;
418
419 buffer = new char[size];
420
421 ret = fl_utf8froma(buffer, size, str, len);
422 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000423
Pierre Ossmanfb450fb2015-03-03 16:34:56 +0100424 vlog.debug("Got clipboard data (%d bytes)", (int)strlen(buffer));
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000425
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000426 // RFB doesn't have separate selection and clipboard concepts, so we
427 // dump the data into both variants.
428 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000429 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000430
431 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000432}
433
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100434void CConn::dataRect(const Rect& r, int encoding)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000435{
436 sock->inStream().startTiming();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000437
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100438 if (encoding != encodingCopyRect)
439 lastServerEncoding = encoding;
440
Pierre Ossman9f273e92015-11-09 16:34:54 +0100441 CConnection::dataRect(r, encoding);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100442
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000443 sock->inStream().stopTiming();
444}
445
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000446void CConn::setCursor(int width, int height, const Point& hotspot,
447 void* data, void* mask)
448{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000449 desktop->setCursor(width, height, hotspot, data, mask);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000450}
451
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000452void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
453{
454 CMsgHandler::fence(flags, len, data);
455
456 if (flags & fenceFlagRequest) {
457 // We handle everything synchronously so we trivially honor these modes
458 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
459
460 writer()->writeFence(flags, len, data);
461 return;
462 }
463
464 if (len == 0) {
465 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000466 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000467 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000468
469 if (cp.supportsContinuousUpdates) {
470 vlog.info(_("Enabling continuous updates"));
471 continuousUpdates = true;
472 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
473 }
474 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000475 } else {
476 // Pixel format change
477 rdr::MemInStream memStream(data, len);
478 PixelFormat pf;
479
480 pf.read(&memStream);
481
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000482 cp.setPF(pf);
483 }
484}
485
DRC33c15e32011-11-03 18:49:21 +0000486
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000487////////////////////// Internal methods //////////////////////
488
489void CConn::resizeFramebuffer()
490{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000491 if (!desktop)
492 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000493
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000494 if (continuousUpdates)
495 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
496
Pierre Ossman6455d852011-06-01 09:33:00 +0000497 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000498}
499
500// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
501// to the connection speed:
502//
503// First we wait for at least one second of bandwidth measurement.
504//
505// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
506// which should be perceptually lossless.
507//
508// If the bandwidth is below that, we choose a more lossy JPEG quality.
509//
510// If the bandwidth drops below 256 Kbps, we switch to palette mode.
511//
512// Note: The system here is fairly arbitrary and should be replaced
513// with something more intelligent at the server end.
514//
515void CConn::autoSelectFormatAndEncoding()
516{
517 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
518 unsigned int timeWaited = sock->inStream().timeWaited();
519 bool newFullColour = fullColour;
520 int newQualityLevel = qualityLevel;
521
522 // Always use Tight
523 if (currentEncoding != encodingTight) {
524 currentEncoding = encodingTight;
525 encodingChange = true;
526 }
527
528 // Check that we have a decent bandwidth measurement
529 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
530 return;
531
532 // Select appropriate quality level
533 if (!noJpeg) {
534 if (kbitsPerSecond > 16000)
535 newQualityLevel = 8;
536 else
537 newQualityLevel = 6;
538
539 if (newQualityLevel != qualityLevel) {
540 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
541 kbitsPerSecond, newQualityLevel);
542 cp.qualityLevel = newQualityLevel;
543 qualityLevel.setParam(newQualityLevel);
544 encodingChange = true;
545 }
546 }
547
548 if (cp.beforeVersion(3, 8)) {
549 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
550 // cursors "asynchronously". If this happens in the middle of a
551 // pixel format change, the server will encode the cursor with
552 // the old format, but the client will try to decode it
553 // according to the new format. This will lead to a
554 // crash. Therefore, we do not allow automatic format change for
555 // old servers.
556 return;
557 }
558
559 // Select best color level
560 newFullColour = (kbitsPerSecond > 256);
561 if (newFullColour != fullColour) {
562 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
563 kbitsPerSecond,
564 newFullColour ? _("enabled") : _("disabled"));
565 fullColour.setParam(newFullColour);
566 formatChange = true;
567 }
568}
569
570// checkEncodings() sends a setEncodings message if one is needed.
571void CConn::checkEncodings()
572{
573 if (encodingChange && writer()) {
574 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
575 writer()->writeSetEncodings(currentEncoding, true);
576 encodingChange = false;
577 }
578}
579
580// requestNewUpdate() requests an update from the server, having set the
581// format and encoding appropriately.
582void CConn::requestNewUpdate()
583{
584 if (formatChange) {
585 PixelFormat pf;
586
587 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000588 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000589
590 if (fullColour) {
591 pf = fullColourPF;
592 } else {
593 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000594 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000595 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000596 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000597 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000598 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000599 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000600
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000601 if (supportsSyncFence) {
602 // We let the fence carry the pixel format and switch once we
603 // get the response back. That way we will be synchronised with
604 // when the server switches.
605 rdr::MemOutStream memStream;
606
607 pf.write(&memStream);
608
609 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
610 memStream.length(), (const char*)memStream.data());
611 } else {
612 // New requests are sent out at the start of processing the last
613 // one, so we cannot switch our internal format right now (doing so
614 // would mean misdecoding the current update).
615 pendingPFChange = true;
616 pendingPF = pf;
617 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000618
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000619 char str[256];
620 pf.print(str, 256);
621 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000622 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000623
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000624 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000625 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000626
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000627 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000628
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000629 if (forceNonincremental || !continuousUpdates) {
630 pendingUpdate = true;
631 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
632 !forceNonincremental);
633 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000634
635 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000636}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000637
638void CConn::handleOptions(void *data)
639{
640 CConn *self = (CConn*)data;
641
642 // Checking all the details of the current set of encodings is just
643 // a pain. Assume something has changed, as resending the encoding
644 // list is cheap. Avoid overriding what the auto logic has selected
645 // though.
646 if (!autoSelect) {
647 int encNum = encodingNum(preferredEncoding);
648
649 if (encNum != -1)
650 self->currentEncoding = encNum;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000651 }
652
Pierre Ossmanda389562011-06-09 08:24:37 +0000653 self->cp.supportsLocalCursor = true;
654
Pierre Ossmana22459d2014-03-17 14:42:10 +0100655 if (customCompressLevel)
656 self->cp.compressLevel = compressLevel;
657 else
658 self->cp.compressLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000659
Pierre Ossmana22459d2014-03-17 14:42:10 +0100660 if (!noJpeg && !autoSelect)
661 self->cp.qualityLevel = qualityLevel;
662 else
663 self->cp.qualityLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000664
665 self->encodingChange = true;
666
667 // Format changes refreshes the entire screen though and are therefore
668 // very costly. It's probably worth the effort to see if it is necessary
669 // here.
670 PixelFormat pf;
671
672 if (fullColour) {
673 pf = self->fullColourPF;
674 } else {
675 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000676 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000677 else if (lowColourLevel == 1)
678 pf = lowColourPF;
679 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000680 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000681 }
682
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000683 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000684 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000685
686 // Without fences, we cannot safely trigger an update request directly
687 // but must wait for the next update to arrive.
688 if (self->supportsSyncFence)
689 self->requestNewUpdate();
690 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000691}
Pierre Ossmand9b90032015-09-23 12:18:52 +0200692
693void CConn::handleUpdateTimeout(void *data)
694{
695 CConn *self = (CConn *)data;
696
697 assert(self);
698
699 self->desktop->updateWindow();
700
701 Fl::repeat_timeout(1.0, handleUpdateTimeout, data);
702}