]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/commitdiff
Call autorandr via systemd from udev, remove pmutils script on systemd systems
authorPhillip Berndt <phillip.berndt@googlemail.com>
Fri, 20 Jan 2017 20:41:06 +0000 (21:41 +0100)
committerGitHub <noreply@github.com>
Fri, 20 Jan 2017 20:41:06 +0000 (21:41 +0100)
This merges pull request #62.

Makefile
README.md
autorandr.py
contrib/packaging/debian/make_deb.sh
contrib/pm-utils/40autorandr
contrib/systemd/autorandr-resume.service [deleted file]
contrib/systemd/autorandr.service [new file with mode: 0644]
contrib/udev/40-monitor-hotplug.rules [deleted file]

index 5c5977ae0eeda327eda29e7ea7d9425a1a297960..7125730d3bfe285ce6d70760e4d0bc3928ec8bea 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -53,37 +53,54 @@ DEFAULT_TARGETS+=autostart_config
 
 install_autostart_config:
        install -D -m 644 contrib/etc/xdg/autostart/autorandr.desktop ${DESTDIR}/${XDG_AUTOSTART_DIR}/autorandr.desktop
+ifneq ($(PREFIX),/usr/)
+       sed -i -re 's#/usr/bin/autorandr#$(subst #,\#,${PREFIX})/bin/autorandr#g' ${DESTDIR}/${XDG_AUTOSTART_DIR}/autorandr.desktop
+endif
 
 uninstall_autostart_config:
        rm -f ${DESTDIR}/${XDG_AUTOSTART_DIR}/autorandr.desktop
 
+# Rules for systemd
+SYSTEMD_UNIT_DIR:=$(shell pkg-config --variable=systemdsystemunitdir systemd 2>/dev/null)
+ifneq (,$(SYSTEMD_UNIT_DIR))
+DEFAULT_TARGETS+=systemd
+endif
+
+install_systemd:
+       $(if $(SYSTEMD_UNIT_DIR),,$(error SYSTEMD_UNIT_DIR is not defined))
+       install -D -m 644 contrib/systemd/autorandr.service ${DESTDIR}/${SYSTEMD_UNIT_DIR}/autorandr.service
+ifneq ($(PREFIX),/usr/)
+       sed -i -re 's#/usr/bin/autorandr#$(subst #,\#,${PREFIX})/bin/autorandr#g' ${DESTDIR}/${SYSTEMD_UNIT_DIR}/autorandr.service
+endif
+       @echo
+       @echo "To activate the systemd unit, run this command as root:"
+       @echo "    systemctl daemon-reload"
+       @echo "    systemctl enable autorandr.service"
+       @echo
+
+uninstall_systemd:
+       $(if $(SYSTEMD_UNIT_DIR),,$(error SYSTEMD_UNIT_DIR is not defined))
+       rm -f ${DESTDIR}/${SYSTEMD_UNIT_DIR}/autorandr.service
+
 # Rules for pmutils
 PM_SLEEPHOOKS_DIR:=$(shell pkg-config --variable=pm_sleephooks pm-utils 2>/dev/null)
 ifneq (,$(PM_SLEEPHOOKS_DIR))
+ifeq (,$(SYSTEMD_UNIT_DIR))
 DEFAULT_TARGETS+=pmutils
 endif
+endif
 
 install_pmutils:
        $(if $(PM_SLEEPHOOKS_DIR),,$(error PM_SLEEPHOOKS_DIR is not defined))
        install -D -m 755 contrib/pm-utils/40autorandr ${DESTDIR}/${PM_SLEEPHOOKS_DIR}/40autorandr
+ifneq ($(PREFIX),/usr/)
+       sed -i -re 's#/usr/bin/autorandr#$(subst #,\#,${PREFIX})/bin/autorandr#g' ${DESTDIR}/${PM_SLEEPHOOKS_DIR}/40autorandr
+endif
 
 uninstall_pmutils:
        $(if $(PM_SLEEPHOOKS_DIR),,$(error PM_SLEEPHOOKS_DIR is not defined))
        rm -f ${DESTDIR}/${PM_SLEEPHOOKS_DIR}/40autorandr
 
-# Rules for systemd
-SYSTEMD_UNIT_DIR:=$(shell pkg-config --variable=systemdsystemunitdir systemd 2>/dev/null)
-ifneq (,$(SYSTEMD_UNIT_DIR))
-DEFAULT_TARGETS+=systemd
-endif
-
-install_systemd:
-       $(if $(SYSTEMD_UNIT_DIR),,$(error SYSTEMD_UNIT_DIR is not defined))
-       install -D -m 644 contrib/systemd/autorandr-resume.service ${DESTDIR}/${SYSTEMD_UNIT_DIR}/autorandr-resume.service
-
-uninstall_systemd:
-       $(if $(SYSTEMD_UNIT_DIR),,$(error SYSTEMD_UNIT_DIR is not defined))
-       rm -f ${DESTDIR}/${SYSTEMD_UNIT_DIR}/autorandr-resume.service
 
 # Rules for udev
 UDEV_RULES_DIR:=$(shell pkg-config --variable=udevdir udev 2>/dev/null)/rules.d
@@ -93,13 +110,12 @@ endif
 
 install_udev:
        $(if $(UDEV_RULES_DIR),,$(error UDEV_RULES_DIR is not defined))
-       install -D -m 644 contrib/udev/40-monitor-hotplug.rules ${DESTDIR}/${UDEV_RULES_DIR}/40-monitor-hotplug.rules
-ifeq (${USER},root)
-       udevadm control --reload-rules
-else
-       @echo "Please run this command as root:"
+       mkdir -p ${DESTDIR}/${UDEV_RULES_DIR}/
+       echo 'ACTION=="change", SUBSYSTEM=="drm", RUN+="$(if $(findstring systemd, $(TARGETS)),/bin/systemctl start autorandr.service,${PREFIX}/bin/autorandr --batch --change --default default)"' > ${DESTDIR}/${UDEV_RULES_DIR}/40-monitor-hotplug.rules
+       @echo
+       @echo "To activate the udev rules, run this command as root:"
        @echo "    udevadm control --reload-rules"
-endif
+       @echo
 
 uninstall_udev:
        $(if $(UDEV_RULES_DIR),,$(error UDEV_RULES_DIR is not defined))
index b20d47e2f7e2e10eeeef6b0369d4b968870c2693..d08b78c4222cc4337ae24cb671ae358b13000eb3 100644 (file)
--- a/README.md
+++ b/README.md
@@ -122,6 +122,11 @@ about it. The same holds for `preswitch`, which is executed before the switch
 takes place, and `postsave`, which is executed after a profile was
 stored/altered.
 
+If you experience issues with xrandr being executed too early after connecting
+a new monitor, then you can create a script `predetect`, which will be executed
+before autorandr attempts to run xrandr. Place e.g. `sleep 1` into that file
+to make autorandr wait a second before running xrandr.
+
 All scripts can also be placed in any of the `$XDG_CONFIG_DIRS`. In addition to
 the script names themselves, any executables in subdirectories named
 `script_name.d` (e.g. `postswitch.d`) are executed as well. In scripts, some of
@@ -131,6 +136,7 @@ The most useful one is `$AUTORANDR_CURRENT_PROFILE`.
 ## Changelog
 
 * *2017-01-18* Accept comments (lines starting with `#`) in config/setup files
+* *2017-01-20* New script hook, `predetect`
 
 **autorandr 1.0**
 
index a47282fece8e24eefeea1dc01c1f90f971b1c0b3..ad4b22b0fbeec18e7e89695d4c9a481f76cc74a9 100755 (executable)
@@ -744,6 +744,8 @@ def exec_scripts(profile_path, script_name, meta_information=None):
     and system-wide configuration folders, named script_name or residing in
     subdirectories named script_name.d.
 
+    If profile_path is None, only global scripts will be invoked.
+
     meta_information is expected to be an dictionary. It will be passed to the block scripts
     in the environment, as variables called AUTORANDR_<CAPITALIZED_KEY_HERE>.
 
@@ -763,8 +765,11 @@ def exec_scripts(profile_path, script_name, meta_information=None):
     if not os.path.isdir(user_profile_path):
         user_profile_path = os.path.join(os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config")), "autorandr")
 
-    for folder in chain((profile_path, os.path.dirname(profile_path), user_profile_path),
-                        (os.path.join(x, "autorandr") for x in os.environ.get("XDG_CONFIG_DIRS", "/etc/xdg").split(":"))):
+    candidate_directories = chain((user_profile_path,), (os.path.join(x, "autorandr") for x in os.environ.get("XDG_CONFIG_DIRS", "/etc/xdg").split(":")))
+    if profile_path:
+        candidate_directories = chain((profile_path,), candidate_directories)
+
+    for folder in candidate_directories:
 
         if script_name not in ran_scripts:
             script = os.path.join(folder, script_name)
@@ -838,6 +843,9 @@ def dispatch_call_to_sessions(argv):
                 process_environ[name] = value
         display = process_environ["DISPLAY"] if "DISPLAY" in process_environ else None
 
+        # To allow scripts to detect batch invocation (especially useful for predetect)
+        process_environ["AUTORANDR_BATCH_PID"] = os.getpid()
+
         if display and display not in X11_displays_done:
             try:
                 pwent = pwd.getpwuid(uid)
@@ -873,6 +881,9 @@ def main(argv):
               file=sys.stderr)
         sys.exit(posix.EX_USAGE)
 
+    if "-h" in options or "--help" in options:
+        exit_help()
+
     # Batch mode
     if "--batch" in options:
         if ("DISPLAY" not in os.environ or not os.environ["DISPLAY"]) and os.getuid() == 0:
@@ -907,6 +918,7 @@ def main(argv):
 
     profile_symlinks = { k: v for k, v in profile_symlinks.items() if v in (x[0] for x in virtual_profiles) or v in profiles }
 
+    exec_scripts(None, "predetect")
     config, modes = parse_xrandr_output()
 
     if "--fingerprint" in options:
@@ -966,9 +978,6 @@ def main(argv):
             raise AutorandrException("Failed to remove profile '%s'" % (options["--remove"],), e)
         sys.exit(0)
 
-    if "-h" in options or "--help" in options:
-        exit_help()
-
     detected_profiles = find_profiles(config, profiles)
     load_profile = False
 
index dddc08fa69d706d27cfc6752786a22015fd16fd0..f049a108fcd4c815fd0f20e93fbbf1df38598c81 100755 (executable)
@@ -34,7 +34,7 @@ mkdir $D
 # Debian(ish) specific part
 make -C "$P/../../../" \
        DESTDIR="$D" \
-       TARGETS="autorandr bash_completion autostart_config pmutils systemd udev" \
+       TARGETS="autorandr bash_completion autostart_config systemd udev" \
        BASH_COMPLETION_DIR=/usr/share/bash-completion/completions \
        SYSTEMD_UNIT_DIR=/lib/systemd/system \
        PM_UTILS_DIR=/usr/lib/pm-utils/sleep.d \
index a80926091539b6bb9c4cfe768f074e697ddd363f..d641957142be6ce0bd7a71d3a8d9ef10e0a68bb6 100755 (executable)
@@ -5,6 +5,6 @@ exec > /var/log/autorandr.log 2>&1
 
 case "$1" in
        thaw|resume)
-               autorandr --batch -c --default default
+               /usr/bin/autorandr --batch --change --default default
                ;;
 esac
diff --git a/contrib/systemd/autorandr-resume.service b/contrib/systemd/autorandr-resume.service
deleted file mode 100644 (file)
index 871fad5..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-[Unit]
-Description=autorandr resume hook
-After=sleep.target
-
-[Service]
-ExecStart=/usr/bin/autorandr --batch -c --default default
-
-[Install]
-WantedBy=sleep.target
diff --git a/contrib/systemd/autorandr.service b/contrib/systemd/autorandr.service
new file mode 100644 (file)
index 0000000..74a78de
--- /dev/null
@@ -0,0 +1,13 @@
+[Unit]
+Description=autorandr execution hook
+After=sleep.target
+StartLimitInterval=5
+StartLimitBurst=1
+
+[Service]
+ExecStart=/usr/bin/autorandr --batch --change --default default
+Type=oneshot
+RemainAfterExit=false
+
+[Install]
+WantedBy=sleep.target
diff --git a/contrib/udev/40-monitor-hotplug.rules b/contrib/udev/40-monitor-hotplug.rules
deleted file mode 100644 (file)
index cbea19b..0000000
+++ /dev/null
@@ -1 +0,0 @@
-ACTION=="change", SUBSYSTEM=="drm", RUN+="/usr/bin/autorandr --batch -c --default default"