[SATLUG] A script for turning off the LCD and locking the screen on laptops

Travis M. travisimo1993 at gmail.com
Tue Dec 18 18:17:46 CST 2007


Hello,

Here is a script for turning off the LCD and locking the screen with xscreensaver that I made almost entirely myself, it is designed to be used with acpid. I tested it and it does all that I need it to do:

---------------------------CUT HERE----------------------------------
#!/bin/bash

export XUSER="$( ps --no-header -C xinit f -o user )" || exit 3
export XAUTHORITY="$( ps --no-header -C xinit f | sed -e 's/.*-auth\ //' -e 's/serverauth.*/Xauthority/' )" || exit 4

evaluate_retval() {
		if ! [[ "$?" = "0" ]]; then
			logger -t 'lidscript' -p 'daemon.crit' 'The return value of the previous' \
																'command was not 0! Exiting..., Error Code: 14'
			if ! [[ -z "$@" ]]; then
				logger -t 'lidscript' -p 'daemon.crit' "The failing command (or pipeline) was: "$@""
			fi

			exit 14
		fi
}

if test $( fgconsole ; evaluate_retval fgconsole ) = $( ps --no-header -C X -o tty | grep -o '[0-9]' ; evaluate_retval 'ps --no-header -C X -o tty | grep -o '\[0-9\]'' )
then
export ON_X_TTY=1
else
export ON_X_TTY=0
fi

# lid button pressed/released event handler
test -x /usr/sbin/laptop_mode && /usr/sbin/laptop_mode auto

xopencloseaction() {
	SLEEP="0.4"
	
	if [[ -z "$XUSER" ]]; [[ -z "$XAUTHORITY" ]]; then
			[[ -z "$XUSER" ]] && \
			logger -t 'lidscript' -p 'daemon.warn' \
			'In xopencloseaction(), XUSER is empty! Cannot continue!'
			
			[[ -z "$XAUTHORITY" ]] && \
			logger -t 'lidscript' -p 'daemon.warn' \
			'In xopencloseaction(), XAUTHORITY is empty! Cannot continue!'
			exit 39
	fi
	
	if [[ -z "$(pgrep -u $XUSER xscreensaver)" ]]; then
		setuidgid $XUSER /usr/bin/xscreensaver -nosplash || exit 43
	fi

	setuidgid $XUSER /usr/bin/grep "^dpmsOff:.*" /home/"$XUSER"/.xscreensaver > .dpmsOff_val || exit 46
	setuidgid $XUSER /usr/bin/sed "s/^dpmsOff:.*/dpmsOff:  0:00:10/" -i /home/"$XUSER"/.xscreensaver || exit 47

	case "$1" in 
		lid_closed)
		t=0; while [[ "$t" -lt "2" ]]; do sleep "$SLEEP"; \
		setuidgid $XUSER /usr/bin/xset dpms force off && (( t++ )); done || exit 52
		;;
		lid_opened)
		t=0; while [[ "$t" -lt "2" ]]; do sleep "$SLEEP"; \
		setuidgid $XUSER /usr/bin/xset dpms force on && (( t++ )); done || exit 56
		;;
	esac

	setuidgid $XUSER /usr/bin/xscreensaver-command -lock || exit 60
	setuidgid $XUSER /usr/bin/xscreensaver-command -restart || exit 61
	setuidgid $XUSER /bin/sed s/^dpmsOff:.*/"$(cat .dpmsOff_val)"/ -i /home/"$XUSER"/.xscreensaver || exit 62
	rm -f .dpmsOff_val
	setuidgid $XUSER /usr/bin/xset dpms 0 0 0 || exit 64
}

if grep -q open /proc/acpi/button/lid/LID/state; then
	if [[ "$ON_X_TTY" = "1" ]]; then
		xopencloseaction lid_opened
	else
		/usr/sbin/vbetool dpms on
	fi
else
	if [[ "$ON_X_TTY" = "1" ]]; then
		xopencloseaction lid_closed
	else
		/usr/sbin/vbetool dpms off
	fi
fi
---------------------------END CUT-----------------------------------



More information about the SATLUG mailing list