Skip to content

Idle Notification via Brightness

Mike Mob edited this page Feb 2, 2019 · 2 revisions

Using xbacklight

export XBACKLIGHT_RESTORE_FILE=$HOME/.cache/brightness
export XBACKLIGHT_NOTIFY=-80
brightness_xbacklight() {
	# Change the screen brightness using xbacklight. (hardware)
	# Set XBACKLIGHT_NOTIFY to a negative value to subtract brightness
	# Set XBACKLIGHT_NOTIFY to a positive value to set an absolute brightness
	pkill xbacklight

	# restore brightness
	if [[ $1 = 'restore' ]]; then
		[[ -f $XBACKLIGHT_RESTORE_FILE ]] && \
			xbacklight -set $(<$XBACKLIGHT_RESTORE_FILE) && \
			rm -f $XBACKLIGHT_RESTORE_FILE
		return
	fi

	# set brightness
	from=$(xbacklight -get)
	to=$XBACKLIGHT_NOTIFY
	(( $to < 0 )) && to=$(echo "x=$from+$to;if(x<1)x=1;x" | bc -l)
	xbacklight -set $to
	echo $from > $XBACKLIGHT_RESTORE_FILE
}; export -f brightness_xbacklight

By exporting the function, we can use brightness_xbacklight inside of a timer's command.

./idlelock.sh \
	--restore 'brightness_xbacklight restore' \
	-20 notify \
		+command 'brightness_xbacklight notify' \
	...

Now, after 20 seconds of inactivity, the screen brightness will be reduced. Once active again, the screen brightness will be restored to the previous brightness value.

Using xrandr

Using xrandr should only be used as a fallback when xbacklight cannot control your display. Xrandr dims your screen using software therefore there will be no real power savings.

export XRANDR_RESTORE=1
export XRANDR_NOTIFY=0.5
brightness_xrandr() {
	# Change the screen brightness using xrandr. (software fallback)
	# Set XRANDR_RESTORE to the brightness value to restore to
	# Set XRANDR_NOTIFY to the brightness value to notify with
	[[ $1 = 'restore' ]] && percent=$XRANDR_RESTORE || percent=$XRANDR_NOTIFY
	while IFS=$'\n' read -r display; do
		xrandr --output $display --brightness $percent
	done <<< "$(xrandr | grep -oP '.*(?= connected)')"
}; export -f brightness_xrandr

By exporting the function, we can use brightness_xrandr inside of a timer's command.

./idlelock.sh \
	--restore 'brightness_xrandr restore' \
	-20 notify \
		+command 'brightness_xrandr notify' \
	...

After 20 seconds of user inactivity, the screen brightness will be reduced to 50% (0.5). Once active again, the screen brightness will be restored to 100% (1).

Unknown Display

Sometimes we have no idea what sort of display we are using and what its capabilities are. You may be using a laptop display where xbacklight works or you may using an external display where only xrandr works. With both brightness_xbacklight and brightness_xrandr exported (see sections above), a new function brightness can be defined and exported to use xbacklight when applicable and xrandr as a fallback.

brightness_xbacklight() { ... }; export -f brightness_xbacklight
brightness_xrandr() { ... }; export -f brightness_xrandr

brightness() {
	# Use xbacklight or fallback to xrandr.
	xbacklight 2> /dev/null && brightness_xbacklight $@ || brightness_xrandr $@
}; export -f brightness

./idlelock.sh \
	--restore 'brightness restore' \
	-20 notify \
		+command 'brightness notify' \
	...

Using this method, it should not matter which type of display you are using. The screen will dim after a period of inactivity and have its brightness restored when active again.

Clone this wiki locally