Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * Copyright 2009-2011 Pierre Ossman <ossman@cendio.se> for Cendio AB |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 3 | * Copyright (C) 2011 D. R. Commander. All Rights Reserved. |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 4 | * |
| 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 Åstrand | c359f36 | 2011-08-23 12:04:46 +0000 | [diff] [blame] | 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include <config.h> |
| 23 | #endif |
| 24 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 25 | #include <assert.h> |
DRC | b65bb93 | 2011-06-24 03:17:00 +0000 | [diff] [blame] | 26 | #ifndef _WIN32 |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 27 | #include <unistd.h> |
DRC | b65bb93 | 2011-06-24 03:17:00 +0000 | [diff] [blame] | 28 | #endif |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 29 | |
| 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 Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 36 | #include <rfb/fenceTypes.h> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 37 | #include <rfb/Timer.h> |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 38 | #include <rdr/MemInStream.h> |
| 39 | #include <rdr/MemOutStream.h> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 40 | #include <network/TcpSocket.h> |
| 41 | |
| 42 | #include <FL/Fl.H> |
| 43 | #include <FL/fl_ask.H> |
| 44 | |
| 45 | #include "CConn.h" |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 46 | #include "OptionsDialog.h" |
Pierre Ossman | 947b48d | 2014-01-27 16:52:35 +0100 | [diff] [blame^] | 47 | #include "DesktopWindow.h" |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 48 | #include "i18n.h" |
| 49 | #include "parameters.h" |
Pierre Ossman | 39ceb50 | 2011-07-12 15:54:25 +0000 | [diff] [blame] | 50 | #include "vncviewer.h" |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 51 | |
DRC | b65bb93 | 2011-06-24 03:17:00 +0000 | [diff] [blame] | 52 | #ifdef WIN32 |
| 53 | #include "win32.h" |
| 54 | #endif |
| 55 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 56 | using namespace rdr; |
| 57 | using namespace rfb; |
| 58 | using namespace std; |
| 59 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 60 | static rfb::LogWriter vlog("CConn"); |
| 61 | |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 62 | // 8 colours (1 bit per component) |
| 63 | static const PixelFormat verylowColourPF(8, 3,false, true, |
| 64 | 1, 1, 1, 2, 1, 0); |
| 65 | // 64 colours (2 bits per component) |
| 66 | static const PixelFormat lowColourPF(8, 6, false, true, |
| 67 | 3, 3, 3, 4, 2, 0); |
| 68 | // 256 colours (palette) |
| 69 | static const PixelFormat mediumColourPF(8, 8, false, false); |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 70 | |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 71 | CConn::CConn(const char* vncServerName, network::Socket* socket=NULL) |
| 72 | : serverHost(0), serverPort(0), desktop(NULL), |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 73 | pendingPFChange(false), |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 74 | currentEncoding(encodingTight), lastServerEncoding((unsigned int)-1), |
| 75 | formatChange(false), encodingChange(false), |
Pierre Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 76 | firstUpdate(true), pendingUpdate(false), continuousUpdates(false), |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 77 | forceNonincremental(true), supportsSyncFence(false) |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 78 | { |
| 79 | setShared(::shared); |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 80 | sock = socket; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 81 | |
| 82 | int encNum = encodingNum(preferredEncoding); |
| 83 | if (encNum != -1) |
| 84 | currentEncoding = encNum; |
| 85 | |
Pierre Ossman | da38956 | 2011-06-09 08:24:37 +0000 | [diff] [blame] | 86 | cp.supportsLocalCursor = true; |
| 87 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 88 | cp.supportsDesktopResize = true; |
| 89 | cp.supportsExtendedDesktopSize = true; |
| 90 | cp.supportsDesktopRename = true; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 91 | |
| 92 | cp.customCompressLevel = customCompressLevel; |
| 93 | cp.compressLevel = compressLevel; |
| 94 | |
| 95 | cp.noJpeg = noJpeg; |
| 96 | cp.qualityLevel = qualityLevel; |
| 97 | |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 98 | if(sock == NULL) { |
| 99 | try { |
| 100 | getHostAndPort(vncServerName, &serverHost, &serverPort); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 101 | |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 102 | 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 Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 110 | } |
| 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 Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 121 | |
| 122 | OptionsDialog::addCallback(handleOptions, this); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | CConn::~CConn() |
| 126 | { |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 127 | OptionsDialog::removeCallback(handleOptions); |
| 128 | |
Pierre Ossman | 6a9e2e6 | 2011-05-19 14:47:43 +0000 | [diff] [blame] | 129 | if (desktop) |
| 130 | delete desktop; |
| 131 | |
Pierre Ossman | 5e04c26 | 2011-11-09 11:31:12 +0000 | [diff] [blame] | 132 | delete [] serverHost; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 133 | if (sock) |
| 134 | Fl::remove_fd(sock->getFd()); |
| 135 | delete sock; |
| 136 | } |
| 137 | |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 138 | void CConn::refreshFramebuffer() |
| 139 | { |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 140 | 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 Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Pierre Ossman | 2eb1d11 | 2011-05-16 12:18:08 +0000 | [diff] [blame] | 148 | const 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 Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 181 | // 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 | |
| 185 | void 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 | |
DRC | 3e7ed81 | 2013-02-26 10:34:22 +0000 | [diff] [blame] | 196 | void CConn::socketEvent(FL_SOCKET fd, void *data) |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 197 | { |
| 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 Ossman | ad8609a | 2012-04-26 09:04:14 +0000 | [diff] [blame] | 217 | vlog.info("%s", e.str()); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 218 | exit_vncviewer(); |
| 219 | } catch (rdr::Exception& e) { |
Pierre Ossman | ad8609a | 2012-04-26 09:04:14 +0000 | [diff] [blame] | 220 | vlog.error("%s", e.str()); |
Pierre Ossman | e2ef5c1 | 2011-07-12 16:56:34 +0000 | [diff] [blame] | 221 | exit_vncviewer(e.str()); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 222 | } |
| 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. |
| 232 | void 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 Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 246 | // Force a switch to the format and encoding we'd like |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 247 | formatChange = encodingChange = true; |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 248 | |
| 249 | // And kick off the update cycle |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 250 | requestNewUpdate(); |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 251 | |
| 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 Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // setDesktopSize() is called when the desktop size changes (including when |
| 261 | // it is set initially). |
| 262 | void 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() |
| 269 | void 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 |
| 283 | void 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 Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 293 | // one. |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 294 | void CConn::framebufferUpdateStart() |
| 295 | { |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 296 | // Note: This might not be true if sync fences are supported |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 297 | pendingUpdate = false; |
| 298 | |
| 299 | requestNewUpdate(); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 300 | } |
| 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. |
| 306 | void CConn::framebufferUpdateEnd() |
| 307 | { |
| 308 | desktop->updateWindow(); |
| 309 | |
| 310 | if (firstUpdate) { |
Pierre Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 311 | // We need fences to make extra update requests and continuous |
| 312 | // updates "safe". See fence() for the next step. |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 313 | if (cp.supportsFence) |
| 314 | writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL); |
| 315 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 316 | firstUpdate = false; |
| 317 | } |
| 318 | |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 319 | // 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 Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 326 | |
| 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 | |
| 339 | void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs) |
| 340 | { |
| 341 | desktop->setColourMapEntries(firstColour, nColours, rgbs); |
| 342 | } |
| 343 | |
| 344 | void CConn::bell() |
| 345 | { |
| 346 | fl_beep(); |
| 347 | } |
| 348 | |
| 349 | void CConn::serverCutText(const char* str, rdr::U32 len) |
| 350 | { |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 351 | char *buffer; |
| 352 | int size, ret; |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 353 | |
Pierre Ossman | 5010488 | 2013-05-24 10:47:27 +0000 | [diff] [blame] | 354 | if (!acceptClipboard) |
| 355 | return; |
| 356 | |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 357 | size = fl_utf8froma(NULL, 0, str, len); |
| 358 | if (size <= 0) |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 359 | return; |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 360 | |
| 361 | size++; |
| 362 | |
| 363 | buffer = new char[size]; |
| 364 | |
| 365 | ret = fl_utf8froma(buffer, size, str, len); |
| 366 | assert(ret < size); |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 367 | |
Pierre Ossman | 5803d3b | 2013-09-05 14:25:40 +0000 | [diff] [blame] | 368 | vlog.debug("Got clipboard data (%d bytes)", strlen(buffer)); |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 369 | |
Pierre Ossman | cf2443c | 2011-09-07 09:01:20 +0000 | [diff] [blame] | 370 | // 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 Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 373 | Fl::copy(buffer, ret, 1); |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 374 | |
| 375 | delete [] buffer; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 376 | } |
| 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 |
| 381 | void CConn::beginRect(const Rect& r, int encoding) |
| 382 | { |
| 383 | sock->inStream().startTiming(); |
| 384 | if (encoding != encodingCopyRect) { |
| 385 | lastServerEncoding = encoding; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | void CConn::endRect(const Rect& r, int encoding) |
| 390 | { |
| 391 | sock->inStream().stopTiming(); |
| 392 | } |
| 393 | |
| 394 | void CConn::fillRect(const rfb::Rect& r, rfb::Pixel p) |
| 395 | { |
| 396 | desktop->fillRect(r,p); |
| 397 | } |
Pierre Ossman | 0dff4b8 | 2014-01-27 16:17:21 +0100 | [diff] [blame] | 398 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 399 | void CConn::imageRect(const rfb::Rect& r, void* p) |
| 400 | { |
| 401 | desktop->imageRect(r,p); |
| 402 | } |
Pierre Ossman | 0dff4b8 | 2014-01-27 16:17:21 +0100 | [diff] [blame] | 403 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 404 | void CConn::copyRect(const rfb::Rect& r, int sx, int sy) |
| 405 | { |
| 406 | desktop->copyRect(r,sx,sy); |
| 407 | } |
Pierre Ossman | 0dff4b8 | 2014-01-27 16:17:21 +0100 | [diff] [blame] | 408 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 409 | void CConn::setCursor(int width, int height, const Point& hotspot, |
| 410 | void* data, void* mask) |
| 411 | { |
Pierre Ossman | 835b4ef | 2011-06-08 17:02:36 +0000 | [diff] [blame] | 412 | desktop->setCursor(width, height, hotspot, data, mask); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 415 | void 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 Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 429 | if (flags & fenceFlagSyncNext) { |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 430 | supportsSyncFence = true; |
Pierre Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 431 | |
| 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 Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 438 | } 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 Ossman | 945cdda | 2014-01-28 14:13:12 +0100 | [diff] [blame] | 450 | rdr::U8* CConn::getRawBufferRW(const rfb::Rect& r, int* stride) { |
| 451 | return desktop->getBufferRW(r, stride); |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 452 | } |
Pierre Ossman | 945cdda | 2014-01-28 14:13:12 +0100 | [diff] [blame] | 453 | void CConn::releaseRawBuffer(const rfb::Rect& r) { |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 454 | desktop->damageRect(r); |
| 455 | } |
| 456 | |
| 457 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 458 | ////////////////////// Internal methods ////////////////////// |
| 459 | |
| 460 | void CConn::resizeFramebuffer() |
| 461 | { |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 462 | if (!desktop) |
| 463 | return; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 464 | |
Pierre Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 465 | if (continuousUpdates) |
| 466 | writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height); |
| 467 | |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 468 | desktop->resizeFramebuffer(cp.width, cp.height); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 469 | } |
| 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 | // |
| 486 | void 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. |
| 542 | void 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. |
| 553 | void CConn::requestNewUpdate() |
| 554 | { |
| 555 | if (formatChange) { |
| 556 | PixelFormat pf; |
| 557 | |
| 558 | /* Catch incorrect requestNewUpdate calls */ |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 559 | assert(!pendingUpdate || supportsSyncFence); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 560 | |
| 561 | if (fullColour) { |
| 562 | pf = fullColourPF; |
| 563 | } else { |
| 564 | if (lowColourLevel == 0) |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 565 | pf = verylowColourPF; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 566 | else if (lowColourLevel == 1) |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 567 | pf = lowColourPF; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 568 | else |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 569 | pf = mediumColourPF; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 570 | } |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 571 | |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 572 | 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 Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 589 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 590 | char str[256]; |
| 591 | pf.print(str, 256); |
| 592 | vlog.info(_("Using pixel format %s"),str); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 593 | writer()->writeSetPixelFormat(pf); |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 594 | |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 595 | formatChange = false; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 596 | } |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 597 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 598 | checkEncodings(); |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 599 | |
Pierre Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 600 | if (forceNonincremental || !continuousUpdates) { |
| 601 | pendingUpdate = true; |
| 602 | writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height), |
| 603 | !forceNonincremental); |
| 604 | } |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 605 | |
| 606 | forceNonincremental = false; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 607 | } |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 608 | |
| 609 | void 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 Ossman | da38956 | 2011-06-09 08:24:37 +0000 | [diff] [blame] | 626 | self->cp.supportsLocalCursor = true; |
| 627 | |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 628 | 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 Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 644 | pf = verylowColourPF; |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 645 | else if (lowColourLevel == 1) |
| 646 | pf = lowColourPF; |
| 647 | else |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 648 | pf = mediumColourPF; |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 651 | if (!pf.equal(self->cp.pf())) { |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 652 | self->formatChange = true; |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 653 | |
| 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 Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 659 | } |