blob: 789cf09e037c0105b76115885a9a8d582c43d0c5 [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
Pierre Ossmana22459d2014-03-17 14:42:10 +010092 if (customCompressLevel)
93 cp.compressLevel = compressLevel;
94 else
95 cp.compressLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000096
Pierre Ossmana22459d2014-03-17 14:42:10 +010097 if (!noJpeg)
98 cp.qualityLevel = qualityLevel;
99 else
100 cp.qualityLevel = -1;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000101
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000102 if(sock == NULL) {
103 try {
104 getHostAndPort(vncServerName, &serverHost, &serverPort);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000105
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000106 sock = new network::TcpSocket(serverHost, serverPort);
107 vlog.info(_("connected to host %s port %d"), serverHost, serverPort);
108 } catch (rdr::Exception& e) {
109 vlog.error("%s", e.str());
110 fl_alert("%s", e.str());
111 exit_vncviewer();
112 return;
113 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000114 }
115
116 Fl::add_fd(sock->getFd(), FL_READ | FL_EXCEPT, socketEvent, this);
117
118 // See callback below
119 sock->inStream().setBlockCallback(this);
120
121 setServerName(serverHost);
122 setStreams(&sock->inStream(), &sock->outStream());
123
124 initialiseProtocol();
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000125
126 OptionsDialog::addCallback(handleOptions, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000127}
128
129CConn::~CConn()
130{
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000131 OptionsDialog::removeCallback(handleOptions);
132
Pierre Ossman6a9e2e62011-05-19 14:47:43 +0000133 if (desktop)
134 delete desktop;
135
Pierre Ossman5e04c262011-11-09 11:31:12 +0000136 delete [] serverHost;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000137 if (sock)
138 Fl::remove_fd(sock->getFd());
139 delete sock;
140}
141
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000142void CConn::refreshFramebuffer()
143{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000144 forceNonincremental = true;
145
146 // Without fences, we cannot safely trigger an update request directly
147 // but must wait for the next update to arrive.
148 if (supportsSyncFence)
149 requestNewUpdate();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000150}
151
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000152const char *CConn::connectionInfo()
153{
154 static char infoText[1024] = "";
155
156 char pfStr[100];
157 char spfStr[100];
158
159 cp.pf().print(pfStr, 100);
160 serverPF.print(spfStr, 100);
161
162 int secType = csecurity->getType();
163
164 snprintf(infoText, sizeof(infoText),
165 _("Desktop name: %.80s\n"
166 "Host: %.80s port: %d\n"
167 "Size: %d x %d\n"
168 "Pixel format: %s\n"
169 "(server default %s)\n"
170 "Requested encoding: %s\n"
171 "Last used encoding: %s\n"
172 "Line speed estimate: %d kbit/s\n"
173 "Protocol version: %d.%d\n"
174 "Security method: %s\n"),
175 cp.name(), serverHost, serverPort, cp.width, cp.height,
176 pfStr, spfStr, encodingName(currentEncoding),
177 encodingName(lastServerEncoding),
178 sock->inStream().kbitsPerSecond(),
179 cp.majorVersion, cp.minorVersion,
180 secTypeName(secType));
181
182 return infoText;
183}
184
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000185// The RFB core is not properly asynchronous, so it calls this callback
186// whenever it needs to block to wait for more data. Since FLTK is
187// monitoring the socket, we just make sure FLTK gets to run.
188
189void CConn::blockCallback()
190{
191 int next_timer;
192
193 next_timer = Timer::checkTimeouts();
194 if (next_timer == 0)
195 next_timer = INT_MAX;
196
197 Fl::wait((double)next_timer / 1000.0);
198}
199
DRC3e7ed812013-02-26 10:34:22 +0000200void CConn::socketEvent(FL_SOCKET fd, void *data)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000201{
202 CConn *cc;
203 static bool recursing = false;
204
205 assert(data);
206 cc = (CConn*)data;
207
208 // I don't think processMsg() is recursion safe, so add this check
209 if (recursing)
210 return;
211
212 recursing = true;
213
214 try {
215 // processMsg() only processes one message, so we need to loop
216 // until the buffers are empty or things will stall.
217 do {
218 cc->processMsg();
219 } while (cc->sock->inStream().checkNoWait(1));
220 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000221 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000222 exit_vncviewer();
223 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000224 vlog.error("%s", e.str());
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000225 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000226 }
227
228 recursing = false;
229}
230
231////////////////////// CConnection callback methods //////////////////////
232
233// serverInit() is called when the serverInit message has been received. At
234// this point we create the desktop window and display it. We also tell the
235// server the pixel format and encodings to use and request the first update.
236void CConn::serverInit()
237{
238 CConnection::serverInit();
239
240 // If using AutoSelect with old servers, start in FullColor
241 // mode. See comment in autoSelectFormatAndEncoding.
242 if (cp.beforeVersion(3, 8) && autoSelect)
243 fullColour.setParam(true);
244
245 serverPF = cp.pf();
246
247 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
248 fullColourPF = desktop->getPreferredPF();
249
Pierre Ossman5d512c32011-11-04 11:42:16 +0000250 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000251 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000252
253 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000254 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000255
256 // This initial update request is a bit of a corner case, so we need
257 // to help out setting the correct format here.
258 assert(pendingPFChange);
259 desktop->setServerPF(pendingPF);
260 cp.setPF(pendingPF);
261 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000262}
263
264// setDesktopSize() is called when the desktop size changes (including when
265// it is set initially).
266void CConn::setDesktopSize(int w, int h)
267{
268 CConnection::setDesktopSize(w,h);
269 resizeFramebuffer();
270}
271
272// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
273void CConn::setExtendedDesktopSize(int reason, int result, int w, int h,
274 const rfb::ScreenSet& layout)
275{
276 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
277
278 if ((reason == reasonClient) && (result != resultSuccess)) {
279 vlog.error(_("SetDesktopSize failed: %d"), result);
280 return;
281 }
282
283 resizeFramebuffer();
284}
285
286// setName() is called when the desktop name changes
287void CConn::setName(const char* name)
288{
289 CConnection::setName(name);
290 if (desktop)
291 desktop->setName(name);
292}
293
294// framebufferUpdateStart() is called at the beginning of an update.
295// Here we try to send out a new framebuffer update request so that the
296// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000297// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000298void CConn::framebufferUpdateStart()
299{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000300 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000301 pendingUpdate = false;
302
303 requestNewUpdate();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000304}
305
306// framebufferUpdateEnd() is called at the end of an update.
307// For each rectangle, the FdInStream will have timed the speed
308// of the connection, allowing us to select format and encoding
309// appropriately, and then request another incremental update.
310void CConn::framebufferUpdateEnd()
311{
312 desktop->updateWindow();
313
314 if (firstUpdate) {
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000315 // We need fences to make extra update requests and continuous
316 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000317 if (cp.supportsFence)
318 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
319
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000320 firstUpdate = false;
321 }
322
Pierre Ossman5d512c32011-11-04 11:42:16 +0000323 // A format change has been scheduled and we are now past the update
324 // with the old format. Time to active the new one.
325 if (pendingPFChange) {
326 desktop->setServerPF(pendingPF);
327 cp.setPF(pendingPF);
328 pendingPFChange = false;
329 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000330
331 // Compute new settings based on updated bandwidth values
332 if (autoSelect)
333 autoSelectFormatAndEncoding();
334
335 // Make sure that the FLTK handling and the timers gets some CPU time
336 // in case of back to back framebuffer updates.
337 Fl::check();
338 Timer::checkTimeouts();
339}
340
341// The rest of the callbacks are fairly self-explanatory...
342
343void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
344{
345 desktop->setColourMapEntries(firstColour, nColours, rgbs);
346}
347
348void CConn::bell()
349{
350 fl_beep();
351}
352
353void CConn::serverCutText(const char* str, rdr::U32 len)
354{
Pierre Ossman689c4582011-05-26 15:39:41 +0000355 char *buffer;
356 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000357
Pierre Ossman50104882013-05-24 10:47:27 +0000358 if (!acceptClipboard)
359 return;
360
Pierre Ossman689c4582011-05-26 15:39:41 +0000361 size = fl_utf8froma(NULL, 0, str, len);
362 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000363 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000364
365 size++;
366
367 buffer = new char[size];
368
369 ret = fl_utf8froma(buffer, size, str, len);
370 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000371
Pierre Ossman5803d3b2013-09-05 14:25:40 +0000372 vlog.debug("Got clipboard data (%d bytes)", strlen(buffer));
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000373
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000374 // RFB doesn't have separate selection and clipboard concepts, so we
375 // dump the data into both variants.
376 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000377 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000378
379 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000380}
381
382// We start timing on beginRect and stop timing on endRect, to
383// avoid skewing the bandwidth estimation as a result of the server
384// being slow or the network having high latency
385void CConn::beginRect(const Rect& r, int encoding)
386{
387 sock->inStream().startTiming();
388 if (encoding != encodingCopyRect) {
389 lastServerEncoding = encoding;
390 }
391}
392
393void CConn::endRect(const Rect& r, int encoding)
394{
395 sock->inStream().stopTiming();
396}
397
398void CConn::fillRect(const rfb::Rect& r, rfb::Pixel p)
399{
400 desktop->fillRect(r,p);
401}
Pierre Ossman0dff4b82014-01-27 16:17:21 +0100402
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000403void CConn::imageRect(const rfb::Rect& r, void* p)
404{
405 desktop->imageRect(r,p);
406}
Pierre Ossman0dff4b82014-01-27 16:17:21 +0100407
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000408void CConn::copyRect(const rfb::Rect& r, int sx, int sy)
409{
410 desktop->copyRect(r,sx,sy);
411}
Pierre Ossman0dff4b82014-01-27 16:17:21 +0100412
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000413void CConn::setCursor(int width, int height, const Point& hotspot,
414 void* data, void* mask)
415{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000416 desktop->setCursor(width, height, hotspot, data, mask);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000417}
418
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000419void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
420{
421 CMsgHandler::fence(flags, len, data);
422
423 if (flags & fenceFlagRequest) {
424 // We handle everything synchronously so we trivially honor these modes
425 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
426
427 writer()->writeFence(flags, len, data);
428 return;
429 }
430
431 if (len == 0) {
432 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000433 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000434 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000435
436 if (cp.supportsContinuousUpdates) {
437 vlog.info(_("Enabling continuous updates"));
438 continuousUpdates = true;
439 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
440 }
441 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000442 } else {
443 // Pixel format change
444 rdr::MemInStream memStream(data, len);
445 PixelFormat pf;
446
447 pf.read(&memStream);
448
449 desktop->setServerPF(pf);
450 cp.setPF(pf);
451 }
452}
453
Pierre Ossman945cdda2014-01-28 14:13:12 +0100454rdr::U8* CConn::getRawBufferRW(const rfb::Rect& r, int* stride) {
455 return desktop->getBufferRW(r, stride);
DRC33c15e32011-11-03 18:49:21 +0000456}
Pierre Ossman945cdda2014-01-28 14:13:12 +0100457void CConn::releaseRawBuffer(const rfb::Rect& r) {
DRC33c15e32011-11-03 18:49:21 +0000458 desktop->damageRect(r);
459}
460
461
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000462////////////////////// Internal methods //////////////////////
463
464void CConn::resizeFramebuffer()
465{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000466 if (!desktop)
467 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000468
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000469 if (continuousUpdates)
470 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
471
Pierre Ossman6455d852011-06-01 09:33:00 +0000472 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000473}
474
475// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
476// to the connection speed:
477//
478// First we wait for at least one second of bandwidth measurement.
479//
480// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
481// which should be perceptually lossless.
482//
483// If the bandwidth is below that, we choose a more lossy JPEG quality.
484//
485// If the bandwidth drops below 256 Kbps, we switch to palette mode.
486//
487// Note: The system here is fairly arbitrary and should be replaced
488// with something more intelligent at the server end.
489//
490void CConn::autoSelectFormatAndEncoding()
491{
492 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
493 unsigned int timeWaited = sock->inStream().timeWaited();
494 bool newFullColour = fullColour;
495 int newQualityLevel = qualityLevel;
496
497 // Always use Tight
498 if (currentEncoding != encodingTight) {
499 currentEncoding = encodingTight;
500 encodingChange = true;
501 }
502
503 // Check that we have a decent bandwidth measurement
504 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
505 return;
506
507 // Select appropriate quality level
508 if (!noJpeg) {
509 if (kbitsPerSecond > 16000)
510 newQualityLevel = 8;
511 else
512 newQualityLevel = 6;
513
514 if (newQualityLevel != qualityLevel) {
515 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
516 kbitsPerSecond, newQualityLevel);
517 cp.qualityLevel = newQualityLevel;
518 qualityLevel.setParam(newQualityLevel);
519 encodingChange = true;
520 }
521 }
522
523 if (cp.beforeVersion(3, 8)) {
524 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
525 // cursors "asynchronously". If this happens in the middle of a
526 // pixel format change, the server will encode the cursor with
527 // the old format, but the client will try to decode it
528 // according to the new format. This will lead to a
529 // crash. Therefore, we do not allow automatic format change for
530 // old servers.
531 return;
532 }
533
534 // Select best color level
535 newFullColour = (kbitsPerSecond > 256);
536 if (newFullColour != fullColour) {
537 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
538 kbitsPerSecond,
539 newFullColour ? _("enabled") : _("disabled"));
540 fullColour.setParam(newFullColour);
541 formatChange = true;
542 }
543}
544
545// checkEncodings() sends a setEncodings message if one is needed.
546void CConn::checkEncodings()
547{
548 if (encodingChange && writer()) {
549 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
550 writer()->writeSetEncodings(currentEncoding, true);
551 encodingChange = false;
552 }
553}
554
555// requestNewUpdate() requests an update from the server, having set the
556// format and encoding appropriately.
557void CConn::requestNewUpdate()
558{
559 if (formatChange) {
560 PixelFormat pf;
561
562 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000563 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000564
565 if (fullColour) {
566 pf = fullColourPF;
567 } else {
568 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000569 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000570 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000571 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000572 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000573 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000574 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000575
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000576 if (supportsSyncFence) {
577 // We let the fence carry the pixel format and switch once we
578 // get the response back. That way we will be synchronised with
579 // when the server switches.
580 rdr::MemOutStream memStream;
581
582 pf.write(&memStream);
583
584 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
585 memStream.length(), (const char*)memStream.data());
586 } else {
587 // New requests are sent out at the start of processing the last
588 // one, so we cannot switch our internal format right now (doing so
589 // would mean misdecoding the current update).
590 pendingPFChange = true;
591 pendingPF = pf;
592 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000593
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000594 char str[256];
595 pf.print(str, 256);
596 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000597 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000598
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000599 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000600 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000601
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000602 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000603
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000604 if (forceNonincremental || !continuousUpdates) {
605 pendingUpdate = true;
606 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
607 !forceNonincremental);
608 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000609
610 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000611}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000612
613void CConn::handleOptions(void *data)
614{
615 CConn *self = (CConn*)data;
616
617 // Checking all the details of the current set of encodings is just
618 // a pain. Assume something has changed, as resending the encoding
619 // list is cheap. Avoid overriding what the auto logic has selected
620 // though.
621 if (!autoSelect) {
622 int encNum = encodingNum(preferredEncoding);
623
624 if (encNum != -1)
625 self->currentEncoding = encNum;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000626 }
627
Pierre Ossmanda389562011-06-09 08:24:37 +0000628 self->cp.supportsLocalCursor = true;
629
Pierre Ossmana22459d2014-03-17 14:42:10 +0100630 if (customCompressLevel)
631 self->cp.compressLevel = compressLevel;
632 else
633 self->cp.compressLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000634
Pierre Ossmana22459d2014-03-17 14:42:10 +0100635 if (!noJpeg && !autoSelect)
636 self->cp.qualityLevel = qualityLevel;
637 else
638 self->cp.qualityLevel = -1;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000639
640 self->encodingChange = true;
641
642 // Format changes refreshes the entire screen though and are therefore
643 // very costly. It's probably worth the effort to see if it is necessary
644 // here.
645 PixelFormat pf;
646
647 if (fullColour) {
648 pf = self->fullColourPF;
649 } else {
650 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000651 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000652 else if (lowColourLevel == 1)
653 pf = lowColourPF;
654 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000655 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000656 }
657
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000658 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000659 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000660
661 // Without fences, we cannot safely trigger an update request directly
662 // but must wait for the next update to arrive.
663 if (self->supportsSyncFence)
664 self->requestNewUpdate();
665 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000666}