Skip to content

Commit 14df4cb

Browse files
committed
Remember the last choice for competing configurations (with equal setups)
fixes wertarbyte#16
1 parent 586dce7 commit 14df4cb

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

autorandr.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -364,24 +364,27 @@ def load_profiles(profile_path):
364364
if config[output_name].edid is None:
365365
del config[output_name]
366366

367-
profiles[profile] = config
367+
profiles[profile] = { "config": config, "path": os.path.join(profile_path, profile), "config-mtime": os.stat(config_name).st_mtime }
368368

369369
return profiles
370370

371-
def find_profile(current_config, profiles):
372-
"Find a profile matching the currently connected outputs"
371+
def find_profiles(current_config, profiles):
372+
"Find profiles matching the currently connected outputs"
373+
detected_profiles = []
373374
for profile_name, profile in profiles.items():
375+
config = profile["config"]
374376
matches = True
375-
for name, output in profile.items():
377+
for name, output in config.items():
376378
if not output.edid:
377379
continue
378380
if name not in current_config or not output.edid_equals(current_config[name]):
379381
matches = False
380382
break
381-
if not matches or any(( name not in profile.keys() for name in current_config.keys() if current_config[name].edid )):
383+
if not matches or any(( name not in config.keys() for name in current_config.keys() if current_config[name].edid )):
382384
continue
383385
if matches:
384-
return profile_name
386+
detected_profiles.append(profile_name)
387+
return detected_profiles
385388

386389
def profile_blocked(profile_path):
387390
"Check if a profile is blocked"
@@ -412,6 +415,14 @@ def save_configuration(profile_path, configuration):
412415
with open(os.path.join(profile_path, "setup"), "w") as setup:
413416
output_setup(configuration, setup)
414417

418+
def update_mtime(filename):
419+
"Update a file's mtime"
420+
try:
421+
os.utime(filename, None)
422+
return True
423+
except:
424+
return False
425+
415426
def apply_configuration(configuration, dry_run=False):
416427
"Apply a configuration"
417428
outputs = sorted(configuration.keys(), key=lambda x: configuration[x].sort_key)
@@ -523,6 +534,8 @@ def main(argv):
523534
profile_path = os.path.join(os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config")), "autorandr")
524535
if os.path.isdir(profile_path):
525536
profiles.update(load_profiles(profile_path))
537+
# Sort by descending mtime
538+
profiles = OrderedDict(sorted(profiles.items(), key=lambda x: -x[1]["config-mtime"]))
526539
except Exception as e:
527540
print("Failed to load profiles:\n%s" % str(e), file=sys.stderr)
528541
sys.exit(1)
@@ -558,7 +571,7 @@ def main(argv):
558571
if "-h" in options or "--help" in options:
559572
exit_help()
560573

561-
detected_profile = find_profile(config, profiles)
574+
detected_profiles = find_profiles(config, profiles)
562575
load_profile = False
563576

564577
if "-l" in options:
@@ -570,10 +583,10 @@ def main(argv):
570583
if profile_blocked(os.path.join(profile_path, profile_name)):
571584
print("%s (blocked)" % profile_name, file=sys.stderr)
572585
continue
573-
if detected_profile == profile_name:
586+
if profile_name in detected_profiles:
574587
print("%s (detected)" % profile_name, file=sys.stderr)
575-
if "-c" in options or "--change" in options:
576-
load_profile = detected_profile
588+
if ("-c" in options or "--change" in options) and not load_profile:
589+
load_profile = profile_name
577590
else:
578591
print(profile_name, file=sys.stderr)
579592

@@ -584,25 +597,30 @@ def main(argv):
584597

585598
if load_profile:
586599
if load_profile in ( x[0] for x in virtual_profiles ):
587-
profile = generate_virtual_profile(config, modes, load_profile)
600+
load_config = generate_virtual_profile(config, modes, load_profile)
601+
scripts_path = os.path.join(profile_path, load_profile)
588602
else:
589603
try:
590604
profile = profiles[load_profile]
605+
load_config = profile["config"]
606+
scripts_path = profile["path"]
591607
except KeyError:
592608
print("Failed to load profile '%s':\nProfile not found" % load_profile, file=sys.stderr)
593609
sys.exit(1)
594-
add_unused_outputs(config, profile)
595-
if profile == dict(config) and not "-f" in options and not "--force" in options:
610+
if load_profile in detected_profiles and detected_profiles[0] != load_profile:
611+
update_mtime(os.path.join(scripts_path, "config"))
612+
add_unused_outputs(config, load_config)
613+
if load_config == dict(config) and not "-f" in options and not "--force" in options:
596614
print("Config already loaded", file=sys.stderr)
597615
sys.exit(0)
598616

599617
try:
600618
if "--dry-run" in options:
601-
apply_configuration(profile, True)
619+
apply_configuration(load_config, True)
602620
else:
603-
exec_scripts(os.path.join(profile_path, load_profile), "preswitch")
604-
apply_configuration(profile, False)
605-
exec_scripts(os.path.join(profile_path, load_profile), "postswitch")
621+
exec_scripts(scripts_path, "preswitch")
622+
apply_configuration(load_config, False)
623+
exec_scripts(scripts_path, "postswitch")
606624
except Exception as e:
607625
print("Failed to apply profile '%s':\n%s" % (load_profile, str(e)), file=sys.stderr)
608626
sys.exit(1)

0 commit comments

Comments
 (0)