X-Git-Url: https://git.ralfj.de/lilass.git/blobdiff_plain/6f78b3a6bb1715b8c742a124f696b6bb8b36751a..babc1614b62e85bacb95ca42d42a7eb618031af9:/external_screen.py?ds=sidebyside diff --git a/external_screen.py b/external_screen.py index e365ef5..895405e 100755 --- a/external_screen.py +++ b/external_screen.py @@ -10,23 +10,37 @@ def getXrandrInformation(): 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? - 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 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 -def res2str(res): +def res2xrandr(res): (w, h) = res return str(w)+'x'+str(h) +def res2user(res): + (w, h) = res + # get ratio + ratio = int(round(16.0*h/w)) + if ratio == 12: # 16:12 = 4:3 + strRatio = '4:3' + elif ratio == 13: # 16:12.8 = 5:4 + strRatio = '5:4' + else: # let's just hope this will never be 14 or more... + strRatio = '16:%d' % ratio + return '%dx%d (%s)' %(w, h, strRatio) + # Check screen setup internalName = "LVDS" externalName = "CRT1" @@ -35,21 +49,32 @@ internalResolutions = connectors[internalName] # there must be a screen assoicat externalResolutions = connectors.get(externalName) # Check what to do -internalArgs = ["--mode", res2str(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 - extPosition = PositionSelection(map(res2str, externalResolutions)) + extPosition = PositionSelection(map(res2user, internalResolutions), map(res2user, externalResolutions)) extPosition.exec_() if not extPosition.result(): sys.exit(1) # the user canceled + extResolution = res2xrandr(externalResolutions[extPosition.extResolutions.currentIndex()]) + intResolution = res2xrandr(internalResolutions[extPosition.intResolutions.currentIndex()]) # 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"] - elif extPosition.position == PositionSelection.LEFT: - externalArgs += ["--left-of", internalName] + externalArgs += ["--primary"] 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"] + externalArgs = ["--off"] # 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)