I have a Lenovo Thinkpad T61, and I had been trying to find a way switch between monitors using a keystroke. Particularly, this meant finding a way to enable/disable monitors using the command line.
This thread suggested that nvidia-settings should make this possible, but apparently, it isn't implemented by Nvidia, yet... (or so the "TODO" section of this document) suggests)
Luckily, we can compile the nv-control-dpy tool, which will give us the tools we need to enable or disable monitors, with a little extra help from scripts I wrote.
First, lets get the source code.
Code:
$ mkdir nvidia-source
$ cd nvidia-source
$ apt-get source nvidia-settings
$ cd nvidia-settings-*
Next, download dependencies
Code:
$ sudo apt-get install build-essential libxext-dev libx11-dev
First, you have to compile the an nvidia library:
Code:
$ cd src/libXNVCtrl
$ make
Now, build nv-control-dpy:
Code:
$ cd ../../sample
$ make nv-control-dpy
If all went well, you should have an executable nv-control-dpy file in your directory. This application lets us perform many of the low-level operations that nvidia-settings performs when you use it from the GUI interface.
Test it out:
Code:
$ ./nv-control-dpy -h
Let's install this executable somewhere convenient:
Code:
$ sudo cp nv-control-dpy /usr/local/bin
I have written some bash scripts to parse the output of nv-control-dpy, and some command-line php scripts to do to the enabling and disabling operations (because my Perl-fu is weak ). These files are attached to this post.
To install the scripts:
Code:
$ tar xzvf nv-tools.tar.gz
$ cd nv-tools
$ sudo cp -r scripts/* /usr/local/bin
Included scripts:
- nv-get-displays - list the currently attached displays
- nv-get-metamodes - list metamodes in a format easily consumable by my php scripts
- nv-get-current-metamode - get the id of the currently attached metamode
- nvidia/nvidia.inc.php - functions for parsing metamodes and switching between modes.
You can control these by writing a simple command-line php script. The "sample/" subdirectory has a few examples of how to enable a single monitor, or enable multiple monitors. Here's an example of enabling a CRT monitor at 1280x1024 resolution:
PHP Code:
#!/usr/bin/php5
<?
require_once('/usr/bin/nvidia/nvidia.inc.php');
enable_single_display('CRT-0', "1280x1024");
Note: you'll need to change /usr/bin/php5 to wherever your command-line php interpreter is located. Also, you may wish to locate the nvidia.inc.php file elsewhere in your filesystem, so update the require_once line if you do
To enable multiple monitors, use the following syntax:
PHP Code:
#!/usr/bin/php5
<?
require_once('/usr/bin/nvidia/nvidia.inc.php');
$displays = array(
'DFP-0' => '1280x800',
'CRT-0' => '1280x1024'
);
enable_displays($displays);
Now, you can just make these scripts executable, and assign them to a key-combination.
If you are interested in how this all works, it goes something like this: For a specific configuration, you have to create a "metamode" using nv-control-dpy which says which monitors are enabled, at what resolution, and at what location relative to one another. The command is: "nv-conrol-dpy --add-metamode <meta-modeline>" . Once you've created a metamode, nv-control-dpy will assign it an id. Using the xrandr utility you can switch to this new configuration by specifying the display resolution and specifying the id as the framerate. Nvidia uses the framerate to store the id, because there is no simple way distinguish two different configurations that have the same resolution in xrandr. The resolution you give to xrandr is the total size of the virtual screen made up of all enabled monitors (e.g. if I have a 1280x1024 monitor cloned with a 1440x900 monitor, the resolution would be 1440x1024).
Bookmarks