Multi monitors panel layouts and background switcher

Multi monitors panel layouts and background switcher

Introduction:
If you use Ubuntu MATE with several monitors, you might have figured out it can be tricky sometimes.

  • When turning on additional monitors, they will be panel-free. You can add/customize panels and docks to it but as soon as you turn an additional monitor off, those will go to the remaining monitor(s) and of course, you don't want that.
  • Turning on more than one monitor during a session gives me corruption on the added monitor space (Windows leave a trail etc...). Reloading/changing the desktop background fixes immediately the issue for whatever reason.

This little script aims to provide a workaround for both problems by switching between panel layouts and desktop background(s) for each situation (monitor 1, monitor 1 + 2 etc...) using a keyboard shortcut.

1 - Preparing panel layouts.

A - For the first monitor.
Edit your panel for your single monitor usage.
Open mate-tweak
Go to the panel section, click save as and name it solo.

B - For the second monitor.
Add your panels on monitor 2 and customize it. To do so, add a new panel on monitor 1 by right clicking on an existing panel, select add a new panel, then go to its properties, untick "extend" and drag it on monitor 2 while maintaining the left alt key and fill it with whatever applets you want.
Save your new dual monitor layout in mate-tweak and name it dual.

C - For the third monitor.
Repeat step B on monitor 3 but name it triple this time.

D - The Plank dock (optional).
If you want your Plank dock on more than one monitor, do this:
In a terminal window:
cd ~/.config/plank & mkdir dock2
cp dock1/launchers dock2/
plank -n dock2
Dock2 should then launch on top of Dock1 with the same launchers icons.
With an additional monitor turned on, hold ctrl and right click on the dock, go to Preferences in order to select the monitor this second dock will be displayed on.
It should now display on the second monitor.

You can repeat this with as many docks you want. Just edit dock2 to dock3 for monitor 3 etc...

Notes:

Layouts are stored in /usr/share/mate-panel/layouts. Customs layouts have "-tweak" added at the end of the filename given in mate-tweak. solo layout will become solo-tweak for instance.

[IMPORTANT]: at the time of the Ubuntu MATE 19.10 Eoan Ermine release (10/17/2019), switching to custom layouts made for several monitors is currently broken. Panels will go on top of each other and sometimes be blank if you use the same applets on your panels.
To fix it:
Go to /usr/share/mate-panel/layouts
If your custom layout is named "multimon", you will find multimon.panel and multimon.layout files.
You need to edit the *.layoutfile as root.
a) In order to put the right panels on the right monitors:
If you use two monitors, you will see this section at the top of the file:

[Toplevel toplevel-0]
size=28
expand=true
orientation=top

Adding monitor=1 as shown below will allow panel 2 to be on monitor 2.

[Toplevel toplevel-0]
size=28
expand=true
orientation=top
monitor=1

If you use three monitors, I assume there will be a Toplevel toplevel-1 section for your third panel. Adding monitor=2 to it should put panel 3 on monitor 3.

b) In order to fix the missing applets:
You need to rename the objects so that they don't share the same name.
If you locate two [Object briskmenu] for instance, rename the second one [Object briskmenu2].
Save and exit.

Now the panels and applets should load correctly when switching between custom layouts.

2 - The switching script

A - Creating the script
Copy this script, paste it in a text editor, edit the path to your background(s), save it and gave it a name. Make it executable (chmod +x filename in a terminal or tick the case in the permissions tab of the file properties) . It's currently written for 3 monitors as triple monitor setups seem to be quite popular.

#!/bin/bash
########## MATE panel switcher ##########
l='mate-tweak --layout'
bg='gsettings set org.mate.background picture-filename'
bgo='gsettings set org.mate.background picture-options'
n='notify-send -i mate -t 2000'
p='plank -n'

function solo()
{
$n 'MATE panel switcher' 'Switching to one monitor layout'
$l solo-tweak
$bg '/path/to/solomonitorbackground.png'
$bgo 'zoom'
pkill plank
sleep 1
$p dock1
}

function dual()
{
$n 'MATE panel switcher' 'Switching to dual monitors layout'
$l dual-tweak
$bg '/path/to/dualmonitorsbackground.png'
$bgo 'spanned'
pkill plank
sleep 1
$p dock1
$p dock2
}

function triple()
{
$n 'MATE panel switcher' 'Switching to triple monitors layout'
$l triple-tweak
$bg '/path/to/triplemonitorsbackground.png'
$bgo 'spanned'
pkill plank
sleep 1
$p dock1
$p dock2
$p dock3
}

if [ $(xrandr | grep -sw 'connected' | wc -l) == "1" ] ; then
solo
elif [ $(xrandr | grep -sw 'connected' | wc -l) == "2" ] ; then
dual
elif [ $(xrandr | grep -sw 'connected' | wc -l) == "3" ] ; then
triple
else
exit
fi

B - Assigning a keyboard shortcut to the script
Open mate-keybinding-properties
Add a new shortcut.
As a command, choose the /path/to/the/scriptname and apply.
Click on "disabled" in the shortcut column next to it and give it the key combination you want. One you will remember. I chose Alt+C.

C - Usage

  • Just enter your keyboard shortcut every time you turn on or off an additional monitor.
  • Code contributor vkareh provided an interesting solution in the forums for automating stuff when plugging several monitors: autorandr. As mentioned here you can use postswitch files in order to execute commands every time you plug/unplug a monitor. Sadly, it would only work when I switch from one to several monitors and not the other way around and seemed to open more and more panels/plank processes in the meantime. So I chose to stick with the manual workaround at the moment.

Notes:

  • Feel free to comment/remove Plank dock related lines if you don't use it.
  • Add other similar functions if you use more than 3 monitors.
  • The "spanned" picture option allows you to use background pictures designed for multi monitors with resolutions like 3840x1080 (2x 1080p monitors) or 5760x1080 (3x 1080p monitors) and stretch them all over the space. Use "zoom" instead if you want the same image repeated on all monitors.
  • pkill plank is there in order to kill docks so that docks from additional monitors don't sit on other docks when turning them off. I couldn't find a command to kill a specific dock so I killed them all. docks for the remaining monitors will automatically be reloaded one second later.

Example screenshots:
Default behavior with two monitors:


Expected behavior with two monitors (using script):

Back to one monitor (without script):
4
Back to one monitor (using script):
3

Enjoy !

6 Likes

Well done! I run triple monitors and have the exact same problems when on occasion I need to disconnect one.

Thanks very much!

Cheers

1 Like

for an easy way and for a full discription to how create multiple docks in plank check this video:

Hi Utsuro,

I think you are the person who can help me fix something. I have had the same external monitor attached to my UM 18.04 for a while. When I retired it from desktop duty I moved it to a different location. Here the external monitor is to the right of the laptop, whereas before it was on the left. I switched the positions around through the GUI tool in System menu. (I don't remember exactly now, but I may have had my panel on the external before, but now it is on the laptop screen.)

The geometry is messed up now. UM treats the laptop screen as 1920x1080, when it is really a 1600x900. The external is 1920x1080 and I think it is running at 1920x1080, but it may be running at 1600x900. I'm not sure. My sticky notes that were just above the panel have disappeared. When dialog boxes open by default in the upper left corner of the default display, it is invisible to me! The Keyring password dialog, for example, always opens there, invisible. It takes the password input because the cursor is in the text field by default, so I manage.

Other Open/Save dialogs also open in the corner, and they are unusable. In some cases a panel item is created, and I can right click and Move the box. I want to manually edit the config file, but I don't remember where it is, and what it is called now. Is it Xorg.conf? Any help would be greatly appreciated.

TIA

For anyone wondering on how to achieve better results using this script, instead of manually triggering it everytime you can add a startup script that detects monitor changes and that launch it for you.
I stumbled on this https://askubuntu.com/a/630335 anwser and changed it a bit to fit my needs.

Just write this python3 script anywhere you want:

#!/usr/bin/env python3
import subprocess
import time

#--- config parameters
command_path = "<path to the mate-panel switch script>"
refresh_time_sec = 1
#---

# Check and wait for the ["xrandr"] command to become available
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],
        stdout=subprocess.DEVNULL,
        stderr=subprocess.STDOUT
        )

# first count
xr1 = None

while True:
    time.sleep(refresh_time_sec)
    # second count
    xr2 = count_screens(get(["xrandr"]))
    # check if there is a change in the screen state
    if xr2 != xr1:
        # if so, run the script
        run_command(command_path)
    # set the second count as initial state for the next loop
    xr1 = xr2

Then add a new startup program by typing inside the command section:
python3 /path/to/this/python/script

In this way you are launching the script automatically everytime you connect/disconnect a monitor.
Hope it helps.

2 Likes

Hi there.
I just wanted to thank you for this post.

It really helped me out with my issue.
I have a triple monitor setup, i was getting triple panel at the top of my desktop when i switch to only one monitor.
That was really annoying.

This post really helped me out.

Thanks alot.

1 Like

Welcome @djangoman to the community!

Bit late to the party, but wanted to say thanks. I did notice when I tried it, that Mate actually names the profiles "ubuntu-mate--solo-tweak" and "ubuntu-mate--dual-tweak" so I just had to name the appropriately in the script. Not sure if that is a recent Mate change.

1 Like