0

I move my laptop (ThinkPad T570, Ubuntu Xenial, Unity/Compiz) between different work environments, and I find that using Unity Control Center "Display" applet doesn't reliably (ever?!) retain the arrangements I specify.

To work around this, I've written a bash/xrandr script which arranges things as I want them. (Available here as a gist, but the details aren't really essential to my question so not including in full). If the screens don't appear correctly then I execute this script to arrange them.

What I'd like to do is have this script trigger on monitor connection. Unity/Compiz do this for the settings stored in Display prefs, so I'd like to know the correct way to take my preferred settings as expressed in bash/xrandr, and have that be delivered by Ubuntu as part of the "native" experience.

UPDATE: I found ~/.config/monitors.xml which seems to be where Unity stores the settings. Here's mine, with new revisions as I swap locations.

I've thought about triggering the rearrangement via /etc/udev/rules.d, but that would require permitting scripts run by root to talk to my WM, which presumably means opening xhost permissions. Since the default OS seems to arrange displays without that (just not to an arrangement I want), I'd prefer to find as "native" a solution as possible.

  • If you can answer this question, you can probably answer Can I set a default display arrangement? too. – Chris Burgess Dec 11 '17 at 21:37
  • you write: "I've thought about triggering the rearrangement via /etc/udev/rules.d, but that would require permitting scripts run by root to talk to my WM, which presumably means opening xhost permissions.", however you can always su - -c "your_script" $user which should let you work around the xhost requirement? – Tomáš Pospíšek Dec 11 '17 at 23:16
  • I did not know X11 could permit local connections based on the calling user. It's possible that it's been >10 years since I dealt with xhost and the like. Will investigate - thanks! (The issue I anticipated is not that the connection is from root, but rather that it's not from within the current X session.) – Chris Burgess Dec 12 '17 at 4:18
  • Possibly you will ll also have to set (and export) the DISPLAY variable. Any X client will want to access the ~/.Xauthority file, to read the current X session credentials. – Tomáš Pospíšek Dec 12 '17 at 7:17
0

I ended up handling this in a bash script which tests the output of xrandr for connected devices, then sets appropriate per-environment layouts.

Below is a simplified version which demonstrates how to do it.

#!/bin/bash

# Unity doesn't remember my screen layouts, so I made this to do it for me.
# GPLv3+, (c) Chris Burgess @xurizaemon

CONNECTED=$( xrandr | grep ' connected' )

LAYOUT=unknown
if [[ $CONNECTED = *"DP-1 connected"* ]] ; then
  LAYOUT=thunderbolt
elif [[ $CONNECTED = *"HDMI-2 connected"* ]] ; then
  LAYOUT=hdmi-2
fi

case "$LAYOUT" in
  "thunderbolt")
    # Thunderbolt, laptop
    xrandr --output DP-1 --auto --left-of eDP-1 --primary
    echo "Configured for $LAYOUT"
    ;;
  "hdmi-2")
    # Set to 57Hz for monitor support
    xrandr --newmode "1920x1080_57.00"  163.00  1920 2040 2240 2560  1080 1083 1088 1119 -hsync +vsync
    xrandr --addmode HDMI-2 "1920x1080_57.00"
    # HDMI, laptop
    xrandr --output HDMI-2 --mode "1920x1080_57.00" --left-of eDP-1 --primary
    echo "Configured for $LAYOUT"
    ;;
  *)
    echo "Layout $LAYOUT not known."
esac

My actual script is available at https://gist.github.com/e1892192ea8dd03a210d2890f8f966e7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.