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