个人工具

UbuntuHelp:BinaryDriverHowto/DynamicMultiMonitor

来自Ubuntu中文

跳转至: 导航, 搜索

IconsPage?action=AttachFile&do=get&target=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: https://help.ubuntu.com/community/BinaryDriverHowto/DynamicMultiMonitor?action=AttachFile&do=get&target=itm-desktop-layout.jpg%7CMy+Desktop%7Cwidth%3D%22800%22 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.

#!java
#!/bin/sh

# Sets the secondary display to the proper resolution if attached.

LAPTOP="LVDS"
HAVE_HDMI="`xrandr | grep 'HDMI-0 connected' | wc -l`"
HAVE_DFP="`xrandr | grep 'DFP1 connected' | wc -l`"

if [ $HAVE_HDMI = "1" ] ; then
    EXTERNAL_OUTPUT="HDMI-0"
elif [ $HAVE_DFP = "1" ] ; then
    EXTERNAL_OUTPUT="DFP1"
else
    EXTERNAL_OUTPUT=""
fi

AT_OFFICE1="`ifconfig eth0 | grep 'addr:192.168.15.' | wc -l`"
AT_OFFICE2="`ifconfig eth0 | grep 'addr:10.0.1.' | wc -l`"

xrandr --output $LAPTOP --preferred

if [ ! -x $EXTERNAL_OUTPUT ] ; then
    if [ $AT_OFFICE2 = "1" ] ; then
        xrandr --output $EXTERNAL_OUTPUT --mode "1680x1050" --pos 1600x0 --primary --output $LAPTOP --mode "1600x900" --pos 0x500
    fi

    if [ $AT_OFFICE1 = "1" ] ; then
        xrandr --output $EXTERNAL_OUTPUT --mode "1920x1080" --pos 1600x0 --primary --output $LAPTOP --mode "1600x900" --pos 0x500
    fi
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 ifconfig eth0 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:

#!java
[email protected]:~/.kde/Autostart$ chmod +x ./setDisplay.sh

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

#!java
[email protected]:~/.kde/Autostart$ chmod +x /usr/local/bin/setDisplay.sh