Idle behavior

Most computer monitors will turn themselves off when your computer tells them to, or when it stops sending a display signal for a little while.  I've not found that the LG TV does that in the same way (and it's a TV after all, why should it?), nor does the TV really wake up when it receives a signal either.  So it would be nice if it were possible to correct that.  This script below can be edited to do just that -- but you'll have to read the comments for the necessary changes to make it work that way.

My situation is a little different -- but oddly the script is very similar to the case above.  When I lock my screen, I want the TV to automatically flip over to a different input (VGA).  When I bump the mouse/keyboard to unlock my computer, I want it to flip back to HDMI1.  Reason for this is I have an older KVM that doesn't do HDMI -- and the LG only has one VGA but my laptop looks much better on HDMI.  So my less frequently used computer is on the VGA input.  I'm lazy and don't like having to flip the KVM (for me, this is double-pressing the scroll lock key) and also find the TV remote to flip inputs, so I'd like it to be more automatic.

This script, while running, waits for DBUS messages to determine when the screensaver has been activated (either because you're not using the computer or because you locked the screen).  When the screen is idle, you can send one set of commands to the tv (like turn it off -- or in my case switch input to VGA).  When the password prompt is shown, you can send a different set of commands (like turn the TV back on, flip input to HDMI, whatever).  Finally, when the user has logged back in and the screensaver is off you can send other commands (just to be pedantic, send the same commands again like turn on the TV, switch input to the correct input to be on the safe side, etc).

This script doesn't eat any CPU or (much) memory sitting there, just run it with nohup.  If you really like it, add it to your session and let it run all the time.  I'm quite happy with it.  I can now flip over to my other computer by pressing ctrl-alt+L and then double-press scroll lock.  It's not perfect, but it beats the crud out of fumbling around with a TV remote all the time.

pollIdle.sh:

#!/bin/bash

## Switch the TV input, and if actually switched then send "exit" to clear OSD

# If you want the TV to power off during the screensaver, then uncomment

# the line below to turn the TV back on

function switchInput {

# ~/tv.sh ka 01

# You might have to adjust this to wait longer depending on your TV

# sleep 10s

if [ "$(~/tv.sh -s kb $1)" != "" ]; then

ignore=$(~/tv.sh mc 5b)

fi

}

# Min wait is the minimum time that the "hello" message will wake up the display

# That is because it gets sent right away when the screensaver is activated, so it is

# a false positive in that case.  Otherwise, after a few seconds it can more or less

# signal that the password prompt has been displayed

minWait=3

# Is the screensaver active (1) or not active (0)

active=0

# The last recorded idle timestamp (used with minWait)

lastIdle=$((`date "+%s"`-$minWait-1))

# Monitor all screensaver state changes and also the freedesktop "Hello" message

dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver',member='ActiveChanged'" \

    --session "interface='org.freedesktop.DBus',member='Hello'" |\

while read line ; do

now=`date "+%s"`

diff=$(($now - $lastIdle))


# If the screensaver is activated, remember its status with

# the variable "active" and switch over

# You could just as easily have the TV power off in this case

# but in my setup, I use this to switch inputs to another computer

if [ "$(echo $line | grep 'boolean true')" != "" ]; then

active=1

lastIdle=$now

switchInput "07"

# If you want the TV to power off during screensaver, remove the line above

# and use this instead: ~/tv.sh ka 0

fi

# If the screensaver is active and we see the hello message

# then it means the user has probably received the login prompt

# It would be good to show the screen again

if (( (active == 1) && (diff > minWait) )); then

if [ "$(echo $line | grep 'member=Hello')" != "" ]; then

lastIdle=$now

switchInput "08"

fi

fi

## The screensaver has finished, reactivate display

if [ "$(echo $line | grep 'boolean false')" != "" ]; then

active=0

switchInput "08"

# been gone a while?  Reajust backlight just in case. :-)

~/adjustBacklight.sh

fi

done