summaryrefslogtreecommitdiffstats
path: root/rfb_win32/CleanDesktop.cxx
blob: 9fb8347816699817c8670cdc483bb6417a259ea4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* Copyright (C) 2002-2004 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
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

// -=- 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/LogWriter.h>
#include <rdr/Exception.h>

using namespace rfb;
using namespace rfb::win32;

static LogWriter vlog("CleanDesktop");


struct ActiveDesktop {
  ActiveDesktop() : handle(0) {
    // - Contact Active Desktop
    HRESULT result = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,
                                      IID_IActiveDesktop, (PVOID*)&handle);
    if (result != S_OK)
      throw rdr::SystemException("failed to contact Active Desktop", result);
  }
  ~ActiveDesktop() {
    if (handle)
      handle->Release();
  }
  bool enable(bool enable_) {
    // - Get the current Active Desktop options
    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);
      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);
        return false;
      }
      handle->ApplyChanges(AD_APPLY_REFRESH);
      return true;
    }
    return false;
  }
  IActiveDesktop* handle;
};


DWORD SysParamsInfo(UINT action, UINT param, PVOID ptr, UINT ini) {
  DWORD r = ERROR_SUCCESS;
  if (!SystemParametersInfo(action, param, ptr, ini)) {
    r = GetLastError();
    vlog.info("SPI error: %d", r);
  }
  return r;
}


CleanDesktop::CleanDesktop() : restoreActiveDesktop(false), restoreWallpaper(false), restoreEffects(false) {
  CoInitialize(0);
}

CleanDesktop::~CleanDesktop() {
  enableEffects();
  enablePattern();
  enableWallpaper();
  CoUninitialize();
}

void CleanDesktop::disableWallpaper() {
  try {
    ImpersonateCurrentUser icu;

    vlog.debug("disable desktop wallpaper/Active Desktop");

    // -=- First attempt to remove the wallpaper using Active Desktop
    try {
      ActiveDesktop ad;
      if (ad.enable(false))
        restoreActiveDesktop = true;
    } catch (rdr::Exception& e) {
      vlog.error(e.str());
    }

    // -=- Switch of normal wallpaper and notify apps
    SysParamsInfo(SPI_SETDESKWALLPAPER, 0, "", SPIF_SENDCHANGE);
    restoreWallpaper = true;

  } catch (rdr::Exception& e) {
    vlog.info(e.str());
  }
}

void CleanDesktop::enableWallpaper() {
  try {
    ImpersonateCurrentUser icu;

    if (restoreActiveDesktop) {
      vlog.debug("restore Active Desktop");

      // -=- First attempt to re-enable Active Desktop
      try {
        ActiveDesktop ad;
        ad.enable(true);
        restoreActiveDesktop = false;
      } catch (rdr::Exception& e) {
        vlog.error(e.str());
      }
    }

    if (restoreWallpaper) {
      vlog.debug("restore desktop wallpaper");

      // -=- Then restore the standard wallpaper if required
	    SysParamsInfo(SPI_SETDESKWALLPAPER, 0, NULL, SPIF_SENDCHANGE);
      restoreWallpaper = false;
    }

  } catch (rdr::Exception& e) {
    vlog.info(e.str());
  }
}


void CleanDesktop::disablePattern() {
  try {
    ImpersonateCurrentUser icu;

    vlog.debug("disable desktop pattern");
    SysParamsInfo(SPI_SETDESKPATTERN, 0, "", SPIF_SENDCHANGE);
    restorePattern = true;

  } catch (rdr::Exception& e) {
    vlog.info(e.str());
  }
}

void CleanDesktop::enablePattern() {
  try {
    if (restorePattern) {
      ImpersonateCurrentUser icu;

      vlog.debug("restoring pattern...");

      TCharArray pattern;
      if (osVersion.isPlatformWindows) {
        RegKey cfgKey;
        cfgKey.openKey(HKEY_CURRENT_USER, _T("Control Panel\\Desktop"));
        pattern.buf = cfgKey.getString(_T("Pattern"));
      }
      SysParamsInfo(SPI_SETDESKPATTERN, 0, pattern.buf, SPIF_SENDCHANGE);
      restorePattern = false;
    }

  } catch (rdr::Exception& e) {
    vlog.info(e.str());
  }
}


void CleanDesktop::disableEffects() {
#if (WINVER >= 0x500)
  try {
    ImpersonateCurrentUser icu;

    vlog.debug("disable desktop effects");

    SysParamsInfo(SPI_SETFONTSMOOTHING, FALSE, 0, SPIF_SENDCHANGE);
    if (SysParamsInfo(SPI_GETUIEFFECTS, 0, &uiEffects, 0) == ERROR_CALL_NOT_IMPLEMENTED) {
      SysParamsInfo(SPI_GETCOMBOBOXANIMATION, 0, &comboBoxAnim, 0);
      SysParamsInfo(SPI_GETGRADIENTCAPTIONS, 0, &gradientCaptions, 0);
      SysParamsInfo(SPI_GETHOTTRACKING, 0, &hotTracking, 0);
      SysParamsInfo(SPI_GETLISTBOXSMOOTHSCROLLING, 0, &listBoxSmoothScroll, 0);
      SysParamsInfo(SPI_GETMENUANIMATION, 0, &menuAnim, 0);
      SysParamsInfo(SPI_SETCOMBOBOXANIMATION, 0, FALSE, SPIF_SENDCHANGE);
      SysParamsInfo(SPI_SETGRADIENTCAPTIONS, 0, FALSE, SPIF_SENDCHANGE);
      SysParamsInfo(SPI_SETHOTTRACKING, 0, FALSE, SPIF_SENDCHANGE);
      SysParamsInfo(SPI_SETLISTBOXSMOOTHSCROLLING, 0, FALSE, SPIF_SENDCHANGE);
      SysParamsInfo(SPI_SETMENUANIMATION, 0, FALSE, SPIF_SENDCHANGE);
    } else {
      SysParamsInfo(SPI_SETUIEFFECTS, 0, FALSE, SPIF_SENDCHANGE);
    }
    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");

      RegKey desktopCfg;
      desktopCfg.openKey(HKEY_CURRENT_USER, _T("Control Panel\\Desktop"));
      SysParamsInfo(SPI_SETFONTSMOOTHING, desktopCfg.getInt(_T("FontSmoothing"), 0) != 0, 0, SPIF_SENDCHANGE);
      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);
        SysParamsInfo(SPI_SETHOTTRACKING, 0, (void*)hotTracking, SPIF_SENDCHANGE);
        SysParamsInfo(SPI_SETLISTBOXSMOOTHSCROLLING, 0, (void*)listBoxSmoothScroll, SPIF_SENDCHANGE);
        SysParamsInfo(SPI_SETMENUANIMATION, 0, (void*)menuAnim, SPIF_SENDCHANGE);
      }
      restoreEffects = false;
#else
      vlog.info("enableEffects not implemented");
#endif
    }

  } catch (rdr::Exception& e) {
    vlog.info(e.str());
  }
}