Automatically switch Xfce panel layout when pluggin in a monitor

This is my documentation of this [blog post](http://colinrrobinson.com/technology/linux/xfce/automatically-switch-xfce-panel-layout-plugging-monitor/#comment-144938) by Colin Robins.
I'm quite thankful for his explanation, and really happy that it now works for me.
His post is ~5 years old and I found a few things that don't work exactly the same anymore.
I tested everything on Xubuntu 20.04.

#### Panel configurations

The handy tool [Xfce Panel profiles](https://docs.xfce.org/apps/xfce4-panel-profiles/start) allows you to backup and export your different desired panel configurations.
I named mine `laptop.tar.bz2` and `external-monitor.tar.bz2`.
You can load a different panel profile from the command line with

   xfce4-panel-profiles load <profile>.tar.bz2

#### `udev` rule

`udev` is responsible for starting our service after connecting or disconnecting the external monitor.
To use it we have write a `udev` rule, i.e. put the following into the file `/etc/udev/rules.d/95-monitor-hotplug.rules`

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

So I'm no expert on `udev` but AFAIK this tells `udev` to start the service `hot_plug.service` whenever there is a "change" concerning the video card output.

#### `systemd` service

So now we still have to write the `systemd` service `hot_plug.service`.
[Here](https://linuxconfig.org/how-to-write-a-simple-systemd-service) are some basics about `systemd` services.
To create our service write the following into the file `/etc/systemd/system/hot_plug.service`

   [Unit]
   Description=Monitor hotplug

   [Service]
   Type=simple
   RemainAfterExit=no
   User=jonas
   ExecStart=/usr/bin/bash /usr/local/bin/hotplug_monitor.sh

   [Install]
   WantedBy=multi-user.target

*Replace jonas by your username*.
So this service calls a script `/usr/local/bin/hotplug_monitor.sh`, which will do the actual work.
For me this script is

   #!/bin/bash

   X_USER=jonas
   export DISPLAY=:0
   export XAUTHORITY=/home/$X_USER/.Xauthority
   export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus

   function connect()
   {
       xfce4-panel-profiles load /home/jonas/.config/xfce4-panel-profiles/external-monitor.tar.bz2
   }

   function disconnect()
   {
       xfce4-panel-profiles load /home/jonas/.config/xfce4-panel-profiles/laptop.tar.bz2
   }

   if [ $(cat /sys/class/drm/card0-HDMI-A-1/status) == "connected" ] ; then
       connect
   elif [ $(cat /sys/class/drm/card0-HDMI-A-1/status) == "disconnected" ] ; then
       disconnect
   else
       exit
   fi

Things you might want to adapt:

1. You should change `X_USER` to your username
2. I store the panel profiles in `/home/jonas/.config/xfce4-panel-profiles/`
3. Check if your external monitor is `card0-HDMI-A-1`. If you use VGA this might be something different.

#### Further adaptions

If you want you can add other things that should be done when connecting the external monitor.
I mostly use this with my Wacom tablet, so I also want to set the drawing area only on the tablet.
So I added the line

   # map the tablet input to the tablet's display
   xsetwacom --set "Wacom One Pen Display 13 Pen stylus" MapToOutput HDMI-A-0

To change the button on the pen to right click instead of middle click I use

   # map the button on the pen to right click
   xsetwacom --set "Wacom One Pen Display 13 Pen stylus" Button 2 "button +3"

tags: computers, udev, systemd, xfce