You are not logged in.

#1 2013-07-30 15:11:50

manuelschneid3r
Member
From: Germany
Registered: 2013-04-14
Posts: 152

screensetup - a handy xrandr wrapper

I wrote a little tool. Maybe it is useful for someone.


Usage:

NAME

screensetup - a handy xrandr wrapper

SYNOPSIS

screensetup [-h] [output] [output]

DESCRIPTION

screensetup is a small wrapper for xrandr to make life more easy. If you are
not using a blaot DE that comes with a UI for screen setup and switch your
outputs pretty often (TV, Monitor, Notebook), this will come in handy for you.
This is about changing setting from command line more convenient as with xrandr.

If invoked with no parameters, all connected outputs are listed. 

If invoked with the name of an connected output, only this output will be
enabled. Every other output will be disabled.

If invoked with two (connected) outputs, these outputs will be enabled and
placed on the screen horizontally, side-by-dide in the order the outputs are
given as parameters. Every other output will be disabled.

If the given parameter is not a connected output, you will get an error message.
Generally the ouptuts are enabled with preferred modes. Refer to xrandr(1) for
more information about the terms output, screen and mode.

  --help -h

      Show this information.

EXAMPLES

screensetup -h

  Prints this usage info

screensetup VGA1 LVDS1

  Enables my analog monitor and my notebook monitor and places the output VGA1
  left of LVDS1 on the screen. Quite useful, because I place my notebook always
  right of my monitor.

screensetup HDMI-0 HDMI-1

  Enable a dualscreen setup with two monitors connected through HDMI.

SEE ALSO

xrandr(1)

AUTHOR

Manuel Schneider

Code:

#!/bin/bash

XRANDR=$(xrandr)


function printUsage {
echo -e \
"\e[32;1mNAME\e[0m

${0##*/} - a handy xrandr wrapper

\e[32;1mSYNOPSIS\e[0m

\e[32m${0##*/}\e[0m [-h] [output] [output]

\e[32;1mDESCRIPTION\e[0m

\e[34m${0##*/}\e[0m is a small wrapper for xrandr to make life more easy. If you are
not using a blaot DE that comes with a UI for screen setup and switch your
outputs pretty often (TV, Monitor, Notebook), this will come in handy for you.
This is about changing setting from command line more convenient as with xrandr.

If invoked with no parameters, all connected outputs are listed. 

If invoked with the name of an connected output, only this output will be
enabled. Every other output will be disabled.

If invoked with two (connected) outputs, these outputs will be enabled and
placed on the screen horizontally, side-by-dide in the order the outputs are
given as parameters. Every other output will be disabled.

If the given parameter is not a connected output, you will get an error message.
Generally the ouptuts are enabled with preferred modes. Refer to xrandr(1) for
more information about the terms output, screen and mode.

  --help -h

      Show this information.

\e[32;1mEXAMPLES\e[0m

${0##*/} -h

  Prints this usage info

${0##*/} VGA1 LVDS1

  Enables my analog monitor and my notebook monitor and places the output VGA1
  left of LVDS1 on the screen. Quite useful, because I place my notebook always
  right of my monitor.

${0##*/} HDMI-0 HDMI-1

  Enable a dualscreen setup with two monitors connected through HDMI.

\e[32;1mSEE ALSO\e[0m

xrandr(1)

\e[32;1mAUTHOR\e[0m

Manuel Schneider" |less -R
}

if [[ "$1" = "-h" || "$1" = "--help" ]];then 
  printUsage
  exit
fi

# Get the names of the connected screens
i=0
while read line
do
  SCR["$i"]="$line"
  (( ++i ))
done < <( echo "$XRANDR" | egrep -i "[[:alpha:]]* connected" | cut -d " " -f1 )

case "$#" in
  0)
    # Just dump the connected outputs
    echo ${SCR[@]} 
    exit
    ;;
  1)
    # Setup the given display
    if ! echo "${SCR[@]}" | grep -i "\b$1\b" > /dev/null; then
      echo "$1 not connected"
      exit
    fi
    COMMAND="xrandr --output $1 --preferred --primary"
    for i in ${SCR[@]}; do
      if [ "$1" != $i ]; then 
        COMMAND+=" --output $i --off"
      fi
    done
    echo "Calling '$COMMAND'"
    eval $COMMAND
    ;;
  2)
    # Setup the two given displays
    if ! echo "${SCR[@]}" | grep -i "\b$1\b" > /dev/null; then
      echo "$1 not connected"
      exit
    fi
    if ! echo "${SCR[@]}" | grep -i "\b$2\b" > /dev/null; then
      echo "$2 not connected"
      exit
    fi
    COMMAND="xrandr --output $1 --preferred --primary --output $2 --preferred --right-of $1"
    for i in ${SCR[@]}; do
      if [[ "$1" != $i && "$2" != $i ]]; then 
        COMMAND+=" --output $i --off"
      fi
    done
    echo "Calling '$COMMAND'"
    eval $COMMAND
    ;;
  *)
    echo "Too much outputs. (TODO)"
    ;;
esac

For improvements, bugs or just unexpected behaviour please write me a mail.

Regards

Last edited by manuelschneid3r (2013-07-30 15:13:55)


Please feel free to correct my english.

Offline

#2 2013-07-30 16:35:21

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,334

Re: screensetup - a handy xrandr wrapper

Moved to Community Contributions


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

Board footer

Powered by FluxBB