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