Forum

Full Version: [HOWTO] Reduce resolution while running VNC-Server (better performance)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi XBian-friends,

here's is an instruction/script by me how you can get your RPi to change resolution automatically when starting/stopping the VNC-Server.

Why it is needed?
It isn't! - But you get a way better performance with lower resolutions if you want to do something in XBMC via VNC.
Of course, you can do it even manually, but automating things is just more comfortable.

Any disadvantage?
As XBMC seems to need a restart after changing the RPi's resolution, XBMC will be restarted whenever you start or stop your
VNC-Server. This can be a disadvantage e.g. if you want to connect via VNC, but the server isn't started yet and XBMC is
currently scraping or something. But for me, it's not a real disadvantage. If someone knows how to bypass this, just shout!

How to:

All you have to do is creating following 3 files:

/etc/default/vnc-autores
Code:
### /etc/default/vnc-autores
# Configuration file for /usr/bin/local/vnc-autores
###

# Enable auto mode? ('vnc-autores auto' will be called when starting/stopping vnc-server)
# 0: no
# 1: yes
AUTOMODE=1

# GROUP and MODE for VNC use
# Get all supported modes by calling: tvservice -m DMT && tvservice -m CEA
VNCGROUP=DMT
VNCMODE=4

# DON'T EDIT ANYTHING BELOW HERE!
# (reserved for temporary vars)
DON'T FORGET TO CONFIGURE THIS FILE FOR YOUR NEEDS!


/etc/init/vnc-autores-controller.conf
Code:
### /etc/init/vnc-autores-controller.conf
# Controller service for /usr/local/bin/vnc-autores
###
task
start on (starting vnc-server or stopped vnc-server)

script
    exec /usr/local/bin/vnc-autores
    exit 0
end script



/usr/local/bin/vnc-autores
Code:
#! /bin/bash
### /usr/local/bin/vnc-autores
# This script get called by /etc/init/vnc-autores-controller.conf when
# starting/stopping vnc-server service to adjust screen resolution.
###

# set name and config file
NAME=vnc-autores
CONFIGFILE=/etc/default/$NAME

# check for sudo
if [ "$UID" -ne 0 ]; then
  echo "$NAME: requires root privileges (run with sudo)" >&2
  exit 1
fi

# check config file
if [ -r $CONFIGFILE ]; then
    . $CONFIGFILE

    if ! [[ $AUTOMODE =~ ^0$|^1$ ]] || ! [[ $VNCGROUP =~ ^DMT$|^CEA$  ]] || ! [[ $VNCMODE =~ ^[0-9]+$ ]]; then
        logger -s -t "ERROR" "$NAME: invalid configuration file ($CONFIGFILE)"
        exit 2
    fi
else
    logger -s -t "ERROR" "$NAME: no configuration file found ($CONFIGFILE)"
    exit 2
fi

# check for enable/disable arguments
case "$1" in
    enable)
        sed -i "s@AUTOMODE=0@AUTOMODE=1@" $CONFIGFILE
        echo "$NAME: enabled" >&1
        exit 0
        ;;
    disable)
        sed -i "s@AUTOMODE=1@AUTOMODE=0@" $CONFIGFILE
        echo "$NAME: disabled" >&1
        exit 0
        ;;
esac

# check if autores is disabled
if [ $AUTOMODE != 1 ]; then
    echo "$NAME: automode is disabled, run 'vnc-autores enable' first" >&1
    exit 0
fi

# get current tvservice state
STATE=$(tvservice -s)
GROUP=$(echo "$STATE" | egrep -o 'DMT|CEA')
MODE=$(echo "$STATE" | egrep -o '\([0-9]+\)'); MODE=${MODE:1:-1}

# function for restarting xbmc
restart_xbmc(){
    # wait for finishing pre/post-start status
    while [[ $(service xbmc status) == *-start* ]]; do
        sleep 2
    done
    service xbmc restart > /dev/null
}

# function for going into VNC mode
go_vnc(){
    if [ "$GROUP $MODE" != "$VNCGROUP $VNCMODE" ]
    then
        # save current tvservice state
        echo "TVGROUP=$GROUP" >> $CONFIGFILE
        echo "TVMODE=$MODE" >> $CONFIGFILE
    
        # switch to VNC mode
        tvservice -e "$VNCGROUP $VNCMODE" > /dev/null
        
        restart_xbmc    
        echo "$NAME: switched to VNC mode" >&1
    else
        echo "$NAME: already in VNC mode" >&1
    fi
}

# function for going into TV mode
go_tv(){
    if [ "$GROUP $MODE" == "$VNCGROUP $VNCMODE" ]
    then
        # check if former TV settings are setted/valid
        if [[ $TVGROUP =~ ^DMT|CEA$  ]] && [[ $TVMODE =~ ^[0-9]+$ ]]
        then
            # switch to former TV settings
            sed -i '/TVGROUP=/,$d' $CONFIGFILE
            tvservice -e "$TVGROUP $TVMODE" > /dev/null
        else
            # switch to preferred TV settings
            echo "$NAME: no former TV settings found, using preferred" >&1
            tvservice -p > /dev/null
        fi
        
        restart_xbmc
        echo "$NAME: switched to TV mode" >&1
    else
        echo "$NAME: already in TV mode" >&1
    fi
}

# determine mode to go
VNCSTATE=$(service vnc-server status)
if [[ "$VNCSTATE" == *starting* ]]; then
    go_vnc
else
    go_tv
fi

exit 0

and make the last one executable via:
Terminal
sudo chmod +x /usr/local/bin/vnc-autores


Alternatively you can download the files...
[attachment=299]



That's it.
If you now start the vnc-server service and the automode of vnc-autores is enabled, it will change the resolution before starting and switch it back after stopping.

At any time you can also disable/enable the automode without manually editing the config file by calling:
Terminal
sudo vnc-autores {disable|enable}


cheers
josch
(24th Jan, 2014 07:48 PM)josch Wrote: [ -> ]Any disadvantage?
As XBMC seems to need a restart after changing the RPi's resolution, XBMC will be restarted whenever you start or stop your
VNC-Server. This can be a disadvantage e.g. if you want to connect via VNC, but the server isn't started yet and XBMC is
currently scraping or something. But for me, it's not a real disadvantage. You could even just don't include the resolution-switch
script in your VNC-Server config and call it manually only when you want to.

@josch

good point (with changing resolution) and even better implementation (using same script and minimum "extra" scripting)

just two additions: - try moving pre-start and post-stop to completely new job. the job will start on starting vnc-server (starting means - vnc won't go main exec until all jobs with "starting vnc" are finished).
this from simple reason that with updates config files distributed by .deb files are preserving user changes, but binaries, scripts and others not. so once we issue new version, your script updates would be reverted.

when you update the start/stop logic and you move user settings outside (for instance /etc/default/vnc), i will add the code to vnc-server distribution deb file (if you allow of course Smile )
Thanks for your feedback!

I will transact your suggestions but it can take some time as I'm very busy the upcoming week.
But I'm sure I will find some hours in the next few days/weeks and will report here...

josch
Well, I just did it before the week starts and used some time of my sunday.

I updated the first post and replaced my old instruction....
Hope that's how you meant it. If not, just tell me. Smile

There is also no need anymore for setting the resolution settings for normal use. It will get
them byself when switching in VNC mode and restore them when switching back.

PS: Changed the title too, because there were a failure regarding too long title length while answering.
Reference URL's