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