X-Git-Url: https://git.ralfj.de/lilass.git/blobdiff_plain/436dc315ae39dfac14edd1c208c26c7419e9f58e..53c8405139ffdc848753335f50b6b746a7bafcb5:/screen.py diff --git a/screen.py b/screen.py index b096fb1..b0c6577 100644 --- a/screen.py +++ b/screen.py @@ -18,6 +18,7 @@ import re, subprocess from enum import Enum +from fractions import Fraction ## utility functions @@ -66,13 +67,8 @@ class Resolution: def __str__(self): # get ratio - ratio = int(round(16.0*self.height/self.width)) - 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 + ratio = Fraction(self.width, self.height) # automatically divides by the gcd + strRatio = "%d:%d" % (ratio.numerator, ratio.denominator) return '%dx%d (%s)' %(self.width, self.height, strRatio) def __repr__(self): @@ -126,24 +122,24 @@ class ScreenSetup: 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.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)) + return """""" % (str(self.name), str(self.edid), ", ".join(str(r) for r in self.getResolutionList())) def isConnected(self): - assert (self.edid is None) == (len(self.resolutions)==0) + 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) + self._resolutions.add(resolution) def appendToEdid(self, s): if self.edid is None: @@ -151,10 +147,8 @@ class Connector: else: self.edid += s - def getPreferredResolution(self): - if self.preferredResolution: - return self.preferredResolution - return max(self.resolutions, key=lambda r: r.pixelCount()) + def getResolutionList(self): + return sorted(self._resolutions, key=lambda r: (0 if r==self.preferredResolution else 1, -r.pixelCount())) class ScreenSituation: connectors = [] # contains all the Connector objects @@ -235,13 +229,13 @@ class ScreenSituation: # return available internal resolutions def internalResolutions(self): - return self.internalConnector.resolutions + return self.internalConnector.getResolutionList() # 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.externalConnector.resolutions + return self.externalConnector.getResolutionList() # return resolutions available for both internal and external screen def commonResolutions(self):