Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +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 | |
| 20 | // -=- Single-Threaded VNC Server implementation |
| 21 | |
| 22 | |
| 23 | // Note about how sockets get closed: |
| 24 | // |
| 25 | // Closing sockets to clients is non-trivial because the code which calls |
| 26 | // VNCServerST must explicitly know about all the sockets (so that it can block |
| 27 | // on them appropriately). However, VNCServerST may want to close clients for |
| 28 | // a number of reasons, and from a variety of entry points. The simplest is |
| 29 | // when processSocketEvent() is called for a client, and the remote end has |
| 30 | // closed its socket. A more complex reason is when processSocketEvent() is |
| 31 | // called for a client which has just sent a ClientInit with the shared flag |
| 32 | // set to false - in this case we want to close all other clients. Yet another |
| 33 | // reason for disconnecting clients is when the desktop size has changed as a |
| 34 | // result of a call to setPixelBuffer(). |
| 35 | // |
| 36 | // The responsibility for creating and deleting sockets is entirely with the |
| 37 | // calling code. When VNCServerST wants to close a connection to a client it |
| 38 | // calls the VNCSConnectionST's close() method which calls shutdown() on the |
| 39 | // socket. Eventually the calling code will notice that the socket has been |
| 40 | // shut down and call removeSocket() so that we can delete the |
| 41 | // VNCSConnectionST. Note that the socket must not be deleted by the calling |
| 42 | // code until after removeSocket() has been called. |
| 43 | // |
| 44 | // One minor complication is that we don't allocate a VNCSConnectionST object |
| 45 | // for a blacklisted host (since we want to minimise the resources used for |
| 46 | // dealing with such a connection). In order to properly implement the |
| 47 | // getSockets function, we must maintain a separate closingSockets list, |
| 48 | // otherwise blacklisted connections might be "forgotten". |
| 49 | |
| 50 | |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 51 | #include <assert.h> |
Pierre Ossman | f99c571 | 2009-03-13 14:41:27 +0000 | [diff] [blame] | 52 | #include <stdlib.h> |
| 53 | |
Pierre Ossman | 707fa12 | 2015-12-11 20:21:20 +0100 | [diff] [blame] | 54 | #include <rfb/ComparingUpdateTracker.h> |
| 55 | #include <rfb/KeyRemapper.h> |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 56 | #include <rfb/LogWriter.h> |
Pierre Ossman | 707fa12 | 2015-12-11 20:21:20 +0100 | [diff] [blame] | 57 | #include <rfb/Security.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 58 | #include <rfb/ServerCore.h> |
| 59 | #include <rfb/VNCServerST.h> |
| 60 | #include <rfb/VNCSConnectionST.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 61 | #include <rfb/util.h> |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 62 | #include <rfb/ledStates.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 63 | |
| 64 | #include <rdr/types.h> |
| 65 | |
| 66 | using namespace rfb; |
| 67 | |
| 68 | static LogWriter slog("VNCServerST"); |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 69 | static LogWriter connectionsLog("Connections"); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 70 | |
| 71 | // |
| 72 | // -=- VNCServerST Implementation |
| 73 | // |
| 74 | |
| 75 | // -=- Constructors/Destructor |
| 76 | |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 77 | VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_) |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 78 | : blHosts(&blacklist), desktop(desktop_), desktopStarted(false), |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 79 | blockCounter(0), pb(0), ledState(ledUnknown), |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 80 | name(strDup(name_)), pointerClient(0), comparer(0), |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 81 | cursor(new Cursor(0, 0, Point(), NULL)), |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 82 | renderedCursorInvalid(false), |
Pierre Ossman | eef6c9a | 2018-10-05 17:11:25 +0200 | [diff] [blame] | 83 | keyRemapper(&KeyRemapper::defInstance), |
Pierre Ossman | 025326d | 2018-10-08 16:03:01 +0200 | [diff] [blame] | 84 | lastConnectionTime(0), frameTimer(this) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 85 | { |
| 86 | lastUserInputTime = lastDisconnectTime = time(0); |
| 87 | slog.debug("creating single-threaded server %s", name.buf); |
| 88 | } |
| 89 | |
| 90 | VNCServerST::~VNCServerST() |
| 91 | { |
| 92 | slog.debug("shutting down server %s", name.buf); |
| 93 | |
| 94 | // Close any active clients, with appropriate logging & cleanup |
| 95 | closeClients("Server shutdown"); |
| 96 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 97 | // Stop trying to render things |
| 98 | stopFrameClock(); |
| 99 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 100 | // Delete all the clients, and their sockets, and any closing sockets |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 101 | while (!clients.empty()) { |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 102 | VNCSConnectionST* client; |
| 103 | client = clients.front(); |
| 104 | clients.pop_front(); |
| 105 | delete client; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Stop the desktop object if active, *only* after deleting all clients! |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 109 | stopDesktop(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 110 | |
Pierre Ossman | 05338bc | 2016-11-08 14:57:11 +0100 | [diff] [blame] | 111 | if (comparer) |
| 112 | comparer->logStats(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 113 | delete comparer; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 114 | |
| 115 | delete cursor; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | |
| 119 | // SocketServer methods |
| 120 | |
| 121 | void VNCServerST::addSocket(network::Socket* sock, bool outgoing) |
| 122 | { |
| 123 | // - Check the connection isn't black-marked |
| 124 | // *** do this in getSecurity instead? |
| 125 | CharArray address(sock->getPeerAddress()); |
| 126 | if (blHosts->isBlackmarked(address.buf)) { |
| 127 | connectionsLog.error("blacklisted: %s", address.buf); |
| 128 | try { |
Pierre Ossman | 4bba246 | 2018-10-11 07:52:50 +0200 | [diff] [blame^] | 129 | rdr::OutStream& os = sock->outStream(); |
| 130 | |
| 131 | // Shortest possible way to tell a client it is not welcome |
| 132 | os.writeBytes("RFB 003.003\n", 12); |
| 133 | os.writeU32(0); |
| 134 | os.writeString("Too many security failures"); |
| 135 | os.flush(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 136 | } catch (rdr::Exception&) { |
| 137 | } |
| 138 | sock->shutdown(); |
| 139 | closingSockets.push_back(sock); |
| 140 | return; |
| 141 | } |
| 142 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 143 | CharArray name; |
| 144 | name.buf = sock->getPeerEndpoint(); |
| 145 | connectionsLog.status("accepted: %s", name.buf); |
| 146 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 147 | if (clients.empty()) { |
| 148 | lastConnectionTime = time(0); |
| 149 | } |
| 150 | |
| 151 | VNCSConnectionST* client = new VNCSConnectionST(this, sock, outgoing); |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 152 | clients.push_front(client); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 153 | client->init(); |
| 154 | } |
| 155 | |
| 156 | void VNCServerST::removeSocket(network::Socket* sock) { |
| 157 | // - If the socket has resources allocated to it, delete them |
| 158 | std::list<VNCSConnectionST*>::iterator ci; |
| 159 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 160 | if ((*ci)->getSock() == sock) { |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 161 | clients.remove(*ci); |
| 162 | |
Pierre Ossman | b684341 | 2018-10-05 17:30:52 +0200 | [diff] [blame] | 163 | // - Release the cursor if this client owns it |
| 164 | if (pointerClient == *ci) |
| 165 | pointerClient = NULL; |
| 166 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 167 | if ((*ci)->authenticated()) |
| 168 | lastDisconnectTime = time(0); |
| 169 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 170 | // - Delete the per-Socket resources |
| 171 | delete *ci; |
| 172 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 173 | CharArray name; |
| 174 | name.buf = sock->getPeerEndpoint(); |
| 175 | connectionsLog.status("closed: %s", name.buf); |
| 176 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 177 | // - Check that the desktop object is still required |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 178 | if (authClientCount() == 0) |
| 179 | stopDesktop(); |
Pierre Ossman | 05338bc | 2016-11-08 14:57:11 +0100 | [diff] [blame] | 180 | |
| 181 | if (comparer) |
| 182 | comparer->logStats(); |
| 183 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // - If the Socket has no resources, it may have been a closingSocket |
| 189 | closingSockets.remove(sock); |
| 190 | } |
| 191 | |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 192 | void VNCServerST::processSocketReadEvent(network::Socket* sock) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 193 | { |
| 194 | // - Find the appropriate VNCSConnectionST and process the event |
| 195 | std::list<VNCSConnectionST*>::iterator ci; |
| 196 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 197 | if ((*ci)->getSock() == sock) { |
| 198 | (*ci)->processMessages(); |
| 199 | return; |
| 200 | } |
| 201 | } |
| 202 | throw rdr::Exception("invalid Socket in VNCServerST"); |
| 203 | } |
| 204 | |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 205 | void VNCServerST::processSocketWriteEvent(network::Socket* sock) |
| 206 | { |
| 207 | // - Find the appropriate VNCSConnectionST and process the event |
| 208 | std::list<VNCSConnectionST*>::iterator ci; |
| 209 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 210 | if ((*ci)->getSock() == sock) { |
| 211 | (*ci)->flushSocket(); |
| 212 | return; |
| 213 | } |
| 214 | } |
| 215 | throw rdr::Exception("invalid Socket in VNCServerST"); |
| 216 | } |
| 217 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 218 | int VNCServerST::checkTimeouts() |
| 219 | { |
| 220 | int timeout = 0; |
| 221 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
Pierre Ossman | 2d61deb | 2011-10-25 15:18:53 +0000 | [diff] [blame] | 222 | |
| 223 | soonestTimeout(&timeout, Timer::checkTimeouts()); |
| 224 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 225 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 226 | ci_next = ci; ci_next++; |
| 227 | soonestTimeout(&timeout, (*ci)->checkIdleTimeout()); |
| 228 | } |
| 229 | |
| 230 | int timeLeft; |
Constantin Kaplinsky | 8499d0c | 2008-08-21 05:51:29 +0000 | [diff] [blame] | 231 | time_t now = time(0); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 232 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 233 | // Check MaxDisconnectionTime |
| 234 | if (rfb::Server::maxDisconnectionTime && clients.empty()) { |
| 235 | if (now < lastDisconnectTime) { |
| 236 | // Someone must have set the time backwards. |
| 237 | slog.info("Time has gone backwards - resetting lastDisconnectTime"); |
| 238 | lastDisconnectTime = now; |
| 239 | } |
| 240 | timeLeft = lastDisconnectTime + rfb::Server::maxDisconnectionTime - now; |
| 241 | if (timeLeft < -60) { |
| 242 | // Someone must have set the time forwards. |
| 243 | slog.info("Time has gone forwards - resetting lastDisconnectTime"); |
| 244 | lastDisconnectTime = now; |
| 245 | timeLeft = rfb::Server::maxDisconnectionTime; |
| 246 | } |
| 247 | if (timeLeft <= 0) { |
| 248 | slog.info("MaxDisconnectionTime reached, exiting"); |
| 249 | exit(0); |
| 250 | } |
| 251 | soonestTimeout(&timeout, timeLeft * 1000); |
| 252 | } |
| 253 | |
| 254 | // Check MaxConnectionTime |
| 255 | if (rfb::Server::maxConnectionTime && lastConnectionTime && !clients.empty()) { |
| 256 | if (now < lastConnectionTime) { |
| 257 | // Someone must have set the time backwards. |
| 258 | slog.info("Time has gone backwards - resetting lastConnectionTime"); |
| 259 | lastConnectionTime = now; |
| 260 | } |
| 261 | timeLeft = lastConnectionTime + rfb::Server::maxConnectionTime - now; |
| 262 | if (timeLeft < -60) { |
| 263 | // Someone must have set the time forwards. |
| 264 | slog.info("Time has gone forwards - resetting lastConnectionTime"); |
| 265 | lastConnectionTime = now; |
| 266 | timeLeft = rfb::Server::maxConnectionTime; |
| 267 | } |
| 268 | if (timeLeft <= 0) { |
| 269 | slog.info("MaxConnectionTime reached, exiting"); |
| 270 | exit(0); |
| 271 | } |
| 272 | soonestTimeout(&timeout, timeLeft * 1000); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | // Check MaxIdleTime |
| 277 | if (rfb::Server::maxIdleTime) { |
| 278 | if (now < lastUserInputTime) { |
| 279 | // Someone must have set the time backwards. |
| 280 | slog.info("Time has gone backwards - resetting lastUserInputTime"); |
| 281 | lastUserInputTime = now; |
| 282 | } |
| 283 | timeLeft = lastUserInputTime + rfb::Server::maxIdleTime - now; |
| 284 | if (timeLeft < -60) { |
| 285 | // Someone must have set the time forwards. |
| 286 | slog.info("Time has gone forwards - resetting lastUserInputTime"); |
| 287 | lastUserInputTime = now; |
| 288 | timeLeft = rfb::Server::maxIdleTime; |
| 289 | } |
| 290 | if (timeLeft <= 0) { |
| 291 | slog.info("MaxIdleTime reached, exiting"); |
| 292 | exit(0); |
| 293 | } |
| 294 | soonestTimeout(&timeout, timeLeft * 1000); |
| 295 | } |
| 296 | |
| 297 | return timeout; |
| 298 | } |
| 299 | |
| 300 | |
| 301 | // VNCServer methods |
| 302 | |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 303 | void VNCServerST::blockUpdates() |
| 304 | { |
| 305 | blockCounter++; |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 306 | |
| 307 | stopFrameClock(); |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | void VNCServerST::unblockUpdates() |
| 311 | { |
| 312 | assert(blockCounter > 0); |
| 313 | |
| 314 | blockCounter--; |
| 315 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 316 | // Restart the frame clock if we have updates |
| 317 | if (blockCounter == 0) { |
| 318 | if (!comparer->is_empty()) |
| 319 | startFrameClock(); |
| 320 | } |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 323 | void VNCServerST::setPixelBuffer(PixelBuffer* pb_, const ScreenSet& layout) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 324 | { |
Pierre Ossman | 05338bc | 2016-11-08 14:57:11 +0100 | [diff] [blame] | 325 | if (comparer) |
| 326 | comparer->logStats(); |
| 327 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 328 | pb = pb_; |
| 329 | delete comparer; |
| 330 | comparer = 0; |
| 331 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 332 | screenLayout = layout; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 333 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 334 | if (!pb) { |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 335 | screenLayout = ScreenSet(); |
| 336 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 337 | if (desktopStarted) |
| 338 | throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?"); |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 339 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 340 | return; |
| 341 | } |
| 342 | |
Pierre Ossman | 6cd6117 | 2018-05-07 14:24:56 +0200 | [diff] [blame] | 343 | // Assume the framebuffer contents wasn't saved and reset everything |
| 344 | // that tracks its contents |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 345 | comparer = new ComparingUpdateTracker(pb); |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 346 | renderedCursorInvalid = true; |
Pierre Ossman | 6cd6117 | 2018-05-07 14:24:56 +0200 | [diff] [blame] | 347 | add_changed(pb->getRect()); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 348 | |
| 349 | // Make sure that we have at least one screen |
| 350 | if (screenLayout.num_screens() == 0) |
| 351 | screenLayout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0)); |
| 352 | |
| 353 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 354 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 355 | ci_next = ci; ci_next++; |
| 356 | (*ci)->pixelBufferChange(); |
| 357 | // Since the new pixel buffer means an ExtendedDesktopSize needs to |
| 358 | // be sent anyway, we don't need to call screenLayoutChange. |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void VNCServerST::setPixelBuffer(PixelBuffer* pb_) |
| 363 | { |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 364 | ScreenSet layout = screenLayout; |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 365 | |
| 366 | // Check that the screen layout is still valid |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 367 | if (pb_ && !layout.validate(pb_->width(), pb_->height())) { |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 368 | Rect fbRect; |
| 369 | ScreenSet::iterator iter, iter_next; |
| 370 | |
| 371 | fbRect.setXYWH(0, 0, pb_->width(), pb_->height()); |
| 372 | |
| 373 | for (iter = layout.begin();iter != layout.end();iter = iter_next) { |
| 374 | iter_next = iter; ++iter_next; |
| 375 | if (iter->dimensions.enclosed_by(fbRect)) |
| 376 | continue; |
| 377 | iter->dimensions = iter->dimensions.intersect(fbRect); |
| 378 | if (iter->dimensions.is_empty()) { |
| 379 | slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer", |
| 380 | (int)iter->id, (unsigned)iter->id); |
| 381 | layout.remove_screen(iter->id); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | setPixelBuffer(pb_, layout); |
| 387 | } |
| 388 | |
| 389 | void VNCServerST::setScreenLayout(const ScreenSet& layout) |
| 390 | { |
| 391 | if (!pb) |
| 392 | throw Exception("setScreenLayout: new screen layout without a PixelBuffer"); |
| 393 | if (!layout.validate(pb->width(), pb->height())) |
| 394 | throw Exception("setScreenLayout: invalid screen layout"); |
| 395 | |
Pierre Ossman | df45320 | 2009-04-02 14:26:45 +0000 | [diff] [blame] | 396 | screenLayout = layout; |
| 397 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 398 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 399 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 400 | ci_next = ci; ci_next++; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 401 | (*ci)->screenLayoutChangeOrClose(reasonServer); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 405 | void VNCServerST::bell() |
| 406 | { |
| 407 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 408 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 409 | ci_next = ci; ci_next++; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 410 | (*ci)->bellOrClose(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
| 414 | void VNCServerST::serverCutText(const char* str, int len) |
| 415 | { |
| 416 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 417 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 418 | ci_next = ci; ci_next++; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 419 | (*ci)->serverCutTextOrClose(str, len); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 423 | void VNCServerST::setName(const char* name_) |
| 424 | { |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 425 | name.replaceBuf(strDup(name_)); |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 426 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 427 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 428 | ci_next = ci; ci_next++; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 429 | (*ci)->setDesktopNameOrClose(name_); |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 433 | void VNCServerST::add_changed(const Region& region) |
| 434 | { |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 435 | if (comparer == NULL) |
| 436 | return; |
| 437 | |
| 438 | comparer->add_changed(region); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 439 | startFrameClock(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | void VNCServerST::add_copied(const Region& dest, const Point& delta) |
| 443 | { |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 444 | if (comparer == NULL) |
| 445 | return; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 446 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 447 | comparer->add_copied(dest, delta); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 448 | startFrameClock(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | void VNCServerST::setCursor(int width, int height, const Point& newHotspot, |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 452 | const rdr::U8* data) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 453 | { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 454 | delete cursor; |
| 455 | cursor = new Cursor(width, height, newHotspot, data); |
| 456 | cursor->crop(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 457 | |
| 458 | renderedCursorInvalid = true; |
| 459 | |
| 460 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 461 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 462 | ci_next = ci; ci_next++; |
| 463 | (*ci)->renderedCursorChange(); |
| 464 | (*ci)->setCursorOrClose(); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | void VNCServerST::setCursorPos(const Point& pos) |
| 469 | { |
| 470 | if (!cursorPos.equals(pos)) { |
| 471 | cursorPos = pos; |
| 472 | renderedCursorInvalid = true; |
| 473 | std::list<VNCSConnectionST*>::iterator ci; |
| 474 | for (ci = clients.begin(); ci != clients.end(); ci++) |
| 475 | (*ci)->renderedCursorChange(); |
| 476 | } |
| 477 | } |
| 478 | |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 479 | void VNCServerST::setLEDState(unsigned int state) |
| 480 | { |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 481 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 482 | |
| 483 | if (state == ledState) |
| 484 | return; |
| 485 | |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 486 | ledState = state; |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 487 | |
| 488 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 489 | ci_next = ci; ci_next++; |
| 490 | (*ci)->setLEDStateOrClose(state); |
| 491 | } |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 492 | } |
| 493 | |
Pierre Ossman | b684341 | 2018-10-05 17:30:52 +0200 | [diff] [blame] | 494 | // Event handlers |
| 495 | |
| 496 | void VNCServerST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) |
| 497 | { |
| 498 | lastUserInputTime = time(0); |
| 499 | |
| 500 | // Remap the key if required |
| 501 | if (keyRemapper) { |
| 502 | rdr::U32 newkey; |
| 503 | newkey = keyRemapper->remapKey(keysym); |
| 504 | if (newkey != keysym) { |
| 505 | slog.debug("Key remapped to 0x%x", newkey); |
| 506 | keysym = newkey; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | desktop->keyEvent(keysym, keycode, down); |
| 511 | } |
| 512 | |
| 513 | void VNCServerST::pointerEvent(VNCSConnectionST* client, |
| 514 | const Point& pos, int buttonMask) |
| 515 | { |
| 516 | lastUserInputTime = time(0); |
| 517 | |
| 518 | // Let one client own the cursor whilst buttons are pressed in order |
| 519 | // to provide a bit more sane user experience |
| 520 | if ((pointerClient != NULL) && (pointerClient != client)) |
| 521 | return; |
| 522 | |
| 523 | if (buttonMask) |
| 524 | pointerClient = client; |
| 525 | else |
| 526 | pointerClient = NULL; |
| 527 | |
| 528 | desktop->pointerEvent(pos, buttonMask); |
| 529 | } |
| 530 | |
| 531 | void VNCServerST::clientCutText(const char* str, int len) |
| 532 | { |
| 533 | desktop->clientCutText(str, len); |
| 534 | } |
| 535 | |
Pierre Ossman | 07e44cc | 2018-10-05 17:32:57 +0200 | [diff] [blame] | 536 | unsigned int VNCServerST::setDesktopSize(VNCSConnectionST* requester, |
| 537 | int fb_width, int fb_height, |
| 538 | const ScreenSet& layout) |
| 539 | { |
| 540 | unsigned int result; |
| 541 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 542 | |
| 543 | // Don't bother the desktop with an invalid configuration |
| 544 | if (!layout.validate(fb_width, fb_height)) |
| 545 | return resultInvalid; |
| 546 | |
| 547 | // FIXME: the desktop will call back to VNCServerST and an extra set |
| 548 | // of ExtendedDesktopSize messages will be sent. This is okay |
| 549 | // protocol-wise, but unnecessary. |
| 550 | result = desktop->setScreenLayout(fb_width, fb_height, layout); |
| 551 | if (result != resultSuccess) |
| 552 | return result; |
| 553 | |
| 554 | // Sanity check |
| 555 | if (screenLayout != layout) |
| 556 | throw Exception("Desktop configured a different screen layout than requested"); |
| 557 | |
| 558 | // Notify other clients |
| 559 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 560 | ci_next = ci; ci_next++; |
| 561 | if ((*ci) == requester) |
| 562 | continue; |
| 563 | (*ci)->screenLayoutChangeOrClose(reasonOtherClient); |
| 564 | } |
| 565 | |
| 566 | return resultSuccess; |
| 567 | } |
| 568 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 569 | // Other public methods |
| 570 | |
| 571 | void VNCServerST::approveConnection(network::Socket* sock, bool accept, |
| 572 | const char* reason) |
| 573 | { |
| 574 | std::list<VNCSConnectionST*>::iterator ci; |
| 575 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 576 | if ((*ci)->getSock() == sock) { |
| 577 | (*ci)->approveConnectionOrClose(accept, reason); |
| 578 | return; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | void VNCServerST::closeClients(const char* reason, network::Socket* except) |
| 584 | { |
| 585 | std::list<VNCSConnectionST*>::iterator i, next_i; |
| 586 | for (i=clients.begin(); i!=clients.end(); i=next_i) { |
| 587 | next_i = i; next_i++; |
| 588 | if ((*i)->getSock() != except) |
| 589 | (*i)->close(reason); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | void VNCServerST::getSockets(std::list<network::Socket*>* sockets) |
| 594 | { |
| 595 | sockets->clear(); |
| 596 | std::list<VNCSConnectionST*>::iterator ci; |
| 597 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 598 | sockets->push_back((*ci)->getSock()); |
| 599 | } |
| 600 | std::list<network::Socket*>::iterator si; |
| 601 | for (si = closingSockets.begin(); si != closingSockets.end(); si++) { |
| 602 | sockets->push_back(*si); |
| 603 | } |
| 604 | } |
| 605 | |
Pierre Ossman | 7d64b33 | 2018-10-08 15:59:02 +0200 | [diff] [blame] | 606 | SConnection* VNCServerST::getConnection(network::Socket* sock) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 607 | std::list<VNCSConnectionST*>::iterator ci; |
| 608 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 609 | if ((*ci)->getSock() == sock) |
| 610 | return *ci; |
| 611 | } |
| 612 | return 0; |
| 613 | } |
| 614 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 615 | bool VNCServerST::handleTimeout(Timer* t) |
| 616 | { |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 617 | if (t == &frameTimer) { |
| 618 | // We keep running until we go a full interval without any updates |
| 619 | if (comparer->is_empty()) |
| 620 | return false; |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 621 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 622 | writeUpdate(); |
Pierre Ossman | 7be73d7 | 2017-11-06 13:16:35 +0100 | [diff] [blame] | 623 | |
| 624 | // If this is the first iteration then we need to adjust the timeout |
| 625 | if (frameTimer.getTimeoutMs() != 1000/rfb::Server::frameRate) { |
| 626 | frameTimer.start(1000/rfb::Server::frameRate); |
| 627 | return false; |
| 628 | } |
| 629 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 630 | return true; |
| 631 | } |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 632 | |
| 633 | return false; |
| 634 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 635 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 636 | void VNCServerST::queryConnection(VNCSConnectionST* client, |
Pierre Ossman | eef6c9a | 2018-10-05 17:11:25 +0200 | [diff] [blame] | 637 | const char* userName) |
| 638 | { |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 639 | // - Authentication succeeded - clear from blacklist |
| 640 | CharArray name; |
| 641 | name.buf = client->getSock()->getPeerAddress(); |
| 642 | blHosts->clearBlackmark(name.buf); |
| 643 | |
| 644 | // - Prepare the desktop for that the client will start requiring |
| 645 | // resources after this |
| 646 | startDesktop(); |
| 647 | |
| 648 | // - Special case to provide a more useful error message |
| 649 | if (rfb::Server::neverShared && |
| 650 | !rfb::Server::disconnectClients && |
| 651 | authClientCount() > 0) { |
| 652 | approveConnection(client->getSock(), false, |
| 653 | "The server is already in use"); |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | // - Are we configured to do queries? |
| 658 | if (!rfb::Server::queryConnect && |
| 659 | !client->getSock()->requiresQuery()) { |
| 660 | approveConnection(client->getSock(), true, NULL); |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | // - Does the client have the right to bypass the query? |
| 665 | if (client->accessCheck(SConnection::AccessNoQuery)) |
| 666 | { |
| 667 | approveConnection(client->getSock(), true, NULL); |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | desktop->queryConnection(client->getSock(), userName); |
| 672 | } |
| 673 | |
| 674 | void VNCServerST::clientReady(VNCSConnectionST* client, bool shared) |
| 675 | { |
| 676 | if (!shared) { |
| 677 | if (rfb::Server::disconnectClients && |
| 678 | client->accessCheck(SConnection::AccessNonShared)) { |
| 679 | // - Close all the other connected clients |
| 680 | slog.debug("non-shared connection - closing clients"); |
| 681 | closeClients("Non-shared connection requested", client->getSock()); |
| 682 | } else { |
| 683 | // - Refuse this connection if there are existing clients, in addition to |
| 684 | // this one |
| 685 | if (authClientCount() > 1) { |
| 686 | client->close("Server is already in use"); |
| 687 | return; |
| 688 | } |
| 689 | } |
| 690 | } |
Pierre Ossman | eef6c9a | 2018-10-05 17:11:25 +0200 | [diff] [blame] | 691 | } |
| 692 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 693 | // -=- Internal methods |
| 694 | |
| 695 | void VNCServerST::startDesktop() |
| 696 | { |
| 697 | if (!desktopStarted) { |
| 698 | slog.debug("starting desktop"); |
| 699 | desktop->start(this); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 700 | if (!pb) |
| 701 | throw Exception("SDesktop::start() did not set a valid PixelBuffer"); |
Pierre Ossman | 6cd6117 | 2018-05-07 14:24:56 +0200 | [diff] [blame] | 702 | desktopStarted = true; |
| 703 | // The tracker might have accumulated changes whilst we were |
| 704 | // stopped, so flush those out |
| 705 | if (!comparer->is_empty()) |
| 706 | writeUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 710 | void VNCServerST::stopDesktop() |
| 711 | { |
| 712 | if (desktopStarted) { |
| 713 | slog.debug("stopping desktop"); |
| 714 | desktopStarted = false; |
| 715 | desktop->stop(); |
| 716 | stopFrameClock(); |
| 717 | } |
| 718 | } |
| 719 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 720 | int VNCServerST::authClientCount() { |
| 721 | int count = 0; |
| 722 | std::list<VNCSConnectionST*>::iterator ci; |
| 723 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 724 | if ((*ci)->authenticated()) |
| 725 | count++; |
| 726 | } |
| 727 | return count; |
| 728 | } |
| 729 | |
| 730 | inline bool VNCServerST::needRenderedCursor() |
| 731 | { |
| 732 | std::list<VNCSConnectionST*>::iterator ci; |
| 733 | for (ci = clients.begin(); ci != clients.end(); ci++) |
| 734 | if ((*ci)->needRenderedCursor()) return true; |
| 735 | return false; |
| 736 | } |
| 737 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 738 | void VNCServerST::startFrameClock() |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 739 | { |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 740 | if (frameTimer.isStarted()) |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 741 | return; |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 742 | if (blockCounter > 0) |
| 743 | return; |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 744 | if (!desktopStarted) |
| 745 | return; |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 746 | |
Pierre Ossman | 7be73d7 | 2017-11-06 13:16:35 +0100 | [diff] [blame] | 747 | // The first iteration will be just half a frame as we get a very |
| 748 | // unstable update rate if we happen to be perfectly in sync with |
| 749 | // the application's update rate |
| 750 | frameTimer.start(1000/rfb::Server::frameRate/2); |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 753 | void VNCServerST::stopFrameClock() |
| 754 | { |
| 755 | frameTimer.stop(); |
| 756 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 757 | |
Pierre Ossman | a2b80d6 | 2018-03-23 09:30:09 +0100 | [diff] [blame] | 758 | int VNCServerST::msToNextUpdate() |
| 759 | { |
| 760 | // FIXME: If the application is updating slower than frameRate then |
| 761 | // we could allow the clients more time here |
| 762 | |
| 763 | if (!frameTimer.isStarted()) |
| 764 | return 1000/rfb::Server::frameRate/2; |
| 765 | else |
| 766 | return frameTimer.getRemainingMs(); |
| 767 | } |
| 768 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 769 | // writeUpdate() is called on a regular interval in order to see what |
| 770 | // updates are pending and propagates them to the update tracker for |
| 771 | // each client. It uses the ComparingUpdateTracker's compare() method |
| 772 | // to filter out areas of the screen which haven't actually changed. It |
| 773 | // also checks the state of the (server-side) rendered cursor, if |
| 774 | // necessary rendering it again with the correct background. |
| 775 | |
| 776 | void VNCServerST::writeUpdate() |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 777 | { |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 778 | UpdateInfo ui; |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 779 | Region toCheck; |
| 780 | |
| 781 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 782 | |
| 783 | assert(blockCounter == 0); |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 784 | assert(desktopStarted); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 785 | |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 786 | comparer->getUpdateInfo(&ui, pb->getRect()); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 787 | toCheck = ui.changed.union_(ui.copied); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 788 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 789 | if (needRenderedCursor()) { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 790 | Rect clippedCursorRect = Rect(0, 0, cursor->width(), cursor->height()) |
| 791 | .translate(cursorPos.subtract(cursor->hotspot())) |
| 792 | .intersect(pb->getRect()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 793 | |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 794 | if (!toCheck.intersect(clippedCursorRect).is_empty()) |
| 795 | renderedCursorInvalid = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | pb->grabRegion(toCheck); |
| 799 | |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 800 | if (getComparerState()) |
| 801 | comparer->enable(); |
| 802 | else |
| 803 | comparer->disable(); |
| 804 | |
| 805 | if (comparer->compare()) |
Constantin Kaplinsky | f0b3be7 | 2008-08-21 05:22:04 +0000 | [diff] [blame] | 806 | comparer->getUpdateInfo(&ui, pb->getRect()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 807 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 808 | comparer->clear(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 809 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 810 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 811 | ci_next = ci; ci_next++; |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 812 | (*ci)->add_copied(ui.copied, ui.copy_delta); |
| 813 | (*ci)->add_changed(ui.changed); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 814 | (*ci)->writeFramebufferUpdateOrClose(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 815 | } |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 816 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 817 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 818 | // checkUpdate() is called by clients to see if it is safe to read from |
| 819 | // the framebuffer at this time. |
| 820 | |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 821 | Region VNCServerST::getPendingRegion() |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 822 | { |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 823 | UpdateInfo ui; |
| 824 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 825 | // Block clients as the frame buffer cannot be safely accessed |
| 826 | if (blockCounter > 0) |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 827 | return pb->getRect(); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 828 | |
| 829 | // Block client from updating if there are pending updates |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 830 | if (comparer->is_empty()) |
| 831 | return Region(); |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 832 | |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 833 | comparer->getUpdateInfo(&ui, pb->getRect()); |
| 834 | |
| 835 | return ui.changed.union_(ui.copied); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 838 | const RenderedCursor* VNCServerST::getRenderedCursor() |
| 839 | { |
| 840 | if (renderedCursorInvalid) { |
Pierre Ossman | 7cb4f31 | 2017-02-24 13:25:00 +0100 | [diff] [blame] | 841 | renderedCursor.update(pb, cursor, cursorPos); |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 842 | renderedCursorInvalid = false; |
| 843 | } |
| 844 | |
| 845 | return &renderedCursor; |
| 846 | } |
| 847 | |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 848 | bool VNCServerST::getComparerState() |
| 849 | { |
| 850 | if (rfb::Server::compareFB == 0) |
| 851 | return false; |
| 852 | if (rfb::Server::compareFB != 2) |
| 853 | return true; |
| 854 | |
| 855 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 856 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 857 | ci_next = ci; ci_next++; |
| 858 | if ((*ci)->getComparerState()) |
| 859 | return true; |
| 860 | } |
| 861 | return false; |
| 862 | } |