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(r'^([\w]+) connected ', line)
15 connector = m.groups()[0]
16 assert connector not in connectors
17 connectors[connector] = []
20 m = re.search(r'^ ([\d]+)x([\d]+) +', line)
22 assert connector is not None
23 connectors[connector].append((int(m.groups()[0]), int(m.groups()[1])))
25 if p.returncode != 0: raise Exception("Querying xrandr for data failed")
30 return str(w)+'x'+str(h)
35 ratio = int(round(16.0*h/w))
36 if ratio == 12: # 16:12 = 4:3
38 elif ratio == 13: # 16:12.8 = 5:4
40 else: # let's just hope this will never be 14 or more...
41 strRatio = '16:%d' % ratio
42 return '%dx%d (%s)' %(w, h, strRatio)
47 connectors = getXrandrInformation()
48 internalResolutions = connectors[internalName] # there must be a screen assoicated to the internal connector
49 externalResolutions = connectors.get(externalName)
52 if externalResolutions is not None: # we need to ask what to do
53 extPosition = PositionSelection(map(res2user, internalResolutions), map(res2user, externalResolutions))
55 if not extPosition.result(): sys.exit(1) # the user canceled
56 extResolution = res2xrandr(externalResolutions[extPosition.extResolutions.currentIndex()])
57 intResolution = res2xrandr(internalResolutions[extPosition.intResolutions.currentIndex()])
59 externalArgs = ["--mode", extResolution] # we definitely want an external screen
60 if extPosition.extOnly.isChecked():
61 internalArgs = ["--off"]
62 externalArgs += ["--primary"]
64 # there are two screens
65 internalArgs = ["--mode", intResolution]
66 if extPosition.posLeft.isChecked():
67 externalArgs += ["--left-of", internalName]
69 externalArgs += ["--right-of", internalName]
70 if extPosition.primExt.isChecked():
71 externalArgs += ["--primary"]
73 internalArgs += ["--primary"]
75 internalArgs = ["--mode", res2str(internalResolutions[0]), "--primary"]
76 externalArgs = ["--off"]
78 call = ["xrandr", "--output", internalName] + internalArgs + ["--output", externalName] + externalArgs
79 print "Call that will be made:",call
80 subprocess.check_call(call)