From 37a55b03716139bdc88b0cd444e8b0eabab6669d Mon Sep 17 00:00:00 2001 From: Greg Darke Date: Fri, 13 Mar 2009 20:31:47 +1100 Subject: [PATCH] Merged in the wallpaper setting code for Windows (Tested on Windows XP, without PIL). --- wallchanger.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/wallchanger.py b/wallchanger.py index a9c8945..7386293 100755 --- a/wallchanger.py +++ b/wallchanger.py @@ -21,6 +21,10 @@ def set_image(filename): def init(*args, **kwargs): """Desktop Changer factory""" + if sys.platform == "win32": + changers.append(WIN32Changer(*args, **kwargs)) + return + logging.debug("Testing for OSX (NonX11)") if commands.getstatusoutput("ps ax -o command -c|grep -q WindowServer")[0] == 0: changers.append(OSXChanger(*args, **kwargs)) @@ -178,6 +182,61 @@ class OSXChanger(BaseChanger): logging.debug(cmd) return not commands.getstatusoutput(cmd)[0] +class WIN32Changer(BaseChanger): + name = "Windows" + _ConvertedWallpaperLocation = os.path.join(os.environ.get('APPDATA', os.path.expanduser('~')), 'wallchanger') + + def __init__(self, *args, **kwargs): + BaseChanger.__init__(self, *args, **kwargs) + if not self.convert: + logging.warn('Running on windows, but convert is not set') + + def remove_old_image_cache(self): + """Cleans up any old temp images""" + if not os.path.isdir(self._ConvertedWallpaperLocation): + os.mkdir(self._ConvertedWallpaperLocation) + for fullpath, filenames, dirnames in os.walk(self._ConvertedWallpaperLocation, topdown=False): + for filename in filenames: + os.unlink(os.path.join(fullpath, filename)) + for dirname in dirnames: + os.unlink(os.path.join(fullpath, dirname)) + + def convert_image_format(self, file): + """Convert the image to a bmp, and store it in a local place""" + self.remove_old_image_cache() + output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.bmp' % time.time()) + import PIL, PIL.Image + img = PIL.Image.open(file) + if img.mode == 'RGBA': + img = img.convert('RGB') + img.save(output_name, 'BMP') + + return output_name, True + + def set_image(self, filename): + import ctypes + user32 = ctypes.windll.user32 + + # Taken from the Platform SDK + SPI_SETDESKWALLPAPER = 20 + SPIF_SENDWININICHANGE = 2 + + if self.convert: + filename, ret = self.convert_image_format(filename) + if not ret: + logging.debug("Convert failed") + return False + + # Parameters for SystemParametersInfoA are: + # (UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni) + user32.SystemParametersInfoA( + SPI_SETDESKWALLPAPER, + 0, + filename, + SPIF_SENDWININICHANGE, + ) + return True + class GnomeChanger(BaseChanger): name = "Gnome" def set_image(self, file): -- 2.39.2