(?: # Differentiate disconnected and connected in first line
disconnected |
unknown\ connection |
- (?P<connected>connected)\s+ # If connected:
- (?:
- (?P<primary>primary\ )? # Might be primary screen
+ (?P<connected>connected)
+ )
+ \s*
+ (?P<primary>primary\ )? # Might be primary screen
+ (?:\s*
(?P<width>[0-9]+)x(?P<height>[0-9]+) # Resolution (might be overridden below!)
- \+(?P<x>[0-9]+)\+(?P<y>[0-9]+)\s+ # Position
+ \+(?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.
- ).*
+ )? # .. but everything of the above only if the screen is in use.
+ (?:[\ \t]*\([^\)]+\))(?:\s*[0-9]+mm\sx\s[0-9]+mm)?
+ (?:[\ \t]*panning\ (?P<panning>[0-9]+x[0-9]+\+[0-9]+\+[0-9]+))? # Panning information
+ (?:[\ \t]*tracking\ (?P<tracking>[0-9]+x[0-9]+\+[0-9]+\+[0-9]+))? # Tracking information
+ (?:[\ \t]*border\ (?P<border>(?:[0-9]+/){3}[0-9]+))? # Border information
(?:\s*(?: # Properties of the output
Gamma: (?P<gamma>[0-9\.: ]+) | # Gamma value
Transform: (?P<transform>(?:[\-0-9\. ]+\s+){3}) | # Transformation matrix
XRANDR_13_DEFAULTS = {
"transform": "1,0,0,0,1,0,0,0,1",
+ "panning": "0x0",
}
XRANDR_12_DEFAULTS = {
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))
def sort_key(self):
"Return a key to sort the outputs for xrandr invocation"
if not self.edid:
+ return -2
+ if "off" in self.options:
return -1
if "pos" in self.options:
x, y = map(float, self.options["pos"].split("x"))
options = {}
if not match["connected"]:
- options["off"] = None
edid = None
- elif not match["width"]:
+ else:
+ edid = "".join(match["edid"].strip().split()) if match["edid"] else "%s-%s" % (XrandrOutput.EDID_UNAVAILABLE, match["output"])
+
+ if not match["width"]:
options["off"] = None
- edid = "".join(match["edid"].strip().split())
else:
if match["mode_width"]:
options["mode"] = "%sx%s" % (match["mode_width"], match["mode_height"])
elif match["reflect"] == "X and Y":
options["reflect"] = "xy"
options["pos"] = "%sx%s" % (match["x"], match["y"])
+ if match["panning"]:
+ panning = [ match["panning"] ]
+ if match["tracking"]:
+ panning += [ "/", match["tracking"] ]
+ if match["border"]:
+ panning += [ "/", match["border"] ]
+ options["panning"] = "".join(panning)
if match["transform"]:
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["gamma"] = gamma
if match["rate"]:
options["rate"] = match["rate"]
- edid = "".join(match["edid"].strip().split())
return XrandrOutput(match["output"], edid, options), modes
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
except:
return False
-def apply_configuration(configuration, dry_run=False):
+def apply_configuration(new_configuration, current_configuration, dry_run=False):
"Apply a configuration"
- outputs = sorted(configuration.keys(), key=lambda x: configuration[x].sort_key)
+ outputs = sorted(new_configuration.keys(), key=lambda x: new_configuration[x].sort_key)
if dry_run:
base_argv = [ "echo", "xrandr" ]
else:
# 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.
+ # - If an active screen already has a transformation and remains active,
+ # the xrandr call fails with an invalid RRSetScreenSize parameter error.
+ # Update the configuration in 3 passes in that case. (On Haswell graphics,
+ # at least.)
+ auxiliary_changes_pre = []
disable_outputs = []
enable_outputs = []
+ remain_active_count = 0
for output in outputs:
- if not configuration[output].edid or "off" in configuration[output].options:
- disable_outputs.append(configuration[output].option_vector)
+ if not new_configuration[output].edid or "off" in new_configuration[output].options:
+ disable_outputs.append(new_configuration[output].option_vector)
else:
- enable_outputs.append(configuration[output].option_vector)
+ if "off" not in current_configuration[output].options:
+ remain_active_count += 1
+ enable_outputs.append(new_configuration[output].option_vector)
+ if xrandr_version() >= Version("1.3.0") and "transform" in current_configuration[output].options:
+ auxiliary_changes_pre.append(["--output", output, "--transform", "none"])
+
+ # Perform pe-change auxiliary changes
+ if auxiliary_changes_pre:
+ argv = base_argv + list(chain.from_iterable(auxiliary_changes_pre))
+ if subprocess.call(argv) != 0:
+ raise RuntimeError("Command failed: %s" % " ".join(argv))
- # 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:
+ # Disable unused outputs, but make sure that there always is at least one active screen
+ disable_keep = 0 if remain_active_count else 1
+ if len(disable_outputs) > disable_keep:
+ if subprocess.call(base_argv + list(chain.from_iterable(disable_outputs[:-1] if disable_keep else disable_outputs))) != 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_outputs = disable_outputs[-1:]
+ disable_outputs = disable_outputs[-1:] if disable_keep else []
# 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
try:
if "--dry-run" in options:
- apply_configuration(load_config, True)
+ apply_configuration(load_config, config, True)
else:
exec_scripts(scripts_path, "preswitch")
- apply_configuration(load_config, False)
+ apply_configuration(load_config, config, False)
exec_scripts(scripts_path, "postswitch")
except Exception as e:
print("Failed to apply profile '%s':\n%s" % (load_profile, str(e)), file=sys.stderr)