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