Swap monitors on the fly using command line

Hi all,

I write this post because I didn’t manage to find the solution of my problem on the internet and I hope that a solution really exists.

I use a GUI application that runs on a PC equipped with CentOS 7.2 and an nVidia video card.
This GUI application is on two monitors that are one on the top of the other (the X screen is only one, :0) and one extends the other.

I would like to write a shell script that swaps the two monitors (the GUI panels on the top are displayed on the bottom and viceversa) on the fly, so without having to reboot Xorg.
At the same time, such swap has to act also on which monitor becomes the primary (i.e. the Gnome top bar should move from one monitor to the other).

So the result would be the same as I physically swapped the display port cables attached to the monitors.

Is it possible to assemble calls to nvidia-settings command line tool to achieve this result?

I’ve tried with xrandr and disper but the result is that, even though the cursor boundaries update accordingly to the new configuration, a shell windows that is on the lower monitor is not transferred to the upper monitor as I would expect.

Thanks for helping me.

I don’t know if it works but I’d try faking a monitor on an unused connector in xorg.conf using ConnectedMonitor,HorizSync,VertRefresh, and set that to clone the primary (bottom?) monitor. To switch, set the bottom monitor to clone top one, then the top one to clone the fake one. Sounds at least logical?

You should be able to do this with carefully-crafted modelines. For example, if nvidia-settings -q CurrentMetaMode reports “DFP-1: nvidia-auto-select +0+0, DFP-2: nvidia-auto-select +1920+0” then you can swap the monitors with

nvidia-settings -a CurrentMetaMode="DFP-2: nvidia-auto-select +0+0, DFP-1: nvidia-auto-select +1920+0"

There’s a detailed description of the MetaMode syntax in the README: https://download.nvidia.com/XFree86/Linux-x86_64/381.22/README/configtwinview.html

Hi aplattner,

following your instructions the monitors correctly update the boundaries, but the open windows keep in the same position (i.e. a firefox instance on the upper screen is always on the upper screen, while it should be ‘transferred’ on the bottom screen).