The "rfb_win32" library merged with VNC 4.1.1 code.

git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/branches/merge-with-vnc-4.1.1@523 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/rfb_win32/CleanDesktop.cxx b/rfb_win32/CleanDesktop.cxx
index 9fb8347..39cca11 100644
--- a/rfb_win32/CleanDesktop.cxx
+++ b/rfb_win32/CleanDesktop.cxx
@@ -1,5 +1,5 @@
-/* Copyright (C) 2002-2004 RealVNC Ltd.  All Rights Reserved.
- *    
+/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
+ * 
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -18,16 +18,22 @@
 
 // -=- CleanDesktop.cxx
 
-#define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 #include <wininet.h>
 #include <shlobj.h>
-
 #include <rfb_win32/CleanDesktop.h>
 #include <rfb_win32/CurrentUser.h>
 #include <rfb_win32/Registry.h>
+#include <rfb_win32/OSVersion.h>
 #include <rfb/LogWriter.h>
 #include <rdr/Exception.h>
+#include <set>
+
+#ifdef SPI_GETUIEFFECTS
+#define RFB_HAVE_SPI_UIEFFECTS
+#else
+#pragma message("  NOTE: Not building Get/Set UI Effects support.")
+#endif
 
 using namespace rfb;
 using namespace rfb::win32;
@@ -47,37 +53,93 @@
     if (handle)
       handle->Release();
   }
+
+  // enableItem
+  //   enables or disables the Nth Active Desktop item
+  bool enableItem(int i, bool enable_) {
+    COMPONENT item;
+    memset(&item, 0, sizeof(item));
+    item.dwSize = sizeof(item);
+
+    HRESULT hr = handle->GetDesktopItem(i, &item, 0);
+    if (hr != S_OK) {
+      vlog.error("unable to GetDesktopItem %d: %ld", i, hr);
+      return false;
+    }
+    item.fChecked = enable_;
+    vlog.debug("%sbling %d: \"%s\"", enable_ ? "ena" : "disa", i, (const char*)CStr(item.wszFriendlyName));
+
+    hr = handle->ModifyDesktopItem(&item, COMP_ELEM_CHECKED);
+    return hr == S_OK;
+  }
+  
+  // enable
+  //   Attempts to enable/disable Active Desktop, returns true if the setting changed,
+  //   false otherwise.
+  //   If Active Desktop *can* be enabled/disabled then that is done.
+  //   If Active Desktop is always on (XP/2K3) then instead the individual items are
+  //   disabled, and true is returned to indicate that they need to be restored later.
   bool enable(bool enable_) {
-    // - Get the current Active Desktop options
+    bool modifyComponents = false;
+
+    vlog.debug("ActiveDesktop::enable");
+
+    // - Firstly, try to disable Active Desktop entirely
+    HRESULT hr;
     COMPONENTSOPT adOptions;
     memset(&adOptions, 0, sizeof(adOptions));
     adOptions.dwSize = sizeof(adOptions);
 
-    HRESULT result = handle->GetDesktopItemOptions(&adOptions, 0);
-    if (result != S_OK) {
-      vlog.error("failed to get Active Desktop options: %d", result);
+    // Attempt to actually disable/enable AD
+    hr = handle->GetDesktopItemOptions(&adOptions, 0);
+    if (hr == S_OK) {
+      // If Active Desktop is already in the desired state then return false (no change)
+      // NB: If AD is enabled AND restoreItems is set then we regard it as disabled...
+      if (((adOptions.fActiveDesktop==0) && restoreItems.empty()) == (enable_==false))
+        return false;
+      adOptions.fActiveDesktop = enable_;
+      hr = handle->SetDesktopItemOptions(&adOptions, 0);
+    }
+    // Apply the change, then test whether it actually took effect
+    if (hr == S_OK)
+      hr = handle->ApplyChanges(AD_APPLY_REFRESH);
+    if (hr == S_OK)
+      hr = handle->GetDesktopItemOptions(&adOptions, 0);
+    if (hr == S_OK)
+      modifyComponents = (adOptions.fActiveDesktop==0) != (enable_==false);
+    if (hr != S_OK) {
+      vlog.error("failed to get/set Active Desktop options: %ld", hr);
       return false;
     }
 
-    // - If Active Desktop is active, disable it
-    if ((adOptions.fActiveDesktop==0) != (enable_==0)) {
-      if (enable_)
-        vlog.debug("enabling Active Desktop");
-      else
-        vlog.debug("disabling Active Desktop");
-
-      adOptions.fActiveDesktop = enable_;
-      result = handle->SetDesktopItemOptions(&adOptions, 0);
-      if (result != S_OK) {
-        vlog.error("failed to disable ActiveDesktop: %d", result);
+    if (enable_) {
+      // - We are re-enabling Active Desktop.  If there are components in restoreItems
+      //   then restore them!
+      std::set<int>::const_iterator i;
+      for (i=restoreItems.begin(); i!=restoreItems.end(); i++) {
+        enableItem(*i, true);
+      }
+      restoreItems.clear();
+    } else if (modifyComponents) {
+      // - Disable all currently enabled items, and add the disabled ones to restoreItems
+      int itemCount = 0;
+      hr = handle->GetDesktopItemCount(&itemCount, 0);
+      if (hr != S_OK) {
+        vlog.error("failed to get desktop item count: %ld", hr);
         return false;
       }
-      handle->ApplyChanges(AD_APPLY_REFRESH);
-      return true;
+      for (unsigned int i=0; i<itemCount; i++) {
+        if (enableItem(i, false))
+          restoreItems.insert(i);
+      }
     }
-    return false;
+
+    // - Apply whatever changes we have made, but DON'T save them!
+    hr = handle->ApplyChanges(AD_APPLY_REFRESH);
+    return hr == S_OK;
   }
   IActiveDesktop* handle;
+  std::set<int> restoreItems;
 };
 
 
@@ -91,7 +153,8 @@
 }
 
 
-CleanDesktop::CleanDesktop() : restoreActiveDesktop(false), restoreWallpaper(false), restoreEffects(false) {
+CleanDesktop::CleanDesktop() : restoreActiveDesktop(false), restoreWallpaper(false),
+                               restorePattern(false), restoreEffects(false) {
   CoInitialize(0);
 }
 
@@ -194,13 +257,13 @@
 
 
 void CleanDesktop::disableEffects() {
-#if (WINVER >= 0x500)
   try {
     ImpersonateCurrentUser icu;
 
     vlog.debug("disable desktop effects");
 
     SysParamsInfo(SPI_SETFONTSMOOTHING, FALSE, 0, SPIF_SENDCHANGE);
+#ifdef RFB_HAVE_SPI_UIEFFECTS
     if (SysParamsInfo(SPI_GETUIEFFECTS, 0, &uiEffects, 0) == ERROR_CALL_NOT_IMPLEMENTED) {
       SysParamsInfo(SPI_GETCOMBOBOXANIMATION, 0, &comboBoxAnim, 0);
       SysParamsInfo(SPI_GETGRADIENTCAPTIONS, 0, &gradientCaptions, 0);
@@ -214,21 +277,23 @@
       SysParamsInfo(SPI_SETMENUANIMATION, 0, FALSE, SPIF_SENDCHANGE);
     } else {
       SysParamsInfo(SPI_SETUIEFFECTS, 0, FALSE, SPIF_SENDCHANGE);
+
+      // We *always* restore UI effects overall, since there is no Windows GUI to do it
+      uiEffects = TRUE;
     }
+#else
+    vlog.debug("  not supported");
+#endif
     restoreEffects = true;
 
   } catch (rdr::Exception& e) {
     vlog.info(e.str());
   }
-#else
-      vlog.info("disableffects not implemented");
-#endif
 }
 
 void CleanDesktop::enableEffects() {
   try {
     if (restoreEffects) {
-#if (WINVER >= 0x500)
       ImpersonateCurrentUser icu;
 
       vlog.debug("restore desktop effects");
@@ -236,6 +301,7 @@
       RegKey desktopCfg;
       desktopCfg.openKey(HKEY_CURRENT_USER, _T("Control Panel\\Desktop"));
       SysParamsInfo(SPI_SETFONTSMOOTHING, desktopCfg.getInt(_T("FontSmoothing"), 0) != 0, 0, SPIF_SENDCHANGE);
+#ifdef RFB_HAVE_SPI_UIEFFECTS
       if (SysParamsInfo(SPI_SETUIEFFECTS, 0, (void*)uiEffects, SPIF_SENDCHANGE) == ERROR_CALL_NOT_IMPLEMENTED) {
         SysParamsInfo(SPI_SETCOMBOBOXANIMATION, 0, (void*)comboBoxAnim, SPIF_SENDCHANGE);
         SysParamsInfo(SPI_SETGRADIENTCAPTIONS, 0, (void*)gradientCaptions, SPIF_SENDCHANGE);
@@ -245,7 +311,7 @@
       }
       restoreEffects = false;
 #else
-      vlog.info("enableEffects not implemented");
+      vlog.info("  not supported");
 #endif
     }