fix preferred Resolution and new config file location
authorConstantin Berhard <git.mail.enormator@xoxy.net>
Tue, 17 Nov 2015 14:31:06 +0000 (15:31 +0100)
committerConstantin Berhard <git.mail.enormator@xoxy.net>
Tue, 17 Nov 2015 14:31:06 +0000 (15:31 +0100)
lilass
screen.py
util.py [new file with mode: 0644]

diff --git a/lilass b/lilass
index 7b94e188e7aa5e5eecd9b0896854fcee551cbf65..f532b01064e8e9545e6bb0909bd7c70e8a8efb4d 100755 (executable)
--- a/lilass
+++ b/lilass
@@ -16,9 +16,9 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
-import argparse, sys, os, re, subprocess
+import argparse, sys, os, os.path, shutil, re, subprocess
 from enum import Enum
-import gui, screen
+import gui, screen, util
 frontend = gui.getFrontend("cli") # the fallback, until we got a proper frontend. This is guaranteed to be available.
 
 
@@ -30,7 +30,6 @@ def commonInternalConnectorNames():
         for suffix in commonInternalConnectorSuffices:
             yield prefix+suffix
 
-
 # Load a section-less config file: maps parameter names to space-separated lists of strings (with shell quotation)
 def loadConfigFile(filename):
     import shlex
@@ -106,7 +105,14 @@ if __name__ == "__main__":
         frontend = gui.getFrontend(cmdArgs.frontend)
         
         # load configuration
-        config = loadConfigFile(os.getenv('HOME') + '/.lilass.conf')
+        legacyConfigFilePath = os.getenv('HOME') + '/.lilass.conf'
+        configDirectory = util.getConfigDirectory()
+        configFilePath = os.path.join(configDirectory, "lilass.conf")
+        if os.path.isfile(legacyConfigFilePath) and not os.path.isfile(configFilePath):
+            # looks like we just upgraded to a new version of lilass
+            util.mkdirP(configDirectory)
+            shutil.move(legacyConfigFilePath, configFilePath)
+        config = loadConfigFile(configFilePath)
         
         # see what situation we are in
         situation = situationByConfig(config)
@@ -135,7 +141,7 @@ if __name__ == "__main__":
                 if setup is None: sys.exit(1) # the user canceled
         else:
             # use first resolution of internal connector
-            setup = screen.ScreenSetup(intResolution = situation.internalResolutions()[0], extResolution = None)
+            setup = screen.ScreenSetup(intResolution = situation.internalConnector.getPreferredResolution(), extResolution = None)
         
         # call xrandr
         xrandrCall = situation.forXrandr(setup)
index 9942e9838296e9a8eecef3d0b8c9475b81d19f8f..b096fb120382832979595c6f81d1bf9984f3d46d 100644 (file)
--- a/screen.py
+++ b/screen.py
@@ -129,6 +129,7 @@ class Connector:
         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)
@@ -149,6 +150,11 @@ class Connector:
             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 = [] # contains all the Connector objects
@@ -209,6 +215,8 @@ class ScreenSituation:
                 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)
diff --git a/util.py b/util.py
new file mode 100644 (file)
index 0000000..00c80a1
--- /dev/null
+++ b/util.py
@@ -0,0 +1,23 @@
+import os, os.path
+
+def getConfigDirectory():
+    d = os.environ.get("XDG_CONFIG_HOME")
+    if d:
+        return os.path.join(d, "lilass")
+    d = os.path.expanduser("~")
+    if d:
+        return os.path.join(d, ".config", "lilass")
+    raise Exception("Couldn't find config directory")
+
+def getDataDirectory():
+    d = os.environ.get("XDG_DATA_HOME")
+    if d:
+        return os.path.join(d, "lilass")
+    d = os.path.expanduser("~")
+    if d:
+        return os.path.join(d, ".local", "share", "lilass")
+    raise Exception("Couldn't find data directory.")
+
+def mkdirP(path, mode=0o700):
+    os.makedirs(path, mode=mode, exist_ok=True)
+