IconsPage/important.png For a more flexible solution, see DynamicMultiMonitor.

IconsPage/info.png Here's a quick little script I wrote to set the proper display when I'm at work.

The Situation

I work at three primary places: home, office one, and office two.

At home, I just use my laptop. At office one I have a 22" monitor and at office two I have a 24" monitor. Obviously, the resolutions are different and when I connect to them I want my displays to be set perfectly.

My laptop is always to the right of my external monitor. Here is a picture of office one's setup:

My Desktop

As you can see the laptop is to the left and lower than my external monitor.

XRandR To The Rescue!

XRandR is a powerful little utility, and with the proper amount of psychology, and extreme violence, I was able to have it do my bidding. What follows is the small shell script I wrote to control, dynamically, the external monitor settings.

   1 #!/bin/sh
   3 # Sets the secondary display to the proper resolution if attached.
   6 HAVE_HDMI="`xrandr | grep 'HDMI-0 connected' | wc -l`"
   7 HAVE_DFP="`xrandr | grep 'DFP1 connected' | wc -l`"
   9 if [ $HAVE_HDMI = "1" ] ; then
  11 elif [ $HAVE_DFP = "1" ] ; then
  13 else
  15 fi
  17 AT_OFFICE1="`ip -o addr show dev eth0 | grep 'inet 192.168.15.' | wc -l`"
  18 AT_OFFICE2="`ip -o addr show dev eth0 | grep 'inet 10.0.1.' | wc -l`"
  20 xrandr --output $LAPTOP --preferred
  22 if [ ! -x $EXTERNAL_OUTPUT ] ; then
  23     if [ $AT_OFFICE2 = "1" ] ; then
  24         xrandr --output $EXTERNAL_OUTPUT --mode "1680x1050" --pos 1600x0 --primary --output $LAPTOP --mode "1600x900" --pos 0x500
  25     fi
  27     if [ $AT_OFFICE1 = "1" ] ; then
  28         xrandr --output $EXTERNAL_OUTPUT --mode "1920x1080" --pos 1600x0 --primary --output $LAPTOP --mode "1600x900" --pos 0x500
  29     fi
  30 fi

First a disclaimer: I am no shell script expert.

My laptop's display is called LVDS. My external monitor is DFP1. However, when using the open source ATI drivers, it is called HDMI-0. Since I've been known to switch between the proprietary and open-source drivers, this script handles both. Obviously, if your external display name is different, just change it.

The script queries XRandR for the state of both HDMI-0 and DFP1. If either are in a connected state, the associated variable is set to a value of "1". I never learned sed and awk so I use the poor man's version: grep and wc.

Since both my monitors are named the same at both offices, I rely on my ethernet connection's IP address to determine where I am. Here I query ip addr for the currently assigned IP address. Each office uses a different subnet so I'm safe.

Checking these values, I'm able to determine where I am and what display is connected. Now the fun begins. Well, it's not really fun...

If my external output is connected $EXTERNAL_OUTPUT I run an XRandR command to set it up the way I like it.

My laptop is 1600x900 at all times. Therefore my external output offset is 1600. This is set in the --pos 1600x0 statement. I also tell XRandR that this is my primary monitor with the --primary command.

Next, my laptop is always below my external monitor's viewport. I like my virtual space to mimic reality. Therefore I set my laptop display to be 500 pixels below the top of my external monitor's viewport. This is done with the --pos 0x500 in the second half of the XRandR call.

And that is it. I named this script setDisplay.sh and placed it in my ~/.kde/Autostart directory.

If you have several users who need to share this script, place it in your /usr/local/bin directory.

The final step is to make it executable:

   1 jablan@lucifurious:~/.kde/Autostart$ chmod +x ./setDisplay.sh

or, if it's in /usr/local/bin:

   1 jablan@lucifurious:~/.kde/Autostart$ chmod +x /usr/local/bin/setDisplay.sh


BinaryDriverHowto/DynamicMultiMonitor (last edited 2011-10-12 08:44:31 by jacksenechal)