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