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