Updated to work with XFCE 4.12, will not work with XFCE 4.10 anymore master
authorJames Bunton <jamesbunton@delx.net.au>
Sun, 19 Jul 2015 10:45:07 +0000 (20:45 +1000)
committerJames Bunton <jamesbunton@delx.net.au>
Sun, 19 Jul 2015 10:45:07 +0000 (20:45 +1000)
wallchanger.py

index 30f1400..40cac72 100755 (executable)
@@ -23,7 +23,14 @@ def set_image(filename):
         if not changer.set_image(filename):
             logging.warning("Failed to set background: wallchanger.set_image(%s), changer=%s", filename, changer)
 
-def check_cmd(cmd):
+def get_displays():
+    for line in subprocess.check_output("xrandr").decode("utf-8").split("\n"):
+        tokens = line.split()
+        if len(tokens) < 2 or tokens[1] != "connected":
+            continue
+        yield tokens[0]
+
+def call_shell(cmd):
     return subprocess.getstatusoutput(cmd)[0] == 0
 
 def init(*args, **kwargs):
@@ -36,7 +43,7 @@ def init(*args, **kwargs):
         return
 
     logging.debug("Testing for OSX (NonX11)")
-    if check_cmd("ps ax -o command -c|grep -q WindowServer"):
+    if call_shell("ps ax -o command -c|grep -q WindowServer"):
         classes.append(OSXChanger)
 
     if 'DISPLAY' not in os.environ or os.environ['DISPLAY'].startswith('/tmp/launch'):
@@ -45,23 +52,23 @@ def init(*args, **kwargs):
     else:
         if os.uname()[0] == 'Darwin':
             # Try to detect if the X11 server is running on OSX
-            if check_cmd("ps ax -o command|grep -q '^/.*X11 .* %s'" % os.environ['DISPLAY']):
+            if call_shell("ps ax -o command|grep -q '^/.*X11 .* %s'" % os.environ['DISPLAY']):
                 # X11 is not running for this display
                 return
 
     logging.debug("Testing for XFCE4")
-    if check_cmd("xwininfo -name 'xfce4-session'"):
+    if call_shell("xwininfo -name 'xfdesktop'"):
         classes.append(Xfce4Changer)
 
     logging.debug("Testing for Gnome")
-    if check_cmd("xwininfo -name 'gnome-settings-daemon'"):
-        if check_cmd("gsettings get org.gnome.desktop.background picture-uri"):
+    if call_shell("xwininfo -name 'gnome-settings-daemon'"):
+        if call_shell("gsettings get org.gnome.desktop.background picture-uri"):
             classes.append(Gnome3Changer)
         else:
             classes.append(Gnome2Changer)
 
     logging.debug("Testing for xloadimage")
-    if check_cmd("which xloadimage"):
+    if call_shell("which xloadimage"):
         classes.append(XLoadImageChanger)
 
     if len(classes) == 0:
@@ -78,17 +85,6 @@ class BaseChanger(object):
         self.background_color = background_color
         self.convert = convert
 
-    try:
-        def _exec_cmd(self, cmd):
-            import subprocess
-            return subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait()
-
-    except ImportError:
-        # A simple implementation of subprocess for python2.4
-        def _exec_cmd(self, cmd):
-            """Runs a program given in cmd"""
-            return os.spawnvp(os.P_WAIT, cmd[0], cmd)
-
     def set_image(self, filename):
         raise NotImplementedError()
 
@@ -132,7 +128,7 @@ class XLoadImageChanger(BaseChanger):
         output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time())
         cmd = ["convert", filename, output_name]
         logging.debug("""Convert command: '"%s"'""", '" "'.join(cmd))
-        return output_name, self._exec_cmd(cmd)
+        return output_name, subprocess.call(cmd) == 0
 
     def set_image(self, filename):
         if self.convert:
@@ -147,7 +143,7 @@ class XLoadImageChanger(BaseChanger):
             filename,
         ]
         logging.debug('''WMaker bgset command: "'%s'"''', "' '".join(cmd))
-        return not self._exec_cmd(cmd)
+        return subprocess.call(cmd) == 0
 
 class OSXChanger(BaseChanger):
     name = "Mac OS X"
@@ -254,26 +250,29 @@ class Gnome2Changer(BaseChanger):
     def set_image(self, filename):
         cmd = ['gconftool-2', '--type', 'string', '--set', '/desktop/gnome/background/picture_filename', filename]
         logging.debug(cmd)
-        return not self._exec_cmd(cmd)
+        return subprocess.call(cmd) == 0
 
 class Gnome3Changer(BaseChanger):
     name = "Gnome3"
     def set_image(self, filename):
         cmd = ['gsettings', 'set', 'org.gnome.desktop.background', 'picture-uri', 'file://'+filename]
         logging.debug(cmd)
-        return not self._exec_cmd(cmd)
+        return subprocess.call(cmd) == 0
 
 class Xfce4Changer(BaseChanger):
     name = "XFCE4"
     def set_image(self, filename):
-        cmd = [
-            "xfconf-query",
-            "-c", "xfce4-desktop",
-            "-p", "/backdrop/screen0/monitor0/image-path",
-            "-s", filename,
-        ]
-        logging.debug(cmd)
-        return not self._exec_cmd(cmd)
+        for display in get_displays():
+            cmd = [
+                "xfconf-query",
+                "-c", "xfce4-desktop",
+                "-p", "/backdrop/screen0/monitor%s/workspace0/last-image" % display,
+                "-s", filename,
+            ]
+            logging.debug(cmd)
+            if subprocess.call(cmd) != 0:
+                return False
+        return True
 
 def main(filename):
     logging.basicConfig(level=logging.DEBUG, format="%(levelname)s: %(message)s")