]> code.delx.au - bg-scripts/blob - lib/WallChanger.py
Fixed a bug with dual monitors under OSX where WallChanger could not update a monitor...
[bg-scripts] / lib / WallChanger.py
1 #! python
2
3 import commands, sys, os, os.path, subprocess, time
4 from GregDebug import debug, setDebugLevel, DEBUG_LEVEL_DEBUG, DEBUG_LEVEL_LOW, DEBUG_LEVEL_MEDIUM, DEBUG_LEVEL_HIGH, DEBUG_INCREMENT
5
6 import python24_adapter # NB: Must be imported before collections
7 import collections
8
9 """This is a cross platform/cross window manager way to change your current
10 desktop image."""
11
12 __all__ = ('RandomBG')
13
14 KDE_CONFIG = os.path.expanduser('~/.kde/share/config/kdesktoprc')
15
16 def RandomBG(*args, **kwargs):
17 """Desktop Changer factory"""
18
19 ret = None
20
21 debug("Testing for OSX (NonX11)", DEBUG_LEVEL_LOW)
22 if commands.getstatusoutput("ps ax -o command -c|grep -q WindowServer")[0] == 0:
23 ret = __OSXChanger(*args, **kwargs)
24
25 if 'DISPLAY' not in os.environ or os.environ['DISPLAY'].startswith('/tmp/launch'):
26 # X11 is not running
27 return ret
28 else:
29 if os.uname()[0] == 'Darwin':
30 # Try to detect if the X11 server is running on OSX
31 if commands.getstatusoutput("ps ax -o command|grep -q '/.*X11 .* %s'" % os.environ['DISPLAY'])[0] != 0:
32 # X11 is not running for this display
33 return ret
34
35 debug("Testing for KDE", DEBUG_LEVEL_LOW)
36 if commands.getstatusoutput("xwininfo -name 'KDE Desktop'")[0] == 0:
37 if ret is not None:
38 ret.nextChanger = __KDEChanger(*args, **kwargs)
39 else:
40 ret = __KDEChanger(*args, **kwargs)
41
42 debug("Testing for WMaker", DEBUG_LEVEL_LOW)
43 if commands.getstatusoutput("xlsclients | grep -qi wmaker")[0] == 0:
44 if ret is not None:
45 ret.nextChanger = __WMakerChanger(*args, **kwargs)
46 else:
47 ret = __WMakerChanger(*args, **kwargs)
48
49 if ret is None:
50 raise Exception("Unknown window manager")
51 else:
52 return ret
53
54 class __BaseChanger(object):
55 def __init__(self, filelist, backgroundColour='black', permanent=False):
56 debug('Determined the window manager is "%s"' % self.__class__.__name__, DEBUG_LEVEL_MEDIUM)
57 self.backgroundColour = backgroundColour
58 self.permanent = permanent
59 self.filelist = filelist
60 # Used to 'chain' background changers
61 self.nextChanger = None
62
63 def callChained(self, filename):
64 if self.nextChanger is None:
65 return True
66 else:
67 return self.nextChanger.changeTo(filename)
68
69 def cycleNext(self):
70 file = self.filelist.getNextRandomImage()
71 return self.changeTo(file) and self.callChained(file)
72
73 def cyclePrev(self):
74 file = self.filelist.getPrevRandomImage()
75 return self.changeTo(file) and self.callChained(file)
76
77 def cycleReload(self):
78 file = self.filelist.getCurrentImage()
79 return self.changeTo(file) and self.callChained(file)
80
81
82 class __WMakerChanger(__BaseChanger):
83 _ConvertedWallpaperLocation = '/tmp/wallpapers_wmaker/'
84 def _removeOldImageCache(self):
85 """Cleans up any old temp images"""
86 if not os.path.isdir(self._ConvertedWallpaperLocation):
87 os.mkdir(self._ConvertedWallpaperLocation)
88 for fullpath, filenames, dirnames in os.walk(self._ConvertedWallpaperLocation, topdown=False):
89 for filename in filenames:
90 os.unlink(os.path.join(fullpath, filename))
91 for dirname in dirnames:
92 os.unlink(os.path.join(fullpath, dirname))
93
94 def _convertImageFormat(self, file):
95 """Convert the image to a png, and store it in a local place"""
96 self._removeOldImageCache()
97 output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time())
98 cmd = ["convert", '-resize', '1280', '-gravity', 'Center', '-crop', '1280x800+0+0', file, output_name]
99 debug("""Convert command: '"%s"'""" % '" "'.join(cmd), DEBUG_LEVEL_DEBUG)
100 return output_name, subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait()
101 def changeTo(self, file):
102 file, convert_status = self._convertImageFormat(file)
103 if convert_status:
104 debug('Convert failed')
105 cmd = ["wmsetbg",
106 "-b", self.backgroundColour, # Sets the background colour to be what the user specified
107 "-S", # 'Smooth' (WTF?)
108 "-e", # Center the image on the screen (only affects when the image in no the in the correct aspect ratio
109 ### "-a", # scale the image, keeping the aspect ratio
110 "-u", # Force this to be the default background
111 "-d" # dither
112 ]
113 if self.permanent:
114 cmd += ["-u"] # update the wmaker database
115 cmd += [file]
116 debug('''WMaker bgset command: "'%s'"''' % "' '".join(cmd), DEBUG_LEVEL_DEBUG)
117 return not subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait()
118
119 class __OSXChanger(__BaseChanger):
120 _ConvertedWallpaperLocation = '/tmp/wallpapers/'
121 _DesktopPlistLocation = os.path.expanduser('~/Library/Preferences/com.apple.desktop.plist')
122
123 def _removeOldImageCache(self):
124 """Cleans up any old temp images"""
125 if not os.path.isdir(self._ConvertedWallpaperLocation):
126 os.mkdir(self._ConvertedWallpaperLocation)
127 for fullpath, filenames, dirnames in os.walk(self._ConvertedWallpaperLocation, topdown=False):
128 for filename in filenames:
129 os.unlink(os.path.join(fullpath, filename))
130 for dirname in dirnames:
131 os.unlink(os.path.join(fullpath, dirname))
132
133 def _convertImageFormat(self, file):
134 """Convert the image to a png, and store it in a local place"""
135 self._removeOldImageCache()
136 output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time())
137 cmd = ["convert", file, output_name]
138 debug("""Convert command: '"%s"'""" % '" "'.join(cmd), DEBUG_LEVEL_DEBUG)
139 return output_name, subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait()
140
141 def _fixDesktopPList(self):
142 """Removes the entry in the desktop plist file that specifies the wallpaper for each monitor"""
143 try:
144 import Foundation
145 desktopPList = Foundation.NSMutableDictionary.dictionaryWithContentsOfFile_(self._DesktopPlistLocation)
146 # Remove all but the 'default' entry
147 for k in desktopPList['Background'].keys():
148 if k == 'default':
149 continue
150 desktopPList['Background'].removeObjectForKey_(k)
151 # Store the plist again (Make sure we write it out atomically -- Don't want to break finder)
152 desktopPList.writeToFile_atomically_(self._DesktopPlistLocation, True)
153 except ImportError:
154 debug('Could not import the Foundation module, you may have problems with dual screens', DEBUG_LEVEL_MEDIUM)
155
156 def changeTo(self, file):
157 output_name, ret = self._convertImageFormat(file)
158 if ret: # Since 0 indicates success
159 debug("Convert failed %s" % ret)
160 return False
161 self._fixDesktopPList()
162 cmd = """osascript -e 'tell application "finder" to set desktop picture to posix file "%s"'""" % output_name
163 debug(cmd, DEBUG_LEVEL_DEBUG)
164 return not commands.getstatusoutput(cmd)[0]
165
166 class __KDEChanger(__BaseChanger):
167 def _parseKDEConfig(self, filename = KDE_CONFIG):
168 fd = open(filename, 'r')
169 result = collection.defaultdict(dict)
170 section = None
171 for line in fd:
172 line = line.strip()
173 if not line or line.startswith('#'):
174 continue
175
176 if line.startswith('[') and line.endswith(']'):
177 section = line[1:-1]
178 result[section] = {}
179 continue
180 elif not section:
181 raise Exception('Invalid kdesktoprc file')
182
183 unpack = line.split('=', 1)
184 if len(unpack) == 2:
185 key, val = unpack
186 else:
187 key, val = unpack[0], None
188 result[section][key] = val
189
190 fd.close()
191 return result
192
193 def _writeKDEConfig(self, config, filename = KDE_CONFIG):
194 fd = open(filename, 'w')
195 for section, values in config.items():
196 print >>fd, '[%s]' % section
197 for k, v in values.items():
198 if v != None:
199 print >>fd, '%s=%s' % (k,v)
200 else:
201 print >>fd, k
202 print >>fd
203 fd.close()
204
205 def changeTo(self, file):
206 kdeconfig = self._parseKDEConfig()
207 #kdeconfig['Background Common']['DrawBackgroundPerScreen_0']='true'
208 for section in ('Desktop0', 'Desktop0Screen0'):
209 kdeconfig[section]['Wallpaper'] = file
210 kdeconfig[section]['UseSHM'] = 'true'
211 kdeconfig[section]['WallpaperMode'] = 'ScaleAndCrop'
212 # Ensure that random mode is disabled...
213 if 'MultiWallpaperMode' in kdeconfig[section]:
214 del kdeconfig[section]['MultiWallpaperMode']
215
216 self._writeKDEConfig(kdeconfig)
217
218 return not subprocess.Popen(['dcop', 'kdesktop', 'KBackgroundIface', 'configure'],
219 stdout=sys.stdout, stderr=sys.stderr, stdin=open('/dev/null', 'r')).wait()