blob: e4d55aa041b5f2598d34152ca3d6207df587a3bd [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 Ossman5156d5e2011-03-09 09:42:34 +000047#include "i18n.h"
48#include "parameters.h"
Pierre Ossman39ceb502011-07-12 15:54:25 +000049#include "vncviewer.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000050
DRCb65bb932011-06-24 03:17:00 +000051#ifdef WIN32
52#include "win32.h"
53#endif
54
Pierre Ossman5156d5e2011-03-09 09:42:34 +000055using namespace rdr;
56using namespace rfb;
57using namespace std;
58
Pierre Ossman5156d5e2011-03-09 09:42:34 +000059static rfb::LogWriter vlog("CConn");
60
Pierre Ossmancf836f22011-07-14 14:34:09 +000061// 8 colours (1 bit per component)
62static const PixelFormat verylowColourPF(8, 3,false, true,
63 1, 1, 1, 2, 1, 0);
64// 64 colours (2 bits per component)
65static const PixelFormat lowColourPF(8, 6, false, true,
66 3, 3, 3, 4, 2, 0);
67// 256 colours (palette)
68static const PixelFormat mediumColourPF(8, 8, false, false);
Pierre Ossmanf4f30942011-05-17 09:39:07 +000069
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000070CConn::CConn(const char* vncServerName, network::Socket* socket=NULL)
71 : serverHost(0), serverPort(0), desktop(NULL),
Pierre Ossman5d512c32011-11-04 11:42:16 +000072 pendingPFChange(false),
Pierre Ossman5156d5e2011-03-09 09:42:34 +000073 currentEncoding(encodingTight), lastServerEncoding((unsigned int)-1),
74 formatChange(false), encodingChange(false),
Pierre Ossmanaa73c892011-11-15 12:13:37 +000075 firstUpdate(true), pendingUpdate(false), continuousUpdates(false),
Pierre Ossmane28bdb22011-11-14 16:02:06 +000076 forceNonincremental(true), supportsSyncFence(false)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000077{
78 setShared(::shared);
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000079 sock = socket;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000080
81 int encNum = encodingNum(preferredEncoding);
82 if (encNum != -1)
83 currentEncoding = encNum;
84
Pierre Ossmanda389562011-06-09 08:24:37 +000085 cp.supportsLocalCursor = true;
86
Pierre Ossman5156d5e2011-03-09 09:42:34 +000087 cp.supportsDesktopResize = true;
88 cp.supportsExtendedDesktopSize = true;
89 cp.supportsDesktopRename = true;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000090
91 cp.customCompressLevel = customCompressLevel;
92 cp.compressLevel = compressLevel;
93
94 cp.noJpeg = noJpeg;
95 cp.qualityLevel = qualityLevel;
96
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000097 if(sock == NULL) {
98 try {
99 getHostAndPort(vncServerName, &serverHost, &serverPort);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000100
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000101 sock = new network::TcpSocket(serverHost, serverPort);
102 vlog.info(_("connected to host %s port %d"), serverHost, serverPort);
103 } catch (rdr::Exception& e) {
104 vlog.error("%s", e.str());
105 fl_alert("%s", e.str());
106 exit_vncviewer();
107 return;
108 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000109 }
110
111 Fl::add_fd(sock->getFd(), FL_READ | FL_EXCEPT, socketEvent, this);
112
113 // See callback below
114 sock->inStream().setBlockCallback(this);
115
116 setServerName(serverHost);
117 setStreams(&sock->inStream(), &sock->outStream());
118
119 initialiseProtocol();
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000120
121 OptionsDialog::addCallback(handleOptions, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000122}
123
124CConn::~CConn()
125{
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000126 OptionsDialog::removeCallback(handleOptions);
127
Pierre Ossman6a9e2e62011-05-19 14:47:43 +0000128 if (desktop)
129 delete desktop;
130
Pierre Ossman5e04c262011-11-09 11:31:12 +0000131 delete [] serverHost;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000132 if (sock)
133 Fl::remove_fd(sock->getFd());
134 delete sock;
135}
136
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000137void CConn::refreshFramebuffer()
138{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000139 forceNonincremental = true;
140
141 // Without fences, we cannot safely trigger an update request directly
142 // but must wait for the next update to arrive.
143 if (supportsSyncFence)
144 requestNewUpdate();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000145}
146
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000147const char *CConn::connectionInfo()
148{
149 static char infoText[1024] = "";
150
151 char pfStr[100];
152 char spfStr[100];
153
154 cp.pf().print(pfStr, 100);
155 serverPF.print(spfStr, 100);
156
157 int secType = csecurity->getType();
158
159 snprintf(infoText, sizeof(infoText),
160 _("Desktop name: %.80s\n"
161 "Host: %.80s port: %d\n"
162 "Size: %d x %d\n"
163 "Pixel format: %s\n"
164 "(server default %s)\n"
165 "Requested encoding: %s\n"
166 "Last used encoding: %s\n"
167 "Line speed estimate: %d kbit/s\n"
168 "Protocol version: %d.%d\n"
169 "Security method: %s\n"),
170 cp.name(), serverHost, serverPort, cp.width, cp.height,
171 pfStr, spfStr, encodingName(currentEncoding),
172 encodingName(lastServerEncoding),
173 sock->inStream().kbitsPerSecond(),
174 cp.majorVersion, cp.minorVersion,
175 secTypeName(secType));
176
177 return infoText;
178}
179
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000180// The RFB core is not properly asynchronous, so it calls this callback
181// whenever it needs to block to wait for more data. Since FLTK is
182// monitoring the socket, we just make sure FLTK gets to run.
183
184void CConn::blockCallback()
185{
186 int next_timer;
187
188 next_timer = Timer::checkTimeouts();
189 if (next_timer == 0)
190 next_timer = INT_MAX;
191
192 Fl::wait((double)next_timer / 1000.0);
193}
194
195void CConn::socketEvent(int fd, void *data)
196{
197 CConn *cc;
198 static bool recursing = false;
199
200 assert(data);
201 cc = (CConn*)data;
202
203 // I don't think processMsg() is recursion safe, so add this check
204 if (recursing)
205 return;
206
207 recursing = true;
208
209 try {
210 // processMsg() only processes one message, so we need to loop
211 // until the buffers are empty or things will stall.
212 do {
213 cc->processMsg();
214 } while (cc->sock->inStream().checkNoWait(1));
215 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000216 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000217 exit_vncviewer();
218 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000219 vlog.error("%s", e.str());
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000220 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000221 }
222
223 recursing = false;
224}
225
226////////////////////// CConnection callback methods //////////////////////
227
228// serverInit() is called when the serverInit message has been received. At
229// this point we create the desktop window and display it. We also tell the
230// server the pixel format and encodings to use and request the first update.
231void CConn::serverInit()
232{
233 CConnection::serverInit();
234
235 // If using AutoSelect with old servers, start in FullColor
236 // mode. See comment in autoSelectFormatAndEncoding.
237 if (cp.beforeVersion(3, 8) && autoSelect)
238 fullColour.setParam(true);
239
240 serverPF = cp.pf();
241
242 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
243 fullColourPF = desktop->getPreferredPF();
244
Pierre Ossman5d512c32011-11-04 11:42:16 +0000245 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000246 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000247
248 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000249 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000250
251 // This initial update request is a bit of a corner case, so we need
252 // to help out setting the correct format here.
253 assert(pendingPFChange);
254 desktop->setServerPF(pendingPF);
255 cp.setPF(pendingPF);
256 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000257}
258
259// setDesktopSize() is called when the desktop size changes (including when
260// it is set initially).
261void CConn::setDesktopSize(int w, int h)
262{
263 CConnection::setDesktopSize(w,h);
264 resizeFramebuffer();
265}
266
267// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
268void CConn::setExtendedDesktopSize(int reason, int result, int w, int h,
269 const rfb::ScreenSet& layout)
270{
271 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
272
273 if ((reason == reasonClient) && (result != resultSuccess)) {
274 vlog.error(_("SetDesktopSize failed: %d"), result);
275 return;
276 }
277
278 resizeFramebuffer();
279}
280
281// setName() is called when the desktop name changes
282void CConn::setName(const char* name)
283{
284 CConnection::setName(name);
285 if (desktop)
286 desktop->setName(name);
287}
288
289// framebufferUpdateStart() is called at the beginning of an update.
290// Here we try to send out a new framebuffer update request so that the
291// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000292// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000293void CConn::framebufferUpdateStart()
294{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000295 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000296 pendingUpdate = false;
297
298 requestNewUpdate();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000299}
300
301// framebufferUpdateEnd() is called at the end of an update.
302// For each rectangle, the FdInStream will have timed the speed
303// of the connection, allowing us to select format and encoding
304// appropriately, and then request another incremental update.
305void CConn::framebufferUpdateEnd()
306{
307 desktop->updateWindow();
308
309 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000310 // We need fences to make extra update requests and continuous
311 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000312 if (cp.supportsFence)
313 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
314
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000315 firstUpdate = false;
316 }
317
Pierre Ossman5d512c32011-11-04 11:42:16 +0000318 // A format change has been scheduled and we are now past the update
319 // with the old format. Time to active the new one.
320 if (pendingPFChange) {
321 desktop->setServerPF(pendingPF);
322 cp.setPF(pendingPF);
323 pendingPFChange = false;
324 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000325
326 // Compute new settings based on updated bandwidth values
327 if (autoSelect)
328 autoSelectFormatAndEncoding();
329
330 // Make sure that the FLTK handling and the timers gets some CPU time
331 // in case of back to back framebuffer updates.
332 Fl::check();
333 Timer::checkTimeouts();
334}
335
336// The rest of the callbacks are fairly self-explanatory...
337
338void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
339{
340 desktop->setColourMapEntries(firstColour, nColours, rgbs);
341}
342
343void CConn::bell()
344{
345 fl_beep();
346}
347
348void CConn::serverCutText(const char* str, rdr::U32 len)
349{
Pierre Ossman689c4582011-05-26 15:39:41 +0000350 char *buffer;
351 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000352
Pierre Ossman689c4582011-05-26 15:39:41 +0000353 size = fl_utf8froma(NULL, 0, str, len);
354 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000355 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000356
357 size++;
358
359 buffer = new char[size];
360
361 ret = fl_utf8froma(buffer, size, str, len);
362 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000363
364 vlog.debug("Got clipboard data: '%s'", buffer);
365
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000366 // RFB doesn't have separate selection and clipboard concepts, so we
367 // dump the data into both variants.
368 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000369 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000370
371 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000372}
373
374// We start timing on beginRect and stop timing on endRect, to
375// avoid skewing the bandwidth estimation as a result of the server
376// being slow or the network having high latency
377void CConn::beginRect(const Rect& r, int encoding)
378{
379 sock->inStream().startTiming();
380 if (encoding != encodingCopyRect) {
381 lastServerEncoding = encoding;
382 }
383}
384
385void CConn::endRect(const Rect& r, int encoding)
386{
387 sock->inStream().stopTiming();
388}
389
390void CConn::fillRect(const rfb::Rect& r, rfb::Pixel p)
391{
392 desktop->fillRect(r,p);
393}
394void CConn::imageRect(const rfb::Rect& r, void* p)
395{
396 desktop->imageRect(r,p);
397}
398void CConn::copyRect(const rfb::Rect& r, int sx, int sy)
399{
400 desktop->copyRect(r,sx,sy);
401}
402void CConn::setCursor(int width, int height, const Point& hotspot,
403 void* data, void* mask)
404{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000405 desktop->setCursor(width, height, hotspot, data, mask);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000406}
407
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000408void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
409{
410 CMsgHandler::fence(flags, len, data);
411
412 if (flags & fenceFlagRequest) {
413 // We handle everything synchronously so we trivially honor these modes
414 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
415
416 writer()->writeFence(flags, len, data);
417 return;
418 }
419
420 if (len == 0) {
421 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000422 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000423 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000424
425 if (cp.supportsContinuousUpdates) {
426 vlog.info(_("Enabling continuous updates"));
427 continuousUpdates = true;
428 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
429 }
430 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000431 } else {
432 // Pixel format change
433 rdr::MemInStream memStream(data, len);
434 PixelFormat pf;
435
436 pf.read(&memStream);
437
438 desktop->setServerPF(pf);
439 cp.setPF(pf);
440 }
441}
442
DRC33c15e32011-11-03 18:49:21 +0000443rdr::U8* CConn::getRawPixelsRW(const rfb::Rect& r, int* stride) {
444 return desktop->getPixelsRW(r, stride);
445}
446void CConn::releaseRawPixels(const rfb::Rect& r) {
447 desktop->damageRect(r);
448}
449
450
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000451////////////////////// Internal methods //////////////////////
452
453void CConn::resizeFramebuffer()
454{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000455 if (!desktop)
456 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000457
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000458 if (continuousUpdates)
459 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
460
Pierre Ossman6455d852011-06-01 09:33:00 +0000461 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000462}
463
464// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
465// to the connection speed:
466//
467// First we wait for at least one second of bandwidth measurement.
468//
469// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
470// which should be perceptually lossless.
471//
472// If the bandwidth is below that, we choose a more lossy JPEG quality.
473//
474// If the bandwidth drops below 256 Kbps, we switch to palette mode.
475//
476// Note: The system here is fairly arbitrary and should be replaced
477// with something more intelligent at the server end.
478//
479void CConn::autoSelectFormatAndEncoding()
480{
481 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
482 unsigned int timeWaited = sock->inStream().timeWaited();
483 bool newFullColour = fullColour;
484 int newQualityLevel = qualityLevel;
485
486 // Always use Tight
487 if (currentEncoding != encodingTight) {
488 currentEncoding = encodingTight;
489 encodingChange = true;
490 }
491
492 // Check that we have a decent bandwidth measurement
493 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
494 return;
495
496 // Select appropriate quality level
497 if (!noJpeg) {
498 if (kbitsPerSecond > 16000)
499 newQualityLevel = 8;
500 else
501 newQualityLevel = 6;
502
503 if (newQualityLevel != qualityLevel) {
504 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
505 kbitsPerSecond, newQualityLevel);
506 cp.qualityLevel = newQualityLevel;
507 qualityLevel.setParam(newQualityLevel);
508 encodingChange = true;
509 }
510 }
511
512 if (cp.beforeVersion(3, 8)) {
513 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
514 // cursors "asynchronously". If this happens in the middle of a
515 // pixel format change, the server will encode the cursor with
516 // the old format, but the client will try to decode it
517 // according to the new format. This will lead to a
518 // crash. Therefore, we do not allow automatic format change for
519 // old servers.
520 return;
521 }
522
523 // Select best color level
524 newFullColour = (kbitsPerSecond > 256);
525 if (newFullColour != fullColour) {
526 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
527 kbitsPerSecond,
528 newFullColour ? _("enabled") : _("disabled"));
529 fullColour.setParam(newFullColour);
530 formatChange = true;
531 }
532}
533
534// checkEncodings() sends a setEncodings message if one is needed.
535void CConn::checkEncodings()
536{
537 if (encodingChange && writer()) {
538 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
539 writer()->writeSetEncodings(currentEncoding, true);
540 encodingChange = false;
541 }
542}
543
544// requestNewUpdate() requests an update from the server, having set the
545// format and encoding appropriately.
546void CConn::requestNewUpdate()
547{
548 if (formatChange) {
549 PixelFormat pf;
550
551 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000552 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000553
554 if (fullColour) {
555 pf = fullColourPF;
556 } else {
557 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000558 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000559 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000560 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000561 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000562 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000563 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000564
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000565 if (supportsSyncFence) {
566 // We let the fence carry the pixel format and switch once we
567 // get the response back. That way we will be synchronised with
568 // when the server switches.
569 rdr::MemOutStream memStream;
570
571 pf.write(&memStream);
572
573 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
574 memStream.length(), (const char*)memStream.data());
575 } else {
576 // New requests are sent out at the start of processing the last
577 // one, so we cannot switch our internal format right now (doing so
578 // would mean misdecoding the current update).
579 pendingPFChange = true;
580 pendingPF = pf;
581 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000582
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000583 char str[256];
584 pf.print(str, 256);
585 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000586 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000587
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000588 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000589 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000590
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000591 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000592
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000593 if (forceNonincremental || !continuousUpdates) {
594 pendingUpdate = true;
595 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
596 !forceNonincremental);
597 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000598
599 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000600}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000601
602void CConn::handleOptions(void *data)
603{
604 CConn *self = (CConn*)data;
605
606 // Checking all the details of the current set of encodings is just
607 // a pain. Assume something has changed, as resending the encoding
608 // list is cheap. Avoid overriding what the auto logic has selected
609 // though.
610 if (!autoSelect) {
611 int encNum = encodingNum(preferredEncoding);
612
613 if (encNum != -1)
614 self->currentEncoding = encNum;
615
616 self->cp.qualityLevel = qualityLevel;
617 }
618
Pierre Ossmanda389562011-06-09 08:24:37 +0000619 self->cp.supportsLocalCursor = true;
620
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000621 self->cp.customCompressLevel = customCompressLevel;
622 self->cp.compressLevel = compressLevel;
623
624 self->cp.noJpeg = noJpeg;
625
626 self->encodingChange = true;
627
628 // Format changes refreshes the entire screen though and are therefore
629 // very costly. It's probably worth the effort to see if it is necessary
630 // here.
631 PixelFormat pf;
632
633 if (fullColour) {
634 pf = self->fullColourPF;
635 } else {
636 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000637 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000638 else if (lowColourLevel == 1)
639 pf = lowColourPF;
640 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000641 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000642 }
643
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000644 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000645 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000646
647 // Without fences, we cannot safely trigger an update request directly
648 // but must wait for the next update to arrive.
649 if (self->supportsSyncFence)
650 self->requestNewUpdate();
651 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000652}