2

I have setup a basic udev rule to detect when I connect or disconnect a mDP cable.

The file is /etc/udev/rules.d/95-monitor-hotplug.rules

KERNEL=="card0", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/var/run/gdm/auth-for-vazquez-OlbTje/database", RUN+="/usr/bin/arandr"

It should just launch arandr when their is a mDP cable connected or disconnected, but nothing happens. I have also reloaded the rules with:

udevadm control --reload-rules 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ This is how the problem was solved. With the links provided by @Gilles. I added the following code to my .profile then pointed the ENV{$XAUTHORITY}="/home/user/.Xauthority" and also added ACTION=="change" to the rules file. After that everything was working as it should. Thanks Gilles.

case $DISPLAY:$XAUTHORITY in
  :*:?*)
    # DISPLAY is set and points to a local display, and XAUTHORITY is
    # set, so merge the contents of `$XAUTHORITY` into ~/.Xauthority.
    XAUTHORITY=~/.Xauthority xauth merge "$XAUTHORITY";;
esac
  • Try to debug with sudo udevmonitor -e or watching /var/log/udev – tiktak May 08 '16 at 18:41
  • I don't have udevmonitor and is their anything I should be grepping for in /var/log/udev/? I see all the usual output from connecting and disconnecting the mDP cable – user3577138 May 08 '16 at 18:58

2 Answers2

4

An udev rule applies to the add action by default. The udev rule is on a graphics card, not on a monitor; so it runs when a graphics card is added to the system, which in practice means at boot time.

Plugging in a monitor results in a change action, not an add action. You can observe this by running udevadm monitor and plugging a monitor in. So the udev rule should specify a change action.

KERNEL=="card0", SUBSYSTEM=="drm", ACTION=="change", \
    ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/var/run/gdm/auth-for-vazquez-OlbTje/database", RUN+="/usr/bin/arandr"

Examples found on the web corroborate my understanding, e.g. codingtony whose monitor-hotplug.sh script may be of interest to you.

The file name under /var/run changes each time you reboot, so you should determine it automatically inside your script. This answer should help.

0

For anyone dealing with nvidia card, to make udev rule work, you will have to turn on the kernel mode setting

sudo grubby --update-kernel=ALL --args='nvidia-drm.modeset=1'