Saturday, April 25, 2015

Monitor/screen standby on Raspberry Pi 2

These instructions are kind of hastily thrown together so you might want to research this on your own a bit more and not simply copy-paste this.

We will use xscreensaver (from the repos) and tvservice - which is (apparently) preinstalled in /opt/vc/bin/tvservice - to control the video output and turn the connected monitor on and off.

Turning the monitor off is very straight forward. Just append the -o switch to the tvservice-command above and your monitor will turn off. This will however get you stuck with the monitor off and with no way of turning it back on.

Running the command with the -p switch sounds like a simple way of getting the output back. Though it is a teensy bit more difficult.

The magic command to get everything back is:
/opt/vc/bin/tvservice -p && fbset -depth 8; fbset -depth 16; xrefresh
Basically we first set the bitdepth to 8 and then back to 16, forcing the xorg-server to reset. Xrefresh repaints all graphics on screen.

Finally we use xscreensaver and its -watch switch to continuously watch for screen blank and unblank events and take the appropriate action.

The rest of this is pretty self explanatory I think so I'll just leave the scripts below and let you figure it out. :)

screen.sh
#!/bin/bash
if [ "$1" == "on" ]; then
 /opt/vc/bin/tvservice -p > /dev/null
 fbset -depth 8; fbset -depth 16; xrefresh
fi

if [ "$1" == "off" ]; then
 /opt/vc/bin/tvservice -o > /dev/null
fi

whatever.sh
#!/bin/bash

go() {
 while read input; do
  case "$input" in
   BLANK*) ~/script/screen.sh off ;;
   UNBLANK*) ~/script/screen.sh on ;;
  esac
 done
}

xscreensaver-command -watch | go