]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blobdiff - autorandr.py
Updated README with missing contributors
[deb_pkgs/autorandr.git] / autorandr.py
index 33e3b2f2ad4c05eab4e096bc3d7b2df6010687e0..da5217bf5658f61d0855f97bffb40cdfd23551bd 100755 (executable)
@@ -126,6 +126,8 @@ class XrandrOutput(object):
 
     XRANDR_DEFAULTS = dict(list(XRANDR_13_DEFAULTS.items()) + list(XRANDR_12_DEFAULTS.items()))
 
+    EDID_UNAVAILABLE = "--CONNECTED-BUT-EDID-UNAVAILABLE-"
+
     def __repr__(self):
         return "<%s%s %s>" % (self.output, (" %s..%s" % (self.edid[:5], self.edid[-5:])) if self.edid else "", " ".join(self.option_vector))
 
@@ -212,7 +214,7 @@ class XrandrOutput(object):
             edid = None
         elif not match["width"]:
             options["off"] = None
-            edid = "".join(match["edid"].strip().split())
+            edid = "".join(match["edid"].strip().split()) if match["edid"] else "%s-%s" % (XrandrOutput.EDID_UNAVAILABLE, match["output"])
         else:
             if match["mode_width"]:
                 options["mode"] = "%sx%s" % (match["mode_width"], match["mode_height"])
@@ -244,7 +246,7 @@ class XrandrOutput(object):
                 options["gamma"] = gamma
             if match["rate"]:
                 options["rate"] = match["rate"]
-            edid = "".join(match["edid"].strip().split())
+            edid = "".join(match["edid"].strip().split()) if match["edid"] else "%s-%s" % (XrandrOutput.EDID_UNAVAILABLE, match["output"])
 
         return XrandrOutput(match["output"], edid, options), modes
 
@@ -278,9 +280,9 @@ class XrandrOutput(object):
     def edid_equals(self, other):
         "Compare to another XrandrOutput's edid and on/off-state, taking legacy autorandr behaviour (md5sum'ing) into account"
         if self.edid and other.edid:
-            if len(self.edid) == 32 and len(other.edid) != 32:
+            if len(self.edid) == 32 and len(other.edid) != 32 and not other.edid.startswith(XrandrOutput.EDID_UNAVAILABLE):
                 return hashlib.md5(binascii.unhexlify(other.edid)).hexdigest() == self.edid
-            if len(self.edid) != 32 and len(other.edid) == 32:
+            if len(self.edid) != 32 and len(other.edid) == 32 and not self.edid.startswith(XrandrOutput.EDID_UNAVAILABLE):
                 return hashlib.md5(binascii.unhexlify(self.edid)).hexdigest() == other.edid
         return self.edid == other.edid
 
@@ -390,7 +392,7 @@ def find_profiles(current_config, profiles):
 
 def profile_blocked(profile_path):
     "Check if a profile is blocked"
-    script = os.path.join(profile_path, "blocked")
+    script = os.path.join(profile_path, "block")
     if not os.access(script, os.X_OK | os.F_OK):
         return False
     return subprocess.call(script) == 0
@@ -433,33 +435,46 @@ def apply_configuration(configuration, dry_run=False):
     else:
         base_argv = [ "xrandr" ]
 
-    # Disable all unused outputs
-    argv = base_argv[:]
-    disable_argv = []
+    # There are several xrandr / driver bugs we need to take care of here:
+    # - We cannot enable more than two screens at the same time
+    #   See https://github.com/phillipberndt/autorandr/pull/6
+    #   and commits f4cce4d and 8429886.
+    # - We cannot disable all screens
+    #   See https://github.com/phillipberndt/autorandr/pull/20
+    # - We should disable screens before enabling others, because there's
+    #   a limit on the number of enabled screens
+    # - We must make sure that the screen at 0x0 is activated first,
+    #   or the other (first) screen to be activated would be moved there.
+
+    disable_outputs = []
+    enable_outputs = []
     for output in outputs:
         if not configuration[output].edid or "off" in configuration[output].options:
-            disable_argv += configuration[output].option_vector
-    if disable_argv:
-        if subprocess.call(base_argv + disable_argv) != 0:
+            disable_outputs.append(configuration[output].option_vector)
+        else:
+            enable_outputs.append(configuration[output].option_vector)
+
+    # Disable all but the last of the outputs to be disabled
+    if len(disable_outputs) > 1:
+        if subprocess.call(base_argv + list(chain.from_iterable(disable_outputs[:-1]))) != 0:
             # Disabling the outputs failed. Retry with the next command:
             # Sometimes disabling of outputs fails due to an invalid RRSetScreenSize.
             # This does not occur if simultaneously the primary screen is reset.
             pass
         else:
-            disable_argv = []
-
-    # Enable remaining outputs in pairs of two
-    # This is required because some drivers can't handle enabling many outputs
-    # in one call. See
-    # https://github.com/phillipberndt/autorandr/pull/6
-    # and commits f4cce4d and 8429886.
-    remaining_outputs = [ x for x in outputs if configuration[x].edid ]
-    for index in range(0, len(remaining_outputs), 2):
-        argv = base_argv[:]
-        if disable_argv:
-            argv += disable_argv
-            disable_argv = []
-        argv += configuration[remaining_outputs[index]].option_vector + (configuration[remaining_outputs[index + 1]].option_vector if index < len(remaining_outputs) - 1 else [])
+            disable_outputs = disable_outputs[-1:]
+
+    # If disable_outputs still has more than one output in it, one of the xrandr-calls below would
+    # disable the last two screens. This is a problem, so if this would happen, instead disable only
+    # one screen in the first call below.
+    if len(disable_outputs) > 0 and len(disable_outputs) % 2 == 0:
+        # In the context of a xrandr call that changes the display state, `--query' should do nothing
+        disable_outputs.insert(0, ['--query'])
+
+    # Enable the remaining outputs in pairs of two operations
+    operations = disable_outputs + enable_outputs
+    for index in range(0, len(operations), 2):
+        argv = base_argv + list(chain.from_iterable(operations[index:index+2]))
         if subprocess.call(argv) != 0:
             raise RuntimeError("Command failed: %s" % " ".join(argv))
 
@@ -469,6 +484,12 @@ def add_unused_outputs(source_configuration, target_configuration):
         if output_name not in target_configuration:
             target_configuration[output_name] = XrandrOutput(output_name, output.edid, { "off": None })
 
+def remove_irrelevant_outputs(source_configuration, target_configuration):
+    "Remove outputs from target that ought to be 'off' and already are"
+    for output_name, output in source_configuration.items():
+        if "off" in output.options and output_name in target_configuration and "off" in target_configuration[output_name].options:
+            del target_configuration[output_name]
+
 def generate_virtual_profile(configuration, modes, profile_name):
     "Generate one of the virtual profiles"
     configuration = copy.deepcopy(configuration)
@@ -619,6 +640,7 @@ def main(argv):
         if load_config == dict(config) and not "-f" in options and not "--force" in options:
             print("Config already loaded", file=sys.stderr)
             sys.exit(0)
+        remove_irrelevant_outputs(config, load_config)
 
         try:
             if "--dry-run" in options: