a8742a37d295c3788448ada6f515b5646cd42c7b
[lilass.git] / external_screen.py
1 #!/usr/bin/python
2 import sys, re, subprocess
3 from PyQt4 import QtGui
4 from selector_window import PositionSelection
5 app = QtGui.QApplication(sys.argv)
6
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
11         for line in p.stdout:
12                 # new connector?
13                 m = re.search('^([\w]+) connected ', line)
14                 if m is not None:
15                         connector = m.groups()[0]
16                         assert connector not in connectors
17                         connectors[connector] = []
18                         continue
19                 # new resolution?
20                 m = re.search('^   ([\d]+x[\d]+) +', line)
21                 if m is not None:
22                         assert connector is not None
23                         connectors[connector].append(int(m.groups()[0]))
24         return connectors
25
26 # Check screen setup
27 internalName = "LVDS"
28 externalName = "CRT1"
29 connectors = getXrandrInformation()
30 internalResolutions = connectors[internalName] # there must be a screen assoicated to the internal connector
31 externalResolutions = connectors.get(externalName)
32
33 # Check what to do
34 internalArgs = ["--mode", internalResolutions[0]] # there must be a resolution for the internal screen
35 externalArgs = ["--off"]
36 if externalResolutions is not None: # we need to ask what to do
37         extPosition = PositionSelection(externalResolutions)
38         extPosition.exec_()
39         if not extPosition.result(): sys.exit(1) # the user canceled
40         # build command-line
41         externalArgs = ["--mode", extPosition.resolution] # we definitely want an external screen
42         if extPosition.position == PositionSelection.EXTERNAL_ONLY:
43                 internalArgs = ["--off"]
44         elif extPosition.position == PositionSelection.LEFT:
45                 externalArgs += ["--left-of", internalName]
46         else:
47                 externalArgs += ["--right-of", internalName]
48 # and do it
49 args = ["--output", internalName] + internalArgs + ["--output", externalName] + externalArgs
50 print args
51 subprocess.check_call(["xrandr"] + args)