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 | |
Pierre Ossman | a22459d | 2014-03-17 14:42:10 +0100 | [diff] [blame] | 92 | if (customCompressLevel) |
| 93 | cp.compressLevel = compressLevel; |
| 94 | else |
| 95 | cp.compressLevel = -1; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 96 | |
Pierre Ossman | a22459d | 2014-03-17 14:42:10 +0100 | [diff] [blame] | 97 | if (!noJpeg) |
| 98 | cp.qualityLevel = qualityLevel; |
| 99 | else |
| 100 | cp.qualityLevel = -1; |
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 | if(sock == NULL) { |
| 103 | try { |
| 104 | getHostAndPort(vncServerName, &serverHost, &serverPort); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 105 | |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 106 | 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 Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 114 | } |
| 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 Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 125 | |
| 126 | OptionsDialog::addCallback(handleOptions, this); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | CConn::~CConn() |
| 130 | { |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 131 | OptionsDialog::removeCallback(handleOptions); |
| 132 | |
Pierre Ossman | 6a9e2e6 | 2011-05-19 14:47:43 +0000 | [diff] [blame] | 133 | if (desktop) |
| 134 | delete desktop; |
| 135 | |
Pierre Ossman | 5e04c26 | 2011-11-09 11:31:12 +0000 | [diff] [blame] | 136 | delete [] serverHost; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 137 | if (sock) |
| 138 | Fl::remove_fd(sock->getFd()); |
| 139 | delete sock; |
| 140 | } |
| 141 | |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 142 | void CConn::refreshFramebuffer() |
| 143 | { |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 144 | 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 Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Pierre Ossman | 2eb1d11 | 2011-05-16 12:18:08 +0000 | [diff] [blame] | 152 | const 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 Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 185 | // 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 | |
| 189 | void 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 | |
DRC | 3e7ed81 | 2013-02-26 10:34:22 +0000 | [diff] [blame] | 200 | void CConn::socketEvent(FL_SOCKET fd, void *data) |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 201 | { |
| 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 Ossman | ad8609a | 2012-04-26 09:04:14 +0000 | [diff] [blame] | 221 | vlog.info("%s", e.str()); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 222 | exit_vncviewer(); |
| 223 | } catch (rdr::Exception& e) { |
Pierre Ossman | ad8609a | 2012-04-26 09:04:14 +0000 | [diff] [blame] | 224 | vlog.error("%s", e.str()); |
Pierre Ossman | e2ef5c1 | 2011-07-12 16:56:34 +0000 | [diff] [blame] | 225 | exit_vncviewer(e.str()); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 226 | } |
| 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. |
| 236 | void 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 Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 250 | // Force a switch to the format and encoding we'd like |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 251 | formatChange = encodingChange = true; |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 252 | |
| 253 | // And kick off the update cycle |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 254 | requestNewUpdate(); |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 255 | |
| 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 Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // setDesktopSize() is called when the desktop size changes (including when |
| 265 | // it is set initially). |
| 266 | void 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() |
| 273 | void 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 |
| 287 | void 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 Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 297 | // one. |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 298 | void CConn::framebufferUpdateStart() |
| 299 | { |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 300 | // Note: This might not be true if sync fences are supported |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 301 | pendingUpdate = false; |
| 302 | |
| 303 | requestNewUpdate(); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 304 | } |
| 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. |
| 310 | void CConn::framebufferUpdateEnd() |
| 311 | { |
| 312 | desktop->updateWindow(); |
| 313 | |
| 314 | if (firstUpdate) { |
Pierre Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 315 | // We need fences to make extra update requests and continuous |
| 316 | // updates "safe". See fence() for the next step. |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 317 | if (cp.supportsFence) |
| 318 | writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL); |
| 319 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 320 | firstUpdate = false; |
| 321 | } |
| 322 | |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 323 | // 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 Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 330 | |
| 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 | |
| 343 | void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs) |
| 344 | { |
| 345 | desktop->setColourMapEntries(firstColour, nColours, rgbs); |
| 346 | } |
| 347 | |
| 348 | void CConn::bell() |
| 349 | { |
| 350 | fl_beep(); |
| 351 | } |
| 352 | |
| 353 | void CConn::serverCutText(const char* str, rdr::U32 len) |
| 354 | { |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 355 | char *buffer; |
| 356 | int size, ret; |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 357 | |
Pierre Ossman | 5010488 | 2013-05-24 10:47:27 +0000 | [diff] [blame] | 358 | if (!acceptClipboard) |
| 359 | return; |
| 360 | |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 361 | size = fl_utf8froma(NULL, 0, str, len); |
| 362 | if (size <= 0) |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 363 | return; |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 364 | |
| 365 | size++; |
| 366 | |
| 367 | buffer = new char[size]; |
| 368 | |
| 369 | ret = fl_utf8froma(buffer, size, str, len); |
| 370 | assert(ret < size); |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 371 | |
Pierre Ossman | 5803d3b | 2013-09-05 14:25:40 +0000 | [diff] [blame] | 372 | vlog.debug("Got clipboard data (%d bytes)", strlen(buffer)); |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 373 | |
Pierre Ossman | cf2443c | 2011-09-07 09:01:20 +0000 | [diff] [blame] | 374 | // 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 Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 377 | Fl::copy(buffer, ret, 1); |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 378 | |
| 379 | delete [] buffer; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 380 | } |
| 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 |
| 385 | void CConn::beginRect(const Rect& r, int encoding) |
| 386 | { |
| 387 | sock->inStream().startTiming(); |
| 388 | if (encoding != encodingCopyRect) { |
| 389 | lastServerEncoding = encoding; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | void CConn::endRect(const Rect& r, int encoding) |
| 394 | { |
| 395 | sock->inStream().stopTiming(); |
| 396 | } |
| 397 | |
| 398 | void CConn::fillRect(const rfb::Rect& r, rfb::Pixel p) |
| 399 | { |
| 400 | desktop->fillRect(r,p); |
| 401 | } |
Pierre Ossman | 0dff4b8 | 2014-01-27 16:17:21 +0100 | [diff] [blame] | 402 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 403 | void CConn::imageRect(const rfb::Rect& r, void* p) |
| 404 | { |
| 405 | desktop->imageRect(r,p); |
| 406 | } |
Pierre Ossman | 0dff4b8 | 2014-01-27 16:17:21 +0100 | [diff] [blame] | 407 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 408 | void CConn::copyRect(const rfb::Rect& r, int sx, int sy) |
| 409 | { |
| 410 | desktop->copyRect(r,sx,sy); |
| 411 | } |
Pierre Ossman | 0dff4b8 | 2014-01-27 16:17:21 +0100 | [diff] [blame] | 412 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 413 | void CConn::setCursor(int width, int height, const Point& hotspot, |
| 414 | void* data, void* mask) |
| 415 | { |
Pierre Ossman | 835b4ef | 2011-06-08 17:02:36 +0000 | [diff] [blame] | 416 | desktop->setCursor(width, height, hotspot, data, mask); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 419 | void 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 Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 433 | if (flags & fenceFlagSyncNext) { |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 434 | supportsSyncFence = true; |
Pierre Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 435 | |
| 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 Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 442 | } 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 Ossman | 945cdda | 2014-01-28 14:13:12 +0100 | [diff] [blame] | 454 | rdr::U8* CConn::getRawBufferRW(const rfb::Rect& r, int* stride) { |
| 455 | return desktop->getBufferRW(r, stride); |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 456 | } |
Pierre Ossman | 945cdda | 2014-01-28 14:13:12 +0100 | [diff] [blame] | 457 | void CConn::releaseRawBuffer(const rfb::Rect& r) { |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 458 | desktop->damageRect(r); |
| 459 | } |
| 460 | |
| 461 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 462 | ////////////////////// Internal methods ////////////////////// |
| 463 | |
| 464 | void CConn::resizeFramebuffer() |
| 465 | { |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 466 | if (!desktop) |
| 467 | return; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 468 | |
Pierre Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 469 | if (continuousUpdates) |
| 470 | writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height); |
| 471 | |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 472 | desktop->resizeFramebuffer(cp.width, cp.height); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 473 | } |
| 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 | // |
| 490 | void 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. |
| 546 | void 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. |
| 557 | void CConn::requestNewUpdate() |
| 558 | { |
| 559 | if (formatChange) { |
| 560 | PixelFormat pf; |
| 561 | |
| 562 | /* Catch incorrect requestNewUpdate calls */ |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 563 | assert(!pendingUpdate || supportsSyncFence); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 564 | |
| 565 | if (fullColour) { |
| 566 | pf = fullColourPF; |
| 567 | } else { |
| 568 | if (lowColourLevel == 0) |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 569 | pf = verylowColourPF; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 570 | else if (lowColourLevel == 1) |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 571 | pf = lowColourPF; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 572 | else |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 573 | pf = mediumColourPF; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 574 | } |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 575 | |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 576 | 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 Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 593 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 594 | char str[256]; |
| 595 | pf.print(str, 256); |
| 596 | vlog.info(_("Using pixel format %s"),str); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 597 | writer()->writeSetPixelFormat(pf); |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 598 | |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 599 | formatChange = false; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 600 | } |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 601 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 602 | checkEncodings(); |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 603 | |
Pierre Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 604 | if (forceNonincremental || !continuousUpdates) { |
| 605 | pendingUpdate = true; |
| 606 | writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height), |
| 607 | !forceNonincremental); |
| 608 | } |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 609 | |
| 610 | forceNonincremental = false; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 611 | } |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 612 | |
| 613 | void 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 Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Pierre Ossman | da38956 | 2011-06-09 08:24:37 +0000 | [diff] [blame] | 628 | self->cp.supportsLocalCursor = true; |
| 629 | |
Pierre Ossman | a22459d | 2014-03-17 14:42:10 +0100 | [diff] [blame] | 630 | if (customCompressLevel) |
| 631 | self->cp.compressLevel = compressLevel; |
| 632 | else |
| 633 | self->cp.compressLevel = -1; |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 634 | |
Pierre Ossman | a22459d | 2014-03-17 14:42:10 +0100 | [diff] [blame] | 635 | if (!noJpeg && !autoSelect) |
| 636 | self->cp.qualityLevel = qualityLevel; |
| 637 | else |
| 638 | self->cp.qualityLevel = -1; |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 639 | |
| 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 Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 651 | pf = verylowColourPF; |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 652 | else if (lowColourLevel == 1) |
| 653 | pf = lowColourPF; |
| 654 | else |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 655 | pf = mediumColourPF; |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 658 | if (!pf.equal(self->cp.pf())) { |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 659 | self->formatChange = true; |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 660 | |
| 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 Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 666 | } |