Prettymad.net

Configure X Script for multiple screens

This is my little script that figures out what screen is attached to X, and configures X and mythtv automatically

#!/bin/bash

# figures out what sort of screen is connected, and sets up X to work with
# that screen, needs to be run as super user

last_connected_screen_file=~mythtv/.mythtv/last_connected_screen

# expected options of $1 are:
# Projector = SANYO
# Monitor = CPD-E200
# TV =

do_xresprobe=1
force_screen=$1
if [ "$force_screen" != "" ]; then
        do_xresprobe=0
        case "$force_screen" in
                "Projector")
                        connected_screen="SANYO"
                        ;;
                "Monitor")
                        connected_screen="CPD-E200"
                        ;;
                "TV")
                        connected_screen=""
                        ;;
        esac
fi

if [ $do_xresprobe -eq 1 ]; then
        connected_screen=`/usr/sbin/xresprobe nvidia | grep ^id: | awk '{print $2}'`
        if [ -e $last_connected_screen_file ]; then
                last_connected_screen=`cat $last_connected_screen_file`
        else
                last_connected_screen=XXXXX
        fi

        # if there is no change we dont proceed
        if [ "$last_connected_screen" == "$connected_screen" ]; then
                # no change in screen layout
                exit
        fi
fi

# we have several screens in X:
# Screen_SonyCRT
# Screen_Projector
# Screen_TV (default)

# we should get this data from the ~mythtv/.mythtv file
mythtv_user=mythtv
mythtv_pass=mythtv
mythtv_db=mythconverg
mythtv_server=localhost

case "$connected_screen" in
        "SANYO")
                echo "Using Projector" 1>&2
                new_screen=Screen_Projector
                echo 'update settings set data = "1" where value = "Deinterlace" and hostname = "anty"' |
                        mysql -u $mythtv_user --password=$mythtv_pass -h $mythtv_server $mythtv_db
                echo 'update settings set data = "0" where value = "AspectOverride" and hostname = "anty"' |
                        mysql -u $mythtv_user --password=$mythtv_pass -h $mythtv_server $mythtv_db
                ;;
        "CPD-E200")
                echo "Using Sony CRT Screen" 1>&2
                new_screen=Screen_SonyCRT
                echo 'update settings set data = "0" where value = "Deinterlace" and hostname = "anty"' |
                        mysql -u $mythtv_user --password=$mythtv_pass -h $mythtv_server $mythtv_db
                echo 'update settings set data = "4" where value = "AspectOverride" and hostname = "anty"' |
                        mysql -u $mythtv_user --password=$mythtv_pass -h $mythtv_server $mythtv_db
                ;;
        *)
                echo "Using Default TV Output, Got: $connected_screen" 1>&2
                new_screen=Screen_TV
                echo 'update settings set data = "0" where value = "Deinterlace" and hostname = "anty"' |
                        mysql -u $mythtv_user --password=$mythtv_pass -h $mythtv_server $mythtv_db
                echo 'update settings set data = "4" where value = "AspectOverride" and hostname = "anty"' |
                        mysql -u $mythtv_user --password=$mythtv_pass -h $mythtv_server $mythtv_db
                ;;
esac

echo "$connected_screen" > $last_connected_screen_file

cat /etc/X11/xorg.conf | sed 's/Screen.*".*"/Screen "'"$new_screen"'"/g' > /tmp/xorg.conf.setupx.sh
mv -f /tmp/xorg.conf.setupx.sh /etc/X11/xorg.conf

# kill the X session(s) if its there
pids=`ps -ef | grep X | grep mythtv | grep -v grep | awk '{print $2}'`
if [ "$pid" != "" ]; then
        kill "$pid"
fi

# kill the startx session(s) if its there
pids=`ps -ef | grep startx | grep mythtv | grep -v grep | awk '{print $2}'`
if [ "$pid" != "" ]; then
        kill "$pid"
fi

# kill the login shell if its there, effectively restarts the X session
pid=`ps -ef | grep "login -f" | grep -v grep | awk '{print $2}'`
if [ "$pid" != "" ]; then
        kill "$pid"
fi