+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
+ self.__lastResolution = None
+ self.hasLastResolution = False
+
+ def __str__(self):
+ return str(self.name)
+
+ def __repr__(self):
+ return """<Connector "%s" EDID="%s" resolutions="%s">""" % (str(self.name), str(self.edid), ", ".join(str(r) for r in self.getResolutionList()))
+
+ def __setLastRes(self, res):
+ # res == None means this display was last switched off
+ if res is not None and not res in self._resolutions:
+ raise ValueError("Resolution "+res+" not available for "+self.name+".")
+ self.__lastResolution = res
+ self.hasLastResolution = True
+
+ def __getLastRes(self):
+ if not self.hasLastResolution:
+ raise ValueError("Connector %s has no last known resolution." % self.name)
+ return self.__lastResolution
+
+ lastResolution = property(__getLastRes, __setLastRes)
+
+ 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 getResolutionList(self):
+ return sorted(self._resolutions, key=lambda r: (0 if self.hasLastResolution and r==self.lastResolution else 1, 0 if r==self.preferredResolution else 1, -r.pixelCount()))