add options to select primary screen and internal screen resolution
[lilass.git] / external_screen.py
index a8742a37d295c3788448ada6f515b5646cd42c7b..6d47ec725962fdc1a713a1b7f756fba9cb7ce07a 100755 (executable)
@@ -10,19 +10,25 @@ def getXrandrInformation():
        connector = None # current connector
        for line in p.stdout:
                # new connector?
        connector = None # current connector
        for line in p.stdout:
                # new connector?
-               m = re.search('^([\w]+) connected ', line)
+               m = re.search(r'^([\w]+) connected ', line)
                if m is not None:
                        connector = m.groups()[0]
                        assert connector not in connectors
                        connectors[connector] = []
                        continue
                # new resolution?
                if m is not None:
                        connector = m.groups()[0]
                        assert connector not in connectors
                        connectors[connector] = []
                        continue
                # new resolution?
-               m = re.search('^   ([\d]+x[\d]+) +', line)
+               m = re.search(r'^   ([\d]+)x([\d]+) +', line)
                if m is not None:
                        assert connector is not None
                if m is not None:
                        assert connector is not None
-                       connectors[connector].append(int(m.groups()[0]))
+                       connectors[connector].append((int(m.groups()[0]), int(m.groups()[1])))
+       p.communicate()
+       if p.returncode != 0: raise Exception("Querying xrandr for data failed")
        return connectors
 
        return connectors
 
+def res2str(res):
+       (w, h) = res
+       return str(w)+'x'+str(h)
+
 # Check screen setup
 internalName = "LVDS"
 externalName = "CRT1"
 # Check screen setup
 internalName = "LVDS"
 externalName = "CRT1"
@@ -31,21 +37,32 @@ internalResolutions = connectors[internalName] # there must be a screen assoicat
 externalResolutions = connectors.get(externalName)
 
 # Check what to do
 externalResolutions = connectors.get(externalName)
 
 # Check what to do
-internalArgs = ["--mode", internalResolutions[0]] # there must be a resolution for the internal screen
-externalArgs = ["--off"]
 if externalResolutions is not None: # we need to ask what to do
 if externalResolutions is not None: # we need to ask what to do
-       extPosition = PositionSelection(externalResolutions)
+       extPosition = PositionSelection(map(res2str, internalResolutions), map(res2str, externalResolutions))
        extPosition.exec_()
        if not extPosition.result(): sys.exit(1) # the user canceled
        extPosition.exec_()
        if not extPosition.result(): sys.exit(1) # the user canceled
+       extResolution = res2str(externalResolutions[extPosition.extResolutions.currentIndex()])
+       intResolution = res2str(internalResolutions[extPosition.intResolutions.currentIndex()])
        # build command-line
        # build command-line
-       externalArgs = ["--mode", extPosition.resolution] # we definitely want an external screen
-       if extPosition.position == PositionSelection.EXTERNAL_ONLY:
+       externalArgs = ["--mode", extResolution] # we definitely want an external screen
+       if extPosition.extOnly.isChecked():
                internalArgs = ["--off"]
                internalArgs = ["--off"]
-       elif extPosition.position == PositionSelection.LEFT:
-               externalArgs += ["--left-of", internalName]
+               externalArgs += ["--primary"]
        else:
        else:
-               externalArgs += ["--right-of", internalName]
+               # there are two screens
+               internalArgs = ["--mode", intResolution]
+               if extPosition.posLeft.isChecked():
+                       externalArgs += ["--left-of", internalName]
+               else:
+                       externalArgs += ["--right-of", internalName]
+               if extPosition.primExt.isChecked():
+                       externalArgs += ["--primary"]
+               else:
+                       internalArgs += ["--primary"]
+else:
+       internalArgs = ["--mode", res2str(internalResolutions[0]), "--primary"] # there must be a resolution for the internal screen
+       externalArgs = ["--off"]
 # and do it
 # and do it
-args = ["--output", internalName] + internalArgs + ["--output", externalName] + externalArgs
-print args
-subprocess.check_call(["xrandr"] + args)
+call = ["xrandr", "--output", internalName] + internalArgs + ["--output", externalName] + externalArgs
+print "Call that will be made:",call
+subprocess.check_call(call)