useful sorting of resolutions
[lilass.git] / screen.py
index e161fcc74521c291a86363c642da57c9f6872b9e..f9bef55740804effac631a19b7d00b2b3f290eaa 100644 (file)
--- a/screen.py
+++ b/screen.py
@@ -18,7 +18,6 @@
 
 import re, subprocess
 from enum import Enum
 
 import re, subprocess
 from enum import Enum
-from functools import total_ordering
 
 ## utility functions
 
 
 ## utility functions
 
@@ -127,29 +126,33 @@ class ScreenSetup:
 
 class Connector:
     def __init__(self, name=None):
 
 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):
     
     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.resolutions))
+        return """<Connector "%s" EDID="%s" resolutions="%s">""" % (str(self.name), str(self.edid), ", ".join(str(r) for r in self.getResolutionList()))
     
     def isConnected(self):
     
     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)
         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:
             self.edid = s
         else:
             self.edid += s
     
     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 r==self.preferredResolution else 1, -r.pixelCount()))
 
 class ScreenSituation:
     connectors = [] # contains all the Connector objects
 
 class ScreenSituation:
     connectors = [] # contains all the Connector objects
@@ -210,6 +213,8 @@ class ScreenSituation:
                 resolution = Resolution(int(m.group(1)), int(m.group(2)))
                 assert connector is not None
                 connector.addResolution(resolution)
                 resolution = Resolution(int(m.group(1)), int(m.group(2)))
                 assert connector is not None
                 connector.addResolution(resolution)
+                if '+preferred' in line:
+                    connector.preferredResolution = resolution
                 continue
             # EDID?
             m = re.search(r'^\s*EDID:\s*$', line)
                 continue
             # EDID?
             m = re.search(r'^\s*EDID:\s*$', line)
@@ -228,20 +233,20 @@ class ScreenSituation:
     
     # return available internal resolutions
     def internalResolutions(self):
     
     # 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 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):
         internalRes = self.internalResolutions()
         externalRes = self.externalResolutions()
         assert externalRes is not None
     
     # return resolutions available for both internal and external screen
     def commonResolutions(self):
         internalRes = self.internalResolutions()
         externalRes = self.externalResolutions()
         assert externalRes is not None
-        return sorted(set(res for res in externalRes if res in internalRes), key=lambda r: -r.pixelCount())
+        return sorted(set(externalRes).intersection(internalRes), key=lambda r: -r.pixelCount())
     
     # compute the xrandr call
     def forXrandr(self, setup):
     
     # compute the xrandr call
     def forXrandr(self, setup):