-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)
-
-def findAvailableConnector(tryConnectors, allConnectors):
- for connector in tryConnectors:
- if connector in allConnectors and allConnectors[connector]: # if the connector exists and is active (i.e. there is a resolution)
- return connector
- return None
-
-# the main function
-def main():
- # load connectors and options
- connectors = getXrandrInformation()
- config = loadConfigFile(os.getenv('HOME') + '/.dsl.conf')
- # find internal connector
- if 'internalConnector' in config:
- if len(config['internalConnector']) != 1:
- raise Exception("You must specify exactly one internal connector.")
- internalConnector = config['internalConnector'][0]
- if not internalConnector in connectors:
- raise Exception("Connector %s does not exist, there is an error in your config file." % internalConnector)
- else:
- # auto-config
- internalConnector = findAvailableConnector(commonInternalConnectorNames, connectors)
- if internalConnector is None:
- raise Exception("Could not automatically find internal connector, please use ~/.dsl.conf to specify it manually.")
- # all the rest is external then, obviously - unless the user wants to do that manually
- if 'externalConnectors' in config:
- externalConnectors = config['externalConnectors']
- for connector in externalConnectors:
- if not connector in connectors:
- raise Exception("Connector %s does not exist, there is an error in your config file." % connector)
- if connector == internalConnector:
- raise Exception("%s is both internal and external, that doesn't make sense." % connector)
- else:
- externalConnectors = connectors.keys()
- externalConnectors.remove(internalConnector)
- if not externalConnectors:
- raise Exception("No external connector found - either your config is wrong, or your machine has only one connector.")
-
- # default: screen off
- args = {} # maps connector names to xrand arguments
- for c in externalConnectors+[internalConnector]:
- args[c] = ["--off"]
-
- # Check what to do
- usedExternalConnector = findAvailableConnector(externalConnectors, connectors) # *the* external connector which is actually used
- if usedExternalConnector is not None: # there's an external screen connected, we need to ask what to do
- internalResolutions = connectors[internalConnector]
- externalResolutions = connectors[usedExternalConnector]
- dialogue = gui.getDialogue(usedExternalConnector, map(res2user, internalResolutions), map(res2user, externalResolutions))
- if not dialogue.run(): sys.exit(1) # the user canceled
- extResolution = res2xrandr(externalResolutions[dialogue.getExtResolutionIndex()])
- intResolution = res2xrandr(internalResolutions[dialogue.getIntResolutionIndex()])
- relPosition = dialogue.getRelativeScreenPosition()
- # build command-line
- args[usedExternalConnector] = ["--mode", extResolution] # set external screen to desired resolution
- if relPosition == RelativeScreenPosition.EXTERNAL_ONLY:
- args[usedExternalConnector] += ["--primary"]
- else:
- # there are two screens
- args[internalConnector] = ["--mode", intResolution] # set internal screen to desired resolution
- # set position
- if relPosition == RelativeScreenPosition.LEFT:
- args[usedExternalConnector] += ["--left-of", internalConnector]
- else:
- args[usedExternalConnector] += ["--right-of", internalConnector]
- # set primary screen
- if dialogue.externalIsPrimary():
- args[usedExternalConnector] += ["--primary"]
- else:
- args[internalConnector] += ["--primary"]
- else:
- # use first resolution
- args[internalConnector] = ["--mode", res2xrandr(connectors[internalConnector][0]), "--primary"]
- # and do it
- call = ["xrandr"]
- for name in args:
- call += ["--output", name] + args[name]
- print "Call that will be made:",call
- subprocess.check_call(call)