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> |
| 36 | #include <rfb/Timer.h> |
| 37 | #include <network/TcpSocket.h> |
| 38 | |
| 39 | #include <FL/Fl.H> |
| 40 | #include <FL/fl_ask.H> |
| 41 | |
| 42 | #include "CConn.h" |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 43 | #include "OptionsDialog.h" |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 44 | #include "i18n.h" |
| 45 | #include "parameters.h" |
Pierre Ossman | 39ceb50 | 2011-07-12 15:54:25 +0000 | [diff] [blame] | 46 | #include "vncviewer.h" |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 47 | |
DRC | b65bb93 | 2011-06-24 03:17:00 +0000 | [diff] [blame] | 48 | #ifdef WIN32 |
| 49 | #include "win32.h" |
| 50 | #endif |
| 51 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 52 | using namespace rdr; |
| 53 | using namespace rfb; |
| 54 | using namespace std; |
| 55 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 56 | static rfb::LogWriter vlog("CConn"); |
| 57 | |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 58 | // 8 colours (1 bit per component) |
| 59 | static const PixelFormat verylowColourPF(8, 3,false, true, |
| 60 | 1, 1, 1, 2, 1, 0); |
| 61 | // 64 colours (2 bits per component) |
| 62 | static const PixelFormat lowColourPF(8, 6, false, true, |
| 63 | 3, 3, 3, 4, 2, 0); |
| 64 | // 256 colours (palette) |
| 65 | static const PixelFormat mediumColourPF(8, 8, false, false); |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 66 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 67 | CConn::CConn(const char* vncServerName) |
| 68 | : serverHost(0), serverPort(0), sock(NULL), desktop(NULL), |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 69 | pendingPFChange(false), |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 70 | currentEncoding(encodingTight), lastServerEncoding((unsigned int)-1), |
| 71 | formatChange(false), encodingChange(false), |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 72 | firstUpdate(true), pendingUpdate(false), |
Pierre Ossman | fba3e86 | 2011-07-15 13:45:19 +0000 | [diff] [blame] | 73 | forceNonincremental(true) |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 74 | { |
| 75 | setShared(::shared); |
| 76 | |
| 77 | int encNum = encodingNum(preferredEncoding); |
| 78 | if (encNum != -1) |
| 79 | currentEncoding = encNum; |
| 80 | |
Pierre Ossman | da38956 | 2011-06-09 08:24:37 +0000 | [diff] [blame] | 81 | cp.supportsLocalCursor = true; |
| 82 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 83 | cp.supportsDesktopResize = true; |
| 84 | cp.supportsExtendedDesktopSize = true; |
| 85 | cp.supportsDesktopRename = true; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 86 | |
| 87 | cp.customCompressLevel = customCompressLevel; |
| 88 | cp.compressLevel = compressLevel; |
| 89 | |
| 90 | cp.noJpeg = noJpeg; |
| 91 | cp.qualityLevel = qualityLevel; |
| 92 | |
| 93 | try { |
| 94 | getHostAndPort(vncServerName, &serverHost, &serverPort); |
| 95 | |
| 96 | sock = new network::TcpSocket(serverHost, serverPort); |
| 97 | vlog.info(_("connected to host %s port %d"), serverHost, serverPort); |
| 98 | } catch (rdr::Exception& e) { |
| 99 | vlog.error(e.str()); |
| 100 | fl_alert(e.str()); |
| 101 | exit_vncviewer(); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | Fl::add_fd(sock->getFd(), FL_READ | FL_EXCEPT, socketEvent, this); |
| 106 | |
| 107 | // See callback below |
| 108 | sock->inStream().setBlockCallback(this); |
| 109 | |
| 110 | setServerName(serverHost); |
| 111 | setStreams(&sock->inStream(), &sock->outStream()); |
| 112 | |
| 113 | initialiseProtocol(); |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 114 | |
| 115 | OptionsDialog::addCallback(handleOptions, this); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | CConn::~CConn() |
| 119 | { |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 120 | OptionsDialog::removeCallback(handleOptions); |
| 121 | |
Pierre Ossman | 6a9e2e6 | 2011-05-19 14:47:43 +0000 | [diff] [blame] | 122 | if (desktop) |
| 123 | delete desktop; |
| 124 | |
Pierre Ossman | 5e04c26 | 2011-11-09 11:31:12 +0000 | [diff] [blame^] | 125 | delete [] serverHost; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 126 | if (sock) |
| 127 | Fl::remove_fd(sock->getFd()); |
| 128 | delete sock; |
| 129 | } |
| 130 | |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 131 | void CConn::refreshFramebuffer() |
| 132 | { |
| 133 | // FIXME: We cannot safely trigger an update request directly but must |
| 134 | // wait for the next update to arrive. |
| 135 | if (!formatChange) |
| 136 | forceNonincremental = true; |
| 137 | } |
| 138 | |
Pierre Ossman | 2eb1d11 | 2011-05-16 12:18:08 +0000 | [diff] [blame] | 139 | const char *CConn::connectionInfo() |
| 140 | { |
| 141 | static char infoText[1024] = ""; |
| 142 | |
| 143 | char pfStr[100]; |
| 144 | char spfStr[100]; |
| 145 | |
| 146 | cp.pf().print(pfStr, 100); |
| 147 | serverPF.print(spfStr, 100); |
| 148 | |
| 149 | int secType = csecurity->getType(); |
| 150 | |
| 151 | snprintf(infoText, sizeof(infoText), |
| 152 | _("Desktop name: %.80s\n" |
| 153 | "Host: %.80s port: %d\n" |
| 154 | "Size: %d x %d\n" |
| 155 | "Pixel format: %s\n" |
| 156 | "(server default %s)\n" |
| 157 | "Requested encoding: %s\n" |
| 158 | "Last used encoding: %s\n" |
| 159 | "Line speed estimate: %d kbit/s\n" |
| 160 | "Protocol version: %d.%d\n" |
| 161 | "Security method: %s\n"), |
| 162 | cp.name(), serverHost, serverPort, cp.width, cp.height, |
| 163 | pfStr, spfStr, encodingName(currentEncoding), |
| 164 | encodingName(lastServerEncoding), |
| 165 | sock->inStream().kbitsPerSecond(), |
| 166 | cp.majorVersion, cp.minorVersion, |
| 167 | secTypeName(secType)); |
| 168 | |
| 169 | return infoText; |
| 170 | } |
| 171 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 172 | // The RFB core is not properly asynchronous, so it calls this callback |
| 173 | // whenever it needs to block to wait for more data. Since FLTK is |
| 174 | // monitoring the socket, we just make sure FLTK gets to run. |
| 175 | |
| 176 | void CConn::blockCallback() |
| 177 | { |
| 178 | int next_timer; |
| 179 | |
| 180 | next_timer = Timer::checkTimeouts(); |
| 181 | if (next_timer == 0) |
| 182 | next_timer = INT_MAX; |
| 183 | |
| 184 | Fl::wait((double)next_timer / 1000.0); |
| 185 | } |
| 186 | |
| 187 | void CConn::socketEvent(int fd, void *data) |
| 188 | { |
| 189 | CConn *cc; |
| 190 | static bool recursing = false; |
| 191 | |
| 192 | assert(data); |
| 193 | cc = (CConn*)data; |
| 194 | |
| 195 | // I don't think processMsg() is recursion safe, so add this check |
| 196 | if (recursing) |
| 197 | return; |
| 198 | |
| 199 | recursing = true; |
| 200 | |
| 201 | try { |
| 202 | // processMsg() only processes one message, so we need to loop |
| 203 | // until the buffers are empty or things will stall. |
| 204 | do { |
| 205 | cc->processMsg(); |
| 206 | } while (cc->sock->inStream().checkNoWait(1)); |
| 207 | } catch (rdr::EndOfStream& e) { |
| 208 | vlog.info(e.str()); |
| 209 | exit_vncviewer(); |
| 210 | } catch (rdr::Exception& e) { |
| 211 | vlog.error(e.str()); |
Pierre Ossman | e2ef5c1 | 2011-07-12 16:56:34 +0000 | [diff] [blame] | 212 | exit_vncviewer(e.str()); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | recursing = false; |
| 216 | } |
| 217 | |
| 218 | ////////////////////// CConnection callback methods ////////////////////// |
| 219 | |
| 220 | // serverInit() is called when the serverInit message has been received. At |
| 221 | // this point we create the desktop window and display it. We also tell the |
| 222 | // server the pixel format and encodings to use and request the first update. |
| 223 | void CConn::serverInit() |
| 224 | { |
| 225 | CConnection::serverInit(); |
| 226 | |
| 227 | // If using AutoSelect with old servers, start in FullColor |
| 228 | // mode. See comment in autoSelectFormatAndEncoding. |
| 229 | if (cp.beforeVersion(3, 8) && autoSelect) |
| 230 | fullColour.setParam(true); |
| 231 | |
| 232 | serverPF = cp.pf(); |
| 233 | |
| 234 | desktop = new DesktopWindow(cp.width, cp.height, cp.name(), serverPF, this); |
| 235 | fullColourPF = desktop->getPreferredPF(); |
| 236 | |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 237 | // Force a switch to the format and encoding we'd like |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 238 | formatChange = encodingChange = true; |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 239 | |
| 240 | // And kick off the update cycle |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 241 | requestNewUpdate(); |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 242 | |
| 243 | // This initial update request is a bit of a corner case, so we need |
| 244 | // to help out setting the correct format here. |
| 245 | assert(pendingPFChange); |
| 246 | desktop->setServerPF(pendingPF); |
| 247 | cp.setPF(pendingPF); |
| 248 | pendingPFChange = false; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // setDesktopSize() is called when the desktop size changes (including when |
| 252 | // it is set initially). |
| 253 | void CConn::setDesktopSize(int w, int h) |
| 254 | { |
| 255 | CConnection::setDesktopSize(w,h); |
| 256 | resizeFramebuffer(); |
| 257 | } |
| 258 | |
| 259 | // setExtendedDesktopSize() is a more advanced version of setDesktopSize() |
| 260 | void CConn::setExtendedDesktopSize(int reason, int result, int w, int h, |
| 261 | const rfb::ScreenSet& layout) |
| 262 | { |
| 263 | CConnection::setExtendedDesktopSize(reason, result, w, h, layout); |
| 264 | |
| 265 | if ((reason == reasonClient) && (result != resultSuccess)) { |
| 266 | vlog.error(_("SetDesktopSize failed: %d"), result); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | resizeFramebuffer(); |
| 271 | } |
| 272 | |
| 273 | // setName() is called when the desktop name changes |
| 274 | void CConn::setName(const char* name) |
| 275 | { |
| 276 | CConnection::setName(name); |
| 277 | if (desktop) |
| 278 | desktop->setName(name); |
| 279 | } |
| 280 | |
| 281 | // framebufferUpdateStart() is called at the beginning of an update. |
| 282 | // Here we try to send out a new framebuffer update request so that the |
| 283 | // 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] | 284 | // one. |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 285 | void CConn::framebufferUpdateStart() |
| 286 | { |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 287 | pendingUpdate = false; |
| 288 | |
| 289 | requestNewUpdate(); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // framebufferUpdateEnd() is called at the end of an update. |
| 293 | // For each rectangle, the FdInStream will have timed the speed |
| 294 | // of the connection, allowing us to select format and encoding |
| 295 | // appropriately, and then request another incremental update. |
| 296 | void CConn::framebufferUpdateEnd() |
| 297 | { |
| 298 | desktop->updateWindow(); |
| 299 | |
| 300 | if (firstUpdate) { |
| 301 | int width, height; |
| 302 | |
| 303 | if (cp.supportsSetDesktopSize && |
| 304 | sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) == 2) { |
| 305 | ScreenSet layout; |
| 306 | |
| 307 | layout = cp.screenLayout; |
| 308 | |
| 309 | if (layout.num_screens() == 0) |
| 310 | layout.add_screen(rfb::Screen()); |
| 311 | else if (layout.num_screens() != 1) { |
| 312 | ScreenSet::iterator iter; |
| 313 | |
| 314 | while (true) { |
| 315 | iter = layout.begin(); |
| 316 | ++iter; |
| 317 | |
| 318 | if (iter == layout.end()) |
| 319 | break; |
| 320 | |
| 321 | layout.remove_screen(iter->id); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | layout.begin()->dimensions.tl.x = 0; |
| 326 | layout.begin()->dimensions.tl.y = 0; |
| 327 | layout.begin()->dimensions.br.x = width; |
| 328 | layout.begin()->dimensions.br.y = height; |
| 329 | |
| 330 | writer()->writeSetDesktopSize(width, height, layout); |
| 331 | } |
| 332 | |
| 333 | firstUpdate = false; |
| 334 | } |
| 335 | |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 336 | // A format change has been scheduled and we are now past the update |
| 337 | // with the old format. Time to active the new one. |
| 338 | if (pendingPFChange) { |
| 339 | desktop->setServerPF(pendingPF); |
| 340 | cp.setPF(pendingPF); |
| 341 | pendingPFChange = false; |
| 342 | } |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 343 | |
| 344 | // Compute new settings based on updated bandwidth values |
| 345 | if (autoSelect) |
| 346 | autoSelectFormatAndEncoding(); |
| 347 | |
| 348 | // Make sure that the FLTK handling and the timers gets some CPU time |
| 349 | // in case of back to back framebuffer updates. |
| 350 | Fl::check(); |
| 351 | Timer::checkTimeouts(); |
| 352 | } |
| 353 | |
| 354 | // The rest of the callbacks are fairly self-explanatory... |
| 355 | |
| 356 | void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs) |
| 357 | { |
| 358 | desktop->setColourMapEntries(firstColour, nColours, rgbs); |
| 359 | } |
| 360 | |
| 361 | void CConn::bell() |
| 362 | { |
| 363 | fl_beep(); |
| 364 | } |
| 365 | |
| 366 | void CConn::serverCutText(const char* str, rdr::U32 len) |
| 367 | { |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 368 | char *buffer; |
| 369 | int size, ret; |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 370 | |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 371 | size = fl_utf8froma(NULL, 0, str, len); |
| 372 | if (size <= 0) |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 373 | return; |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 374 | |
| 375 | size++; |
| 376 | |
| 377 | buffer = new char[size]; |
| 378 | |
| 379 | ret = fl_utf8froma(buffer, size, str, len); |
| 380 | assert(ret < size); |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 381 | |
| 382 | vlog.debug("Got clipboard data: '%s'", buffer); |
| 383 | |
Pierre Ossman | cf2443c | 2011-09-07 09:01:20 +0000 | [diff] [blame] | 384 | // RFB doesn't have separate selection and clipboard concepts, so we |
| 385 | // dump the data into both variants. |
| 386 | Fl::copy(buffer, ret, 0); |
Pierre Ossman | d81e8f4 | 2011-05-19 14:47:15 +0000 | [diff] [blame] | 387 | Fl::copy(buffer, ret, 1); |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 388 | |
| 389 | delete [] buffer; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // We start timing on beginRect and stop timing on endRect, to |
| 393 | // avoid skewing the bandwidth estimation as a result of the server |
| 394 | // being slow or the network having high latency |
| 395 | void CConn::beginRect(const Rect& r, int encoding) |
| 396 | { |
| 397 | sock->inStream().startTiming(); |
| 398 | if (encoding != encodingCopyRect) { |
| 399 | lastServerEncoding = encoding; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | void CConn::endRect(const Rect& r, int encoding) |
| 404 | { |
| 405 | sock->inStream().stopTiming(); |
| 406 | } |
| 407 | |
| 408 | void CConn::fillRect(const rfb::Rect& r, rfb::Pixel p) |
| 409 | { |
| 410 | desktop->fillRect(r,p); |
| 411 | } |
| 412 | void CConn::imageRect(const rfb::Rect& r, void* p) |
| 413 | { |
| 414 | desktop->imageRect(r,p); |
| 415 | } |
| 416 | void CConn::copyRect(const rfb::Rect& r, int sx, int sy) |
| 417 | { |
| 418 | desktop->copyRect(r,sx,sy); |
| 419 | } |
| 420 | void CConn::setCursor(int width, int height, const Point& hotspot, |
| 421 | void* data, void* mask) |
| 422 | { |
Pierre Ossman | 835b4ef | 2011-06-08 17:02:36 +0000 | [diff] [blame] | 423 | desktop->setCursor(width, height, hotspot, data, mask); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 424 | } |
| 425 | |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 426 | rdr::U8* CConn::getRawPixelsRW(const rfb::Rect& r, int* stride) { |
| 427 | return desktop->getPixelsRW(r, stride); |
| 428 | } |
| 429 | void CConn::releaseRawPixels(const rfb::Rect& r) { |
| 430 | desktop->damageRect(r); |
| 431 | } |
| 432 | |
| 433 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 434 | ////////////////////// Internal methods ////////////////////// |
| 435 | |
| 436 | void CConn::resizeFramebuffer() |
| 437 | { |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 438 | if (!desktop) |
| 439 | return; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 440 | |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 441 | desktop->resizeFramebuffer(cp.width, cp.height); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // autoSelectFormatAndEncoding() chooses the format and encoding appropriate |
| 445 | // to the connection speed: |
| 446 | // |
| 447 | // First we wait for at least one second of bandwidth measurement. |
| 448 | // |
| 449 | // Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality, |
| 450 | // which should be perceptually lossless. |
| 451 | // |
| 452 | // If the bandwidth is below that, we choose a more lossy JPEG quality. |
| 453 | // |
| 454 | // If the bandwidth drops below 256 Kbps, we switch to palette mode. |
| 455 | // |
| 456 | // Note: The system here is fairly arbitrary and should be replaced |
| 457 | // with something more intelligent at the server end. |
| 458 | // |
| 459 | void CConn::autoSelectFormatAndEncoding() |
| 460 | { |
| 461 | int kbitsPerSecond = sock->inStream().kbitsPerSecond(); |
| 462 | unsigned int timeWaited = sock->inStream().timeWaited(); |
| 463 | bool newFullColour = fullColour; |
| 464 | int newQualityLevel = qualityLevel; |
| 465 | |
| 466 | // Always use Tight |
| 467 | if (currentEncoding != encodingTight) { |
| 468 | currentEncoding = encodingTight; |
| 469 | encodingChange = true; |
| 470 | } |
| 471 | |
| 472 | // Check that we have a decent bandwidth measurement |
| 473 | if ((kbitsPerSecond == 0) || (timeWaited < 10000)) |
| 474 | return; |
| 475 | |
| 476 | // Select appropriate quality level |
| 477 | if (!noJpeg) { |
| 478 | if (kbitsPerSecond > 16000) |
| 479 | newQualityLevel = 8; |
| 480 | else |
| 481 | newQualityLevel = 6; |
| 482 | |
| 483 | if (newQualityLevel != qualityLevel) { |
| 484 | vlog.info(_("Throughput %d kbit/s - changing to quality %d"), |
| 485 | kbitsPerSecond, newQualityLevel); |
| 486 | cp.qualityLevel = newQualityLevel; |
| 487 | qualityLevel.setParam(newQualityLevel); |
| 488 | encodingChange = true; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | if (cp.beforeVersion(3, 8)) { |
| 493 | // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with |
| 494 | // cursors "asynchronously". If this happens in the middle of a |
| 495 | // pixel format change, the server will encode the cursor with |
| 496 | // the old format, but the client will try to decode it |
| 497 | // according to the new format. This will lead to a |
| 498 | // crash. Therefore, we do not allow automatic format change for |
| 499 | // old servers. |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | // Select best color level |
| 504 | newFullColour = (kbitsPerSecond > 256); |
| 505 | if (newFullColour != fullColour) { |
| 506 | vlog.info(_("Throughput %d kbit/s - full color is now %s"), |
| 507 | kbitsPerSecond, |
| 508 | newFullColour ? _("enabled") : _("disabled")); |
| 509 | fullColour.setParam(newFullColour); |
| 510 | formatChange = true; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | // checkEncodings() sends a setEncodings message if one is needed. |
| 515 | void CConn::checkEncodings() |
| 516 | { |
| 517 | if (encodingChange && writer()) { |
| 518 | vlog.info(_("Using %s encoding"),encodingName(currentEncoding)); |
| 519 | writer()->writeSetEncodings(currentEncoding, true); |
| 520 | encodingChange = false; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // requestNewUpdate() requests an update from the server, having set the |
| 525 | // format and encoding appropriately. |
| 526 | void CConn::requestNewUpdate() |
| 527 | { |
| 528 | if (formatChange) { |
| 529 | PixelFormat pf; |
| 530 | |
| 531 | /* Catch incorrect requestNewUpdate calls */ |
| 532 | assert(pendingUpdate == false); |
| 533 | |
| 534 | if (fullColour) { |
| 535 | pf = fullColourPF; |
| 536 | } else { |
| 537 | if (lowColourLevel == 0) |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 538 | pf = verylowColourPF; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 539 | else if (lowColourLevel == 1) |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 540 | pf = lowColourPF; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 541 | else |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 542 | pf = mediumColourPF; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 543 | } |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 544 | |
| 545 | // New requests are sent out at the start of processing the last |
| 546 | // one, so we cannot switch our internal format right now (doing so |
| 547 | // would mean misdecoding the current update). |
| 548 | pendingPFChange = true; |
| 549 | pendingPF = pf; |
| 550 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 551 | char str[256]; |
| 552 | pf.print(str, 256); |
| 553 | vlog.info(_("Using pixel format %s"),str); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 554 | writer()->writeSetPixelFormat(pf); |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 555 | |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 556 | formatChange = false; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 557 | } |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 558 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 559 | checkEncodings(); |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 560 | |
Pierre Ossman | 5d512c3 | 2011-11-04 11:42:16 +0000 | [diff] [blame] | 561 | pendingUpdate = true; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 562 | writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height), |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 563 | !forceNonincremental); |
| 564 | |
| 565 | forceNonincremental = false; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 566 | } |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 567 | |
| 568 | void CConn::handleOptions(void *data) |
| 569 | { |
| 570 | CConn *self = (CConn*)data; |
| 571 | |
| 572 | // Checking all the details of the current set of encodings is just |
| 573 | // a pain. Assume something has changed, as resending the encoding |
| 574 | // list is cheap. Avoid overriding what the auto logic has selected |
| 575 | // though. |
| 576 | if (!autoSelect) { |
| 577 | int encNum = encodingNum(preferredEncoding); |
| 578 | |
| 579 | if (encNum != -1) |
| 580 | self->currentEncoding = encNum; |
| 581 | |
| 582 | self->cp.qualityLevel = qualityLevel; |
| 583 | } |
| 584 | |
Pierre Ossman | da38956 | 2011-06-09 08:24:37 +0000 | [diff] [blame] | 585 | self->cp.supportsLocalCursor = true; |
| 586 | |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 587 | self->cp.customCompressLevel = customCompressLevel; |
| 588 | self->cp.compressLevel = compressLevel; |
| 589 | |
| 590 | self->cp.noJpeg = noJpeg; |
| 591 | |
| 592 | self->encodingChange = true; |
| 593 | |
| 594 | // Format changes refreshes the entire screen though and are therefore |
| 595 | // very costly. It's probably worth the effort to see if it is necessary |
| 596 | // here. |
| 597 | PixelFormat pf; |
| 598 | |
| 599 | if (fullColour) { |
| 600 | pf = self->fullColourPF; |
| 601 | } else { |
| 602 | if (lowColourLevel == 0) |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 603 | pf = verylowColourPF; |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 604 | else if (lowColourLevel == 1) |
| 605 | pf = lowColourPF; |
| 606 | else |
Pierre Ossman | cf836f2 | 2011-07-14 14:34:09 +0000 | [diff] [blame] | 607 | pf = mediumColourPF; |
Pierre Ossman | f4f3094 | 2011-05-17 09:39:07 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | if (!pf.equal(self->cp.pf())) |
| 611 | self->formatChange = true; |
| 612 | } |