]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blobdiff - autorandr.py
Keep outputs with edid when loading profiles
[deb_pkgs/autorandr.git] / autorandr.py
index fd18b15e42360b1ff5060682cab54657b04c656e..1be7e7bbd31763397e3507217d15a285db4a7f8d 100755 (executable)
@@ -83,12 +83,14 @@ class XrandrOutput(object):
             disconnected |
             unknown\ connection |
             (?P<connected>connected)\s+                                                 # If connected:
+            (?:
             (?P<primary>primary\ )?                                                     # Might be primary screen
-            (?P<width>[0-9]+)x(?P<height>[0-9]+)                                        # Resolution
+            (?P<width>[0-9]+)x(?P<height>[0-9]+)                                        # Resolution (might be overridden below!)
             \+(?P<x>[0-9]+)\+(?P<y>[0-9]+)\s+                                           # Position
             (?:\(0x[0-9a-fA-F]+\)\s+)?                                                  # XID
             (?P<rotate>(?:normal|left|right|inverted))\s+                               # Rotation
             (?:(?P<reflect>X\ and\ Y|X|Y)\ axis)?                                       # Reflection
+            )?                                                                          # .. but everything of the above only if the screen is in use.
         ).*
         (?:\s*(?:                                                                       # Properties of the output
             Gamma: (?P<gamma>[0-9\.:\s]+) |                                             # Gamma value
@@ -98,7 +100,8 @@ class XrandrOutput(object):
         ))+
         \s*
         (?P<modes>(?:
-            [0-9]+x[0-9]+.+?\*current.+\s+h:.+\s+v:.+clock\s+(?P<rate>[0-9\.]+)Hz\s* |  # Interesting (current) resolution: Extract rate
+            (?P<mode_width>[0-9]+)x(?P<mode_height>[0-9]+).+?\*current.+\s+
+                h:.+\s+v:.+clock\s+(?P<rate>[0-9\.]+)Hz\s* |                            # Interesting (current) resolution: Extract rate
             [0-9]+x[0-9]+.+\s+h:.+\s+v:.+\s*                                            # Other resolutions
         )*)
     """
@@ -190,13 +193,21 @@ class XrandrOutput(object):
         if not match["connected"]:
             options["off"] = None
             edid = None
+        elif not match["width"]:
+            options["off"] = None
+            edid = "".join(match["edid"].strip().split())
         else:
-            if match["rotate"] not in ("left", "right"):
-                options["mode"] = "%sx%s" % (match["width"], match["height"])
+            if "mode_width" in match:
+                options["mode"] = "%sx%s" % (match["mode_width"], match["mode_height"])
             else:
-                options["mode"] = "%sx%s" % (match["height"], match["width"])
+                if match["rotate"] not in ("left", "right"):
+                    options["mode"] = "%sx%s" % (match["width"], match["height"])
+                else:
+                    options["mode"] = "%sx%s" % (match["height"], match["width"])
             if match["rotate"] != "normal":
                 options["rotate"] = match["rotate"]
+            if "primary" in match and match["primary"]:
+                options["primary"] = None
             if "reflect" in match:
                 if match["reflect"] == "X":
                     options["reflect"] = "x"
@@ -209,6 +220,10 @@ class XrandrOutput(object):
                 transformation = ",".join(match["transform"].strip().split())
                 if transformation != "1.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,1.000000":
                     options["transform"] = transformation
+                    if "mode_width" not in match:
+                        # TODO We'd need to apply the reverse transformation here. Let's see if someone complains, I doubt that this
+                        # special case is actually required.
+                        print("Warning: Output %s has a transformation applied. Could not determine correct mode!", file=sys.stderr)
             if match["gamma"]:
                 gamma = match["gamma"].strip()
                 if gamma != "1.0:1.0:1.0":
@@ -227,17 +242,17 @@ class XrandrOutput(object):
             if line:
                 line = line.split(None, 1)
                 options[line[0]] = line[1] if len(line) > 1 else None
-        if "off" in options:
+
+        if options["output"] in edid_map:
+            edid = edid_map[options["output"]]
+        elif "off" in options:
             edid = None
         else:
-            if options["output"] in edid_map:
-                edid = edid_map[options["output"]]
-            else:
-                fuzzy_edid_map = [ re.sub("(card[0-9]+|-)", "", x) for x in edid_map.keys() ]
-                fuzzy_output = re.sub("(card[0-9]+|-)", "", options["output"])
-                if fuzzy_output not in fuzzy_edid_map:
-                    raise RuntimeError("Failed to find a corresponding output in config/setup for output `%s'" % options["output"])
-                edid = edid_map[list(edid_map.keys())[fuzzy_edid_map.index(fuzzy_output)]]
+            fuzzy_edid_map = [ re.sub("(card[0-9]+|-)", "", x) for x in edid_map.keys() ]
+            fuzzy_output = re.sub("(card[0-9]+|-)", "", options["output"])
+            if fuzzy_output not in fuzzy_edid_map:
+                raise RuntimeError("Failed to find a corresponding output in config/setup for output `%s'" % options["output"])
+            edid = edid_map[list(edid_map.keys())[fuzzy_edid_map.index(fuzzy_output)]]
         output = options["output"]
         del options["output"]
 
@@ -270,6 +285,8 @@ def debug_regexp(pattern, string):
         bounds = ( 0, len(string) )
         while bounds[0] != bounds[1]:
             half = int((bounds[0] + bounds[1]) / 2)
+            if half == bounds[0]:
+                break
             bounds = (half, bounds[1]) if regex.search(pattern, string[:half], partial=True) else (bounds[0], half - 1)
         partial_length = bounds[0]
         return ("Regular expression matched until position "
@@ -322,8 +339,8 @@ def load_profiles(profile_path):
             else:
                 buffer.append(line)
 
-        for output_name, output in config.items():
-            if "off" in output.options:
+        for output_name in list(config.keys()):
+            if config[output_name].edid is None:
                 del config[output_name]
 
         profiles[profile] = config