Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | 8c3bd69 | 2018-03-22 15:58:54 +0100 | [diff] [blame] | 2 | * Copyright 2009-2018 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This software is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this software; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
| 19 | |
Pierre Ossman | a830bec | 2011-11-08 12:12:02 +0000 | [diff] [blame] | 20 | #include <network/TcpSocket.h> |
Pierre Ossman | 707fa12 | 2015-12-11 20:21:20 +0100 | [diff] [blame] | 21 | |
| 22 | #include <rfb/ComparingUpdateTracker.h> |
| 23 | #include <rfb/Encoder.h> |
| 24 | #include <rfb/KeyRemapper.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 25 | #include <rfb/LogWriter.h> |
Adam Tkac | 5a0caed | 2010-04-23 13:58:10 +0000 | [diff] [blame] | 26 | #include <rfb/Security.h> |
Pierre Ossman | 707fa12 | 2015-12-11 20:21:20 +0100 | [diff] [blame] | 27 | #include <rfb/ServerCore.h> |
| 28 | #include <rfb/SMsgWriter.h> |
| 29 | #include <rfb/VNCServerST.h> |
| 30 | #include <rfb/VNCSConnectionST.h> |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 31 | #include <rfb/screenTypes.h> |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 32 | #include <rfb/fenceTypes.h> |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 33 | #include <rfb/ledStates.h> |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 34 | #define XK_LATIN1 |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 35 | #define XK_MISCELLANY |
| 36 | #define XK_XKB_KEYS |
| 37 | #include <rfb/keysymdef.h> |
| 38 | |
| 39 | using namespace rfb; |
| 40 | |
| 41 | static LogWriter vlog("VNCSConnST"); |
| 42 | |
Pierre Ossman | 71ca8d5 | 2017-09-15 11:03:12 +0200 | [diff] [blame] | 43 | static Cursor emptyCursor(0, 0, Point(0, 0), NULL); |
| 44 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 45 | VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s, |
| 46 | bool reverse) |
Pierre Ossman | 7069bdd | 2015-02-06 14:41:58 +0100 | [diff] [blame] | 47 | : sock(s), reverseConnection(reverse), |
Pierre Ossman | 3630475 | 2017-10-04 16:21:57 +0200 | [diff] [blame] | 48 | inProcessMessages(false), |
Pierre Ossman | b8b1e96 | 2012-07-20 10:47:00 +0000 | [diff] [blame] | 49 | pendingSyncFence(false), syncFence(false), fenceFlags(0), |
Pierre Ossman | a99d14d | 2015-12-13 15:43:46 +0100 | [diff] [blame] | 50 | fenceDataLen(0), fenceData(NULL), congestionTimer(this), |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 51 | server(server_), updates(false), |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 52 | updateRenderedCursor(false), removeRenderedCursor(false), |
Pierre Ossman | a40ab20 | 2016-04-29 15:35:56 +0200 | [diff] [blame] | 53 | continuousUpdates(false), encodeManager(this), pointerEventTime(0), |
Pierre Ossman | 25db44a | 2017-11-16 16:40:44 +0100 | [diff] [blame] | 54 | clientHasCursor(false), |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 55 | accessRights(AccessDefault), startTime(time(0)) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 56 | { |
| 57 | setStreams(&sock->inStream(), &sock->outStream()); |
| 58 | peerEndpoint.buf = sock->getPeerEndpoint(); |
| 59 | VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf); |
| 60 | |
| 61 | // Configure the socket |
| 62 | setSocketTimeouts(); |
| 63 | lastEventTime = time(0); |
| 64 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 65 | server->clients.push_front(this); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | VNCSConnectionST::~VNCSConnectionST() |
| 70 | { |
| 71 | // If we reach here then VNCServerST is deleting us! |
| 72 | VNCServerST::connectionsLog.write(1,"closed: %s (%s)", |
| 73 | peerEndpoint.buf, |
| 74 | (closeReason.buf) ? closeReason.buf : ""); |
| 75 | |
| 76 | // Release any keys the client still had pressed |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 77 | while (!pressedKeys.empty()) { |
| 78 | rdr::U32 keysym, keycode; |
| 79 | |
| 80 | keysym = pressedKeys.begin()->second; |
| 81 | keycode = pressedKeys.begin()->first; |
| 82 | pressedKeys.erase(pressedKeys.begin()); |
| 83 | |
| 84 | vlog.debug("Releasing key 0x%x / 0x%x on client disconnect", |
| 85 | keysym, keycode); |
| 86 | server->desktop->keyEvent(keysym, keycode, false); |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 87 | } |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 88 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 89 | if (server->pointerClient == this) |
| 90 | server->pointerClient = 0; |
| 91 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 92 | // Remove this client from the server |
| 93 | server->clients.remove(this); |
| 94 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 95 | delete [] fenceData; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | // Methods called from VNCServerST |
| 100 | |
| 101 | bool VNCSConnectionST::init() |
| 102 | { |
| 103 | try { |
| 104 | initialiseProtocol(); |
| 105 | } catch (rdr::Exception& e) { |
| 106 | close(e.str()); |
| 107 | return false; |
| 108 | } |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | void VNCSConnectionST::close(const char* reason) |
| 113 | { |
| 114 | // Log the reason for the close |
| 115 | if (!closeReason.buf) |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 116 | closeReason.buf = strDup(reason); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 117 | else |
| 118 | vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason); |
| 119 | |
| 120 | if (authenticated()) { |
| 121 | server->lastDisconnectTime = time(0); |
| 122 | } |
| 123 | |
| 124 | // Just shutdown the socket and mark our state as closing. Eventually the |
| 125 | // calling code will call VNCServerST's removeSocket() method causing us to |
| 126 | // be deleted. |
| 127 | sock->shutdown(); |
| 128 | setState(RFBSTATE_CLOSING); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | void VNCSConnectionST::processMessages() |
| 133 | { |
| 134 | if (state() == RFBSTATE_CLOSING) return; |
| 135 | try { |
| 136 | // - Now set appropriate socket timeouts and process data |
| 137 | setSocketTimeouts(); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 138 | |
| 139 | inProcessMessages = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 140 | |
Pierre Ossman | a830bec | 2011-11-08 12:12:02 +0000 | [diff] [blame] | 141 | // Get the underlying TCP layer to build large packets if we send |
| 142 | // multiple small responses. |
Peter Ã…strand (astrand) | 01dc1a6 | 2017-10-10 12:56:04 +0200 | [diff] [blame] | 143 | sock->cork(true); |
Pierre Ossman | a830bec | 2011-11-08 12:12:02 +0000 | [diff] [blame] | 144 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 145 | while (getInStream()->checkNoWait(1)) { |
Pierre Ossman | b8b1e96 | 2012-07-20 10:47:00 +0000 | [diff] [blame] | 146 | if (pendingSyncFence) { |
| 147 | syncFence = true; |
| 148 | pendingSyncFence = false; |
| 149 | } |
| 150 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 151 | processMsg(); |
Pierre Ossman | b8b1e96 | 2012-07-20 10:47:00 +0000 | [diff] [blame] | 152 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 153 | if (syncFence) { |
| 154 | writer()->writeFence(fenceFlags, fenceDataLen, fenceData); |
| 155 | syncFence = false; |
| 156 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Pierre Ossman | a830bec | 2011-11-08 12:12:02 +0000 | [diff] [blame] | 159 | // Flush out everything in case we go idle after this. |
Peter Ã…strand (astrand) | 01dc1a6 | 2017-10-10 12:56:04 +0200 | [diff] [blame] | 160 | sock->cork(false); |
Pierre Ossman | a830bec | 2011-11-08 12:12:02 +0000 | [diff] [blame] | 161 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 162 | inProcessMessages = false; |
Constantin Kaplinsky | 2ef6695 | 2008-08-29 11:33:46 +0000 | [diff] [blame] | 163 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 164 | // If there were anything requiring an update, try to send it here. |
| 165 | // We wait until now with this to aggregate responses and to give |
| 166 | // higher priority to user actions such as keyboard and pointer events. |
| 167 | writeFramebufferUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 168 | } catch (rdr::EndOfStream&) { |
| 169 | close("Clean disconnection"); |
| 170 | } catch (rdr::Exception &e) { |
| 171 | close(e.str()); |
| 172 | } |
| 173 | } |
| 174 | |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 175 | void VNCSConnectionST::flushSocket() |
| 176 | { |
| 177 | if (state() == RFBSTATE_CLOSING) return; |
| 178 | try { |
| 179 | setSocketTimeouts(); |
| 180 | sock->outStream().flush(); |
Pierre Ossman | a40ab20 | 2016-04-29 15:35:56 +0200 | [diff] [blame] | 181 | // Flushing the socket might release an update that was previously |
| 182 | // delayed because of congestion. |
| 183 | if (sock->outStream().bufferUsage() == 0) |
| 184 | writeFramebufferUpdate(); |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 185 | } catch (rdr::Exception &e) { |
| 186 | close(e.str()); |
| 187 | } |
| 188 | } |
| 189 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 190 | void VNCSConnectionST::pixelBufferChange() |
| 191 | { |
| 192 | try { |
| 193 | if (!authenticated()) return; |
| 194 | if (cp.width && cp.height && (server->pb->width() != cp.width || |
| 195 | server->pb->height() != cp.height)) |
| 196 | { |
| 197 | // We need to clip the next update to the new size, but also add any |
| 198 | // extra bits if it's bigger. If we wanted to do this exactly, something |
| 199 | // like the code below would do it, but at the moment we just update the |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 200 | // entire new size. However, we do need to clip the damagedCursorRegion |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 201 | // because that might be added to updates in writeFramebufferUpdate(). |
| 202 | |
| 203 | //updates.intersect(server->pb->getRect()); |
| 204 | // |
| 205 | //if (server->pb->width() > cp.width) |
| 206 | // updates.add_changed(Rect(cp.width, 0, server->pb->width(), |
| 207 | // server->pb->height())); |
| 208 | //if (server->pb->height() > cp.height) |
| 209 | // updates.add_changed(Rect(0, cp.height, cp.width, |
| 210 | // server->pb->height())); |
| 211 | |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 212 | damagedCursorRegion.assign_intersect(server->pb->getRect()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 213 | |
| 214 | cp.width = server->pb->width(); |
| 215 | cp.height = server->pb->height(); |
Pierre Ossman | 34e62f3 | 2009-03-20 21:46:12 +0000 | [diff] [blame] | 216 | cp.screenLayout = server->screenLayout; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 217 | if (state() == RFBSTATE_NORMAL) { |
Pierre Ossman | 2ee430a | 2009-05-28 12:54:24 +0000 | [diff] [blame] | 218 | // We should only send EDS to client asking for both |
| 219 | if (!writer()->writeExtendedDesktopSize()) { |
| 220 | if (!writer()->writeSetDesktopSize()) { |
| 221 | close("Client does not support desktop resize"); |
| 222 | return; |
| 223 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
Pierre Ossman | 6b2f113 | 2016-11-30 08:03:35 +0100 | [diff] [blame] | 226 | |
| 227 | // Drop any lossy tracking that is now outside the framebuffer |
| 228 | encodeManager.pruneLosslessRefresh(Region(server->pb->getRect())); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 229 | } |
| 230 | // Just update the whole screen at the moment because we're too lazy to |
| 231 | // work out what's actually changed. |
| 232 | updates.clear(); |
| 233 | updates.add_changed(server->pb->getRect()); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 234 | writeFramebufferUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 235 | } catch(rdr::Exception &e) { |
| 236 | close(e.str()); |
| 237 | } |
| 238 | } |
| 239 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 240 | void VNCSConnectionST::writeFramebufferUpdateOrClose() |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 241 | { |
| 242 | try { |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 243 | writeFramebufferUpdate(); |
| 244 | } catch(rdr::Exception &e) { |
| 245 | close(e.str()); |
| 246 | } |
| 247 | } |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 248 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 249 | void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason) |
| 250 | { |
| 251 | try { |
| 252 | screenLayoutChange(reason); |
Pierre Ossman | b218ecd | 2017-11-16 16:43:13 +0100 | [diff] [blame] | 253 | writeFramebufferUpdate(); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 254 | } catch(rdr::Exception &e) { |
| 255 | close(e.str()); |
| 256 | } |
| 257 | } |
| 258 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 259 | void VNCSConnectionST::bellOrClose() |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 260 | { |
| 261 | try { |
| 262 | if (state() == RFBSTATE_NORMAL) writer()->writeBell(); |
| 263 | } catch(rdr::Exception& e) { |
| 264 | close(e.str()); |
| 265 | } |
| 266 | } |
| 267 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 268 | void VNCSConnectionST::serverCutTextOrClose(const char *str, int len) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 269 | { |
| 270 | try { |
| 271 | if (!(accessRights & AccessCutText)) return; |
| 272 | if (!rfb::Server::sendCutText) return; |
| 273 | if (state() == RFBSTATE_NORMAL) |
| 274 | writer()->writeServerCutText(str, len); |
| 275 | } catch(rdr::Exception& e) { |
| 276 | close(e.str()); |
| 277 | } |
| 278 | } |
| 279 | |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 280 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 281 | void VNCSConnectionST::setDesktopNameOrClose(const char *name) |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 282 | { |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 283 | try { |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 284 | setDesktopName(name); |
Pierre Ossman | b218ecd | 2017-11-16 16:43:13 +0100 | [diff] [blame] | 285 | writeFramebufferUpdate(); |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 286 | } catch(rdr::Exception& e) { |
| 287 | close(e.str()); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 292 | void VNCSConnectionST::setCursorOrClose() |
| 293 | { |
| 294 | try { |
| 295 | setCursor(); |
Pierre Ossman | b218ecd | 2017-11-16 16:43:13 +0100 | [diff] [blame] | 296 | writeFramebufferUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 297 | } catch(rdr::Exception& e) { |
| 298 | close(e.str()); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 303 | void VNCSConnectionST::setLEDStateOrClose(unsigned int state) |
| 304 | { |
| 305 | try { |
| 306 | setLEDState(state); |
Pierre Ossman | b218ecd | 2017-11-16 16:43:13 +0100 | [diff] [blame] | 307 | writeFramebufferUpdate(); |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 308 | } catch(rdr::Exception& e) { |
| 309 | close(e.str()); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 314 | int VNCSConnectionST::checkIdleTimeout() |
| 315 | { |
| 316 | int idleTimeout = rfb::Server::idleTimeout; |
| 317 | if (idleTimeout == 0) return 0; |
| 318 | if (state() != RFBSTATE_NORMAL && idleTimeout < 15) |
| 319 | idleTimeout = 15; // minimum of 15 seconds while authenticating |
| 320 | time_t now = time(0); |
| 321 | if (now < lastEventTime) { |
| 322 | // Someone must have set the time backwards. Set lastEventTime so that the |
| 323 | // idleTimeout will count from now. |
| 324 | vlog.info("Time has gone backwards - resetting idle timeout"); |
| 325 | lastEventTime = now; |
| 326 | } |
| 327 | int timeLeft = lastEventTime + idleTimeout - now; |
| 328 | if (timeLeft < -60) { |
| 329 | // Our callback is over a minute late - someone must have set the time |
| 330 | // forwards. Set lastEventTime so that the idleTimeout will count from |
| 331 | // now. |
| 332 | vlog.info("Time has gone forwards - resetting idle timeout"); |
| 333 | lastEventTime = now; |
| 334 | return secsToMillis(idleTimeout); |
| 335 | } |
| 336 | if (timeLeft <= 0) { |
| 337 | close("Idle timeout"); |
| 338 | return 0; |
| 339 | } |
| 340 | return secsToMillis(timeLeft); |
| 341 | } |
| 342 | |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 343 | |
| 344 | bool VNCSConnectionST::getComparerState() |
| 345 | { |
| 346 | // We interpret a low compression level as an indication that the client |
| 347 | // wants to prioritise CPU usage over bandwidth, and hence disable the |
| 348 | // comparing update tracker. |
| 349 | return (cp.compressLevel == -1) || (cp.compressLevel > 1); |
| 350 | } |
| 351 | |
| 352 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 353 | // renderedCursorChange() is called whenever the server-side rendered cursor |
| 354 | // changes shape or position. It ensures that the next update will clean up |
| 355 | // the old rendered cursor and if necessary draw the new rendered cursor. |
| 356 | |
| 357 | void VNCSConnectionST::renderedCursorChange() |
| 358 | { |
| 359 | if (state() != RFBSTATE_NORMAL) return; |
Pierre Ossman | 71ca8d5 | 2017-09-15 11:03:12 +0200 | [diff] [blame] | 360 | // Are we switching between client-side and server-side cursor? |
Pierre Ossman | 25db44a | 2017-11-16 16:40:44 +0100 | [diff] [blame] | 361 | if (clientHasCursor == needRenderedCursor()) |
Pierre Ossman | 71ca8d5 | 2017-09-15 11:03:12 +0200 | [diff] [blame] | 362 | setCursorOrClose(); |
Pierre Ossman | 25db44a | 2017-11-16 16:40:44 +0100 | [diff] [blame] | 363 | bool hasRenderedCursor = !damagedCursorRegion.is_empty(); |
Pierre Ossman | 330ca42 | 2017-11-06 13:15:55 +0100 | [diff] [blame] | 364 | if (hasRenderedCursor) |
Pierre Ossman | 5c9e1e5 | 2011-11-08 10:32:05 +0000 | [diff] [blame] | 365 | removeRenderedCursor = true; |
Pierre Ossman | 6b0bc29 | 2011-12-21 13:17:54 +0000 | [diff] [blame] | 366 | if (needRenderedCursor()) { |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 367 | updateRenderedCursor = true; |
Pierre Ossman | 6b0bc29 | 2011-12-21 13:17:54 +0000 | [diff] [blame] | 368 | writeFramebufferUpdateOrClose(); |
| 369 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | // needRenderedCursor() returns true if this client needs the server-side |
| 373 | // rendered cursor. This may be because it does not support local cursor or |
| 374 | // because the current cursor position has not been set by this client. |
| 375 | // Unfortunately we can't know for sure when the current cursor position has |
| 376 | // been set by this client. We guess that this is the case when the current |
| 377 | // cursor position is the same as the last pointer event from this client, or |
| 378 | // if it is a very short time since this client's last pointer event (up to a |
| 379 | // second). [ Ideally we should do finer-grained timing here and make the time |
| 380 | // configurable, but I don't think it's that important. ] |
| 381 | |
| 382 | bool VNCSConnectionST::needRenderedCursor() |
| 383 | { |
Pierre Ossman | 77ede0a | 2016-12-05 16:57:30 +0100 | [diff] [blame] | 384 | if (state() != RFBSTATE_NORMAL) |
| 385 | return false; |
| 386 | |
Pierre Ossman | 324043e | 2017-08-16 16:26:11 +0200 | [diff] [blame] | 387 | if (!cp.supportsLocalCursorWithAlpha && |
| 388 | !cp.supportsLocalCursor && !cp.supportsLocalXCursor) |
Pierre Ossman | 77ede0a | 2016-12-05 16:57:30 +0100 | [diff] [blame] | 389 | return true; |
| 390 | if (!server->cursorPos.equals(pointerEventPos) && |
| 391 | (time(0) - pointerEventTime) > 0) |
| 392 | return true; |
| 393 | |
| 394 | return false; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | |
| 398 | void VNCSConnectionST::approveConnectionOrClose(bool accept, |
| 399 | const char* reason) |
| 400 | { |
| 401 | try { |
| 402 | approveConnection(accept, reason); |
| 403 | } catch (rdr::Exception& e) { |
| 404 | close(e.str()); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | |
| 409 | |
| 410 | // -=- Callbacks from SConnection |
| 411 | |
| 412 | void VNCSConnectionST::authSuccess() |
| 413 | { |
| 414 | lastEventTime = time(0); |
| 415 | |
| 416 | server->startDesktop(); |
| 417 | |
| 418 | // - Set the connection parameters appropriately |
| 419 | cp.width = server->pb->width(); |
| 420 | cp.height = server->pb->height(); |
Pierre Ossman | 34e62f3 | 2009-03-20 21:46:12 +0000 | [diff] [blame] | 421 | cp.screenLayout = server->screenLayout; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 422 | cp.setName(server->getName()); |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 423 | cp.setLEDState(server->ledState); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 424 | |
| 425 | // - Set the default pixel format |
| 426 | cp.setPF(server->pb->getPF()); |
| 427 | char buffer[256]; |
| 428 | cp.pf().print(buffer, 256); |
| 429 | vlog.info("Server default pixel format %s", buffer); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 430 | |
| 431 | // - Mark the entire display as "dirty" |
| 432 | updates.add_changed(server->pb->getRect()); |
| 433 | startTime = time(0); |
| 434 | } |
| 435 | |
| 436 | void VNCSConnectionST::queryConnection(const char* userName) |
| 437 | { |
| 438 | // - Authentication succeeded - clear from blacklist |
| 439 | CharArray name; name.buf = sock->getPeerAddress(); |
| 440 | server->blHosts->clearBlackmark(name.buf); |
| 441 | |
| 442 | // - Special case to provide a more useful error message |
| 443 | if (rfb::Server::neverShared && !rfb::Server::disconnectClients && |
| 444 | server->authClientCount() > 0) { |
| 445 | approveConnection(false, "The server is already in use"); |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | // - Does the client have the right to bypass the query? |
| 450 | if (reverseConnection || |
| 451 | !(rfb::Server::queryConnect || sock->requiresQuery()) || |
| 452 | (accessRights & AccessNoQuery)) |
| 453 | { |
| 454 | approveConnection(true); |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | // - Get the server to display an Accept/Reject dialog, if required |
| 459 | // If a dialog is displayed, the result will be PENDING, and the |
| 460 | // server will call approveConnection at a later time |
| 461 | CharArray reason; |
| 462 | VNCServerST::queryResult qr = server->queryConnection(sock, userName, |
| 463 | &reason.buf); |
Pierre Ossman | 3630475 | 2017-10-04 16:21:57 +0200 | [diff] [blame] | 464 | if (qr == VNCServerST::PENDING) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 465 | return; |
| 466 | |
| 467 | // - If server returns ACCEPT/REJECT then pass result to SConnection |
| 468 | approveConnection(qr == VNCServerST::ACCEPT, reason.buf); |
| 469 | } |
| 470 | |
| 471 | void VNCSConnectionST::clientInit(bool shared) |
| 472 | { |
| 473 | lastEventTime = time(0); |
| 474 | if (rfb::Server::alwaysShared || reverseConnection) shared = true; |
Pierre Ossman | e7be49b | 2014-12-02 14:33:17 +0100 | [diff] [blame] | 475 | if (!(accessRights & AccessNonShared)) shared = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 476 | if (rfb::Server::neverShared) shared = false; |
| 477 | if (!shared) { |
Pierre Ossman | e7be49b | 2014-12-02 14:33:17 +0100 | [diff] [blame] | 478 | if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 479 | // - Close all the other connected clients |
| 480 | vlog.debug("non-shared connection - closing clients"); |
| 481 | server->closeClients("Non-shared connection requested", getSock()); |
| 482 | } else { |
| 483 | // - Refuse this connection if there are existing clients, in addition to |
| 484 | // this one |
| 485 | if (server->authClientCount() > 1) { |
| 486 | close("Server is already in use"); |
| 487 | return; |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | SConnection::clientInit(shared); |
| 492 | } |
| 493 | |
| 494 | void VNCSConnectionST::setPixelFormat(const PixelFormat& pf) |
| 495 | { |
| 496 | SConnection::setPixelFormat(pf); |
| 497 | char buffer[256]; |
| 498 | pf.print(buffer, 256); |
| 499 | vlog.info("Client pixel format %s", buffer); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 500 | setCursor(); |
| 501 | } |
| 502 | |
| 503 | void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask) |
| 504 | { |
| 505 | pointerEventTime = lastEventTime = time(0); |
| 506 | server->lastUserInputTime = lastEventTime; |
| 507 | if (!(accessRights & AccessPtrEvents)) return; |
| 508 | if (!rfb::Server::acceptPointerEvents) return; |
| 509 | if (!server->pointerClient || server->pointerClient == this) { |
| 510 | pointerEventPos = pos; |
| 511 | if (buttonMask) |
| 512 | server->pointerClient = this; |
| 513 | else |
| 514 | server->pointerClient = 0; |
| 515 | server->desktop->pointerEvent(pointerEventPos, buttonMask); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | |
| 520 | class VNCSConnectionSTShiftPresser { |
| 521 | public: |
| 522 | VNCSConnectionSTShiftPresser(SDesktop* desktop_) |
| 523 | : desktop(desktop_), pressed(false) {} |
| 524 | ~VNCSConnectionSTShiftPresser() { |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 525 | if (pressed) { |
| 526 | vlog.debug("Releasing fake Shift_L"); |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 527 | desktop->keyEvent(XK_Shift_L, 0, false); |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 528 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 529 | } |
| 530 | void press() { |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 531 | vlog.debug("Pressing fake Shift_L"); |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 532 | desktop->keyEvent(XK_Shift_L, 0, true); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 533 | pressed = true; |
| 534 | } |
| 535 | SDesktop* desktop; |
| 536 | bool pressed; |
| 537 | }; |
| 538 | |
| 539 | // keyEvent() - record in the pressedKeys which keys were pressed. Allow |
| 540 | // multiple down events (for autorepeat), but only allow a single up event. |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 541 | void VNCSConnectionST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) { |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 542 | rdr::U32 lookup; |
| 543 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 544 | lastEventTime = time(0); |
| 545 | server->lastUserInputTime = lastEventTime; |
| 546 | if (!(accessRights & AccessKeyEvents)) return; |
| 547 | if (!rfb::Server::acceptKeyEvents) return; |
| 548 | |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 549 | if (down) |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 550 | vlog.debug("Key pressed: 0x%x / 0x%x", keysym, keycode); |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 551 | else |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 552 | vlog.debug("Key released: 0x%x / 0x%x", keysym, keycode); |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 553 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 554 | // Remap the key if required |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 555 | if (server->keyRemapper) { |
| 556 | rdr::U32 newkey; |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 557 | newkey = server->keyRemapper->remapKey(keysym); |
| 558 | if (newkey != keysym) { |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 559 | vlog.debug("Key remapped to 0x%x", newkey); |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 560 | keysym = newkey; |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 561 | } |
| 562 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 563 | |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 564 | // Avoid lock keys if we don't know the server state |
| 565 | if ((server->ledState == ledUnknown) && |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 566 | ((keysym == XK_Caps_Lock) || |
| 567 | (keysym == XK_Num_Lock) || |
| 568 | (keysym == XK_Scroll_Lock))) { |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 569 | vlog.debug("Ignoring lock key (e.g. caps lock)"); |
| 570 | return; |
| 571 | } |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 572 | |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 573 | // Lock key heuristics |
| 574 | // (only for clients that do not support the LED state extension) |
| 575 | if (!cp.supportsLEDState) { |
| 576 | // Always ignore ScrollLock as we don't have a heuristic |
| 577 | // for that |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 578 | if (keysym == XK_Scroll_Lock) { |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 579 | vlog.debug("Ignoring lock key (e.g. caps lock)"); |
| 580 | return; |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 581 | } |
| 582 | |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 583 | if (down && (server->ledState != ledUnknown)) { |
| 584 | // CapsLock synchronisation heuristic |
| 585 | // (this assumes standard interaction between CapsLock the Shift |
| 586 | // keys and normal characters) |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 587 | if (((keysym >= XK_A) && (keysym <= XK_Z)) || |
| 588 | ((keysym >= XK_a) && (keysym <= XK_z))) { |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 589 | bool uppercase, shift, lock; |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 590 | |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 591 | uppercase = (keysym >= XK_A) && (keysym <= XK_Z); |
Pierre Ossman | 851e680 | 2017-09-12 16:44:44 +0200 | [diff] [blame] | 592 | shift = isShiftPressed(); |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 593 | lock = server->ledState & ledCapsLock; |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 594 | |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 595 | if (lock == (uppercase == shift)) { |
| 596 | vlog.debug("Inserting fake CapsLock to get in sync with client"); |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 597 | server->desktop->keyEvent(XK_Caps_Lock, 0, true); |
| 598 | server->desktop->keyEvent(XK_Caps_Lock, 0, false); |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
| 602 | // NumLock synchronisation heuristic |
| 603 | // (this is more cautious because of the differences between Unix, |
| 604 | // Windows and macOS) |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 605 | if (((keysym >= XK_KP_Home) && (keysym <= XK_KP_Delete)) || |
| 606 | ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) || |
| 607 | (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal)) { |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 608 | bool number, shift, lock; |
| 609 | |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 610 | number = ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) || |
| 611 | (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal); |
Pierre Ossman | 851e680 | 2017-09-12 16:44:44 +0200 | [diff] [blame] | 612 | shift = isShiftPressed(); |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 613 | lock = server->ledState & ledNumLock; |
| 614 | |
| 615 | if (shift) { |
| 616 | // We don't know the appropriate NumLock state for when Shift |
| 617 | // is pressed as it could be one of: |
| 618 | // |
| 619 | // a) A Unix client where Shift negates NumLock |
| 620 | // |
| 621 | // b) A Windows client where Shift only cancels NumLock |
| 622 | // |
| 623 | // c) A macOS client where Shift doesn't have any effect |
| 624 | // |
| 625 | } else if (lock == (number == shift)) { |
| 626 | vlog.debug("Inserting fake NumLock to get in sync with client"); |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 627 | server->desktop->keyEvent(XK_Num_Lock, 0, true); |
| 628 | server->desktop->keyEvent(XK_Num_Lock, 0, false); |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 629 | } |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 630 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | |
| 634 | // Turn ISO_Left_Tab into shifted Tab. |
| 635 | VNCSConnectionSTShiftPresser shiftPresser(server->desktop); |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 636 | if (keysym == XK_ISO_Left_Tab) { |
Pierre Ossman | 851e680 | 2017-09-12 16:44:44 +0200 | [diff] [blame] | 637 | if (!isShiftPressed()) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 638 | shiftPresser.press(); |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 639 | keysym = XK_Tab; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 642 | // We need to be able to track keys, so generate a fake index when we |
| 643 | // aren't given a keycode |
| 644 | if (keycode == 0) |
| 645 | lookup = 0x80000000 | keysym; |
| 646 | else |
| 647 | lookup = keycode; |
| 648 | |
| 649 | // We force the same keysym for an already down key for the |
| 650 | // sake of sanity |
| 651 | if (pressedKeys.find(lookup) != pressedKeys.end()) |
| 652 | keysym = pressedKeys[lookup]; |
| 653 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 654 | if (down) { |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 655 | pressedKeys[lookup] = keysym; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 656 | } else { |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 657 | if (!pressedKeys.erase(lookup)) |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 658 | return; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 659 | } |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 660 | |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 661 | server->desktop->keyEvent(keysym, keycode, down); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | void VNCSConnectionST::clientCutText(const char* str, int len) |
| 665 | { |
| 666 | if (!(accessRights & AccessCutText)) return; |
| 667 | if (!rfb::Server::acceptCutText) return; |
| 668 | server->desktop->clientCutText(str, len); |
| 669 | } |
| 670 | |
| 671 | void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental) |
| 672 | { |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 673 | Rect safeRect; |
| 674 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 675 | if (!(accessRights & AccessView)) return; |
| 676 | |
| 677 | SConnection::framebufferUpdateRequest(r, incremental); |
| 678 | |
Pierre Ossman | d9a59ba | 2009-03-20 15:55:37 +0000 | [diff] [blame] | 679 | // Check that the client isn't sending crappy requests |
| 680 | if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) { |
| 681 | vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d", |
| 682 | r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height); |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 683 | safeRect = r.intersect(Rect(0, 0, cp.width, cp.height)); |
| 684 | } else { |
| 685 | safeRect = r; |
Pierre Ossman | d9a59ba | 2009-03-20 15:55:37 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Constantin Kaplinsky | 2ef6695 | 2008-08-29 11:33:46 +0000 | [diff] [blame] | 688 | // Just update the requested region. |
| 689 | // Framebuffer update will be sent a bit later, see processMessages(). |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 690 | Region reqRgn(r); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 691 | if (!incremental || !continuousUpdates) |
| 692 | requested.assign_union(reqRgn); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 693 | |
| 694 | if (!incremental) { |
| 695 | // Non-incremental update - treat as if area requested has changed |
| 696 | updates.add_changed(reqRgn); |
Pierre Ossman | 53125a7 | 2009-04-22 15:37:51 +0000 | [diff] [blame] | 697 | |
| 698 | // And send the screen layout to the client (which, unlike the |
| 699 | // framebuffer dimensions, the client doesn't get during init) |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 700 | writer()->writeExtendedDesktopSize(); |
Pierre Ossman | 53125a7 | 2009-04-22 15:37:51 +0000 | [diff] [blame] | 701 | |
| 702 | // We do not send a DesktopSize since it only contains the |
| 703 | // framebuffer size (which the client already should know) and |
| 704 | // because some clients don't handle extra DesktopSize events |
| 705 | // very well. |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 706 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Pierre Ossman | 34bb061 | 2009-03-21 21:16:14 +0000 | [diff] [blame] | 709 | void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height, |
| 710 | const ScreenSet& layout) |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 711 | { |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 712 | unsigned int result; |
| 713 | |
Michal Srb | b318b8f | 2014-11-24 13:18:28 +0200 | [diff] [blame] | 714 | if (!(accessRights & AccessSetDesktopSize)) return; |
| 715 | if (!rfb::Server::acceptSetDesktopSize) return; |
| 716 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 717 | // Don't bother the desktop with an invalid configuration |
| 718 | if (!layout.validate(fb_width, fb_height)) { |
| 719 | writer()->writeExtendedDesktopSize(reasonClient, resultInvalid, |
| 720 | fb_width, fb_height, layout); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 721 | return; |
| 722 | } |
| 723 | |
| 724 | // FIXME: the desktop will call back to VNCServerST and an extra set |
| 725 | // of ExtendedDesktopSize messages will be sent. This is okay |
| 726 | // protocol-wise, but unnecessary. |
| 727 | result = server->desktop->setScreenLayout(fb_width, fb_height, layout); |
| 728 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 729 | writer()->writeExtendedDesktopSize(reasonClient, result, |
| 730 | fb_width, fb_height, layout); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 731 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 732 | // Only notify other clients on success |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 733 | if (result == resultSuccess) { |
| 734 | if (server->screenLayout != layout) |
| 735 | throw Exception("Desktop configured a different screen layout than requested"); |
| 736 | server->notifyScreenLayoutChange(this); |
| 737 | } |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 740 | void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[]) |
| 741 | { |
Pierre Ossman | b2a417c | 2015-12-11 19:32:21 +0100 | [diff] [blame] | 742 | rdr::U8 type; |
| 743 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 744 | if (flags & fenceFlagRequest) { |
| 745 | if (flags & fenceFlagSyncNext) { |
Pierre Ossman | b8b1e96 | 2012-07-20 10:47:00 +0000 | [diff] [blame] | 746 | pendingSyncFence = true; |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 747 | |
| 748 | fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext); |
| 749 | fenceDataLen = len; |
| 750 | delete [] fenceData; |
Michal Srb | f3afa24 | 2017-03-27 19:02:15 +0300 | [diff] [blame] | 751 | fenceData = NULL; |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 752 | if (len > 0) { |
| 753 | fenceData = new char[len]; |
| 754 | memcpy(fenceData, data, len); |
| 755 | } |
| 756 | |
| 757 | return; |
| 758 | } |
| 759 | |
| 760 | // We handle everything synchronously so we trivially honor these modes |
| 761 | flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter); |
| 762 | |
| 763 | writer()->writeFence(flags, len, data); |
| 764 | return; |
| 765 | } |
| 766 | |
Pierre Ossman | b2a417c | 2015-12-11 19:32:21 +0100 | [diff] [blame] | 767 | if (len < 1) |
| 768 | vlog.error("Fence response of unexpected size received"); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 769 | |
Pierre Ossman | b2a417c | 2015-12-11 19:32:21 +0100 | [diff] [blame] | 770 | type = data[0]; |
| 771 | |
| 772 | switch (type) { |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 773 | case 0: |
| 774 | // Initial dummy fence; |
| 775 | break; |
Pierre Ossman | b2a417c | 2015-12-11 19:32:21 +0100 | [diff] [blame] | 776 | case 1: |
Pierre Ossman | c09e558 | 2015-12-11 20:23:17 +0100 | [diff] [blame] | 777 | congestion.gotPong(); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 778 | break; |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 779 | default: |
Pierre Ossman | b2a417c | 2015-12-11 19:32:21 +0100 | [diff] [blame] | 780 | vlog.error("Fence response of unexpected type received"); |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 784 | void VNCSConnectionST::enableContinuousUpdates(bool enable, |
| 785 | int x, int y, int w, int h) |
| 786 | { |
| 787 | Rect rect; |
| 788 | |
| 789 | if (!cp.supportsFence || !cp.supportsContinuousUpdates) |
| 790 | throw Exception("Client tried to enable continuous updates when not allowed"); |
| 791 | |
| 792 | continuousUpdates = enable; |
| 793 | |
| 794 | rect.setXYWH(x, y, w, h); |
| 795 | cuRegion.reset(rect); |
| 796 | |
| 797 | if (enable) { |
| 798 | requested.clear(); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 799 | } else { |
| 800 | writer()->writeEndOfContinuousUpdates(); |
| 801 | } |
| 802 | } |
| 803 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 804 | // supportsLocalCursor() is called whenever the status of |
| 805 | // cp.supportsLocalCursor has changed. If the client does now support local |
| 806 | // cursor, we make sure that the old server-side rendered cursor is cleaned up |
| 807 | // and the cursor is sent to the client. |
| 808 | |
| 809 | void VNCSConnectionST::supportsLocalCursor() |
| 810 | { |
Pierre Ossman | 387a417 | 2017-11-16 16:44:36 +0100 | [diff] [blame] | 811 | bool hasRenderedCursor = !damagedCursorRegion.is_empty(); |
| 812 | if (hasRenderedCursor && !needRenderedCursor()) |
| 813 | removeRenderedCursor = true; |
| 814 | setCursor(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 817 | void VNCSConnectionST::supportsFence() |
| 818 | { |
Pierre Ossman | b2a417c | 2015-12-11 19:32:21 +0100 | [diff] [blame] | 819 | char type = 0; |
| 820 | writer()->writeFence(fenceFlagRequest, sizeof(type), &type); |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 823 | void VNCSConnectionST::supportsContinuousUpdates() |
| 824 | { |
| 825 | // We refuse to use continuous updates if we cannot monitor the buffer |
| 826 | // usage using fences. |
| 827 | if (!cp.supportsFence) |
| 828 | return; |
| 829 | |
| 830 | writer()->writeEndOfContinuousUpdates(); |
| 831 | } |
| 832 | |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 833 | void VNCSConnectionST::supportsLEDState() |
| 834 | { |
| 835 | writer()->writeLEDState(); |
| 836 | } |
| 837 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 838 | |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 839 | bool VNCSConnectionST::handleTimeout(Timer* t) |
| 840 | { |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 841 | try { |
Pierre Ossman | a40ab20 | 2016-04-29 15:35:56 +0200 | [diff] [blame] | 842 | if (t == &congestionTimer) |
Pierre Ossman | a99d14d | 2015-12-13 15:43:46 +0100 | [diff] [blame] | 843 | writeFramebufferUpdate(); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 844 | } catch (rdr::Exception& e) { |
| 845 | close(e.str()); |
| 846 | } |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 847 | |
| 848 | return false; |
| 849 | } |
| 850 | |
Pierre Ossman | 851e680 | 2017-09-12 16:44:44 +0200 | [diff] [blame] | 851 | bool VNCSConnectionST::isShiftPressed() |
| 852 | { |
| 853 | std::map<rdr::U32, rdr::U32>::const_iterator iter; |
| 854 | |
| 855 | for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter) { |
| 856 | if (iter->second == XK_Shift_L) |
| 857 | return true; |
| 858 | if (iter->second == XK_Shift_R) |
| 859 | return true; |
| 860 | } |
| 861 | |
| 862 | return false; |
| 863 | } |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 864 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 865 | void VNCSConnectionST::writeRTTPing() |
| 866 | { |
Pierre Ossman | b2a417c | 2015-12-11 19:32:21 +0100 | [diff] [blame] | 867 | char type; |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 868 | |
| 869 | if (!cp.supportsFence) |
| 870 | return; |
| 871 | |
Pierre Ossman | a99d14d | 2015-12-13 15:43:46 +0100 | [diff] [blame] | 872 | congestion.updatePosition(sock->outStream().length()); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 873 | |
| 874 | // We need to make sure any old update are already processed by the |
| 875 | // time we get the response back. This allows us to reliably throttle |
| 876 | // back on client overload, as well as network overload. |
Pierre Ossman | b2a417c | 2015-12-11 19:32:21 +0100 | [diff] [blame] | 877 | type = 1; |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 878 | writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore, |
Pierre Ossman | b2a417c | 2015-12-11 19:32:21 +0100 | [diff] [blame] | 879 | sizeof(type), &type); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 880 | |
Pierre Ossman | a99d14d | 2015-12-13 15:43:46 +0100 | [diff] [blame] | 881 | congestion.sentPing(); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 884 | bool VNCSConnectionST::isCongested() |
| 885 | { |
Pierre Ossman | ce26181 | 2018-07-17 15:01:53 +0200 | [diff] [blame] | 886 | int eta; |
Pierre Ossman | a99d14d | 2015-12-13 15:43:46 +0100 | [diff] [blame] | 887 | |
| 888 | congestionTimer.stop(); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 889 | |
| 890 | // Stuff still waiting in the send buffer? |
Pierre Ossman | 352d062 | 2016-04-29 15:50:54 +0200 | [diff] [blame] | 891 | sock->outStream().flush(); |
Pierre Ossman | 8cf7163 | 2016-03-11 09:38:08 +0100 | [diff] [blame] | 892 | congestion.debugTrace("congestion-trace.csv", sock->getFd()); |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 893 | if (sock->outStream().bufferUsage() > 0) |
| 894 | return true; |
| 895 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 896 | if (!cp.supportsFence) |
| 897 | return false; |
| 898 | |
Pierre Ossman | a99d14d | 2015-12-13 15:43:46 +0100 | [diff] [blame] | 899 | congestion.updatePosition(sock->outStream().length()); |
| 900 | if (!congestion.isCongested()) |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 901 | return false; |
| 902 | |
Pierre Ossman | a99d14d | 2015-12-13 15:43:46 +0100 | [diff] [blame] | 903 | eta = congestion.getUncongestedETA(); |
| 904 | if (eta >= 0) |
| 905 | congestionTimer.start(eta); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 906 | |
| 907 | return true; |
| 908 | } |
| 909 | |
| 910 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 911 | void VNCSConnectionST::writeFramebufferUpdate() |
| 912 | { |
Pierre Ossman | a99d14d | 2015-12-13 15:43:46 +0100 | [diff] [blame] | 913 | congestion.updatePosition(sock->outStream().length()); |
| 914 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 915 | // We're in the middle of processing a command that's supposed to be |
| 916 | // synchronised. Allowing an update to slip out right now might violate |
| 917 | // that synchronisation. |
| 918 | if (syncFence) |
| 919 | return; |
| 920 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 921 | // We try to aggregate responses, so don't send out anything whilst we |
| 922 | // still have incoming messages. processMessages() will give us another |
| 923 | // chance to run once things are idle. |
| 924 | if (inProcessMessages) |
| 925 | return; |
| 926 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 927 | if (state() != RFBSTATE_NORMAL) |
| 928 | return; |
| 929 | if (requested.is_empty() && !continuousUpdates) |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 930 | return; |
Pierre Ossman | d9a59ba | 2009-03-20 15:55:37 +0000 | [diff] [blame] | 931 | |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 932 | // Check that we actually have some space on the link and retry in a |
| 933 | // bit if things are congested. |
Pierre Ossman | a40ab20 | 2016-04-29 15:35:56 +0200 | [diff] [blame] | 934 | if (isCongested()) |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 935 | return; |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 936 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 937 | // Updates often consists of many small writes, and in continuous |
| 938 | // mode, we will also have small fence messages around the update. We |
| 939 | // need to aggregate these in order to not clog up TCP's congestion |
| 940 | // window. |
Peter Ã…strand (astrand) | 01dc1a6 | 2017-10-10 12:56:04 +0200 | [diff] [blame] | 941 | sock->cork(true); |
Pierre Ossman | 36dadf8 | 2011-11-15 12:11:32 +0000 | [diff] [blame] | 942 | |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 943 | // First take care of any updates that cannot contain framebuffer data |
| 944 | // changes. |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 945 | writeNoDataUpdate(); |
| 946 | |
| 947 | // Then real data (if possible) |
| 948 | writeDataUpdate(); |
| 949 | |
Peter Ã…strand (astrand) | 01dc1a6 | 2017-10-10 12:56:04 +0200 | [diff] [blame] | 950 | sock->cork(false); |
Pierre Ossman | a99d14d | 2015-12-13 15:43:46 +0100 | [diff] [blame] | 951 | |
| 952 | congestion.updatePosition(sock->outStream().length()); |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | void VNCSConnectionST::writeNoDataUpdate() |
| 956 | { |
| 957 | if (!writer()->needNoDataUpdate()) |
| 958 | return; |
| 959 | |
| 960 | writer()->writeNoDataUpdate(); |
| 961 | |
| 962 | // Make sure no data update is sent until next request |
| 963 | requested.clear(); |
| 964 | } |
| 965 | |
| 966 | void VNCSConnectionST::writeDataUpdate() |
| 967 | { |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 968 | Region req, pending; |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 969 | UpdateInfo ui; |
| 970 | bool needNewUpdateInfo; |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 971 | const RenderedCursor *cursor; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 972 | |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 973 | updates.enable_copyrect(cp.useCopyRect); |
| 974 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 975 | // See what the client has requested (if anything) |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 976 | if (continuousUpdates) |
| 977 | req = cuRegion.union_(requested); |
| 978 | else |
| 979 | req = requested; |
| 980 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 981 | if (req.is_empty()) |
| 982 | return; |
| 983 | |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 984 | // Get any framebuffer changes we haven't yet been informed of |
| 985 | pending = server->getPendingRegion(); |
| 986 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 987 | // Get the lists of updates. Prior to exporting the data to the `ui' object, |
| 988 | // getUpdateInfo() will normalize the `updates' object such way that its |
| 989 | // `changed' and `copied' regions would not intersect. |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 990 | updates.getUpdateInfo(&ui, req); |
| 991 | needNewUpdateInfo = false; |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 992 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 993 | // If the previous position of the rendered cursor overlaps the source of the |
| 994 | // copy, then when the copy happens the corresponding rectangle in the |
| 995 | // destination will be wrong, so add it to the changed region. |
| 996 | |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 997 | if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) { |
| 998 | Region bogusCopiedCursor; |
| 999 | |
Pierre Ossman | 74385d3 | 2018-03-22 16:00:18 +0100 | [diff] [blame] | 1000 | bogusCopiedCursor = damagedCursorRegion; |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1001 | bogusCopiedCursor.translate(ui.copy_delta); |
| 1002 | bogusCopiedCursor.assign_intersect(server->pb->getRect()); |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 1003 | if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1004 | updates.add_changed(bogusCopiedCursor); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1005 | needNewUpdateInfo = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1006 | } |
| 1007 | } |
| 1008 | |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1009 | // If we need to remove the old rendered cursor, just add the region to |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1010 | // the changed region. |
| 1011 | |
| 1012 | if (removeRenderedCursor) { |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1013 | updates.add_changed(damagedCursorRegion); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1014 | needNewUpdateInfo = true; |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1015 | damagedCursorRegion.clear(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1016 | removeRenderedCursor = false; |
| 1017 | } |
| 1018 | |
Pierre Ossman | 8c3bd69 | 2018-03-22 15:58:54 +0100 | [diff] [blame] | 1019 | // If we need a full cursor update then make sure its entire region |
| 1020 | // is marked as changed. |
| 1021 | |
| 1022 | if (updateRenderedCursor) { |
| 1023 | updates.add_changed(server->getRenderedCursor()->getEffectiveRect()); |
| 1024 | needNewUpdateInfo = true; |
| 1025 | updateRenderedCursor = false; |
| 1026 | } |
| 1027 | |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1028 | // The `updates' object could change, make sure we have valid update info. |
| 1029 | |
| 1030 | if (needNewUpdateInfo) |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 1031 | updates.getUpdateInfo(&ui, req); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1032 | |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 1033 | // If there are queued updates then we cannot safely send an update |
| 1034 | // without risking a partially updated screen |
| 1035 | |
| 1036 | if (!pending.is_empty()) { |
| 1037 | // However we might still be able to send a lossless refresh |
| 1038 | req.assign_subtract(pending); |
| 1039 | req.assign_subtract(ui.changed); |
| 1040 | req.assign_subtract(ui.copied); |
| 1041 | |
| 1042 | ui.changed.clear(); |
| 1043 | ui.copied.clear(); |
| 1044 | } |
| 1045 | |
Pierre Ossman | 8c3bd69 | 2018-03-22 15:58:54 +0100 | [diff] [blame] | 1046 | // Does the client need a server-side rendered cursor? |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1047 | |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 1048 | cursor = NULL; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1049 | if (needRenderedCursor()) { |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1050 | Rect renderedCursorRect; |
| 1051 | |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 1052 | cursor = server->getRenderedCursor(); |
Pierre Ossman | 8c3bd69 | 2018-03-22 15:58:54 +0100 | [diff] [blame] | 1053 | renderedCursorRect = cursor->getEffectiveRect(); |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 1054 | |
Pierre Ossman | 8c3bd69 | 2018-03-22 15:58:54 +0100 | [diff] [blame] | 1055 | // Check that we don't try to copy over the cursor area, and |
| 1056 | // if that happens we need to treat it as changed so that we can |
| 1057 | // re-render it |
| 1058 | if (!ui.copied.intersect(renderedCursorRect).is_empty()) { |
| 1059 | ui.changed.assign_union(ui.copied.intersect(renderedCursorRect)); |
| 1060 | ui.copied.assign_subtract(renderedCursorRect); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Pierre Ossman | 8c3bd69 | 2018-03-22 15:58:54 +0100 | [diff] [blame] | 1063 | // Track where we've rendered the cursor |
| 1064 | damagedCursorRegion.assign_union(ui.changed.intersect(renderedCursorRect)); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 1067 | // Return if there is nothing to send the client. |
| 1068 | |
Pierre Ossman | 6b2f113 | 2016-11-30 08:03:35 +0100 | [diff] [blame] | 1069 | if (ui.is_empty() && !writer()->needFakeUpdate() && |
| 1070 | !encodeManager.needsLosslessRefresh(req)) |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1071 | return; |
Pierre Ossman | fdba3fe | 2014-01-31 13:12:18 +0100 | [diff] [blame] | 1072 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1073 | writeRTTPing(); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 1074 | |
Pierre Ossman | 6b2f113 | 2016-11-30 08:03:35 +0100 | [diff] [blame] | 1075 | if (!ui.is_empty()) |
| 1076 | encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor); |
Pierre Ossman | a2b80d6 | 2018-03-23 09:30:09 +0100 | [diff] [blame] | 1077 | else { |
| 1078 | size_t maxUpdateSize; |
| 1079 | |
| 1080 | // FIXME: If continuous updates aren't used then the client might |
| 1081 | // be slower than frameRate in its requests and we could |
| 1082 | // afford a larger update size |
| 1083 | |
| 1084 | // FIXME: Bandwidth estimation without congestion control |
| 1085 | maxUpdateSize = congestion.getBandwidth() * |
| 1086 | server->msToNextUpdate() / 1000; |
| 1087 | |
| 1088 | encodeManager.writeLosslessRefresh(req, server->getPixelBuffer(), |
| 1089 | cursor, maxUpdateSize); |
| 1090 | } |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 1091 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1092 | writeRTTPing(); |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 1093 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1094 | // The request might be for just part of the screen, so we cannot |
| 1095 | // just clear the entire update tracker. |
| 1096 | updates.subtract(req); |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 1097 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1098 | requested.clear(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 1102 | void VNCSConnectionST::screenLayoutChange(rdr::U16 reason) |
| 1103 | { |
| 1104 | if (!authenticated()) |
| 1105 | return; |
| 1106 | |
| 1107 | cp.screenLayout = server->screenLayout; |
| 1108 | |
| 1109 | if (state() != RFBSTATE_NORMAL) |
| 1110 | return; |
| 1111 | |
| 1112 | writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height, |
| 1113 | cp.screenLayout); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1116 | |
| 1117 | // setCursor() is called whenever the cursor has changed shape or pixel format. |
| 1118 | // If the client supports local cursor then it will arrange for the cursor to |
| 1119 | // be sent to the client. |
| 1120 | |
| 1121 | void VNCSConnectionST::setCursor() |
| 1122 | { |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 1123 | if (state() != RFBSTATE_NORMAL) |
| 1124 | return; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 1125 | |
Pierre Ossman | 71ca8d5 | 2017-09-15 11:03:12 +0200 | [diff] [blame] | 1126 | // We need to blank out the client's cursor or there will be two |
Pierre Ossman | 25db44a | 2017-11-16 16:40:44 +0100 | [diff] [blame] | 1127 | if (needRenderedCursor()) { |
Pierre Ossman | 71ca8d5 | 2017-09-15 11:03:12 +0200 | [diff] [blame] | 1128 | cp.setCursor(emptyCursor); |
Pierre Ossman | 25db44a | 2017-11-16 16:40:44 +0100 | [diff] [blame] | 1129 | clientHasCursor = false; |
| 1130 | } else { |
Pierre Ossman | 71ca8d5 | 2017-09-15 11:03:12 +0200 | [diff] [blame] | 1131 | cp.setCursor(*server->cursor); |
Pierre Ossman | 25db44a | 2017-11-16 16:40:44 +0100 | [diff] [blame] | 1132 | clientHasCursor = true; |
| 1133 | } |
Pierre Ossman | 126e564 | 2014-02-13 14:40:25 +0100 | [diff] [blame] | 1134 | |
Pierre Ossman | 8053c8e | 2017-02-21 12:59:04 +0100 | [diff] [blame] | 1135 | if (!writer()->writeSetCursorWithAlpha()) { |
| 1136 | if (!writer()->writeSetCursor()) { |
| 1137 | if (!writer()->writeSetXCursor()) { |
| 1138 | // No client support |
| 1139 | return; |
| 1140 | } |
Pierre Ossman | 126e564 | 2014-02-13 14:40:25 +0100 | [diff] [blame] | 1141 | } |
| 1142 | } |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | void VNCSConnectionST::setDesktopName(const char *name) |
| 1146 | { |
| 1147 | cp.setName(name); |
| 1148 | |
| 1149 | if (state() != RFBSTATE_NORMAL) |
| 1150 | return; |
| 1151 | |
| 1152 | if (!writer()->writeSetDesktopName()) { |
| 1153 | fprintf(stderr, "Client does not support desktop rename\n"); |
| 1154 | return; |
| 1155 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 1158 | void VNCSConnectionST::setLEDState(unsigned int ledstate) |
| 1159 | { |
| 1160 | if (state() != RFBSTATE_NORMAL) |
| 1161 | return; |
| 1162 | |
| 1163 | cp.setLEDState(ledstate); |
| 1164 | |
Pierre Ossman | b218ecd | 2017-11-16 16:43:13 +0100 | [diff] [blame] | 1165 | writer()->writeLEDState(); |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 1166 | } |
| 1167 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1168 | void VNCSConnectionST::setSocketTimeouts() |
| 1169 | { |
| 1170 | int timeoutms = rfb::Server::clientWaitTimeMillis; |
| 1171 | soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout)); |
| 1172 | if (timeoutms == 0) |
| 1173 | timeoutms = -1; |
| 1174 | sock->inStream().setTimeout(timeoutms); |
| 1175 | sock->outStream().setTimeout(timeoutms); |
| 1176 | } |
| 1177 | |
| 1178 | char* VNCSConnectionST::getStartTime() |
| 1179 | { |
| 1180 | char* result = ctime(&startTime); |
| 1181 | result[24] = '\0'; |
| 1182 | return result; |
| 1183 | } |
| 1184 | |
| 1185 | void VNCSConnectionST::setStatus(int status) |
| 1186 | { |
| 1187 | switch (status) { |
| 1188 | case 0: |
| 1189 | accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView; |
| 1190 | break; |
| 1191 | case 1: |
Pierre Ossman | 7728be2 | 2015-03-03 16:39:37 +0100 | [diff] [blame] | 1192 | accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1193 | break; |
| 1194 | case 2: |
Adam Tkac | 8e98506 | 2011-02-07 11:33:57 +0000 | [diff] [blame] | 1195 | accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1196 | break; |
| 1197 | } |
| 1198 | framebufferUpdateRequest(server->pb->getRect(), false); |
| 1199 | } |
| 1200 | int VNCSConnectionST::getStatus() |
| 1201 | { |
| 1202 | if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007) |
| 1203 | return 0; |
| 1204 | if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001) |
| 1205 | return 1; |
| 1206 | if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000) |
| 1207 | return 2; |
| 1208 | return 4; |
| 1209 | } |
| 1210 | |