Call autorandr via systemd from udev, remove pmutils script on systemd systems

This merges pull request #62.
This commit is contained in:
Phillip Berndt 2017-01-20 21:41:06 +01:00 committed by GitHub
commit 20fe858e08
8 changed files with 73 additions and 39 deletions

View File

@ -53,24 +53,13 @@ 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 pmutils
PM_SLEEPHOOKS_DIR:=$(shell pkg-config --variable=pm_sleephooks pm-utils 2>/dev/null)
ifneq (,$(PM_SLEEPHOOKS_DIR))
DEFAULT_TARGETS+=pmutils
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
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))
@ -79,11 +68,39 @@ 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
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-resume.service
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 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))

View File

@ -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**

View File

@ -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

View File

@ -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 \

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -1 +0,0 @@
ACTION=="change", SUBSYSTEM=="drm", RUN+="/usr/bin/autorandr --batch -c --default default"