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 |
| 3 | * |
| 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This software is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this software; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
| 19 | |
| 20 | #include <assert.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <rfb/CMsgWriter.h> |
| 24 | #include <rfb/encodings.h> |
| 25 | #include <rfb/Hostname.h> |
| 26 | #include <rfb/LogWriter.h> |
| 27 | #include <rfb/util.h> |
| 28 | #include <rfb/screenTypes.h> |
| 29 | #include <rfb/Timer.h> |
| 30 | #include <network/TcpSocket.h> |
| 31 | |
| 32 | #include <FL/Fl.H> |
| 33 | #include <FL/fl_ask.H> |
| 34 | |
| 35 | #include "CConn.h" |
| 36 | #include "i18n.h" |
| 37 | #include "parameters.h" |
| 38 | |
| 39 | using namespace rdr; |
| 40 | using namespace rfb; |
| 41 | using namespace std; |
| 42 | |
| 43 | extern void exit_vncviewer(); |
| 44 | |
| 45 | static rfb::LogWriter vlog("CConn"); |
| 46 | |
| 47 | CConn::CConn(const char* vncServerName) |
| 48 | : serverHost(0), serverPort(0), sock(NULL), desktop(NULL), |
| 49 | currentEncoding(encodingTight), lastServerEncoding((unsigned int)-1), |
| 50 | formatChange(false), encodingChange(false), |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 51 | firstUpdate(true), pendingUpdate(false), |
| 52 | forceNonincremental(false) |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 53 | { |
| 54 | setShared(::shared); |
| 55 | |
| 56 | int encNum = encodingNum(preferredEncoding); |
| 57 | if (encNum != -1) |
| 58 | currentEncoding = encNum; |
| 59 | |
| 60 | cp.supportsDesktopResize = true; |
| 61 | cp.supportsExtendedDesktopSize = true; |
| 62 | cp.supportsDesktopRename = true; |
| 63 | cp.supportsLocalCursor = useLocalCursor; |
| 64 | |
| 65 | cp.customCompressLevel = customCompressLevel; |
| 66 | cp.compressLevel = compressLevel; |
| 67 | |
| 68 | cp.noJpeg = noJpeg; |
| 69 | cp.qualityLevel = qualityLevel; |
| 70 | |
| 71 | try { |
| 72 | getHostAndPort(vncServerName, &serverHost, &serverPort); |
| 73 | |
| 74 | sock = new network::TcpSocket(serverHost, serverPort); |
| 75 | vlog.info(_("connected to host %s port %d"), serverHost, serverPort); |
| 76 | } catch (rdr::Exception& e) { |
| 77 | vlog.error(e.str()); |
| 78 | fl_alert(e.str()); |
| 79 | exit_vncviewer(); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | Fl::add_fd(sock->getFd(), FL_READ | FL_EXCEPT, socketEvent, this); |
| 84 | |
| 85 | // See callback below |
| 86 | sock->inStream().setBlockCallback(this); |
| 87 | |
| 88 | setServerName(serverHost); |
| 89 | setStreams(&sock->inStream(), &sock->outStream()); |
| 90 | |
| 91 | initialiseProtocol(); |
| 92 | } |
| 93 | |
| 94 | CConn::~CConn() |
| 95 | { |
| 96 | free(serverHost); |
| 97 | if (sock) |
| 98 | Fl::remove_fd(sock->getFd()); |
| 99 | delete sock; |
| 100 | } |
| 101 | |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 102 | void CConn::refreshFramebuffer() |
| 103 | { |
| 104 | // FIXME: We cannot safely trigger an update request directly but must |
| 105 | // wait for the next update to arrive. |
| 106 | if (!formatChange) |
| 107 | forceNonincremental = true; |
| 108 | } |
| 109 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 110 | // The RFB core is not properly asynchronous, so it calls this callback |
| 111 | // whenever it needs to block to wait for more data. Since FLTK is |
| 112 | // monitoring the socket, we just make sure FLTK gets to run. |
| 113 | |
| 114 | void CConn::blockCallback() |
| 115 | { |
| 116 | int next_timer; |
| 117 | |
| 118 | next_timer = Timer::checkTimeouts(); |
| 119 | if (next_timer == 0) |
| 120 | next_timer = INT_MAX; |
| 121 | |
| 122 | Fl::wait((double)next_timer / 1000.0); |
| 123 | } |
| 124 | |
| 125 | void CConn::socketEvent(int fd, void *data) |
| 126 | { |
| 127 | CConn *cc; |
| 128 | static bool recursing = false; |
| 129 | |
| 130 | assert(data); |
| 131 | cc = (CConn*)data; |
| 132 | |
| 133 | // I don't think processMsg() is recursion safe, so add this check |
| 134 | if (recursing) |
| 135 | return; |
| 136 | |
| 137 | recursing = true; |
| 138 | |
| 139 | try { |
| 140 | // processMsg() only processes one message, so we need to loop |
| 141 | // until the buffers are empty or things will stall. |
| 142 | do { |
| 143 | cc->processMsg(); |
| 144 | } while (cc->sock->inStream().checkNoWait(1)); |
| 145 | } catch (rdr::EndOfStream& e) { |
| 146 | vlog.info(e.str()); |
| 147 | exit_vncviewer(); |
| 148 | } catch (rdr::Exception& e) { |
| 149 | vlog.error(e.str()); |
| 150 | fl_alert(e.str()); |
| 151 | exit_vncviewer(); |
| 152 | } |
| 153 | |
| 154 | recursing = false; |
| 155 | } |
| 156 | |
| 157 | ////////////////////// CConnection callback methods ////////////////////// |
| 158 | |
| 159 | // serverInit() is called when the serverInit message has been received. At |
| 160 | // this point we create the desktop window and display it. We also tell the |
| 161 | // server the pixel format and encodings to use and request the first update. |
| 162 | void CConn::serverInit() |
| 163 | { |
| 164 | CConnection::serverInit(); |
| 165 | |
| 166 | // If using AutoSelect with old servers, start in FullColor |
| 167 | // mode. See comment in autoSelectFormatAndEncoding. |
| 168 | if (cp.beforeVersion(3, 8) && autoSelect) |
| 169 | fullColour.setParam(true); |
| 170 | |
| 171 | serverPF = cp.pf(); |
| 172 | |
| 173 | desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this); |
| 174 | fullColourPF = desktop->getPreferredPF(); |
| 175 | |
| 176 | formatChange = encodingChange = true; |
| 177 | requestNewUpdate(); |
| 178 | } |
| 179 | |
| 180 | // setDesktopSize() is called when the desktop size changes (including when |
| 181 | // it is set initially). |
| 182 | void CConn::setDesktopSize(int w, int h) |
| 183 | { |
| 184 | CConnection::setDesktopSize(w,h); |
| 185 | resizeFramebuffer(); |
| 186 | } |
| 187 | |
| 188 | // setExtendedDesktopSize() is a more advanced version of setDesktopSize() |
| 189 | void CConn::setExtendedDesktopSize(int reason, int result, int w, int h, |
| 190 | const rfb::ScreenSet& layout) |
| 191 | { |
| 192 | CConnection::setExtendedDesktopSize(reason, result, w, h, layout); |
| 193 | |
| 194 | if ((reason == reasonClient) && (result != resultSuccess)) { |
| 195 | vlog.error(_("SetDesktopSize failed: %d"), result); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | resizeFramebuffer(); |
| 200 | } |
| 201 | |
| 202 | // setName() is called when the desktop name changes |
| 203 | void CConn::setName(const char* name) |
| 204 | { |
| 205 | CConnection::setName(name); |
| 206 | if (desktop) |
| 207 | desktop->setName(name); |
| 208 | } |
| 209 | |
| 210 | // framebufferUpdateStart() is called at the beginning of an update. |
| 211 | // Here we try to send out a new framebuffer update request so that the |
| 212 | // next update can be sent out in parallel with us decoding the current |
| 213 | // one. We cannot do this if we're in the middle of a format change |
| 214 | // though. |
| 215 | void CConn::framebufferUpdateStart() |
| 216 | { |
| 217 | if (!formatChange) { |
| 218 | pendingUpdate = true; |
| 219 | requestNewUpdate(); |
| 220 | } else |
| 221 | pendingUpdate = false; |
| 222 | } |
| 223 | |
| 224 | // framebufferUpdateEnd() is called at the end of an update. |
| 225 | // For each rectangle, the FdInStream will have timed the speed |
| 226 | // of the connection, allowing us to select format and encoding |
| 227 | // appropriately, and then request another incremental update. |
| 228 | void CConn::framebufferUpdateEnd() |
| 229 | { |
| 230 | desktop->updateWindow(); |
| 231 | |
| 232 | if (firstUpdate) { |
| 233 | int width, height; |
| 234 | |
| 235 | if (cp.supportsSetDesktopSize && |
| 236 | sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) == 2) { |
| 237 | ScreenSet layout; |
| 238 | |
| 239 | layout = cp.screenLayout; |
| 240 | |
| 241 | if (layout.num_screens() == 0) |
| 242 | layout.add_screen(rfb::Screen()); |
| 243 | else if (layout.num_screens() != 1) { |
| 244 | ScreenSet::iterator iter; |
| 245 | |
| 246 | while (true) { |
| 247 | iter = layout.begin(); |
| 248 | ++iter; |
| 249 | |
| 250 | if (iter == layout.end()) |
| 251 | break; |
| 252 | |
| 253 | layout.remove_screen(iter->id); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | layout.begin()->dimensions.tl.x = 0; |
| 258 | layout.begin()->dimensions.tl.y = 0; |
| 259 | layout.begin()->dimensions.br.x = width; |
| 260 | layout.begin()->dimensions.br.y = height; |
| 261 | |
| 262 | writer()->writeSetDesktopSize(width, height, layout); |
| 263 | } |
| 264 | |
| 265 | firstUpdate = false; |
| 266 | } |
| 267 | |
| 268 | // A format change prevented us from sending this before the update, |
| 269 | // so make sure to send it now. |
| 270 | if (formatChange && !pendingUpdate) |
| 271 | requestNewUpdate(); |
| 272 | |
| 273 | // Compute new settings based on updated bandwidth values |
| 274 | if (autoSelect) |
| 275 | autoSelectFormatAndEncoding(); |
| 276 | |
| 277 | // Make sure that the FLTK handling and the timers gets some CPU time |
| 278 | // in case of back to back framebuffer updates. |
| 279 | Fl::check(); |
| 280 | Timer::checkTimeouts(); |
| 281 | } |
| 282 | |
| 283 | // The rest of the callbacks are fairly self-explanatory... |
| 284 | |
| 285 | void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs) |
| 286 | { |
| 287 | desktop->setColourMapEntries(firstColour, nColours, rgbs); |
| 288 | } |
| 289 | |
| 290 | void CConn::bell() |
| 291 | { |
| 292 | fl_beep(); |
| 293 | } |
| 294 | |
| 295 | void CConn::serverCutText(const char* str, rdr::U32 len) |
| 296 | { |
| 297 | // desktop->serverCutText(str,len); |
| 298 | } |
| 299 | |
| 300 | // We start timing on beginRect and stop timing on endRect, to |
| 301 | // avoid skewing the bandwidth estimation as a result of the server |
| 302 | // being slow or the network having high latency |
| 303 | void CConn::beginRect(const Rect& r, int encoding) |
| 304 | { |
| 305 | sock->inStream().startTiming(); |
| 306 | if (encoding != encodingCopyRect) { |
| 307 | lastServerEncoding = encoding; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | void CConn::endRect(const Rect& r, int encoding) |
| 312 | { |
| 313 | sock->inStream().stopTiming(); |
| 314 | } |
| 315 | |
| 316 | void CConn::fillRect(const rfb::Rect& r, rfb::Pixel p) |
| 317 | { |
| 318 | desktop->fillRect(r,p); |
| 319 | } |
| 320 | void CConn::imageRect(const rfb::Rect& r, void* p) |
| 321 | { |
| 322 | desktop->imageRect(r,p); |
| 323 | } |
| 324 | void CConn::copyRect(const rfb::Rect& r, int sx, int sy) |
| 325 | { |
| 326 | desktop->copyRect(r,sx,sy); |
| 327 | } |
| 328 | void CConn::setCursor(int width, int height, const Point& hotspot, |
| 329 | void* data, void* mask) |
| 330 | { |
| 331 | // desktop->setCursor(width, height, hotspot, data, mask); |
| 332 | } |
| 333 | |
| 334 | ////////////////////// Internal methods ////////////////////// |
| 335 | |
| 336 | void CConn::resizeFramebuffer() |
| 337 | { |
| 338 | /* |
| 339 | if (!desktop) |
| 340 | return; |
| 341 | if ((desktop->width() == cp.width) && (desktop->height() == cp.height)) |
| 342 | return; |
| 343 | |
| 344 | desktop->resize(cp.width, cp.height); |
| 345 | */ |
| 346 | } |
| 347 | |
| 348 | // autoSelectFormatAndEncoding() chooses the format and encoding appropriate |
| 349 | // to the connection speed: |
| 350 | // |
| 351 | // First we wait for at least one second of bandwidth measurement. |
| 352 | // |
| 353 | // Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality, |
| 354 | // which should be perceptually lossless. |
| 355 | // |
| 356 | // If the bandwidth is below that, we choose a more lossy JPEG quality. |
| 357 | // |
| 358 | // If the bandwidth drops below 256 Kbps, we switch to palette mode. |
| 359 | // |
| 360 | // Note: The system here is fairly arbitrary and should be replaced |
| 361 | // with something more intelligent at the server end. |
| 362 | // |
| 363 | void CConn::autoSelectFormatAndEncoding() |
| 364 | { |
| 365 | int kbitsPerSecond = sock->inStream().kbitsPerSecond(); |
| 366 | unsigned int timeWaited = sock->inStream().timeWaited(); |
| 367 | bool newFullColour = fullColour; |
| 368 | int newQualityLevel = qualityLevel; |
| 369 | |
| 370 | // Always use Tight |
| 371 | if (currentEncoding != encodingTight) { |
| 372 | currentEncoding = encodingTight; |
| 373 | encodingChange = true; |
| 374 | } |
| 375 | |
| 376 | // Check that we have a decent bandwidth measurement |
| 377 | if ((kbitsPerSecond == 0) || (timeWaited < 10000)) |
| 378 | return; |
| 379 | |
| 380 | // Select appropriate quality level |
| 381 | if (!noJpeg) { |
| 382 | if (kbitsPerSecond > 16000) |
| 383 | newQualityLevel = 8; |
| 384 | else |
| 385 | newQualityLevel = 6; |
| 386 | |
| 387 | if (newQualityLevel != qualityLevel) { |
| 388 | vlog.info(_("Throughput %d kbit/s - changing to quality %d"), |
| 389 | kbitsPerSecond, newQualityLevel); |
| 390 | cp.qualityLevel = newQualityLevel; |
| 391 | qualityLevel.setParam(newQualityLevel); |
| 392 | encodingChange = true; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | if (cp.beforeVersion(3, 8)) { |
| 397 | // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with |
| 398 | // cursors "asynchronously". If this happens in the middle of a |
| 399 | // pixel format change, the server will encode the cursor with |
| 400 | // the old format, but the client will try to decode it |
| 401 | // according to the new format. This will lead to a |
| 402 | // crash. Therefore, we do not allow automatic format change for |
| 403 | // old servers. |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | // Select best color level |
| 408 | newFullColour = (kbitsPerSecond > 256); |
| 409 | if (newFullColour != fullColour) { |
| 410 | vlog.info(_("Throughput %d kbit/s - full color is now %s"), |
| 411 | kbitsPerSecond, |
| 412 | newFullColour ? _("enabled") : _("disabled")); |
| 413 | fullColour.setParam(newFullColour); |
| 414 | formatChange = true; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | // checkEncodings() sends a setEncodings message if one is needed. |
| 419 | void CConn::checkEncodings() |
| 420 | { |
| 421 | if (encodingChange && writer()) { |
| 422 | vlog.info(_("Using %s encoding"),encodingName(currentEncoding)); |
| 423 | writer()->writeSetEncodings(currentEncoding, true); |
| 424 | encodingChange = false; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // requestNewUpdate() requests an update from the server, having set the |
| 429 | // format and encoding appropriately. |
| 430 | void CConn::requestNewUpdate() |
| 431 | { |
| 432 | if (formatChange) { |
| 433 | PixelFormat pf; |
| 434 | |
| 435 | /* Catch incorrect requestNewUpdate calls */ |
| 436 | assert(pendingUpdate == false); |
| 437 | |
| 438 | if (fullColour) { |
| 439 | pf = fullColourPF; |
| 440 | } else { |
| 441 | if (lowColourLevel == 0) |
| 442 | pf = PixelFormat(8,3,0,1,1,1,1,2,1,0); |
| 443 | else if (lowColourLevel == 1) |
| 444 | pf = PixelFormat(8,6,0,1,3,3,3,4,2,0); |
| 445 | else |
| 446 | pf = PixelFormat(8,8,0,0); |
| 447 | } |
| 448 | char str[256]; |
| 449 | pf.print(str, 256); |
| 450 | vlog.info(_("Using pixel format %s"),str); |
| 451 | desktop->setServerPF(pf); |
| 452 | cp.setPF(pf); |
| 453 | writer()->writeSetPixelFormat(pf); |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 454 | |
| 455 | forceNonincremental = true; |
| 456 | |
| 457 | formatChange = false; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 458 | } |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 459 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 460 | checkEncodings(); |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 461 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 462 | writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height), |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 463 | !forceNonincremental); |
| 464 | |
| 465 | forceNonincremental = false; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 466 | } |