Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 2 | * Copyright 2009 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 | #include <rfb/VNCSConnectionST.h> |
| 21 | #include <rfb/LogWriter.h> |
| 22 | #include <rfb/secTypes.h> |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 23 | #include <rfb/screenTypes.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 24 | #include <rfb/ServerCore.h> |
| 25 | #include <rfb/ComparingUpdateTracker.h> |
| 26 | #include <rfb/KeyRemapper.h> |
| 27 | #define XK_MISCELLANY |
| 28 | #define XK_XKB_KEYS |
| 29 | #include <rfb/keysymdef.h> |
| 30 | |
| 31 | using namespace rfb; |
| 32 | |
| 33 | static LogWriter vlog("VNCSConnST"); |
| 34 | |
| 35 | VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s, |
| 36 | bool reverse) |
| 37 | : SConnection(server_->securityFactory, reverse), sock(s), server(server_), |
| 38 | updates(false), image_getter(server->useEconomicTranslate), |
| 39 | drawRenderedCursor(false), removeRenderedCursor(false), |
| 40 | pointerEventTime(0), accessRights(AccessDefault), |
Pierre Ossman | f99c571 | 2009-03-13 14:41:27 +0000 | [diff] [blame] | 41 | startTime(time(0)) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 42 | { |
| 43 | setStreams(&sock->inStream(), &sock->outStream()); |
| 44 | peerEndpoint.buf = sock->getPeerEndpoint(); |
| 45 | VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf); |
| 46 | |
| 47 | // Configure the socket |
| 48 | setSocketTimeouts(); |
| 49 | lastEventTime = time(0); |
| 50 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 51 | server->clients.push_front(this); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | VNCSConnectionST::~VNCSConnectionST() |
| 56 | { |
| 57 | // If we reach here then VNCServerST is deleting us! |
| 58 | VNCServerST::connectionsLog.write(1,"closed: %s (%s)", |
| 59 | peerEndpoint.buf, |
| 60 | (closeReason.buf) ? closeReason.buf : ""); |
| 61 | |
| 62 | // Release any keys the client still had pressed |
| 63 | std::set<rdr::U32>::iterator i; |
| 64 | for (i=pressedKeys.begin(); i!=pressedKeys.end(); i++) |
| 65 | server->desktop->keyEvent(*i, false); |
| 66 | if (server->pointerClient == this) |
| 67 | server->pointerClient = 0; |
| 68 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 69 | // Remove this client from the server |
| 70 | server->clients.remove(this); |
| 71 | |
| 72 | } |
| 73 | |
| 74 | |
| 75 | // Methods called from VNCServerST |
| 76 | |
| 77 | bool VNCSConnectionST::init() |
| 78 | { |
| 79 | try { |
| 80 | initialiseProtocol(); |
| 81 | } catch (rdr::Exception& e) { |
| 82 | close(e.str()); |
| 83 | return false; |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | void VNCSConnectionST::close(const char* reason) |
| 89 | { |
| 90 | // Log the reason for the close |
| 91 | if (!closeReason.buf) |
| 92 | closeReason.buf = strDup(reason); |
| 93 | else |
| 94 | vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason); |
| 95 | |
| 96 | if (authenticated()) { |
| 97 | server->lastDisconnectTime = time(0); |
| 98 | } |
| 99 | |
| 100 | // Just shutdown the socket and mark our state as closing. Eventually the |
| 101 | // calling code will call VNCServerST's removeSocket() method causing us to |
| 102 | // be deleted. |
| 103 | sock->shutdown(); |
| 104 | setState(RFBSTATE_CLOSING); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void VNCSConnectionST::processMessages() |
| 109 | { |
| 110 | if (state() == RFBSTATE_CLOSING) return; |
| 111 | try { |
| 112 | // - Now set appropriate socket timeouts and process data |
| 113 | setSocketTimeouts(); |
| 114 | bool clientsReadyBefore = server->clientsReadyForUpdate(); |
| 115 | |
| 116 | while (getInStream()->checkNoWait(1)) { |
| 117 | processMsg(); |
| 118 | } |
| 119 | |
Constantin Kaplinsky | 2ef6695 | 2008-08-29 11:33:46 +0000 | [diff] [blame] | 120 | // If there were update requests, try to send a framebuffer update. |
Pierre Ossman | 02e43d7 | 2009-03-05 11:57:11 +0000 | [diff] [blame] | 121 | // We don't send updates immediately on requests as this way, we |
| 122 | // give higher priority to user actions such as keyboard and |
| 123 | // pointer events. |
Constantin Kaplinsky | 2ef6695 | 2008-08-29 11:33:46 +0000 | [diff] [blame] | 124 | if (!requested.is_empty()) { |
| 125 | writeFramebufferUpdate(); |
| 126 | } |
| 127 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 128 | if (!clientsReadyBefore && !requested.is_empty()) |
| 129 | server->desktop->framebufferUpdateRequest(); |
| 130 | } catch (rdr::EndOfStream&) { |
| 131 | close("Clean disconnection"); |
| 132 | } catch (rdr::Exception &e) { |
| 133 | close(e.str()); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void VNCSConnectionST::writeFramebufferUpdateOrClose() |
| 138 | { |
| 139 | try { |
| 140 | writeFramebufferUpdate(); |
| 141 | } catch(rdr::Exception &e) { |
| 142 | close(e.str()); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void VNCSConnectionST::pixelBufferChange() |
| 147 | { |
| 148 | try { |
| 149 | if (!authenticated()) return; |
| 150 | if (cp.width && cp.height && (server->pb->width() != cp.width || |
| 151 | server->pb->height() != cp.height)) |
| 152 | { |
| 153 | // We need to clip the next update to the new size, but also add any |
| 154 | // extra bits if it's bigger. If we wanted to do this exactly, something |
| 155 | // like the code below would do it, but at the moment we just update the |
| 156 | // entire new size. However, we do need to clip the renderedCursorRect |
| 157 | // because that might be added to updates in writeFramebufferUpdate(). |
| 158 | |
| 159 | //updates.intersect(server->pb->getRect()); |
| 160 | // |
| 161 | //if (server->pb->width() > cp.width) |
| 162 | // updates.add_changed(Rect(cp.width, 0, server->pb->width(), |
| 163 | // server->pb->height())); |
| 164 | //if (server->pb->height() > cp.height) |
| 165 | // updates.add_changed(Rect(0, cp.height, cp.width, |
| 166 | // server->pb->height())); |
| 167 | |
| 168 | renderedCursorRect = renderedCursorRect.intersect(server->pb->getRect()); |
| 169 | |
| 170 | cp.width = server->pb->width(); |
| 171 | cp.height = server->pb->height(); |
Pierre Ossman | 34e62f3 | 2009-03-20 21:46:12 +0000 | [diff] [blame] | 172 | cp.screenLayout = server->screenLayout; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 173 | if (state() == RFBSTATE_NORMAL) { |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 174 | if (!writer()->writeSetDesktopSize() && |
| 175 | !writer()->writeExtendedDesktopSize()) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 176 | close("Client does not support desktop resize"); |
| 177 | return; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | // Just update the whole screen at the moment because we're too lazy to |
| 182 | // work out what's actually changed. |
| 183 | updates.clear(); |
| 184 | updates.add_changed(server->pb->getRect()); |
| 185 | vlog.debug("pixel buffer changed - re-initialising image getter"); |
| 186 | image_getter.init(server->pb, cp.pf(), writer()); |
| 187 | if (writer()->needFakeUpdate()) |
| 188 | writeFramebufferUpdate(); |
| 189 | } catch(rdr::Exception &e) { |
| 190 | close(e.str()); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void VNCSConnectionST::setColourMapEntriesOrClose(int firstColour,int nColours) |
| 195 | { |
| 196 | try { |
| 197 | setColourMapEntries(firstColour, nColours); |
| 198 | } catch(rdr::Exception& e) { |
| 199 | close(e.str()); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void VNCSConnectionST::bell() |
| 204 | { |
| 205 | try { |
| 206 | if (state() == RFBSTATE_NORMAL) writer()->writeBell(); |
| 207 | } catch(rdr::Exception& e) { |
| 208 | close(e.str()); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | void VNCSConnectionST::serverCutText(const char *str, int len) |
| 213 | { |
| 214 | try { |
| 215 | if (!(accessRights & AccessCutText)) return; |
| 216 | if (!rfb::Server::sendCutText) return; |
| 217 | if (state() == RFBSTATE_NORMAL) |
| 218 | writer()->writeServerCutText(str, len); |
| 219 | } catch(rdr::Exception& e) { |
| 220 | close(e.str()); |
| 221 | } |
| 222 | } |
| 223 | |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 224 | |
| 225 | void VNCSConnectionST::setDesktopName(const char *name) |
| 226 | { |
| 227 | cp.setName(name); |
| 228 | try { |
| 229 | if (state() == RFBSTATE_NORMAL) { |
| 230 | if (!writer()->writeSetDesktopName()) { |
| 231 | fprintf(stderr, "Client does not support desktop rename\n"); |
| 232 | } |
| 233 | } |
| 234 | } catch(rdr::Exception& e) { |
| 235 | close(e.str()); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 240 | void VNCSConnectionST::setCursorOrClose() |
| 241 | { |
| 242 | try { |
| 243 | setCursor(); |
| 244 | } catch(rdr::Exception& e) { |
| 245 | close(e.str()); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | |
| 250 | int VNCSConnectionST::checkIdleTimeout() |
| 251 | { |
| 252 | int idleTimeout = rfb::Server::idleTimeout; |
| 253 | if (idleTimeout == 0) return 0; |
| 254 | if (state() != RFBSTATE_NORMAL && idleTimeout < 15) |
| 255 | idleTimeout = 15; // minimum of 15 seconds while authenticating |
| 256 | time_t now = time(0); |
| 257 | if (now < lastEventTime) { |
| 258 | // Someone must have set the time backwards. Set lastEventTime so that the |
| 259 | // idleTimeout will count from now. |
| 260 | vlog.info("Time has gone backwards - resetting idle timeout"); |
| 261 | lastEventTime = now; |
| 262 | } |
| 263 | int timeLeft = lastEventTime + idleTimeout - now; |
| 264 | if (timeLeft < -60) { |
| 265 | // Our callback is over a minute late - someone must have set the time |
| 266 | // forwards. Set lastEventTime so that the idleTimeout will count from |
| 267 | // now. |
| 268 | vlog.info("Time has gone forwards - resetting idle timeout"); |
| 269 | lastEventTime = now; |
| 270 | return secsToMillis(idleTimeout); |
| 271 | } |
| 272 | if (timeLeft <= 0) { |
| 273 | close("Idle timeout"); |
| 274 | return 0; |
| 275 | } |
| 276 | return secsToMillis(timeLeft); |
| 277 | } |
| 278 | |
| 279 | // renderedCursorChange() is called whenever the server-side rendered cursor |
| 280 | // changes shape or position. It ensures that the next update will clean up |
| 281 | // the old rendered cursor and if necessary draw the new rendered cursor. |
| 282 | |
| 283 | void VNCSConnectionST::renderedCursorChange() |
| 284 | { |
| 285 | if (state() != RFBSTATE_NORMAL) return; |
| 286 | removeRenderedCursor = true; |
| 287 | if (needRenderedCursor()) |
| 288 | drawRenderedCursor = true; |
| 289 | } |
| 290 | |
| 291 | // needRenderedCursor() returns true if this client needs the server-side |
| 292 | // rendered cursor. This may be because it does not support local cursor or |
| 293 | // because the current cursor position has not been set by this client. |
| 294 | // Unfortunately we can't know for sure when the current cursor position has |
| 295 | // been set by this client. We guess that this is the case when the current |
| 296 | // cursor position is the same as the last pointer event from this client, or |
| 297 | // if it is a very short time since this client's last pointer event (up to a |
| 298 | // second). [ Ideally we should do finer-grained timing here and make the time |
| 299 | // configurable, but I don't think it's that important. ] |
| 300 | |
| 301 | bool VNCSConnectionST::needRenderedCursor() |
| 302 | { |
| 303 | return (state() == RFBSTATE_NORMAL |
| 304 | && (!cp.supportsLocalCursor && !cp.supportsLocalXCursor |
| 305 | || (!server->cursorPos.equals(pointerEventPos) && |
| 306 | (time(0) - pointerEventTime) > 0))); |
| 307 | } |
| 308 | |
| 309 | |
| 310 | void VNCSConnectionST::approveConnectionOrClose(bool accept, |
| 311 | const char* reason) |
| 312 | { |
| 313 | try { |
| 314 | approveConnection(accept, reason); |
| 315 | } catch (rdr::Exception& e) { |
| 316 | close(e.str()); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | |
| 321 | |
| 322 | // -=- Callbacks from SConnection |
| 323 | |
| 324 | void VNCSConnectionST::authSuccess() |
| 325 | { |
| 326 | lastEventTime = time(0); |
| 327 | |
| 328 | server->startDesktop(); |
| 329 | |
| 330 | // - Set the connection parameters appropriately |
| 331 | cp.width = server->pb->width(); |
| 332 | cp.height = server->pb->height(); |
Pierre Ossman | 34e62f3 | 2009-03-20 21:46:12 +0000 | [diff] [blame] | 333 | cp.screenLayout = server->screenLayout; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 334 | cp.setName(server->getName()); |
| 335 | |
| 336 | // - Set the default pixel format |
| 337 | cp.setPF(server->pb->getPF()); |
| 338 | char buffer[256]; |
| 339 | cp.pf().print(buffer, 256); |
| 340 | vlog.info("Server default pixel format %s", buffer); |
| 341 | image_getter.init(server->pb, cp.pf(), 0); |
| 342 | |
| 343 | // - Mark the entire display as "dirty" |
| 344 | updates.add_changed(server->pb->getRect()); |
| 345 | startTime = time(0); |
| 346 | } |
| 347 | |
| 348 | void VNCSConnectionST::queryConnection(const char* userName) |
| 349 | { |
| 350 | // - Authentication succeeded - clear from blacklist |
| 351 | CharArray name; name.buf = sock->getPeerAddress(); |
| 352 | server->blHosts->clearBlackmark(name.buf); |
| 353 | |
| 354 | // - Special case to provide a more useful error message |
| 355 | if (rfb::Server::neverShared && !rfb::Server::disconnectClients && |
| 356 | server->authClientCount() > 0) { |
| 357 | approveConnection(false, "The server is already in use"); |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | // - Does the client have the right to bypass the query? |
| 362 | if (reverseConnection || |
| 363 | !(rfb::Server::queryConnect || sock->requiresQuery()) || |
| 364 | (accessRights & AccessNoQuery)) |
| 365 | { |
| 366 | approveConnection(true); |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | // - Get the server to display an Accept/Reject dialog, if required |
| 371 | // If a dialog is displayed, the result will be PENDING, and the |
| 372 | // server will call approveConnection at a later time |
| 373 | CharArray reason; |
| 374 | VNCServerST::queryResult qr = server->queryConnection(sock, userName, |
| 375 | &reason.buf); |
| 376 | if (qr == VNCServerST::PENDING) |
| 377 | return; |
| 378 | |
| 379 | // - If server returns ACCEPT/REJECT then pass result to SConnection |
| 380 | approveConnection(qr == VNCServerST::ACCEPT, reason.buf); |
| 381 | } |
| 382 | |
| 383 | void VNCSConnectionST::clientInit(bool shared) |
| 384 | { |
| 385 | lastEventTime = time(0); |
| 386 | if (rfb::Server::alwaysShared || reverseConnection) shared = true; |
| 387 | if (rfb::Server::neverShared) shared = false; |
| 388 | if (!shared) { |
| 389 | if (rfb::Server::disconnectClients) { |
| 390 | // - Close all the other connected clients |
| 391 | vlog.debug("non-shared connection - closing clients"); |
| 392 | server->closeClients("Non-shared connection requested", getSock()); |
| 393 | } else { |
| 394 | // - Refuse this connection if there are existing clients, in addition to |
| 395 | // this one |
| 396 | if (server->authClientCount() > 1) { |
| 397 | close("Server is already in use"); |
| 398 | return; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | SConnection::clientInit(shared); |
| 403 | } |
| 404 | |
| 405 | void VNCSConnectionST::setPixelFormat(const PixelFormat& pf) |
| 406 | { |
| 407 | SConnection::setPixelFormat(pf); |
| 408 | char buffer[256]; |
| 409 | pf.print(buffer, 256); |
| 410 | vlog.info("Client pixel format %s", buffer); |
| 411 | image_getter.init(server->pb, pf, writer()); |
| 412 | setCursor(); |
| 413 | } |
| 414 | |
| 415 | void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask) |
| 416 | { |
| 417 | pointerEventTime = lastEventTime = time(0); |
| 418 | server->lastUserInputTime = lastEventTime; |
| 419 | if (!(accessRights & AccessPtrEvents)) return; |
| 420 | if (!rfb::Server::acceptPointerEvents) return; |
| 421 | if (!server->pointerClient || server->pointerClient == this) { |
| 422 | pointerEventPos = pos; |
| 423 | if (buttonMask) |
| 424 | server->pointerClient = this; |
| 425 | else |
| 426 | server->pointerClient = 0; |
| 427 | server->desktop->pointerEvent(pointerEventPos, buttonMask); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | |
| 432 | class VNCSConnectionSTShiftPresser { |
| 433 | public: |
| 434 | VNCSConnectionSTShiftPresser(SDesktop* desktop_) |
| 435 | : desktop(desktop_), pressed(false) {} |
| 436 | ~VNCSConnectionSTShiftPresser() { |
| 437 | if (pressed) { desktop->keyEvent(XK_Shift_L, false); } |
| 438 | } |
| 439 | void press() { |
| 440 | desktop->keyEvent(XK_Shift_L, true); |
| 441 | pressed = true; |
| 442 | } |
| 443 | SDesktop* desktop; |
| 444 | bool pressed; |
| 445 | }; |
| 446 | |
| 447 | // keyEvent() - record in the pressedKeys which keys were pressed. Allow |
| 448 | // multiple down events (for autorepeat), but only allow a single up event. |
| 449 | void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) { |
| 450 | lastEventTime = time(0); |
| 451 | server->lastUserInputTime = lastEventTime; |
| 452 | if (!(accessRights & AccessKeyEvents)) return; |
| 453 | if (!rfb::Server::acceptKeyEvents) return; |
| 454 | |
| 455 | // Remap the key if required |
| 456 | if (server->keyRemapper) |
| 457 | key = server->keyRemapper->remapKey(key); |
| 458 | |
| 459 | // Turn ISO_Left_Tab into shifted Tab. |
| 460 | VNCSConnectionSTShiftPresser shiftPresser(server->desktop); |
| 461 | if (key == XK_ISO_Left_Tab) { |
| 462 | if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() && |
| 463 | pressedKeys.find(XK_Shift_R) == pressedKeys.end()) |
| 464 | shiftPresser.press(); |
| 465 | key = XK_Tab; |
| 466 | } |
| 467 | |
| 468 | if (down) { |
| 469 | pressedKeys.insert(key); |
| 470 | } else { |
| 471 | if (!pressedKeys.erase(key)) return; |
| 472 | } |
| 473 | server->desktop->keyEvent(key, down); |
| 474 | } |
| 475 | |
| 476 | void VNCSConnectionST::clientCutText(const char* str, int len) |
| 477 | { |
| 478 | if (!(accessRights & AccessCutText)) return; |
| 479 | if (!rfb::Server::acceptCutText) return; |
| 480 | server->desktop->clientCutText(str, len); |
| 481 | } |
| 482 | |
| 483 | void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental) |
| 484 | { |
| 485 | if (!(accessRights & AccessView)) return; |
| 486 | |
| 487 | SConnection::framebufferUpdateRequest(r, incremental); |
| 488 | |
Pierre Ossman | d9a59ba | 2009-03-20 15:55:37 +0000 | [diff] [blame] | 489 | // Check that the client isn't sending crappy requests |
| 490 | if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) { |
| 491 | vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d", |
| 492 | r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height); |
| 493 | // We crop the size later in writeFramebufferUpdate() so no need to |
| 494 | // do so now. |
| 495 | } |
| 496 | |
Constantin Kaplinsky | 2ef6695 | 2008-08-29 11:33:46 +0000 | [diff] [blame] | 497 | // Just update the requested region. |
| 498 | // Framebuffer update will be sent a bit later, see processMessages(). |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 499 | Region reqRgn(r); |
| 500 | requested.assign_union(reqRgn); |
| 501 | |
| 502 | if (!incremental) { |
| 503 | // Non-incremental update - treat as if area requested has changed |
| 504 | updates.add_changed(reqRgn); |
| 505 | server->comparer->add_changed(reqRgn); |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 506 | // And update the clients view of screen layout |
| 507 | writer()->writeSetDesktopSize(); |
| 508 | writer()->writeExtendedDesktopSize(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 509 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Pierre Ossman | 34bb061 | 2009-03-21 21:16:14 +0000 | [diff] [blame] | 512 | void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height, |
| 513 | const ScreenSet& layout) |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 514 | { |
| 515 | vlog.info("Rejecting client request to change desktop size"); |
| 516 | writer()->writeExtendedDesktopSize(resultProhibited); |
| 517 | } |
| 518 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 519 | void VNCSConnectionST::setInitialColourMap() |
| 520 | { |
| 521 | setColourMapEntries(0, 0); |
| 522 | } |
| 523 | |
| 524 | // supportsLocalCursor() is called whenever the status of |
| 525 | // cp.supportsLocalCursor has changed. If the client does now support local |
| 526 | // cursor, we make sure that the old server-side rendered cursor is cleaned up |
| 527 | // and the cursor is sent to the client. |
| 528 | |
| 529 | void VNCSConnectionST::supportsLocalCursor() |
| 530 | { |
| 531 | if (cp.supportsLocalCursor || cp.supportsLocalXCursor) { |
| 532 | removeRenderedCursor = true; |
| 533 | drawRenderedCursor = false; |
| 534 | setCursor(); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | void VNCSConnectionST::writeSetCursorCallback() |
| 539 | { |
| 540 | if (cp.supportsLocalXCursor) { |
| 541 | Pixel pix0, pix1; |
| 542 | rdr::U8Array bitmap(server->cursor.getBitmap(&pix0, &pix1)); |
| 543 | if (bitmap.buf) { |
| 544 | // The client supports XCursor and the cursor only has two |
| 545 | // colors. Use the XCursor encoding. |
| 546 | writer()->writeSetXCursor(server->cursor.width(), |
| 547 | server->cursor.height(), |
| 548 | server->cursor.hotspot.x, |
| 549 | server->cursor.hotspot.y, |
| 550 | bitmap.buf, server->cursor.mask.buf); |
| 551 | return; |
| 552 | } else { |
| 553 | // More than two colors |
| 554 | if (!cp.supportsLocalCursor) { |
| 555 | // FIXME: We could reduce to two colors. |
| 556 | vlog.info("Unable to send multicolor cursor: RichCursor not supported by client"); |
| 557 | return; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | // Use RichCursor |
| 563 | rdr::U8* transData = writer()->getImageBuf(server->cursor.area()); |
| 564 | image_getter.translatePixels(server->cursor.data, transData, |
| 565 | server->cursor.area()); |
| 566 | writer()->writeSetCursor(server->cursor.width(), |
| 567 | server->cursor.height(), |
| 568 | server->cursor.hotspot, |
| 569 | transData, server->cursor.mask.buf); |
| 570 | } |
| 571 | |
| 572 | |
| 573 | void VNCSConnectionST::writeFramebufferUpdate() |
| 574 | { |
Pierre Ossman | d9a59ba | 2009-03-20 15:55:37 +0000 | [diff] [blame] | 575 | // The framebuffer might have changed size since the |
| 576 | // FramebufferUpdateRequest message was received. Clip it to the current |
| 577 | // size of the framebuffer. |
| 578 | requested = requested.intersect(Region(Rect(0, 0, cp.width, cp.height))); |
| 579 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 580 | if (state() != RFBSTATE_NORMAL || requested.is_empty()) return; |
| 581 | |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 582 | updates.enable_copyrect(cp.useCopyRect); |
| 583 | |
Pierre Ossman | 02e43d7 | 2009-03-05 11:57:11 +0000 | [diff] [blame] | 584 | server->checkUpdate(); |
Constantin Kaplinsky | a09dc14 | 2008-12-18 12:08:15 +0000 | [diff] [blame] | 585 | |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 586 | // Get the lists of updates. Prior to exporting the data to the `ui' object, |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 587 | // getUpdateInfo() will normalize the `updates' object such way that its |
Pierre Ossman | 02e43d7 | 2009-03-05 11:57:11 +0000 | [diff] [blame] | 588 | // `changed' and `copied' regions would not intersect. |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 589 | |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 590 | UpdateInfo ui; |
| 591 | updates.getUpdateInfo(&ui, requested); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 592 | bool needNewUpdateInfo = false; |
| 593 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 594 | // If the previous position of the rendered cursor overlaps the source of the |
| 595 | // copy, then when the copy happens the corresponding rectangle in the |
| 596 | // destination will be wrong, so add it to the changed region. |
| 597 | |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 598 | if (!ui.copied.is_empty() && !renderedCursorRect.is_empty()) { |
| 599 | Rect bogusCopiedCursor = (renderedCursorRect.translate(ui.copy_delta) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 600 | .intersect(server->pb->getRect())); |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 601 | if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 602 | updates.add_changed(bogusCopiedCursor); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 603 | needNewUpdateInfo = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
| 607 | // If we need to remove the old rendered cursor, just add the rectangle to |
| 608 | // the changed region. |
| 609 | |
| 610 | if (removeRenderedCursor) { |
| 611 | updates.add_changed(renderedCursorRect); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 612 | needNewUpdateInfo = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 613 | renderedCursorRect.clear(); |
| 614 | removeRenderedCursor = false; |
| 615 | } |
| 616 | |
| 617 | // Return if there is nothing to send the client. |
| 618 | |
| 619 | if (updates.is_empty() && !writer()->needFakeUpdate() && !drawRenderedCursor) |
| 620 | return; |
| 621 | |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 622 | // The `updates' object could change, make sure we have valid update info. |
| 623 | |
| 624 | if (needNewUpdateInfo) |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 625 | updates.getUpdateInfo(&ui, requested); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 626 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 627 | // If the client needs a server-side rendered cursor, work out the cursor |
| 628 | // rectangle. If it's empty then don't bother drawing it, but if it overlaps |
| 629 | // with the update region, we need to draw the rendered cursor regardless of |
| 630 | // whether it has changed. |
| 631 | |
| 632 | if (needRenderedCursor()) { |
| 633 | renderedCursorRect |
| 634 | = (server->renderedCursor.getRect(server->renderedCursorTL) |
| 635 | .intersect(requested.get_bounding_rect())); |
| 636 | |
| 637 | if (renderedCursorRect.is_empty()) { |
| 638 | drawRenderedCursor = false; |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 639 | } else if (!ui.changed.union_(ui.copied) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 640 | .intersect(renderedCursorRect).is_empty()) { |
| 641 | drawRenderedCursor = true; |
| 642 | } |
| 643 | |
| 644 | // We could remove the new cursor rect from updates here. It's not clear |
| 645 | // whether this is worth it. If we do remove it, then we won't draw over |
| 646 | // the same bit of screen twice, but we have the overhead of a more complex |
| 647 | // region. |
| 648 | |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 649 | //if (drawRenderedCursor) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 650 | // updates.subtract(renderedCursorRect); |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 651 | // updates.getUpdateInfo(&ui, requested); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 652 | //} |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 655 | if (!ui.is_empty() || writer()->needFakeUpdate() || drawRenderedCursor) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 656 | // Compute the number of rectangles. Tight encoder makes the things more |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 657 | // complicated as compared to the original VNC4. |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 658 | writer()->setupCurrentEncoder(); |
Constantin Kaplinsky | 1a845d0 | 2007-08-31 21:06:53 +0000 | [diff] [blame] | 659 | int nRects = (ui.copied.numRects() + |
Constantin Kaplinsky | 1a845d0 | 2007-08-31 21:06:53 +0000 | [diff] [blame] | 660 | (drawRenderedCursor ? 1 : 0)); |
Constantin Kaplinsky | 651606d | 2007-10-17 17:40:23 +0000 | [diff] [blame] | 661 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 662 | std::vector<Rect> rects; |
| 663 | std::vector<Rect>::const_iterator i; |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 664 | ui.changed.get_rects(&rects); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 665 | for (i = rects.begin(); i != rects.end(); i++) { |
| 666 | if (i->width() && i->height()) |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 667 | nRects += writer()->getNumRects(*i); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | writer()->writeFramebufferUpdateStart(nRects); |
| 671 | Region updatedRegion; |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 672 | writer()->writeRects(ui, &image_getter, &updatedRegion); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 673 | updates.subtract(updatedRegion); |
| 674 | if (drawRenderedCursor) |
| 675 | writeRenderedCursorRect(); |
| 676 | writer()->writeFramebufferUpdateEnd(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 677 | requested.clear(); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | |
| 682 | // writeRenderedCursorRect() writes a single rectangle drawing the rendered |
| 683 | // cursor on the client. |
| 684 | |
| 685 | void VNCSConnectionST::writeRenderedCursorRect() |
| 686 | { |
| 687 | image_getter.setPixelBuffer(&server->renderedCursor); |
| 688 | image_getter.setOffset(server->renderedCursorTL); |
| 689 | |
| 690 | Rect actual; |
| 691 | writer()->writeRect(renderedCursorRect, &image_getter, &actual); |
| 692 | |
| 693 | image_getter.setPixelBuffer(server->pb); |
| 694 | image_getter.setOffset(Point(0,0)); |
| 695 | |
| 696 | drawRenderedCursor = false; |
| 697 | } |
| 698 | |
| 699 | void VNCSConnectionST::setColourMapEntries(int firstColour, int nColours) |
| 700 | { |
| 701 | if (!readyForSetColourMapEntries) return; |
| 702 | if (server->pb->getPF().trueColour) return; |
| 703 | |
| 704 | image_getter.setColourMapEntries(firstColour, nColours, writer()); |
| 705 | |
| 706 | if (cp.pf().trueColour) { |
| 707 | updates.add_changed(server->pb->getRect()); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | |
| 712 | // setCursor() is called whenever the cursor has changed shape or pixel format. |
| 713 | // If the client supports local cursor then it will arrange for the cursor to |
| 714 | // be sent to the client. |
| 715 | |
| 716 | void VNCSConnectionST::setCursor() |
| 717 | { |
| 718 | if (state() != RFBSTATE_NORMAL || !cp.supportsLocalCursor) return; |
| 719 | writer()->cursorChange(this); |
| 720 | if (writer()->needFakeUpdate()) |
| 721 | writeFramebufferUpdate(); |
| 722 | } |
| 723 | |
| 724 | void VNCSConnectionST::setSocketTimeouts() |
| 725 | { |
| 726 | int timeoutms = rfb::Server::clientWaitTimeMillis; |
| 727 | soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout)); |
| 728 | if (timeoutms == 0) |
| 729 | timeoutms = -1; |
| 730 | sock->inStream().setTimeout(timeoutms); |
| 731 | sock->outStream().setTimeout(timeoutms); |
| 732 | } |
| 733 | |
| 734 | char* VNCSConnectionST::getStartTime() |
| 735 | { |
| 736 | char* result = ctime(&startTime); |
| 737 | result[24] = '\0'; |
| 738 | return result; |
| 739 | } |
| 740 | |
| 741 | void VNCSConnectionST::setStatus(int status) |
| 742 | { |
| 743 | switch (status) { |
| 744 | case 0: |
| 745 | accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView; |
| 746 | break; |
| 747 | case 1: |
| 748 | accessRights = accessRights & !(AccessPtrEvents | AccessKeyEvents) | AccessView; |
| 749 | break; |
| 750 | case 2: |
| 751 | accessRights = accessRights & !(AccessPtrEvents | AccessKeyEvents | AccessView); |
| 752 | break; |
| 753 | } |
| 754 | framebufferUpdateRequest(server->pb->getRect(), false); |
| 755 | } |
| 756 | int VNCSConnectionST::getStatus() |
| 757 | { |
| 758 | if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007) |
| 759 | return 0; |
| 760 | if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001) |
| 761 | return 1; |
| 762 | if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000) |
| 763 | return 2; |
| 764 | return 4; |
| 765 | } |
| 766 | |