X-Git-Url: https://code.delx.au/bg-scripts/blobdiff_plain/a8d026613301bb315f8f66f50b1b35d8db996744..9bbea708782fc721666590c03c36f574cbb3e756:/wallchanger.py diff --git a/wallchanger.py b/wallchanger.py index 5982621..4ee3b08 100755 --- a/wallchanger.py +++ b/wallchanger.py @@ -4,8 +4,12 @@ # Licensed for distribution under the GPL version 2, check COPYING for details # This is a cross platform/cross window manager way to change your wallpaper -import commands, sys, os, os.path, subprocess, time +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)) @@ -31,7 +39,7 @@ def init(*args, **kwargs): else: if os.uname()[0] == 'Darwin': # Try to detect if the X11 server is running on OSX - if commands.getstatusoutput("ps ax -o command|grep -q '/.*X11 .* %s'" % os.environ['DISPLAY'])[0] != 0: + if commands.getstatusoutput("ps ax -o command|grep -q '^/.*X11 .* %s'" % os.environ['DISPLAY'])[0] != 0: # X11 is not running for this display return @@ -39,8 +47,12 @@ def init(*args, **kwargs): if commands.getstatusoutput("xwininfo -name 'KDE Desktop'")[0] == 0: changers.append(KDEChanger(*args, **kwargs)) + logging.debug("Testing for Unity") + if commands.getstatusoutput("xlsclients | grep -qi unity")[0] == 0: + changers.append(UnityChanger(*args, **kwargs)) + logging.debug("Testing for Gnome") - if commands.getstatusoutput("xwininfo -name 'gnome-session'")[0] == 0: + if commands.getstatusoutput("xwininfo -name 'gnome-settings-daemon'")[0] == 0: changers.append(GnomeChanger(*args, **kwargs)) logging.debug("Testing for WMaker") @@ -59,9 +71,46 @@ class BaseChanger(object): self.permanent = permanent self.convert = convert + try: + import subprocess + except ImportError: + self._runProgram = self._runProgram_command + else: + self._runProgram = self._runProgram_subprocess + + def _runProgram_subprocess(self, cmd): + import subprocess + return subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait() + + # A simple implementation of subprocess for python2.4 + def _runProgram_command(self, cmd): + """Runs a program given in cmd""" + return os.spawnvp(os.P_WAIT, cmd[0], cmd) + 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/' @@ -81,7 +130,7 @@ class WMakerChanger(BaseChanger): output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time()) cmd = ["convert", '-resize', '1280', '-gravity', 'Center', '-crop', '1280x800+0+0', file, output_name] logging.debug("""Convert command: '"%s"'""", '" "'.join(cmd)) - return output_name, subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait() + return output_name, self._runProgram(cmd) def set_image(self, file): if self.convert: @@ -100,7 +149,7 @@ class WMakerChanger(BaseChanger): cmd += ["-u"] # update the wmaker database cmd += [file] logging.debug('''WMaker bgset command: "'%s'"''', "' '".join(cmd)) - return not subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait() + return not self._runProgram(cmd) class OSXChanger(BaseChanger): name = "Mac OS X" @@ -125,10 +174,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 @@ -162,12 +208,62 @@ 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): cmd = ['gconftool-2', '--type', 'string', '--set', '/desktop/gnome/background/picture_filename', file] logging.debug(cmd) - return not subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait() + return not self._runProgram(cmd) + +class UnityChanger(BaseChanger): + name = "Unity" + def set_image(self, file): + cmd = ['gsettings', 'set', 'org.gnome.desktop.background', 'picture-uri', 'file://'+file] + logging.debug(cmd) + return not self._runProgram(cmd) class KDEChanger(BaseChanger): name = "KDE" @@ -183,7 +279,7 @@ class KDEChanger(BaseChanger): cmds.append(['dcop', 'kdesktop', 'KBackgroundIface', 'configure']) for cmd in cmds: logging.debug(cmd) - if subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait() != 0: + if self._runProgram(cmd) != 0: return False return True