Skip to content

Two heads are better than one… right?


Most days, a certain developer’s primary PC sits atop a port replicator. Attached are two monitors. At home, it sits atop it’s namesake – a lap. Specifically, the lap belonging to the aforementioned developer.

Setting up the dual head display was simple enough. Since the machine has a Nvida graphics interface, the ‘nvidia-settings’ utility provided the quickest path to two-screened bliss. It provides a straightforward GUI to configure displays, and generate a corresponding xorg.conf, very intuitive stuff.

However, when the machine is off the docking station and running on the single native display, the desktop still spans two screens and only one is visible. Move the mouse too far to the left, and it dissapears from sight. Keyboard shortcuts to move windows between screens or workspaces? Now you see it, now you don’t. Clearly, this will not do.

In the past, a few different methods have solved this issue sufficiently. A script that runs when the desktop environment, or window manager can be used to check xrandr, and adjust output accordingly, etc… This causes the screen to flicker after X starts though. Surely there must be a better solution?

A little searching yielded this post ( which provides a pretty simple way to solve this problem. Create separate xorg.conf files, ok, simple enough. Edit a couple of config files? Sure, we could do that. Enter a runlevelat startup, or choose a different option from your boot menu? Whoa, hold on there buddy. We’re way too lazy, not to mention impatient, for all of that.

Besides, if we had to choose a ‘hardware profile’ from the boot loader menu, we’d have to increase the wait time from zero to something, well… else. Thanks but no thanks. On top of that, what if we aren’t even booting into X? We’ve done all that work for nothing? We are way too lazy for that.

Then the a-ha moment comes – if we have two (or, multiple) Xorg configs, there has to be some way to automatically choose the right one when X starts. Now you’re talking!

So, we have xorg-single and xorg-dual or lvds/tmds, whatever you fancy.

A quick read through /etc/X11/Xsession yields this juicy morsel; $HOME/.xsessionrc is sourced on startup. Seems like a reasonable placed to start.

Half an order of leftover hunan chicken, a lousy B movie from the 90’s, a handful of bash script tutorials, and one too many caffeine-charged beverages later, we have a script that should take the grunt work out of it and ‘anticipate what we want’ as Larry Wall would put it (wiki 3 tenets…)

Now all we need to do is call it from someplace before X is actually started.

On Ubuntu, the following lines in /etc/event.d/rc5 do the trick quite nicely.

pre-start script
exec /path/to/your/script
end script

Provided you put your script somewhere in a proper $PATH (eg /usr/local/bin) and set the appropriate permissions (chmod 755), this should do the trick.

If you’re not on Ubuntu or not using the Upstart init daemon, your inittab might be a suitable location. Otherwise, you could hook into the rc5 System-V init script.

Something you will probably want to consider when choosing a location is whether it will be affected by some external program. Say, for example, a package manager, or some script that autogenerates config files and start scripts.

There you have it, a simple way to dynamically choose your configuration. Next stop – hotplugging! Now that you mention it, with Upstart that might not be so difficult…

Hopefully this is useful to you in some way. Until next time, happy hacking!

Initially, the plan was to implement the configuration selector script in Ruby. However, for the purpose of making it usable to the greatest number of people, Bash was used instead. Suggestions for simplifying or improving the script are most appreciated!

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: