SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2013-02-17 13:43:01

d-
Member
Registered: 2012-12-13
Posts: 13

External screen handler

Hi there,

On my keyboard I've got this functional key bound to F10 (called by xev XF86Display), which is supposed to handle displaying to external output devices, and since I connect my laptop to another screen quite often, I thougth it would be very nice to be able to change output automatically with single keystroke.

Thus, I created this simple script that handles the xrandr "state" when called. They are:
1. Extended desktop to the right.
2. Only attached output.
3. Only standard monitor output (LVDS1).

Configuration:
In order to start using this script you need to change to variables, STANDARD_OUTPUT and DESIRED_OUTPUT.
They are set to (respectively) LVDS1 (main screen in most laptops) and VGA1.
The DESIRED_OUTPUT variable is used when more than one device is connected. If it's present, it'll be chosen. If not, the first device given by xrandr will be chosen.

To bind the key use: http://crunchbanglinux.org/wiki/xev-det … eybindings.

The script:

#!/bin/bash
STATE_FILE=/tmp/randr-state
STANDARD_OUTPUT=LVDS1
DESIRED_OUTPUT=VGA1

function contains() {
    local n=$#
    local value=${!n}
    for ((i=1;i < $#;i++)) {
        if [ "${!i}" == "${value}" ]; then
            echo "y"
            return 0
        fi
    }
    echo "n"
    return 1
}

function assign_output() {
    array=($@)
    for output in ${array[@]}
    do
	if [ ! $output == $STANDARD_OUTPUT ]; then
	    TO_CHANGE=$output
	    break
	fi
    done
}

set_state() {
    if [ -z $1 ]; then
	return 0
    else
	echo $1 > $STATE_FILE
    fi
}

if [ -f $STATE_FILE ];
then
    STATE=$(<$STATE_FILE)
else
    STATE=0
fi

outputs=( `xrandr | grep -w "connected" | cut -d" " -f1` )
out_num=${#outputs[@]}

xrandr --output LVDS1 |while read -a line;do
RES="${line[0]}"
[[ ${RES} != [0-9]* ]] && continue
echo ${RES}
done

case "$out_num" in
    [0-1])
	exit 0
	;;
    2)
	assign_output ${outputs[@]}
    	;;
    *)
	if [ $(contains "${outputs[@]}" "$DESIRED_OUTPUT") == "y" ]; then
	    TO_CHANGE=$DESIRED_OUTPUT
	else
	    assign_output ${outputs[@]}
	fi
	;;
esac

case "$STATE" in
    0)	xrandr --output $TO_CHANGE --auto --right-of $STANDARD_OUTPUT
	set_state 1
	;;
    1)  xrandr --output $TO_CHANGE --auto
	xrandr --output $STANDARD_OUTPUT --auto
	xrandr --output $STANDARD_OUTPUT --off
	set_state 2
	;;
    2)  xrandr --output $TO_CHANGE --off
	set_state 0
	;;
esac

I know it's not the most neatly written script, but it does it's work. Suggestions on improving it are more than greatly welcomed. smile

I hope this helps someone. smile

Cheers,
d-.

Last edited by d- (2013-02-17 13:54:26)

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo