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 | // -=- CleanDesktop.cxx |
| 20 | |
| 21 | #include <windows.h> |
| 22 | #include <wininet.h> |
| 23 | #include <shlobj.h> |
| 24 | #include <rfb_win32/CleanDesktop.h> |
| 25 | #include <rfb_win32/CurrentUser.h> |
| 26 | #include <rfb_win32/Registry.h> |
| 27 | #include <rfb_win32/OSVersion.h> |
| 28 | #include <rfb/LogWriter.h> |
| 29 | #include <rdr/Exception.h> |
| 30 | #include <set> |
| 31 | |
| 32 | #ifdef SPI_GETUIEFFECTS |
| 33 | #define RFB_HAVE_SPI_UIEFFECTS |
| 34 | #else |
| 35 | #pragma message(" NOTE: Not building Get/Set UI Effects support.") |
| 36 | #endif |
| 37 | |
| 38 | using namespace rfb; |
| 39 | using namespace rfb::win32; |
| 40 | |
| 41 | static LogWriter vlog("CleanDesktop"); |
| 42 | |
| 43 | |
| 44 | struct ActiveDesktop { |
| 45 | ActiveDesktop() : handle(0) { |
| 46 | // - Contact Active Desktop |
| 47 | HRESULT result = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, |
| 48 | IID_IActiveDesktop, (PVOID*)&handle); |
| 49 | if (result != S_OK) |
| 50 | throw rdr::SystemException("failed to contact Active Desktop", result); |
| 51 | } |
| 52 | ~ActiveDesktop() { |
| 53 | if (handle) |
| 54 | handle->Release(); |
| 55 | } |
| 56 | |
| 57 | // enableItem |
| 58 | // enables or disables the Nth Active Desktop item |
| 59 | bool enableItem(int i, bool enable_) { |
| 60 | COMPONENT item; |
| 61 | memset(&item, 0, sizeof(item)); |
| 62 | item.dwSize = sizeof(item); |
| 63 | |
| 64 | HRESULT hr = handle->GetDesktopItem(i, &item, 0); |
| 65 | if (hr != S_OK) { |
| 66 | vlog.error("unable to GetDesktopItem %d: %ld", i, hr); |
| 67 | return false; |
| 68 | } |
| 69 | item.fChecked = enable_; |
| 70 | vlog.debug("%sbling %d: \"%s\"", enable_ ? "ena" : "disa", i, (const char*)CStr(item.wszFriendlyName)); |
| 71 | |
| 72 | hr = handle->ModifyDesktopItem(&item, COMP_ELEM_CHECKED); |
| 73 | return hr == S_OK; |
| 74 | } |
| 75 | |
| 76 | // enable |
| 77 | // Attempts to enable/disable Active Desktop, returns true if the setting changed, |
| 78 | // false otherwise. |
| 79 | // If Active Desktop *can* be enabled/disabled then that is done. |
| 80 | // If Active Desktop is always on (XP/2K3) then instead the individual items are |
| 81 | // disabled, and true is returned to indicate that they need to be restored later. |
| 82 | bool enable(bool enable_) { |
| 83 | bool modifyComponents = false; |
| 84 | |
| 85 | vlog.debug("ActiveDesktop::enable"); |
| 86 | |
| 87 | // - Firstly, try to disable Active Desktop entirely |
| 88 | HRESULT hr; |
| 89 | COMPONENTSOPT adOptions; |
| 90 | memset(&adOptions, 0, sizeof(adOptions)); |
| 91 | adOptions.dwSize = sizeof(adOptions); |
| 92 | |
| 93 | // Attempt to actually disable/enable AD |
| 94 | hr = handle->GetDesktopItemOptions(&adOptions, 0); |
| 95 | if (hr == S_OK) { |
| 96 | // If Active Desktop is already in the desired state then return false (no change) |
| 97 | // NB: If AD is enabled AND restoreItems is set then we regard it as disabled... |
| 98 | if (((adOptions.fActiveDesktop==0) && restoreItems.empty()) == (enable_==false)) |
| 99 | return false; |
| 100 | adOptions.fActiveDesktop = enable_; |
| 101 | hr = handle->SetDesktopItemOptions(&adOptions, 0); |
| 102 | } |
| 103 | // Apply the change, then test whether it actually took effect |
| 104 | if (hr == S_OK) |
| 105 | hr = handle->ApplyChanges(AD_APPLY_REFRESH); |
| 106 | if (hr == S_OK) |
| 107 | hr = handle->GetDesktopItemOptions(&adOptions, 0); |
| 108 | if (hr == S_OK) |
| 109 | modifyComponents = (adOptions.fActiveDesktop==0) != (enable_==false); |
| 110 | if (hr != S_OK) { |
| 111 | vlog.error("failed to get/set Active Desktop options: %ld", hr); |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | if (enable_) { |
| 116 | // - We are re-enabling Active Desktop. If there are components in restoreItems |
| 117 | // then restore them! |
| 118 | std::set<int>::const_iterator i; |
| 119 | for (i=restoreItems.begin(); i!=restoreItems.end(); i++) { |
| 120 | enableItem(*i, true); |
| 121 | } |
| 122 | restoreItems.clear(); |
| 123 | } else if (modifyComponents) { |
| 124 | // - Disable all currently enabled items, and add the disabled ones to restoreItems |
| 125 | int itemCount = 0; |
| 126 | hr = handle->GetDesktopItemCount(&itemCount, 0); |
| 127 | if (hr != S_OK) { |
| 128 | vlog.error("failed to get desktop item count: %ld", hr); |
| 129 | return false; |
| 130 | } |
| 131 | for (unsigned int i=0; i<itemCount; i++) { |
| 132 | if (enableItem(i, false)) |
| 133 | restoreItems.insert(i); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // - Apply whatever changes we have made, but DON'T save them! |
| 138 | hr = handle->ApplyChanges(AD_APPLY_REFRESH); |
| 139 | return hr == S_OK; |
| 140 | } |
| 141 | IActiveDesktop* handle; |
| 142 | std::set<int> restoreItems; |
| 143 | }; |
| 144 | |
| 145 | |
| 146 | DWORD SysParamsInfo(UINT action, UINT param, PVOID ptr, UINT ini) { |
| 147 | DWORD r = ERROR_SUCCESS; |
| 148 | if (!SystemParametersInfo(action, param, ptr, ini)) { |
| 149 | r = GetLastError(); |
| 150 | vlog.info("SPI error: %d", r); |
| 151 | } |
| 152 | return r; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | CleanDesktop::CleanDesktop() : restoreActiveDesktop(false), restoreWallpaper(false), |
| 157 | restorePattern(false), restoreEffects(false) { |
| 158 | CoInitialize(0); |
| 159 | } |
| 160 | |
| 161 | CleanDesktop::~CleanDesktop() { |
| 162 | enableEffects(); |
| 163 | enablePattern(); |
| 164 | enableWallpaper(); |
| 165 | CoUninitialize(); |
| 166 | } |
| 167 | |
| 168 | void CleanDesktop::disableWallpaper() { |
| 169 | try { |
| 170 | ImpersonateCurrentUser icu; |
| 171 | |
| 172 | vlog.debug("disable desktop wallpaper/Active Desktop"); |
| 173 | |
| 174 | // -=- First attempt to remove the wallpaper using Active Desktop |
| 175 | try { |
| 176 | ActiveDesktop ad; |
| 177 | if (ad.enable(false)) |
| 178 | restoreActiveDesktop = true; |
| 179 | } catch (rdr::Exception& e) { |
| 180 | vlog.error(e.str()); |
| 181 | } |
| 182 | |
| 183 | // -=- Switch of normal wallpaper and notify apps |
Adam Tkac | 934f63c | 2009-10-12 15:54:59 +0000 | [diff] [blame^] | 184 | SysParamsInfo(SPI_SETDESKWALLPAPER, 0, (PVOID) "", SPIF_SENDCHANGE); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 185 | restoreWallpaper = true; |
| 186 | |
| 187 | } catch (rdr::Exception& e) { |
| 188 | vlog.info(e.str()); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | void CleanDesktop::enableWallpaper() { |
| 193 | try { |
| 194 | ImpersonateCurrentUser icu; |
| 195 | |
| 196 | if (restoreActiveDesktop) { |
| 197 | vlog.debug("restore Active Desktop"); |
| 198 | |
| 199 | // -=- First attempt to re-enable Active Desktop |
| 200 | try { |
| 201 | ActiveDesktop ad; |
| 202 | ad.enable(true); |
| 203 | restoreActiveDesktop = false; |
| 204 | } catch (rdr::Exception& e) { |
| 205 | vlog.error(e.str()); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if (restoreWallpaper) { |
| 210 | vlog.debug("restore desktop wallpaper"); |
| 211 | |
| 212 | // -=- Then restore the standard wallpaper if required |
| 213 | SysParamsInfo(SPI_SETDESKWALLPAPER, 0, NULL, SPIF_SENDCHANGE); |
| 214 | restoreWallpaper = false; |
| 215 | } |
| 216 | |
| 217 | } catch (rdr::Exception& e) { |
| 218 | vlog.info(e.str()); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | void CleanDesktop::disablePattern() { |
| 224 | try { |
| 225 | ImpersonateCurrentUser icu; |
| 226 | |
| 227 | vlog.debug("disable desktop pattern"); |
Adam Tkac | 934f63c | 2009-10-12 15:54:59 +0000 | [diff] [blame^] | 228 | SysParamsInfo(SPI_SETDESKPATTERN, 0, (PVOID) "", SPIF_SENDCHANGE); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 229 | restorePattern = true; |
| 230 | |
| 231 | } catch (rdr::Exception& e) { |
| 232 | vlog.info(e.str()); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | void CleanDesktop::enablePattern() { |
| 237 | try { |
| 238 | if (restorePattern) { |
| 239 | ImpersonateCurrentUser icu; |
| 240 | |
| 241 | vlog.debug("restoring pattern..."); |
| 242 | |
| 243 | TCharArray pattern; |
| 244 | if (osVersion.isPlatformWindows) { |
| 245 | RegKey cfgKey; |
| 246 | cfgKey.openKey(HKEY_CURRENT_USER, _T("Control Panel\\Desktop")); |
| 247 | pattern.buf = cfgKey.getString(_T("Pattern")); |
| 248 | } |
| 249 | SysParamsInfo(SPI_SETDESKPATTERN, 0, pattern.buf, SPIF_SENDCHANGE); |
| 250 | restorePattern = false; |
| 251 | } |
| 252 | |
| 253 | } catch (rdr::Exception& e) { |
| 254 | vlog.info(e.str()); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | void CleanDesktop::disableEffects() { |
| 260 | try { |
| 261 | ImpersonateCurrentUser icu; |
| 262 | |
| 263 | vlog.debug("disable desktop effects"); |
| 264 | |
| 265 | SysParamsInfo(SPI_SETFONTSMOOTHING, FALSE, 0, SPIF_SENDCHANGE); |
| 266 | #ifdef RFB_HAVE_SPI_UIEFFECTS |
| 267 | if (SysParamsInfo(SPI_GETUIEFFECTS, 0, &uiEffects, 0) == ERROR_CALL_NOT_IMPLEMENTED) { |
| 268 | SysParamsInfo(SPI_GETCOMBOBOXANIMATION, 0, &comboBoxAnim, 0); |
| 269 | SysParamsInfo(SPI_GETGRADIENTCAPTIONS, 0, &gradientCaptions, 0); |
| 270 | SysParamsInfo(SPI_GETHOTTRACKING, 0, &hotTracking, 0); |
| 271 | SysParamsInfo(SPI_GETLISTBOXSMOOTHSCROLLING, 0, &listBoxSmoothScroll, 0); |
| 272 | SysParamsInfo(SPI_GETMENUANIMATION, 0, &menuAnim, 0); |
| 273 | SysParamsInfo(SPI_SETCOMBOBOXANIMATION, 0, FALSE, SPIF_SENDCHANGE); |
| 274 | SysParamsInfo(SPI_SETGRADIENTCAPTIONS, 0, FALSE, SPIF_SENDCHANGE); |
| 275 | SysParamsInfo(SPI_SETHOTTRACKING, 0, FALSE, SPIF_SENDCHANGE); |
| 276 | SysParamsInfo(SPI_SETLISTBOXSMOOTHSCROLLING, 0, FALSE, SPIF_SENDCHANGE); |
| 277 | SysParamsInfo(SPI_SETMENUANIMATION, 0, FALSE, SPIF_SENDCHANGE); |
| 278 | } else { |
| 279 | SysParamsInfo(SPI_SETUIEFFECTS, 0, FALSE, SPIF_SENDCHANGE); |
| 280 | |
| 281 | // We *always* restore UI effects overall, since there is no Windows GUI to do it |
| 282 | uiEffects = TRUE; |
| 283 | } |
| 284 | #else |
| 285 | vlog.debug(" not supported"); |
| 286 | #endif |
| 287 | restoreEffects = true; |
| 288 | |
| 289 | } catch (rdr::Exception& e) { |
| 290 | vlog.info(e.str()); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | void CleanDesktop::enableEffects() { |
| 295 | try { |
| 296 | if (restoreEffects) { |
| 297 | ImpersonateCurrentUser icu; |
| 298 | |
| 299 | vlog.debug("restore desktop effects"); |
| 300 | |
| 301 | RegKey desktopCfg; |
| 302 | desktopCfg.openKey(HKEY_CURRENT_USER, _T("Control Panel\\Desktop")); |
| 303 | SysParamsInfo(SPI_SETFONTSMOOTHING, desktopCfg.getInt(_T("FontSmoothing"), 0) != 0, 0, SPIF_SENDCHANGE); |
| 304 | #ifdef RFB_HAVE_SPI_UIEFFECTS |
| 305 | if (SysParamsInfo(SPI_SETUIEFFECTS, 0, (void*)uiEffects, SPIF_SENDCHANGE) == ERROR_CALL_NOT_IMPLEMENTED) { |
| 306 | SysParamsInfo(SPI_SETCOMBOBOXANIMATION, 0, (void*)comboBoxAnim, SPIF_SENDCHANGE); |
| 307 | SysParamsInfo(SPI_SETGRADIENTCAPTIONS, 0, (void*)gradientCaptions, SPIF_SENDCHANGE); |
| 308 | SysParamsInfo(SPI_SETHOTTRACKING, 0, (void*)hotTracking, SPIF_SENDCHANGE); |
| 309 | SysParamsInfo(SPI_SETLISTBOXSMOOTHSCROLLING, 0, (void*)listBoxSmoothScroll, SPIF_SENDCHANGE); |
| 310 | SysParamsInfo(SPI_SETMENUANIMATION, 0, (void*)menuAnim, SPIF_SENDCHANGE); |
| 311 | } |
| 312 | restoreEffects = false; |
| 313 | #else |
| 314 | vlog.info(" not supported"); |
| 315 | #endif |
| 316 | } |
| 317 | |
| 318 | } catch (rdr::Exception& e) { |
| 319 | vlog.info(e.str()); |
| 320 | } |
| 321 | } |