blob: 305eddcecd164e1964c62023b9b793fab9be7777 [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
163 char pfStr[100];
164 char spfStr[100];
165
166 cp.pf().print(pfStr, 100);
167 serverPF.print(spfStr, 100);
168
169 int secType = csecurity->getType();
170
171 snprintf(infoText, sizeof(infoText),
172 _("Desktop name: %.80s\n"
173 "Host: %.80s port: %d\n"
174 "Size: %d x %d\n"
175 "Pixel format: %s\n"
176 "(server default %s)\n"
177 "Requested encoding: %s\n"
178 "Last used encoding: %s\n"
179 "Line speed estimate: %d kbit/s\n"
180 "Protocol version: %d.%d\n"
181 "Security method: %s\n"),
182 cp.name(), serverHost, serverPort, cp.width, cp.height,
183 pfStr, spfStr, encodingName(currentEncoding),
184 encodingName(lastServerEncoding),
185 sock->inStream().kbitsPerSecond(),
186 cp.majorVersion, cp.minorVersion,
187 secTypeName(secType));
188
189 return infoText;
190}
191
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000192// The RFB core is not properly asynchronous, so it calls this callback
193// whenever it needs to block to wait for more data. Since FLTK is
194// monitoring the socket, we just make sure FLTK gets to run.
195
196void CConn::blockCallback()
197{
198 int next_timer;
199
200 next_timer = Timer::checkTimeouts();
201 if (next_timer == 0)
202 next_timer = INT_MAX;
203
204 Fl::wait((double)next_timer / 1000.0);
205}
206
DRC3e7ed812013-02-26 10:34:22 +0000207void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000208{
209 CConn *cc;
210 static bool recursing = false;
211
212 assert(data);
213 cc = (CConn*)data;
214
215 // I don't think processMsg() is recursion safe, so add this check
216 if (recursing)
217 return;
218
219 recursing = true;
220
221 try {
222 // processMsg() only processes one message, so we need to loop
223 // until the buffers are empty or things will stall.
224 do {
225 cc->processMsg();
226 } while (cc->sock->inStream().checkNoWait(1));
227 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000228 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000229 exit_vncviewer();
230 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000231 vlog.error("%s", e.str());
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000232 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000233 }
234
235 recursing = false;
236}
237
238////////////////////// CConnection callback methods //////////////////////
239
240// serverInit() is called when the serverInit message has been received. At
241// this point we create the desktop window and display it. We also tell the
242// server the pixel format and encodings to use and request the first update.
243void CConn::serverInit()
244{
245 CConnection::serverInit();
246
247 // If using AutoSelect with old servers, start in FullColor
248 // mode. See comment in autoSelectFormatAndEncoding.
249 if (cp.beforeVersion(3, 8) && autoSelect)
250 fullColour.setParam(true);
251
252 serverPF = cp.pf();
253
254 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
255 fullColourPF = desktop->getPreferredPF();
256
Pierre Ossman5d512c32011-11-04 11:42:16 +0000257 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000258 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000259
260 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000261 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000262
263 // This initial update request is a bit of a corner case, so we need
264 // to help out setting the correct format here.
265 assert(pendingPFChange);
Pierre Ossman5d512c32011-11-04 11:42:16 +0000266 cp.setPF(pendingPF);
267 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000268}
269
270// setDesktopSize() is called when the desktop size changes (including when
271// it is set initially).
272void CConn::setDesktopSize(int w, int h)
273{
274 CConnection::setDesktopSize(w,h);
275 resizeFramebuffer();
276}
277
278// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
279void CConn::setExtendedDesktopSize(int reason, int result, int w, int h,
280 const rfb::ScreenSet& layout)
281{
282 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
283
284 if ((reason == reasonClient) && (result != resultSuccess)) {
285 vlog.error(_("SetDesktopSize failed: %d"), result);
286 return;
287 }
288
289 resizeFramebuffer();
290}
291
292// setName() is called when the desktop name changes
293void CConn::setName(const char* name)
294{
295 CConnection::setName(name);
296 if (desktop)
297 desktop->setName(name);
298}
299
300// framebufferUpdateStart() is called at the beginning of an update.
301// Here we try to send out a new framebuffer update request so that the
302// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000303// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000304void CConn::framebufferUpdateStart()
305{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000306 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000307 pendingUpdate = false;
308
309 requestNewUpdate();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000310}
311
312// framebufferUpdateEnd() is called at the end of an update.
313// For each rectangle, the FdInStream will have timed the speed
314// of the connection, allowing us to select format and encoding
315// appropriately, and then request another incremental update.
316void CConn::framebufferUpdateEnd()
317{
318 desktop->updateWindow();
319
320 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000321 // We need fences to make extra update requests and continuous
322 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000323 if (cp.supportsFence)
324 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
325
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000326 firstUpdate = false;
327 }
328
Pierre Ossman5d512c32011-11-04 11:42:16 +0000329 // A format change has been scheduled and we are now past the update
330 // with the old format. Time to active the new one.
331 if (pendingPFChange) {
Pierre Ossman5d512c32011-11-04 11:42:16 +0000332 cp.setPF(pendingPF);
333 pendingPFChange = false;
334 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000335
336 // Compute new settings based on updated bandwidth values
337 if (autoSelect)
338 autoSelectFormatAndEncoding();
339
340 // Make sure that the FLTK handling and the timers gets some CPU time
341 // in case of back to back framebuffer updates.
342 Fl::check();
343 Timer::checkTimeouts();
344}
345
346// The rest of the callbacks are fairly self-explanatory...
347
348void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
349{
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100350 vlog.error("Invalid SetColourMapEntries from server!");
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000351}
352
353void CConn::bell()
354{
355 fl_beep();
356}
357
358void CConn::serverCutText(const char* str, rdr::U32 len)
359{
Pierre Ossman689c4582011-05-26 15:39:41 +0000360 char *buffer;
361 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000362
Pierre Ossman50104882013-05-24 10:47:27 +0000363 if (!acceptClipboard)
364 return;
365
Pierre Ossman689c4582011-05-26 15:39:41 +0000366 size = fl_utf8froma(NULL, 0, str, len);
367 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000368 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000369
370 size++;
371
372 buffer = new char[size];
373
374 ret = fl_utf8froma(buffer, size, str, len);
375 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000376
Pierre Ossman5803d3b2013-09-05 14:25:40 +0000377 vlog.debug("Got clipboard data (%d bytes)", strlen(buffer));
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000378
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000379 // RFB doesn't have separate selection and clipboard concepts, so we
380 // dump the data into both variants.
381 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000382 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000383
384 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000385}
386
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100387void CConn::dataRect(const Rect& r, int encoding)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000388{
389 sock->inStream().startTiming();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000390
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100391 if (encoding != encodingCopyRect)
392 lastServerEncoding = encoding;
393
394 if (!Decoder::supported(encoding)) {
395 fprintf(stderr, "Unknown rect encoding %d\n", encoding);
396 throw Exception("Unknown rect encoding");
397 }
398
399 if (!decoders[encoding]) {
Pierre Ossman668468b2014-01-31 12:37:32 +0100400 decoders[encoding] = Decoder::createDecoder(encoding, this);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100401 if (!decoders[encoding]) {
402 fprintf(stderr, "Unknown rect encoding %d\n", encoding);
403 throw Exception("Unknown rect encoding");
404 }
405 }
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200406 decoders[encoding]->readRect(r, desktop->getFramebuffer());
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100407
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000408 sock->inStream().stopTiming();
409}
410
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000411void CConn::setCursor(int width, int height, const Point& hotspot,
412 void* data, void* mask)
413{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000414 desktop->setCursor(width, height, hotspot, data, mask);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000415}
416
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000417void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
418{
419 CMsgHandler::fence(flags, len, data);
420
421 if (flags & fenceFlagRequest) {
422 // We handle everything synchronously so we trivially honor these modes
423 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
424
425 writer()->writeFence(flags, len, data);
426 return;
427 }
428
429 if (len == 0) {
430 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000431 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000432 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000433
434 if (cp.supportsContinuousUpdates) {
435 vlog.info(_("Enabling continuous updates"));
436 continuousUpdates = true;
437 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
438 }
439 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000440 } else {
441 // Pixel format change
442 rdr::MemInStream memStream(data, len);
443 PixelFormat pf;
444
445 pf.read(&memStream);
446
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000447 cp.setPF(pf);
448 }
449}
450
DRC33c15e32011-11-03 18:49:21 +0000451
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000452////////////////////// Internal methods //////////////////////
453
454void CConn::resizeFramebuffer()
455{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000456 if (!desktop)
457 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000458
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000459 if (continuousUpdates)
460 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
461
Pierre Ossman6455d852011-06-01 09:33:00 +0000462 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000463}
464
465// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
466// to the connection speed:
467//
468// First we wait for at least one second of bandwidth measurement.
469//
470// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
471// which should be perceptually lossless.
472//
473// If the bandwidth is below that, we choose a more lossy JPEG quality.
474//
475// If the bandwidth drops below 256 Kbps, we switch to palette mode.
476//
477// Note: The system here is fairly arbitrary and should be replaced
478// with something more intelligent at the server end.
479//
480void CConn::autoSelectFormatAndEncoding()
481{
482 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
483 unsigned int timeWaited = sock->inStream().timeWaited();
484 bool newFullColour = fullColour;
485 int newQualityLevel = qualityLevel;
486
487 // Always use Tight
488 if (currentEncoding != encodingTight) {
489 currentEncoding = encodingTight;
490 encodingChange = true;
491 }
492
493 // Check that we have a decent bandwidth measurement
494 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
495 return;
496
497 // Select appropriate quality level
498 if (!noJpeg) {
499 if (kbitsPerSecond > 16000)
500 newQualityLevel = 8;
501 else
502 newQualityLevel = 6;
503
504 if (newQualityLevel != qualityLevel) {
505 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
506 kbitsPerSecond, newQualityLevel);
507 cp.qualityLevel = newQualityLevel;
508 qualityLevel.setParam(newQualityLevel);
509 encodingChange = true;
510 }
511 }
512
513 if (cp.beforeVersion(3, 8)) {
514 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
515 // cursors "asynchronously". If this happens in the middle of a
516 // pixel format change, the server will encode the cursor with
517 // the old format, but the client will try to decode it
518 // according to the new format. This will lead to a
519 // crash. Therefore, we do not allow automatic format change for
520 // old servers.
521 return;
522 }
523
524 // Select best color level
525 newFullColour = (kbitsPerSecond > 256);
526 if (newFullColour != fullColour) {
527 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
528 kbitsPerSecond,
529 newFullColour ? _("enabled") : _("disabled"));
530 fullColour.setParam(newFullColour);
531 formatChange = true;
532 }
533}
534
535// checkEncodings() sends a setEncodings message if one is needed.
536void CConn::checkEncodings()
537{
538 if (encodingChange && writer()) {
539 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
540 writer()->writeSetEncodings(currentEncoding, true);
541 encodingChange = false;
542 }
543}
544
545// requestNewUpdate() requests an update from the server, having set the
546// format and encoding appropriately.
547void CConn::requestNewUpdate()
548{
549 if (formatChange) {
550 PixelFormat pf;
551
552 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000553 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000554
555 if (fullColour) {
556 pf = fullColourPF;
557 } else {
558 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000559 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000560 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000561 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000562 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000563 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000564 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000565
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000566 if (supportsSyncFence) {
567 // We let the fence carry the pixel format and switch once we
568 // get the response back. That way we will be synchronised with
569 // when the server switches.
570 rdr::MemOutStream memStream;
571
572 pf.write(&memStream);
573
574 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
575 memStream.length(), (const char*)memStream.data());
576 } else {
577 // New requests are sent out at the start of processing the last
578 // one, so we cannot switch our internal format right now (doing so
579 // would mean misdecoding the current update).
580 pendingPFChange = true;
581 pendingPF = pf;
582 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000583
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000584 char str[256];
585 pf.print(str, 256);
586 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000587 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000588
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000589 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000590 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000591
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000592 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000593
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000594 if (forceNonincremental || !continuousUpdates) {
595 pendingUpdate = true;
596 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
597 !forceNonincremental);
598 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000599
600 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000601}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000602
603void CConn::handleOptions(void *data)
604{
605 CConn *self = (CConn*)data;
606
607 // Checking all the details of the current set of encodings is just
608 // a pain. Assume something has changed, as resending the encoding
609 // list is cheap. Avoid overriding what the auto logic has selected
610 // though.
611 if (!autoSelect) {
612 int encNum = encodingNum(preferredEncoding);
613
614 if (encNum != -1)
615 self->currentEncoding = encNum;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000616 }
617
Pierre Ossmanda389562011-06-09 08:24:37 +0000618 self->cp.supportsLocalCursor = true;
619
Pierre Ossmana22459d2014-03-17 14:42:10 +0100620 if (customCompressLevel)
621 self->cp.compressLevel = compressLevel;
622 else
623 self->cp.compressLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000624
Pierre Ossmana22459d2014-03-17 14:42:10 +0100625 if (!noJpeg && !autoSelect)
626 self->cp.qualityLevel = qualityLevel;
627 else
628 self->cp.qualityLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000629
630 self->encodingChange = true;
631
632 // Format changes refreshes the entire screen though and are therefore
633 // very costly. It's probably worth the effort to see if it is necessary
634 // here.
635 PixelFormat pf;
636
637 if (fullColour) {
638 pf = self->fullColourPF;
639 } else {
640 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000641 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000642 else if (lowColourLevel == 1)
643 pf = lowColourPF;
644 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000645 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000646 }
647
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000648 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000649 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000650
651 // Without fences, we cannot safely trigger an update request directly
652 // but must wait for the next update to arrive.
653 if (self->supportsSyncFence)
654 self->requestNewUpdate();
655 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000656}