blob: 23f07b4dba677cabbf5fcefb958a7ff7fce2146e [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>
31#include <rfb/encodings.h>
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +010032#include <rfb/Decoder.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000033#include <rfb/Hostname.h>
34#include <rfb/LogWriter.h>
35#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
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +010084 memset(decoders, 0, sizeof(decoders));
85
Pierre Ossman5156d5e2011-03-09 09:42:34 +000086 int encNum = encodingNum(preferredEncoding);
87 if (encNum != -1)
88 currentEncoding = encNum;
89
Pierre Ossmanda389562011-06-09 08:24:37 +000090 cp.supportsLocalCursor = true;
91
Pierre Ossman5156d5e2011-03-09 09:42:34 +000092 cp.supportsDesktopResize = true;
93 cp.supportsExtendedDesktopSize = true;
94 cp.supportsDesktopRename = true;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000095
Pierre Ossmana22459d2014-03-17 14:42:10 +010096 if (customCompressLevel)
97 cp.compressLevel = compressLevel;
98 else
99 cp.compressLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000100
Pierre Ossmana22459d2014-03-17 14:42:10 +0100101 if (!noJpeg)
102 cp.qualityLevel = qualityLevel;
103 else
104 cp.qualityLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000105
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000106 if(sock == NULL) {
107 try {
108 getHostAndPort(vncServerName, &serverHost, &serverPort);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000109
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000110 sock = new network::TcpSocket(serverHost, serverPort);
111 vlog.info(_("connected to host %s port %d"), serverHost, serverPort);
112 } catch (rdr::Exception& e) {
113 vlog.error("%s", e.str());
114 fl_alert("%s", e.str());
115 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);
136
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100137 for (int i = 0; i < sizeof(decoders)/sizeof(decoders[0]); i++)
138 delete decoders[i];
139
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
186 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 Ossmanb2046432014-09-22 11:17:34 +0200192 serverPF.print(pfStr, 100);
193 snprintf(scratch, sizeof(scratch),
194 _("(server default %s)"), pfStr);
195 strcat(infoText, scratch);
196 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000197
Pierre Ossmanb2046432014-09-22 11:17:34 +0200198 snprintf(scratch, sizeof(scratch),
199 _("Requested encoding: %s"), encodingName(currentEncoding));
200 strcat(infoText, scratch);
201 strcat(infoText, "\n");
202
203 snprintf(scratch, sizeof(scratch),
204 _("Last used encoding: %s"), encodingName(lastServerEncoding));
205 strcat(infoText, scratch);
206 strcat(infoText, "\n");
207
208 snprintf(scratch, sizeof(scratch),
209 _("Line speed estimate: %d kbit/s"), sock->inStream().kbitsPerSecond());
210 strcat(infoText, scratch);
211 strcat(infoText, "\n");
212
213 snprintf(scratch, sizeof(scratch),
214 _("Protocol version: %d.%d"), cp.majorVersion, cp.minorVersion);
215 strcat(infoText, scratch);
216 strcat(infoText, "\n");
217
218 snprintf(scratch, sizeof(scratch),
219 _("Security method: %s"), secTypeName(csecurity->getType()));
220 strcat(infoText, scratch);
221 strcat(infoText, "\n");
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000222
223 return infoText;
224}
225
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000226// The RFB core is not properly asynchronous, so it calls this callback
227// whenever it needs to block to wait for more data. Since FLTK is
228// monitoring the socket, we just make sure FLTK gets to run.
229
230void CConn::blockCallback()
231{
232 int next_timer;
233
234 next_timer = Timer::checkTimeouts();
235 if (next_timer == 0)
236 next_timer = INT_MAX;
237
238 Fl::wait((double)next_timer / 1000.0);
239}
240
DRC3e7ed812013-02-26 10:34:22 +0000241void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000242{
243 CConn *cc;
244 static bool recursing = false;
245
246 assert(data);
247 cc = (CConn*)data;
248
249 // I don't think processMsg() is recursion safe, so add this check
250 if (recursing)
251 return;
252
253 recursing = true;
254
255 try {
256 // processMsg() only processes one message, so we need to loop
257 // until the buffers are empty or things will stall.
258 do {
259 cc->processMsg();
260 } while (cc->sock->inStream().checkNoWait(1));
261 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000262 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000263 exit_vncviewer();
264 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000265 vlog.error("%s", e.str());
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000266 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000267 }
268
269 recursing = false;
270}
271
272////////////////////// CConnection callback methods //////////////////////
273
274// serverInit() is called when the serverInit message has been received. At
275// this point we create the desktop window and display it. We also tell the
276// server the pixel format and encodings to use and request the first update.
277void CConn::serverInit()
278{
279 CConnection::serverInit();
280
281 // If using AutoSelect with old servers, start in FullColor
282 // mode. See comment in autoSelectFormatAndEncoding.
283 if (cp.beforeVersion(3, 8) && autoSelect)
284 fullColour.setParam(true);
285
286 serverPF = cp.pf();
287
288 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
289 fullColourPF = desktop->getPreferredPF();
290
Pierre Ossman5d512c32011-11-04 11:42:16 +0000291 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000292 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000293
294 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000295 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000296
297 // This initial update request is a bit of a corner case, so we need
298 // to help out setting the correct format here.
299 assert(pendingPFChange);
Pierre Ossman5d512c32011-11-04 11:42:16 +0000300 cp.setPF(pendingPF);
301 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000302}
303
304// setDesktopSize() is called when the desktop size changes (including when
305// it is set initially).
306void CConn::setDesktopSize(int w, int h)
307{
308 CConnection::setDesktopSize(w,h);
309 resizeFramebuffer();
310}
311
312// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
313void CConn::setExtendedDesktopSize(int reason, int result, int w, int h,
314 const rfb::ScreenSet& layout)
315{
316 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
317
318 if ((reason == reasonClient) && (result != resultSuccess)) {
319 vlog.error(_("SetDesktopSize failed: %d"), result);
320 return;
321 }
322
323 resizeFramebuffer();
324}
325
326// setName() is called when the desktop name changes
327void CConn::setName(const char* name)
328{
329 CConnection::setName(name);
330 if (desktop)
331 desktop->setName(name);
332}
333
334// framebufferUpdateStart() is called at the beginning of an update.
335// Here we try to send out a new framebuffer update request so that the
336// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000337// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000338void CConn::framebufferUpdateStart()
339{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000340 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000341 pendingUpdate = false;
342
343 requestNewUpdate();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000344}
345
346// framebufferUpdateEnd() is called at the end of an update.
347// For each rectangle, the FdInStream will have timed the speed
348// of the connection, allowing us to select format and encoding
349// appropriately, and then request another incremental update.
350void CConn::framebufferUpdateEnd()
351{
352 desktop->updateWindow();
353
354 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000355 // We need fences to make extra update requests and continuous
356 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000357 if (cp.supportsFence)
358 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
359
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000360 firstUpdate = false;
361 }
362
Pierre Ossman5d512c32011-11-04 11:42:16 +0000363 // A format change has been scheduled and we are now past the update
364 // with the old format. Time to active the new one.
365 if (pendingPFChange) {
Pierre Ossman5d512c32011-11-04 11:42:16 +0000366 cp.setPF(pendingPF);
367 pendingPFChange = false;
368 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000369
370 // Compute new settings based on updated bandwidth values
371 if (autoSelect)
372 autoSelectFormatAndEncoding();
373
374 // Make sure that the FLTK handling and the timers gets some CPU time
375 // in case of back to back framebuffer updates.
376 Fl::check();
377 Timer::checkTimeouts();
378}
379
380// The rest of the callbacks are fairly self-explanatory...
381
382void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
383{
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200384 vlog.error(_("Invalid SetColourMapEntries from server!"));
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000385}
386
387void CConn::bell()
388{
389 fl_beep();
390}
391
392void CConn::serverCutText(const char* str, rdr::U32 len)
393{
Pierre Ossman689c4582011-05-26 15:39:41 +0000394 char *buffer;
395 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000396
Pierre Ossman50104882013-05-24 10:47:27 +0000397 if (!acceptClipboard)
398 return;
399
Pierre Ossman689c4582011-05-26 15:39:41 +0000400 size = fl_utf8froma(NULL, 0, str, len);
401 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000402 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000403
404 size++;
405
406 buffer = new char[size];
407
408 ret = fl_utf8froma(buffer, size, str, len);
409 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000410
Pierre Ossman5803d3b2013-09-05 14:25:40 +0000411 vlog.debug("Got clipboard data (%d bytes)", strlen(buffer));
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000412
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000413 // RFB doesn't have separate selection and clipboard concepts, so we
414 // dump the data into both variants.
415 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000416 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000417
418 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000419}
420
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100421void CConn::dataRect(const Rect& r, int encoding)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000422{
423 sock->inStream().startTiming();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000424
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100425 if (encoding != encodingCopyRect)
426 lastServerEncoding = encoding;
427
428 if (!Decoder::supported(encoding)) {
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200429 vlog.error(_("Unknown rect encoding %d"), encoding);
430 throw Exception(_("Unknown rect encoding"));
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100431 }
432
433 if (!decoders[encoding]) {
Pierre Ossman668468b2014-01-31 12:37:32 +0100434 decoders[encoding] = Decoder::createDecoder(encoding, this);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100435 if (!decoders[encoding]) {
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200436 vlog.error(_("Unknown rect encoding %d"), encoding);
437 throw Exception(_("Unknown rect encoding"));
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100438 }
439 }
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200440 decoders[encoding]->readRect(r, desktop->getFramebuffer());
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100441
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000442 sock->inStream().stopTiming();
443}
444
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000445void CConn::setCursor(int width, int height, const Point& hotspot,
446 void* data, void* mask)
447{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000448 desktop->setCursor(width, height, hotspot, data, mask);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000449}
450
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000451void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
452{
453 CMsgHandler::fence(flags, len, data);
454
455 if (flags & fenceFlagRequest) {
456 // We handle everything synchronously so we trivially honor these modes
457 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
458
459 writer()->writeFence(flags, len, data);
460 return;
461 }
462
463 if (len == 0) {
464 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000465 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000466 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000467
468 if (cp.supportsContinuousUpdates) {
469 vlog.info(_("Enabling continuous updates"));
470 continuousUpdates = true;
471 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
472 }
473 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000474 } else {
475 // Pixel format change
476 rdr::MemInStream memStream(data, len);
477 PixelFormat pf;
478
479 pf.read(&memStream);
480
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000481 cp.setPF(pf);
482 }
483}
484
DRC33c15e32011-11-03 18:49:21 +0000485
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000486////////////////////// Internal methods //////////////////////
487
488void CConn::resizeFramebuffer()
489{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000490 if (!desktop)
491 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000492
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000493 if (continuousUpdates)
494 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
495
Pierre Ossman6455d852011-06-01 09:33:00 +0000496 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000497}
498
499// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
500// to the connection speed:
501//
502// First we wait for at least one second of bandwidth measurement.
503//
504// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
505// which should be perceptually lossless.
506//
507// If the bandwidth is below that, we choose a more lossy JPEG quality.
508//
509// If the bandwidth drops below 256 Kbps, we switch to palette mode.
510//
511// Note: The system here is fairly arbitrary and should be replaced
512// with something more intelligent at the server end.
513//
514void CConn::autoSelectFormatAndEncoding()
515{
516 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
517 unsigned int timeWaited = sock->inStream().timeWaited();
518 bool newFullColour = fullColour;
519 int newQualityLevel = qualityLevel;
520
521 // Always use Tight
522 if (currentEncoding != encodingTight) {
523 currentEncoding = encodingTight;
524 encodingChange = true;
525 }
526
527 // Check that we have a decent bandwidth measurement
528 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
529 return;
530
531 // Select appropriate quality level
532 if (!noJpeg) {
533 if (kbitsPerSecond > 16000)
534 newQualityLevel = 8;
535 else
536 newQualityLevel = 6;
537
538 if (newQualityLevel != qualityLevel) {
539 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
540 kbitsPerSecond, newQualityLevel);
541 cp.qualityLevel = newQualityLevel;
542 qualityLevel.setParam(newQualityLevel);
543 encodingChange = true;
544 }
545 }
546
547 if (cp.beforeVersion(3, 8)) {
548 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
549 // cursors "asynchronously". If this happens in the middle of a
550 // pixel format change, the server will encode the cursor with
551 // the old format, but the client will try to decode it
552 // according to the new format. This will lead to a
553 // crash. Therefore, we do not allow automatic format change for
554 // old servers.
555 return;
556 }
557
558 // Select best color level
559 newFullColour = (kbitsPerSecond > 256);
560 if (newFullColour != fullColour) {
561 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
562 kbitsPerSecond,
563 newFullColour ? _("enabled") : _("disabled"));
564 fullColour.setParam(newFullColour);
565 formatChange = true;
566 }
567}
568
569// checkEncodings() sends a setEncodings message if one is needed.
570void CConn::checkEncodings()
571{
572 if (encodingChange && writer()) {
573 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
574 writer()->writeSetEncodings(currentEncoding, true);
575 encodingChange = false;
576 }
577}
578
579// requestNewUpdate() requests an update from the server, having set the
580// format and encoding appropriately.
581void CConn::requestNewUpdate()
582{
583 if (formatChange) {
584 PixelFormat pf;
585
586 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000587 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000588
589 if (fullColour) {
590 pf = fullColourPF;
591 } else {
592 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000593 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000594 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000595 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000596 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000597 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000598 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000599
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000600 if (supportsSyncFence) {
601 // We let the fence carry the pixel format and switch once we
602 // get the response back. That way we will be synchronised with
603 // when the server switches.
604 rdr::MemOutStream memStream;
605
606 pf.write(&memStream);
607
608 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
609 memStream.length(), (const char*)memStream.data());
610 } else {
611 // New requests are sent out at the start of processing the last
612 // one, so we cannot switch our internal format right now (doing so
613 // would mean misdecoding the current update).
614 pendingPFChange = true;
615 pendingPF = pf;
616 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000617
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000618 char str[256];
619 pf.print(str, 256);
620 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000621 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000622
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000623 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000624 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000625
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000626 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000627
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000628 if (forceNonincremental || !continuousUpdates) {
629 pendingUpdate = true;
630 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
631 !forceNonincremental);
632 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000633
634 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000635}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000636
637void CConn::handleOptions(void *data)
638{
639 CConn *self = (CConn*)data;
640
641 // Checking all the details of the current set of encodings is just
642 // a pain. Assume something has changed, as resending the encoding
643 // list is cheap. Avoid overriding what the auto logic has selected
644 // though.
645 if (!autoSelect) {
646 int encNum = encodingNum(preferredEncoding);
647
648 if (encNum != -1)
649 self->currentEncoding = encNum;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000650 }
651
Pierre Ossmanda389562011-06-09 08:24:37 +0000652 self->cp.supportsLocalCursor = true;
653
Pierre Ossmana22459d2014-03-17 14:42:10 +0100654 if (customCompressLevel)
655 self->cp.compressLevel = compressLevel;
656 else
657 self->cp.compressLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000658
Pierre Ossmana22459d2014-03-17 14:42:10 +0100659 if (!noJpeg && !autoSelect)
660 self->cp.qualityLevel = qualityLevel;
661 else
662 self->cp.qualityLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000663
664 self->encodingChange = true;
665
666 // Format changes refreshes the entire screen though and are therefore
667 // very costly. It's probably worth the effort to see if it is necessary
668 // here.
669 PixelFormat pf;
670
671 if (fullColour) {
672 pf = self->fullColourPF;
673 } else {
674 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000675 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000676 else if (lowColourLevel == 1)
677 pf = lowColourPF;
678 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000679 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000680 }
681
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000682 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000683 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000684
685 // Without fences, we cannot safely trigger an update request directly
686 // but must wait for the next update to arrive.
687 if (self->supportsSyncFence)
688 self->requestNewUpdate();
689 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000690}