xhotplug

Run arbirary commands in response to newly connected X11 displays
git clone git://git.danielmoch.com/xhotplug.git
Log | Files | Refs | README | LICENSE

commit 0fca77bed49ee266fe23422a7f6d0275456f180c
parent 155443e5ac5207ea289a7525c1650bb33fc84587
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Sat,  4 Jul 2020 12:03:32 -0400

Add documentation

Diffstat:
ALICENSE | 17+++++++++++++++++
MMakefile | 21++++++++++++++++++---
AREADME | 32++++++++++++++++++++++++++++++++
Mconfig.mk | 4+++-
Axhotplug.1 | 121+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 191 insertions(+), 4 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,17 @@ +ISC License (ISC) + +Copyright 2020 Daniel Moch + +Permission to use, copy, modify, and/or distribute this software +for any purpose with or without fee is hereby granted, provided +that the above copyright notice and this permission notice appear +in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL +DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA +OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER +TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. diff --git a/Makefile b/Makefile @@ -1,3 +1,6 @@ +# See LICENSE file for copyright and license details +.POSIX: + include config.mk all: xhotplug @@ -10,11 +13,23 @@ xhotplug: ${OBJ} install: xhotplug install -Dm755 xhotplug ${DESTDIR}${PREFIX}/bin/xhotplug + install -Dm644 xhotplug.1 ${DESTDIR}${MANPATH}/man1/xhotplug.1 uninstall: - -rm -f ${DESTDIR}${PREFIX}/bin/xhotplug + rm -f ${DESTDIR}${PREFIX}/bin/xhotplug + rm -f ${DESTDIR}${MANPATH}/man1/xhotplug.1 clean: - -rm *.o xhotplug + rm *.o xhotplug + +dist: + rm -rf xhotplug-${VERSION} + mkdir xhotplug-${VERSION} + cp ${DIST_SRC} xhotplug-${VERSION} + tar -cf xhotplug-${VERSION} | gzip > xhotplug-${VERSION}.tar.gz + +distclean: + rm -rf xhotplug-${VERSION} + rm -f xhotplug-${VERSION}.tar.gz -.PHONY: all clean install uninstall +.PHONY: all clean install uninstall dist diff --git a/README b/README @@ -0,0 +1,32 @@ +xhotplug +======== + +X11 monitor attach/detach event responder + +Installation +------------ + +$ make +# make install + +Usage +----- + +There are several ways to make use of xhotplug. The easiest is to +add the following line to $HOME/.xession (or $HOME/.xinitrc if you +use xinit): + + xhotplug script & + +Many desktop environments provide a way to add startup programs, +although they probably also detect new displays on their own. + +Documentation +------------- + +See xhotplug(1) + +License Terms +------------- + +See the LICENSE file in this repository diff --git a/config.mk b/config.mk @@ -3,10 +3,12 @@ MANPATH := ${PREFIX}/share/man X11BASE := /usr/X11R6 SRC = xhotplug.c +DIST_SRC = ${SRC} Makefile config.mk xhotplug.1 OBJ = ${SRC:.c=.o} INCS = -I${X11BASE}/include LIBS = -L${X11BASE}/lib -lxcb -lxcb-randr +VERSION = 0.1.0dev0 -CPPFLAGS := -DVERSION=\"0.1.0dev0\" +CPPFLAGS := -DVERSION=\"${VERSION}\" CFLAGS := -std=c99 -pedantic-errors -Wall -Wextra -Werror -O2 -c ${INCS} LDFLAGS := ${LIBS} diff --git a/xhotplug.1 b/xhotplug.1 @@ -0,0 +1,121 @@ +.Dd 2020-07-04 +.Dt XHOTPLUG 1 +.Os +.Sh NAME +.Nm xhotplug +.Nd X11 monitor attach/detach event responder +.Sh SYNOPSIS +.Nm +.Op Fl vh +.Ar script +.Sh DESCRIPTION +.Nm +is a simple tool designed to respond to X11 monitor attach/detach +events. +It can be used on systems without udev to, for example, dynamically +reconfigure monitors via +.Xr xrandr 1 . +.Pp +Upon invocation, +.Nm +reads the required +.Ar script +argument and checks that the file pointed to by it can be executed. +The file pointed to by +.Ar script +is considered not to be executable if one of the following conditions +is met: +.Pp +.Bl -bullet -compact +.It +The file for directory containing +.Ar script +does not exist or cannot be read. +.It +The file is world-writeable. +.It +The file is not owned by the current user or the superuser. +.It +The file's executable bit is not set. +.El +.Pp +If the above checks pass, +.Nm +waits for monitor attach/detach events, calling +.Ar script +whenever one is received. +.Sh OPTIONS +.Bl -tag -width Ds +.It Fl v +Print version and exit. +.It Fl h +Print usage and exit. +.El +.Sh EXAMPLES +There are several ways to make use of xhotplug. +The easiest is to add the following line to +.Pa $HOME/.xession +(or +.Pa $HOME/.xinitrc +if you use xinit): +.Pp +.Dl xhotplug script & +.Pp +Many desktop environments provide a way to add startup programs, +although they probably also detect new displays on their own. +.Pp +The script called by +.Nm +must be able to determine which monitors are currently connected and respond accordingly. +The below script does this, using +.Xr xrandr 1 +to configure the screen: +.Pp +.Bd -literal -offset indent -compact +#!/bin/sh + +expected="eDP-1 DP-1-3" + +for mon in $expected +do + if xrandr -q | grep $mon | grep ' connected' >/dev/null 2>&1 + then + connected="$connected $mon" + else + disconnected="$disconnected $mon" + fi +done + +cmd="xrandr --dpi eDP-1" + +for mon in $connected +do + if [ "$mon" = "eDP-1" ] + then + cmd="$cmd --output eDP-1 --auto --primary" + elif [ "$mon" = "DP-1-3" ] + then + cmd="$cmd --output DP-1-3 --auto --right-of eDP-1 --scale 2x2" + fi +done + +for mon in $disconnected +do + cmd="$cmd --output $mon --off" +done + +echo "my-screen.local: Running cmd $cmd" >> "$HOME/var/log/my" +$cmd +.Eb +.Sh EXIT STATUS +.Ex -std +.Sh SEE ALSO +.Xr xrandr 1 +.Sh HISTORY +.Nm +is based on a +.Lk https://gist.github.com/mafrasi2/4ee01e0ba4dad20cf7a80ae463f32fca "Gist by GitHub user mafrasi2" . +.Sh AUTHORS +.Nm +was written by +.An Daniel Moch Aq Mt daniel@danielmoch.com .