#!/bin/bash ### Configuration SLEEP=5 # poll lid state every SLEEP seconds UNROTATE="yes" # restore normal orientation when lid is open ROTATE="/usr/local/bin/rotate-screen" # rotate-screen command LID="/proc/acpi/button/lid/LID" # lid status path SPOOL_FILE="/var/spool/rotate-screen/rotation" # 'rotate-screen' command spool file ### while true; do # get current rotation state from run file if it exists DISPLAY="$(cat $SPOOL_FILE | cut -d' ' -f2)" XAUTH="$(cat $SPOOL_FILE | cut -d' ' -f3)" ROTATION="$(XAUTHORITY=$XAUTH DISPLAY=$DISPLAY xrandr --query --verbose | grep ' connected' | cut -d' ' -f5)" LID_STATE="$(cat $LID/state | awk '{print $2}')" HIBERNATING="$(ps aux | grep '[h]ibernate')" if [ x"$LID_STATE" = x"closed" -a x"$ROTATION" = x"normal" -a x"$HIBERNATING" = x ]; then "$ROTATE" echo rotate elif [ x"$LID_STATE" = x"open" -a ! x"$ROTATION" = x"normal" ]; then [ "$UNROTATE" = "yes" ] && "$ROTATE" normal && echo restore fi sleep $SLEEP done