Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +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 | // -=- SDisplay.cxx |
| 20 | // |
| 21 | // The SDisplay class encapsulates a particular system display. |
| 22 | |
| 23 | #include <rfb_win32/SDisplay.h> |
| 24 | #include <rfb_win32/Service.h> |
| 25 | #include <rfb_win32/TsSessions.h> |
| 26 | #include <rfb_win32/CleanDesktop.h> |
| 27 | #include <rfb_win32/CurrentUser.h> |
| 28 | #include <rfb_win32/DynamicFn.h> |
| 29 | #include <rfb_win32/MonitorInfo.h> |
| 30 | #include <rfb_win32/SDisplayCorePolling.h> |
| 31 | #include <rfb_win32/SDisplayCoreWMHooks.h> |
| 32 | #include <rfb_win32/SDisplayCoreDriver.h> |
| 33 | #include <rfb/Exception.h> |
| 34 | #include <rfb/LogWriter.h> |
| 35 | |
| 36 | |
| 37 | using namespace rdr; |
| 38 | using namespace rfb; |
| 39 | using namespace rfb::win32; |
| 40 | |
| 41 | static LogWriter vlog("SDisplay"); |
| 42 | |
| 43 | // - SDisplay-specific configuration options |
| 44 | |
| 45 | IntParameter rfb::win32::SDisplay::updateMethod("UpdateMethod", |
| 46 | "How to discover desktop updates; 0 - Polling, 1 - Application hooking, 2 - Driver hooking.", 1); |
| 47 | BoolParameter rfb::win32::SDisplay::disableLocalInputs("DisableLocalInputs", |
| 48 | "Disable local keyboard and pointer input while the server is in use", false); |
| 49 | StringParameter rfb::win32::SDisplay::disconnectAction("DisconnectAction", |
| 50 | "Action to perform when all clients have disconnected. (None, Lock, Logoff)", "None"); |
| 51 | StringParameter displayDevice("DisplayDevice", |
| 52 | "Display device name of the monitor to be remoted, or empty to export the whole desktop.", ""); |
| 53 | BoolParameter rfb::win32::SDisplay::removeWallpaper("RemoveWallpaper", |
| 54 | "Remove the desktop wallpaper when the server is in use.", false); |
| 55 | BoolParameter rfb::win32::SDisplay::removePattern("RemovePattern", |
| 56 | "Remove the desktop background pattern when the server is in use.", false); |
| 57 | BoolParameter rfb::win32::SDisplay::disableEffects("DisableEffects", |
| 58 | "Disable desktop user interface effects when the server is in use.", false); |
| 59 | |
| 60 | |
| 61 | ////////////////////////////////////////////////////////////////////////////// |
| 62 | // |
| 63 | // SDisplay |
| 64 | // |
| 65 | |
| 66 | typedef BOOL (WINAPI *_LockWorkStation_proto)(); |
| 67 | DynamicFn<_LockWorkStation_proto> _LockWorkStation(_T("user32.dll"), "LockWorkStation"); |
| 68 | |
| 69 | // -=- Constructor/Destructor |
| 70 | |
| 71 | SDisplay::SDisplay() |
| 72 | : server(0), pb(0), device(0), |
| 73 | core(0), ptr(0), kbd(0), clipboard(0), |
| 74 | inputs(0), monitor(0), cleanDesktop(0), cursor(0), |
| 75 | statusLocation(0) |
| 76 | { |
| 77 | updateEvent.h = CreateEvent(0, TRUE, FALSE, 0); |
| 78 | } |
| 79 | |
| 80 | SDisplay::~SDisplay() |
| 81 | { |
| 82 | // XXX when the VNCServer has been deleted with clients active, stop() |
| 83 | // doesn't get called - this ought to be fixed in VNCServerST. In any event, |
| 84 | // we should never call any methods on VNCServer once we're being deleted. |
| 85 | // This is because it is supposed to be guaranteed that the SDesktop exists |
| 86 | // throughout the lifetime of the VNCServer. So if we're being deleted, then |
| 87 | // the VNCServer ought not to exist and therefore we shouldn't invoke any |
| 88 | // methods on it. Setting server to zero here ensures that stop() doesn't |
| 89 | // call setPixelBuffer(0) on the server. |
| 90 | server = 0; |
| 91 | if (core) stop(); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | // -=- SDesktop interface |
| 96 | |
| 97 | void SDisplay::start(VNCServer* vs) |
| 98 | { |
| 99 | vlog.debug("starting"); |
| 100 | |
| 101 | // Try to make session zero the console session |
| 102 | if (!inConsoleSession()) |
| 103 | setConsoleSession(); |
| 104 | |
| 105 | // Start the SDisplay core |
| 106 | server = vs; |
| 107 | startCore(); |
| 108 | |
| 109 | vlog.debug("started"); |
| 110 | |
| 111 | if (statusLocation) *statusLocation = true; |
| 112 | } |
| 113 | |
| 114 | void SDisplay::stop() |
| 115 | { |
| 116 | vlog.debug("stopping"); |
| 117 | |
| 118 | // If we successfully start()ed then perform the DisconnectAction |
| 119 | if (core) { |
| 120 | CurrentUserToken cut; |
Peter Åstrand | b22dbef | 2008-12-09 14:57:53 +0000 | [diff] [blame] | 121 | CharArray action(disconnectAction.getData()); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 122 | if (stricmp(action.buf, "Logoff") == 0) { |
| 123 | if (!cut.h) |
| 124 | vlog.info("ignoring DisconnectAction=Logoff - no current user"); |
| 125 | else |
| 126 | ExitWindowsEx(EWX_LOGOFF, 0); |
| 127 | } else if (stricmp(action.buf, "Lock") == 0) { |
| 128 | if (!cut.h) { |
| 129 | vlog.info("ignoring DisconnectAction=Lock - no current user"); |
| 130 | } else { |
| 131 | if (_LockWorkStation.isValid()) |
| 132 | (*_LockWorkStation)(); |
| 133 | else |
| 134 | ExitWindowsEx(EWX_LOGOFF, 0); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Stop the SDisplayCore |
| 140 | if (server) |
| 141 | server->setPixelBuffer(0); |
| 142 | stopCore(); |
| 143 | server = 0; |
| 144 | |
| 145 | vlog.debug("stopped"); |
| 146 | |
| 147 | if (statusLocation) *statusLocation = false; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | void SDisplay::startCore() { |
| 152 | |
| 153 | // Currently, we just check whether we're in the console session, and |
| 154 | // fail if not |
| 155 | if (!inConsoleSession()) |
| 156 | throw rdr::Exception("Console is not session zero - oreconnect to restore Console sessin"); |
| 157 | |
| 158 | // Switch to the current input desktop |
| 159 | if (rfb::win32::desktopChangeRequired()) { |
| 160 | if (!rfb::win32::changeDesktop()) |
| 161 | throw rdr::Exception("unable to switch into input desktop"); |
| 162 | } |
| 163 | |
| 164 | // Initialise the change tracker and clipper |
| 165 | updates.clear(); |
| 166 | clipper.setUpdateTracker(server); |
| 167 | |
| 168 | // Create the framebuffer object |
| 169 | recreatePixelBuffer(true); |
| 170 | |
| 171 | // Create the SDisplayCore |
| 172 | updateMethod_ = updateMethod; |
| 173 | int tryMethod = updateMethod_; |
| 174 | while (!core) { |
| 175 | try { |
| 176 | if (tryMethod == 2) |
| 177 | core = new SDisplayCoreDriver(this, &updates); |
| 178 | else if (tryMethod == 1) |
| 179 | core = new SDisplayCoreWMHooks(this, &updates); |
| 180 | else |
| 181 | core = new SDisplayCorePolling(this, &updates); |
| 182 | core->setScreenRect(screenRect); |
| 183 | } catch (rdr::Exception& e) { |
| 184 | delete core; core = 0; |
| 185 | if (tryMethod == 0) |
| 186 | throw rdr::Exception("unable to access desktop"); |
| 187 | tryMethod--; |
| 188 | vlog.error(e.str()); |
| 189 | } |
| 190 | } |
| 191 | vlog.info("Started %s", core->methodName()); |
| 192 | |
| 193 | // Start display monitor, clipboard handler and input handlers |
| 194 | monitor = new WMMonitor; |
| 195 | monitor->setNotifier(this); |
| 196 | clipboard = new Clipboard; |
| 197 | clipboard->setNotifier(this); |
| 198 | ptr = new SPointer; |
| 199 | kbd = new SKeyboard; |
| 200 | inputs = new WMBlockInput; |
| 201 | cursor = new WMCursor; |
| 202 | |
| 203 | // Apply desktop optimisations |
| 204 | cleanDesktop = new CleanDesktop; |
| 205 | if (removePattern) |
| 206 | cleanDesktop->disablePattern(); |
| 207 | if (removeWallpaper) |
| 208 | cleanDesktop->disableWallpaper(); |
| 209 | if (disableEffects) |
| 210 | cleanDesktop->disableEffects(); |
| 211 | isWallpaperRemoved = removeWallpaper; |
| 212 | isPatternRemoved = removePattern; |
| 213 | areEffectsDisabled = disableEffects; |
| 214 | } |
| 215 | |
| 216 | void SDisplay::stopCore() { |
| 217 | if (core) |
| 218 | vlog.info("Stopping %s", core->methodName()); |
| 219 | delete core; core = 0; |
| 220 | delete pb; pb = 0; |
| 221 | delete device; device = 0; |
| 222 | delete monitor; monitor = 0; |
| 223 | delete clipboard; clipboard = 0; |
| 224 | delete inputs; inputs = 0; |
| 225 | delete ptr; ptr = 0; |
| 226 | delete kbd; kbd = 0; |
| 227 | delete cleanDesktop; cleanDesktop = 0; |
| 228 | delete cursor; cursor = 0; |
| 229 | ResetEvent(updateEvent); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | bool SDisplay::areHooksAvailable() { |
| 234 | return WMHooks::areAvailable(); |
| 235 | } |
| 236 | |
| 237 | bool SDisplay::isDriverAvailable() { |
| 238 | return SDisplayCoreDriver::isAvailable(); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | bool SDisplay::isRestartRequired() { |
| 243 | // - We must restart the SDesktop if: |
| 244 | // 1. We are no longer in the input desktop. |
| 245 | // 2. The any setting has changed. |
| 246 | |
| 247 | // - Check that our session is the Console |
| 248 | if (!inConsoleSession()) |
| 249 | return true; |
| 250 | |
| 251 | // - Check that we are in the input desktop |
| 252 | if (rfb::win32::desktopChangeRequired()) |
| 253 | return true; |
| 254 | |
| 255 | // - Check that the update method setting hasn't changed |
| 256 | // NB: updateMethod reflects the *selected* update method, not |
| 257 | // necessarily the one in use, since we fall back to simpler |
| 258 | // methods if more advanced ones fail! |
| 259 | if (updateMethod_ != updateMethod) |
| 260 | return true; |
| 261 | |
| 262 | // - Check that the desktop optimisation settings haven't changed |
| 263 | // This isn't very efficient, but it shouldn't change very often! |
| 264 | if ((isWallpaperRemoved != removeWallpaper) || |
| 265 | (isPatternRemoved != removePattern) || |
| 266 | (areEffectsDisabled != disableEffects)) |
| 267 | return true; |
| 268 | |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | void SDisplay::restartCore() { |
| 274 | vlog.info("restarting"); |
| 275 | |
| 276 | // Stop the existing Core related resources |
| 277 | stopCore(); |
| 278 | try { |
| 279 | // Start a new Core if possible |
| 280 | startCore(); |
| 281 | vlog.info("restarted"); |
| 282 | } catch (rdr::Exception& e) { |
| 283 | // If startCore() fails then we MUST disconnect all clients, |
| 284 | // to cause the server to stop() the desktop. |
| 285 | // Otherwise, the SDesktop is in an inconsistent state |
| 286 | // and the server will crash. |
| 287 | server->closeClients(e.str()); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | |
| 292 | void SDisplay::pointerEvent(const Point& pos, int buttonmask) { |
| 293 | if (pb->getRect().contains(pos)) { |
| 294 | Point screenPos = pos.translate(screenRect.tl); |
| 295 | // - Check that the SDesktop doesn't need restarting |
| 296 | if (isRestartRequired()) |
| 297 | restartCore(); |
| 298 | if (ptr) |
| 299 | ptr->pointerEvent(screenPos, buttonmask); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | void SDisplay::keyEvent(rdr::U32 key, bool down) { |
| 304 | // - Check that the SDesktop doesn't need restarting |
| 305 | if (isRestartRequired()) |
| 306 | restartCore(); |
| 307 | if (kbd) |
| 308 | kbd->keyEvent(key, down); |
| 309 | } |
| 310 | |
| 311 | void SDisplay::clientCutText(const char* text, int len) { |
| 312 | CharArray clip_sz(len+1); |
| 313 | memcpy(clip_sz.buf, text, len); |
| 314 | clip_sz.buf[len] = 0; |
| 315 | clipboard->setClipText(clip_sz.buf); |
| 316 | } |
| 317 | |
| 318 | |
| 319 | void SDisplay::framebufferUpdateRequest() |
| 320 | { |
| 321 | SetEvent(updateEvent); |
| 322 | } |
| 323 | |
| 324 | Point SDisplay::getFbSize() { |
| 325 | bool startAndStop = !core; |
| 326 | |
| 327 | // If not started, do minimal initialisation to get desktop size. |
| 328 | if (startAndStop) |
| 329 | recreatePixelBuffer(); |
| 330 | Point result = Point(pb->width(), pb->height()); |
| 331 | |
| 332 | // Destroy the initialised structures. |
| 333 | if (startAndStop) |
| 334 | stopCore(); |
| 335 | return result; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | void |
| 340 | SDisplay::notifyClipboardChanged(const char* text, int len) { |
| 341 | vlog.debug("clipboard text changed"); |
| 342 | if (server) |
| 343 | server->serverCutText(text, len); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | void |
| 348 | SDisplay::notifyDisplayEvent(WMMonitor::Notifier::DisplayEventType evt) { |
| 349 | switch (evt) { |
| 350 | case WMMonitor::Notifier::DisplaySizeChanged: |
| 351 | vlog.debug("desktop size changed"); |
| 352 | recreatePixelBuffer(); |
| 353 | break; |
| 354 | case WMMonitor::Notifier::DisplayPixelFormatChanged: |
| 355 | vlog.debug("desktop format changed"); |
| 356 | recreatePixelBuffer(); |
| 357 | break; |
| 358 | case WMMonitor::Notifier::DisplayColourMapChanged: |
| 359 | vlog.debug("desktop colormap changed"); |
| 360 | pb->updateColourMap(); |
| 361 | if (server) |
| 362 | server->setColourMapEntries(); |
| 363 | break; |
| 364 | default: |
| 365 | vlog.error("unknown display event received"); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | void |
| 370 | SDisplay::processEvent(HANDLE event) { |
| 371 | if (event == updateEvent) { |
| 372 | vlog.write(120, "processEvent"); |
| 373 | ResetEvent(updateEvent); |
| 374 | |
| 375 | // - If the SDisplay isn't even started then quit now |
| 376 | if (!core) { |
| 377 | vlog.error("not start()ed"); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | // - Ensure that the disableLocalInputs flag is respected |
| 382 | inputs->blockInputs(disableLocalInputs); |
| 383 | |
| 384 | // - Only process updates if the server is ready |
| 385 | if (server && server->clientsReadyForUpdate()) { |
| 386 | bool try_update = false; |
| 387 | |
| 388 | // - Check that the SDesktop doesn't need restarting |
| 389 | if (isRestartRequired()) { |
| 390 | restartCore(); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | // - Flush any updates from the core |
| 395 | try { |
| 396 | core->flushUpdates(); |
| 397 | } catch (rdr::Exception& e) { |
| 398 | vlog.error(e.str()); |
| 399 | restartCore(); |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | // Ensure the cursor is up to date |
| 404 | WMCursor::Info info = cursor->getCursorInfo(); |
| 405 | if (old_cursor != info) { |
| 406 | // Update the cursor shape if the visibility has changed |
| 407 | bool set_cursor = info.visible != old_cursor.visible; |
| 408 | // OR if the cursor is visible and the shape has changed. |
| 409 | set_cursor |= info.visible && (old_cursor.cursor != info.cursor); |
| 410 | |
| 411 | // Update the cursor shape |
| 412 | if (set_cursor) |
| 413 | pb->setCursor(info.visible ? info.cursor : 0, server); |
| 414 | |
| 415 | // Update the cursor position |
| 416 | // NB: First translate from Screen coordinates to Desktop |
| 417 | Point desktopPos = info.position.translate(screenRect.tl.negate()); |
| 418 | server->setCursorPos(desktopPos); |
| 419 | try_update = true; |
| 420 | |
| 421 | old_cursor = info; |
| 422 | } |
| 423 | |
| 424 | // Flush any changes to the server |
| 425 | try_update = flushChangeTracker() || try_update; |
| 426 | if (try_update) { |
| 427 | server->tryUpdate(); |
| 428 | } |
| 429 | } |
| 430 | return; |
| 431 | } |
| 432 | throw rdr::Exception("No such event"); |
| 433 | } |
| 434 | |
| 435 | |
| 436 | // -=- Protected methods |
| 437 | |
| 438 | void |
| 439 | SDisplay::recreatePixelBuffer(bool force) { |
| 440 | // Open the specified display device |
| 441 | // If no device is specified, open entire screen using GetDC(). |
| 442 | // Opening the whole display with CreateDC doesn't work on multi-monitor |
| 443 | // systems for some reason. |
| 444 | DeviceContext* new_device = 0; |
| 445 | TCharArray deviceName(displayDevice.getData()); |
| 446 | if (deviceName.buf[0]) { |
| 447 | vlog.info("Attaching to device %s", (const char*)CStr(deviceName.buf)); |
| 448 | new_device = new DeviceDC(deviceName.buf); |
| 449 | } |
| 450 | if (!new_device) { |
| 451 | vlog.info("Attaching to virtual desktop"); |
| 452 | new_device = new WindowDC(0); |
| 453 | } |
| 454 | |
| 455 | // Get the coordinates of the specified dispay device |
| 456 | Rect newScreenRect; |
| 457 | if (deviceName.buf[0]) { |
| 458 | MonitorInfo info(CStr(deviceName.buf)); |
| 459 | newScreenRect = Rect(info.rcMonitor.left, info.rcMonitor.top, |
| 460 | info.rcMonitor.right, info.rcMonitor.bottom); |
| 461 | } else { |
| 462 | newScreenRect = new_device->getClipBox(); |
| 463 | } |
| 464 | |
| 465 | // If nothing has changed & a recreate has not been forced, delete |
| 466 | // the new device context and return |
| 467 | if (pb && !force && |
| 468 | newScreenRect.equals(screenRect) && |
| 469 | new_device->getPF().equal(pb->getPF())) { |
| 470 | delete new_device; |
| 471 | return; |
| 472 | } |
| 473 | |
| 474 | // Flush any existing changes to the server |
| 475 | flushChangeTracker(); |
| 476 | |
| 477 | // Delete the old pixelbuffer and device context |
| 478 | vlog.debug("deleting old pixel buffer & device"); |
| 479 | if (pb) |
| 480 | delete pb; |
| 481 | if (device) |
| 482 | delete device; |
| 483 | |
| 484 | // Create a DeviceFrameBuffer attached to the new device |
| 485 | vlog.debug("creating pixel buffer"); |
| 486 | DeviceFrameBuffer* new_buffer = new DeviceFrameBuffer(*new_device); |
| 487 | |
| 488 | // Replace the old PixelBuffer |
| 489 | screenRect = newScreenRect; |
| 490 | pb = new_buffer; |
| 491 | device = new_device; |
| 492 | |
| 493 | // Initialise the pixels |
| 494 | pb->grabRegion(pb->getRect()); |
| 495 | |
| 496 | // Prevent future grabRect operations from throwing exceptions |
| 497 | pb->setIgnoreGrabErrors(true); |
| 498 | |
| 499 | // Update the clipping update tracker |
| 500 | clipper.setClipRect(pb->getRect()); |
| 501 | |
| 502 | // Inform the core of the changes |
| 503 | if (core) |
| 504 | core->setScreenRect(screenRect); |
| 505 | |
| 506 | // Inform the server of the changes |
| 507 | if (server) |
| 508 | server->setPixelBuffer(pb); |
| 509 | } |
| 510 | |
| 511 | bool SDisplay::flushChangeTracker() { |
| 512 | if (updates.is_empty()) |
| 513 | return false; |
| 514 | |
| 515 | vlog.write(120, "flushChangeTracker"); |
| 516 | |
| 517 | // Translate the update coordinates from Screen coords to Desktop |
| 518 | updates.translate(screenRect.tl.negate()); |
| 519 | |
| 520 | // Clip the updates & flush them to the server |
| 521 | updates.copyTo(&clipper); |
| 522 | updates.clear(); |
| 523 | return true; |
| 524 | } |