Selecting NVidia screens on commandline

NVidia has a nice program to manage display devices like monitors, TVs etc. I’ve used this often to play movies on my PC and watch them on our big wide screen LCD TV. What I wanted to do, is automate switching between monitor and TV. The final goal is that I can push a button on a remote to switch the output. Hey, I’m a software engineer, I automate things.

The easiest way to do that would be two write a shell script that can perform the switch for me. NVidia has be so kind as to provide us with software that can do that. It’s nv-control-dpy, packaged in the nvidia-settings source, combined with xrandr.

Here is a step-by-step guide of how I managed to get things running.

  1. Hook up your TV if you haven’t done so.

  2. Let X detect the TV:

    nv-control-dpy --probe-dpys
    nv-control-dpy --build-modepool
  3. Tell X to include the TV in the current set of displays. You do this by getting a list of numbers from nv-control-dpy --get-associated-dpys:

    Using NV-CONTROL extension 1.14 on :0
    Connected Display Devices:
      CRT-0 (0x00000001): Acer AL1906
      CRT-1 (0x00000002): SAMSUNG
    associated display device mask: 0x00000001

    Add the two (0x...) numbers together and include them in the next call:

    nv-control-dpy --set-associated-dpys 0x00000003
  4. Add a metamode so that X knows which device to enable/disable. The above command gave you the names and numbers of the display devices - they could be TV-0, CRT-1, DPY-4, etc. To enable the device, set it to “nvidia-auto-select”. To disable a device, set it to “NULL”. Here is the command that I gave to disable my Acer monitor and enable my Samsung TV:

    nv-control-dpy --add-metamode \
        'CRT-0: NULL, CRT-1: nvidia-auto-select'
  5. The last step is to tell X to actually perform the switch. You use the xrandr command for this. First, get a list of possible resolutions using xrandr:

    Screen 0: minimum 320 x 240, current 1280 x 1024
    default connected 1280x1024+0+0 0mm x 0mm
       1280x1024      50.0*    51.0
       1280x960       52.0
       1280x800       53.0
       1280x768       54.0
       1152x864       55.0     56.0
       1152x768       57.0
       1024x768       58.0     59.0     60.0
       832x624        61.0
       800x600        62.0     63.0     64.0     65.0
       800x512        66.0
       720x450        67.0
       700x525        68.0     69.0
       640x480        70.0     71.0     72.0
       576x384        73.0
       512x384        74.0     75.0
       400x300        76.0
       320x240        77.0     78.0
       1360x768       79.0
  6. The last resolution in that list is the metamode we just added. To switch to it, use xrandr -s resolution@refreshrate:

    xrandr -s 1360x768@79

To switch back, use the same xrandr -s resolution@refreshrate trick, in my case xrandr -s 1280x1024@50

Once you’ve figured out the names of the devices, the bit masks and the meta mode lines, you can of course place the commands in shell scripts and really get to automate stuff. I’ll leave that as an exercise for the reader.

dr. Sybren A. Stüvel
dr. Sybren A. Stüvel
Open Source software developer, photographer, drummer, and electronics tinkerer