2 import sys, re, subprocess
3 from PyQt4 import QtGui
4 from selector_window import PositionSelection
5 app = QtGui.QApplication(sys.argv)
7 def getXrandrInformation():
8 p = subprocess.Popen(["xrandr", "-q"], stdout=subprocess.PIPE)
9 connectors = {} # map of connector names to a list of resolutions
10 connector = None # current connector
13 m = re.search('^([\w]+) connected ', line)
15 connector = m.groups()[0]
16 assert connector not in connectors
17 connectors[connector] = []
20 m = re.search('^ ([\d]+)x([\d]+) +', line)
22 assert connector is not None
23 connectors[connector].append((int(m.groups()[0]), int(m.groups()[1])))
28 return str(w)+'x'+str(h)
33 connectors = getXrandrInformation()
34 internalResolutions = connectors[internalName] # there must be a screen assoicated to the internal connector
35 externalResolutions = connectors.get(externalName)
38 internalArgs = ["--mode", res2str(internalResolutions[0])] # there must be a resolution for the internal screen
39 externalArgs = ["--off"]
40 if externalResolutions is not None: # we need to ask what to do
41 extPosition = PositionSelection(map(res2str, externalResolutions))
43 if not extPosition.result(): sys.exit(1) # the user canceled
45 externalArgs = ["--mode", extPosition.resolution] # we definitely want an external screen
46 if extPosition.position == PositionSelection.EXTERNAL_ONLY:
47 internalArgs = ["--off"]
48 elif extPosition.position == PositionSelection.LEFT:
49 externalArgs += ["--left-of", internalName]
51 externalArgs += ["--right-of", internalName]
53 args = ["--output", internalName] + internalArgs + ["--output", externalName] + externalArgs
55 subprocess.check_call(["xrandr"] + args)