Skip to content

Commit ea7526a

Browse files
committed
Mode names need not be of the form <width>x<height>, but can be arbitrary
In the hope for _some_ sanity, the code still asumes that there is no whitespace in mode names. This fixes wertarbyte#25.
1 parent 93e5266 commit ea7526a

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

autorandr.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,17 @@ class XrandrOutput(object):
136136
))+
137137
\s*
138138
(?P<modes>(?:
139-
(?P<mode_width>[0-9]+)x(?P<mode_height>[0-9]+).+?\*current.*\s+
140-
h:.+\s+v:.+clock\s+(?P<rate>[0-9\.]+)Hz\s* | # Interesting (current) resolution: Extract rate
141-
[0-9]+x[0-9]+(?:(?!\*current).)+\s+h:.+\s+v:.+\s* # Other resolutions
139+
(?P<mode_name>\S+).+?\*current.*\s+ # Interesting (current) resolution: Extract rate
140+
h:\s+width\s+(?P<mode_width>[0-9]+).+\s+
141+
v:\s+height\s+(?P<mode_height>[0-9]+).+clock\s+(?P<rate>[0-9\.]+)Hz\s* |
142+
\S+(?:(?!\*current).)+\s+h:.+\s+v:.+\s* # Other resolutions
142143
)*)
143144
"""
144145

145146
XRANDR_OUTPUT_MODES_REGEXP = """(?x)
146-
(?P<width>[0-9]+)x(?P<height>[0-9]+)
147-
.*?(?P<preferred>\+preferred)?
148-
\s+h:.+
149-
\s+v:.+clock\s+(?P<rate>[0-9\.]+)Hz
147+
(?P<name>\S+).+?(?P<preferred>\+preferred)?\s+
148+
h:\s+width\s+(?P<width>[0-9]+).+\s+
149+
v:\s+height\s+(?P<height>[0-9]+).+clock\s+(?P<rate>[0-9\.]+)Hz\s* |
150150
"""
151151

152152
XRANDR_13_DEFAULTS = {
@@ -242,7 +242,7 @@ def from_xrandr_output(cls, xrandr_output):
242242

243243
modes = []
244244
if match["modes"]:
245-
modes = [ x.groupdict() for x in re.finditer(XrandrOutput.XRANDR_OUTPUT_MODES_REGEXP, match["modes"]) ]
245+
modes = [ x.groupdict() for x in re.finditer(XrandrOutput.XRANDR_OUTPUT_MODES_REGEXP, match["modes"]) if x.group("name") ]
246246
if not modes:
247247
raise AutorandrException("Parsing XRandR output failed, couldn't find any display modes", report_bug=True)
248248

@@ -255,7 +255,9 @@ def from_xrandr_output(cls, xrandr_output):
255255
if not match["width"]:
256256
options["off"] = None
257257
else:
258-
if match["mode_width"]:
258+
if match["mode_name"]:
259+
options["mode"] = match["mode_name"]
260+
elif match["mode_width"]:
259261
options["mode"] = "%sx%s" % (match["mode_width"], match["mode_height"])
260262
else:
261263
if match["rotate"] not in ("left", "right"):
@@ -283,7 +285,7 @@ def from_xrandr_output(cls, xrandr_output):
283285
transformation = ",".join(match["transform"].strip().split())
284286
if transformation != "1.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,1.000000":
285287
options["transform"] = transformation
286-
if not match["mode_width"]:
288+
if not match["mode_name"]:
287289
# TODO We'd need to apply the reverse transformation here. Let's see if someone complains, I doubt that this
288290
# special case is actually required.
289291
print("Warning: Output %s has a transformation applied. Could not determine correct mode! Using `%s'." % (match["output"], options["mode"]), file=sys.stderr)
@@ -562,7 +564,7 @@ def generate_virtual_profile(configuration, modes, profile_name):
562564
for output in configuration:
563565
configuration[output].options = {}
564566
if output in modes:
565-
configuration[output].options["mode"] = "%sx%s" % common_resolution[-1]
567+
configuration[output].options["mode"] = [ x["name"] for x in sorted(modes[output], key=lambda x: 0 if x["preferred"] else 1) if x["width"] == common_resolution[-1][0] and x["height"] == common_resolution[-1][1] ][0]
566568
configuration[output].options["pos"] = "0x0"
567569
else:
568570
configuration[output].options["off"] = None
@@ -579,7 +581,7 @@ def generate_virtual_profile(configuration, modes, profile_name):
579581
configuration[output].options = {}
580582
if output in modes:
581583
mode = sorted(modes[output], key=lambda a: int(a["width"])*int(a["height"]) + (10**6 if a["preferred"] else 0))[-1]
582-
configuration[output].options["mode"] = "%sx%s" % (mode["width"], mode["height"])
584+
configuration[output].options["mode"] = mode["name"]
583585
configuration[output].options["rate"] = mode["rate"]
584586
configuration[output].options["pos"] = pos_specifier % shift
585587
shift += int(mode[shift_index])

0 commit comments

Comments
 (0)