X-Git-Url: https://git.ralfj.de/lilass.git/blobdiff_plain/a6c79626b2d248d0484b5242c0a2051da8ec84d1..436dc315ae39dfac14edd1c208c26c7419e9f58e:/screen.py diff --git a/screen.py b/screen.py index d567912..b096fb1 100644 --- a/screen.py +++ b/screen.py @@ -47,7 +47,6 @@ class RelativeScreenPosition(Enum): self._value_ = len(cls.__members__) self.text = text - class Resolution: '''Represents a resolution of a screen''' def __init__(self, width, height): @@ -62,6 +61,9 @@ class Resolution: def __ne__(self, other): return not self.__eq__(other) + def __hash__(self): + return hash(("Resolution",self.width,self.height)) + def __str__(self): # get ratio ratio = int(round(16.0*self.height/self.width)) @@ -76,6 +78,9 @@ class Resolution: def __repr__(self): return 'screen.Resolution('+self.forXrandr()+')' + def pixelCount(self): + return self.width * self.height + def forXrandr(self): return str(self.width)+'x'+str(self.height) @@ -119,11 +124,42 @@ class ScreenSetup: }[self.relPosition], intName] return args +class Connector: + def __init__(self, name=None): + self.name = name # connector name, e.g. "HDMI1" + self.edid = None # EDID string for the connector, or None if disconnected + self.resolutions = set() # list of Resolution objects, empty if disconnected + self.preferredResolution = None + + def __str__(self): + return str(self.name) + + def __repr__(self): + return """""" % (str(self.name), str(self.edid), ", ".join(str(r) for r in self.resolutions)) + + def isConnected(self): + assert (self.edid is None) == (len(self.resolutions)==0) + return self.edid is not None + + def addResolution(self, resolution): + assert isinstance(resolution, Resolution) + self.resolutions.add(resolution) + + def appendToEdid(self, s): + if self.edid is None: + self.edid = s + else: + self.edid += s + + def getPreferredResolution(self): + if self.preferredResolution: + return self.preferredResolution + return max(self.resolutions, key=lambda r: r.pixelCount()) class ScreenSituation: - connectors = {} # maps connector names to lists of Resolution (empty list -> disabled connector) - internalConnector = None # name of the internal connector (will be an enabled one) - externalConnector = None # name of the used external connector (an enabled one), or None + connectors = [] # contains all the Connector objects + internalConnector = None # the internal Connector object (will be an enabled one) + externalConnector = None # the used external Connector object (an enabled one), or None '''Represents the "screen situation" a machine can be in: Which connectors exist, which resolutions do they have, what are the names for the internal and external screen''' def __init__(self, internalConnectorNames, externalConnectorNames = None): @@ -131,6 +167,9 @@ class ScreenSituation: just choose any remaining connector.''' # which connectors are there? self._getXrandrInformation() + for c in self.connectors: + print(repr(c)) + print() # figure out which is the internal connector self.internalConnector = self._findAvailableConnector(internalConnectorNames) if self.internalConnector is None: @@ -138,8 +177,8 @@ class ScreenSituation: print("Detected internal connector:",self.internalConnector) # and the external one if externalConnectorNames is None: - externalConnectorNames = list(self.connectors.keys()) - externalConnectorNames.remove(self.internalConnector) + externalConnectorNames = map(lambda c: c.name, self.connectors) + externalConnectorNames = set(filter(lambda name: name != self.internalConnector.name, externalConnectorNames)) self.externalConnector = self._findAvailableConnector(externalConnectorNames) if self.internalConnector == self.externalConnector: raise Exception("Internal and external connector are the same. This must not happen. Please fix ~/.dsl.conf."); @@ -148,7 +187,16 @@ class ScreenSituation: # Run xrandr and fill the dict of connector names mapped to lists of available resolutions. def _getXrandrInformation(self): connector = None # current connector - for line in processOutputGen("xrandr", "-q"): + readingEdid = False + for line in processOutputGen("xrandr", "-q", "--verbose"): + if readingEdid: + m = re.match(r'^\s*([0-9a-f]+)\s*$', line) + if m is not None: + connector.appendToEdid(m.group(1)) + continue + else: + readingEdid = False + # fallthrough to the rest of the loop for parsing of this line # screen? m = re.search(r'^Screen [0-9]+: ', line) if m is not None: # ignore this line @@ -157,55 +205,61 @@ class ScreenSituation: # new connector? m = re.search(r'^([\w\-]+) (dis)?connected ', line) if m is not None: - connector = m.groups()[0] - assert connector not in self.connectors - self.connectors[connector] = [] + connector = Connector(m.group(1)) + assert not any(c.name == connector.name for c in self.connectors) + self.connectors.append(connector) continue # new resolution? - m = re.search(r'^ ([\d]+)x([\d]+) +', line) + m = re.search(r'^\s*([\d]+)x([\d]+)', line) if m is not None: - resolution = Resolution(int(m.groups()[0]), int(m.groups()[1])) + resolution = Resolution(int(m.group(1)), int(m.group(2))) assert connector is not None - self.connectors[connector].append(resolution) + connector.addResolution(resolution) + if '+preferred' in line: + connector.preferredResolution = resolution + continue + # EDID? + m = re.search(r'^\s*EDID:\s*$', line) + if m is not None: + readingEdid = True continue # unknown line # not fatal, e.g. xrandr shows strange stuff when a display is enabled, but not connected - print("Warning: Unknown xrandr line %s" % line) + #print("Warning: Unknown xrandr line %s" % line) - # return the first available connector from those listed in , skipping disabled connectors - def _findAvailableConnector(self, tryConnectors): - for connector in tryConnectors: - if connector in self.connectors and len(self.connectors[connector]): # if the connector exists and is active (i.e. there is a resolution) - return connector + # return the first available connector from those listed in , skipping disabled connectors + def _findAvailableConnector(self, tryConnectorNames): + for c in filter(lambda c: c.name in tryConnectorNames and c.isConnected(), self.connectors): + return c return None # return available internal resolutions def internalResolutions(self): - return self.connectors[self.internalConnector] + return self.internalConnector.resolutions # return available external resolutions (or None, if there is no external screen connected) def externalResolutions(self): if self.externalConnector is None: return None - return self.connectors[self.externalConnector] + return self.externalConnector.resolutions # return resolutions available for both internal and external screen def commonResolutions(self): internalRes = self.internalResolutions() externalRes = self.externalResolutions() assert externalRes is not None - return [res for res in externalRes if res in internalRes] + return sorted(set(externalRes).intersection(internalRes), key=lambda r: -r.pixelCount()) # compute the xrandr call def forXrandr(self, setup): # turn all screens off connectorArgs = {} # maps connector names to xrand arguments - for c in self.connectors.keys(): - connectorArgs[c] = ["--off"] + for c in self.connectors: + connectorArgs[c.name] = ["--off"] # set arguments for the relevant ones - connectorArgs[self.internalConnector] = setup.getInternalArgs() + connectorArgs[self.internalConnector.name] = setup.getInternalArgs() if self.externalConnector is not None: - connectorArgs[self.externalConnector] = setup.getExternalArgs(self.internalConnector) + connectorArgs[self.externalConnector.name] = setup.getExternalArgs(self.internalConnector.name) else: assert setup.extResolution is None, "There's no external screen to set a resolution for" # now compose the arguments