The One with the Thoughts of Frans

Multiple Monitors: Enabling And Disabling With the Aid of nvidia-glx

As I wrote last year, I wish to “automatically output whatever video I’m playing in fullscreen on another monitor.” Because the other monitor isn’t next to my main monitor, most of the time the output doesn’t have any practical purposes. That isn’t the biggest problem, although you might want to consider Mouse Jail. However, I recently changed from Nouveau, the open-source nVidia drivers, to nvidia-glx, the proprietary nVidia drivers that offer all the latest and greatest OpenGL acceleration: I wanted to play Achron.

After extracting the Achron archive, it turned out that the game wanted to spread across the entire virtual space available, i.e., across both of my monitors. Disabling one monitor is easy enough, but it would have to be automated. Besides, figuring out how to automate this would be a fun exercise in itself.

First of all, there were some issues with xorg.conf. nvidia-settings can generate one, but it’s not the greatest and requires some manual modification, if only to make sure your own mouse remaps and what not aren’t left behind. What we’re interested in is the section where it talks about metamodes. I thought I’d need two of them to enable easy switching: one with the second monitor enabled and one where it was disabled. Since I only actually use the second monitor for VLC video output, it should be disabled by default and enabled only for VLC: Option "metamodes" "DFP: nvidia-auto-select +0+0, CRT: NULL; DFP: nvidia-auto-select +0+0, CRT: nvidia-auto-select +1280+0". Alas, if only it were this simple. As it turns out, now the graphics driver creates a gigantic virtual display space in order to accommodate the largest resolution metamode, allowing you to pan around in it. New plan: we disable the second monitor on boot and dynamically add a metamode when starting VLC. The line in xorg.conf now looks only like this: Option "metamodes" "DFP: nvidia-auto-select +0+0, CRT: NULL".

So, how to add metamodes on the fly? Well, the nvidia-glx driver comes with this nice little utility called nv-control-dpy. Based on that I wrote the following shell script to start VLC:


# Check if the desired resolution is available and if not, add it. The check isn't really necessary for proper operation but it keeps down error output.
[[ "`nv-control-dpy --print-metamodes`" !=  *@1920x1200* ]] && nv-control-dpy --add-metamode "DFP-0: nvidia-auto-select @1280x1024 +0+0, CRT-1: nvidia-auto-select @1920x1200 +1280+0"
xrandr -r 51.0 -s 3200x1200

# See
source /etc/profile
ulimit -v 1048576

/usr/bin/vlc $@

# Check if there's still an instance of VLC running.
# $$ is the process's own PID; testing the exit code of pgrep vlc for success wouldn't work 'cause this script itself would still cause that to test positive. Incidentally, see how to check for exit codes at (Yes, this comment is all based on me testing something and wondering for a minute or so why my script wasn't working correctly.)
# To check that you can try
#somecommand  argument1 argument2
#[ $RETVAL -eq 0 ] && echo Success
#[ $RETVAL -ne 0 ] && echo Failure
[ "`pgrep vlc`" == $$ ] && xrandr -r 50.0 -s 1280x1024

The nVidia driver plays a little trick on X11 by pretending the different metamodes have different refresh rates. In some future update this should no longer be necessary, but for now that’s how you can easily switch between them with xrandr. The script is named vlc and added to my PATH in such a manner that it overrides the default link to the VLC binary. I think that should be enough information to adjust this to your own personal needs.

1 Comment

  1. […] The surrounding fluff is my script for automatically enabling and disabling my intended output monitor. […]

    April 17, 2013 @ 21:32Permalink
    A Poor Man’s Clone Window With Mplayer | The One with the Thoughts of Frans

RSS feed for comments on this post· TrackBack URI

Leave a Comment

You must be logged in to post a comment.