blob: cf963f0f48a9853459813af1e70cea5ccd0c22df [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
DRC3e7ed812013-02-26 10:34:22 +0000195void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000196{
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 Ossman50104882013-05-24 10:47:27 +0000353 if (!acceptClipboard)
354 return;
355
Pierre Ossman689c4582011-05-26 15:39:41 +0000356 size = fl_utf8froma(NULL, 0, str, len);
357 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000358 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000359
360 size++;
361
362 buffer = new char[size];
363
364 ret = fl_utf8froma(buffer, size, str, len);
365 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000366
Pierre Ossman5803d3b2013-09-05 14:25:40 +0000367 vlog.debug("Got clipboard data (%d bytes)", strlen(buffer));
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000368
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000369 // RFB doesn't have separate selection and clipboard concepts, so we
370 // dump the data into both variants.
371 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000372 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000373
374 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000375}
376
377// We start timing on beginRect and stop timing on endRect, to
378// avoid skewing the bandwidth estimation as a result of the server
379// being slow or the network having high latency
380void CConn::beginRect(const Rect& r, int encoding)
381{
382 sock->inStream().startTiming();
383 if (encoding != encodingCopyRect) {
384 lastServerEncoding = encoding;
385 }
386}
387
388void CConn::endRect(const Rect& r, int encoding)
389{
390 sock->inStream().stopTiming();
391}
392
393void CConn::fillRect(const rfb::Rect& r, rfb::Pixel p)
394{
395 desktop->fillRect(r,p);
396}
Pierre Ossman0dff4b82014-01-27 16:17:21 +0100397
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000398void CConn::imageRect(const rfb::Rect& r, void* p)
399{
400 desktop->imageRect(r,p);
401}
Pierre Ossman0dff4b82014-01-27 16:17:21 +0100402
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000403void CConn::copyRect(const rfb::Rect& r, int sx, int sy)
404{
405 desktop->copyRect(r,sx,sy);
406}
Pierre Ossman0dff4b82014-01-27 16:17:21 +0100407
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000408void CConn::setCursor(int width, int height, const Point& hotspot,
409 void* data, void* mask)
410{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000411 desktop->setCursor(width, height, hotspot, data, mask);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000412}
413
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000414void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
415{
416 CMsgHandler::fence(flags, len, data);
417
418 if (flags & fenceFlagRequest) {
419 // We handle everything synchronously so we trivially honor these modes
420 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
421
422 writer()->writeFence(flags, len, data);
423 return;
424 }
425
426 if (len == 0) {
427 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000428 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000429 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000430
431 if (cp.supportsContinuousUpdates) {
432 vlog.info(_("Enabling continuous updates"));
433 continuousUpdates = true;
434 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
435 }
436 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000437 } else {
438 // Pixel format change
439 rdr::MemInStream memStream(data, len);
440 PixelFormat pf;
441
442 pf.read(&memStream);
443
444 desktop->setServerPF(pf);
445 cp.setPF(pf);
446 }
447}
448
Pierre Ossman945cdda2014-01-28 14:13:12 +0100449rdr::U8* CConn::getRawBufferRW(const rfb::Rect& r, int* stride) {
450 return desktop->getBufferRW(r, stride);
DRC33c15e32011-11-03 18:49:21 +0000451}
Pierre Ossman945cdda2014-01-28 14:13:12 +0100452void CConn::releaseRawBuffer(const rfb::Rect& r) {
DRC33c15e32011-11-03 18:49:21 +0000453 desktop->damageRect(r);
454}
455
456
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000457////////////////////// Internal methods //////////////////////
458
459void CConn::resizeFramebuffer()
460{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000461 if (!desktop)
462 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000463
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000464 if (continuousUpdates)
465 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
466
Pierre Ossman6455d852011-06-01 09:33:00 +0000467 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000468}
469
470// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
471// to the connection speed:
472//
473// First we wait for at least one second of bandwidth measurement.
474//
475// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
476// which should be perceptually lossless.
477//
478// If the bandwidth is below that, we choose a more lossy JPEG quality.
479//
480// If the bandwidth drops below 256 Kbps, we switch to palette mode.
481//
482// Note: The system here is fairly arbitrary and should be replaced
483// with something more intelligent at the server end.
484//
485void CConn::autoSelectFormatAndEncoding()
486{
487 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
488 unsigned int timeWaited = sock->inStream().timeWaited();
489 bool newFullColour = fullColour;
490 int newQualityLevel = qualityLevel;
491
492 // Always use Tight
493 if (currentEncoding != encodingTight) {
494 currentEncoding = encodingTight;
495 encodingChange = true;
496 }
497
498 // Check that we have a decent bandwidth measurement
499 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
500 return;
501
502 // Select appropriate quality level
503 if (!noJpeg) {
504 if (kbitsPerSecond > 16000)
505 newQualityLevel = 8;
506 else
507 newQualityLevel = 6;
508
509 if (newQualityLevel != qualityLevel) {
510 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
511 kbitsPerSecond, newQualityLevel);
512 cp.qualityLevel = newQualityLevel;
513 qualityLevel.setParam(newQualityLevel);
514 encodingChange = true;
515 }
516 }
517
518 if (cp.beforeVersion(3, 8)) {
519 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
520 // cursors "asynchronously". If this happens in the middle of a
521 // pixel format change, the server will encode the cursor with
522 // the old format, but the client will try to decode it
523 // according to the new format. This will lead to a
524 // crash. Therefore, we do not allow automatic format change for
525 // old servers.
526 return;
527 }
528
529 // Select best color level
530 newFullColour = (kbitsPerSecond > 256);
531 if (newFullColour != fullColour) {
532 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
533 kbitsPerSecond,
534 newFullColour ? _("enabled") : _("disabled"));
535 fullColour.setParam(newFullColour);
536 formatChange = true;
537 }
538}
539
540// checkEncodings() sends a setEncodings message if one is needed.
541void CConn::checkEncodings()
542{
543 if (encodingChange && writer()) {
544 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
545 writer()->writeSetEncodings(currentEncoding, true);
546 encodingChange = false;
547 }
548}
549
550// requestNewUpdate() requests an update from the server, having set the
551// format and encoding appropriately.
552void CConn::requestNewUpdate()
553{
554 if (formatChange) {
555 PixelFormat pf;
556
557 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000558 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000559
560 if (fullColour) {
561 pf = fullColourPF;
562 } else {
563 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000564 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000565 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000566 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000567 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000568 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000569 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000570
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000571 if (supportsSyncFence) {
572 // We let the fence carry the pixel format and switch once we
573 // get the response back. That way we will be synchronised with
574 // when the server switches.
575 rdr::MemOutStream memStream;
576
577 pf.write(&memStream);
578
579 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
580 memStream.length(), (const char*)memStream.data());
581 } else {
582 // New requests are sent out at the start of processing the last
583 // one, so we cannot switch our internal format right now (doing so
584 // would mean misdecoding the current update).
585 pendingPFChange = true;
586 pendingPF = pf;
587 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000588
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000589 char str[256];
590 pf.print(str, 256);
591 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000592 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000593
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000594 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000595 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000596
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000597 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000598
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000599 if (forceNonincremental || !continuousUpdates) {
600 pendingUpdate = true;
601 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
602 !forceNonincremental);
603 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000604
605 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000606}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000607
608void CConn::handleOptions(void *data)
609{
610 CConn *self = (CConn*)data;
611
612 // Checking all the details of the current set of encodings is just
613 // a pain. Assume something has changed, as resending the encoding
614 // list is cheap. Avoid overriding what the auto logic has selected
615 // though.
616 if (!autoSelect) {
617 int encNum = encodingNum(preferredEncoding);
618
619 if (encNum != -1)
620 self->currentEncoding = encNum;
621
622 self->cp.qualityLevel = qualityLevel;
623 }
624
Pierre Ossmanda389562011-06-09 08:24:37 +0000625 self->cp.supportsLocalCursor = true;
626
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000627 self->cp.customCompressLevel = customCompressLevel;
628 self->cp.compressLevel = compressLevel;
629
630 self->cp.noJpeg = noJpeg;
631
632 self->encodingChange = true;
633
634 // Format changes refreshes the entire screen though and are therefore
635 // very costly. It's probably worth the effort to see if it is necessary
636 // here.
637 PixelFormat pf;
638
639 if (fullColour) {
640 pf = self->fullColourPF;
641 } else {
642 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000643 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000644 else if (lowColourLevel == 1)
645 pf = lowColourPF;
646 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000647 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000648 }
649
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000650 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000651 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000652
653 // Without fences, we cannot safely trigger an update request directly
654 // but must wait for the next update to arrive.
655 if (self->supportsSyncFence)
656 self->requestNewUpdate();
657 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000658}