blob: d7f57b5f8f54c169344ea37991ecbf519e34d642 [file] [log] [blame]
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 * Copyright 2009-2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRC33c15e32011-11-03 18:49:21 +00003 * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
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>
32#include <rfb/Hostname.h>
33#include <rfb/LogWriter.h>
34#include <rfb/util.h>
35#include <rfb/screenTypes.h>
Pierre Ossmane28bdb22011-11-14 16:02:06 +000036#include <rfb/fenceTypes.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000037#include <rfb/Timer.h>
Pierre Ossmane28bdb22011-11-14 16:02:06 +000038#include <rdr/MemInStream.h>
39#include <rdr/MemOutStream.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000040#include <network/TcpSocket.h>
41
42#include <FL/Fl.H>
43#include <FL/fl_ask.H>
44
45#include "CConn.h"
Pierre Ossmanf4f30942011-05-17 09:39:07 +000046#include "OptionsDialog.h"
Pierre Ossman947b48d2014-01-27 16:52:35 +010047#include "DesktopWindow.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000048#include "i18n.h"
49#include "parameters.h"
Pierre Ossman39ceb502011-07-12 15:54:25 +000050#include "vncviewer.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000051
DRCb65bb932011-06-24 03:17:00 +000052#ifdef WIN32
53#include "win32.h"
54#endif
55
Pierre Ossman5156d5e2011-03-09 09:42:34 +000056using namespace rdr;
57using namespace rfb;
58using namespace std;
59
Pierre Ossman5156d5e2011-03-09 09:42:34 +000060static rfb::LogWriter vlog("CConn");
61
Pierre Ossmancf836f22011-07-14 14:34:09 +000062// 8 colours (1 bit per component)
63static const PixelFormat verylowColourPF(8, 3,false, true,
64 1, 1, 1, 2, 1, 0);
65// 64 colours (2 bits per component)
66static const PixelFormat lowColourPF(8, 6, false, true,
67 3, 3, 3, 4, 2, 0);
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010068// 256 colours (2-3 bits per component)
69static const PixelFormat mediumColourPF(8, 8, false, true,
70 7, 7, 3, 5, 2, 0);
Pierre Ossmanf4f30942011-05-17 09:39:07 +000071
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000072CConn::CConn(const char* vncServerName, network::Socket* socket=NULL)
73 : serverHost(0), serverPort(0), desktop(NULL),
Pierre Ossman5d512c32011-11-04 11:42:16 +000074 pendingPFChange(false),
Pierre Ossman5156d5e2011-03-09 09:42:34 +000075 currentEncoding(encodingTight), lastServerEncoding((unsigned int)-1),
76 formatChange(false), encodingChange(false),
Pierre Ossmanaa73c892011-11-15 12:13:37 +000077 firstUpdate(true), pendingUpdate(false), continuousUpdates(false),
Pierre Ossmane28bdb22011-11-14 16:02:06 +000078 forceNonincremental(true), supportsSyncFence(false)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000079{
80 setShared(::shared);
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000081 sock = socket;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000082
83 int encNum = encodingNum(preferredEncoding);
84 if (encNum != -1)
85 currentEncoding = encNum;
86
Pierre Ossmanda389562011-06-09 08:24:37 +000087 cp.supportsLocalCursor = true;
88
Pierre Ossman5156d5e2011-03-09 09:42:34 +000089 cp.supportsDesktopResize = true;
90 cp.supportsExtendedDesktopSize = true;
91 cp.supportsDesktopRename = true;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000092
Pierre Ossmana22459d2014-03-17 14:42:10 +010093 if (customCompressLevel)
94 cp.compressLevel = compressLevel;
95 else
96 cp.compressLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000097
Pierre Ossmana22459d2014-03-17 14:42:10 +010098 if (!noJpeg)
99 cp.qualityLevel = qualityLevel;
100 else
101 cp.qualityLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000102
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000103 if(sock == NULL) {
104 try {
105 getHostAndPort(vncServerName, &serverHost, &serverPort);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000106
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000107 sock = new network::TcpSocket(serverHost, serverPort);
108 vlog.info(_("connected to host %s port %d"), serverHost, serverPort);
109 } catch (rdr::Exception& e) {
110 vlog.error("%s", e.str());
111 fl_alert("%s", e.str());
112 exit_vncviewer();
113 return;
114 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000115 }
116
117 Fl::add_fd(sock->getFd(), FL_READ | FL_EXCEPT, socketEvent, this);
118
119 // See callback below
120 sock->inStream().setBlockCallback(this);
121
122 setServerName(serverHost);
123 setStreams(&sock->inStream(), &sock->outStream());
124
125 initialiseProtocol();
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000126
127 OptionsDialog::addCallback(handleOptions, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000128}
129
130CConn::~CConn()
131{
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000132 OptionsDialog::removeCallback(handleOptions);
133
Pierre Ossman6a9e2e62011-05-19 14:47:43 +0000134 if (desktop)
135 delete desktop;
136
Pierre Ossman5e04c262011-11-09 11:31:12 +0000137 delete [] serverHost;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000138 if (sock)
139 Fl::remove_fd(sock->getFd());
140 delete sock;
141}
142
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000143void CConn::refreshFramebuffer()
144{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000145 forceNonincremental = true;
146
147 // Without fences, we cannot safely trigger an update request directly
148 // but must wait for the next update to arrive.
149 if (supportsSyncFence)
150 requestNewUpdate();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000151}
152
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000153const char *CConn::connectionInfo()
154{
155 static char infoText[1024] = "";
156
157 char pfStr[100];
158 char spfStr[100];
159
160 cp.pf().print(pfStr, 100);
161 serverPF.print(spfStr, 100);
162
163 int secType = csecurity->getType();
164
165 snprintf(infoText, sizeof(infoText),
166 _("Desktop name: %.80s\n"
167 "Host: %.80s port: %d\n"
168 "Size: %d x %d\n"
169 "Pixel format: %s\n"
170 "(server default %s)\n"
171 "Requested encoding: %s\n"
172 "Last used encoding: %s\n"
173 "Line speed estimate: %d kbit/s\n"
174 "Protocol version: %d.%d\n"
175 "Security method: %s\n"),
176 cp.name(), serverHost, serverPort, cp.width, cp.height,
177 pfStr, spfStr, encodingName(currentEncoding),
178 encodingName(lastServerEncoding),
179 sock->inStream().kbitsPerSecond(),
180 cp.majorVersion, cp.minorVersion,
181 secTypeName(secType));
182
183 return infoText;
184}
185
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000186// The RFB core is not properly asynchronous, so it calls this callback
187// whenever it needs to block to wait for more data. Since FLTK is
188// monitoring the socket, we just make sure FLTK gets to run.
189
190void CConn::blockCallback()
191{
192 int next_timer;
193
194 next_timer = Timer::checkTimeouts();
195 if (next_timer == 0)
196 next_timer = INT_MAX;
197
198 Fl::wait((double)next_timer / 1000.0);
199}
200
DRC3e7ed812013-02-26 10:34:22 +0000201void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000202{
203 CConn *cc;
204 static bool recursing = false;
205
206 assert(data);
207 cc = (CConn*)data;
208
209 // I don't think processMsg() is recursion safe, so add this check
210 if (recursing)
211 return;
212
213 recursing = true;
214
215 try {
216 // processMsg() only processes one message, so we need to loop
217 // until the buffers are empty or things will stall.
218 do {
219 cc->processMsg();
220 } while (cc->sock->inStream().checkNoWait(1));
221 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000222 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000223 exit_vncviewer();
224 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000225 vlog.error("%s", e.str());
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000226 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000227 }
228
229 recursing = false;
230}
231
232////////////////////// CConnection callback methods //////////////////////
233
234// serverInit() is called when the serverInit message has been received. At
235// this point we create the desktop window and display it. We also tell the
236// server the pixel format and encodings to use and request the first update.
237void CConn::serverInit()
238{
239 CConnection::serverInit();
240
241 // If using AutoSelect with old servers, start in FullColor
242 // mode. See comment in autoSelectFormatAndEncoding.
243 if (cp.beforeVersion(3, 8) && autoSelect)
244 fullColour.setParam(true);
245
246 serverPF = cp.pf();
247
248 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
249 fullColourPF = desktop->getPreferredPF();
250
Pierre Ossman5d512c32011-11-04 11:42:16 +0000251 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000252 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000253
254 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000255 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000256
257 // This initial update request is a bit of a corner case, so we need
258 // to help out setting the correct format here.
259 assert(pendingPFChange);
260 desktop->setServerPF(pendingPF);
261 cp.setPF(pendingPF);
262 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000263}
264
265// setDesktopSize() is called when the desktop size changes (including when
266// it is set initially).
267void CConn::setDesktopSize(int w, int h)
268{
269 CConnection::setDesktopSize(w,h);
270 resizeFramebuffer();
271}
272
273// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
274void CConn::setExtendedDesktopSize(int reason, int result, int w, int h,
275 const rfb::ScreenSet& layout)
276{
277 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
278
279 if ((reason == reasonClient) && (result != resultSuccess)) {
280 vlog.error(_("SetDesktopSize failed: %d"), result);
281 return;
282 }
283
284 resizeFramebuffer();
285}
286
287// setName() is called when the desktop name changes
288void CConn::setName(const char* name)
289{
290 CConnection::setName(name);
291 if (desktop)
292 desktop->setName(name);
293}
294
295// framebufferUpdateStart() is called at the beginning of an update.
296// Here we try to send out a new framebuffer update request so that the
297// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000298// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000299void CConn::framebufferUpdateStart()
300{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000301 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000302 pendingUpdate = false;
303
304 requestNewUpdate();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000305}
306
307// framebufferUpdateEnd() is called at the end of an update.
308// For each rectangle, the FdInStream will have timed the speed
309// of the connection, allowing us to select format and encoding
310// appropriately, and then request another incremental update.
311void CConn::framebufferUpdateEnd()
312{
313 desktop->updateWindow();
314
315 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000316 // We need fences to make extra update requests and continuous
317 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000318 if (cp.supportsFence)
319 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
320
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000321 firstUpdate = false;
322 }
323
Pierre Ossman5d512c32011-11-04 11:42:16 +0000324 // A format change has been scheduled and we are now past the update
325 // with the old format. Time to active the new one.
326 if (pendingPFChange) {
327 desktop->setServerPF(pendingPF);
328 cp.setPF(pendingPF);
329 pendingPFChange = false;
330 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000331
332 // Compute new settings based on updated bandwidth values
333 if (autoSelect)
334 autoSelectFormatAndEncoding();
335
336 // Make sure that the FLTK handling and the timers gets some CPU time
337 // in case of back to back framebuffer updates.
338 Fl::check();
339 Timer::checkTimeouts();
340}
341
342// The rest of the callbacks are fairly self-explanatory...
343
344void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
345{
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100346 vlog.error("Invalid SetColourMapEntries from server!");
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000347}
348
349void CConn::bell()
350{
351 fl_beep();
352}
353
354void CConn::serverCutText(const char* str, rdr::U32 len)
355{
Pierre Ossman689c4582011-05-26 15:39:41 +0000356 char *buffer;
357 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000358
Pierre Ossman50104882013-05-24 10:47:27 +0000359 if (!acceptClipboard)
360 return;
361
Pierre Ossman689c4582011-05-26 15:39:41 +0000362 size = fl_utf8froma(NULL, 0, str, len);
363 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000364 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000365
366 size++;
367
368 buffer = new char[size];
369
370 ret = fl_utf8froma(buffer, size, str, len);
371 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000372
Pierre Ossman5803d3b2013-09-05 14:25:40 +0000373 vlog.debug("Got clipboard data (%d bytes)", strlen(buffer));
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000374
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000375 // RFB doesn't have separate selection and clipboard concepts, so we
376 // dump the data into both variants.
377 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000378 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000379
380 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000381}
382
383// We start timing on beginRect and stop timing on endRect, to
384// avoid skewing the bandwidth estimation as a result of the server
385// being slow or the network having high latency
386void CConn::beginRect(const Rect& r, int encoding)
387{
388 sock->inStream().startTiming();
389 if (encoding != encodingCopyRect) {
390 lastServerEncoding = encoding;
391 }
392}
393
394void CConn::endRect(const Rect& r, int encoding)
395{
396 sock->inStream().stopTiming();
397}
398
399void CConn::fillRect(const rfb::Rect& r, rfb::Pixel p)
400{
401 desktop->fillRect(r,p);
402}
Pierre Ossman0dff4b82014-01-27 16:17:21 +0100403
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000404void CConn::imageRect(const rfb::Rect& r, void* p)
405{
406 desktop->imageRect(r,p);
407}
Pierre Ossman0dff4b82014-01-27 16:17:21 +0100408
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000409void CConn::copyRect(const rfb::Rect& r, int sx, int sy)
410{
411 desktop->copyRect(r,sx,sy);
412}
Pierre Ossman0dff4b82014-01-27 16:17:21 +0100413
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000414void CConn::setCursor(int width, int height, const Point& hotspot,
415 void* data, void* mask)
416{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000417 desktop->setCursor(width, height, hotspot, data, mask);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000418}
419
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000420void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
421{
422 CMsgHandler::fence(flags, len, data);
423
424 if (flags & fenceFlagRequest) {
425 // We handle everything synchronously so we trivially honor these modes
426 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
427
428 writer()->writeFence(flags, len, data);
429 return;
430 }
431
432 if (len == 0) {
433 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000434 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000435 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000436
437 if (cp.supportsContinuousUpdates) {
438 vlog.info(_("Enabling continuous updates"));
439 continuousUpdates = true;
440 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
441 }
442 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000443 } else {
444 // Pixel format change
445 rdr::MemInStream memStream(data, len);
446 PixelFormat pf;
447
448 pf.read(&memStream);
449
450 desktop->setServerPF(pf);
451 cp.setPF(pf);
452 }
453}
454
Pierre Ossman945cdda2014-01-28 14:13:12 +0100455rdr::U8* CConn::getRawBufferRW(const rfb::Rect& r, int* stride) {
456 return desktop->getBufferRW(r, stride);
DRC33c15e32011-11-03 18:49:21 +0000457}
Pierre Ossman945cdda2014-01-28 14:13:12 +0100458void CConn::releaseRawBuffer(const rfb::Rect& r) {
DRC33c15e32011-11-03 18:49:21 +0000459 desktop->damageRect(r);
460}
461
462
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000463////////////////////// Internal methods //////////////////////
464
465void CConn::resizeFramebuffer()
466{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000467 if (!desktop)
468 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000469
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000470 if (continuousUpdates)
471 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
472
Pierre Ossman6455d852011-06-01 09:33:00 +0000473 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000474}
475
476// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
477// to the connection speed:
478//
479// First we wait for at least one second of bandwidth measurement.
480//
481// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
482// which should be perceptually lossless.
483//
484// If the bandwidth is below that, we choose a more lossy JPEG quality.
485//
486// If the bandwidth drops below 256 Kbps, we switch to palette mode.
487//
488// Note: The system here is fairly arbitrary and should be replaced
489// with something more intelligent at the server end.
490//
491void CConn::autoSelectFormatAndEncoding()
492{
493 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
494 unsigned int timeWaited = sock->inStream().timeWaited();
495 bool newFullColour = fullColour;
496 int newQualityLevel = qualityLevel;
497
498 // Always use Tight
499 if (currentEncoding != encodingTight) {
500 currentEncoding = encodingTight;
501 encodingChange = true;
502 }
503
504 // Check that we have a decent bandwidth measurement
505 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
506 return;
507
508 // Select appropriate quality level
509 if (!noJpeg) {
510 if (kbitsPerSecond > 16000)
511 newQualityLevel = 8;
512 else
513 newQualityLevel = 6;
514
515 if (newQualityLevel != qualityLevel) {
516 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
517 kbitsPerSecond, newQualityLevel);
518 cp.qualityLevel = newQualityLevel;
519 qualityLevel.setParam(newQualityLevel);
520 encodingChange = true;
521 }
522 }
523
524 if (cp.beforeVersion(3, 8)) {
525 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
526 // cursors "asynchronously". If this happens in the middle of a
527 // pixel format change, the server will encode the cursor with
528 // the old format, but the client will try to decode it
529 // according to the new format. This will lead to a
530 // crash. Therefore, we do not allow automatic format change for
531 // old servers.
532 return;
533 }
534
535 // Select best color level
536 newFullColour = (kbitsPerSecond > 256);
537 if (newFullColour != fullColour) {
538 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
539 kbitsPerSecond,
540 newFullColour ? _("enabled") : _("disabled"));
541 fullColour.setParam(newFullColour);
542 formatChange = true;
543 }
544}
545
546// checkEncodings() sends a setEncodings message if one is needed.
547void CConn::checkEncodings()
548{
549 if (encodingChange && writer()) {
550 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
551 writer()->writeSetEncodings(currentEncoding, true);
552 encodingChange = false;
553 }
554}
555
556// requestNewUpdate() requests an update from the server, having set the
557// format and encoding appropriately.
558void CConn::requestNewUpdate()
559{
560 if (formatChange) {
561 PixelFormat pf;
562
563 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000564 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000565
566 if (fullColour) {
567 pf = fullColourPF;
568 } else {
569 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000570 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000571 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000572 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000573 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000574 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000575 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000576
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000577 if (supportsSyncFence) {
578 // We let the fence carry the pixel format and switch once we
579 // get the response back. That way we will be synchronised with
580 // when the server switches.
581 rdr::MemOutStream memStream;
582
583 pf.write(&memStream);
584
585 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
586 memStream.length(), (const char*)memStream.data());
587 } else {
588 // New requests are sent out at the start of processing the last
589 // one, so we cannot switch our internal format right now (doing so
590 // would mean misdecoding the current update).
591 pendingPFChange = true;
592 pendingPF = pf;
593 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000594
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000595 char str[256];
596 pf.print(str, 256);
597 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000598 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000599
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000600 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000601 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000602
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000603 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000604
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000605 if (forceNonincremental || !continuousUpdates) {
606 pendingUpdate = true;
607 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
608 !forceNonincremental);
609 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000610
611 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000612}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000613
614void CConn::handleOptions(void *data)
615{
616 CConn *self = (CConn*)data;
617
618 // Checking all the details of the current set of encodings is just
619 // a pain. Assume something has changed, as resending the encoding
620 // list is cheap. Avoid overriding what the auto logic has selected
621 // though.
622 if (!autoSelect) {
623 int encNum = encodingNum(preferredEncoding);
624
625 if (encNum != -1)
626 self->currentEncoding = encNum;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000627 }
628
Pierre Ossmanda389562011-06-09 08:24:37 +0000629 self->cp.supportsLocalCursor = true;
630
Pierre Ossmana22459d2014-03-17 14:42:10 +0100631 if (customCompressLevel)
632 self->cp.compressLevel = compressLevel;
633 else
634 self->cp.compressLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000635
Pierre Ossmana22459d2014-03-17 14:42:10 +0100636 if (!noJpeg && !autoSelect)
637 self->cp.qualityLevel = qualityLevel;
638 else
639 self->cp.qualityLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000640
641 self->encodingChange = true;
642
643 // Format changes refreshes the entire screen though and are therefore
644 // very costly. It's probably worth the effort to see if it is necessary
645 // here.
646 PixelFormat pf;
647
648 if (fullColour) {
649 pf = self->fullColourPF;
650 } else {
651 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000652 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000653 else if (lowColourLevel == 1)
654 pf = lowColourPF;
655 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000656 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000657 }
658
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000659 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000660 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000661
662 // Without fences, we cannot safely trigger an update request directly
663 // but must wait for the next update to arrive.
664 if (self->supportsSyncFence)
665 self->requestNewUpdate();
666 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000667}