Automatically switch Xfce panel layout when plugging in a monitor

Xfce has great multi-monitor support. Different panels on each monitor, it all works great. I’ve used it with multiple monitors on my desktop for 5+ years. Love it.

But you run into some annoyances with laptops. Half the time I’m using my laptop on its own, and half the time I’m using it with an external monitor. You plug the monitor in for the first time, then you create a new panel for it and drag it over. But then when you unplug the monitor, that second panel doesn’t go away. Instead, it moves over to the main laptop screen and the only way to hide it is to delete it. Then when you plug the external monitor in again, you need to recreate it, readd all the items and so on.

Well, the Xubuntu team has made a tool called Xfce Panel Switch (Arch package: xfpanel-switch).

I’ll quickly walk though how to setup automatic switching.

Step One – Panel Configs

First, setup your panel structure the way you like it when you’re using just your laptop screen. Open Xfce Panel Switch, select Current Configuration, click Export, and save it as laptop.tar.bz2. Then plug your monitor in, setup your panels the way you like, select Current Configuration again, and export as externalmon.tar.bz2.

If you want to test out the panel switching, use the commands:

python3 /usr/share/xfpanel-switch/xfpanel-switch/panelconfig.py load /home/colin/laptop.tar.bz2 python3 /usr/share/xfpanel-switch/xfpanel-switch/panelconfig.py load /home/colin/externalmon.tar.bz2

Step Two – Udev and systemd

We could stop here and create a desktop shortcut we click each time, but where’s the fun is that?

Create a new udev rule to trigger our systemd service: /etc/udev/rules.d/95-monitor-hotplug.rules

ACTION=="change", KERNEL=="card0", SUBSYSTEM=="drm", RUN+="/usr/bin/systemctl start hot_plug.service"

Create our hot_plug service: /etc/systemd/system/hot_plug.service

[Unit] Description=Monitor hotplug [Service] Type=simple RemainAfterExit=no User=colin ExecStart=/usr/bin/bash /usr/local/bin/hotplug_monitor.sh [Install] WantedBy=multi-user.target

Replace colin with your own username

Create the script that our service executes: nano /usr/local/bin/hotplug_monitor.sh

#!/bin/bash # Replace colin with your username X_USER=colin export DISPLAY=:0 export XAUTHORITY=/home/$X_USER/.Xauthority export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus function connect() { # Remember to change the path to wherever you are storing your configs python3 /usr/share/xfpanel-switch/xfpanel-switch/panelconfig.py load /home/colin/externalmon.tar.bz2 } function disconnect() { # Remember to change the path to wherever you are storing your configs python3 /usr/share/xfpanel-switch/xfpanel-switch/panelconfig.py load /home/colin/laptop.tar.bz2 } # Replace card0-DP-1 with the card and port you are using if [ $(cat /sys/class/drm/card0-DP-1/status) == "connected" ] ; then connect elif [ $(cat /sys/class/drm/card0-DP-1/status) == "disconnected" ] ; then disconnect else exit fi

Remember to make it executable chmod +x /usr/local/bin/hotplug_monitor.sh

Important: you may need to replace card0-DP-1 in the above script with the card and port that you are using. I’m using VGA, for HDMI you probably need to check card0-HDMI-A-1 instead.

Also remember to change the paths to the location where you a storing your panel configs. I personally store them at /home/colin/.config/xfce4-panel-switcher/

(Optional) Step Three – Set monitor layout

Now that we have a script which will execute anything we like whenever we plug or unplug our monitor, lets also use it to have xrandr setup our monitor layout.

In the connect function, I have:

xrandr --output eDP1 --left-of DP1 --preferred --output eDP1 --primary

and in the disconnect function:

xrandr --output DP1 --off

This sets up my laptop monitor (eDP1) as my primary, and my VGA monitor (DP1) as a secondary monitor which is positioned to the right of it. You’ll have to change eDP1 and DP1 to reflect what your devices are. Look at the xrandr article on the Arch Wiki for more information.

Final thoughts

This saves me at least a couple minutes every time I plug my laptop in. Most of the time I didn’t even bother setting up panels on the secondary monitor because having to redo every time was getting old.

The obvious downside here is that every time you make any changes to your panel, you need to open the Xfce Panel Switcher app and export the new config to wherever it is you’re keeping them. But how often do you really need to change your panel layout? My desktop one hasn’t changed in years.

I also want to give thanks and credit to Iah on the Arch forums for posting how to detect the udev event for a monitor being plugged in.

Hope this helps!

Clicking notifications in Chrome causes the window to change workspaces Use MariaDB with PhpStorm