]> code.delx.au - bg-scripts/blobdiff - lib/x11_helpers.py
Initial import
[bg-scripts] / lib / x11_helpers.py
diff --git a/lib/x11_helpers.py b/lib/x11_helpers.py
new file mode 100644 (file)
index 0000000..b479132
--- /dev/null
@@ -0,0 +1,38 @@
+#! python
+# Copyright 2007 Greg Darke <starstuff@optusnet.com.au>
+# Licensed for distribution under the GPL version 2, check COPYING for details
+# Some little helper utils
+
+import subprocess, commands, itertools
+
+__all__ = ('getResolutions', )
+
+def _seperateGroups(lines):
+       ret = []
+       current_section = []
+       for line in lines:
+               if line.strip() == '':
+                       ret.append(current_section)
+                       current_section = []
+                       continue
+               current_section.append(line)
+       if current_section:
+               ret.append(current_section)
+       return ret
+
+def getResolutions():
+       xdpyinfo_status, xdpyinfo_output = commands.getstatusoutput('xdpyinfo')
+       lines = xdpyinfo_output.splitlines()
+       groups = _seperateGroups(xdpyinfo_output.splitlines())
+
+       screens = []
+       for screen_data in itertools.islice(groups, 1, None, None):
+               _, screen_number = screen_data[0].split()
+               # remove the leading and trailing characters
+               screen_number = screen_number[1:-1]
+
+               _, screen_resolution_str, _, _, _ = screen_data[1].strip().split()
+               screen_resolution = screen_resolution_str.split('x')
+
+               screens.append( (screen_number, tuple(int(val) for val in screen_resolution)))
+       return dict(screens)