Switching Monitor Profiles

It's funny, when your home office is your couch, you tend to forget how
nice it can be when you dock a laptop and have all the extra screen real
estate a monitor brings. For many years, I left my work laptop docked
at work, and when I worked from home, I just VPNed in with a personal
laptop. Lately though, I've recognized the benefits of splitting personal
life and work, so I've taken to carrying my laptop with me when I go to and
from the office. Because we invested in a docking station, it's relatively
simple to transition between a laptop on my lap and a laptop on a desk
with an extra monitor—except for one little thing: my external monitor
is in portrait mode.

It must have been about two years ago that I started favoring
widescreen monitors in portrait mode (Figure 1). Really, all I need to
get work done is a Web browser and a few terminals, and I found if I keep
the Web browser on the laptop screen, I can fit a nice large screen
session or two in all the vertical space of a portrait-mode monitor. This
makes reading man pages and other documentation nice, plus I always
can split my screens vertically if I need to compare the contents of two
terminals (see my "Do the Splits" column in the September 2008 issue
for more information on how to do that: http://www.linuxjournal.com/article/10159). The only problem with portrait
mode is that all the GUI monitor configuration tools tend not to handle
portrait-mode monitors well, particularly if you want to combine them with
a landscape-mode laptop screen. So, I found I needed to run a special xrandr
command to set up the monitor and make sure it lined up correctly with my
laptop screen. Plus, every time I transition between docked and undocked
modes, I need to move my terminal windows from the large portrait-mode
monitor over to a second desktop on my laptop screen. This all seemed
like something a script could figure out for me, so in this article, I
explain the script I use to transition from docked to undocked mode.

Figure 1. Kyle's Current Desktop Setup

Basically, my script needs to do two things when it's run. First, it
needs to run the appropriate xrandr command to enable or disable the
external display, and second, it needs to reset all of my windows to
their default location. Although I could just have one script I run when I'm
docked and another when I'm undocked, I can find out my state from the
system itself, so I can keep everything within one script. I've set up a
script like this on my last two work-provided laptops and on the ThinkPad
X220, I was able to use a /sys file to gauge the state of the dock:

Unfortunately, on my new laptop (a ThinkPad X230) this file no longer
can detect the dock state. At first I was annoyed, but when writing this
column, I realized that this made the script potentially more useful
for everyone who doesn't have a docking station. My workaround was to
use xrandr itself to check for the connection state of a video device
my external monitor was connected to that was present only when I was
docked. If you run xrandr with no other arguments, you will see a list
of a number of different potential video devices on your system:

In the above case, the laptop is not docked, so only the primary monitor
(LVDS1) is connected. When I docked the device and ran the same command,
I noticed that my monitor was connected to HDMI3, so I could grep for
the connection state of HDMI3 to detect when I'm docked. My new skeleton
script looks more like this:

In your case, you would compare the output of xrandr when docked (or
when an external monitor is connected) and when undocked, and use that
to determine which device it corresponds to.

Now that I can detect whether I'm docked, I should do something about
it. The first thing I need to do is to enable output on my external
monitor (HDMI3), tell xrandr that it's to the right of my laptop screen,
and set it to portrait mode by telling xrandr to rotate it left:

/usr/bin/xrandr --output HDMI3 --auto --right-of LVDS1 --rotate left

This works fine; however, the way that the portrait-mode monitor and
my laptop line up on the desktop makes moving a mouse between the two
rather awkward. When I move from the top of the laptop screen to the far
right edge, the mouse pointer moves a foot up to the top of the external
monitor. Ideally, I'd like the mouse pointer to more or less be lined up
when it crosses between screens, but because one monitor is landscape and
the other is portrait, I need to tell xrandr to place my laptop monitor
lower in the virtual desktop. Depending on your respective resolutions,
this position takes some tinkering, but I found the following command
lined up my two displays well:

/usr/bin/xrandr --output LVDS1 --pos 0x1152

This takes care of my screen when I'm docked, so when I'm undocked, I
basically have to undo any of the above changes I've made. This means
turning the HDMI3 output off and moving the position of LVDS1 back to
the 0x0 coordinates:

After I saved the script, I bound a key combination on my desktop I could
press to execute it whenever I docked or undocked. Of course, ideally I would
set up some sort of udev script or something like it to
run the script automatically, but so far, I haven't found the right hook that worked
on my laptop. The only other addition I've made is after the above case
statement, I sleep for a second and then call a reset_windows shell script
that uses wmctrl, much like I discussed in my November 2008 Hack and /
column "Memories of the Way Windows Were" (http://www.linuxjournal.com/article/10213), only it also contains the
same case statement so it moves windows one way when docked and another
when not:

Of course, the above wmctrl commands are completely custom to my terminal
titles, but it should serve as an okay guide for getting started on your own. In my
case, I want to move two terminals to the second desktop when in laptop
mode and to the external monitor on the first desktop when docked. Why not
just combine the two scripts? Well, I want to be able to reset my windows
sometimes outside of docking or undocking (this script also is bound to
a different key combo). In the end, I have a simple, easy-to-modify set of
scripts I can use to keep windows and my desktops exactly how I want them.

______________________

Kyle Rankin is a director of engineering operations in the San Francisco Bay Area, the author of a number of books including DevOps Troubleshooting and The Official Ubuntu Server Book, and is a columnist for Linux Journal.

Comment viewing options

Lamborghini Veneno – a racing prototype and road-going super sports car as a tribute to the 50th anniversary of Automobili Lamborghini Bulldozer Work Lights the year of its 50th anniversary Automobili Lamborghini is presenting an extremely exclusive model at the Geneva Motor Show 2013.

having to click screen resolution and select different screens etc is a bit of a pain, and to get round this I used to have an autohotkey script that opened up the NVIDIA control panel and select a different iphone 5s jailbreak profile. However in W7, for some reason everytime I select a new profile it seems to mess up all the settings/not work at all.

I really like what you guys are up too. Such clever work and coverage! Keep up the superb works guys I've you guys to blogroll. buying instagram followers provides quality social services. it is possible to buy followers on instagram. Máy lọc nước nanomáy lọc nước

I really like what you guys are up too. Such clever work and coverage! Keep up the superb works guys I've you guys to blogroll. buying instagram followers provides quality social services. it is possible to buy followers on instagram. Máy sấy quần áo cho trẻ Máy sấy quần áo

Hi Kyle,
Your story and your solution to the problem is amazing. You must be enjoying it but what I prefer is two different PCs on my work station with one large and one mid-sized monitor. Obviously, my personal laptop with me always.....

Your scripts call for 'wmctrl'. I finally found this in my Mint-15/Ubuntu repositories -- stand alone, not associated with X11 or Xorg or anything.
It seems important enough that it should be part of some desktop '*-tools' packages.
~~~ 0;-Dan

I devoured this article in hopes that it resolves troubles I have been fighting for months. Our parts are very similar so I hope thing work the same.

I have the X220-tablet-PC in the "convertible" implementation.
My desktop monitor is a 26-inch that also rotates portrait and landscape.
I'm struggling with the "convertible" aspects of the X220-tablet and cannot seem to get events (DBUS, ACPI, whatever) for things that are supposed to happen.

Thanks for every other informative web site. The place else may I get that kind of info written in such a perfect approach? I’ve a mission that I’m simply now working on, and I have been at the glance out for such info.
Finally I read a very effective material, thank you from my heartתיקון גגות רעפים

I really like what you guys are up too. Such clever work and coverage! Keep up the superb works guys I've you guys to blogroll. buying instagram followers provides quality social services. it is possible to buy followers on instagram.

I did something similar not too long ago. My method to run automatically was through acpi. The script goes in /etc/acpi/actions and needs a corresponding event file:event=ibm/hotkey LEN0068:00 00000080 0000401.
action=/etc/acpi/actions/monitors.sh

(If you find a way to do this through udev, I'm all ears...)

Also, be aware that if you run commands like xrandr or wmctrl through a system process you will need to give them access to the X display. (This is useful for cron scripts as well.)USER=$(id -n -u)
export XAUTHORITY=`find /var/run/lightdm/$USER -type f | head -n 1`
export DISPLAY=:0

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.