Search

Ubuntu 14.04 LTS - Monitors Position Lost in Dual Display Setup

Contents[Hide]

Ubuntu

If you are using a dual monitor display under Ubuntu Gnome Trusty 14.04 LTS, you may have noticed that both screen position can be set without any glitch thru the system parameters, but that this configuration is lost after every reboot.

In fact, if your screen positions are not the default proposed one (laptop screen at the left and second monitor at the right), it will be reset to the default one after every reboot. And, this occurs even if your monitor.xml configuration file is fully configured.

This article explains how to circumvent this Ubuntu misbehavior. It allows you to set your dual monitor positions and to make it survive after every reboot.

It has been tested under Ubuntu Gnome 14.04 LTS, but it should be applicable under other Ubuntu flavours.

1. Setup a Clean Configuration

Under modern Ubuntu system, multiple monitors configuration is saved in ~/.config/monitors.xml.

In some cases, this file can become corrupted or include multiple definitions.

So first step is to suppress this file and to recreate a clean one with your specific monitors configuration.

Terminal
# rm ~/.config/monitors.xml

You can now configure your monitors setup thru the menu Parameters / Displays.

Here is an example of my monitors setup :

ubuntu-dual-display-position

Once you have applied your monitors configuration, you should get a newly generated ~/.config/monitors.xml file.

After next reboot

If you reboot your computer, your monitor's resolution should be applied, but you may have lost the specific positions you've just setup.

If you check ~/.config/monitors.xml, it still contains the full configuration, including monitors size, position, refresh rate and rotation :

{displayfile filename=~/.config/monitors.xml}
  <configuration>
      <clone>no</clone>
      <output name="LVDS">
          <vendor>???</vendor>
          <product>0x0000</product>
          <serial>0x00000000</serial>
          <width>1366</width>
          <height>768</height>
          <rate>60</rate>
          <x>1920</x>
          <y>0</y>
          <rotation>normal</rotation>
          <reflect_x>no</reflect_x>
          <reflect_y>no</reflect_y>
          <primary>yes</primary>
      </output>
      <output name="DFP1">
      </output>
      <output name="CRT1">
          <vendor>ACR</vendor>
          <product>0x01a9</product>
          <serial>0x0460306f</serial>
          <width>1920</width>
          <height>1080</height>
          <rate>60</rate>
          <x>0</x>
          <y>0</y>
          <rotation>normal</rotation>
          <reflect_x>no</reflect_x>
          <reflect_y>no</reflect_y>
          <primary>no</primary>
      </output>
  </configuration>
</monitors>
{/jb_bluebox}

But Ubuntu Gnome 14.04 is not applying it correctly during login.

Hopefully, we can force this predefined position thru xrandr command line tool.

Terminal
# xrandr --output LVDS --pos 1920x0 --output CRT1 --pos 0x0

Your monitors respective positions should be back to what you've just configured thru the Displays interface.

2. Automatic Script to Force Monitors Position

We now have all the keys to write a script that will analyse monitors.xml predefined positions and apply them thru xrand command.

This script will be in charge of few steps :

  1. get the first configuration defined in the file
  2. list all monitors
  3. get their respective parameters (position, size, refresh rate and rotation)
  4. apply them thru randr

It uses a command line XML parser called xmllint which is provided by libxml2-utils package.

As this script will be used during login process where a delay is needed, it takes a delay in seconds as parameter.

/usr/local/sbin/update-monitor-position
#!/bin/bash
# -------------------------------------------------
#  Get monitors configuration from monitor.xml and apply it for current user session.
#  In case of multiple definitions in monitor.xml only first one is used.
#
#  See http://bernaerts.dyndns.org/linux/74-ubuntu/309-ubuntu-dual-display-monitor-position-lost
#  for instructions
#
#  Parameters :
#    $1 : waiting time in sec. before forcing configuration (optional)
#
#  Revision history :
#    19/04/2014, V1.0 - Creation by N. Bernaerts
#    10/07/2014, V1.1 - Wait 5 seconds for X to fully initialize
#    01/09/2014, V1.2 - Correct NULL file bug (thanks to Ivan Harmady) and handle rotation
#    07/10/2014, V1.3 - Add monitors size and rate handling (idea from jescalante)
#    08/10/2014, V1.4 - Handle primary display parameter
#    08/12/2014, V1.5 - Waiting time in seconds becomes a parameter
# -------------------------------------------------

# monitor.xml path
MONITOR_XML="$HOME/.config/monitors.xml"

# get number of declared monitors
NUM=$(xmllint --xpath 'count(//monitors/configuration['1']/output)' $MONITOR_XML)

# loop thru declared monitors to create the command line parameters
for (( i=1; i<=$NUM; i++)); do
  # get attributes of current monitor (name and x & y positions)
  NAME=$(xmllint --xpath 'string(//monitors/configuration['1']/output['$i']/@name)' $MONITOR_XML 2>/dev/null)
  POS_X=$(xmllint --xpath '//monitors/configuration['1']/output['$i']/x/text()' $MONITOR_XML 2>/dev/null)
  POS_Y=$(xmllint --xpath '//monitors/configuration['1']/output['$i']/y/text()' $MONITOR_XML 2>/dev/null)
  ROTATE=$(xmllint --xpath '//monitors/configuration['1']/output['$i']/rotation/text()' $MONITOR_XML 2>/dev/null)
  WIDTH=$(xmllint --xpath '//monitors/configuration['1']/output['$i']/width/text()' $MONITOR_XML 2>/dev/null)
  HEIGHT=$(xmllint --xpath '//monitors/configuration['1']/output['$i']/height/text()' $MONITOR_XML 2>/dev/null)
  RATE=$(xmllint --xpath '//monitors/configuration['1']/output['$i']/rate/text()' $MONITOR_XML 2>/dev/null)
  PRIMARY=$(xmllint --xpath '//monitors/configuration['1']/output['$i']/primary/text()' $MONITOR_XML 2>/dev/null)

  # if position is defined for current monitor, add its position and orientation to command line parameters
  [ -n "$POS_X" ] && PARAM_ARR=("${PARAM_ARR[@]}" "--output" "$NAME" "--pos" "${POS_X}x${POS_Y}" "--fbmm" "${WIDTH}x${HEIGHT}" "--rate" "$RATE" "--rotate" "$ROTATE")
 
  # if monitor is defined as primary, adds it to command line parameters
  [ "$PRIMARY" = "yes" ] && PARAM_ARR=("${PARAM_ARR[@]}" "--primary")
done

# if needed, wait for some seconds (for X to finish initialisation)
[ -n "$1" ] && sleep $1

# position all monitors
xrandr "${PARAM_ARR[@]}"

/usr/share/applications/update-monitor-position.desktop
[Desktop Entry]
Type=Application
Exec=update-monitor-position
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=Update Monitors Position
Name=Update Monitors Position
Comment[en_US]=Force monitors position from monitor.xml
Comment=Force monitors position from monitor.xml
Icon=display

You can download the script from my GitHub repo and make it executable :

Terminal
# sudo wget -O /usr/local/sbin/update-monitor-position https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/ubuntugnome/update-monitor-position
# sudo chmod +x /usr/local/sbin/update-monitor-position
# sudo wget -O /usr/share/applications/update-monitor-position.desktop https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/ubuntugnome/update-monitor-position.desktop
# sudo chmod +x /usr/share/applications/update-monitor-position.desktop

With the help of this script, it is now very simple to force your specific monitors positions : Just launch Update Monitor Position application.

Your monitors should be back in place.

3. Declare Script at Startup

Now that script is installed and tested, last step is to execute it at every login.

This can be done easily by adding one entry in Startup Applications Preferences.

As login process takes some time, it's advisable to wait for some time before updating monitor position.
That's why a 5 seconds parameter is given to the script.

Entry should be :

  • Name : Update Monitors Position
  • Command : update-monitor-position 5
  • Comment : Force monitors position 5 seconds after login

ubuntu-dual-display-startup

If you want to declare it thru command line, just run these commands :

Terminal
# mkdir -p $HOME/.config/autostart
# wget -O $HOME/.config/autostart/update-monitor-position.desktop https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/ubuntugnome/update-monitor-position.desktop
# sed -i -e 's/^Exec=.*$/Exec=update-monitor-position 5/' $HOME/.config/autostart/update-monitor-position.desktop
# chmod +x $HOME/.config/autostart/update-monitor-position.desktop

You can now reboot your computer.

Your monitors pre-defined positions should be applied at every login from what you've configured in the Displays configuration.

4. In Case of Trouble

In case proper configuration is not applied, first thing to do is :

  1. to remove ~/.config/monitors.xml
  2. to reconfigure your display settings
  3. to login again

In fact, for some unknown reasons, the <configuration> section may be duplicated in ~/.config/monitors.xml.

 

Hope it helps.

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus