1

Every time I disconnect my three external monitors from my laptop, or when I plug them back in, all the resolution and extended desktop settings are messed up. I'll have to change back the settings to get it right again.

I am using gnome 3.18.5 on Ubuntu 16.04. I have Intel HD graphics 530 and an Nvidia 960M with nvidia driver version 375.39. I also installed the gnome shell extension called 'Fix-Multi-Monitors', which did indeed fix a bunch of issues like windows only moving between two out of three monitors with window moving shortcuts.

Anyway, what I'd like to do is somehow have the settings saved for when I've plugged in all three monitors, so I can simply run one single script or setting, so it instantly loads up how I want it, or maybe even that this happens automatically when three displays are detected. I should add that I always plug in the monitors the same way.

What also bothers me is that I have this wallpaper enabled which spans all three monitors but when they are disconnected the wallpaper becomes a thin line on the laptop monitor with the rest of the screen black. I'd like to see only the middle part of that wallpaper to show in that case or maybe have another wallpaper load up automatically when external monitors are disconnected. I hope someone can help me or direct me in the right way to make this a better experience.

I did find a command line tool called disper of which i've read the man page and tried a bunch of commands but I don't think it's able to do what I want.

The paths to my wallpapers are:

/home/olm/Pictures/Wallpapers/3monitorwallpaper.jpg /home/olm/Pictures/Wallpapers/1monitorwallpaper.jpg

  • Those are two separate issues, the wallpaper and the screen setup. Although we can combine it in one script, it should be two questions, and possible other users will most likely only use the functionalities separately. Which would you consider the main part? About the screen setup; there is a quick option for which I will need your xrandr output, or a smarter snapshot script of which I am not sure I will make it this weekend :) Just let me know. – Jacob Vlijm Mar 17 '17 at 21:42
  • Thanks for helping out. I see you also editted the question so the grammar is better. Do I have to accept your changes or is it already approved automatically? The resolutions/screen arrangement setup is the most important part to me. Here is my xrandr output: pastebin.com/mTdehbGf The resolutions often fall back to 1920x1080, probably because my laptop monitor is 1920x1080 and my three external monitors are 1920x1200 – Odin Mugabe Mar 17 '17 at 23:39
  • Sorry, forgot about your post. Before I post my answer, just to check: does the command xrandr --output DP-2 --pos 0x0 && xrandr --output HDMI-0 --pos 1920x0 && xrandr --output DP-3 --pos 3840x0 && xrandr --output eDP-1-1 --pos 5760x0 setup your screens correctly? – Jacob Vlijm Mar 20 '17 at 11:54
  • It does. They are in the right order. My laptop is indeed to the right of my three monitors. However, I'd like to have the embedded laptop screen disabled when the three monitors are connected and the middle hdmi screen as a primary monitor. I'll read the xrandr manpage about this command. – Odin Mugabe Mar 20 '17 at 15:27
  • Ok so this command sets it how I want: xrandr --output DP-2 --pos 0x0 --mode 1920x1200 && xrandr --output HDMI-0 --pos 1920x0 --mode 1920x1200 && xrandr --output DP-3 --pos 3840x0 --mode 1920x1200 && xrandr --output eDP-1-1 --off. Now it would be nice to know how to trigger this when all three external monitors are detected as being connected. I'll also look for a command to set a certain wallpaper – Odin Mugabe Mar 20 '17 at 16:37
0

1. Script to run your command if four screens are connected

The script below is an edited version of this one.

What it does

Once per five seconds, it checks the number of connected screens. If the number changes, and the total number of connected screens is four, it runs the xrandr command we found in the comment(s).

How to use

  1. Copy the script into an empty file, save it as four_screens.py
  2. Test run the script from a terminal with the command:

    python3 /path/to/four_screens.py
    

    and connect your screens. After the fourth is connected, your screen setup should be made.

  3. If all works fine, add the script to Startup Applications: Dash > Startup Applications > Add. Add the command:

    python3 /path/to/four_screens.py
    
#!/usr/bin/env python3
import subprocess
import time

#--- set both commands (connect / disconnect) below
connect_command = "xrandr --output DP-2 --pos 0x0 --mode 1920x1200 "\
                  "&& xrandr --output HDMI-0 --pos 1920x0 --mode 1920x1200 "\
                  "&& xrandr --output DP-3 --pos 3840x0 --mode 1920x1200 "\
                  "&& xrandr --output eDP-1-1 --off"

disconnect_command = ""
#---

while True:
    time.sleep(5)
    try:
        subprocess.Popen(["xrandr"])
    except:
        pass
    else:
        break


# function to get the output of xrandr
def get(cmd): return subprocess.check_output(cmd).decode("utf-8")
# - to count the occurrenc of " connected "
def count_screens(xr): return xr.count(" connected ")
# - to run the connect / disconnect command(s)
def run_command(cmd): subprocess.Popen(["/bin/bash", "-c", cmd])

# first count
xr1 = None

while True:
    time.sleep(5)
    # second count
    xr2 = count_screens(get(["xrandr"]))
    # check if there is a change in the screen state
    if xr2 != xr1:
        if xr2 == 4:
            # command to run if connected (two screens)
            run_command(connect_command)
        elif xr2 == 1:
            # command to run if disconnected (one screen)
            # uncomment run_command(disconnect_command) to enable, then also comment out pass
            pass
            # run_command(disconnect_command)
    # set the second count as initial state for the next loop
    xr1 = xr2

Notes

  1. The script is extremely low on juice, it adds no noticeable burden whatsoever.
  2. In the same script, we can run the wallpaper change, but to do so, please post the (path to) the two wallpapers into your question.

2. Alternatively, a shortcut

If, for whatever reason, you'd prefer not to run a background script, you can run the same command through a keyboard shortcut:

Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

/bin/bash -c "xrandr --output DP-2 --pos 0x0 --mode 1920x1200 && xrandr --output HDMI-0 --pos 1920x0 --mode 1920x1200 && xrandr --output DP-3 --pos 3840x0 --mode 1920x1200 && xrandr --output eDP-1-1 --off"
  • Ok great! I added the paths to the wallpapers. – Odin Mugabe Mar 20 '17 at 22:53
  • The script seems to work most of the time but there's a chance that all four monitors get the same mirrored output when the script is running and and I connect all monitors. My theory about what happens is that the xrandr command sets the displays but right after, another mechanism sets them all mirrored. Depending on at what time in the loop the last one of the three displays is connected, the desktop might be set according to the xrandr settings or all four mirrored. – Odin Mugabe Mar 21 '17 at 0:55
  • Maybe it can be fixed by adding a small delay between the dectection of the 3 monitors and the xrandr command so the xrandr command always runs after the initial mirrored setting? – Odin Mugabe Mar 21 '17 at 0:56
  • @OdinMugabe Let's first fix this and then take care of the wallpaper. Please try this one: pastebin.com/C1CXV9EZ – Jacob Vlijm Mar 21 '17 at 6:28
  • It still only works about two thirds of the times when 4 monitors are detected. I've also noticed that my mouse becomes unresponsive for like 200 ms every 5 seconds because of the script. I'm not sure if this also would happen if 'd run the script as a startup program. I'm actually fine with having the xrandr command saved as a shell script so I can manually run it to setup my desktop, that's fine for me. It's already a huge improvement over what I had to do previously. – Odin Mugabe Mar 21 '17 at 10:38
0

In the meantime i've also found this handy command to change the wallpaper in gnome.

gsettings set org.gnome.desktop.background picture-uri file:///path/to/wallpaper.jpg

So now I use that together with the Xrandr commands in a script to setup my desktop every time I connect my monitors to the laptop.

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.