]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blobdiff - autorandr.py
Add a configuration file for default option values
[deb_pkgs/autorandr.git] / autorandr.py
index fbc1445bf09e015e2214a7a4c4d53f438fa54a16..9c272a79f05b8b199ae43580fe212bd3a569b7fd 100755 (executable)
@@ -42,6 +42,11 @@ from distutils.version import LooseVersion as Version
 from functools import reduce
 from itertools import chain
 
 from functools import reduce
 from itertools import chain
 
+if sys.version_info.major == 2:
+    import ConfigParser as configparser
+else:
+    import configparser
+
 try:
     input = raw_input
 except NameError:
 try:
     input = raw_input
 except NameError:
@@ -49,6 +54,7 @@ except NameError:
 
 virtual_profiles = [
     # (name, description, callback)
 
 virtual_profiles = [
     # (name, description, callback)
+    ("off", "Disable all outputs", None),
     ("common", "Clone all connected outputs at the largest common resolution", None),
     ("clone-largest", "Clone all connected outputs with the largest resolution (scaled down if necessary)", None),
     ("horizontal", "Stack all connected outputs horizontally at their largest resolution", None),
     ("common", "Clone all connected outputs at the largest common resolution", None),
     ("clone-largest", "Clone all connected outputs with the largest resolution (scaled down if necessary)", None),
     ("horizontal", "Stack all connected outputs horizontally at their largest resolution", None),
@@ -60,32 +66,25 @@ Usage: autorandr [options]
 
 -h, --help              get this small help
 -c, --change            reload current setup
 
 -h, --help              get this small help
 -c, --change            reload current setup
+-d, --default <profile> make profile <profile> the default profile
+-l, --load <profile>    load profile <profile>
 -s, --save <profile>    save your current setup to profile <profile>
 -r, --remove <profile>  remove profile <profile>
 -s, --save <profile>    save your current setup to profile <profile>
 -r, --remove <profile>  remove profile <profile>
--l, --load <profile>    load profile <profile>
--d, --default <profile> make profile <profile> the default profile
---skip-options <option> comma separated list of xrandr arguments (e.g. "gamma")
-                        to skip both in detecting changes and applying a profile
---force                 force (re)loading of a profile
---fingerprint           fingerprint your current hardware setup
+--batch                 run autorandr for all users with active X11 sessions
 --config                dump your current xrandr setup
 --config                dump your current xrandr setup
---dry-run               don't change anything, only print the xrandr commands
 --debug                 enable verbose output
 --debug                 enable verbose output
---batch                 run autorandr for all users with active X11 sessions
-
- To prevent a profile from being loaded, place a script called "block" in its
- directory. The script is evaluated before the screen setup is inspected, and
- in case of it returning a value of 0 the profile is skipped. This can be used
- to query the status of a docking station you are about to leave.
+--dry-run               don't change anything, only print the xrandr commands
+--fingerprint           fingerprint your current hardware setup
+--force                 force (re)loading of a profile
+--skip-options <option> comma separated list of xrandr arguments (e.g. "gamma")
+                        to skip both in detecting changes and applying a profile
 
  If no suitable profile can be identified, the current configuration is kept.
  To change this behaviour and switch to a fallback configuration, specify
  --default <profile>.
 
 
  If no suitable profile can be identified, the current configuration is kept.
  To change this behaviour and switch to a fallback configuration, specify
  --default <profile>.
 
- Another script called "postswitch" can be placed in the directory
- ~/.config/autorandr (or ~/.autorandr if you have an old installation) as well
- as in any profile directories: The scripts are executed after a mode switch
- has taken place and can notify window managers.
+ autorandr supports a set of per-profile and global hooks. See the documentation
+ for details.
 
  The following virtual configurations are available:
 """.strip()
 
  The following virtual configurations are available:
 """.strip()
@@ -663,9 +662,9 @@ def apply_configuration(new_configuration, current_configuration, dry_run=False)
 
             option_vector = new_configuration[output].option_vector
             if xrandr_version() >= Version("1.3.0"):
 
             option_vector = new_configuration[output].option_vector
             if xrandr_version() >= Version("1.3.0"):
-                for option in ("transform", "panning"):
+                for option, off_value in (("transform", "none"), ("panning", "0x0")):
                     if option in current_configuration[output].options:
                     if option in current_configuration[output].options:
-                        auxiliary_changes_pre.append(["--output", output, "--%s" % option, "none"])
+                        auxiliary_changes_pre.append(["--output", output, "--%s" % option, off_value])
                     else:
                         try:
                             option_index = option_vector.index("--%s" % option)
                     else:
                         try:
                             option_index = option_vector.index("--%s" % option)
@@ -806,6 +805,11 @@ def generate_virtual_profile(configuration, modes, profile_name):
                 configuration[output].options["transform"] = "{},0,{},0,{},{},0,0,1".format(scale, mov_x, scale, mov_y)
             else:
                 configuration[output].options["off"] = None
                 configuration[output].options["transform"] = "{},0,{},0,{},{},0,0,1".format(scale, mov_x, scale, mov_y)
             else:
                 configuration[output].options["off"] = None
+    elif profile_name == "off":
+        for output in configuration:
+            for key in list(configuration[output].options.keys()):
+                del configuration[output].options[key]
+            configuration[output].options["off"] = None
     return configuration
 
 
     return configuration
 
 
@@ -872,12 +876,11 @@ def exec_scripts(profile_path, script_name, meta_information=None):
 
     candidate_directories = [user_profile_path]
     for config_dir in os.environ.get("XDG_CONFIG_DIRS", "/etc/xdg").split(":"):
 
     candidate_directories = [user_profile_path]
     for config_dir in os.environ.get("XDG_CONFIG_DIRS", "/etc/xdg").split(":"):
-        candidate_directories += os.path.join(config_dir, "autorandr")
+        candidate_directories.append(os.path.join(config_dir, "autorandr"))
     if profile_path:
     if profile_path:
-        candidate_directories += profile_path
+        candidate_directories.append(profile_path)
 
     for folder in candidate_directories:
 
     for folder in candidate_directories:
-
         if script_name not in ran_scripts:
             script = os.path.join(folder, script_name)
             if os.access(script, os.X_OK | os.F_OK):
         if script_name not in ran_scripts:
             script = os.path.join(folder, script_name)
             if os.access(script, os.X_OK | os.F_OK):
@@ -964,8 +967,8 @@ def dispatch_call_to_sessions(argv):
 
         process_environ = {}
         for environ_entry in open(environ_file).read().split("\0"):
 
         process_environ = {}
         for environ_entry in open(environ_file).read().split("\0"):
-            if "=" in environ_entry:
-                name, value = environ_entry.split("=", 1)
+            name, sep, value = environ_entry.partition("=")
+            if name and sep:
                 if name == "DISPLAY" and "." in value:
                     value = value[:value.find(".")]
                 process_environ[name] = value
                 if name == "DISPLAY" and "." in value:
                     value = value[:value.find(".")]
                 process_environ[name] = value
@@ -1010,6 +1013,14 @@ def dispatch_call_to_sessions(argv):
             X11_displays_done.add(display)
 
 
             X11_displays_done.add(display)
 
 
+def read_config(options, directory):
+    """Parse a configuration config.ini from directory and merge it into
+    the options dictionary"""
+    config = configparser.ConfigParser()
+    config.read(os.path.join(directory, "settings.ini"))
+    for key, value in config.items("config"):
+        options.setdefault("--%s" % key, value)
+
 def main(argv):
     try:
         opts, args = getopt.getopt(argv[1:], "s:r:l:d:cfh",
 def main(argv):
     try:
         opts, args = getopt.getopt(argv[1:], "s:r:l:d:cfh",
@@ -1048,6 +1059,7 @@ def main(argv):
             if os.path.isdir(system_profile_path):
                 profiles.update(load_profiles(system_profile_path))
                 profile_symlinks.update(get_symlinks(system_profile_path))
             if os.path.isdir(system_profile_path):
                 profiles.update(load_profiles(system_profile_path))
                 profile_symlinks.update(get_symlinks(system_profile_path))
+                read_config(options, system_profile_path)
         # For the user's profiles, prefer the legacy ~/.autorandr if it already exists
         # profile_path is also used later on to store configurations
         profile_path = os.path.expanduser("~/.autorandr")
         # For the user's profiles, prefer the legacy ~/.autorandr if it already exists
         # profile_path is also used later on to store configurations
         profile_path = os.path.expanduser("~/.autorandr")
@@ -1057,6 +1069,7 @@ def main(argv):
         if os.path.isdir(profile_path):
             profiles.update(load_profiles(profile_path))
             profile_symlinks.update(get_symlinks(profile_path))
         if os.path.isdir(profile_path):
             profiles.update(load_profiles(profile_path))
             profile_symlinks.update(get_symlinks(profile_path))
+            read_config(options, profile_path)
         # Sort by descending mtime
         profiles = OrderedDict(sorted(profiles.items(), key=lambda x: -x[1]["config-mtime"]))
     except Exception as e:
         # Sort by descending mtime
         profiles = OrderedDict(sorted(profiles.items(), key=lambda x: -x[1]["config-mtime"]))
     except Exception as e: