blob: c5cd4a45467f4daf73ffac68d96fd17d75199016 [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);
68// 256 colours (palette)
69static const PixelFormat mediumColourPF(8, 8, false, false);
Pierre Ossmanf4f30942011-05-17 09:39:07 +000070
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000071CConn::CConn(const char* vncServerName, network::Socket* socket=NULL)
72 : serverHost(0), serverPort(0), desktop(NULL),
Pierre Ossman5d512c32011-11-04 11:42:16 +000073 pendingPFChange(false),
Pierre Ossman5156d5e2011-03-09 09:42:34 +000074 currentEncoding(encodingTight), lastServerEncoding((unsigned int)-1),
75 formatChange(false), encodingChange(false),
Pierre Ossmanaa73c892011-11-15 12:13:37 +000076 firstUpdate(true), pendingUpdate(false), continuousUpdates(false),
Pierre Ossmane28bdb22011-11-14 16:02:06 +000077 forceNonincremental(true), supportsSyncFence(false)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000078{
79 setShared(::shared);
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000080 sock = socket;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000081
82 int encNum = encodingNum(preferredEncoding);
83 if (encNum != -1)
84 currentEncoding = encNum;
85
Pierre Ossmanda389562011-06-09 08:24:37 +000086 cp.supportsLocalCursor = true;
87
Pierre Ossman5156d5e2011-03-09 09:42:34 +000088 cp.supportsDesktopResize = true;
89 cp.supportsExtendedDesktopSize = true;
90 cp.supportsDesktopRename = true;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000091
92 cp.customCompressLevel = customCompressLevel;
93 cp.compressLevel = compressLevel;
94
95 cp.noJpeg = noJpeg;
96 cp.qualityLevel = qualityLevel;
97
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000098 if(sock == NULL) {
99 try {
100 getHostAndPort(vncServerName, &serverHost, &serverPort);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000101
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000102 sock = new network::TcpSocket(serverHost, serverPort);
103 vlog.info(_("connected to host %s port %d"), serverHost, serverPort);
104 } catch (rdr::Exception& e) {
105 vlog.error("%s", e.str());
106 fl_alert("%s", e.str());
107 exit_vncviewer();
108 return;
109 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000110 }
111
112 Fl::add_fd(sock->getFd(), FL_READ | FL_EXCEPT, socketEvent, this);
113
114 // See callback below
115 sock->inStream().setBlockCallback(this);
116
117 setServerName(serverHost);
118 setStreams(&sock->inStream(), &sock->outStream());
119
120 initialiseProtocol();
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000121
122 OptionsDialog::addCallback(handleOptions, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000123}
124
125CConn::~CConn()
126{
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000127 OptionsDialog::removeCallback(handleOptions);
128
Pierre Ossman6a9e2e62011-05-19 14:47:43 +0000129 if (desktop)
130 delete desktop;
131
Pierre Ossman5e04c262011-11-09 11:31:12 +0000132 delete [] serverHost;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000133 if (sock)
134 Fl::remove_fd(sock->getFd());
135 delete sock;
136}
137
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000138void CConn::refreshFramebuffer()
139{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000140 forceNonincremental = true;
141
142 // Without fences, we cannot safely trigger an update request directly
143 // but must wait for the next update to arrive.
144 if (supportsSyncFence)
145 requestNewUpdate();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000146}
147
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000148const char *CConn::connectionInfo()
149{
150 static char infoText[1024] = "";
151
152 char pfStr[100];
153 char spfStr[100];
154
155 cp.pf().print(pfStr, 100);
156 serverPF.print(spfStr, 100);
157
158 int secType = csecurity->getType();
159
160 snprintf(infoText, sizeof(infoText),
161 _("Desktop name: %.80s\n"
162 "Host: %.80s port: %d\n"
163 "Size: %d x %d\n"
164 "Pixel format: %s\n"
165 "(server default %s)\n"
166 "Requested encoding: %s\n"
167 "Last used encoding: %s\n"
168 "Line speed estimate: %d kbit/s\n"
169 "Protocol version: %d.%d\n"
170 "Security method: %s\n"),
171 cp.name(), serverHost, serverPort, cp.width, cp.height,
172 pfStr, spfStr, encodingName(currentEncoding),
173 encodingName(lastServerEncoding),
174 sock->inStream().kbitsPerSecond(),
175 cp.majorVersion, cp.minorVersion,
176 secTypeName(secType));
177
178 return infoText;
179}
180
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000181// The RFB core is not properly asynchronous, so it calls this callback
182// whenever it needs to block to wait for more data. Since FLTK is
183// monitoring the socket, we just make sure FLTK gets to run.
184
185void CConn::blockCallback()
186{
187 int next_timer;
188
189 next_timer = Timer::checkTimeouts();
190 if (next_timer == 0)
191 next_timer = INT_MAX;
192
193 Fl::wait((double)next_timer / 1000.0);
194}
195
DRC3e7ed812013-02-26 10:34:22 +0000196void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000197{
198 CConn *cc;
199 static bool recursing = false;
200
201 assert(data);
202 cc = (CConn*)data;
203
204 // I don't think processMsg() is recursion safe, so add this check
205 if (recursing)
206 return;
207
208 recursing = true;
209
210 try {
211 // processMsg() only processes one message, so we need to loop
212 // until the buffers are empty or things will stall.
213 do {
214 cc->processMsg();
215 } while (cc->sock->inStream().checkNoWait(1));
216 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000217 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000218 exit_vncviewer();
219 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000220 vlog.error("%s", e.str());
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000221 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000222 }
223
224 recursing = false;
225}
226
227////////////////////// CConnection callback methods //////////////////////
228
229// serverInit() is called when the serverInit message has been received. At
230// this point we create the desktop window and display it. We also tell the
231// server the pixel format and encodings to use and request the first update.
232void CConn::serverInit()
233{
234 CConnection::serverInit();
235
236 // If using AutoSelect with old servers, start in FullColor
237 // mode. See comment in autoSelectFormatAndEncoding.
238 if (cp.beforeVersion(3, 8) && autoSelect)
239 fullColour.setParam(true);
240
241 serverPF = cp.pf();
242
243 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
244 fullColourPF = desktop->getPreferredPF();
245
Pierre Ossman5d512c32011-11-04 11:42:16 +0000246 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000247 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000248
249 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000250 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000251
252 // This initial update request is a bit of a corner case, so we need
253 // to help out setting the correct format here.
254 assert(pendingPFChange);
255 desktop->setServerPF(pendingPF);
256 cp.setPF(pendingPF);
257 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000258}
259
260// setDesktopSize() is called when the desktop size changes (including when
261// it is set initially).
262void CConn::setDesktopSize(int w, int h)
263{
264 CConnection::setDesktopSize(w,h);
265 resizeFramebuffer();
266}
267
268// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
269void CConn::setExtendedDesktopSize(int reason, int result, int w, int h,
270 const rfb::ScreenSet& layout)
271{
272 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
273
274 if ((reason == reasonClient) && (result != resultSuccess)) {
275 vlog.error(_("SetDesktopSize failed: %d"), result);
276 return;
277 }
278
279 resizeFramebuffer();
280}
281
282// setName() is called when the desktop name changes
283void CConn::setName(const char* name)
284{
285 CConnection::setName(name);
286 if (desktop)
287 desktop->setName(name);
288}
289
290// framebufferUpdateStart() is called at the beginning of an update.
291// Here we try to send out a new framebuffer update request so that the
292// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000293// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000294void CConn::framebufferUpdateStart()
295{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000296 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000297 pendingUpdate = false;
298
299 requestNewUpdate();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000300}
301
302// framebufferUpdateEnd() is called at the end of an update.
303// For each rectangle, the FdInStream will have timed the speed
304// of the connection, allowing us to select format and encoding
305// appropriately, and then request another incremental update.
306void CConn::framebufferUpdateEnd()
307{
308 desktop->updateWindow();
309
310 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000311 // We need fences to make extra update requests and continuous
312 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000313 if (cp.supportsFence)
314 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
315
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000316 firstUpdate = false;
317 }
318
Pierre Ossman5d512c32011-11-04 11:42:16 +0000319 // A format change has been scheduled and we are now past the update
320 // with the old format. Time to active the new one.
321 if (pendingPFChange) {
322 desktop->setServerPF(pendingPF);
323 cp.setPF(pendingPF);
324 pendingPFChange = false;
325 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000326
327 // Compute new settings based on updated bandwidth values
328 if (autoSelect)
329 autoSelectFormatAndEncoding();
330
331 // Make sure that the FLTK handling and the timers gets some CPU time
332 // in case of back to back framebuffer updates.
333 Fl::check();
334 Timer::checkTimeouts();
335}
336
337// The rest of the callbacks are fairly self-explanatory...
338
339void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
340{
341 desktop->setColourMapEntries(firstColour, nColours, rgbs);
342}
343
344void CConn::bell()
345{
346 fl_beep();
347}
348
349void CConn::serverCutText(const char* str, rdr::U32 len)
350{
Pierre Ossman689c4582011-05-26 15:39:41 +0000351 char *buffer;
352 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000353
Pierre Ossman50104882013-05-24 10:47:27 +0000354 if (!acceptClipboard)
355 return;
356
Pierre Ossman689c4582011-05-26 15:39:41 +0000357 size = fl_utf8froma(NULL, 0, str, len);
358 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000359 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000360
361 size++;
362
363 buffer = new char[size];
364
365 ret = fl_utf8froma(buffer, size, str, len);
366 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000367
Pierre Ossman5803d3b2013-09-05 14:25:40 +0000368 vlog.debug("Got clipboard data (%d bytes)", strlen(buffer));
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000369
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000370 // RFB doesn't have separate selection and clipboard concepts, so we
371 // dump the data into both variants.
372 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000373 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000374
375 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000376}
377
378// We start timing on beginRect and stop timing on endRect, to
379// avoid skewing the bandwidth estimation as a result of the server
380// being slow or the network having high latency
381void CConn::beginRect(const Rect& r, int encoding)
382{
383 sock->inStream().startTiming();
384 if (encoding != encodingCopyRect) {
385 lastServerEncoding = encoding;
386 }
387}
388
389void CConn::endRect(const Rect& r, int encoding)
390{
391 sock->inStream().stopTiming();
392}
393
394void CConn::fillRect(const rfb::Rect& r, rfb::Pixel p)
395{
396 desktop->fillRect(r,p);
397}
Pierre Ossman0dff4b82014-01-27 16:17:21 +0100398
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000399void CConn::imageRect(const rfb::Rect& r, void* p)
400{
401 desktop->imageRect(r,p);
402}
Pierre Ossman0dff4b82014-01-27 16:17:21 +0100403
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000404void CConn::copyRect(const rfb::Rect& r, int sx, int sy)
405{
406 desktop->copyRect(r,sx,sy);
407}
Pierre Ossman0dff4b82014-01-27 16:17:21 +0100408
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000409void CConn::setCursor(int width, int height, const Point& hotspot,
410 void* data, void* mask)
411{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000412 desktop->setCursor(width, height, hotspot, data, mask);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000413}
414
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000415void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
416{
417 CMsgHandler::fence(flags, len, data);
418
419 if (flags & fenceFlagRequest) {
420 // We handle everything synchronously so we trivially honor these modes
421 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
422
423 writer()->writeFence(flags, len, data);
424 return;
425 }
426
427 if (len == 0) {
428 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000429 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000430 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000431
432 if (cp.supportsContinuousUpdates) {
433 vlog.info(_("Enabling continuous updates"));
434 continuousUpdates = true;
435 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
436 }
437 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000438 } else {
439 // Pixel format change
440 rdr::MemInStream memStream(data, len);
441 PixelFormat pf;
442
443 pf.read(&memStream);
444
445 desktop->setServerPF(pf);
446 cp.setPF(pf);
447 }
448}
449
Pierre Ossman945cdda2014-01-28 14:13:12 +0100450rdr::U8* CConn::getRawBufferRW(const rfb::Rect& r, int* stride) {
451 return desktop->getBufferRW(r, stride);
DRC33c15e32011-11-03 18:49:21 +0000452}
Pierre Ossman945cdda2014-01-28 14:13:12 +0100453void CConn::releaseRawBuffer(const rfb::Rect& r) {
DRC33c15e32011-11-03 18:49:21 +0000454 desktop->damageRect(r);
455}
456
457
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000458////////////////////// Internal methods //////////////////////
459
460void CConn::resizeFramebuffer()
461{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000462 if (!desktop)
463 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000464
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000465 if (continuousUpdates)
466 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
467
Pierre Ossman6455d852011-06-01 09:33:00 +0000468 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000469}
470
471// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
472// to the connection speed:
473//
474// First we wait for at least one second of bandwidth measurement.
475//
476// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
477// which should be perceptually lossless.
478//
479// If the bandwidth is below that, we choose a more lossy JPEG quality.
480//
481// If the bandwidth drops below 256 Kbps, we switch to palette mode.
482//
483// Note: The system here is fairly arbitrary and should be replaced
484// with something more intelligent at the server end.
485//
486void CConn::autoSelectFormatAndEncoding()
487{
488 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
489 unsigned int timeWaited = sock->inStream().timeWaited();
490 bool newFullColour = fullColour;
491 int newQualityLevel = qualityLevel;
492
493 // Always use Tight
494 if (currentEncoding != encodingTight) {
495 currentEncoding = encodingTight;
496 encodingChange = true;
497 }
498
499 // Check that we have a decent bandwidth measurement
500 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
501 return;
502
503 // Select appropriate quality level
504 if (!noJpeg) {
505 if (kbitsPerSecond > 16000)
506 newQualityLevel = 8;
507 else
508 newQualityLevel = 6;
509
510 if (newQualityLevel != qualityLevel) {
511 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
512 kbitsPerSecond, newQualityLevel);
513 cp.qualityLevel = newQualityLevel;
514 qualityLevel.setParam(newQualityLevel);
515 encodingChange = true;
516 }
517 }
518
519 if (cp.beforeVersion(3, 8)) {
520 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
521 // cursors "asynchronously". If this happens in the middle of a
522 // pixel format change, the server will encode the cursor with
523 // the old format, but the client will try to decode it
524 // according to the new format. This will lead to a
525 // crash. Therefore, we do not allow automatic format change for
526 // old servers.
527 return;
528 }
529
530 // Select best color level
531 newFullColour = (kbitsPerSecond > 256);
532 if (newFullColour != fullColour) {
533 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
534 kbitsPerSecond,
535 newFullColour ? _("enabled") : _("disabled"));
536 fullColour.setParam(newFullColour);
537 formatChange = true;
538 }
539}
540
541// checkEncodings() sends a setEncodings message if one is needed.
542void CConn::checkEncodings()
543{
544 if (encodingChange && writer()) {
545 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
546 writer()->writeSetEncodings(currentEncoding, true);
547 encodingChange = false;
548 }
549}
550
551// requestNewUpdate() requests an update from the server, having set the
552// format and encoding appropriately.
553void CConn::requestNewUpdate()
554{
555 if (formatChange) {
556 PixelFormat pf;
557
558 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000559 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000560
561 if (fullColour) {
562 pf = fullColourPF;
563 } else {
564 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000565 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000566 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000567 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000568 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000569 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000570 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000571
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000572 if (supportsSyncFence) {
573 // We let the fence carry the pixel format and switch once we
574 // get the response back. That way we will be synchronised with
575 // when the server switches.
576 rdr::MemOutStream memStream;
577
578 pf.write(&memStream);
579
580 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
581 memStream.length(), (const char*)memStream.data());
582 } else {
583 // New requests are sent out at the start of processing the last
584 // one, so we cannot switch our internal format right now (doing so
585 // would mean misdecoding the current update).
586 pendingPFChange = true;
587 pendingPF = pf;
588 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000589
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000590 char str[256];
591 pf.print(str, 256);
592 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000593 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000594
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000595 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000596 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000597
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000598 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000599
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000600 if (forceNonincremental || !continuousUpdates) {
601 pendingUpdate = true;
602 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
603 !forceNonincremental);
604 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000605
606 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000607}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000608
609void CConn::handleOptions(void *data)
610{
611 CConn *self = (CConn*)data;
612
613 // Checking all the details of the current set of encodings is just
614 // a pain. Assume something has changed, as resending the encoding
615 // list is cheap. Avoid overriding what the auto logic has selected
616 // though.
617 if (!autoSelect) {
618 int encNum = encodingNum(preferredEncoding);
619
620 if (encNum != -1)
621 self->currentEncoding = encNum;
622
623 self->cp.qualityLevel = qualityLevel;
624 }
625
Pierre Ossmanda389562011-06-09 08:24:37 +0000626 self->cp.supportsLocalCursor = true;
627
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000628 self->cp.customCompressLevel = customCompressLevel;
629 self->cp.compressLevel = compressLevel;
630
631 self->cp.noJpeg = noJpeg;
632
633 self->encodingChange = true;
634
635 // Format changes refreshes the entire screen though and are therefore
636 // very costly. It's probably worth the effort to see if it is necessary
637 // here.
638 PixelFormat pf;
639
640 if (fullColour) {
641 pf = self->fullColourPF;
642 } else {
643 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000644 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000645 else if (lowColourLevel == 1)
646 pf = lowColourPF;
647 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000648 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000649 }
650
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000651 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000652 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000653
654 // Without fences, we cannot safely trigger an update request directly
655 // but must wait for the next update to arrive.
656 if (self->supportsSyncFence)
657 self->requestNewUpdate();
658 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000659}