blob: 405cd979fe3aacdf3d22a934b39a449e3dab555c [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 Ossman5156d5e2011-03-09 09:42:34 +000070CConn::CConn(const char* vncServerName)
71 : serverHost(0), serverPort(0), sock(NULL), 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);
79
80 int encNum = encodingNum(preferredEncoding);
81 if (encNum != -1)
82 currentEncoding = encNum;
83
Pierre Ossmanda389562011-06-09 08:24:37 +000084 cp.supportsLocalCursor = true;
85
Pierre Ossman5156d5e2011-03-09 09:42:34 +000086 cp.supportsDesktopResize = true;
87 cp.supportsExtendedDesktopSize = true;
88 cp.supportsDesktopRename = true;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000089
90 cp.customCompressLevel = customCompressLevel;
91 cp.compressLevel = compressLevel;
92
93 cp.noJpeg = noJpeg;
94 cp.qualityLevel = qualityLevel;
95
96 try {
97 getHostAndPort(vncServerName, &serverHost, &serverPort);
98
99 sock = new network::TcpSocket(serverHost, serverPort);
100 vlog.info(_("connected to host %s port %d"), serverHost, serverPort);
101 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000102 vlog.error("%s", e.str());
Pierre Ossmanf52740e2012-04-25 15:43:56 +0000103 fl_alert("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000104 exit_vncviewer();
105 return;
106 }
107
108 Fl::add_fd(sock->getFd(), FL_READ | FL_EXCEPT, socketEvent, this);
109
110 // See callback below
111 sock->inStream().setBlockCallback(this);
112
113 setServerName(serverHost);
114 setStreams(&sock->inStream(), &sock->outStream());
115
116 initialiseProtocol();
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000117
118 OptionsDialog::addCallback(handleOptions, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000119}
120
121CConn::~CConn()
122{
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000123 OptionsDialog::removeCallback(handleOptions);
124
Pierre Ossman6a9e2e62011-05-19 14:47:43 +0000125 if (desktop)
126 delete desktop;
127
Pierre Ossman5e04c262011-11-09 11:31:12 +0000128 delete [] serverHost;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000129 if (sock)
130 Fl::remove_fd(sock->getFd());
131 delete sock;
132}
133
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000134void CConn::refreshFramebuffer()
135{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000136 forceNonincremental = true;
137
138 // Without fences, we cannot safely trigger an update request directly
139 // but must wait for the next update to arrive.
140 if (supportsSyncFence)
141 requestNewUpdate();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000142}
143
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000144const char *CConn::connectionInfo()
145{
146 static char infoText[1024] = "";
147
148 char pfStr[100];
149 char spfStr[100];
150
151 cp.pf().print(pfStr, 100);
152 serverPF.print(spfStr, 100);
153
154 int secType = csecurity->getType();
155
156 snprintf(infoText, sizeof(infoText),
157 _("Desktop name: %.80s\n"
158 "Host: %.80s port: %d\n"
159 "Size: %d x %d\n"
160 "Pixel format: %s\n"
161 "(server default %s)\n"
162 "Requested encoding: %s\n"
163 "Last used encoding: %s\n"
164 "Line speed estimate: %d kbit/s\n"
165 "Protocol version: %d.%d\n"
166 "Security method: %s\n"),
167 cp.name(), serverHost, serverPort, cp.width, cp.height,
168 pfStr, spfStr, encodingName(currentEncoding),
169 encodingName(lastServerEncoding),
170 sock->inStream().kbitsPerSecond(),
171 cp.majorVersion, cp.minorVersion,
172 secTypeName(secType));
173
174 return infoText;
175}
176
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000177// The RFB core is not properly asynchronous, so it calls this callback
178// whenever it needs to block to wait for more data. Since FLTK is
179// monitoring the socket, we just make sure FLTK gets to run.
180
181void CConn::blockCallback()
182{
183 int next_timer;
184
185 next_timer = Timer::checkTimeouts();
186 if (next_timer == 0)
187 next_timer = INT_MAX;
188
189 Fl::wait((double)next_timer / 1000.0);
190}
191
192void CConn::socketEvent(int fd, void *data)
193{
194 CConn *cc;
195 static bool recursing = false;
196
197 assert(data);
198 cc = (CConn*)data;
199
200 // I don't think processMsg() is recursion safe, so add this check
201 if (recursing)
202 return;
203
204 recursing = true;
205
206 try {
207 // processMsg() only processes one message, so we need to loop
208 // until the buffers are empty or things will stall.
209 do {
210 cc->processMsg();
211 } while (cc->sock->inStream().checkNoWait(1));
212 } catch (rdr::EndOfStream& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000213 vlog.info("%s", e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000214 exit_vncviewer();
215 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000216 vlog.error("%s", e.str());
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000217 exit_vncviewer(e.str());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000218 }
219
220 recursing = false;
221}
222
223////////////////////// CConnection callback methods //////////////////////
224
225// serverInit() is called when the serverInit message has been received. At
226// this point we create the desktop window and display it. We also tell the
227// server the pixel format and encodings to use and request the first update.
228void CConn::serverInit()
229{
230 CConnection::serverInit();
231
232 // If using AutoSelect with old servers, start in FullColor
233 // mode. See comment in autoSelectFormatAndEncoding.
234 if (cp.beforeVersion(3, 8) && autoSelect)
235 fullColour.setParam(true);
236
237 serverPF = cp.pf();
238
239 desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this);
240 fullColourPF = desktop->getPreferredPF();
241
Pierre Ossman5d512c32011-11-04 11:42:16 +0000242 // Force a switch to the format and encoding we'd like
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000243 formatChange = encodingChange = true;
Pierre Ossman5d512c32011-11-04 11:42:16 +0000244
245 // And kick off the update cycle
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000246 requestNewUpdate();
Pierre Ossman5d512c32011-11-04 11:42:16 +0000247
248 // This initial update request is a bit of a corner case, so we need
249 // to help out setting the correct format here.
250 assert(pendingPFChange);
251 desktop->setServerPF(pendingPF);
252 cp.setPF(pendingPF);
253 pendingPFChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000254}
255
256// setDesktopSize() is called when the desktop size changes (including when
257// it is set initially).
258void CConn::setDesktopSize(int w, int h)
259{
260 CConnection::setDesktopSize(w,h);
261 resizeFramebuffer();
262}
263
264// setExtendedDesktopSize() is a more advanced version of setDesktopSize()
265void CConn::setExtendedDesktopSize(int reason, int result, int w, int h,
266 const rfb::ScreenSet& layout)
267{
268 CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
269
270 if ((reason == reasonClient) && (result != resultSuccess)) {
271 vlog.error(_("SetDesktopSize failed: %d"), result);
272 return;
273 }
274
275 resizeFramebuffer();
276}
277
278// setName() is called when the desktop name changes
279void CConn::setName(const char* name)
280{
281 CConnection::setName(name);
282 if (desktop)
283 desktop->setName(name);
284}
285
286// framebufferUpdateStart() is called at the beginning of an update.
287// Here we try to send out a new framebuffer update request so that the
288// next update can be sent out in parallel with us decoding the current
Pierre Ossman5d512c32011-11-04 11:42:16 +0000289// one.
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000290void CConn::framebufferUpdateStart()
291{
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000292 // Note: This might not be true if sync fences are supported
Pierre Ossman5d512c32011-11-04 11:42:16 +0000293 pendingUpdate = false;
294
295 requestNewUpdate();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000296}
297
298// framebufferUpdateEnd() is called at the end of an update.
299// For each rectangle, the FdInStream will have timed the speed
300// of the connection, allowing us to select format and encoding
301// appropriately, and then request another incremental update.
302void CConn::framebufferUpdateEnd()
303{
304 desktop->updateWindow();
305
306 if (firstUpdate) {
307 int width, height;
308
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000309 // We need fences to make extra update requests and continuous
310 // updates "safe". See fence() for the next step.
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000311 if (cp.supportsFence)
312 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL);
313
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000314 if (cp.supportsSetDesktopSize &&
315 sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) == 2) {
316 ScreenSet layout;
317
318 layout = cp.screenLayout;
319
320 if (layout.num_screens() == 0)
321 layout.add_screen(rfb::Screen());
322 else if (layout.num_screens() != 1) {
323 ScreenSet::iterator iter;
324
325 while (true) {
326 iter = layout.begin();
327 ++iter;
328
329 if (iter == layout.end())
330 break;
331
332 layout.remove_screen(iter->id);
333 }
334 }
335
336 layout.begin()->dimensions.tl.x = 0;
337 layout.begin()->dimensions.tl.y = 0;
338 layout.begin()->dimensions.br.x = width;
339 layout.begin()->dimensions.br.y = height;
340
341 writer()->writeSetDesktopSize(width, height, layout);
342 }
343
344 firstUpdate = false;
345 }
346
Pierre Ossman5d512c32011-11-04 11:42:16 +0000347 // A format change has been scheduled and we are now past the update
348 // with the old format. Time to active the new one.
349 if (pendingPFChange) {
350 desktop->setServerPF(pendingPF);
351 cp.setPF(pendingPF);
352 pendingPFChange = false;
353 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000354
355 // Compute new settings based on updated bandwidth values
356 if (autoSelect)
357 autoSelectFormatAndEncoding();
358
359 // Make sure that the FLTK handling and the timers gets some CPU time
360 // in case of back to back framebuffer updates.
361 Fl::check();
362 Timer::checkTimeouts();
363}
364
365// The rest of the callbacks are fairly self-explanatory...
366
367void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
368{
369 desktop->setColourMapEntries(firstColour, nColours, rgbs);
370}
371
372void CConn::bell()
373{
374 fl_beep();
375}
376
377void CConn::serverCutText(const char* str, rdr::U32 len)
378{
Pierre Ossman689c4582011-05-26 15:39:41 +0000379 char *buffer;
380 int size, ret;
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000381
Pierre Ossman689c4582011-05-26 15:39:41 +0000382 size = fl_utf8froma(NULL, 0, str, len);
383 if (size <= 0)
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000384 return;
Pierre Ossman689c4582011-05-26 15:39:41 +0000385
386 size++;
387
388 buffer = new char[size];
389
390 ret = fl_utf8froma(buffer, size, str, len);
391 assert(ret < size);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000392
393 vlog.debug("Got clipboard data: '%s'", buffer);
394
Pierre Ossmancf2443c2011-09-07 09:01:20 +0000395 // RFB doesn't have separate selection and clipboard concepts, so we
396 // dump the data into both variants.
397 Fl::copy(buffer, ret, 0);
Pierre Ossmand81e8f42011-05-19 14:47:15 +0000398 Fl::copy(buffer, ret, 1);
Pierre Ossman689c4582011-05-26 15:39:41 +0000399
400 delete [] buffer;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000401}
402
403// We start timing on beginRect and stop timing on endRect, to
404// avoid skewing the bandwidth estimation as a result of the server
405// being slow or the network having high latency
406void CConn::beginRect(const Rect& r, int encoding)
407{
408 sock->inStream().startTiming();
409 if (encoding != encodingCopyRect) {
410 lastServerEncoding = encoding;
411 }
412}
413
414void CConn::endRect(const Rect& r, int encoding)
415{
416 sock->inStream().stopTiming();
417}
418
419void CConn::fillRect(const rfb::Rect& r, rfb::Pixel p)
420{
421 desktop->fillRect(r,p);
422}
423void CConn::imageRect(const rfb::Rect& r, void* p)
424{
425 desktop->imageRect(r,p);
426}
427void CConn::copyRect(const rfb::Rect& r, int sx, int sy)
428{
429 desktop->copyRect(r,sx,sy);
430}
431void CConn::setCursor(int width, int height, const Point& hotspot,
432 void* data, void* mask)
433{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000434 desktop->setCursor(width, height, hotspot, data, mask);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000435}
436
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000437void CConn::fence(rdr::U32 flags, unsigned len, const char data[])
438{
439 CMsgHandler::fence(flags, len, data);
440
441 if (flags & fenceFlagRequest) {
442 // We handle everything synchronously so we trivially honor these modes
443 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
444
445 writer()->writeFence(flags, len, data);
446 return;
447 }
448
449 if (len == 0) {
450 // Initial probe
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000451 if (flags & fenceFlagSyncNext) {
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000452 supportsSyncFence = true;
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000453
454 if (cp.supportsContinuousUpdates) {
455 vlog.info(_("Enabling continuous updates"));
456 continuousUpdates = true;
457 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
458 }
459 }
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000460 } else {
461 // Pixel format change
462 rdr::MemInStream memStream(data, len);
463 PixelFormat pf;
464
465 pf.read(&memStream);
466
467 desktop->setServerPF(pf);
468 cp.setPF(pf);
469 }
470}
471
DRC33c15e32011-11-03 18:49:21 +0000472rdr::U8* CConn::getRawPixelsRW(const rfb::Rect& r, int* stride) {
473 return desktop->getPixelsRW(r, stride);
474}
475void CConn::releaseRawPixels(const rfb::Rect& r) {
476 desktop->damageRect(r);
477}
478
479
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000480////////////////////// Internal methods //////////////////////
481
482void CConn::resizeFramebuffer()
483{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000484 if (!desktop)
485 return;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000486
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000487 if (continuousUpdates)
488 writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height);
489
Pierre Ossman6455d852011-06-01 09:33:00 +0000490 desktop->resizeFramebuffer(cp.width, cp.height);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000491}
492
493// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
494// to the connection speed:
495//
496// First we wait for at least one second of bandwidth measurement.
497//
498// Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
499// which should be perceptually lossless.
500//
501// If the bandwidth is below that, we choose a more lossy JPEG quality.
502//
503// If the bandwidth drops below 256 Kbps, we switch to palette mode.
504//
505// Note: The system here is fairly arbitrary and should be replaced
506// with something more intelligent at the server end.
507//
508void CConn::autoSelectFormatAndEncoding()
509{
510 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
511 unsigned int timeWaited = sock->inStream().timeWaited();
512 bool newFullColour = fullColour;
513 int newQualityLevel = qualityLevel;
514
515 // Always use Tight
516 if (currentEncoding != encodingTight) {
517 currentEncoding = encodingTight;
518 encodingChange = true;
519 }
520
521 // Check that we have a decent bandwidth measurement
522 if ((kbitsPerSecond == 0) || (timeWaited < 10000))
523 return;
524
525 // Select appropriate quality level
526 if (!noJpeg) {
527 if (kbitsPerSecond > 16000)
528 newQualityLevel = 8;
529 else
530 newQualityLevel = 6;
531
532 if (newQualityLevel != qualityLevel) {
533 vlog.info(_("Throughput %d kbit/s - changing to quality %d"),
534 kbitsPerSecond, newQualityLevel);
535 cp.qualityLevel = newQualityLevel;
536 qualityLevel.setParam(newQualityLevel);
537 encodingChange = true;
538 }
539 }
540
541 if (cp.beforeVersion(3, 8)) {
542 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
543 // cursors "asynchronously". If this happens in the middle of a
544 // pixel format change, the server will encode the cursor with
545 // the old format, but the client will try to decode it
546 // according to the new format. This will lead to a
547 // crash. Therefore, we do not allow automatic format change for
548 // old servers.
549 return;
550 }
551
552 // Select best color level
553 newFullColour = (kbitsPerSecond > 256);
554 if (newFullColour != fullColour) {
555 vlog.info(_("Throughput %d kbit/s - full color is now %s"),
556 kbitsPerSecond,
557 newFullColour ? _("enabled") : _("disabled"));
558 fullColour.setParam(newFullColour);
559 formatChange = true;
560 }
561}
562
563// checkEncodings() sends a setEncodings message if one is needed.
564void CConn::checkEncodings()
565{
566 if (encodingChange && writer()) {
567 vlog.info(_("Using %s encoding"),encodingName(currentEncoding));
568 writer()->writeSetEncodings(currentEncoding, true);
569 encodingChange = false;
570 }
571}
572
573// requestNewUpdate() requests an update from the server, having set the
574// format and encoding appropriately.
575void CConn::requestNewUpdate()
576{
577 if (formatChange) {
578 PixelFormat pf;
579
580 /* Catch incorrect requestNewUpdate calls */
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000581 assert(!pendingUpdate || supportsSyncFence);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000582
583 if (fullColour) {
584 pf = fullColourPF;
585 } else {
586 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000587 pf = verylowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000588 else if (lowColourLevel == 1)
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000589 pf = lowColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000590 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000591 pf = mediumColourPF;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000592 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000593
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000594 if (supportsSyncFence) {
595 // We let the fence carry the pixel format and switch once we
596 // get the response back. That way we will be synchronised with
597 // when the server switches.
598 rdr::MemOutStream memStream;
599
600 pf.write(&memStream);
601
602 writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext,
603 memStream.length(), (const char*)memStream.data());
604 } else {
605 // New requests are sent out at the start of processing the last
606 // one, so we cannot switch our internal format right now (doing so
607 // would mean misdecoding the current update).
608 pendingPFChange = true;
609 pendingPF = pf;
610 }
Pierre Ossman5d512c32011-11-04 11:42:16 +0000611
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000612 char str[256];
613 pf.print(str, 256);
614 vlog.info(_("Using pixel format %s"),str);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000615 writer()->writeSetPixelFormat(pf);
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000616
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000617 formatChange = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000618 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000619
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000620 checkEncodings();
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000621
Pierre Ossmanaa73c892011-11-15 12:13:37 +0000622 if (forceNonincremental || !continuousUpdates) {
623 pendingUpdate = true;
624 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
625 !forceNonincremental);
626 }
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000627
628 forceNonincremental = false;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000629}
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000630
631void CConn::handleOptions(void *data)
632{
633 CConn *self = (CConn*)data;
634
635 // Checking all the details of the current set of encodings is just
636 // a pain. Assume something has changed, as resending the encoding
637 // list is cheap. Avoid overriding what the auto logic has selected
638 // though.
639 if (!autoSelect) {
640 int encNum = encodingNum(preferredEncoding);
641
642 if (encNum != -1)
643 self->currentEncoding = encNum;
644
645 self->cp.qualityLevel = qualityLevel;
646 }
647
Pierre Ossmanda389562011-06-09 08:24:37 +0000648 self->cp.supportsLocalCursor = true;
649
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000650 self->cp.customCompressLevel = customCompressLevel;
651 self->cp.compressLevel = compressLevel;
652
653 self->cp.noJpeg = noJpeg;
654
655 self->encodingChange = true;
656
657 // Format changes refreshes the entire screen though and are therefore
658 // very costly. It's probably worth the effort to see if it is necessary
659 // here.
660 PixelFormat pf;
661
662 if (fullColour) {
663 pf = self->fullColourPF;
664 } else {
665 if (lowColourLevel == 0)
Pierre Ossmancf836f22011-07-14 14:34:09 +0000666 pf = verylowColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000667 else if (lowColourLevel == 1)
668 pf = lowColourPF;
669 else
Pierre Ossmancf836f22011-07-14 14:34:09 +0000670 pf = mediumColourPF;
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000671 }
672
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000673 if (!pf.equal(self->cp.pf())) {
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000674 self->formatChange = true;
Pierre Ossmane28bdb22011-11-14 16:02:06 +0000675
676 // Without fences, we cannot safely trigger an update request directly
677 // but must wait for the next update to arrive.
678 if (self->supportsSyncFence)
679 self->requestNewUpdate();
680 }
Pierre Ossmanf4f30942011-05-17 09:39:07 +0000681}