#! /bin/sh
# 
# Laptop mode tools module: selectively disable video outputs
#

do_xrandr () {
  	w -hs | while read -r USER TTY DISPLAY REMAINDER; do	
	  	if [ "${DISPLAY#:}" != "$DISPLAY" ] ; then
	  		# It's an X display.
		  	if su $USER -c "xrandr --output $1 --$2" | grep -q display ; then
				log "VERBOSE" "Unable to set video output $1 to $2."
			else 
				log "VERBOSE" "Set video output $1 to $2"
			fi
		fi
        done
}

if [ x$CONTROL_VIDEO_OUTPUTS = x1 ] ; then
	if [ $ON_AC -eq 1 ]; then
		if [ "$ACTIVATE" -eq 1 ]; then
			DISABLE_VIDEO_OUTPUTS="$LM_AC_DISABLE_VIDEO_OUTPUTS"
		else
			DISABLE_VIDEO_OUTPUTS="$NOLM_AC_DISABLE_VIDEO_OUTPUTS"
		fi
	else
		DISABLE_VIDEO_OUTPUTS="$BATT_DISABLE_VIDEO_OUTPUTS"
	fi

	if [ -f /var/run/laptop-mode-tools/disabled-video-outputs ] ; then
		cat /var/run/laptop-mode-tools/disabled-video-outputs |
			while read VIDEO_OUTPUT REMAINDER ; do
				do_xrandr $VIDEO_OUTPUT auto
			done
	fi
			
	rm -f /var/run/laptop-mode-tools/disabled-video-outputs
	
	for VIDEO_OUTPUT in $DISABLE_VIDEO_OUTPUTS ; do
		echo $VIDEO_OUTPUT >> /var/run/laptop-mode-tools/disabled-video-outputs
		
		do_xrandr $VIDEO_OUTPUT off
	done
else
	log "VERBOSE" "video-out module is disabled."
fi

