X-Git-Url: https://code.delx.au/bg-scripts/blobdiff_plain/dc57c2553ce89d1bdd8b97616299b93bd656db33..94a833d3bd9534b5e8b1487c78ddf7ba8bc0d3a9:/wallchanger.py diff --git a/wallchanger.py b/wallchanger.py index a9c8945..74be97b 100755 --- a/wallchanger.py +++ b/wallchanger.py @@ -6,6 +6,10 @@ import commands, sys, os, os.path, time import logging +try: + import PIL, PIL.Image +except ImportError: + PIL = None __all__ = ("init", "set_image") @@ -21,6 +25,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)) @@ -78,6 +86,27 @@ class BaseChanger(object): def set_image(self, filename): raise NotImplementedError() + def convert_image_format(self, filename, format='BMP', allowAlpha=False, extension='.bmp'): + """Convert the image to another format, and store it in a local place""" + if not os.path.exists(filename): + logger.warn('The input file "%s" does not exist, so it will not be converted', filename) + return filename, False + if PIL is None: + logger.warn('PIL could not be found, not converting image format') + return filename, False + + self.remove_old_image_cache() + output_name = os.path.join(self._ConvertedWallpaperLocation, '%s%s' % (time.time(), extension)) + img = PIL.Image.open(filename) + + # Remove the alpha channel if the user doens't want it + if not allowAlpha and img.mode == 'RGBA': + img = img.convert('RGB') + img.save(output_name, format) + + return output_name, True + + class WMakerChanger(BaseChanger): name = "WindowMaker" _ConvertedWallpaperLocation = '/tmp/wallpapers_wmaker/' @@ -141,10 +170,7 @@ class OSXChanger(BaseChanger): self.remove_old_image_cache() output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time()) try: - import PIL, PIL.Image - img = PIL.Image.open(file) - img.save(output_name, "PNG") - return output_name, True + return super(OSXChanger, self).convert_image_format(file, format='PNG', extension='.png') except ImportError: logging.debug('Could not load PIL, going to try just copying the image') import shutil @@ -178,6 +204,49 @@ 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 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):