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 | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 47 | #include "i18n.h" |
| 48 | #include "parameters.h" |
Pierre Ossman | 39ceb50 | 2011-07-12 15:54:25 +0000 | [diff] [blame] | 49 | #include "vncviewer.h" |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 50 | |
DRC | b65bb93 | 2011-06-24 03:17:00 +0000 | [diff] [blame] | 51 | #ifdef WIN32 |
| 52 | #include "win32.h" |
| 53 | #endif |
| 54 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 55 | using namespace rdr; |
| 56 | using namespace rfb; |
| 57 | using namespace std; |
| 58 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 59 | static rfb::LogWriter vlog("CConn"); |
| 60 | |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 61 | // 8 colours (1 bit per component) |
| 62 | static const PixelFormat verylowColourPF(8, 3,false, true, |
| 63 | 1, 1, 1, 2, 1, 0); |
| 64 | // 64 colours (2 bits per component) |
| 65 | static const PixelFormat lowColourPF(8, 6, false, true, |
| 66 | 3, 3, 3, 4, 2, 0); |
| 67 | // 256 colours (palette) |
| 68 | static const PixelFormat mediumColourPF(8, 8, false, false); |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 69 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 70 | CConn::CConn(const char* vncServerName) |
| 71 | : serverHost(0), serverPort(0), sock(NULL), desktop(NULL), |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 72 | pendingPFChange(false), |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 73 | currentEncoding(encodingTight), lastServerEncoding((unsigned int)-1), |
| 74 | formatChange(false), encodingChange(false), |
Pierre Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 75 | firstUpdate(true), pendingUpdate(false), continuousUpdates(false), |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 76 | forceNonincremental(true), supportsSyncFence(false) |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 77 | { |
| 78 | setShared(::shared); |
| 79 | |
| 80 | int encNum = encodingNum(preferredEncoding); |
| 81 | if (encNum != -1) |
| 82 | currentEncoding = encNum; |
| 83 | |
Pierre Ossman | da38956 | 2011-06-09 08:24:37 +0000 | [diff] [blame] | 84 | cp.supportsLocalCursor = true; |
| 85 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 86 | cp.supportsDesktopResize = true; |
| 87 | cp.supportsExtendedDesktopSize = true; |
| 88 | cp.supportsDesktopRename = true; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 89 | |
| 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 Ossman | ad8609a | 2012-04-26 09:04:14 +0000 | [diff] [blame] | 102 | vlog.error("%s", e.str()); |
Pierre Ossman | f52740e | 2012-04-25 15:43:56 +0000 | [diff] [blame] | 103 | fl_alert("%s", e.str()); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 104 | 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 Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 117 | |
| 118 | OptionsDialog::addCallback(handleOptions, this); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | CConn::~CConn() |
| 122 | { |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 123 | OptionsDialog::removeCallback(handleOptions); |
| 124 | |
Pierre Ossman | 6a9e2e6 | 2011-05-19 14:47:43 +0000 | [diff] [blame] | 125 | if (desktop) |
| 126 | delete desktop; |
| 127 | |
Pierre Ossman | 5e04c26 | 2011-11-09 11:31:12 +0000 | [diff] [blame] | 128 | delete [] serverHost; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 129 | if (sock) |
| 130 | Fl::remove_fd(sock->getFd()); |
| 131 | delete sock; |
| 132 | } |
| 133 | |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 134 | void CConn::refreshFramebuffer() |
| 135 | { |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 136 | 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 Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Pierre Ossman | 2eb1d11 | 2011-05-16 12:18:08 +0000 | [diff] [blame] | 144 | const 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 Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 177 | // 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 | |
| 181 | void 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 | |
| 192 | void 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 Ossman | ad8609a | 2012-04-26 09:04:14 +0000 | [diff] [blame] | 213 | vlog.info("%s", e.str()); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 214 | exit_vncviewer(); |
| 215 | } catch (rdr::Exception& e) { |
Pierre Ossman | ad8609a | 2012-04-26 09:04:14 +0000 | [diff] [blame] | 216 | vlog.error("%s", e.str()); |
Pierre Ossman | e2ef5c1 | 2011-07-12 16:56:34 +0000 | [diff] [blame] | 217 | exit_vncviewer(e.str()); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 218 | } |
| 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. |
| 228 | void 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 Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 242 | // Force a switch to the format and encoding we'd like |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 243 | formatChange = encodingChange = true; |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 244 | |
| 245 | // And kick off the update cycle |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 246 | requestNewUpdate(); |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 247 | |
| 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 Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // setDesktopSize() is called when the desktop size changes (including when |
| 257 | // it is set initially). |
| 258 | void 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() |
| 265 | void 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 |
| 279 | void 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 Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 289 | // one. |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 290 | void CConn::framebufferUpdateStart() |
| 291 | { |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 292 | // Note: This might not be true if sync fences are supported |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 293 | pendingUpdate = false; |
| 294 | |
| 295 | requestNewUpdate(); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 296 | } |
| 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. |
| 302 | void CConn::framebufferUpdateEnd() |
| 303 | { |
| 304 | desktop->updateWindow(); |
| 305 | |
| 306 | if (firstUpdate) { |
Pierre Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 307 | // We need fences to make extra update requests and continuous |
| 308 | // updates "safe". See fence() for the next step. |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 309 | if (cp.supportsFence) |
| 310 | writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, 0, NULL); |
| 311 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 312 | firstUpdate = false; |
| 313 | } |
| 314 | |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 315 | // A format change has been scheduled and we are now past the update |
| 316 | // with the old format. Time to active the new one. |
| 317 | if (pendingPFChange) { |
| 318 | desktop->setServerPF(pendingPF); |
| 319 | cp.setPF(pendingPF); |
| 320 | pendingPFChange = false; |
| 321 | } |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 322 | |
| 323 | // Compute new settings based on updated bandwidth values |
| 324 | if (autoSelect) |
| 325 | autoSelectFormatAndEncoding(); |
| 326 | |
| 327 | // Make sure that the FLTK handling and the timers gets some CPU time |
| 328 | // in case of back to back framebuffer updates. |
| 329 | Fl::check(); |
| 330 | Timer::checkTimeouts(); |
| 331 | } |
| 332 | |
| 333 | // The rest of the callbacks are fairly self-explanatory... |
| 334 | |
| 335 | void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs) |
| 336 | { |
| 337 | desktop->setColourMapEntries(firstColour, nColours, rgbs); |
| 338 | } |
| 339 | |
| 340 | void CConn::bell() |
| 341 | { |
| 342 | fl_beep(); |
| 343 | } |
| 344 | |
| 345 | void CConn::serverCutText(const char* str, rdr::U32 len) |
| 346 | { |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 347 | char *buffer; |
| 348 | int size, ret; |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 349 | |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 350 | size = fl_utf8froma(NULL, 0, str, len); |
| 351 | if (size <= 0) |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 352 | return; |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 353 | |
| 354 | size++; |
| 355 | |
| 356 | buffer = new char[size]; |
| 357 | |
| 358 | ret = fl_utf8froma(buffer, size, str, len); |
| 359 | assert(ret < size); |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 360 | |
| 361 | vlog.debug("Got clipboard data: '%s'", buffer); |
| 362 | |
Pierre Ossman | cf2443c | 2011-09-07 09:01:20 +0000 | [diff] [blame] | 363 | // RFB doesn't have separate selection and clipboard concepts, so we |
| 364 | // dump the data into both variants. |
| 365 | Fl::copy(buffer, ret, 0); |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 366 | Fl::copy(buffer, ret, 1); |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 367 | |
| 368 | delete [] buffer; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | // We start timing on beginRect and stop timing on endRect, to |
| 372 | // avoid skewing the bandwidth estimation as a result of the server |
| 373 | // being slow or the network having high latency |
| 374 | void CConn::beginRect(const Rect& r, int encoding) |
| 375 | { |
| 376 | sock->inStream().startTiming(); |
| 377 | if (encoding != encodingCopyRect) { |
| 378 | lastServerEncoding = encoding; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void CConn::endRect(const Rect& r, int encoding) |
| 383 | { |
| 384 | sock->inStream().stopTiming(); |
| 385 | } |
| 386 | |
| 387 | void CConn::fillRect(const rfb::Rect& r, rfb::Pixel p) |
| 388 | { |
| 389 | desktop->fillRect(r,p); |
| 390 | } |
| 391 | void CConn::imageRect(const rfb::Rect& r, void* p) |
| 392 | { |
| 393 | desktop->imageRect(r,p); |
| 394 | } |
| 395 | void CConn::copyRect(const rfb::Rect& r, int sx, int sy) |
| 396 | { |
| 397 | desktop->copyRect(r,sx,sy); |
| 398 | } |
| 399 | void CConn::setCursor(int width, int height, const Point& hotspot, |
| 400 | void* data, void* mask) |
| 401 | { |
Pierre Ossman | 835b4ef | 2011-06-08 17:02:36 +0000 | [diff] [blame] | 402 | desktop->setCursor(width, height, hotspot, data, mask); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 405 | void CConn::fence(rdr::U32 flags, unsigned len, const char data[]) |
| 406 | { |
| 407 | CMsgHandler::fence(flags, len, data); |
| 408 | |
| 409 | if (flags & fenceFlagRequest) { |
| 410 | // We handle everything synchronously so we trivially honor these modes |
| 411 | flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter); |
| 412 | |
| 413 | writer()->writeFence(flags, len, data); |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | if (len == 0) { |
| 418 | // Initial probe |
Pierre Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 419 | if (flags & fenceFlagSyncNext) { |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 420 | supportsSyncFence = true; |
Pierre Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 421 | |
| 422 | if (cp.supportsContinuousUpdates) { |
| 423 | vlog.info(_("Enabling continuous updates")); |
| 424 | continuousUpdates = true; |
| 425 | writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height); |
| 426 | } |
| 427 | } |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 428 | } else { |
| 429 | // Pixel format change |
| 430 | rdr::MemInStream memStream(data, len); |
| 431 | PixelFormat pf; |
| 432 | |
| 433 | pf.read(&memStream); |
| 434 | |
| 435 | desktop->setServerPF(pf); |
| 436 | cp.setPF(pf); |
| 437 | } |
| 438 | } |
| 439 | |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 440 | rdr::U8* CConn::getRawPixelsRW(const rfb::Rect& r, int* stride) { |
| 441 | return desktop->getPixelsRW(r, stride); |
| 442 | } |
| 443 | void CConn::releaseRawPixels(const rfb::Rect& r) { |
| 444 | desktop->damageRect(r); |
| 445 | } |
| 446 | |
| 447 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 448 | ////////////////////// Internal methods ////////////////////// |
| 449 | |
| 450 | void CConn::resizeFramebuffer() |
| 451 | { |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 452 | if (!desktop) |
| 453 | return; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 454 | |
Pierre Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 455 | if (continuousUpdates) |
| 456 | writer()->writeEnableContinuousUpdates(true, 0, 0, cp.width, cp.height); |
| 457 | |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 458 | desktop->resizeFramebuffer(cp.width, cp.height); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // autoSelectFormatAndEncoding() chooses the format and encoding appropriate |
| 462 | // to the connection speed: |
| 463 | // |
| 464 | // First we wait for at least one second of bandwidth measurement. |
| 465 | // |
| 466 | // Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality, |
| 467 | // which should be perceptually lossless. |
| 468 | // |
| 469 | // If the bandwidth is below that, we choose a more lossy JPEG quality. |
| 470 | // |
| 471 | // If the bandwidth drops below 256 Kbps, we switch to palette mode. |
| 472 | // |
| 473 | // Note: The system here is fairly arbitrary and should be replaced |
| 474 | // with something more intelligent at the server end. |
| 475 | // |
| 476 | void CConn::autoSelectFormatAndEncoding() |
| 477 | { |
| 478 | int kbitsPerSecond = sock->inStream().kbitsPerSecond(); |
| 479 | unsigned int timeWaited = sock->inStream().timeWaited(); |
| 480 | bool newFullColour = fullColour; |
| 481 | int newQualityLevel = qualityLevel; |
| 482 | |
| 483 | // Always use Tight |
| 484 | if (currentEncoding != encodingTight) { |
| 485 | currentEncoding = encodingTight; |
| 486 | encodingChange = true; |
| 487 | } |
| 488 | |
| 489 | // Check that we have a decent bandwidth measurement |
| 490 | if ((kbitsPerSecond == 0) || (timeWaited < 10000)) |
| 491 | return; |
| 492 | |
| 493 | // Select appropriate quality level |
| 494 | if (!noJpeg) { |
| 495 | if (kbitsPerSecond > 16000) |
| 496 | newQualityLevel = 8; |
| 497 | else |
| 498 | newQualityLevel = 6; |
| 499 | |
| 500 | if (newQualityLevel != qualityLevel) { |
| 501 | vlog.info(_("Throughput %d kbit/s - changing to quality %d"), |
| 502 | kbitsPerSecond, newQualityLevel); |
| 503 | cp.qualityLevel = newQualityLevel; |
| 504 | qualityLevel.setParam(newQualityLevel); |
| 505 | encodingChange = true; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | if (cp.beforeVersion(3, 8)) { |
| 510 | // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with |
| 511 | // cursors "asynchronously". If this happens in the middle of a |
| 512 | // pixel format change, the server will encode the cursor with |
| 513 | // the old format, but the client will try to decode it |
| 514 | // according to the new format. This will lead to a |
| 515 | // crash. Therefore, we do not allow automatic format change for |
| 516 | // old servers. |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | // Select best color level |
| 521 | newFullColour = (kbitsPerSecond > 256); |
| 522 | if (newFullColour != fullColour) { |
| 523 | vlog.info(_("Throughput %d kbit/s - full color is now %s"), |
| 524 | kbitsPerSecond, |
| 525 | newFullColour ? _("enabled") : _("disabled")); |
| 526 | fullColour.setParam(newFullColour); |
| 527 | formatChange = true; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | // checkEncodings() sends a setEncodings message if one is needed. |
| 532 | void CConn::checkEncodings() |
| 533 | { |
| 534 | if (encodingChange && writer()) { |
| 535 | vlog.info(_("Using %s encoding"),encodingName(currentEncoding)); |
| 536 | writer()->writeSetEncodings(currentEncoding, true); |
| 537 | encodingChange = false; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | // requestNewUpdate() requests an update from the server, having set the |
| 542 | // format and encoding appropriately. |
| 543 | void CConn::requestNewUpdate() |
| 544 | { |
| 545 | if (formatChange) { |
| 546 | PixelFormat pf; |
| 547 | |
| 548 | /* Catch incorrect requestNewUpdate calls */ |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 549 | assert(!pendingUpdate || supportsSyncFence); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 550 | |
| 551 | if (fullColour) { |
| 552 | pf = fullColourPF; |
| 553 | } else { |
| 554 | if (lowColourLevel == 0) |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 555 | pf = verylowColourPF; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 556 | else if (lowColourLevel == 1) |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 557 | pf = lowColourPF; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 558 | else |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 559 | pf = mediumColourPF; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 560 | } |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 561 | |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 562 | if (supportsSyncFence) { |
| 563 | // We let the fence carry the pixel format and switch once we |
| 564 | // get the response back. That way we will be synchronised with |
| 565 | // when the server switches. |
| 566 | rdr::MemOutStream memStream; |
| 567 | |
| 568 | pf.write(&memStream); |
| 569 | |
| 570 | writer()->writeFence(fenceFlagRequest | fenceFlagSyncNext, |
| 571 | memStream.length(), (const char*)memStream.data()); |
| 572 | } else { |
| 573 | // New requests are sent out at the start of processing the last |
| 574 | // one, so we cannot switch our internal format right now (doing so |
| 575 | // would mean misdecoding the current update). |
| 576 | pendingPFChange = true; |
| 577 | pendingPF = pf; |
| 578 | } |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 579 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 580 | char str[256]; |
| 581 | pf.print(str, 256); |
| 582 | vlog.info(_("Using pixel format %s"),str); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 583 | writer()->writeSetPixelFormat(pf); |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 584 | |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 585 | formatChange = false; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 586 | } |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 587 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 588 | checkEncodings(); |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 589 | |
Pierre Ossman | aa73c89 | 2011-11-15 12:13:37 +0000 | [diff] [blame] | 590 | if (forceNonincremental || !continuousUpdates) { |
| 591 | pendingUpdate = true; |
| 592 | writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height), |
| 593 | !forceNonincremental); |
| 594 | } |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 595 | |
| 596 | forceNonincremental = false; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 597 | } |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 598 | |
| 599 | void CConn::handleOptions(void *data) |
| 600 | { |
| 601 | CConn *self = (CConn*)data; |
| 602 | |
| 603 | // Checking all the details of the current set of encodings is just |
| 604 | // a pain. Assume something has changed, as resending the encoding |
| 605 | // list is cheap. Avoid overriding what the auto logic has selected |
| 606 | // though. |
| 607 | if (!autoSelect) { |
| 608 | int encNum = encodingNum(preferredEncoding); |
| 609 | |
| 610 | if (encNum != -1) |
| 611 | self->currentEncoding = encNum; |
| 612 | |
| 613 | self->cp.qualityLevel = qualityLevel; |
| 614 | } |
| 615 | |
Pierre Ossman | da38956 | 2011-06-09 08:24:37 +0000 | [diff] [blame] | 616 | self->cp.supportsLocalCursor = true; |
| 617 | |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 618 | self->cp.customCompressLevel = customCompressLevel; |
| 619 | self->cp.compressLevel = compressLevel; |
| 620 | |
| 621 | self->cp.noJpeg = noJpeg; |
| 622 | |
| 623 | self->encodingChange = true; |
| 624 | |
| 625 | // Format changes refreshes the entire screen though and are therefore |
| 626 | // very costly. It's probably worth the effort to see if it is necessary |
| 627 | // here. |
| 628 | PixelFormat pf; |
| 629 | |
| 630 | if (fullColour) { |
| 631 | pf = self->fullColourPF; |
| 632 | } else { |
| 633 | if (lowColourLevel == 0) |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 634 | pf = verylowColourPF; |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 635 | else if (lowColourLevel == 1) |
| 636 | pf = lowColourPF; |
| 637 | else |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 638 | pf = mediumColourPF; |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 641 | if (!pf.equal(self->cp.pf())) { |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 642 | self->formatChange = true; |
Pierre Ossman | e28bdb2 | 2011-11-14 16:02:06 +0000 | [diff] [blame] | 643 | |
| 644 | // Without fences, we cannot safely trigger an update request directly |
| 645 | // but must wait for the next update to arrive. |
| 646 | if (self->supportsSyncFence) |
| 647 | self->requestNewUpdate(); |
| 648 | } |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 649 | } |