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