Chinaunix首页 | 论坛 | 博客
  • 博客访问: 676205
  • 博文数量: 156
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 1201
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-05 20:08
文章分类

全部博文(156)

文章存档

2010年(13)

2008年(39)

2007年(104)

我的朋友

分类: LINUX

2010-05-18 09:32:46

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.

#!/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:

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


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


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


阅读(1894) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~