+def add_unused_outputs(source_configuration, target_configuration):
+ "Add outputs that are missing in target to target, in 'off' state"
+ for output_name, output in source_configuration.items():
+ if output_name not in target_configuration:
+ target_configuration[output_name] = XrandrOutput(output_name, output.edid, { "off": None })
+
+def generate_virtual_profile(configuration, modes, profile_name):
+ "Generate one of the virtual profiles"
+ configuration = copy.deepcopy(configuration)
+ if profile_name == "common":
+ common_resolution = [ set(( ( mode["width"], mode["height"] ) for mode in output )) for output in modes.values() ]
+ common_resolution = reduce(lambda a, b: a & b, common_resolution[1:], common_resolution[0])
+ common_resolution = sorted(common_resolution, key=lambda a: int(a[0])*int(a[1]))
+ if common_resolution:
+ for output in configuration:
+ configuration[output].options = {}
+ if output in modes:
+ configuration[output].options["mode"] = "%sx%s" % common_resolution[-1]
+ configuration[output].options["pos"] = "0x0"
+ else:
+ configuration[output].options["off"] = None
+ elif profile_name in ("horizontal", "vertical"):
+ shift = 0
+ if profile_name == "horizontal":
+ shift_index = "width"
+ pos_specifier = "%sx0"
+ else:
+ shift_index = "height"
+ pos_specifier = "0x%s"
+
+ for output in configuration:
+ configuration[output].options = {}
+ if output in modes:
+ mode = sorted(modes[output], key=lambda a: int(a["width"])*int(a["height"]) + (10**6 if a["preferred"] else 0))[-1]
+ configuration[output].options["mode"] = "%sx%s" % (mode["width"], mode["height"])
+ configuration[output].options["rate"] = mode["rate"]
+ configuration[output].options["pos"] = pos_specifier % shift
+ shift += int(mode[shift_index])
+ else:
+ configuration[output].options["off"] = None
+ return configuration
+