From e1c85639c3dbcfe86a7003bc916efafe629bcbf9 Mon Sep 17 00:00:00 2001
From: Phillip Berndt <phillip.berndt@googlemail.com>
Date: Tue, 31 Dec 2019 11:41:27 +0100
Subject: [PATCH] Prefer X11 launcher (like srandr) over udev/systemd setup

See #162. The X11 launcher, that waits for randr events in X11 and runs
autorandr manually, proved to be more reliable than the setup using udev
and systemd. Make it the default.
---
 Makefile                                      | 26 ++++++++++++++-----
 README.md                                     |  1 +
 .../xdg/autostart/autorandr-launcher.desktop  |  6 +++++
 3 files changed, 26 insertions(+), 7 deletions(-)
 create mode 100644 contrib/etc/xdg/autostart/autorandr-launcher.desktop

diff --git a/Makefile b/Makefile
index d8b27c6..a4a70e4 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,7 @@
 DESTDIR=/
 PREFIX=/usr/
 RPM_SPEC=contrib/packaging/rpm/autorandr.spec
+CFLAGS=-O2
 
 .PHONY: all install uninstall autorandr bash_completion autostart_config pmutils systemd udev
 
@@ -23,10 +24,9 @@ all:
 	@echo
 	@echo 'E.g. "make install TARGETS='autorandr pmutils' PM_UTILS_DIR=/etc/pm/sleep.d".'
 	@echo
-	@echo "An additional TARGETS variable \"launcher\" is available. This"
-	@echo "installs a launcher called \"autorandr_launcher\". The launcher"
-	@echo "is able to be run by the user and calls autorandr automatically"
-	@echo "without using udev rules."
+	@echo "By default, if xcb libraries are available, autorandr prefers to"
+	@echo "install a launcher that listens for X11 randr events and runs"
+	@echo "autorandr whenever something changes, over udev/systemd rules."
 	@echo
 	@echo "The following additional targets are available:"
 	@echo
@@ -139,12 +139,24 @@ uninstall_manpage:
 	rm -f ${DESTDIR}/${MANDIR}/autorandr.1
 
 # Rules for launcher
+LAUNCHER_FLAGS=$(shell pkg-config --libs --cflags pkg-config xcb xcb-randr 2>/dev/null)
+ifneq (,$(LAUNCHER_FLAGS))
+DEFAULT_TARGETS+=launcher
+DEFAULT_TARGETS:=$(filter-out systemd udev,$(DEFAULT_TARGETS))
+endif
+
 install_launcher:
-	gcc -Wall contrib/autorandr_launcher/autorandr_launcher.c -o contrib/autorandr_launcher/autorandr_launcher -lxcb -lxcb-randr
-	install -D -m 755 contrib/autorandr_launcher/autorandr_launcher ${DESTDIR}${PREFIX}/bin/autorandr_launcher
+	gcc -Wall $(CFLAGS) contrib/autorandr_launcher/autorandr_launcher.c -o contrib/autorandr_launcher/autorandr-launcher $(LAUNCHER_FLAGS)
+	install -D -m 755 contrib/autorandr_launcher/autorandr_launcher ${DESTDIR}${PREFIX}/bin/autorandr-launcher
+
+	install -D -m 644 contrib/etc/xdg/autostart/autorandr-launcher.desktop ${DESTDIR}/${XDG_AUTOSTART_DIR}/autorandr-launcher.desktop
+ifneq ($(PREFIX),/usr/)
+	sed -i -re 's#/usr/bin/autorandr-launcher#$(subst #,\#,${PREFIX})/bin/autorandr-launcher#g' ${DESTDIR}/${XDG_AUTOSTART_DIR}/autorandr-launcher.desktop
+endif
 
 uninstall_launcher:
-	rm -f ${DESTDIR}${PREFIX}/bin/autorandr_launcher
+	rm -f ${DESTDIR}${PREFIX}/bin/autorandr-launcher
+	rm -f ${DESTDIR}/${XDG_AUTOSTART_DIR}/autorandr-launcher.desktop
 
 TARGETS=$(DEFAULT_TARGETS)
 install: $(patsubst %,install_%,$(TARGETS))
diff --git a/README.md b/README.md
index 145074b..ad32de4 100644
--- a/README.md
+++ b/README.md
@@ -217,6 +217,7 @@ profiles matching multiple (or any) monitors.
 **autorandr 1.10 (dev)**
 * *2019-12-31* Fix output positioning if the top-left output is not the first
 * *2019-12-31* Accept negative gamma values (and interpret them as 0)
+* *2019-12-31* Prefer the X11 launcher over systemd/udev configuration
 
 **autorandr 1.9**
 
diff --git a/contrib/etc/xdg/autostart/autorandr-launcher.desktop b/contrib/etc/xdg/autostart/autorandr-launcher.desktop
new file mode 100644
index 0000000..1a5bff0
--- /dev/null
+++ b/contrib/etc/xdg/autostart/autorandr-launcher.desktop
@@ -0,0 +1,6 @@
+[Desktop Entry]
+Name=Autorandr Launcher
+Comment=Automatically run autorandr whenever the configuration changes
+Type=Application
+Exec=/usr/bin/autorandr-launcher
+X-GNOME-Autostart-Phase=Initialization
-- 
2.39.5